blob: b9605448c1c5455ffa7cd228c5aae9025fa567a2 [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"
Dan Gohman2048b852009-11-23 18:04:58 +000015#include "SelectionDAGBuilder.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "SDNodeDbgValue.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000017#include "llvm/ADT/BitVector.h"
David Blaikie6d9dbd52013-06-16 20:34:15 +000018#include "llvm/ADT/Optional.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"
Jakub Staszak81bfd712013-01-10 22:13:13 +000021#include "llvm/Analysis/BranchProbabilityInfo.h"
Chris Lattner8047d9a2009-12-24 00:37:38 +000022#include "llvm/Analysis/ConstantFolding.h"
Nadav Rotemc05d3062012-09-06 09:17:37 +000023#include "llvm/Analysis/ValueTracking.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000024#include "llvm/CodeGen/Analysis.h"
25#include "llvm/CodeGen/FastISel.h"
26#include "llvm/CodeGen/FunctionLoweringInfo.h"
27#include "llvm/CodeGen/GCMetadata.h"
28#include "llvm/CodeGen/GCStrategy.h"
29#include "llvm/CodeGen/MachineFrameInfo.h"
30#include "llvm/CodeGen/MachineFunction.h"
31#include "llvm/CodeGen/MachineInstrBuilder.h"
32#include "llvm/CodeGen/MachineJumpTableInfo.h"
33#include "llvm/CodeGen/MachineModuleInfo.h"
34#include "llvm/CodeGen/MachineRegisterInfo.h"
35#include "llvm/CodeGen/SelectionDAG.h"
Andrew Trick3d74dea2013-10-31 22:11:56 +000036#include "llvm/CodeGen/StackMaps.h"
Bill Wendling0bcbd1d2012-06-28 00:05:13 +000037#include "llvm/DebugInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000038#include "llvm/IR/CallingConv.h"
39#include "llvm/IR/Constants.h"
40#include "llvm/IR/DataLayout.h"
41#include "llvm/IR/DerivedTypes.h"
42#include "llvm/IR/Function.h"
43#include "llvm/IR/GlobalVariable.h"
44#include "llvm/IR/InlineAsm.h"
45#include "llvm/IR/Instructions.h"
46#include "llvm/IR/IntrinsicInst.h"
47#include "llvm/IR/Intrinsics.h"
48#include "llvm/IR/LLVMContext.h"
49#include "llvm/IR/Module.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000050#include "llvm/Support/CommandLine.h"
51#include "llvm/Support/Debug.h"
52#include "llvm/Support/ErrorHandling.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000053#include "llvm/Support/MathExtras.h"
54#include "llvm/Support/raw_ostream.h"
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000055#include "llvm/Target/TargetFrameLowering.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000056#include "llvm/Target/TargetInstrInfo.h"
Dale Johannesen49de9822009-02-05 01:49:45 +000057#include "llvm/Target/TargetIntrinsicInfo.h"
Owen Anderson243eb9e2011-12-08 22:15:21 +000058#include "llvm/Target/TargetLibraryInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000059#include "llvm/Target/TargetLowering.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000060#include "llvm/Target/TargetOptions.h"
Richard Sandifordac168b82013-08-12 10:28:10 +000061#include "llvm/Target/TargetSelectionDAGInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000062#include <algorithm>
63using namespace llvm;
64
Dale Johannesen601d3c02008-09-05 01:48:15 +000065/// LimitFloatPrecision - Generate low-precision inline sequences for
66/// some float libcalls (6, 8 or 12 bits).
67static unsigned LimitFloatPrecision;
68
69static cl::opt<unsigned, true>
70LimitFPPrecision("limit-float-precision",
71 cl::desc("Generate low-precision inline sequences "
72 "for some float libcalls"),
73 cl::location(LimitFloatPrecision),
74 cl::init(0));
75
Andrew Trickde91f3c2010-11-12 17:50:46 +000076// Limit the width of DAG chains. This is important in general to prevent
77// prevent DAG-based analysis from blowing up. For example, alias analysis and
78// load clustering may not complete in reasonable time. It is difficult to
79// recognize and avoid this situation within each individual analysis, and
80// future analyses are likely to have the same behavior. Limiting DAG width is
Andrew Trickb9e6fe12010-11-20 07:26:51 +000081// the safe approach, and will be especially important with global DAGs.
Andrew Trickde91f3c2010-11-12 17:50:46 +000082//
83// MaxParallelChains default is arbitrarily high to avoid affecting
84// optimization, but could be lowered to improve compile time. Any ld-ld-st-st
Andrew Trickb9e6fe12010-11-20 07:26:51 +000085// sequence over this should have been converted to llvm.memcpy by the
86// frontend. It easy to induce this behavior with .ll code such as:
87// %buffer = alloca [4096 x i8]
88// %data = load [4096 x i8]* %argPtr
89// store [4096 x i8] %data, [4096 x i8]* %buffer
Andrew Trick778583a2011-03-11 17:46:59 +000090static const unsigned MaxParallelChains = 64;
Andrew Trickde91f3c2010-11-12 17:50:46 +000091
Andrew Trickac6d9be2013-05-25 02:42:55 +000092static SDValue getCopyFromPartsVector(SelectionDAG &DAG, SDLoc DL,
Chris Lattner3ac18842010-08-24 23:20:40 +000093 const SDValue *Parts, unsigned NumParts,
Patrik Hagglundb9e12e52012-12-19 12:33:30 +000094 MVT PartVT, EVT ValueVT, const Value *V);
Michael J. Spencere70c5262010-10-16 08:25:21 +000095
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000096/// getCopyFromParts - Create a value that contains the specified legal parts
97/// combined into the value they represent. If the parts combine to a type
98/// larger then ValueVT then AssertOp can be used to specify whether the extra
99/// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT
100/// (ISD::AssertSext).
Andrew Trickac6d9be2013-05-25 02:42:55 +0000101static SDValue getCopyFromParts(SelectionDAG &DAG, SDLoc DL,
Dale Johannesen66978ee2009-01-31 02:22:37 +0000102 const SDValue *Parts,
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000103 unsigned NumParts, MVT PartVT, EVT ValueVT,
Bill Wendling12931302012-09-26 04:04:19 +0000104 const Value *V,
Duncan Sands0b3aa262009-01-28 14:42:54 +0000105 ISD::NodeType AssertOp = ISD::DELETED_NODE) {
Chris Lattner3ac18842010-08-24 23:20:40 +0000106 if (ValueVT.isVector())
Bill Wendling12931302012-09-26 04:04:19 +0000107 return getCopyFromPartsVector(DAG, DL, Parts, NumParts,
108 PartVT, ValueVT, V);
Michael J. Spencere70c5262010-10-16 08:25:21 +0000109
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000110 assert(NumParts > 0 && "No parts to assemble!");
Dan Gohmane9530ec2009-01-15 16:58:17 +0000111 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000112 SDValue Val = Parts[0];
113
114 if (NumParts > 1) {
115 // Assemble the value from multiple parts.
Chris Lattner3ac18842010-08-24 23:20:40 +0000116 if (ValueVT.isInteger()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000117 unsigned PartBits = PartVT.getSizeInBits();
118 unsigned ValueBits = ValueVT.getSizeInBits();
119
120 // Assemble the power of 2 part.
121 unsigned RoundParts = NumParts & (NumParts - 1) ?
122 1 << Log2_32(NumParts) : NumParts;
123 unsigned RoundBits = PartBits * RoundParts;
Owen Andersone50ed302009-08-10 22:56:29 +0000124 EVT RoundVT = RoundBits == ValueBits ?
Owen Anderson23b9b192009-08-12 00:36:31 +0000125 ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000126 SDValue Lo, Hi;
127
Owen Anderson23b9b192009-08-12 00:36:31 +0000128 EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2);
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000129
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000130 if (RoundParts > 2) {
Chris Lattner3ac18842010-08-24 23:20:40 +0000131 Lo = getCopyFromParts(DAG, DL, Parts, RoundParts / 2,
Bill Wendling12931302012-09-26 04:04:19 +0000132 PartVT, HalfVT, V);
Chris Lattner3ac18842010-08-24 23:20:40 +0000133 Hi = getCopyFromParts(DAG, DL, Parts + RoundParts / 2,
Bill Wendling12931302012-09-26 04:04:19 +0000134 RoundParts / 2, PartVT, HalfVT, V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000135 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000136 Lo = DAG.getNode(ISD::BITCAST, DL, HalfVT, Parts[0]);
137 Hi = DAG.getNode(ISD::BITCAST, DL, HalfVT, Parts[1]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000138 }
Bill Wendling3ea3c242009-12-22 02:10:19 +0000139
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000140 if (TLI.isBigEndian())
141 std::swap(Lo, Hi);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000142
Chris Lattner3ac18842010-08-24 23:20:40 +0000143 Val = DAG.getNode(ISD::BUILD_PAIR, DL, RoundVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000144
145 if (RoundParts < NumParts) {
146 // Assemble the trailing non-power-of-2 part.
147 unsigned OddParts = NumParts - RoundParts;
Owen Anderson23b9b192009-08-12 00:36:31 +0000148 EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits);
Chris Lattner3ac18842010-08-24 23:20:40 +0000149 Hi = getCopyFromParts(DAG, DL,
Bill Wendling12931302012-09-26 04:04:19 +0000150 Parts + RoundParts, OddParts, PartVT, OddVT, V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000151
152 // Combine the round and odd parts.
153 Lo = Val;
154 if (TLI.isBigEndian())
155 std::swap(Lo, Hi);
Owen Anderson23b9b192009-08-12 00:36:31 +0000156 EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Chris Lattner3ac18842010-08-24 23:20:40 +0000157 Hi = DAG.getNode(ISD::ANY_EXTEND, DL, TotalVT, Hi);
158 Hi = DAG.getNode(ISD::SHL, DL, TotalVT, Hi,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000159 DAG.getConstant(Lo.getValueType().getSizeInBits(),
Duncan Sands92abc622009-01-31 15:50:11 +0000160 TLI.getPointerTy()));
Chris Lattner3ac18842010-08-24 23:20:40 +0000161 Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, TotalVT, Lo);
162 Val = DAG.getNode(ISD::OR, DL, TotalVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000163 }
Eli Friedman2ac8b322009-05-20 06:02:09 +0000164 } else if (PartVT.isFloatingPoint()) {
165 // FP split into multiple FP parts (for ppcf128)
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000166 assert(ValueVT == EVT(MVT::ppcf128) && PartVT == MVT::f64 &&
Eli Friedman2ac8b322009-05-20 06:02:09 +0000167 "Unexpected split");
168 SDValue Lo, Hi;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000169 Lo = DAG.getNode(ISD::BITCAST, DL, EVT(MVT::f64), Parts[0]);
170 Hi = DAG.getNode(ISD::BITCAST, DL, EVT(MVT::f64), Parts[1]);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000171 if (TLI.isBigEndian())
172 std::swap(Lo, Hi);
Chris Lattner3ac18842010-08-24 23:20:40 +0000173 Val = DAG.getNode(ISD::BUILD_PAIR, DL, ValueVT, Lo, Hi);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000174 } else {
175 // FP split into integer parts (soft fp)
176 assert(ValueVT.isFloatingPoint() && PartVT.isInteger() &&
177 !PartVT.isVector() && "Unexpected split");
Owen Anderson23b9b192009-08-12 00:36:31 +0000178 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
Bill Wendling12931302012-09-26 04:04:19 +0000179 Val = getCopyFromParts(DAG, DL, Parts, NumParts, PartVT, IntVT, V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000180 }
181 }
182
183 // There is now one part, held in Val. Correct it to match ValueVT.
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000184 EVT PartEVT = Val.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000185
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000186 if (PartEVT == ValueVT)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000187 return Val;
188
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000189 if (PartEVT.isInteger() && ValueVT.isInteger()) {
190 if (ValueVT.bitsLT(PartEVT)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000191 // For a truncate, see if we have any information to
192 // indicate whether the truncated bits will always be
193 // zero or sign-extension.
194 if (AssertOp != ISD::DELETED_NODE)
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000195 Val = DAG.getNode(AssertOp, DL, PartEVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000196 DAG.getValueType(ValueVT));
Chris Lattner3ac18842010-08-24 23:20:40 +0000197 return DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000198 }
Chris Lattner3ac18842010-08-24 23:20:40 +0000199 return DAG.getNode(ISD::ANY_EXTEND, DL, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000200 }
201
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000202 if (PartEVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
Chris Lattner3ac18842010-08-24 23:20:40 +0000203 // FP_ROUND's are always exact here.
204 if (ValueVT.bitsLT(Val.getValueType()))
205 return DAG.getNode(ISD::FP_ROUND, DL, ValueVT, Val,
Pete Cooperf57e1c22012-01-17 01:54:07 +0000206 DAG.getTargetConstant(1, TLI.getPointerTy()));
Bill Wendling3ea3c242009-12-22 02:10:19 +0000207
Chris Lattner3ac18842010-08-24 23:20:40 +0000208 return DAG.getNode(ISD::FP_EXTEND, DL, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000209 }
210
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000211 if (PartEVT.getSizeInBits() == ValueVT.getSizeInBits())
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000212 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000213
Torok Edwinc23197a2009-07-14 16:55:14 +0000214 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000215}
216
Bill Wendling12931302012-09-26 04:04:19 +0000217/// getCopyFromPartsVector - Create a value that contains the specified legal
218/// parts combined into the value they represent. If the parts combine to a
219/// type larger then ValueVT then AssertOp can be used to specify whether the
220/// extra bits are known to be zero (ISD::AssertZext) or sign extended from
221/// ValueVT (ISD::AssertSext).
Andrew Trickac6d9be2013-05-25 02:42:55 +0000222static SDValue getCopyFromPartsVector(SelectionDAG &DAG, SDLoc DL,
Chris Lattner3ac18842010-08-24 23:20:40 +0000223 const SDValue *Parts, unsigned NumParts,
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000224 MVT PartVT, EVT ValueVT, const Value *V) {
Chris Lattner3ac18842010-08-24 23:20:40 +0000225 assert(ValueVT.isVector() && "Not a vector value");
226 assert(NumParts > 0 && "No parts to assemble!");
227 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
228 SDValue Val = Parts[0];
Michael J. Spencere70c5262010-10-16 08:25:21 +0000229
Chris Lattner3ac18842010-08-24 23:20:40 +0000230 // Handle a multi-element vector.
231 if (NumParts > 1) {
Patrik Hagglundee211d22012-12-19 11:53:21 +0000232 EVT IntermediateVT;
233 MVT RegisterVT;
Chris Lattner3ac18842010-08-24 23:20:40 +0000234 unsigned NumIntermediates;
235 unsigned NumRegs =
236 TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT,
237 NumIntermediates, RegisterVT);
238 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
239 NumParts = NumRegs; // Silence a compiler warning.
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000240 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
Patrik Hagglundee211d22012-12-19 11:53:21 +0000241 assert(RegisterVT == Parts[0].getSimpleValueType() &&
Chris Lattner3ac18842010-08-24 23:20:40 +0000242 "Part type doesn't match part!");
Michael J. Spencere70c5262010-10-16 08:25:21 +0000243
Chris Lattner3ac18842010-08-24 23:20:40 +0000244 // Assemble the parts into intermediate operands.
245 SmallVector<SDValue, 8> Ops(NumIntermediates);
246 if (NumIntermediates == NumParts) {
247 // If the register was not expanded, truncate or copy the value,
248 // as appropriate.
249 for (unsigned i = 0; i != NumParts; ++i)
250 Ops[i] = getCopyFromParts(DAG, DL, &Parts[i], 1,
Bill Wendling12931302012-09-26 04:04:19 +0000251 PartVT, IntermediateVT, V);
Chris Lattner3ac18842010-08-24 23:20:40 +0000252 } else if (NumParts > 0) {
253 // If the intermediate type was expanded, build the intermediate
254 // operands from the parts.
255 assert(NumParts % NumIntermediates == 0 &&
256 "Must expand into a divisible number of parts!");
257 unsigned Factor = NumParts / NumIntermediates;
258 for (unsigned i = 0; i != NumIntermediates; ++i)
259 Ops[i] = getCopyFromParts(DAG, DL, &Parts[i * Factor], Factor,
Bill Wendling12931302012-09-26 04:04:19 +0000260 PartVT, IntermediateVT, V);
Chris Lattner3ac18842010-08-24 23:20:40 +0000261 }
Michael J. Spencere70c5262010-10-16 08:25:21 +0000262
Chris Lattner3ac18842010-08-24 23:20:40 +0000263 // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the
264 // intermediate operands.
265 Val = DAG.getNode(IntermediateVT.isVector() ?
266 ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, DL,
267 ValueVT, &Ops[0], NumIntermediates);
268 }
Michael J. Spencere70c5262010-10-16 08:25:21 +0000269
Chris Lattner3ac18842010-08-24 23:20:40 +0000270 // There is now one part, held in Val. Correct it to match ValueVT.
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000271 EVT PartEVT = Val.getValueType();
Michael J. Spencere70c5262010-10-16 08:25:21 +0000272
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000273 if (PartEVT == ValueVT)
Chris Lattner3ac18842010-08-24 23:20:40 +0000274 return Val;
Michael J. Spencere70c5262010-10-16 08:25:21 +0000275
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000276 if (PartEVT.isVector()) {
Chris Lattnere6f7c262010-08-25 22:49:25 +0000277 // If the element type of the source/dest vectors are the same, but the
278 // parts vector has more elements than the value vector, then we have a
279 // vector widening case (e.g. <2 x float> -> <4 x float>). Extract the
280 // elements we want.
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000281 if (PartEVT.getVectorElementType() == ValueVT.getVectorElementType()) {
282 assert(PartEVT.getVectorNumElements() > ValueVT.getVectorNumElements() &&
Chris Lattnere6f7c262010-08-25 22:49:25 +0000283 "Cannot narrow, it would be a lossy transformation");
284 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, ValueVT, Val,
Tom Stellard425b76c2013-08-05 22:22:01 +0000285 DAG.getConstant(0, TLI.getVectorIdxTy()));
Michael J. Spencere70c5262010-10-16 08:25:21 +0000286 }
287
Chris Lattnere6f7c262010-08-25 22:49:25 +0000288 // Vector/Vector bitcast.
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000289 if (ValueVT.getSizeInBits() == PartEVT.getSizeInBits())
Nadav Rotem0b666362011-06-04 20:58:08 +0000290 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
291
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000292 assert(PartEVT.getVectorNumElements() == ValueVT.getVectorNumElements() &&
Nadav Rotem0b666362011-06-04 20:58:08 +0000293 "Cannot handle this kind of promotion");
294 // Promoted vector extract
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000295 bool Smaller = ValueVT.bitsLE(PartEVT);
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000296 return DAG.getNode((Smaller ? ISD::TRUNCATE : ISD::ANY_EXTEND),
297 DL, ValueVT, Val);
Nadav Rotem0b666362011-06-04 20:58:08 +0000298
Chris Lattnere6f7c262010-08-25 22:49:25 +0000299 }
Eric Christopher471e4222011-06-08 23:55:35 +0000300
Eric Christopher9aaa02a2011-06-01 19:55:10 +0000301 // Trivial bitcast if the types are the same size and the destination
302 // vector type is legal.
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000303 if (PartEVT.getSizeInBits() == ValueVT.getSizeInBits() &&
Eric Christopher9aaa02a2011-06-01 19:55:10 +0000304 TLI.isTypeLegal(ValueVT))
305 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
Michael J. Spencere70c5262010-10-16 08:25:21 +0000306
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000307 // Handle cases such as i8 -> <1 x i1>
Bill Wendling12931302012-09-26 04:04:19 +0000308 if (ValueVT.getVectorNumElements() != 1) {
309 LLVMContext &Ctx = *DAG.getContext();
310 Twine ErrMsg("non-trivial scalar-to-vector conversion");
311 if (const Instruction *I = dyn_cast_or_null<Instruction>(V)) {
312 if (const CallInst *CI = dyn_cast<CallInst>(I))
313 if (isa<InlineAsm>(CI->getCalledValue()))
314 ErrMsg = ErrMsg + ", possible invalid constraint for vector type";
315 Ctx.emitError(I, ErrMsg);
316 } else {
317 Ctx.emitError(ErrMsg);
318 }
Chad Rosierf0b07552013-05-01 19:49:26 +0000319 return DAG.getUNDEF(ValueVT);
Bill Wendling12931302012-09-26 04:04:19 +0000320 }
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000321
322 if (ValueVT.getVectorNumElements() == 1 &&
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000323 ValueVT.getVectorElementType() != PartEVT) {
324 bool Smaller = ValueVT.bitsLE(PartEVT);
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000325 Val = DAG.getNode((Smaller ? ISD::TRUNCATE : ISD::ANY_EXTEND),
326 DL, ValueVT.getScalarType(), Val);
327 }
328
Chris Lattner3ac18842010-08-24 23:20:40 +0000329 return DAG.getNode(ISD::BUILD_VECTOR, DL, ValueVT, Val);
330}
331
Andrew Trickac6d9be2013-05-25 02:42:55 +0000332static void getCopyToPartsVector(SelectionDAG &DAG, SDLoc dl,
Chris Lattnera13b8602010-08-24 23:10:06 +0000333 SDValue Val, SDValue *Parts, unsigned NumParts,
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000334 MVT PartVT, const Value *V);
Michael J. Spencere70c5262010-10-16 08:25:21 +0000335
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000336/// getCopyToParts - Create a series of nodes that contain the specified value
337/// split into legal parts. If the parts contain more bits than Val, then, for
338/// integers, ExtendKind can be used to specify how to generate the extra bits.
Andrew Trickac6d9be2013-05-25 02:42:55 +0000339static void getCopyToParts(SelectionDAG &DAG, SDLoc DL,
Bill Wendling3ea3c242009-12-22 02:10:19 +0000340 SDValue Val, SDValue *Parts, unsigned NumParts,
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000341 MVT PartVT, const Value *V,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000342 ISD::NodeType ExtendKind = ISD::ANY_EXTEND) {
Owen Andersone50ed302009-08-10 22:56:29 +0000343 EVT ValueVT = Val.getValueType();
Michael J. Spencere70c5262010-10-16 08:25:21 +0000344
Chris Lattnera13b8602010-08-24 23:10:06 +0000345 // Handle the vector case separately.
346 if (ValueVT.isVector())
Bill Wendlingf18eb582012-09-26 06:16:18 +0000347 return getCopyToPartsVector(DAG, DL, Val, Parts, NumParts, PartVT, V);
Michael J. Spencere70c5262010-10-16 08:25:21 +0000348
Chris Lattnera13b8602010-08-24 23:10:06 +0000349 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000350 unsigned PartBits = PartVT.getSizeInBits();
Dale Johannesen8a36f502009-02-25 22:39:13 +0000351 unsigned OrigNumParts = NumParts;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000352 assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!");
353
Chris Lattnera13b8602010-08-24 23:10:06 +0000354 if (NumParts == 0)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000355 return;
356
Chris Lattnera13b8602010-08-24 23:10:06 +0000357 assert(!ValueVT.isVector() && "Vector case handled elsewhere");
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000358 EVT PartEVT = PartVT;
359 if (PartEVT == ValueVT) {
Chris Lattnera13b8602010-08-24 23:10:06 +0000360 assert(NumParts == 1 && "No-op copy with multiple parts!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000361 Parts[0] = Val;
362 return;
363 }
364
Chris Lattnera13b8602010-08-24 23:10:06 +0000365 if (NumParts * PartBits > ValueVT.getSizeInBits()) {
366 // If the parts cover more bits than the value has, promote the value.
367 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
368 assert(NumParts == 1 && "Do not know what to promote to!");
369 Val = DAG.getNode(ISD::FP_EXTEND, DL, PartVT, Val);
370 } else {
Bill Wendling9e8ceb02012-02-23 23:25:25 +0000371 assert((PartVT.isInteger() || PartVT == MVT::x86mmx) &&
372 ValueVT.isInteger() &&
Michael J. Spencere70c5262010-10-16 08:25:21 +0000373 "Unknown mismatch!");
Chris Lattnera13b8602010-08-24 23:10:06 +0000374 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
375 Val = DAG.getNode(ExtendKind, DL, ValueVT, Val);
Bill Wendling9e8ceb02012-02-23 23:25:25 +0000376 if (PartVT == MVT::x86mmx)
377 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
Chris Lattnera13b8602010-08-24 23:10:06 +0000378 }
379 } else if (PartBits == ValueVT.getSizeInBits()) {
380 // Different types of the same size.
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000381 assert(NumParts == 1 && PartEVT != ValueVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000382 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
Chris Lattnera13b8602010-08-24 23:10:06 +0000383 } else if (NumParts * PartBits < ValueVT.getSizeInBits()) {
384 // If the parts cover less bits than value has, truncate the value.
Bill Wendling9e8ceb02012-02-23 23:25:25 +0000385 assert((PartVT.isInteger() || PartVT == MVT::x86mmx) &&
386 ValueVT.isInteger() &&
Chris Lattnera13b8602010-08-24 23:10:06 +0000387 "Unknown mismatch!");
388 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
389 Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
Bill Wendling9e8ceb02012-02-23 23:25:25 +0000390 if (PartVT == MVT::x86mmx)
391 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
Chris Lattnera13b8602010-08-24 23:10:06 +0000392 }
393
394 // The value may have changed - recompute ValueVT.
395 ValueVT = Val.getValueType();
396 assert(NumParts * PartBits == ValueVT.getSizeInBits() &&
397 "Failed to tile the value with PartVT!");
398
399 if (NumParts == 1) {
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000400 if (PartEVT != ValueVT) {
Bill Wendlingf18eb582012-09-26 06:16:18 +0000401 LLVMContext &Ctx = *DAG.getContext();
402 Twine ErrMsg("scalar-to-vector conversion failed");
403 if (const Instruction *I = dyn_cast_or_null<Instruction>(V)) {
404 if (const CallInst *CI = dyn_cast<CallInst>(I))
405 if (isa<InlineAsm>(CI->getCalledValue()))
406 ErrMsg = ErrMsg + ", possible invalid constraint for vector type";
407 Ctx.emitError(I, ErrMsg);
408 } else {
409 Ctx.emitError(ErrMsg);
410 }
411 }
412
Chris Lattnera13b8602010-08-24 23:10:06 +0000413 Parts[0] = Val;
414 return;
415 }
416
417 // Expand the value into multiple parts.
418 if (NumParts & (NumParts - 1)) {
419 // The number of parts is not a power of 2. Split off and copy the tail.
420 assert(PartVT.isInteger() && ValueVT.isInteger() &&
421 "Do not know what to expand to!");
422 unsigned RoundParts = 1 << Log2_32(NumParts);
423 unsigned RoundBits = RoundParts * PartBits;
424 unsigned OddParts = NumParts - RoundParts;
425 SDValue OddVal = DAG.getNode(ISD::SRL, DL, ValueVT, Val,
426 DAG.getIntPtrConstant(RoundBits));
Bill Wendlingf18eb582012-09-26 06:16:18 +0000427 getCopyToParts(DAG, DL, OddVal, Parts + RoundParts, OddParts, PartVT, V);
Chris Lattnera13b8602010-08-24 23:10:06 +0000428
429 if (TLI.isBigEndian())
430 // The odd parts were reversed by getCopyToParts - unreverse them.
431 std::reverse(Parts + RoundParts, Parts + NumParts);
432
433 NumParts = RoundParts;
434 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
435 Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
436 }
437
438 // The number of parts is a power of 2. Repeatedly bisect the value using
439 // EXTRACT_ELEMENT.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000440 Parts[0] = DAG.getNode(ISD::BITCAST, DL,
Chris Lattnera13b8602010-08-24 23:10:06 +0000441 EVT::getIntegerVT(*DAG.getContext(),
442 ValueVT.getSizeInBits()),
443 Val);
444
445 for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) {
446 for (unsigned i = 0; i < NumParts; i += StepSize) {
447 unsigned ThisBits = StepSize * PartBits / 2;
448 EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits);
449 SDValue &Part0 = Parts[i];
450 SDValue &Part1 = Parts[i+StepSize/2];
451
452 Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL,
453 ThisVT, Part0, DAG.getIntPtrConstant(1));
454 Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL,
455 ThisVT, Part0, DAG.getIntPtrConstant(0));
456
457 if (ThisBits == PartBits && ThisVT != PartVT) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000458 Part0 = DAG.getNode(ISD::BITCAST, DL, PartVT, Part0);
459 Part1 = DAG.getNode(ISD::BITCAST, DL, PartVT, Part1);
Chris Lattnera13b8602010-08-24 23:10:06 +0000460 }
461 }
462 }
463
464 if (TLI.isBigEndian())
465 std::reverse(Parts, Parts + OrigNumParts);
466}
467
468
469/// getCopyToPartsVector - Create a series of nodes that contain the specified
470/// value split into legal parts.
Andrew Trickac6d9be2013-05-25 02:42:55 +0000471static void getCopyToPartsVector(SelectionDAG &DAG, SDLoc DL,
Chris Lattnera13b8602010-08-24 23:10:06 +0000472 SDValue Val, SDValue *Parts, unsigned NumParts,
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000473 MVT PartVT, const Value *V) {
Chris Lattnera13b8602010-08-24 23:10:06 +0000474 EVT ValueVT = Val.getValueType();
475 assert(ValueVT.isVector() && "Not a vector");
476 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Michael J. Spencere70c5262010-10-16 08:25:21 +0000477
Chris Lattnera13b8602010-08-24 23:10:06 +0000478 if (NumParts == 1) {
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000479 EVT PartEVT = PartVT;
480 if (PartEVT == ValueVT) {
Chris Lattnere6f7c262010-08-25 22:49:25 +0000481 // Nothing to do.
482 } else if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) {
483 // Bitconvert vector->vector case.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000484 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
Chris Lattnere6f7c262010-08-25 22:49:25 +0000485 } else if (PartVT.isVector() &&
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000486 PartEVT.getVectorElementType() == ValueVT.getVectorElementType() &&
487 PartEVT.getVectorNumElements() > ValueVT.getVectorNumElements()) {
Chris Lattnere6f7c262010-08-25 22:49:25 +0000488 EVT ElementVT = PartVT.getVectorElementType();
489 // Vector widening case, e.g. <2 x float> -> <4 x float>. Shuffle in
490 // undef elements.
491 SmallVector<SDValue, 16> Ops;
492 for (unsigned i = 0, e = ValueVT.getVectorNumElements(); i != e; ++i)
493 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
Tom Stellard425b76c2013-08-05 22:22:01 +0000494 ElementVT, Val, DAG.getConstant(i,
495 TLI.getVectorIdxTy())));
Michael J. Spencere70c5262010-10-16 08:25:21 +0000496
Chris Lattnere6f7c262010-08-25 22:49:25 +0000497 for (unsigned i = ValueVT.getVectorNumElements(),
498 e = PartVT.getVectorNumElements(); i != e; ++i)
499 Ops.push_back(DAG.getUNDEF(ElementVT));
500
501 Val = DAG.getNode(ISD::BUILD_VECTOR, DL, PartVT, &Ops[0], Ops.size());
502
503 // FIXME: Use CONCAT for 2x -> 4x.
Michael J. Spencere70c5262010-10-16 08:25:21 +0000504
Chris Lattnere6f7c262010-08-25 22:49:25 +0000505 //SDValue UndefElts = DAG.getUNDEF(VectorTy);
506 //Val = DAG.getNode(ISD::CONCAT_VECTORS, DL, PartVT, Val, UndefElts);
Nadav Rotem0b666362011-06-04 20:58:08 +0000507 } else if (PartVT.isVector() &&
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000508 PartEVT.getVectorElementType().bitsGE(
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000509 ValueVT.getVectorElementType()) &&
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000510 PartEVT.getVectorNumElements() == ValueVT.getVectorNumElements()) {
Nadav Rotem0b666362011-06-04 20:58:08 +0000511
512 // Promoted vector extract
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000513 bool Smaller = PartEVT.bitsLE(ValueVT);
Nadav Rotemc6341e62011-06-19 08:49:38 +0000514 Val = DAG.getNode((Smaller ? ISD::TRUNCATE : ISD::ANY_EXTEND),
515 DL, PartVT, Val);
Nadav Rotem0b666362011-06-04 20:58:08 +0000516 } else{
Chris Lattnere6f7c262010-08-25 22:49:25 +0000517 // Vector -> scalar conversion.
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000518 assert(ValueVT.getVectorNumElements() == 1 &&
Chris Lattnere6f7c262010-08-25 22:49:25 +0000519 "Only trivial vector-to-scalar conversions should get here!");
520 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
Tom Stellard425b76c2013-08-05 22:22:01 +0000521 PartVT, Val, DAG.getConstant(0, TLI.getVectorIdxTy()));
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000522
523 bool Smaller = ValueVT.bitsLE(PartVT);
524 Val = DAG.getNode((Smaller ? ISD::TRUNCATE : ISD::ANY_EXTEND),
525 DL, PartVT, Val);
Chris Lattnera13b8602010-08-24 23:10:06 +0000526 }
Michael J. Spencere70c5262010-10-16 08:25:21 +0000527
Chris Lattnera13b8602010-08-24 23:10:06 +0000528 Parts[0] = Val;
529 return;
530 }
Michael J. Spencere70c5262010-10-16 08:25:21 +0000531
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000532 // Handle a multi-element vector.
Patrik Hagglundee211d22012-12-19 11:53:21 +0000533 EVT IntermediateVT;
534 MVT RegisterVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000535 unsigned NumIntermediates;
Owen Anderson23b9b192009-08-12 00:36:31 +0000536 unsigned NumRegs = TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT,
Devang Patel8f09bea2010-08-26 20:32:32 +0000537 IntermediateVT,
538 NumIntermediates, RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000539 unsigned NumElements = ValueVT.getVectorNumElements();
Michael J. Spencere70c5262010-10-16 08:25:21 +0000540
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000541 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
542 NumParts = NumRegs; // Silence a compiler warning.
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000543 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
Michael J. Spencere70c5262010-10-16 08:25:21 +0000544
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000545 // Split the vector into intermediate operands.
546 SmallVector<SDValue, 8> Ops(NumIntermediates);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000547 for (unsigned i = 0; i != NumIntermediates; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000548 if (IntermediateVT.isVector())
Chris Lattnera13b8602010-08-24 23:10:06 +0000549 Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000550 IntermediateVT, Val,
Tom Stellard425b76c2013-08-05 22:22:01 +0000551 DAG.getConstant(i * (NumElements / NumIntermediates),
552 TLI.getVectorIdxTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000553 else
Chris Lattnera13b8602010-08-24 23:10:06 +0000554 Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
Tom Stellard425b76c2013-08-05 22:22:01 +0000555 IntermediateVT, Val,
556 DAG.getConstant(i, TLI.getVectorIdxTy()));
Bill Wendling3ea3c242009-12-22 02:10:19 +0000557 }
Michael J. Spencere70c5262010-10-16 08:25:21 +0000558
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000559 // Split the intermediate operands into legal parts.
560 if (NumParts == NumIntermediates) {
561 // If the register was not expanded, promote or copy the value,
562 // as appropriate.
563 for (unsigned i = 0; i != NumParts; ++i)
Bill Wendlingf18eb582012-09-26 06:16:18 +0000564 getCopyToParts(DAG, DL, Ops[i], &Parts[i], 1, PartVT, V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000565 } else if (NumParts > 0) {
566 // If the intermediate type was expanded, split each the value into
567 // legal parts.
568 assert(NumParts % NumIntermediates == 0 &&
569 "Must expand into a divisible number of parts!");
570 unsigned Factor = NumParts / NumIntermediates;
571 for (unsigned i = 0; i != NumIntermediates; ++i)
Bill Wendlingf18eb582012-09-26 06:16:18 +0000572 getCopyToParts(DAG, DL, Ops[i], &Parts[i*Factor], Factor, PartVT, V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000573 }
574}
575
Dan Gohman462f6b52010-05-29 17:53:24 +0000576namespace {
577 /// RegsForValue - This struct represents the registers (physical or virtual)
578 /// that a particular set of values is assigned, and the type information
579 /// about the value. The most common situation is to represent one value at a
580 /// time, but struct or array values are handled element-wise as multiple
581 /// values. The splitting of aggregates is performed recursively, so that we
582 /// never have aggregate-typed registers. The values at this point do not
583 /// necessarily have legal types, so each value may require one or more
584 /// registers of some legal type.
585 ///
586 struct RegsForValue {
587 /// ValueVTs - The value types of the values, which may not be legal, and
588 /// may need be promoted or synthesized from one or more registers.
589 ///
590 SmallVector<EVT, 4> ValueVTs;
591
592 /// RegVTs - The value types of the registers. This is the same size as
593 /// ValueVTs and it records, for each value, what the type of the assigned
594 /// register or registers are. (Individual values are never synthesized
595 /// from more than one type of register.)
596 ///
597 /// With virtual registers, the contents of RegVTs is redundant with TLI's
598 /// getRegisterType member function, however when with physical registers
599 /// it is necessary to have a separate record of the types.
600 ///
Patrik Hagglund8963fec2012-12-19 12:23:01 +0000601 SmallVector<MVT, 4> RegVTs;
Dan Gohman462f6b52010-05-29 17:53:24 +0000602
603 /// Regs - This list holds the registers assigned to the values.
604 /// Each legal or promoted value requires one register, and each
605 /// expanded value requires multiple registers.
606 ///
607 SmallVector<unsigned, 4> Regs;
608
609 RegsForValue() {}
610
611 RegsForValue(const SmallVector<unsigned, 4> &regs,
Patrik Hagglund8963fec2012-12-19 12:23:01 +0000612 MVT regvt, EVT valuevt)
Dan Gohman462f6b52010-05-29 17:53:24 +0000613 : ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs) {}
614
Dan Gohman462f6b52010-05-29 17:53:24 +0000615 RegsForValue(LLVMContext &Context, const TargetLowering &tli,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000616 unsigned Reg, Type *Ty) {
Dan Gohman462f6b52010-05-29 17:53:24 +0000617 ComputeValueVTs(tli, Ty, ValueVTs);
618
619 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
620 EVT ValueVT = ValueVTs[Value];
621 unsigned NumRegs = tli.getNumRegisters(Context, ValueVT);
Patrik Hagglunddfcf33a2012-12-19 11:48:16 +0000622 MVT RegisterVT = tli.getRegisterType(Context, ValueVT);
Dan Gohman462f6b52010-05-29 17:53:24 +0000623 for (unsigned i = 0; i != NumRegs; ++i)
624 Regs.push_back(Reg + i);
625 RegVTs.push_back(RegisterVT);
626 Reg += NumRegs;
627 }
628 }
629
630 /// areValueTypesLegal - Return true if types of all the values are legal.
631 bool areValueTypesLegal(const TargetLowering &TLI) {
632 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
Patrik Hagglund8963fec2012-12-19 12:23:01 +0000633 MVT RegisterVT = RegVTs[Value];
Dan Gohman462f6b52010-05-29 17:53:24 +0000634 if (!TLI.isTypeLegal(RegisterVT))
635 return false;
636 }
637 return true;
638 }
639
640 /// append - Add the specified values to this one.
641 void append(const RegsForValue &RHS) {
642 ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end());
643 RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end());
644 Regs.append(RHS.Regs.begin(), RHS.Regs.end());
645 }
646
647 /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
648 /// this value and returns the result as a ValueVTs value. This uses
649 /// Chain/Flag as the input and updates them for the output Chain/Flag.
650 /// If the Flag pointer is NULL, no flag is used.
651 SDValue getCopyFromRegs(SelectionDAG &DAG, FunctionLoweringInfo &FuncInfo,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000652 SDLoc dl,
Bill Wendling12931302012-09-26 04:04:19 +0000653 SDValue &Chain, SDValue *Flag,
654 const Value *V = 0) const;
Dan Gohman462f6b52010-05-29 17:53:24 +0000655
656 /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
657 /// specified value into the registers specified by this object. This uses
658 /// Chain/Flag as the input and updates them for the output Chain/Flag.
659 /// If the Flag pointer is NULL, no flag is used.
Andrew Trickac6d9be2013-05-25 02:42:55 +0000660 void getCopyToRegs(SDValue Val, SelectionDAG &DAG, SDLoc dl,
Bill Wendlingf18eb582012-09-26 06:16:18 +0000661 SDValue &Chain, SDValue *Flag, const Value *V) const;
Dan Gohman462f6b52010-05-29 17:53:24 +0000662
663 /// AddInlineAsmOperands - Add this value to the specified inlineasm node
664 /// operand list. This adds the code marker, matching input operand index
665 /// (if applicable), and includes the number of values added into it.
666 void AddInlineAsmOperands(unsigned Kind,
667 bool HasMatching, unsigned MatchingIdx,
668 SelectionDAG &DAG,
669 std::vector<SDValue> &Ops) const;
670 };
671}
672
673/// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
674/// this value and returns the result as a ValueVT value. This uses
675/// Chain/Flag as the input and updates them for the output Chain/Flag.
676/// If the Flag pointer is NULL, no flag is used.
677SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG,
678 FunctionLoweringInfo &FuncInfo,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000679 SDLoc dl,
Bill Wendling12931302012-09-26 04:04:19 +0000680 SDValue &Chain, SDValue *Flag,
681 const Value *V) const {
Dan Gohman7da5d3f2010-07-26 18:15:41 +0000682 // A Value with type {} or [0 x %t] needs no registers.
683 if (ValueVTs.empty())
684 return SDValue();
685
Dan Gohman462f6b52010-05-29 17:53:24 +0000686 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
687
688 // Assemble the legal parts into the final values.
689 SmallVector<SDValue, 4> Values(ValueVTs.size());
690 SmallVector<SDValue, 8> Parts;
691 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
692 // Copy the legal parts from the registers.
693 EVT ValueVT = ValueVTs[Value];
694 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVT);
Patrik Hagglund8963fec2012-12-19 12:23:01 +0000695 MVT RegisterVT = RegVTs[Value];
Dan Gohman462f6b52010-05-29 17:53:24 +0000696
697 Parts.resize(NumRegs);
698 for (unsigned i = 0; i != NumRegs; ++i) {
699 SDValue P;
700 if (Flag == 0) {
701 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
702 } else {
703 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag);
704 *Flag = P.getValue(2);
705 }
706
707 Chain = P.getValue(1);
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000708 Parts[i] = P;
Dan Gohman462f6b52010-05-29 17:53:24 +0000709
710 // If the source register was virtual and if we know something about it,
711 // add an assert node.
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000712 if (!TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) ||
Cameron Zwariche1497b92011-02-24 10:00:08 +0000713 !RegisterVT.isInteger() || RegisterVT.isVector())
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000714 continue;
Cameron Zwariche1497b92011-02-24 10:00:08 +0000715
716 const FunctionLoweringInfo::LiveOutInfo *LOI =
717 FuncInfo.GetLiveOutRegInfo(Regs[Part+i]);
718 if (!LOI)
719 continue;
Dan Gohman462f6b52010-05-29 17:53:24 +0000720
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000721 unsigned RegSize = RegisterVT.getSizeInBits();
Cameron Zwariche1497b92011-02-24 10:00:08 +0000722 unsigned NumSignBits = LOI->NumSignBits;
723 unsigned NumZeroBits = LOI->KnownZero.countLeadingOnes();
Dan Gohman462f6b52010-05-29 17:53:24 +0000724
Quentin Colombeta3fb49c2013-06-18 20:14:39 +0000725 if (NumZeroBits == RegSize) {
726 // The current value is a zero.
727 // Explicitly express that as it would be easier for
728 // optimizations to kick in.
729 Parts[i] = DAG.getConstant(0, RegisterVT);
730 continue;
731 }
732
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000733 // FIXME: We capture more information than the dag can represent. For
734 // now, just use the tightest assertzext/assertsext possible.
735 bool isSExt = true;
736 EVT FromVT(MVT::Other);
737 if (NumSignBits == RegSize)
738 isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1
739 else if (NumZeroBits >= RegSize-1)
740 isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1
741 else if (NumSignBits > RegSize-8)
742 isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8
743 else if (NumZeroBits >= RegSize-8)
744 isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8
745 else if (NumSignBits > RegSize-16)
746 isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16
747 else if (NumZeroBits >= RegSize-16)
748 isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16
749 else if (NumSignBits > RegSize-32)
750 isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32
751 else if (NumZeroBits >= RegSize-32)
752 isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32
753 else
754 continue;
Dan Gohman462f6b52010-05-29 17:53:24 +0000755
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000756 // Add an assertion node.
757 assert(FromVT != MVT::Other);
758 Parts[i] = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
759 RegisterVT, P, DAG.getValueType(FromVT));
Dan Gohman462f6b52010-05-29 17:53:24 +0000760 }
761
762 Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(),
Bill Wendling12931302012-09-26 04:04:19 +0000763 NumRegs, RegisterVT, ValueVT, V);
Dan Gohman462f6b52010-05-29 17:53:24 +0000764 Part += NumRegs;
765 Parts.clear();
766 }
767
768 return DAG.getNode(ISD::MERGE_VALUES, dl,
769 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
770 &Values[0], ValueVTs.size());
771}
772
773/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
774/// specified value into the registers specified by this object. This uses
775/// Chain/Flag as the input and updates them for the output Chain/Flag.
776/// If the Flag pointer is NULL, no flag is used.
Andrew Trickac6d9be2013-05-25 02:42:55 +0000777void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, SDLoc dl,
Bill Wendlingf18eb582012-09-26 06:16:18 +0000778 SDValue &Chain, SDValue *Flag,
779 const Value *V) const {
Dan Gohman462f6b52010-05-29 17:53:24 +0000780 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
781
782 // Get the list of the values's legal parts.
783 unsigned NumRegs = Regs.size();
784 SmallVector<SDValue, 8> Parts(NumRegs);
785 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
786 EVT ValueVT = ValueVTs[Value];
787 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), ValueVT);
Patrik Hagglund8963fec2012-12-19 12:23:01 +0000788 MVT RegisterVT = RegVTs[Value];
Evan Cheng2766a472012-12-06 19:13:27 +0000789 ISD::NodeType ExtendKind =
790 TLI.isZExtFree(Val, RegisterVT)? ISD::ZERO_EXTEND: ISD::ANY_EXTEND;
Dan Gohman462f6b52010-05-29 17:53:24 +0000791
Chris Lattner3ac18842010-08-24 23:20:40 +0000792 getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value),
Evan Cheng2766a472012-12-06 19:13:27 +0000793 &Parts[Part], NumParts, RegisterVT, V, ExtendKind);
Dan Gohman462f6b52010-05-29 17:53:24 +0000794 Part += NumParts;
795 }
796
797 // Copy the parts into the registers.
798 SmallVector<SDValue, 8> Chains(NumRegs);
799 for (unsigned i = 0; i != NumRegs; ++i) {
800 SDValue Part;
801 if (Flag == 0) {
802 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
803 } else {
804 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag);
805 *Flag = Part.getValue(1);
806 }
807
808 Chains[i] = Part.getValue(0);
809 }
810
811 if (NumRegs == 1 || Flag)
812 // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
813 // flagged to it. That is the CopyToReg nodes and the user are considered
814 // a single scheduling unit. If we create a TokenFactor and return it as
815 // chain, then the TokenFactor is both a predecessor (operand) of the
816 // user as well as a successor (the TF operands are flagged to the user).
817 // c1, f1 = CopyToReg
818 // c2, f2 = CopyToReg
819 // c3 = TokenFactor c1, c2
820 // ...
821 // = op c3, ..., f2
822 Chain = Chains[NumRegs-1];
823 else
824 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs);
825}
826
827/// AddInlineAsmOperands - Add this value to the specified inlineasm node
828/// operand list. This adds the code marker and includes the number of
829/// values added into it.
830void RegsForValue::AddInlineAsmOperands(unsigned Code, bool HasMatching,
831 unsigned MatchingIdx,
832 SelectionDAG &DAG,
833 std::vector<SDValue> &Ops) const {
834 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
835
836 unsigned Flag = InlineAsm::getFlagWord(Code, Regs.size());
837 if (HasMatching)
838 Flag = InlineAsm::getFlagWordForMatchingOp(Flag, MatchingIdx);
Jakob Stoklund Olesen459b74b2011-10-12 23:37:29 +0000839 else if (!Regs.empty() &&
840 TargetRegisterInfo::isVirtualRegister(Regs.front())) {
841 // Put the register class of the virtual registers in the flag word. That
842 // way, later passes can recompute register class constraints for inline
843 // assembly as well as normal instructions.
844 // Don't do this for tied operands that can use the regclass information
845 // from the def.
846 const MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
847 const TargetRegisterClass *RC = MRI.getRegClass(Regs.front());
848 Flag = InlineAsm::getFlagWordForRegClass(Flag, RC->getID());
849 }
850
Dan Gohman462f6b52010-05-29 17:53:24 +0000851 SDValue Res = DAG.getTargetConstant(Flag, MVT::i32);
852 Ops.push_back(Res);
853
854 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
855 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVTs[Value]);
Patrik Hagglund8963fec2012-12-19 12:23:01 +0000856 MVT RegisterVT = RegVTs[Value];
Dan Gohman462f6b52010-05-29 17:53:24 +0000857 for (unsigned i = 0; i != NumRegs; ++i) {
858 assert(Reg < Regs.size() && "Mismatch in # registers expected");
859 Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT));
860 }
861 }
862}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000863
Owen Anderson243eb9e2011-12-08 22:15:21 +0000864void SelectionDAGBuilder::init(GCFunctionInfo *gfi, AliasAnalysis &aa,
865 const TargetLibraryInfo *li) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000866 AA = &aa;
867 GFI = gfi;
Owen Anderson243eb9e2011-12-08 22:15:21 +0000868 LibInfo = li;
Micah Villmow3574eca2012-10-08 16:38:25 +0000869 TD = DAG.getTarget().getDataLayout();
Richard Smithcb1f68d2012-08-22 00:42:39 +0000870 Context = DAG.getContext();
Bill Wendling4ed1fb02011-10-15 01:00:26 +0000871 LPadToCallSiteMap.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000872}
873
Dan Gohmanb02b62a2010-04-14 18:24:06 +0000874/// clear - Clear out the current SelectionDAG and the associated
Dan Gohman2048b852009-11-23 18:04:58 +0000875/// state and prepare this SelectionDAGBuilder object to be used
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000876/// for a new block. This doesn't clear out information about
877/// additional blocks that are needed to complete switch lowering
878/// or PHI node updating; that information is cleared out as it is
879/// consumed.
Dan Gohman2048b852009-11-23 18:04:58 +0000880void SelectionDAGBuilder::clear() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000881 NodeMap.clear();
Devang Patel9126c0d2010-06-01 19:59:01 +0000882 UnusedArgNodeMap.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000883 PendingLoads.clear();
884 PendingExports.clear();
Andrew Trickea5db0c2013-05-25 02:20:36 +0000885 CurInst = NULL;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000886 HasTailCall = false;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000887}
888
Devang Patel23385752011-05-23 17:44:13 +0000889/// clearDanglingDebugInfo - Clear the dangling debug information
Benjamin Kramerd9b0b022012-06-02 10:20:22 +0000890/// map. This function is separated from the clear so that debug
Devang Patel23385752011-05-23 17:44:13 +0000891/// information that is dangling in a basic block can be properly
892/// resolved in a different basic block. This allows the
893/// SelectionDAG to resolve dangling debug information attached
894/// to PHI nodes.
895void SelectionDAGBuilder::clearDanglingDebugInfo() {
896 DanglingDebugInfoMap.clear();
897}
898
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000899/// getRoot - Return the current virtual root of the Selection DAG,
900/// flushing any PendingLoad items. This must be done before emitting
901/// a store or any other node that may need to be ordered after any
902/// prior load instructions.
903///
Dan Gohman2048b852009-11-23 18:04:58 +0000904SDValue SelectionDAGBuilder::getRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000905 if (PendingLoads.empty())
906 return DAG.getRoot();
907
908 if (PendingLoads.size() == 1) {
909 SDValue Root = PendingLoads[0];
910 DAG.setRoot(Root);
911 PendingLoads.clear();
912 return Root;
913 }
914
915 // Otherwise, we have to make a token factor node.
Andrew Trickac6d9be2013-05-25 02:42:55 +0000916 SDValue Root = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000917 &PendingLoads[0], PendingLoads.size());
918 PendingLoads.clear();
919 DAG.setRoot(Root);
920 return Root;
921}
922
923/// getControlRoot - Similar to getRoot, but instead of flushing all the
924/// PendingLoad items, flush all the PendingExports items. It is necessary
925/// to do this before emitting a terminator instruction.
926///
Dan Gohman2048b852009-11-23 18:04:58 +0000927SDValue SelectionDAGBuilder::getControlRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000928 SDValue Root = DAG.getRoot();
929
930 if (PendingExports.empty())
931 return Root;
932
933 // Turn all of the CopyToReg chains into one factored node.
934 if (Root.getOpcode() != ISD::EntryToken) {
935 unsigned i = 0, e = PendingExports.size();
936 for (; i != e; ++i) {
937 assert(PendingExports[i].getNode()->getNumOperands() > 1);
938 if (PendingExports[i].getNode()->getOperand(0) == Root)
939 break; // Don't add the root if we already indirectly depend on it.
940 }
941
942 if (i == e)
943 PendingExports.push_back(Root);
944 }
945
Andrew Trickac6d9be2013-05-25 02:42:55 +0000946 Root = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000947 &PendingExports[0],
948 PendingExports.size());
949 PendingExports.clear();
950 DAG.setRoot(Root);
951 return Root;
952}
953
Dan Gohman46510a72010-04-15 01:51:59 +0000954void SelectionDAGBuilder::visit(const Instruction &I) {
Dan Gohmanc105a2b2010-04-22 20:55:53 +0000955 // Set up outgoing PHI node register values before emitting the terminator.
956 if (isa<TerminatorInst>(&I))
957 HandlePHINodesInSuccessorBlocks(I.getParent());
958
Andrew Trickdd0fb012013-05-25 03:08:10 +0000959 ++SDNodeOrder;
960
Andrew Trickea5db0c2013-05-25 02:20:36 +0000961 CurInst = &I;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000962
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000963 visit(I.getOpcode(), I);
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000964
Dan Gohman92884f72010-04-20 15:03:56 +0000965 if (!isa<TerminatorInst>(&I) && !HasTailCall)
966 CopyToExportRegsIfNeeded(&I);
967
Andrew Trickea5db0c2013-05-25 02:20:36 +0000968 CurInst = NULL;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000969}
970
Dan Gohmanba5be5c2010-04-20 15:00:41 +0000971void SelectionDAGBuilder::visitPHI(const PHINode &) {
972 llvm_unreachable("SelectionDAGBuilder shouldn't visit PHI nodes!");
973}
974
Dan Gohman46510a72010-04-15 01:51:59 +0000975void SelectionDAGBuilder::visit(unsigned Opcode, const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000976 // Note: this doesn't use InstVisitor, because it has to work with
977 // ConstantExpr's in addition to instructions.
978 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000979 default: llvm_unreachable("Unknown instruction type encountered!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000980 // Build the switch statement using the Instruction.def file.
981#define HANDLE_INST(NUM, OPCODE, CLASS) \
Galina Kistanova72ea0c92012-07-19 04:50:12 +0000982 case Instruction::OPCODE: visit##OPCODE((const CLASS&)I); break;
Chandler Carruth0b8c9a82013-01-02 11:36:10 +0000983#include "llvm/IR/Instruction.def"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000984 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000985}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000986
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000987// resolveDanglingDebugInfo - if we saw an earlier dbg_value referring to V,
988// generate the debug data structures now that we've seen its definition.
989void SelectionDAGBuilder::resolveDanglingDebugInfo(const Value *V,
990 SDValue Val) {
991 DanglingDebugInfo &DDI = DanglingDebugInfoMap[V];
Devang Patel4cf81c42010-08-26 23:35:15 +0000992 if (DDI.getDI()) {
993 const DbgValueInst *DI = DDI.getDI();
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000994 DebugLoc dl = DDI.getdl();
995 unsigned DbgSDNodeOrder = DDI.getSDNodeOrder();
Devang Patel4cf81c42010-08-26 23:35:15 +0000996 MDNode *Variable = DI->getVariable();
997 uint64_t Offset = DI->getOffset();
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000998 SDDbgValue *SDV;
999 if (Val.getNode()) {
Devang Patel78a06e52010-08-25 20:39:26 +00001000 if (!EmitFuncArgumentDbgValue(V, Variable, Offset, Val)) {
Dale Johannesenbdc09d92010-07-16 00:02:08 +00001001 SDV = DAG.getDbgValue(Variable, Val.getNode(),
1002 Val.getResNo(), Offset, dl, DbgSDNodeOrder);
1003 DAG.AddDbgValue(SDV, Val.getNode(), false);
1004 }
Owen Anderson95771af2011-02-25 21:41:48 +00001005 } else
Adrian Prantl5da4e4f2013-05-22 18:02:19 +00001006 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
Dale Johannesenbdc09d92010-07-16 00:02:08 +00001007 DanglingDebugInfoMap[V] = DanglingDebugInfo();
1008 }
1009}
1010
Nick Lewycky8de34002011-09-30 22:19:53 +00001011/// getValue - Return an SDValue for the given Value.
Dan Gohman2048b852009-11-23 18:04:58 +00001012SDValue SelectionDAGBuilder::getValue(const Value *V) {
Dan Gohman28a17352010-07-01 01:59:43 +00001013 // If we already have an SDValue for this value, use it. It's important
1014 // to do this first, so that we don't create a CopyFromReg if we already
1015 // have a regular SDValue.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001016 SDValue &N = NodeMap[V];
1017 if (N.getNode()) return N;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001018
Dan Gohman28a17352010-07-01 01:59:43 +00001019 // If there's a virtual register allocated and initialized for this
1020 // value, use it.
1021 DenseMap<const Value *, unsigned>::iterator It = FuncInfo.ValueMap.find(V);
1022 if (It != FuncInfo.ValueMap.end()) {
1023 unsigned InReg = It->second;
Bill Wendlingba54bca2013-06-19 21:36:55 +00001024 RegsForValue RFV(*DAG.getContext(), *TM.getTargetLowering(),
1025 InReg, V->getType());
Dan Gohman28a17352010-07-01 01:59:43 +00001026 SDValue Chain = DAG.getEntryNode();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001027 N = RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, NULL, V);
Devang Patel8f314282011-01-25 18:09:58 +00001028 resolveDanglingDebugInfo(V, N);
1029 return N;
Dan Gohman28a17352010-07-01 01:59:43 +00001030 }
1031
1032 // Otherwise create a new SDValue and remember it.
1033 SDValue Val = getValueImpl(V);
1034 NodeMap[V] = Val;
Dale Johannesenbdc09d92010-07-16 00:02:08 +00001035 resolveDanglingDebugInfo(V, Val);
Dan Gohman28a17352010-07-01 01:59:43 +00001036 return Val;
1037}
1038
1039/// getNonRegisterValue - Return an SDValue for the given Value, but
1040/// don't look in FuncInfo.ValueMap for a virtual register.
1041SDValue SelectionDAGBuilder::getNonRegisterValue(const Value *V) {
1042 // If we already have an SDValue for this value, use it.
1043 SDValue &N = NodeMap[V];
1044 if (N.getNode()) return N;
1045
1046 // Otherwise create a new SDValue and remember it.
1047 SDValue Val = getValueImpl(V);
1048 NodeMap[V] = Val;
Dale Johannesenbdc09d92010-07-16 00:02:08 +00001049 resolveDanglingDebugInfo(V, Val);
Dan Gohman28a17352010-07-01 01:59:43 +00001050 return Val;
1051}
1052
Dale Johannesenbdc09d92010-07-16 00:02:08 +00001053/// getValueImpl - Helper function for getValue and getNonRegisterValue.
Dan Gohman28a17352010-07-01 01:59:43 +00001054/// Create an SDValue for the given value.
1055SDValue SelectionDAGBuilder::getValueImpl(const Value *V) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00001056 const TargetLowering *TLI = TM.getTargetLowering();
1057
Dan Gohman383b5f62010-04-17 15:32:28 +00001058 if (const Constant *C = dyn_cast<Constant>(V)) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00001059 EVT VT = TLI->getValueType(V->getType(), true);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001060
Dan Gohman383b5f62010-04-17 15:32:28 +00001061 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C))
Dan Gohman28a17352010-07-01 01:59:43 +00001062 return DAG.getConstant(*CI, VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001063
Dan Gohman383b5f62010-04-17 15:32:28 +00001064 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001065 return DAG.getGlobalAddress(GV, getCurSDLoc(), VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001066
Matt Arsenault4fe5b642013-11-16 20:24:41 +00001067 if (isa<ConstantPointerNull>(C)) {
1068 unsigned AS = V->getType()->getPointerAddressSpace();
1069 return DAG.getConstant(0, TLI->getPointerTy(AS));
1070 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001071
Dan Gohman383b5f62010-04-17 15:32:28 +00001072 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Dan Gohman28a17352010-07-01 01:59:43 +00001073 return DAG.getConstantFP(*CFP, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001074
Nate Begeman9008ca62009-04-27 18:41:29 +00001075 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
Dan Gohman28a17352010-07-01 01:59:43 +00001076 return DAG.getUNDEF(VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001077
Dan Gohman383b5f62010-04-17 15:32:28 +00001078 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001079 visit(CE->getOpcode(), *CE);
1080 SDValue N1 = NodeMap[V];
Dan Gohmanac7d05c2010-04-16 16:55:18 +00001081 assert(N1.getNode() && "visit didn't populate the NodeMap!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001082 return N1;
1083 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001084
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001085 if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
1086 SmallVector<SDValue, 4> Constants;
1087 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
1088 OI != OE; ++OI) {
1089 SDNode *Val = getValue(*OI).getNode();
Dan Gohmaned48caf2009-09-08 01:44:02 +00001090 // If the operand is an empty aggregate, there are no values.
1091 if (!Val) continue;
1092 // Add each leaf value from the operand to the Constants list
1093 // to form a flattened list of all the values.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001094 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
1095 Constants.push_back(SDValue(Val, i));
1096 }
Bill Wendling87710f02009-12-21 23:47:40 +00001097
Bill Wendling4533cac2010-01-28 21:51:40 +00001098 return DAG.getMergeValues(&Constants[0], Constants.size(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001099 getCurSDLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001100 }
Stephen Lin155615d2013-07-08 00:37:03 +00001101
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001102 if (const ConstantDataSequential *CDS =
1103 dyn_cast<ConstantDataSequential>(C)) {
1104 SmallVector<SDValue, 4> Ops;
Chris Lattner0f193b82012-01-25 01:27:20 +00001105 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001106 SDNode *Val = getValue(CDS->getElementAsConstant(i)).getNode();
1107 // Add each leaf value from the operand to the Constants list
1108 // to form a flattened list of all the values.
1109 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
1110 Ops.push_back(SDValue(Val, i));
1111 }
1112
1113 if (isa<ArrayType>(CDS->getType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001114 return DAG.getMergeValues(&Ops[0], Ops.size(), getCurSDLoc());
1115 return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurSDLoc(),
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001116 VT, &Ops[0], Ops.size());
1117 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001118
Duncan Sands1df98592010-02-16 11:11:14 +00001119 if (C->getType()->isStructTy() || C->getType()->isArrayTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001120 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
1121 "Unknown struct or array constant!");
1122
Owen Andersone50ed302009-08-10 22:56:29 +00001123 SmallVector<EVT, 4> ValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00001124 ComputeValueVTs(*TLI, C->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001125 unsigned NumElts = ValueVTs.size();
1126 if (NumElts == 0)
1127 return SDValue(); // empty struct
1128 SmallVector<SDValue, 4> Constants(NumElts);
1129 for (unsigned i = 0; i != NumElts; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00001130 EVT EltVT = ValueVTs[i];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001131 if (isa<UndefValue>(C))
Dale Johannesene8d72302009-02-06 23:05:02 +00001132 Constants[i] = DAG.getUNDEF(EltVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001133 else if (EltVT.isFloatingPoint())
1134 Constants[i] = DAG.getConstantFP(0, EltVT);
1135 else
1136 Constants[i] = DAG.getConstant(0, EltVT);
1137 }
Bill Wendling87710f02009-12-21 23:47:40 +00001138
Bill Wendling4533cac2010-01-28 21:51:40 +00001139 return DAG.getMergeValues(&Constants[0], NumElts,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001140 getCurSDLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001141 }
1142
Dan Gohman383b5f62010-04-17 15:32:28 +00001143 if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
Dan Gohman29cbade2009-11-20 23:18:13 +00001144 return DAG.getBlockAddress(BA, VT);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001145
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001146 VectorType *VecTy = cast<VectorType>(V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001147 unsigned NumElements = VecTy->getNumElements();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001148
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001149 // Now that we know the number and type of the elements, get that number of
1150 // elements into the Ops array based on what kind of constant it is.
1151 SmallVector<SDValue, 16> Ops;
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001152 if (const ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001153 for (unsigned i = 0; i != NumElements; ++i)
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001154 Ops.push_back(getValue(CV->getOperand(i)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001155 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00001156 assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!");
Bill Wendlingba54bca2013-06-19 21:36:55 +00001157 EVT EltVT = TLI->getValueType(VecTy->getElementType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001158
1159 SDValue Op;
Nate Begeman9008ca62009-04-27 18:41:29 +00001160 if (EltVT.isFloatingPoint())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001161 Op = DAG.getConstantFP(0, EltVT);
1162 else
1163 Op = DAG.getConstant(0, EltVT);
1164 Ops.assign(NumElements, Op);
1165 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001166
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001167 // Create a BUILD_VECTOR node.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001168 return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00001169 VT, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001170 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001171
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001172 // If this is a static alloca, generate it as the frameindex instead of
1173 // computation.
1174 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
1175 DenseMap<const AllocaInst*, int>::iterator SI =
1176 FuncInfo.StaticAllocaMap.find(AI);
1177 if (SI != FuncInfo.StaticAllocaMap.end())
Bill Wendlingba54bca2013-06-19 21:36:55 +00001178 return DAG.getFrameIndex(SI->second, TLI->getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001179 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001180
Dan Gohman28a17352010-07-01 01:59:43 +00001181 // If this is an instruction which fast-isel has deferred, select it now.
1182 if (const Instruction *Inst = dyn_cast<Instruction>(V)) {
Dan Gohman84023e02010-07-10 09:00:22 +00001183 unsigned InReg = FuncInfo.InitializeRegForValue(Inst);
Bill Wendlingba54bca2013-06-19 21:36:55 +00001184 RegsForValue RFV(*DAG.getContext(), *TLI, InReg, Inst->getType());
Dan Gohman84023e02010-07-10 09:00:22 +00001185 SDValue Chain = DAG.getEntryNode();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001186 return RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, NULL, V);
Dan Gohman28a17352010-07-01 01:59:43 +00001187 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001188
Dan Gohman28a17352010-07-01 01:59:43 +00001189 llvm_unreachable("Can't get register for value!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001190}
1191
Dan Gohman46510a72010-04-15 01:51:59 +00001192void SelectionDAGBuilder::visitRet(const ReturnInst &I) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00001193 const TargetLowering *TLI = TM.getTargetLowering();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001194 SDValue Chain = getControlRoot();
1195 SmallVector<ISD::OutputArg, 8> Outs;
Dan Gohmanc9403652010-07-07 15:54:55 +00001196 SmallVector<SDValue, 8> OutVals;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001197
Dan Gohman7451d3e2010-05-29 17:03:36 +00001198 if (!FuncInfo.CanLowerReturn) {
1199 unsigned DemoteReg = FuncInfo.DemoteRegister;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001200 const Function *F = I.getParent()->getParent();
1201
1202 // Emit a store of the return value through the virtual register.
1203 // Leave Outs empty so that LowerReturn won't try to load return
1204 // registers the usual way.
1205 SmallVector<EVT, 1> PtrValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00001206 ComputeValueVTs(*TLI, PointerType::getUnqual(F->getReturnType()),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001207 PtrValueVTs);
1208
1209 SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]);
1210 SDValue RetOp = getValue(I.getOperand(0));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001211
Owen Andersone50ed302009-08-10 22:56:29 +00001212 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001213 SmallVector<uint64_t, 4> Offsets;
Bill Wendlingba54bca2013-06-19 21:36:55 +00001214 ComputeValueVTs(*TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets);
Dan Gohman7ea1ca62008-10-21 20:00:42 +00001215 unsigned NumValues = ValueVTs.size();
Dan Gohman7ea1ca62008-10-21 20:00:42 +00001216
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001217 SmallVector<SDValue, 4> Chains(NumValues);
Bill Wendling87710f02009-12-21 23:47:40 +00001218 for (unsigned i = 0; i != NumValues; ++i) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001219 SDValue Add = DAG.getNode(ISD::ADD, getCurSDLoc(),
Chris Lattnera13b8602010-08-24 23:10:06 +00001220 RetPtr.getValueType(), RetPtr,
1221 DAG.getIntPtrConstant(Offsets[i]));
Bill Wendling87710f02009-12-21 23:47:40 +00001222 Chains[i] =
Andrew Trickac6d9be2013-05-25 02:42:55 +00001223 DAG.getStore(Chain, getCurSDLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001224 SDValue(RetOp.getNode(), RetOp.getResNo() + i),
Chris Lattner84bd98a2010-09-21 18:58:22 +00001225 // FIXME: better loc info would be nice.
1226 Add, MachinePointerInfo(), false, false, 0);
Bill Wendling87710f02009-12-21 23:47:40 +00001227 }
1228
Andrew Trickac6d9be2013-05-25 02:42:55 +00001229 Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001230 MVT::Other, &Chains[0], NumValues);
Chris Lattner25d58372010-02-28 18:53:13 +00001231 } else if (I.getNumOperands() != 0) {
1232 SmallVector<EVT, 4> ValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00001233 ComputeValueVTs(*TLI, I.getOperand(0)->getType(), ValueVTs);
Chris Lattner25d58372010-02-28 18:53:13 +00001234 unsigned NumValues = ValueVTs.size();
1235 if (NumValues) {
1236 SDValue RetOp = getValue(I.getOperand(0));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001237 for (unsigned j = 0, f = NumValues; j != f; ++j) {
1238 EVT VT = ValueVTs[j];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001239
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001240 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001241
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001242 const Function *F = I.getParent()->getParent();
Bill Wendling8b62abd2012-12-30 13:01:51 +00001243 if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
1244 Attribute::SExt))
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001245 ExtendKind = ISD::SIGN_EXTEND;
Bill Wendling8b62abd2012-12-30 13:01:51 +00001246 else if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
1247 Attribute::ZExt))
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001248 ExtendKind = ISD::ZERO_EXTEND;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001249
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001250 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger())
Bill Wendlingba54bca2013-06-19 21:36:55 +00001251 VT = TLI->getTypeForExtArgOrReturn(VT.getSimpleVT(), ExtendKind);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001252
Bill Wendlingba54bca2013-06-19 21:36:55 +00001253 unsigned NumParts = TLI->getNumRegisters(*DAG.getContext(), VT);
1254 MVT PartVT = TLI->getRegisterType(*DAG.getContext(), VT);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001255 SmallVector<SDValue, 4> Parts(NumParts);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001256 getCopyToParts(DAG, getCurSDLoc(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001257 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
Bill Wendlingf18eb582012-09-26 06:16:18 +00001258 &Parts[0], NumParts, PartVT, &I, ExtendKind);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001259
1260 // 'inreg' on function refers to return value
1261 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
Bill Wendling8b62abd2012-12-30 13:01:51 +00001262 if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
1263 Attribute::InReg))
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001264 Flags.setInReg();
1265
1266 // Propagate extension type if any
Cameron Zwarich8df6bf52011-03-16 22:20:07 +00001267 if (ExtendKind == ISD::SIGN_EXTEND)
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001268 Flags.setSExt();
Cameron Zwarich8df6bf52011-03-16 22:20:07 +00001269 else if (ExtendKind == ISD::ZERO_EXTEND)
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001270 Flags.setZExt();
1271
Dan Gohmanc9403652010-07-07 15:54:55 +00001272 for (unsigned i = 0; i < NumParts; ++i) {
1273 Outs.push_back(ISD::OutputArg(Flags, Parts[i].getValueType(),
Tom Stellardd0716b02013-10-23 00:44:24 +00001274 VT, /*isfixed=*/true, 0, 0));
Dan Gohmanc9403652010-07-07 15:54:55 +00001275 OutVals.push_back(Parts[i]);
1276 }
Evan Cheng3927f432009-03-25 20:20:11 +00001277 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001278 }
1279 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001280
1281 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001282 CallingConv::ID CallConv =
1283 DAG.getMachineFunction().getFunction()->getCallingConv();
Bill Wendlingba54bca2013-06-19 21:36:55 +00001284 Chain = TM.getTargetLowering()->LowerReturn(Chain, CallConv, isVarArg,
1285 Outs, OutVals, getCurSDLoc(),
1286 DAG);
Dan Gohman5e866062009-08-06 15:37:27 +00001287
1288 // Verify that the target's LowerReturn behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00001289 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00001290 "LowerReturn didn't return a valid chain!");
1291
1292 // Update the DAG with the new chain value resulting from return lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001293 DAG.setRoot(Chain);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001294}
1295
Dan Gohmanad62f532009-04-23 23:13:24 +00001296/// CopyToExportRegsIfNeeded - If the given value has virtual registers
1297/// created for it, emit nodes to copy the value into the virtual
1298/// registers.
Dan Gohman46510a72010-04-15 01:51:59 +00001299void SelectionDAGBuilder::CopyToExportRegsIfNeeded(const Value *V) {
Rafael Espindola3fa82832011-05-13 15:18:06 +00001300 // Skip empty types
1301 if (V->getType()->isEmptyTy())
1302 return;
1303
Dan Gohman33b7a292010-04-16 17:15:02 +00001304 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
1305 if (VMI != FuncInfo.ValueMap.end()) {
1306 assert(!V->use_empty() && "Unused value assigned virtual registers!");
1307 CopyValueToVirtualRegister(V, VMI->second);
Dan Gohmanad62f532009-04-23 23:13:24 +00001308 }
1309}
1310
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001311/// ExportFromCurrentBlock - If this condition isn't known to be exported from
1312/// the current basic block, add it to ValueMap now so that we'll get a
1313/// CopyTo/FromReg.
Dan Gohman46510a72010-04-15 01:51:59 +00001314void SelectionDAGBuilder::ExportFromCurrentBlock(const Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001315 // No need to export constants.
1316 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001317
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001318 // Already exported?
1319 if (FuncInfo.isExportedInst(V)) return;
1320
1321 unsigned Reg = FuncInfo.InitializeRegForValue(V);
1322 CopyValueToVirtualRegister(V, Reg);
1323}
1324
Dan Gohman46510a72010-04-15 01:51:59 +00001325bool SelectionDAGBuilder::isExportableFromCurrentBlock(const Value *V,
Dan Gohman2048b852009-11-23 18:04:58 +00001326 const BasicBlock *FromBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001327 // The operands of the setcc have to be in this block. We don't know
1328 // how to export them from some other block.
Dan Gohman46510a72010-04-15 01:51:59 +00001329 if (const Instruction *VI = dyn_cast<Instruction>(V)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001330 // Can export from current BB.
1331 if (VI->getParent() == FromBB)
1332 return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001333
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001334 // Is already exported, noop.
1335 return FuncInfo.isExportedInst(V);
1336 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001337
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001338 // If this is an argument, we can export it if the BB is the entry block or
1339 // if it is already exported.
1340 if (isa<Argument>(V)) {
1341 if (FromBB == &FromBB->getParent()->getEntryBlock())
1342 return true;
1343
1344 // Otherwise, can only export this if it is already exported.
1345 return FuncInfo.isExportedInst(V);
1346 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001347
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001348 // Otherwise, constants can always be exported.
1349 return true;
1350}
1351
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001352/// Return branch probability calculated by BranchProbabilityInfo for IR blocks.
Jakub Staszak25101bb2011-12-20 20:03:10 +00001353uint32_t SelectionDAGBuilder::getEdgeWeight(const MachineBasicBlock *Src,
1354 const MachineBasicBlock *Dst) const {
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001355 BranchProbabilityInfo *BPI = FuncInfo.BPI;
1356 if (!BPI)
1357 return 0;
Jakub Staszak95ece8e2011-07-29 20:05:36 +00001358 const BasicBlock *SrcBB = Src->getBasicBlock();
1359 const BasicBlock *DstBB = Dst->getBasicBlock();
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001360 return BPI->getEdgeWeight(SrcBB, DstBB);
1361}
1362
Jakub Staszakc8f34de2011-07-29 22:25:21 +00001363void SelectionDAGBuilder::
1364addSuccessorWithWeight(MachineBasicBlock *Src, MachineBasicBlock *Dst,
1365 uint32_t Weight /* = 0 */) {
1366 if (!Weight)
1367 Weight = getEdgeWeight(Src, Dst);
1368 Src->addSuccessor(Dst, Weight);
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001369}
1370
1371
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001372static bool InBlock(const Value *V, const BasicBlock *BB) {
1373 if (const Instruction *I = dyn_cast<Instruction>(V))
1374 return I->getParent() == BB;
1375 return true;
1376}
1377
Dan Gohmanc2277342008-10-17 21:16:08 +00001378/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
1379/// This function emits a branch and is used at the leaves of an OR or an
1380/// AND operator tree.
1381///
1382void
Dan Gohman46510a72010-04-15 01:51:59 +00001383SelectionDAGBuilder::EmitBranchForMergedCondition(const Value *Cond,
Dan Gohman2048b852009-11-23 18:04:58 +00001384 MachineBasicBlock *TBB,
1385 MachineBasicBlock *FBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001386 MachineBasicBlock *CurBB,
1387 MachineBasicBlock *SwitchBB) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001388 const BasicBlock *BB = CurBB->getBasicBlock();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001389
Dan Gohmanc2277342008-10-17 21:16:08 +00001390 // If the leaf of the tree is a comparison, merge the condition into
1391 // the caseblock.
Dan Gohman46510a72010-04-15 01:51:59 +00001392 if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001393 // The operands of the cmp have to be in this block. We don't know
1394 // how to export them from some other block. If this is the first block
1395 // of the sequence, no exporting is needed.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001396 if (CurBB == SwitchBB ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001397 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
1398 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001399 ISD::CondCode Condition;
Dan Gohman46510a72010-04-15 01:51:59 +00001400 if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001401 Condition = getICmpCondCode(IC->getPredicate());
Dan Gohman46510a72010-04-15 01:51:59 +00001402 } else if (const FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001403 Condition = getFCmpCondCode(FC->getPredicate());
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001404 if (TM.Options.NoNaNsFPMath)
1405 Condition = getFCmpCodeWithoutNaN(Condition);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001406 } else {
1407 Condition = ISD::SETEQ; // silence warning.
Torok Edwinc23197a2009-07-14 16:55:14 +00001408 llvm_unreachable("Unknown compare instruction");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001409 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001410
1411 CaseBlock CB(Condition, BOp->getOperand(0),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001412 BOp->getOperand(1), NULL, TBB, FBB, CurBB);
1413 SwitchCases.push_back(CB);
1414 return;
1415 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001416 }
1417
1418 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001419 CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanc2277342008-10-17 21:16:08 +00001420 NULL, TBB, FBB, CurBB);
1421 SwitchCases.push_back(CB);
1422}
1423
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001424/// FindMergedConditions - If Cond is an expression like
Dan Gohman46510a72010-04-15 01:51:59 +00001425void SelectionDAGBuilder::FindMergedConditions(const Value *Cond,
Dan Gohman2048b852009-11-23 18:04:58 +00001426 MachineBasicBlock *TBB,
1427 MachineBasicBlock *FBB,
1428 MachineBasicBlock *CurBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001429 MachineBasicBlock *SwitchBB,
Dan Gohman2048b852009-11-23 18:04:58 +00001430 unsigned Opc) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001431 // If this node is not part of the or/and tree, emit it as a branch.
Dan Gohman46510a72010-04-15 01:51:59 +00001432 const Instruction *BOp = dyn_cast<Instruction>(Cond);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001433 if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001434 (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
1435 BOp->getParent() != CurBB->getBasicBlock() ||
1436 !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1437 !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001438 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001439 return;
1440 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001441
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001442 // Create TmpBB after CurBB.
1443 MachineFunction::iterator BBI = CurBB;
1444 MachineFunction &MF = DAG.getMachineFunction();
1445 MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
1446 CurBB->getParent()->insert(++BBI, TmpBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001447
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001448 if (Opc == Instruction::Or) {
1449 // Codegen X | Y as:
1450 // jmp_if_X TBB
1451 // jmp TmpBB
1452 // TmpBB:
1453 // jmp_if_Y TBB
1454 // jmp FBB
1455 //
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001456
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001457 // Emit the LHS condition.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001458 FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, SwitchBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001459
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001460 // Emit the RHS condition into TmpBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001461 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001462 } else {
1463 assert(Opc == Instruction::And && "Unknown merge op!");
1464 // Codegen X & Y as:
1465 // jmp_if_X TmpBB
1466 // jmp FBB
1467 // TmpBB:
1468 // jmp_if_Y TBB
1469 // jmp FBB
1470 //
1471 // This requires creation of TmpBB after CurBB.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001472
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001473 // Emit the LHS condition.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001474 FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, SwitchBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001475
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001476 // Emit the RHS condition into TmpBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001477 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001478 }
1479}
1480
1481/// If the set of cases should be emitted as a series of branches, return true.
1482/// If we should emit this as a bunch of and/or'd together conditions, return
1483/// false.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001484bool
Stephen Lin09f8ca32013-07-06 21:44:25 +00001485SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001486 if (Cases.size() != 2) return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001487
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001488 // If this is two comparisons of the same values or'd or and'd together, they
1489 // will get folded into a single comparison, so don't emit two blocks.
1490 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1491 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1492 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1493 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1494 return false;
1495 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001496
Chris Lattner133ce872010-01-02 00:00:03 +00001497 // Handle: (X != null) | (Y != null) --> (X|Y) != 0
1498 // Handle: (X == null) & (Y == null) --> (X|Y) == 0
1499 if (Cases[0].CmpRHS == Cases[1].CmpRHS &&
1500 Cases[0].CC == Cases[1].CC &&
1501 isa<Constant>(Cases[0].CmpRHS) &&
1502 cast<Constant>(Cases[0].CmpRHS)->isNullValue()) {
1503 if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB)
1504 return false;
1505 if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB)
1506 return false;
1507 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00001508
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001509 return true;
1510}
1511
Dan Gohman46510a72010-04-15 01:51:59 +00001512void SelectionDAGBuilder::visitBr(const BranchInst &I) {
Dan Gohman84023e02010-07-10 09:00:22 +00001513 MachineBasicBlock *BrMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001514
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001515 // Update machine-CFG edges.
1516 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1517
1518 // Figure out which block is immediately after the current one.
1519 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001520 MachineFunction::iterator BBI = BrMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001521 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001522 NextBlock = BBI;
1523
1524 if (I.isUnconditional()) {
1525 // Update machine-CFG edges.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001526 BrMBB->addSuccessor(Succ0MBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001527
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001528 // If this is not a fall-through branch, emit the branch.
Bill Wendling4533cac2010-01-28 21:51:40 +00001529 if (Succ0MBB != NextBlock)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001530 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001531 MVT::Other, getControlRoot(),
Bill Wendling4533cac2010-01-28 21:51:40 +00001532 DAG.getBasicBlock(Succ0MBB)));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001533
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001534 return;
1535 }
1536
1537 // If this condition is one of the special cases we handle, do special stuff
1538 // now.
Dan Gohman46510a72010-04-15 01:51:59 +00001539 const Value *CondVal = I.getCondition();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001540 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1541
1542 // If this is a series of conditions that are or'd or and'd together, emit
1543 // this as a sequence of branches instead of setcc's with and/or operations.
Chris Lattnerde189be2010-11-30 18:12:52 +00001544 // As long as jumps are not expensive, this should improve performance.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001545 // For example, instead of something like:
1546 // cmp A, B
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001547 // C = seteq
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001548 // cmp D, E
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001549 // F = setle
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001550 // or C, F
1551 // jnz foo
1552 // Emit:
1553 // cmp A, B
1554 // je foo
1555 // cmp D, E
1556 // jle foo
1557 //
Dan Gohman46510a72010-04-15 01:51:59 +00001558 if (const BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00001559 if (!TM.getTargetLowering()->isJumpExpensive() &&
Chris Lattnerde189be2010-11-30 18:12:52 +00001560 BOp->hasOneUse() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001561 (BOp->getOpcode() == Instruction::And ||
1562 BOp->getOpcode() == Instruction::Or)) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001563 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, BrMBB, BrMBB,
1564 BOp->getOpcode());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001565 // If the compares in later blocks need to use values not currently
1566 // exported from this block, export them now. This block should always
1567 // be the first entry.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001568 assert(SwitchCases[0].ThisBB == BrMBB && "Unexpected lowering!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001569
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001570 // Allow some cases to be rejected.
1571 if (ShouldEmitAsBranches(SwitchCases)) {
1572 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1573 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1574 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1575 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001576
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001577 // Emit the branch for this block.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001578 visitSwitchCase(SwitchCases[0], BrMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001579 SwitchCases.erase(SwitchCases.begin());
1580 return;
1581 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001582
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001583 // Okay, we decided not to do this, remove any inserted MBB's and clear
1584 // SwitchCases.
1585 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001586 FuncInfo.MF->erase(SwitchCases[i].ThisBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001587
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001588 SwitchCases.clear();
1589 }
1590 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001591
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001592 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001593 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohman99be8ae2010-04-19 22:41:47 +00001594 NULL, Succ0MBB, Succ1MBB, BrMBB);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001595
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001596 // Use visitSwitchCase to actually insert the fast branch sequence for this
1597 // cond branch.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001598 visitSwitchCase(CB, BrMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001599}
1600
1601/// visitSwitchCase - Emits the necessary code to represent a single node in
1602/// the binary search tree resulting from lowering a switch instruction.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001603void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB,
1604 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001605 SDValue Cond;
1606 SDValue CondLHS = getValue(CB.CmpLHS);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001607 SDLoc dl = getCurSDLoc();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001608
1609 // Build the setcc now.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001610 if (CB.CmpMHS == NULL) {
1611 // Fold "(X == true)" to X and "(X == false)" to !X to
1612 // handle common cases produced by branch lowering.
Owen Anderson5defacc2009-07-31 17:39:07 +00001613 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001614 CB.CC == ISD::SETEQ)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001615 Cond = CondLHS;
Owen Anderson5defacc2009-07-31 17:39:07 +00001616 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001617 CB.CC == ISD::SETEQ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001618 SDValue True = DAG.getConstant(1, CondLHS.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001619 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001620 } else
Owen Anderson825b72b2009-08-11 20:47:22 +00001621 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001622 } else {
Bob Wilsondb3a9e62013-09-09 19:14:35 +00001623 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001624
Anton Korobeynikov23218582008-12-23 22:25:27 +00001625 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
1626 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001627
1628 SDValue CmpOp = getValue(CB.CmpMHS);
Owen Andersone50ed302009-08-10 22:56:29 +00001629 EVT VT = CmpOp.getValueType();
Stephen Lin155615d2013-07-08 00:37:03 +00001630
Bob Wilsondb3a9e62013-09-09 19:14:35 +00001631 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001632 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT),
Bob Wilsondb3a9e62013-09-09 19:14:35 +00001633 ISD::SETLE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001634 } else {
Dale Johannesenf5d97892009-02-04 01:48:28 +00001635 SDValue SUB = DAG.getNode(ISD::SUB, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001636 VT, CmpOp, DAG.getConstant(Low, VT));
Owen Anderson825b72b2009-08-11 20:47:22 +00001637 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001638 DAG.getConstant(High-Low, VT), ISD::SETULE);
1639 }
1640 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001641
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001642 // Update successor info
Jakub Staszakc8f34de2011-07-29 22:25:21 +00001643 addSuccessorWithWeight(SwitchBB, CB.TrueBB, CB.TrueWeight);
Jakob Stoklund Olesene7fdef42012-08-20 21:39:52 +00001644 // TrueBB and FalseBB are always different unless the incoming IR is
1645 // degenerate. This only happens when running llc on weird IR.
1646 if (CB.TrueBB != CB.FalseBB)
1647 addSuccessorWithWeight(SwitchBB, CB.FalseBB, CB.FalseWeight);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001648
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001649 // Set NextBlock to be the MBB immediately after the current one, if any.
1650 // This is used to avoid emitting unnecessary branches to the next block.
1651 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001652 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001653 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001654 NextBlock = BBI;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001655
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001656 // If the lhs block is the next block, invert the condition so that we can
1657 // fall through to the lhs instead of the rhs block.
1658 if (CB.TrueBB == NextBlock) {
1659 std::swap(CB.TrueBB, CB.FalseBB);
1660 SDValue True = DAG.getConstant(1, Cond.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001661 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001662 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001663
Dale Johannesenf5d97892009-02-04 01:48:28 +00001664 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001665 MVT::Other, getControlRoot(), Cond,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001666 DAG.getBasicBlock(CB.TrueBB));
Bill Wendling87710f02009-12-21 23:47:40 +00001667
Evan Cheng266a99d2010-09-23 06:51:55 +00001668 // Insert the false branch. Do this even if it's a fall through branch,
1669 // this makes it easier to do DAG optimizations which require inverting
1670 // the branch condition.
1671 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
1672 DAG.getBasicBlock(CB.FalseBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001673
1674 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001675}
1676
1677/// visitJumpTable - Emit JumpTable node in the current MBB
Dan Gohman2048b852009-11-23 18:04:58 +00001678void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001679 // Emit the code for the jump table
1680 assert(JT.Reg != -1U && "Should lower JT Header first!");
Bill Wendlingba54bca2013-06-19 21:36:55 +00001681 EVT PTy = TM.getTargetLowering()->getPointerTy();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001682 SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurSDLoc(),
Dale Johannesena04b7572009-02-03 23:04:43 +00001683 JT.Reg, PTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001684 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001685 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurSDLoc(),
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001686 MVT::Other, Index.getValue(1),
1687 Table, Index);
1688 DAG.setRoot(BrJumpTable);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001689}
1690
1691/// visitJumpTableHeader - This function emits necessary code to produce index
1692/// in the JumpTable from switch case.
Dan Gohman2048b852009-11-23 18:04:58 +00001693void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001694 JumpTableHeader &JTH,
1695 MachineBasicBlock *SwitchBB) {
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001696 // Subtract the lowest switch case value from the value being switched on and
1697 // conditional branch to default mbb if the result is greater than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001698 // difference between smallest and largest cases.
1699 SDValue SwitchOp = getValue(JTH.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001700 EVT VT = SwitchOp.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001701 SDValue Sub = DAG.getNode(ISD::SUB, getCurSDLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001702 DAG.getConstant(JTH.First, VT));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001703
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001704 // The SDNode we just created, which holds the value being switched on minus
Dan Gohmanf451cb82010-02-10 16:03:48 +00001705 // the smallest case value, needs to be copied to a virtual register so it
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001706 // can be used as an index into the jump table in a subsequent basic block.
1707 // This value may be smaller or larger than the target's pointer type, and
1708 // therefore require extension or truncating.
Bill Wendlingba54bca2013-06-19 21:36:55 +00001709 const TargetLowering *TLI = TM.getTargetLowering();
1710 SwitchOp = DAG.getZExtOrTrunc(Sub, getCurSDLoc(), TLI->getPointerTy());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001711
Bill Wendlingba54bca2013-06-19 21:36:55 +00001712 unsigned JumpTableReg = FuncInfo.CreateReg(TLI->getPointerTy());
Andrew Trickac6d9be2013-05-25 02:42:55 +00001713 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurSDLoc(),
Dale Johannesena04b7572009-02-03 23:04:43 +00001714 JumpTableReg, SwitchOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001715 JT.Reg = JumpTableReg;
1716
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001717 // Emit the range check for the jump table, and branch to the default block
1718 // for the switch statement if the value being switched on exceeds the largest
1719 // case in the switch.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001720 SDValue CMP = DAG.getSetCC(getCurSDLoc(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00001721 TLI->getSetCCResultType(*DAG.getContext(),
1722 Sub.getValueType()),
Matt Arsenault225ed702013-05-18 00:21:46 +00001723 Sub,
1724 DAG.getConstant(JTH.Last - JTH.First,VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001725 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001726
1727 // Set NextBlock to be the MBB immediately after the current one, if any.
1728 // This is used to avoid emitting unnecessary branches to the next block.
1729 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001730 MachineFunction::iterator BBI = SwitchBB;
Bill Wendling87710f02009-12-21 23:47:40 +00001731
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001732 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001733 NextBlock = BBI;
1734
Andrew Trickac6d9be2013-05-25 02:42:55 +00001735 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurSDLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001736 MVT::Other, CopyTo, CMP,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001737 DAG.getBasicBlock(JT.Default));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001738
Bill Wendling4533cac2010-01-28 21:51:40 +00001739 if (JT.MBB != NextBlock)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001740 BrCond = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, BrCond,
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001741 DAG.getBasicBlock(JT.MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001742
Bill Wendling87710f02009-12-21 23:47:40 +00001743 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001744}
1745
Michael Gottesman657484f2013-08-20 07:00:16 +00001746/// Codegen a new tail for a stack protector check ParentMBB which has had its
1747/// tail spliced into a stack protector check success bb.
1748///
1749/// For a high level explanation of how this fits into the stack protector
1750/// generation see the comment on the declaration of class
1751/// StackProtectorDescriptor.
1752void SelectionDAGBuilder::visitSPDescriptorParent(StackProtectorDescriptor &SPD,
1753 MachineBasicBlock *ParentBB) {
1754
1755 // First create the loads to the guard/stack slot for the comparison.
1756 const TargetLowering *TLI = TM.getTargetLowering();
1757 EVT PtrTy = TLI->getPointerTy();
1758
1759 MachineFrameInfo *MFI = ParentBB->getParent()->getFrameInfo();
1760 int FI = MFI->getStackProtectorIndex();
1761
1762 const Value *IRGuard = SPD.getGuard();
1763 SDValue GuardPtr = getValue(IRGuard);
1764 SDValue StackSlotPtr = DAG.getFrameIndex(FI, PtrTy);
1765
1766 unsigned Align =
1767 TLI->getDataLayout()->getPrefTypeAlignment(IRGuard->getType());
1768 SDValue Guard = DAG.getLoad(PtrTy, getCurSDLoc(), DAG.getEntryNode(),
1769 GuardPtr, MachinePointerInfo(IRGuard, 0),
1770 true, false, false, Align);
1771
1772 SDValue StackSlot = DAG.getLoad(PtrTy, getCurSDLoc(), DAG.getEntryNode(),
1773 StackSlotPtr,
1774 MachinePointerInfo::getFixedStack(FI),
1775 true, false, false, Align);
1776
1777 // Perform the comparison via a subtract/getsetcc.
1778 EVT VT = Guard.getValueType();
1779 SDValue Sub = DAG.getNode(ISD::SUB, getCurSDLoc(), VT, Guard, StackSlot);
1780
1781 SDValue Cmp = DAG.getSetCC(getCurSDLoc(),
1782 TLI->getSetCCResultType(*DAG.getContext(),
1783 Sub.getValueType()),
1784 Sub, DAG.getConstant(0, VT),
1785 ISD::SETNE);
1786
1787 // If the sub is not 0, then we know the guard/stackslot do not equal, so
1788 // branch to failure MBB.
1789 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurSDLoc(),
1790 MVT::Other, StackSlot.getOperand(0),
1791 Cmp, DAG.getBasicBlock(SPD.getFailureMBB()));
1792 // Otherwise branch to success MBB.
1793 SDValue Br = DAG.getNode(ISD::BR, getCurSDLoc(),
1794 MVT::Other, BrCond,
1795 DAG.getBasicBlock(SPD.getSuccessMBB()));
1796
1797 DAG.setRoot(Br);
1798}
1799
1800/// Codegen the failure basic block for a stack protector check.
1801///
1802/// A failure stack protector machine basic block consists simply of a call to
1803/// __stack_chk_fail().
1804///
1805/// For a high level explanation of how this fits into the stack protector
1806/// generation see the comment on the declaration of class
1807/// StackProtectorDescriptor.
1808void
1809SelectionDAGBuilder::visitSPDescriptorFailure(StackProtectorDescriptor &SPD) {
1810 const TargetLowering *TLI = TM.getTargetLowering();
1811 SDValue Chain = TLI->makeLibCall(DAG, RTLIB::STACKPROTECTOR_CHECK_FAIL,
1812 MVT::isVoid, 0, 0, false, getCurSDLoc(),
Michael Gottesman58a9b432013-08-22 23:45:24 +00001813 false, false).second;
Michael Gottesman657484f2013-08-20 07:00:16 +00001814 DAG.setRoot(Chain);
1815}
1816
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001817/// visitBitTestHeader - This function emits necessary code to produce value
1818/// suitable for "bit tests"
Dan Gohman99be8ae2010-04-19 22:41:47 +00001819void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B,
1820 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001821 // Subtract the minimum value
1822 SDValue SwitchOp = getValue(B.SValue);
Patrik Hagglund34525f92012-12-11 11:14:33 +00001823 EVT VT = SwitchOp.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001824 SDValue Sub = DAG.getNode(ISD::SUB, getCurSDLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001825 DAG.getConstant(B.First, VT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001826
1827 // Check range
Bill Wendlingba54bca2013-06-19 21:36:55 +00001828 const TargetLowering *TLI = TM.getTargetLowering();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001829 SDValue RangeCmp = DAG.getSetCC(getCurSDLoc(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00001830 TLI->getSetCCResultType(*DAG.getContext(),
Matt Arsenault225ed702013-05-18 00:21:46 +00001831 Sub.getValueType()),
Bill Wendling87710f02009-12-21 23:47:40 +00001832 Sub, DAG.getConstant(B.Range, VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001833 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001834
Evan Chengd08e5b42011-01-06 01:02:44 +00001835 // Determine the type of the test operands.
1836 bool UsePtrType = false;
Bill Wendlingba54bca2013-06-19 21:36:55 +00001837 if (!TLI->isTypeLegal(VT))
Evan Chengd08e5b42011-01-06 01:02:44 +00001838 UsePtrType = true;
1839 else {
1840 for (unsigned i = 0, e = B.Cases.size(); i != e; ++i)
Eli Friedman5c75af62011-10-12 22:46:45 +00001841 if (!isUIntN(VT.getSizeInBits(), B.Cases[i].Mask)) {
Evan Chengd08e5b42011-01-06 01:02:44 +00001842 // Switch table case range are encoded into series of masks.
1843 // Just use pointer type, it's guaranteed to fit.
1844 UsePtrType = true;
1845 break;
1846 }
1847 }
1848 if (UsePtrType) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00001849 VT = TLI->getPointerTy();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001850 Sub = DAG.getZExtOrTrunc(Sub, getCurSDLoc(), VT);
Evan Chengd08e5b42011-01-06 01:02:44 +00001851 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001852
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00001853 B.RegVT = VT.getSimpleVT();
Patrik Hagglund8963fec2012-12-19 12:23:01 +00001854 B.Reg = FuncInfo.CreateReg(B.RegVT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001855 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurSDLoc(),
Evan Chengd08e5b42011-01-06 01:02:44 +00001856 B.Reg, Sub);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001857
1858 // Set NextBlock to be the MBB immediately after the current one, if any.
1859 // This is used to avoid emitting unnecessary branches to the next block.
1860 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001861 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001862 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001863 NextBlock = BBI;
1864
1865 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1866
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001867 addSuccessorWithWeight(SwitchBB, B.Default);
1868 addSuccessorWithWeight(SwitchBB, MBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001869
Andrew Trickac6d9be2013-05-25 02:42:55 +00001870 SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurSDLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001871 MVT::Other, CopyTo, RangeCmp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001872 DAG.getBasicBlock(B.Default));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001873
Evan Cheng8c1f4322010-09-23 18:32:19 +00001874 if (MBB != NextBlock)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001875 BrRange = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, CopyTo,
Evan Cheng8c1f4322010-09-23 18:32:19 +00001876 DAG.getBasicBlock(MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001877
Bill Wendling87710f02009-12-21 23:47:40 +00001878 DAG.setRoot(BrRange);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001879}
1880
1881/// visitBitTestCase - this function produces one "bit test"
Evan Chengd08e5b42011-01-06 01:02:44 +00001882void SelectionDAGBuilder::visitBitTestCase(BitTestBlock &BB,
1883 MachineBasicBlock* NextMBB,
Manman Ren1a710fd2012-08-24 18:14:27 +00001884 uint32_t BranchWeightToNext,
Dan Gohman2048b852009-11-23 18:04:58 +00001885 unsigned Reg,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001886 BitTestCase &B,
1887 MachineBasicBlock *SwitchBB) {
Patrik Hagglund8963fec2012-12-19 12:23:01 +00001888 MVT VT = BB.RegVT;
Andrew Trickac6d9be2013-05-25 02:42:55 +00001889 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurSDLoc(),
Evan Chengd08e5b42011-01-06 01:02:44 +00001890 Reg, VT);
Dan Gohman8e0163a2010-06-24 02:06:24 +00001891 SDValue Cmp;
Benjamin Kramer3ff25512011-07-14 01:38:42 +00001892 unsigned PopCount = CountPopulation_64(B.Mask);
Bill Wendlingba54bca2013-06-19 21:36:55 +00001893 const TargetLowering *TLI = TM.getTargetLowering();
Benjamin Kramer3ff25512011-07-14 01:38:42 +00001894 if (PopCount == 1) {
Dan Gohman8e0163a2010-06-24 02:06:24 +00001895 // Testing for a single bit; just compare the shift count with what it
1896 // would need to be to shift a 1 bit in that position.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001897 Cmp = DAG.getSetCC(getCurSDLoc(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00001898 TLI->getSetCCResultType(*DAG.getContext(), VT),
Dan Gohman8e0163a2010-06-24 02:06:24 +00001899 ShiftOp,
Michael J. Spencerc6af2432013-05-24 22:23:49 +00001900 DAG.getConstant(countTrailingZeros(B.Mask), VT),
Dan Gohman8e0163a2010-06-24 02:06:24 +00001901 ISD::SETEQ);
Benjamin Kramer3ff25512011-07-14 01:38:42 +00001902 } else if (PopCount == BB.Range) {
1903 // There is only one zero bit in the range, test for it directly.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001904 Cmp = DAG.getSetCC(getCurSDLoc(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00001905 TLI->getSetCCResultType(*DAG.getContext(), VT),
Benjamin Kramer3ff25512011-07-14 01:38:42 +00001906 ShiftOp,
1907 DAG.getConstant(CountTrailingOnes_64(B.Mask), VT),
1908 ISD::SETNE);
Dan Gohman8e0163a2010-06-24 02:06:24 +00001909 } else {
1910 // Make desired shift
Andrew Trickac6d9be2013-05-25 02:42:55 +00001911 SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurSDLoc(), VT,
Evan Chengd08e5b42011-01-06 01:02:44 +00001912 DAG.getConstant(1, VT), ShiftOp);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001913
Dan Gohman8e0163a2010-06-24 02:06:24 +00001914 // Emit bit tests and jumps
Andrew Trickac6d9be2013-05-25 02:42:55 +00001915 SDValue AndOp = DAG.getNode(ISD::AND, getCurSDLoc(),
Evan Chengd08e5b42011-01-06 01:02:44 +00001916 VT, SwitchVal, DAG.getConstant(B.Mask, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00001917 Cmp = DAG.getSetCC(getCurSDLoc(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00001918 TLI->getSetCCResultType(*DAG.getContext(), VT),
Evan Chengd08e5b42011-01-06 01:02:44 +00001919 AndOp, DAG.getConstant(0, VT),
Dan Gohman8e0163a2010-06-24 02:06:24 +00001920 ISD::SETNE);
1921 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001922
Manman Ren1a710fd2012-08-24 18:14:27 +00001923 // The branch weight from SwitchBB to B.TargetBB is B.ExtraWeight.
1924 addSuccessorWithWeight(SwitchBB, B.TargetBB, B.ExtraWeight);
1925 // The branch weight from SwitchBB to NextMBB is BranchWeightToNext.
1926 addSuccessorWithWeight(SwitchBB, NextMBB, BranchWeightToNext);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001927
Andrew Trickac6d9be2013-05-25 02:42:55 +00001928 SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurSDLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001929 MVT::Other, getControlRoot(),
Dan Gohman8e0163a2010-06-24 02:06:24 +00001930 Cmp, DAG.getBasicBlock(B.TargetBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001931
1932 // Set NextBlock to be the MBB immediately after the current one, if any.
1933 // This is used to avoid emitting unnecessary branches to the next block.
1934 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001935 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001936 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001937 NextBlock = BBI;
1938
Evan Cheng8c1f4322010-09-23 18:32:19 +00001939 if (NextMBB != NextBlock)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001940 BrAnd = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, BrAnd,
Evan Cheng8c1f4322010-09-23 18:32:19 +00001941 DAG.getBasicBlock(NextMBB));
Bill Wendling0777e922009-12-21 21:59:52 +00001942
Bill Wendling87710f02009-12-21 23:47:40 +00001943 DAG.setRoot(BrAnd);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001944}
1945
Dan Gohman46510a72010-04-15 01:51:59 +00001946void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) {
Dan Gohman84023e02010-07-10 09:00:22 +00001947 MachineBasicBlock *InvokeMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001948
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001949 // Retrieve successors.
1950 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1951 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1952
Gabor Greifb67e6b32009-01-15 11:10:44 +00001953 const Value *Callee(I.getCalledValue());
Nuno Lopes85b40892012-06-28 22:30:12 +00001954 const Function *Fn = dyn_cast<Function>(Callee);
Gabor Greifb67e6b32009-01-15 11:10:44 +00001955 if (isa<InlineAsm>(Callee))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001956 visitInlineAsm(&I);
Nuno Lopes85b40892012-06-28 22:30:12 +00001957 else if (Fn && Fn->isIntrinsic()) {
1958 assert(Fn->getIntrinsicID() == Intrinsic::donothing);
Nuno Lopes4532bf62012-07-18 00:07:17 +00001959 // Ignore invokes to @llvm.donothing: jump directly to the next BB.
Nuno Lopes85b40892012-06-28 22:30:12 +00001960 } else
Gabor Greifb67e6b32009-01-15 11:10:44 +00001961 LowerCallTo(&I, getValue(Callee), false, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001962
1963 // If the value of the invoke is used outside of its defining block, make it
1964 // available as a virtual register.
Dan Gohmanad62f532009-04-23 23:13:24 +00001965 CopyToExportRegsIfNeeded(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001966
1967 // Update successor info
Chandler Carruthf2645682011-11-22 11:37:46 +00001968 addSuccessorWithWeight(InvokeMBB, Return);
1969 addSuccessorWithWeight(InvokeMBB, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001970
1971 // Drop into normal successor.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001972 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00001973 MVT::Other, getControlRoot(),
1974 DAG.getBasicBlock(Return)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001975}
1976
Bill Wendlingdccc03b2011-07-31 06:30:59 +00001977void SelectionDAGBuilder::visitResume(const ResumeInst &RI) {
1978 llvm_unreachable("SelectionDAGBuilder shouldn't visit resume instructions!");
1979}
1980
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00001981void SelectionDAGBuilder::visitLandingPad(const LandingPadInst &LP) {
1982 assert(FuncInfo.MBB->isLandingPad() &&
1983 "Call to landingpad not in landing pad!");
1984
1985 MachineBasicBlock *MBB = FuncInfo.MBB;
1986 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
1987 AddLandingPadInfo(LP, MMI, MBB);
1988
Bill Wendlingbdf9db62012-02-13 23:47:16 +00001989 // If there aren't registers to copy the values into (e.g., during SjLj
1990 // exceptions), then don't bother to create these DAG nodes.
Bill Wendlingba54bca2013-06-19 21:36:55 +00001991 const TargetLowering *TLI = TM.getTargetLowering();
1992 if (TLI->getExceptionPointerRegister() == 0 &&
1993 TLI->getExceptionSelectorRegister() == 0)
Bill Wendlingbdf9db62012-02-13 23:47:16 +00001994 return;
1995
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00001996 SmallVector<EVT, 2> ValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00001997 ComputeValueVTs(*TLI, LP.getType(), ValueVTs);
Jakob Stoklund Olesen918b7c82013-07-04 04:53:45 +00001998 assert(ValueVTs.size() == 2 && "Only two-valued landingpads are supported");
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00001999
Jakob Stoklund Olesen918b7c82013-07-04 04:53:45 +00002000 // Get the two live-in registers as SDValues. The physregs have already been
2001 // copied into virtual registers.
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00002002 SDValue Ops[2];
Jakob Stoklund Olesen918b7c82013-07-04 04:53:45 +00002003 Ops[0] = DAG.getZExtOrTrunc(
2004 DAG.getCopyFromReg(DAG.getEntryNode(), getCurSDLoc(),
2005 FuncInfo.ExceptionPointerVirtReg, TLI->getPointerTy()),
2006 getCurSDLoc(), ValueVTs[0]);
2007 Ops[1] = DAG.getZExtOrTrunc(
2008 DAG.getCopyFromReg(DAG.getEntryNode(), getCurSDLoc(),
2009 FuncInfo.ExceptionSelectorVirtReg, TLI->getPointerTy()),
2010 getCurSDLoc(), ValueVTs[1]);
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00002011
Jakob Stoklund Olesen918b7c82013-07-04 04:53:45 +00002012 // Merge into one.
Andrew Trickac6d9be2013-05-25 02:42:55 +00002013 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00002014 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
2015 &Ops[0], 2);
Jakob Stoklund Olesen918b7c82013-07-04 04:53:45 +00002016 setValue(&LP, Res);
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00002017}
2018
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002019/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
2020/// small case ranges).
Dan Gohman2048b852009-11-23 18:04:58 +00002021bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR,
2022 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00002023 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00002024 MachineBasicBlock *Default,
2025 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002026 // Size is the number of Cases represented by this range.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002027 size_t Size = CR.Range.second - CR.Range.first;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002028 if (Size > 3)
Anton Korobeynikov23218582008-12-23 22:25:27 +00002029 return false;
2030
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002031 // Get the MachineFunction which holds the current MBB. This is used when
2032 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002033 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002034
2035 // Figure out which block is immediately after the current one.
2036 MachineBasicBlock *NextBlock = 0;
2037 MachineFunction::iterator BBI = CR.CaseBB;
2038
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002039 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002040 NextBlock = BBI;
2041
Manman Ren1a710fd2012-08-24 18:14:27 +00002042 BranchProbabilityInfo *BPI = FuncInfo.BPI;
Benjamin Kramerce750f02010-11-22 09:45:38 +00002043 // If any two of the cases has the same destination, and if one value
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002044 // is the same as the other, but has one bit unset that the other has set,
2045 // use bit manipulation to do two compares at once. For example:
2046 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
Benjamin Kramerce750f02010-11-22 09:45:38 +00002047 // TODO: This could be extended to merge any 2 cases in switches with 3 cases.
2048 // TODO: Handle cases where CR.CaseBB != SwitchBB.
2049 if (Size == 2 && CR.CaseBB == SwitchBB) {
2050 Case &Small = *CR.Range.first;
2051 Case &Big = *(CR.Range.second-1);
2052
2053 if (Small.Low == Small.High && Big.Low == Big.High && Small.BB == Big.BB) {
2054 const APInt& SmallValue = cast<ConstantInt>(Small.Low)->getValue();
2055 const APInt& BigValue = cast<ConstantInt>(Big.Low)->getValue();
2056
2057 // Check that there is only one bit different.
2058 if (BigValue.countPopulation() == SmallValue.countPopulation() + 1 &&
2059 (SmallValue | BigValue) == BigValue) {
2060 // Isolate the common bit.
2061 APInt CommonBit = BigValue & ~SmallValue;
2062 assert((SmallValue | CommonBit) == BigValue &&
2063 CommonBit.countPopulation() == 1 && "Not a common bit?");
2064
2065 SDValue CondLHS = getValue(SV);
2066 EVT VT = CondLHS.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00002067 SDLoc DL = getCurSDLoc();
Benjamin Kramerce750f02010-11-22 09:45:38 +00002068
2069 SDValue Or = DAG.getNode(ISD::OR, DL, VT, CondLHS,
2070 DAG.getConstant(CommonBit, VT));
2071 SDValue Cond = DAG.getSetCC(DL, MVT::i1,
2072 Or, DAG.getConstant(BigValue, VT),
2073 ISD::SETEQ);
2074
2075 // Update successor info.
Manman Ren1a710fd2012-08-24 18:14:27 +00002076 // Both Small and Big will jump to Small.BB, so we sum up the weights.
2077 addSuccessorWithWeight(SwitchBB, Small.BB,
2078 Small.ExtraWeight + Big.ExtraWeight);
2079 addSuccessorWithWeight(SwitchBB, Default,
2080 // The default destination is the first successor in IR.
2081 BPI ? BPI->getEdgeWeight(SwitchBB->getBasicBlock(), (unsigned)0) : 0);
Benjamin Kramerce750f02010-11-22 09:45:38 +00002082
2083 // Insert the true branch.
2084 SDValue BrCond = DAG.getNode(ISD::BRCOND, DL, MVT::Other,
2085 getControlRoot(), Cond,
2086 DAG.getBasicBlock(Small.BB));
2087
2088 // Insert the false branch.
2089 BrCond = DAG.getNode(ISD::BR, DL, MVT::Other, BrCond,
2090 DAG.getBasicBlock(Default));
2091
2092 DAG.setRoot(BrCond);
2093 return true;
2094 }
2095 }
2096 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002097
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002098 // Order cases by weight so the most likely case will be checked first.
Manman Ren1a710fd2012-08-24 18:14:27 +00002099 uint32_t UnhandledWeights = 0;
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002100 if (BPI) {
2101 for (CaseItr I = CR.Range.first, IE = CR.Range.second; I != IE; ++I) {
Manman Ren1a710fd2012-08-24 18:14:27 +00002102 uint32_t IWeight = I->ExtraWeight;
2103 UnhandledWeights += IWeight;
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002104 for (CaseItr J = CR.Range.first; J < I; ++J) {
Manman Ren1a710fd2012-08-24 18:14:27 +00002105 uint32_t JWeight = J->ExtraWeight;
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002106 if (IWeight > JWeight)
2107 std::swap(*I, *J);
2108 }
2109 }
2110 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002111 // Rearrange the case blocks so that the last one falls through if possible.
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002112 Case &BackCase = *(CR.Range.second-1);
Benjamin Kramer5db954d2012-05-26 21:19:12 +00002113 if (Size > 1 &&
2114 NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002115 // The last case block won't fall through into 'NextBlock' if we emit the
2116 // branches in this order. See if rearranging a case value would help.
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002117 // We start at the bottom as it's the case with the least weight.
Stephen Lin09f8ca32013-07-06 21:44:25 +00002118 for (Case *I = &*(CR.Range.second-2), *E = &*CR.Range.first-1; I != E; --I)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002119 if (I->BB == NextBlock) {
2120 std::swap(*I, BackCase);
2121 break;
2122 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002123 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002124
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002125 // Create a CaseBlock record representing a conditional branch to
2126 // the Case's target mbb if the value being switched on SV is equal
2127 // to C.
2128 MachineBasicBlock *CurBlock = CR.CaseBB;
2129 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
2130 MachineBasicBlock *FallThrough;
2131 if (I != E-1) {
2132 FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
2133 CurMF->insert(BBI, FallThrough);
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002134
2135 // Put SV in a virtual register to make it available from the new blocks.
2136 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002137 } else {
2138 // If the last case doesn't match, go to the default block.
2139 FallThrough = Default;
2140 }
2141
Dan Gohman46510a72010-04-15 01:51:59 +00002142 const Value *RHS, *LHS, *MHS;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002143 ISD::CondCode CC;
2144 if (I->High == I->Low) {
2145 // This is just small small case range :) containing exactly 1 case
2146 CC = ISD::SETEQ;
2147 LHS = SV; RHS = I->High; MHS = NULL;
2148 } else {
Bob Wilsondb3a9e62013-09-09 19:14:35 +00002149 CC = ISD::SETLE;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002150 LHS = I->Low; MHS = SV; RHS = I->High;
2151 }
Jakub Staszakc8f34de2011-07-29 22:25:21 +00002152
Manman Ren1a710fd2012-08-24 18:14:27 +00002153 // The false weight should be sum of all un-handled cases.
2154 UnhandledWeights -= I->ExtraWeight;
Jakub Staszakc8f34de2011-07-29 22:25:21 +00002155 CaseBlock CB(CC, LHS, RHS, MHS, /* truebb */ I->BB, /* falsebb */ FallThrough,
2156 /* me */ CurBlock,
Manman Ren1a710fd2012-08-24 18:14:27 +00002157 /* trueweight */ I->ExtraWeight,
2158 /* falseweight */ UnhandledWeights);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002159
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002160 // If emitting the first comparison, just call visitSwitchCase to emit the
2161 // code into the current block. Otherwise, push the CaseBlock onto the
2162 // vector to be later processed by SDISel, and insert the node's MBB
2163 // before the next MBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002164 if (CurBlock == SwitchBB)
2165 visitSwitchCase(CB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002166 else
2167 SwitchCases.push_back(CB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002168
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002169 CurBlock = FallThrough;
2170 }
2171
2172 return true;
2173}
2174
2175static inline bool areJTsAllowed(const TargetLowering &TLI) {
Evan Cheng769951f2012-07-02 22:39:56 +00002176 return TLI.supportJumpTables() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00002177 (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
2178 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002179}
Anton Korobeynikov23218582008-12-23 22:25:27 +00002180
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002181static APInt ComputeRange(const APInt &First, const APInt &Last) {
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002182 uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1;
Bob Wilsondb3a9e62013-09-09 19:14:35 +00002183 APInt LastExt = Last.sext(BitWidth), FirstExt = First.sext(BitWidth);
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002184 return (LastExt - FirstExt + 1ULL);
2185}
2186
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002187/// handleJTSwitchCase - Emit jumptable for current switch case range
Chris Lattnerc3ab3882011-09-09 22:06:59 +00002188bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec &CR,
2189 CaseRecVector &WorkList,
2190 const Value *SV,
2191 MachineBasicBlock *Default,
Dan Gohman99be8ae2010-04-19 22:41:47 +00002192 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002193 Case& FrontCase = *CR.Range.first;
2194 Case& BackCase = *(CR.Range.second-1);
2195
Chris Lattnere880efe2009-11-07 07:50:34 +00002196 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
2197 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002198
Chris Lattnere880efe2009-11-07 07:50:34 +00002199 APInt TSize(First.getBitWidth(), 0);
Chris Lattnerc3ab3882011-09-09 22:06:59 +00002200 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002201 TSize += I->size();
2202
Bill Wendlingba54bca2013-06-19 21:36:55 +00002203 const TargetLowering *TLI = TM.getTargetLowering();
2204 if (!areJTsAllowed(*TLI) || TSize.ult(TLI->getMinimumJumpTableEntries()))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002205 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002206
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002207 APInt Range = ComputeRange(First, Last);
Jakob Stoklund Olesen79443912011-10-26 01:47:48 +00002208 // The density is TSize / Range. Require at least 40%.
2209 // It should not be possible for IntTSize to saturate for sane code, but make
2210 // sure we handle Range saturation correctly.
2211 uint64_t IntRange = Range.getLimitedValue(UINT64_MAX/10);
2212 uint64_t IntTSize = TSize.getLimitedValue(UINT64_MAX/10);
2213 if (IntTSize * 10 < IntRange * 4)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002214 return false;
2215
David Greene4b69d992010-01-05 01:24:57 +00002216 DEBUG(dbgs() << "Lowering jump table\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002217 << "First entry: " << First << ". Last entry: " << Last << '\n'
Jakob Stoklund Olesen79443912011-10-26 01:47:48 +00002218 << "Range: " << Range << ". Size: " << TSize << ".\n\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002219
2220 // Get the MachineFunction which holds the current MBB. This is used when
2221 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002222 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002223
2224 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002225 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00002226 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002227
2228 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
2229
2230 // Create a new basic block to hold the code for loading the address
2231 // of the jump table, and jumping to it. Update successor information;
2232 // we will either branch to the default case for the switch, or the jump
2233 // table.
2234 MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2235 CurMF->insert(BBI, JumpTableBB);
Jakub Staszak7cc2b072011-06-16 20:22:37 +00002236
2237 addSuccessorWithWeight(CR.CaseBB, Default);
2238 addSuccessorWithWeight(CR.CaseBB, JumpTableBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002239
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002240 // Build a vector of destination BBs, corresponding to each target
2241 // of the jump table. If the value of the jump table slot corresponds to
2242 // a case statement, push the case's BB onto the vector, otherwise, push
2243 // the default BB.
2244 std::vector<MachineBasicBlock*> DestBBs;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002245 APInt TEI = First;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002246 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
Chris Lattner071c62f2010-01-25 23:26:13 +00002247 const APInt &Low = cast<ConstantInt>(I->Low)->getValue();
2248 const APInt &High = cast<ConstantInt>(I->High)->getValue();
Anton Korobeynikov23218582008-12-23 22:25:27 +00002249
Bob Wilsondb3a9e62013-09-09 19:14:35 +00002250 if (Low.sle(TEI) && TEI.sle(High)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002251 DestBBs.push_back(I->BB);
2252 if (TEI==High)
2253 ++I;
2254 } else {
2255 DestBBs.push_back(Default);
2256 }
2257 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002258
Manman Ren1a710fd2012-08-24 18:14:27 +00002259 // Calculate weight for each unique destination in CR.
2260 DenseMap<MachineBasicBlock*, uint32_t> DestWeights;
2261 if (FuncInfo.BPI)
2262 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
2263 DenseMap<MachineBasicBlock*, uint32_t>::iterator Itr =
2264 DestWeights.find(I->BB);
Stephen Lin155615d2013-07-08 00:37:03 +00002265 if (Itr != DestWeights.end())
Manman Ren1a710fd2012-08-24 18:14:27 +00002266 Itr->second += I->ExtraWeight;
2267 else
2268 DestWeights[I->BB] = I->ExtraWeight;
2269 }
2270
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002271 // Update successor info. Add one edge to each unique successor.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002272 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
2273 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002274 E = DestBBs.end(); I != E; ++I) {
2275 if (!SuccsHandled[(*I)->getNumber()]) {
2276 SuccsHandled[(*I)->getNumber()] = true;
Manman Ren1a710fd2012-08-24 18:14:27 +00002277 DenseMap<MachineBasicBlock*, uint32_t>::iterator Itr =
2278 DestWeights.find(*I);
2279 addSuccessorWithWeight(JumpTableBB, *I,
2280 Itr != DestWeights.end() ? Itr->second : 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002281 }
2282 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002283
Bob Wilsond1ec31d2010-03-18 18:42:41 +00002284 // Create a jump table index for this jump table.
Bill Wendlingba54bca2013-06-19 21:36:55 +00002285 unsigned JTEncoding = TLI->getJumpTableEncoding();
Chris Lattner071c62f2010-01-25 23:26:13 +00002286 unsigned JTI = CurMF->getOrCreateJumpTableInfo(JTEncoding)
Bob Wilsond1ec31d2010-03-18 18:42:41 +00002287 ->createJumpTableIndex(DestBBs);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002288
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002289 // Set the jump table information so that we can codegen it as a second
2290 // MachineBasicBlock
2291 JumpTable JT(-1U, JTI, JumpTableBB, Default);
Dan Gohman99be8ae2010-04-19 22:41:47 +00002292 JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == SwitchBB));
2293 if (CR.CaseBB == SwitchBB)
2294 visitJumpTableHeader(JT, JTH, SwitchBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002295
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002296 JTCases.push_back(JumpTableBlock(JTH, JT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002297 return true;
2298}
2299
2300/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
2301/// 2 subtrees.
Dan Gohman2048b852009-11-23 18:04:58 +00002302bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR,
2303 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00002304 const Value* SV,
Stephen Lin09f8ca32013-07-06 21:44:25 +00002305 MachineBasicBlock* Default,
2306 MachineBasicBlock* SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002307 // Get the MachineFunction which holds the current MBB. This is used when
2308 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002309 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002310
2311 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002312 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00002313 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002314
2315 Case& FrontCase = *CR.Range.first;
2316 Case& BackCase = *(CR.Range.second-1);
2317 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
2318
2319 // Size is the number of Cases represented by this range.
2320 unsigned Size = CR.Range.second - CR.Range.first;
2321
Chris Lattnere880efe2009-11-07 07:50:34 +00002322 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
2323 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002324 double FMetric = 0;
2325 CaseItr Pivot = CR.Range.first + Size/2;
2326
2327 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
2328 // (heuristically) allow us to emit JumpTable's later.
Chris Lattnere880efe2009-11-07 07:50:34 +00002329 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002330 for (CaseItr I = CR.Range.first, E = CR.Range.second;
2331 I!=E; ++I)
2332 TSize += I->size();
2333
Chris Lattnere880efe2009-11-07 07:50:34 +00002334 APInt LSize = FrontCase.size();
2335 APInt RSize = TSize-LSize;
David Greene4b69d992010-01-05 01:24:57 +00002336 DEBUG(dbgs() << "Selecting best pivot: \n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002337 << "First: " << First << ", Last: " << Last <<'\n'
2338 << "LSize: " << LSize << ", RSize: " << RSize << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002339 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
2340 J!=E; ++I, ++J) {
Chris Lattnere880efe2009-11-07 07:50:34 +00002341 const APInt &LEnd = cast<ConstantInt>(I->High)->getValue();
2342 const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002343 APInt Range = ComputeRange(LEnd, RBegin);
Stepan Dyatkovskiyc2c52a62012-05-15 06:50:18 +00002344 assert((Range - 2ULL).isNonNegative() &&
2345 "Invalid case distance");
Chris Lattnerc3e4e592011-04-09 06:57:13 +00002346 // Use volatile double here to avoid excess precision issues on some hosts,
2347 // e.g. that use 80-bit X87 registers.
2348 volatile double LDensity =
2349 (double)LSize.roundToDouble() /
Chris Lattnere880efe2009-11-07 07:50:34 +00002350 (LEnd - First + 1ULL).roundToDouble();
Chris Lattnerc3e4e592011-04-09 06:57:13 +00002351 volatile double RDensity =
2352 (double)RSize.roundToDouble() /
Chris Lattnere880efe2009-11-07 07:50:34 +00002353 (Last - RBegin + 1ULL).roundToDouble();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002354 double Metric = Range.logBase2()*(LDensity+RDensity);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002355 // Should always split in some non-trivial place
David Greene4b69d992010-01-05 01:24:57 +00002356 DEBUG(dbgs() <<"=>Step\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002357 << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n'
2358 << "LDensity: " << LDensity
2359 << ", RDensity: " << RDensity << '\n'
2360 << "Metric: " << Metric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002361 if (FMetric < Metric) {
2362 Pivot = J;
2363 FMetric = Metric;
David Greene4b69d992010-01-05 01:24:57 +00002364 DEBUG(dbgs() << "Current metric set to: " << FMetric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002365 }
2366
2367 LSize += J->size();
2368 RSize -= J->size();
2369 }
Bill Wendlingba54bca2013-06-19 21:36:55 +00002370
2371 const TargetLowering *TLI = TM.getTargetLowering();
2372 if (areJTsAllowed(*TLI)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002373 // If our case is dense we *really* should handle it earlier!
2374 assert((FMetric > 0) && "Should handle dense range earlier!");
2375 } else {
2376 Pivot = CR.Range.first + Size/2;
2377 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002378
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002379 CaseRange LHSR(CR.Range.first, Pivot);
2380 CaseRange RHSR(Pivot, CR.Range.second);
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00002381 const Constant *C = Pivot->Low;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002382 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002383
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002384 // We know that we branch to the LHS if the Value being switched on is
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002385 // less than the Pivot value, C. We use this to optimize our binary
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002386 // tree a bit, by recognizing that if SV is greater than or equal to the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002387 // LHS's Case Value, and that Case Value is exactly one less than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002388 // Pivot's Value, then we can branch directly to the LHS's Target,
2389 // rather than creating a leaf node for it.
2390 if ((LHSR.second - LHSR.first) == 1 &&
2391 LHSR.first->High == CR.GE &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00002392 cast<ConstantInt>(C)->getValue() ==
2393 (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002394 TrueBB = LHSR.first->BB;
2395 } else {
2396 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2397 CurMF->insert(BBI, TrueBB);
2398 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002399
2400 // Put SV in a virtual register to make it available from the new blocks.
2401 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002402 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002403
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002404 // Similar to the optimization above, if the Value being switched on is
2405 // known to be less than the Constant CR.LT, and the current Case Value
2406 // is CR.LT - 1, then we can branch directly to the target block for
2407 // the current Case Value, rather than emitting a RHS leaf node for it.
2408 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00002409 cast<ConstantInt>(RHSR.first->Low)->getValue() ==
2410 (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002411 FalseBB = RHSR.first->BB;
2412 } else {
2413 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2414 CurMF->insert(BBI, FalseBB);
2415 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002416
2417 // Put SV in a virtual register to make it available from the new blocks.
2418 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002419 }
2420
2421 // Create a CaseBlock record representing a conditional branch to
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002422 // the LHS node if the value being switched on SV is less than C.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002423 // Otherwise, branch to LHS.
Bob Wilsondb3a9e62013-09-09 19:14:35 +00002424 CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002425
Dan Gohman99be8ae2010-04-19 22:41:47 +00002426 if (CR.CaseBB == SwitchBB)
2427 visitSwitchCase(CB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002428 else
2429 SwitchCases.push_back(CB);
2430
2431 return true;
2432}
2433
2434/// handleBitTestsSwitchCase - if current case range has few destination and
2435/// range span less, than machine word bitwidth, encode case range into series
2436/// of masks and emit bit tests with these masks.
Dan Gohman2048b852009-11-23 18:04:58 +00002437bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR,
2438 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00002439 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00002440 MachineBasicBlock* Default,
Stephen Lin09f8ca32013-07-06 21:44:25 +00002441 MachineBasicBlock* SwitchBB) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00002442 const TargetLowering *TLI = TM.getTargetLowering();
2443 EVT PTy = TLI->getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002444 unsigned IntPtrBits = PTy.getSizeInBits();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002445
2446 Case& FrontCase = *CR.Range.first;
2447 Case& BackCase = *(CR.Range.second-1);
2448
2449 // Get the MachineFunction which holds the current MBB. This is used when
2450 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002451 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002452
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00002453 // If target does not have legal shift left, do not emit bit tests at all.
Matt Arsenault599c0af2013-10-21 19:24:15 +00002454 if (!TLI->isOperationLegal(ISD::SHL, PTy))
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00002455 return false;
2456
Anton Korobeynikov23218582008-12-23 22:25:27 +00002457 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002458 for (CaseItr I = CR.Range.first, E = CR.Range.second;
2459 I!=E; ++I) {
2460 // Single case counts one, case range - two.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002461 numCmps += (I->Low == I->High ? 1 : 2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002462 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002463
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002464 // Count unique destinations
2465 SmallSet<MachineBasicBlock*, 4> Dests;
2466 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
2467 Dests.insert(I->BB);
2468 if (Dests.size() > 3)
2469 // Don't bother the code below, if there are too much unique destinations
2470 return false;
2471 }
David Greene4b69d992010-01-05 01:24:57 +00002472 DEBUG(dbgs() << "Total number of unique destinations: "
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002473 << Dests.size() << '\n'
2474 << "Total number of comparisons: " << numCmps << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00002475
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002476 // Compute span of values.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002477 const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue();
2478 const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002479 APInt cmpRange = maxValue - minValue;
2480
David Greene4b69d992010-01-05 01:24:57 +00002481 DEBUG(dbgs() << "Compare range: " << cmpRange << '\n'
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002482 << "Low bound: " << minValue << '\n'
2483 << "High bound: " << maxValue << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00002484
Dan Gohmane0567812010-04-08 23:03:40 +00002485 if (cmpRange.uge(IntPtrBits) ||
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002486 (!(Dests.size() == 1 && numCmps >= 3) &&
2487 !(Dests.size() == 2 && numCmps >= 5) &&
2488 !(Dests.size() >= 3 && numCmps >= 6)))
2489 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002490
David Greene4b69d992010-01-05 01:24:57 +00002491 DEBUG(dbgs() << "Emitting bit tests\n");
Anton Korobeynikov23218582008-12-23 22:25:27 +00002492 APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth());
2493
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002494 // Optimize the case where all the case values fit in a
2495 // word without having to subtract minValue. In this case,
2496 // we can optimize away the subtraction.
Bob Wilsondb3a9e62013-09-09 19:14:35 +00002497 if (minValue.isNonNegative() && maxValue.slt(IntPtrBits)) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002498 cmpRange = maxValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002499 } else {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002500 lowBound = minValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002501 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002502
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002503 CaseBitsVector CasesBits;
2504 unsigned i, count = 0;
2505
2506 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
2507 MachineBasicBlock* Dest = I->BB;
2508 for (i = 0; i < count; ++i)
2509 if (Dest == CasesBits[i].BB)
2510 break;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002511
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002512 if (i == count) {
2513 assert((count < 3) && "Too much destinations to test!");
Manman Ren1a710fd2012-08-24 18:14:27 +00002514 CasesBits.push_back(CaseBits(0, Dest, 0, 0/*Weight*/));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002515 count++;
2516 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002517
2518 const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue();
2519 const APInt& highValue = cast<ConstantInt>(I->High)->getValue();
2520
2521 uint64_t lo = (lowValue - lowBound).getZExtValue();
2522 uint64_t hi = (highValue - lowBound).getZExtValue();
Manman Ren1a710fd2012-08-24 18:14:27 +00002523 CasesBits[i].ExtraWeight += I->ExtraWeight;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002524
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002525 for (uint64_t j = lo; j <= hi; j++) {
2526 CasesBits[i].Mask |= 1ULL << j;
2527 CasesBits[i].Bits++;
2528 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002529
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002530 }
2531 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
Anton Korobeynikov23218582008-12-23 22:25:27 +00002532
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002533 BitTestInfo BTC;
2534
2535 // Figure out which block is immediately after the current one.
2536 MachineFunction::iterator BBI = CR.CaseBB;
2537 ++BBI;
2538
2539 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
2540
David Greene4b69d992010-01-05 01:24:57 +00002541 DEBUG(dbgs() << "Cases:\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002542 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
David Greene4b69d992010-01-05 01:24:57 +00002543 DEBUG(dbgs() << "Mask: " << CasesBits[i].Mask
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002544 << ", Bits: " << CasesBits[i].Bits
2545 << ", BB: " << CasesBits[i].BB << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002546
2547 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2548 CurMF->insert(BBI, CaseBB);
2549 BTC.push_back(BitTestCase(CasesBits[i].Mask,
2550 CaseBB,
Manman Ren1a710fd2012-08-24 18:14:27 +00002551 CasesBits[i].BB, CasesBits[i].ExtraWeight));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002552
2553 // Put SV in a virtual register to make it available from the new blocks.
2554 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002555 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002556
2557 BitTestBlock BTB(lowBound, cmpRange, SV,
Evan Chengd08e5b42011-01-06 01:02:44 +00002558 -1U, MVT::Other, (CR.CaseBB == SwitchBB),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002559 CR.CaseBB, Default, BTC);
2560
Dan Gohman99be8ae2010-04-19 22:41:47 +00002561 if (CR.CaseBB == SwitchBB)
2562 visitBitTestHeader(BTB, SwitchBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002563
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002564 BitTestCases.push_back(BTB);
2565
2566 return true;
2567}
2568
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002569/// Clusterify - Transform simple list of Cases into list of CaseRange's
Dan Gohman2048b852009-11-23 18:04:58 +00002570size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases,
2571 const SwitchInst& SI) {
Bob Wilsondb3a9e62013-09-09 19:14:35 +00002572 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002573
Manman Ren1a710fd2012-08-24 18:14:27 +00002574 BranchProbabilityInfo *BPI = FuncInfo.BPI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002575 // Start with "simple" cases
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002576 for (SwitchInst::ConstCaseIt i = SI.case_begin(), e = SI.case_end();
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002577 i != e; ++i) {
2578 const BasicBlock *SuccBB = i.getCaseSuccessor();
Jakub Staszakc8f34de2011-07-29 22:25:21 +00002579 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SuccBB];
2580
Bob Wilsondb3a9e62013-09-09 19:14:35 +00002581 uint32_t ExtraWeight =
2582 BPI ? BPI->getEdgeWeight(SI.getParent(), i.getSuccessorIndex()) : 0;
2583
2584 Cases.push_back(Case(i.getCaseValue(), i.getCaseValue(),
2585 SMBB, ExtraWeight));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002586 }
Bob Wilsondb3a9e62013-09-09 19:14:35 +00002587 std::sort(Cases.begin(), Cases.end(), CaseCmp());
Stephen Lin155615d2013-07-08 00:37:03 +00002588
Bob Wilsondb3a9e62013-09-09 19:14:35 +00002589 // Merge case into clusters
2590 if (Cases.size() >= 2)
2591 // Must recompute end() each iteration because it may be
2592 // invalidated by erase if we hold on to it
2593 for (CaseItr I = Cases.begin(), J = llvm::next(Cases.begin());
2594 J != Cases.end(); ) {
2595 const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue();
2596 const APInt& currentValue = cast<ConstantInt>(I->High)->getValue();
2597 MachineBasicBlock* nextBB = J->BB;
2598 MachineBasicBlock* currentBB = I->BB;
Stephen Lin155615d2013-07-08 00:37:03 +00002599
Bob Wilsondb3a9e62013-09-09 19:14:35 +00002600 // If the two neighboring cases go to the same destination, merge them
2601 // into a single case.
2602 if ((nextValue - currentValue == 1) && (currentBB == nextBB)) {
2603 I->High = J->High;
2604 I->ExtraWeight += J->ExtraWeight;
2605 J = Cases.erase(J);
2606 } else {
2607 I = J++;
2608 }
2609 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002610
Bob Wilsondb3a9e62013-09-09 19:14:35 +00002611 for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
2612 if (I->Low != I->High)
2613 // A range counts double, since it requires two compares.
2614 ++numCmps;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002615 }
2616
2617 return numCmps;
2618}
2619
Jakob Stoklund Olesen2622f462010-09-30 19:44:31 +00002620void SelectionDAGBuilder::UpdateSplitBlock(MachineBasicBlock *First,
2621 MachineBasicBlock *Last) {
2622 // Update JTCases.
2623 for (unsigned i = 0, e = JTCases.size(); i != e; ++i)
2624 if (JTCases[i].first.HeaderBB == First)
2625 JTCases[i].first.HeaderBB = Last;
2626
2627 // Update BitTestCases.
2628 for (unsigned i = 0, e = BitTestCases.size(); i != e; ++i)
2629 if (BitTestCases[i].Parent == First)
2630 BitTestCases[i].Parent = Last;
2631}
2632
Dan Gohman46510a72010-04-15 01:51:59 +00002633void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) {
Dan Gohman84023e02010-07-10 09:00:22 +00002634 MachineBasicBlock *SwitchMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00002635
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002636 // Figure out which block is immediately after the current one.
2637 MachineBasicBlock *NextBlock = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002638 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
2639
2640 // If there is only the default destination, branch to it if it is not the
2641 // next basic block. Otherwise, just fall through.
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00002642 if (!SI.getNumCases()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002643 // Update machine-CFG edges.
2644
2645 // If this is not a fall-through branch, emit the branch.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002646 SwitchMBB->addSuccessor(Default);
Bill Wendling4533cac2010-01-28 21:51:40 +00002647 if (Default != NextBlock)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002648 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002649 MVT::Other, getControlRoot(),
2650 DAG.getBasicBlock(Default)));
Bill Wendling49fcff82009-12-21 22:30:11 +00002651
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002652 return;
2653 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002654
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002655 // If there are any non-default case statements, create a vector of Cases
2656 // representing each one, and sort the vector so that we can efficiently
2657 // create a binary search tree from them.
2658 CaseVector Cases;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002659 size_t numCmps = Clusterify(Cases, SI);
David Greene4b69d992010-01-05 01:24:57 +00002660 DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size()
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002661 << ". Total compares: " << numCmps << '\n');
Duncan Sands17001ce2011-10-18 12:44:00 +00002662 (void)numCmps;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002663
2664 // Get the Value to be switched on and default basic blocks, which will be
2665 // inserted into CaseBlock records, representing basic blocks in the binary
2666 // search tree.
Eli Friedmanbb5a7442011-09-29 20:21:17 +00002667 const Value *SV = SI.getCondition();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002668
2669 // Push the initial CaseRec onto the worklist
2670 CaseRecVector WorkList;
Dan Gohman99be8ae2010-04-19 22:41:47 +00002671 WorkList.push_back(CaseRec(SwitchMBB,0,0,
2672 CaseRange(Cases.begin(),Cases.end())));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002673
2674 while (!WorkList.empty()) {
2675 // Grab a record representing a case range to process off the worklist
2676 CaseRec CR = WorkList.back();
2677 WorkList.pop_back();
2678
Dan Gohman99be8ae2010-04-19 22:41:47 +00002679 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002680 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002681
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002682 // If the range has few cases (two or less) emit a series of specific
2683 // tests.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002684 if (handleSmallSwitchRange(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002685 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002686
Sebastian Pop1a37d7e2012-09-25 20:35:36 +00002687 // If the switch has more than N blocks, and is at least 40% dense, and the
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002688 // target supports indirect branches, then emit a jump table rather than
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002689 // lowering the switch to a binary tree of conditional branches.
Sebastian Pop1a37d7e2012-09-25 20:35:36 +00002690 // N defaults to 4 and is controlled via TLS.getMinimumJumpTableEntries().
Dan Gohman99be8ae2010-04-19 22:41:47 +00002691 if (handleJTSwitchCase(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002692 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002693
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002694 // Emit binary tree. We need to pick a pivot, and push left and right ranges
2695 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002696 handleBTSplitSwitchCase(CR, WorkList, SV, Default, SwitchMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002697 }
2698}
2699
Dan Gohman46510a72010-04-15 01:51:59 +00002700void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) {
Dan Gohman84023e02010-07-10 09:00:22 +00002701 MachineBasicBlock *IndirectBrMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00002702
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002703 // Update machine-CFG edges with unique successors.
Nadav Rotemee0ce152012-10-23 21:05:33 +00002704 SmallSet<BasicBlock*, 32> Done;
2705 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i) {
2706 BasicBlock *BB = I.getSuccessor(i);
2707 bool Inserted = Done.insert(BB);
2708 if (!Inserted)
2709 continue;
2710
2711 MachineBasicBlock *Succ = FuncInfo.MBBMap[BB];
Jakub Staszak7cc2b072011-06-16 20:22:37 +00002712 addSuccessorWithWeight(IndirectBrMBB, Succ);
2713 }
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002714
Andrew Trickac6d9be2013-05-25 02:42:55 +00002715 DAG.setRoot(DAG.getNode(ISD::BRIND, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002716 MVT::Other, getControlRoot(),
2717 getValue(I.getAddress())));
Bill Wendling49fcff82009-12-21 22:30:11 +00002718}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002719
Dan Gohman46510a72010-04-15 01:51:59 +00002720void SelectionDAGBuilder::visitFSub(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002721 // -0.0 - X --> fneg
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002722 Type *Ty = I.getType();
Chris Lattner2ca5c862011-02-15 00:14:00 +00002723 if (isa<Constant>(I.getOperand(0)) &&
2724 I.getOperand(0) == ConstantFP::getZeroValueForNegation(Ty)) {
2725 SDValue Op2 = getValue(I.getOperand(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00002726 setValue(&I, DAG.getNode(ISD::FNEG, getCurSDLoc(),
Chris Lattner2ca5c862011-02-15 00:14:00 +00002727 Op2.getValueType(), Op2));
2728 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002729 }
Bill Wendling49fcff82009-12-21 22:30:11 +00002730
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002731 visitBinary(I, ISD::FSUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002732}
2733
Dan Gohman46510a72010-04-15 01:51:59 +00002734void SelectionDAGBuilder::visitBinary(const User &I, unsigned OpCode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002735 SDValue Op1 = getValue(I.getOperand(0));
2736 SDValue Op2 = getValue(I.getOperand(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00002737 setValue(&I, DAG.getNode(OpCode, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002738 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002739}
2740
Dan Gohman46510a72010-04-15 01:51:59 +00002741void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002742 SDValue Op1 = getValue(I.getOperand(0));
2743 SDValue Op2 = getValue(I.getOperand(1));
Owen Anderson95771af2011-02-25 21:41:48 +00002744
Bill Wendlingba54bca2013-06-19 21:36:55 +00002745 EVT ShiftTy = TM.getTargetLowering()->getShiftAmountTy(Op2.getValueType());
Owen Anderson95771af2011-02-25 21:41:48 +00002746
Chris Lattnerd3027732011-02-13 09:02:52 +00002747 // Coerce the shift amount to the right type if we can.
2748 if (!I.getType()->isVectorTy() && Op2.getValueType() != ShiftTy) {
Chris Lattner915eeb42011-02-13 09:10:56 +00002749 unsigned ShiftSize = ShiftTy.getSizeInBits();
2750 unsigned Op2Size = Op2.getValueType().getSizeInBits();
Andrew Trickac6d9be2013-05-25 02:42:55 +00002751 SDLoc DL = getCurSDLoc();
Owen Anderson95771af2011-02-25 21:41:48 +00002752
Dan Gohman57fc82d2009-04-09 03:51:29 +00002753 // If the operand is smaller than the shift count type, promote it.
Chris Lattnerd3027732011-02-13 09:02:52 +00002754 if (ShiftSize > Op2Size)
2755 Op2 = DAG.getNode(ISD::ZERO_EXTEND, DL, ShiftTy, Op2);
Owen Anderson95771af2011-02-25 21:41:48 +00002756
Dan Gohman57fc82d2009-04-09 03:51:29 +00002757 // If the operand is larger than the shift count type but the shift
2758 // count type has enough bits to represent any shift value, truncate
2759 // it now. This is a common case and it exposes the truncate to
2760 // optimization early.
Chris Lattnerd3027732011-02-13 09:02:52 +00002761 else if (ShiftSize >= Log2_32_Ceil(Op2.getValueType().getSizeInBits()))
2762 Op2 = DAG.getNode(ISD::TRUNCATE, DL, ShiftTy, Op2);
2763 // Otherwise we'll need to temporarily settle for some other convenient
Chris Lattnere0751182011-02-13 19:09:16 +00002764 // type. Type legalization will make adjustments once the shiftee is split.
Chris Lattnerd3027732011-02-13 09:02:52 +00002765 else
Chris Lattnere0751182011-02-13 19:09:16 +00002766 Op2 = DAG.getZExtOrTrunc(Op2, DL, MVT::i32);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002767 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002768
Andrew Trickac6d9be2013-05-25 02:42:55 +00002769 setValue(&I, DAG.getNode(Opcode, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002770 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002771}
2772
Benjamin Kramer9c640302011-07-08 10:31:30 +00002773void SelectionDAGBuilder::visitSDiv(const User &I) {
Benjamin Kramer9c640302011-07-08 10:31:30 +00002774 SDValue Op1 = getValue(I.getOperand(0));
2775 SDValue Op2 = getValue(I.getOperand(1));
2776
2777 // Turn exact SDivs into multiplications.
2778 // FIXME: This should be in DAGCombiner, but it doesn't have access to the
2779 // exact bit.
Benjamin Kramer3492a4a2011-07-08 12:08:24 +00002780 if (isa<BinaryOperator>(&I) && cast<BinaryOperator>(&I)->isExact() &&
2781 !isa<ConstantSDNode>(Op1) &&
Benjamin Kramer9c640302011-07-08 10:31:30 +00002782 isa<ConstantSDNode>(Op2) && !cast<ConstantSDNode>(Op2)->isNullValue())
Bill Wendlingba54bca2013-06-19 21:36:55 +00002783 setValue(&I, TM.getTargetLowering()->BuildExactSDIV(Op1, Op2,
2784 getCurSDLoc(), DAG));
Benjamin Kramer9c640302011-07-08 10:31:30 +00002785 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00002786 setValue(&I, DAG.getNode(ISD::SDIV, getCurSDLoc(), Op1.getValueType(),
Benjamin Kramer9c640302011-07-08 10:31:30 +00002787 Op1, Op2));
2788}
2789
Dan Gohman46510a72010-04-15 01:51:59 +00002790void SelectionDAGBuilder::visitICmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002791 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002792 if (const ICmpInst *IC = dyn_cast<ICmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002793 predicate = IC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002794 else if (const ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002795 predicate = ICmpInst::Predicate(IC->getPredicate());
2796 SDValue Op1 = getValue(I.getOperand(0));
2797 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002798 ISD::CondCode Opcode = getICmpCondCode(predicate);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002799
Bill Wendlingba54bca2013-06-19 21:36:55 +00002800 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002801 setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Opcode));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002802}
2803
Dan Gohman46510a72010-04-15 01:51:59 +00002804void SelectionDAGBuilder::visitFCmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002805 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002806 if (const FCmpInst *FC = dyn_cast<FCmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002807 predicate = FC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002808 else if (const ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002809 predicate = FCmpInst::Predicate(FC->getPredicate());
2810 SDValue Op1 = getValue(I.getOperand(0));
2811 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002812 ISD::CondCode Condition = getFCmpCondCode(predicate);
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002813 if (TM.Options.NoNaNsFPMath)
2814 Condition = getFCmpCodeWithoutNaN(Condition);
Bill Wendlingba54bca2013-06-19 21:36:55 +00002815 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002816 setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Condition));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002817}
2818
Dan Gohman46510a72010-04-15 01:51:59 +00002819void SelectionDAGBuilder::visitSelect(const User &I) {
Owen Andersone50ed302009-08-10 22:56:29 +00002820 SmallVector<EVT, 4> ValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00002821 ComputeValueVTs(*TM.getTargetLowering(), I.getType(), ValueVTs);
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002822 unsigned NumValues = ValueVTs.size();
Bill Wendling49fcff82009-12-21 22:30:11 +00002823 if (NumValues == 0) return;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002824
Bill Wendling49fcff82009-12-21 22:30:11 +00002825 SmallVector<SDValue, 4> Values(NumValues);
2826 SDValue Cond = getValue(I.getOperand(0));
2827 SDValue TrueVal = getValue(I.getOperand(1));
2828 SDValue FalseVal = getValue(I.getOperand(2));
Duncan Sands28b77e92011-09-06 19:07:46 +00002829 ISD::NodeType OpCode = Cond.getValueType().isVector() ?
2830 ISD::VSELECT : ISD::SELECT;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002831
Bill Wendling4533cac2010-01-28 21:51:40 +00002832 for (unsigned i = 0; i != NumValues; ++i)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002833 Values[i] = DAG.getNode(OpCode, getCurSDLoc(),
Duncan Sands28b77e92011-09-06 19:07:46 +00002834 TrueVal.getNode()->getValueType(TrueVal.getResNo()+i),
Chris Lattnerb3e87b22010-03-12 07:15:36 +00002835 Cond,
Bill Wendling49fcff82009-12-21 22:30:11 +00002836 SDValue(TrueVal.getNode(),
2837 TrueVal.getResNo() + i),
2838 SDValue(FalseVal.getNode(),
2839 FalseVal.getResNo() + i));
2840
Andrew Trickac6d9be2013-05-25 02:42:55 +00002841 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002842 DAG.getVTList(&ValueVTs[0], NumValues),
2843 &Values[0], NumValues));
Bill Wendling49fcff82009-12-21 22:30:11 +00002844}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002845
Dan Gohman46510a72010-04-15 01:51:59 +00002846void SelectionDAGBuilder::visitTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002847 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2848 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002849 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002850 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002851}
2852
Dan Gohman46510a72010-04-15 01:51:59 +00002853void SelectionDAGBuilder::visitZExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002854 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2855 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2856 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002857 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002858 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002859}
2860
Dan Gohman46510a72010-04-15 01:51:59 +00002861void SelectionDAGBuilder::visitSExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002862 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2863 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2864 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002865 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002866 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002867}
2868
Dan Gohman46510a72010-04-15 01:51:59 +00002869void SelectionDAGBuilder::visitFPTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002870 // FPTrunc is never a no-op cast, no need to check
2871 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002872 const TargetLowering *TLI = TM.getTargetLowering();
2873 EVT DestVT = TLI->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002874 setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurSDLoc(),
Pete Cooperf57e1c22012-01-17 01:54:07 +00002875 DestVT, N,
Bill Wendlingba54bca2013-06-19 21:36:55 +00002876 DAG.getTargetConstant(0, TLI->getPointerTy())));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002877}
2878
Stephen Lin09f8ca32013-07-06 21:44:25 +00002879void SelectionDAGBuilder::visitFPExt(const User &I) {
Hal Finkel46bb70c2011-10-18 03:51:57 +00002880 // FPExt is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002881 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002882 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002883 setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002884}
2885
Dan Gohman46510a72010-04-15 01:51:59 +00002886void SelectionDAGBuilder::visitFPToUI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002887 // FPToUI is never a no-op cast, no need to check
2888 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002889 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002890 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002891}
2892
Dan Gohman46510a72010-04-15 01:51:59 +00002893void SelectionDAGBuilder::visitFPToSI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002894 // FPToSI is never a no-op cast, no need to check
2895 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002896 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002897 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002898}
2899
Dan Gohman46510a72010-04-15 01:51:59 +00002900void SelectionDAGBuilder::visitUIToFP(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002901 // UIToFP is never a no-op cast, no need to check
2902 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002903 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002904 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002905}
2906
Stephen Lin09f8ca32013-07-06 21:44:25 +00002907void SelectionDAGBuilder::visitSIToFP(const User &I) {
Bill Wendling181b6272008-10-19 20:34:04 +00002908 // SIToFP is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002909 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002910 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002911 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002912}
2913
Dan Gohman46510a72010-04-15 01:51:59 +00002914void SelectionDAGBuilder::visitPtrToInt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002915 // What to do depends on the size of the integer and the size of the pointer.
2916 // We can either truncate, zero extend, or no-op, accordingly.
2917 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002918 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002919 setValue(&I, DAG.getZExtOrTrunc(N, getCurSDLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002920}
2921
Dan Gohman46510a72010-04-15 01:51:59 +00002922void SelectionDAGBuilder::visitIntToPtr(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002923 // What to do depends on the size of the integer and the size of the pointer.
2924 // We can either truncate, zero extend, or no-op, accordingly.
2925 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002926 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002927 setValue(&I, DAG.getZExtOrTrunc(N, getCurSDLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002928}
2929
Dan Gohman46510a72010-04-15 01:51:59 +00002930void SelectionDAGBuilder::visitBitCast(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002931 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002932 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002933
Bill Wendling49fcff82009-12-21 22:30:11 +00002934 // BitCast assures us that source and destination are the same size so this is
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002935 // either a BITCAST or a no-op.
Bill Wendling4533cac2010-01-28 21:51:40 +00002936 if (DestVT != N.getValueType())
Andrew Trickac6d9be2013-05-25 02:42:55 +00002937 setValue(&I, DAG.getNode(ISD::BITCAST, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002938 DestVT, N)); // convert types.
2939 else
Bill Wendling49fcff82009-12-21 22:30:11 +00002940 setValue(&I, N); // noop cast.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002941}
2942
Matt Arsenault59d3ae62013-11-15 01:34:59 +00002943void SelectionDAGBuilder::visitAddrSpaceCast(const User &I) {
2944 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2945 const Value *SV = I.getOperand(0);
2946 SDValue N = getValue(SV);
2947 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
2948
2949 unsigned SrcAS = SV->getType()->getPointerAddressSpace();
2950 unsigned DestAS = I.getType()->getPointerAddressSpace();
2951
2952 if (!TLI.isNoopAddrSpaceCast(SrcAS, DestAS))
2953 N = DAG.getAddrSpaceCast(getCurSDLoc(), DestVT, N, SrcAS, DestAS);
2954
2955 setValue(&I, N);
2956}
2957
Dan Gohman46510a72010-04-15 01:51:59 +00002958void SelectionDAGBuilder::visitInsertElement(const User &I) {
Tom Stellard425b76c2013-08-05 22:22:01 +00002959 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002960 SDValue InVec = getValue(I.getOperand(0));
2961 SDValue InVal = getValue(I.getOperand(1));
Tom Stellard425b76c2013-08-05 22:22:01 +00002962 SDValue InIdx = DAG.getSExtOrTrunc(getValue(I.getOperand(2)),
2963 getCurSDLoc(), TLI.getVectorIdxTy());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002964 setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurSDLoc(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00002965 TM.getTargetLowering()->getValueType(I.getType()),
Bill Wendling4533cac2010-01-28 21:51:40 +00002966 InVec, InVal, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002967}
2968
Dan Gohman46510a72010-04-15 01:51:59 +00002969void SelectionDAGBuilder::visitExtractElement(const User &I) {
Tom Stellard425b76c2013-08-05 22:22:01 +00002970 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002971 SDValue InVec = getValue(I.getOperand(0));
Tom Stellard425b76c2013-08-05 22:22:01 +00002972 SDValue InIdx = DAG.getSExtOrTrunc(getValue(I.getOperand(1)),
2973 getCurSDLoc(), TLI.getVectorIdxTy());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002974 setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurSDLoc(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00002975 TM.getTargetLowering()->getValueType(I.getType()),
2976 InVec, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002977}
2978
Craig Topper51578342012-01-04 09:23:09 +00002979// Utility for visitShuffleVector - Return true if every element in Mask,
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00002980// beginning from position Pos and ending in Pos+Size, falls within the
Craig Topper51578342012-01-04 09:23:09 +00002981// specified sequential range [L, L+Pos). or is undef.
2982static bool isSequentialInRange(const SmallVectorImpl<int> &Mask,
Craig Topper23de31b2012-04-11 03:06:35 +00002983 unsigned Pos, unsigned Size, int Low) {
2984 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Craig Topper51578342012-01-04 09:23:09 +00002985 if (Mask[i] >= 0 && Mask[i] != Low)
Nate Begeman9008ca62009-04-27 18:41:29 +00002986 return false;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002987 return true;
2988}
2989
Dan Gohman46510a72010-04-15 01:51:59 +00002990void SelectionDAGBuilder::visitShuffleVector(const User &I) {
Mon P Wang230e4fa2008-11-21 04:25:21 +00002991 SDValue Src1 = getValue(I.getOperand(0));
2992 SDValue Src2 = getValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002993
Chris Lattner56243b82012-01-26 02:51:13 +00002994 SmallVector<int, 8> Mask;
2995 ShuffleVectorInst::getShuffleMask(cast<Constant>(I.getOperand(2)), Mask);
2996 unsigned MaskNumElts = Mask.size();
Bill Wendlingba54bca2013-06-19 21:36:55 +00002997
2998 const TargetLowering *TLI = TM.getTargetLowering();
2999 EVT VT = TLI->getValueType(I.getType());
Owen Andersone50ed302009-08-10 22:56:29 +00003000 EVT SrcVT = Src1.getValueType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00003001 unsigned SrcNumElts = SrcVT.getVectorNumElements();
Mon P Wangaeb06d22008-11-10 04:46:22 +00003002
Mon P Wangc7849c22008-11-16 05:06:27 +00003003 if (SrcNumElts == MaskNumElts) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003004 setValue(&I, DAG.getVectorShuffle(VT, getCurSDLoc(), Src1, Src2,
Bill Wendling4533cac2010-01-28 21:51:40 +00003005 &Mask[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00003006 return;
3007 }
3008
3009 // Normalize the shuffle vector since mask and vector length don't match.
Mon P Wangc7849c22008-11-16 05:06:27 +00003010 if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) {
3011 // Mask is longer than the source vectors and is a multiple of the source
3012 // vectors. We can use concatenate vector to make the mask and vectors
Mon P Wang230e4fa2008-11-21 04:25:21 +00003013 // lengths match.
Craig Topper51578342012-01-04 09:23:09 +00003014 if (SrcNumElts*2 == MaskNumElts) {
3015 // First check for Src1 in low and Src2 in high
3016 if (isSequentialInRange(Mask, 0, SrcNumElts, 0) &&
3017 isSequentialInRange(Mask, SrcNumElts, SrcNumElts, SrcNumElts)) {
3018 // The shuffle is concatenating two vectors together.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003019 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurSDLoc(),
Craig Topper51578342012-01-04 09:23:09 +00003020 VT, Src1, Src2));
3021 return;
3022 }
3023 // Then check for Src2 in low and Src1 in high
3024 if (isSequentialInRange(Mask, 0, SrcNumElts, SrcNumElts) &&
3025 isSequentialInRange(Mask, SrcNumElts, SrcNumElts, 0)) {
3026 // The shuffle is concatenating two vectors together.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003027 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurSDLoc(),
Craig Topper51578342012-01-04 09:23:09 +00003028 VT, Src2, Src1));
3029 return;
3030 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00003031 }
3032
Mon P Wangc7849c22008-11-16 05:06:27 +00003033 // Pad both vectors with undefs to make them the same length as the mask.
3034 unsigned NumConcat = MaskNumElts / SrcNumElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00003035 bool Src1U = Src1.getOpcode() == ISD::UNDEF;
3036 bool Src2U = Src2.getOpcode() == ISD::UNDEF;
Dale Johannesene8d72302009-02-06 23:05:02 +00003037 SDValue UndefVal = DAG.getUNDEF(SrcVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00003038
Nate Begeman9008ca62009-04-27 18:41:29 +00003039 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
3040 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
Mon P Wang230e4fa2008-11-21 04:25:21 +00003041 MOps1[0] = Src1;
3042 MOps2[0] = Src2;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003043
3044 Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003045 getCurSDLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003046 &MOps1[0], NumConcat);
3047 Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003048 getCurSDLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003049 &MOps2[0], NumConcat);
Mon P Wang230e4fa2008-11-21 04:25:21 +00003050
Mon P Wangaeb06d22008-11-10 04:46:22 +00003051 // Readjust mask for new input vector length.
Nate Begeman9008ca62009-04-27 18:41:29 +00003052 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003053 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003054 int Idx = Mask[i];
Craig Topper23de31b2012-04-11 03:06:35 +00003055 if (Idx >= (int)SrcNumElts)
3056 Idx -= SrcNumElts - MaskNumElts;
3057 MappedOps.push_back(Idx);
Mon P Wangaeb06d22008-11-10 04:46:22 +00003058 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003059
Andrew Trickac6d9be2013-05-25 02:42:55 +00003060 setValue(&I, DAG.getVectorShuffle(VT, getCurSDLoc(), Src1, Src2,
Bill Wendling4533cac2010-01-28 21:51:40 +00003061 &MappedOps[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00003062 return;
3063 }
3064
Mon P Wangc7849c22008-11-16 05:06:27 +00003065 if (SrcNumElts > MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00003066 // Analyze the access pattern of the vector to see if we can extract
3067 // two subvectors and do the shuffle. The analysis is done by calculating
3068 // the range of elements the mask access on both vectors.
Craig Topper10612dc2012-04-08 23:15:04 +00003069 int MinRange[2] = { static_cast<int>(SrcNumElts),
3070 static_cast<int>(SrcNumElts)};
Mon P Wangc7849c22008-11-16 05:06:27 +00003071 int MaxRange[2] = {-1, -1};
3072
Nate Begeman5a5ca152009-04-29 05:20:52 +00003073 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003074 int Idx = Mask[i];
Craig Topper10612dc2012-04-08 23:15:04 +00003075 unsigned Input = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003076 if (Idx < 0)
3077 continue;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003078
Nate Begeman5a5ca152009-04-29 05:20:52 +00003079 if (Idx >= (int)SrcNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003080 Input = 1;
3081 Idx -= SrcNumElts;
Mon P Wangaeb06d22008-11-10 04:46:22 +00003082 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003083 if (Idx > MaxRange[Input])
3084 MaxRange[Input] = Idx;
3085 if (Idx < MinRange[Input])
3086 MinRange[Input] = Idx;
Mon P Wangaeb06d22008-11-10 04:46:22 +00003087 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00003088
Mon P Wangc7849c22008-11-16 05:06:27 +00003089 // Check if the access is smaller than the vector size and can we find
3090 // a reasonable extract index.
Craig Topper10612dc2012-04-08 23:15:04 +00003091 int RangeUse[2] = { -1, -1 }; // 0 = Unused, 1 = Extract, -1 = Can not
3092 // Extract.
Mon P Wangc7849c22008-11-16 05:06:27 +00003093 int StartIdx[2]; // StartIdx to extract from
Craig Topper10612dc2012-04-08 23:15:04 +00003094 for (unsigned Input = 0; Input < 2; ++Input) {
3095 if (MinRange[Input] >= (int)SrcNumElts && MaxRange[Input] < 0) {
Mon P Wangc7849c22008-11-16 05:06:27 +00003096 RangeUse[Input] = 0; // Unused
3097 StartIdx[Input] = 0;
Craig Topperf873dde2012-04-08 17:53:33 +00003098 continue;
Mon P Wang230e4fa2008-11-21 04:25:21 +00003099 }
Craig Topperf873dde2012-04-08 17:53:33 +00003100
3101 // Find a good start index that is a multiple of the mask length. Then
3102 // see if the rest of the elements are in range.
3103 StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts;
3104 if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts &&
3105 StartIdx[Input] + MaskNumElts <= SrcNumElts)
3106 RangeUse[Input] = 1; // Extract from a multiple of the mask length.
Mon P Wangc7849c22008-11-16 05:06:27 +00003107 }
3108
Bill Wendling636e2582009-08-21 18:16:06 +00003109 if (RangeUse[0] == 0 && RangeUse[1] == 0) {
Bill Wendling4533cac2010-01-28 21:51:40 +00003110 setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
Mon P Wangc7849c22008-11-16 05:06:27 +00003111 return;
3112 }
Craig Topper10612dc2012-04-08 23:15:04 +00003113 if (RangeUse[0] >= 0 && RangeUse[1] >= 0) {
Mon P Wangc7849c22008-11-16 05:06:27 +00003114 // Extract appropriate subvector and generate a vector shuffle
Craig Topper10612dc2012-04-08 23:15:04 +00003115 for (unsigned Input = 0; Input < 2; ++Input) {
Bill Wendling87710f02009-12-21 23:47:40 +00003116 SDValue &Src = Input == 0 ? Src1 : Src2;
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003117 if (RangeUse[Input] == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00003118 Src = DAG.getUNDEF(VT);
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003119 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00003120 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurSDLoc(), VT,
Tom Stellard425b76c2013-08-05 22:22:01 +00003121 Src, DAG.getConstant(StartIdx[Input],
3122 TLI->getVectorIdxTy()));
Mon P Wangaeb06d22008-11-10 04:46:22 +00003123 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003124
Mon P Wangc7849c22008-11-16 05:06:27 +00003125 // Calculate new mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00003126 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003127 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003128 int Idx = Mask[i];
Craig Topper23de31b2012-04-11 03:06:35 +00003129 if (Idx >= 0) {
3130 if (Idx < (int)SrcNumElts)
3131 Idx -= StartIdx[0];
3132 else
3133 Idx -= SrcNumElts + StartIdx[1] - MaskNumElts;
3134 }
3135 MappedOps.push_back(Idx);
Mon P Wangc7849c22008-11-16 05:06:27 +00003136 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003137
Andrew Trickac6d9be2013-05-25 02:42:55 +00003138 setValue(&I, DAG.getVectorShuffle(VT, getCurSDLoc(), Src1, Src2,
Bill Wendling4533cac2010-01-28 21:51:40 +00003139 &MappedOps[0]));
Mon P Wangc7849c22008-11-16 05:06:27 +00003140 return;
Mon P Wangaeb06d22008-11-10 04:46:22 +00003141 }
3142 }
3143
Mon P Wangc7849c22008-11-16 05:06:27 +00003144 // We can't use either concat vectors or extract subvectors so fall back to
3145 // replacing the shuffle with extract and build vector.
3146 // to insert and build vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003147 EVT EltVT = VT.getVectorElementType();
Tom Stellard425b76c2013-08-05 22:22:01 +00003148 EVT IdxVT = TLI->getVectorIdxTy();
Mon P Wangaeb06d22008-11-10 04:46:22 +00003149 SmallVector<SDValue,8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003150 for (unsigned i = 0; i != MaskNumElts; ++i) {
Craig Topper23de31b2012-04-11 03:06:35 +00003151 int Idx = Mask[i];
3152 SDValue Res;
3153
3154 if (Idx < 0) {
3155 Res = DAG.getUNDEF(EltVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00003156 } else {
Craig Topper23de31b2012-04-11 03:06:35 +00003157 SDValue &Src = Idx < (int)SrcNumElts ? Src1 : Src2;
3158 if (Idx >= (int)SrcNumElts) Idx -= SrcNumElts;
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003159
Andrew Trickac6d9be2013-05-25 02:42:55 +00003160 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurSDLoc(),
Tom Stellard425b76c2013-08-05 22:22:01 +00003161 EltVT, Src, DAG.getConstant(Idx, IdxVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00003162 }
Craig Topper23de31b2012-04-11 03:06:35 +00003163
3164 Ops.push_back(Res);
Mon P Wangaeb06d22008-11-10 04:46:22 +00003165 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003166
Andrew Trickac6d9be2013-05-25 02:42:55 +00003167 setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00003168 VT, &Ops[0], Ops.size()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003169}
3170
Dan Gohman46510a72010-04-15 01:51:59 +00003171void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003172 const Value *Op0 = I.getOperand(0);
3173 const Value *Op1 = I.getOperand(1);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003174 Type *AggTy = I.getType();
3175 Type *ValTy = Op1->getType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003176 bool IntoUndef = isa<UndefValue>(Op0);
3177 bool FromUndef = isa<UndefValue>(Op1);
3178
Jay Foadfc6d3a42011-07-13 10:26:04 +00003179 unsigned LinearIndex = ComputeLinearIndex(AggTy, I.getIndices());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003180
Bill Wendlingba54bca2013-06-19 21:36:55 +00003181 const TargetLowering *TLI = TM.getTargetLowering();
Owen Andersone50ed302009-08-10 22:56:29 +00003182 SmallVector<EVT, 4> AggValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003183 ComputeValueVTs(*TLI, AggTy, AggValueVTs);
Owen Andersone50ed302009-08-10 22:56:29 +00003184 SmallVector<EVT, 4> ValValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003185 ComputeValueVTs(*TLI, ValTy, ValValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003186
3187 unsigned NumAggValues = AggValueVTs.size();
3188 unsigned NumValValues = ValValueVTs.size();
3189 SmallVector<SDValue, 4> Values(NumAggValues);
3190
3191 SDValue Agg = getValue(Op0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003192 unsigned i = 0;
3193 // Copy the beginning value(s) from the original aggregate.
3194 for (; i != LinearIndex; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00003195 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003196 SDValue(Agg.getNode(), Agg.getResNo() + i);
3197 // Copy values from the inserted value(s).
Rafael Espindola3fa82832011-05-13 15:18:06 +00003198 if (NumValValues) {
3199 SDValue Val = getValue(Op1);
3200 for (; i != LinearIndex + NumValValues; ++i)
3201 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
3202 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
3203 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003204 // Copy remaining value(s) from the original aggregate.
3205 for (; i != NumAggValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00003206 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003207 SDValue(Agg.getNode(), Agg.getResNo() + i);
3208
Andrew Trickac6d9be2013-05-25 02:42:55 +00003209 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00003210 DAG.getVTList(&AggValueVTs[0], NumAggValues),
3211 &Values[0], NumAggValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003212}
3213
Dan Gohman46510a72010-04-15 01:51:59 +00003214void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003215 const Value *Op0 = I.getOperand(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003216 Type *AggTy = Op0->getType();
3217 Type *ValTy = I.getType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003218 bool OutOfUndef = isa<UndefValue>(Op0);
3219
Jay Foadfc6d3a42011-07-13 10:26:04 +00003220 unsigned LinearIndex = ComputeLinearIndex(AggTy, I.getIndices());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003221
Bill Wendlingba54bca2013-06-19 21:36:55 +00003222 const TargetLowering *TLI = TM.getTargetLowering();
Owen Andersone50ed302009-08-10 22:56:29 +00003223 SmallVector<EVT, 4> ValValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003224 ComputeValueVTs(*TLI, ValTy, ValValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003225
3226 unsigned NumValValues = ValValueVTs.size();
Rafael Espindola3fa82832011-05-13 15:18:06 +00003227
3228 // Ignore a extractvalue that produces an empty object
3229 if (!NumValValues) {
3230 setValue(&I, DAG.getUNDEF(MVT(MVT::Other)));
3231 return;
3232 }
3233
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003234 SmallVector<SDValue, 4> Values(NumValValues);
3235
3236 SDValue Agg = getValue(Op0);
3237 // Copy out the selected value(s).
3238 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
3239 Values[i - LinearIndex] =
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00003240 OutOfUndef ?
Dale Johannesene8d72302009-02-06 23:05:02 +00003241 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00003242 SDValue(Agg.getNode(), Agg.getResNo() + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003243
Andrew Trickac6d9be2013-05-25 02:42:55 +00003244 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00003245 DAG.getVTList(&ValValueVTs[0], NumValValues),
3246 &Values[0], NumValValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003247}
3248
Dan Gohman46510a72010-04-15 01:51:59 +00003249void SelectionDAGBuilder::visitGetElementPtr(const User &I) {
Matt Arsenaultff718122013-10-21 20:03:54 +00003250 Value *Op0 = I.getOperand(0);
Nadav Rotem1c239202012-02-28 14:13:19 +00003251 // Note that the pointer operand may be a vector of pointers. Take the scalar
3252 // element which holds a pointer.
Matt Arsenaultff718122013-10-21 20:03:54 +00003253 Type *Ty = Op0->getType()->getScalarType();
3254 unsigned AS = Ty->getPointerAddressSpace();
3255 SDValue N = getValue(Op0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003256
Dan Gohman46510a72010-04-15 01:51:59 +00003257 for (GetElementPtrInst::const_op_iterator OI = I.op_begin()+1, E = I.op_end();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003258 OI != E; ++OI) {
Dan Gohman46510a72010-04-15 01:51:59 +00003259 const Value *Idx = *OI;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003260 if (StructType *StTy = dyn_cast<StructType>(Ty)) {
Duncan Sandsb2df01a2012-11-13 13:01:58 +00003261 unsigned Field = cast<Constant>(Idx)->getUniqueInteger().getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003262 if (Field) {
3263 // N = N + Offset
3264 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003265 N = DAG.getNode(ISD::ADD, getCurSDLoc(), N.getValueType(), N,
Duncan Sandsb2df01a2012-11-13 13:01:58 +00003266 DAG.getConstant(Offset, N.getValueType()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003267 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00003268
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003269 Ty = StTy->getElementType(Field);
3270 } else {
3271 Ty = cast<SequentialType>(Ty)->getElementType();
3272
3273 // If this is a constant subscript, handle it quickly.
Bill Wendlingba54bca2013-06-19 21:36:55 +00003274 const TargetLowering *TLI = TM.getTargetLowering();
Dan Gohman46510a72010-04-15 01:51:59 +00003275 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Dan Gohmane368b462010-06-18 14:22:04 +00003276 if (CI->isZero()) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003277 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +00003278 TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Evan Cheng65b52df2009-02-09 21:01:06 +00003279 SDValue OffsVal;
Tom Stellardda25cd32013-08-26 15:05:36 +00003280 EVT PTy = TLI->getPointerTy(AS);
Owen Anderson77547be2009-08-10 18:56:59 +00003281 unsigned PtrBits = PTy.getSizeInBits();
Bill Wendlinge1a90422009-12-21 23:10:19 +00003282 if (PtrBits < 64)
Tom Stellardda25cd32013-08-26 15:05:36 +00003283 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), PTy,
Owen Anderson825b72b2009-08-11 20:47:22 +00003284 DAG.getConstant(Offs, MVT::i64));
Bill Wendlinge1a90422009-12-21 23:10:19 +00003285 else
Tom Stellardda25cd32013-08-26 15:05:36 +00003286 OffsVal = DAG.getConstant(Offs, PTy);
Bill Wendlinge1a90422009-12-21 23:10:19 +00003287
Andrew Trickac6d9be2013-05-25 02:42:55 +00003288 N = DAG.getNode(ISD::ADD, getCurSDLoc(), N.getValueType(), N,
Evan Chengb1032a82009-02-09 20:54:38 +00003289 OffsVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003290 continue;
3291 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003292
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003293 // N = N + Idx * ElementSize;
Tom Stellardda25cd32013-08-26 15:05:36 +00003294 APInt ElementSize = APInt(TLI->getPointerSizeInBits(AS),
Dan Gohman7abbd042009-10-23 17:57:43 +00003295 TD->getTypeAllocSize(Ty));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003296 SDValue IdxN = getValue(Idx);
3297
3298 // If the index is smaller or larger than intptr_t, truncate or extend
3299 // it.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003300 IdxN = DAG.getSExtOrTrunc(IdxN, getCurSDLoc(), N.getValueType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003301
3302 // If this is a multiply by a power of two, turn it into a shl
3303 // immediately. This is a very common case.
3304 if (ElementSize != 1) {
Dan Gohman7abbd042009-10-23 17:57:43 +00003305 if (ElementSize.isPowerOf2()) {
3306 unsigned Amt = ElementSize.logBase2();
Andrew Trickac6d9be2013-05-25 02:42:55 +00003307 IdxN = DAG.getNode(ISD::SHL, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003308 N.getValueType(), IdxN,
Nadav Rotem16087692011-12-05 06:29:09 +00003309 DAG.getConstant(Amt, IdxN.getValueType()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003310 } else {
Duncan Sandsb2df01a2012-11-13 13:01:58 +00003311 SDValue Scale = DAG.getConstant(ElementSize, IdxN.getValueType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003312 IdxN = DAG.getNode(ISD::MUL, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003313 N.getValueType(), IdxN, Scale);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003314 }
3315 }
3316
Andrew Trickac6d9be2013-05-25 02:42:55 +00003317 N = DAG.getNode(ISD::ADD, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003318 N.getValueType(), N, IdxN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003319 }
3320 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00003321
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003322 setValue(&I, N);
3323}
3324
Dan Gohman46510a72010-04-15 01:51:59 +00003325void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003326 // If this is a fixed sized alloca in the entry block of the function,
3327 // allocate it statically on the stack.
3328 if (FuncInfo.StaticAllocaMap.count(&I))
3329 return; // getValue will auto-populate this.
3330
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003331 Type *Ty = I.getAllocatedType();
Bill Wendlingba54bca2013-06-19 21:36:55 +00003332 const TargetLowering *TLI = TM.getTargetLowering();
3333 uint64_t TySize = TLI->getDataLayout()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003334 unsigned Align =
Bill Wendlingba54bca2013-06-19 21:36:55 +00003335 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003336 I.getAlignment());
3337
3338 SDValue AllocSize = getValue(I.getArraySize());
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003339
Bill Wendlingba54bca2013-06-19 21:36:55 +00003340 EVT IntPtr = TLI->getPointerTy();
Dan Gohmanf75a7d32010-05-28 01:14:11 +00003341 if (AllocSize.getValueType() != IntPtr)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003342 AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurSDLoc(), IntPtr);
Dan Gohmanf75a7d32010-05-28 01:14:11 +00003343
Andrew Trickac6d9be2013-05-25 02:42:55 +00003344 AllocSize = DAG.getNode(ISD::MUL, getCurSDLoc(), IntPtr,
Dan Gohmanf75a7d32010-05-28 01:14:11 +00003345 AllocSize,
3346 DAG.getConstant(TySize, IntPtr));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003347
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003348 // Handle alignment. If the requested alignment is less than or equal to
3349 // the stack alignment, ignore it. If the size is greater than or equal to
3350 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00003351 unsigned StackAlign = TM.getFrameLowering()->getStackAlignment();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003352 if (Align <= StackAlign)
3353 Align = 0;
3354
3355 // Round the size of the allocation up to the stack alignment size
3356 // by add SA-1 to the size.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003357 AllocSize = DAG.getNode(ISD::ADD, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003358 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003359 DAG.getIntPtrConstant(StackAlign-1));
Bill Wendling856ff412009-12-22 00:12:37 +00003360
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003361 // Mask out the low bits for alignment purposes.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003362 AllocSize = DAG.getNode(ISD::AND, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003363 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003364 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
3365
3366 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Owen Anderson825b72b2009-08-11 20:47:22 +00003367 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003368 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurSDLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003369 VTs, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003370 setValue(&I, DSA);
3371 DAG.setRoot(DSA.getValue(1));
Bill Wendling856ff412009-12-22 00:12:37 +00003372
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003373 // Inform the Frame Information that we have just allocated a variable-sized
3374 // object.
Bob Wilson8f637ad2013-02-08 20:35:15 +00003375 FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject(Align ? Align : 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003376}
3377
Dan Gohman46510a72010-04-15 01:51:59 +00003378void SelectionDAGBuilder::visitLoad(const LoadInst &I) {
Eli Friedman327236c2011-08-24 20:50:09 +00003379 if (I.isAtomic())
3380 return visitAtomicLoad(I);
3381
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003382 const Value *SV = I.getOperand(0);
3383 SDValue Ptr = getValue(SV);
3384
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003385 Type *Ty = I.getType();
David Greene1e559442010-02-15 17:00:31 +00003386
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003387 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00003388 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Pete Cooperd752e0f2011-11-08 18:42:53 +00003389 bool isInvariant = I.getMetadata("invariant.load") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003390 unsigned Alignment = I.getAlignment();
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00003391 const MDNode *TBAAInfo = I.getMetadata(LLVMContext::MD_tbaa);
Rafael Espindola95d594c2012-03-31 18:14:00 +00003392 const MDNode *Ranges = I.getMetadata(LLVMContext::MD_range);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003393
Owen Andersone50ed302009-08-10 22:56:29 +00003394 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003395 SmallVector<uint64_t, 4> Offsets;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003396 ComputeValueVTs(*TM.getTargetLowering(), Ty, ValueVTs, &Offsets);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003397 unsigned NumValues = ValueVTs.size();
3398 if (NumValues == 0)
3399 return;
3400
3401 SDValue Root;
3402 bool ConstantMemory = false;
Andrew Trickde91f3c2010-11-12 17:50:46 +00003403 if (I.isVolatile() || NumValues > MaxParallelChains)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003404 // Serialize volatile loads with other side effects.
3405 Root = getRoot();
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00003406 else if (AA->pointsToConstantMemory(
3407 AliasAnalysis::Location(SV, AA->getTypeStoreSize(Ty), TBAAInfo))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003408 // Do not serialize (non-volatile) loads of constant memory with anything.
3409 Root = DAG.getEntryNode();
3410 ConstantMemory = true;
3411 } else {
3412 // Do not serialize non-volatile loads against each other.
3413 Root = DAG.getRoot();
3414 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003415
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003416 SmallVector<SDValue, 4> Values(NumValues);
Andrew Trickde91f3c2010-11-12 17:50:46 +00003417 SmallVector<SDValue, 4> Chains(std::min(unsigned(MaxParallelChains),
3418 NumValues));
Owen Andersone50ed302009-08-10 22:56:29 +00003419 EVT PtrVT = Ptr.getValueType();
Andrew Trickde91f3c2010-11-12 17:50:46 +00003420 unsigned ChainI = 0;
3421 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
3422 // Serializing loads here may result in excessive register pressure, and
3423 // TokenFactor places arbitrary choke points on the scheduler. SD scheduling
3424 // could recover a bit by hoisting nodes upward in the chain by recognizing
3425 // they are side-effect free or do not alias. The optimizer should really
3426 // avoid this case by converting large object/array copies to llvm.memcpy
3427 // (MaxParallelChains should always remain as failsafe).
3428 if (ChainI == MaxParallelChains) {
3429 assert(PendingLoads.empty() && "PendingLoads must be serialized first");
Andrew Trickac6d9be2013-05-25 02:42:55 +00003430 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003431 MVT::Other, &Chains[0], ChainI);
3432 Root = Chain;
3433 ChainI = 0;
3434 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00003435 SDValue A = DAG.getNode(ISD::ADD, getCurSDLoc(),
Bill Wendling856ff412009-12-22 00:12:37 +00003436 PtrVT, Ptr,
3437 DAG.getConstant(Offsets[i], PtrVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003438 SDValue L = DAG.getLoad(ValueVTs[i], getCurSDLoc(), Root,
Michael J. Spencere70c5262010-10-16 08:25:21 +00003439 A, MachinePointerInfo(SV, Offsets[i]), isVolatile,
Rafael Espindola95d594c2012-03-31 18:14:00 +00003440 isNonTemporal, isInvariant, Alignment, TBAAInfo,
3441 Ranges);
Bill Wendling856ff412009-12-22 00:12:37 +00003442
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003443 Values[i] = L;
Andrew Trickde91f3c2010-11-12 17:50:46 +00003444 Chains[ChainI] = L.getValue(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003445 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003446
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003447 if (!ConstantMemory) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003448 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003449 MVT::Other, &Chains[0], ChainI);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003450 if (isVolatile)
3451 DAG.setRoot(Chain);
3452 else
3453 PendingLoads.push_back(Chain);
3454 }
3455
Andrew Trickac6d9be2013-05-25 02:42:55 +00003456 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00003457 DAG.getVTList(&ValueVTs[0], NumValues),
3458 &Values[0], NumValues));
Bill Wendling856ff412009-12-22 00:12:37 +00003459}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003460
Dan Gohman46510a72010-04-15 01:51:59 +00003461void SelectionDAGBuilder::visitStore(const StoreInst &I) {
Eli Friedman327236c2011-08-24 20:50:09 +00003462 if (I.isAtomic())
3463 return visitAtomicStore(I);
3464
Dan Gohman46510a72010-04-15 01:51:59 +00003465 const Value *SrcV = I.getOperand(0);
3466 const Value *PtrV = I.getOperand(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003467
Owen Andersone50ed302009-08-10 22:56:29 +00003468 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003469 SmallVector<uint64_t, 4> Offsets;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003470 ComputeValueVTs(*TM.getTargetLowering(), SrcV->getType(), ValueVTs, &Offsets);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003471 unsigned NumValues = ValueVTs.size();
3472 if (NumValues == 0)
3473 return;
3474
3475 // Get the lowered operands. Note that we do this after
3476 // checking if NumResults is zero, because with zero results
3477 // the operands won't have values in the map.
3478 SDValue Src = getValue(SrcV);
3479 SDValue Ptr = getValue(PtrV);
3480
3481 SDValue Root = getRoot();
Andrew Trickde91f3c2010-11-12 17:50:46 +00003482 SmallVector<SDValue, 4> Chains(std::min(unsigned(MaxParallelChains),
3483 NumValues));
Owen Andersone50ed302009-08-10 22:56:29 +00003484 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003485 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00003486 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003487 unsigned Alignment = I.getAlignment();
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00003488 const MDNode *TBAAInfo = I.getMetadata(LLVMContext::MD_tbaa);
Bill Wendling856ff412009-12-22 00:12:37 +00003489
Andrew Trickde91f3c2010-11-12 17:50:46 +00003490 unsigned ChainI = 0;
3491 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
3492 // See visitLoad comments.
3493 if (ChainI == MaxParallelChains) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003494 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003495 MVT::Other, &Chains[0], ChainI);
3496 Root = Chain;
3497 ChainI = 0;
3498 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00003499 SDValue Add = DAG.getNode(ISD::ADD, getCurSDLoc(), PtrVT, Ptr,
Bill Wendling856ff412009-12-22 00:12:37 +00003500 DAG.getConstant(Offsets[i], PtrVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003501 SDValue St = DAG.getStore(Root, getCurSDLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003502 SDValue(Src.getNode(), Src.getResNo() + i),
3503 Add, MachinePointerInfo(PtrV, Offsets[i]),
3504 isVolatile, isNonTemporal, Alignment, TBAAInfo);
3505 Chains[ChainI] = St;
Bill Wendling856ff412009-12-22 00:12:37 +00003506 }
3507
Andrew Trickac6d9be2013-05-25 02:42:55 +00003508 SDValue StoreNode = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003509 MVT::Other, &Chains[0], ChainI);
Devang Patel7e13efa2010-10-26 22:14:52 +00003510 DAG.setRoot(StoreNode);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003511}
3512
Eli Friedman26689ac2011-08-03 21:06:02 +00003513static SDValue InsertFenceForAtomic(SDValue Chain, AtomicOrdering Order,
Eli Friedman327236c2011-08-24 20:50:09 +00003514 SynchronizationScope Scope,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003515 bool Before, SDLoc dl,
Eli Friedman26689ac2011-08-03 21:06:02 +00003516 SelectionDAG &DAG,
3517 const TargetLowering &TLI) {
3518 // Fence, if necessary
3519 if (Before) {
Eli Friedman069e2ed2011-08-26 02:59:24 +00003520 if (Order == AcquireRelease || Order == SequentiallyConsistent)
Eli Friedman26689ac2011-08-03 21:06:02 +00003521 Order = Release;
3522 else if (Order == Acquire || Order == Monotonic)
3523 return Chain;
3524 } else {
3525 if (Order == AcquireRelease)
3526 Order = Acquire;
3527 else if (Order == Release || Order == Monotonic)
3528 return Chain;
3529 }
3530 SDValue Ops[3];
3531 Ops[0] = Chain;
Eli Friedman327236c2011-08-24 20:50:09 +00003532 Ops[1] = DAG.getConstant(Order, TLI.getPointerTy());
3533 Ops[2] = DAG.getConstant(Scope, TLI.getPointerTy());
Eli Friedman26689ac2011-08-03 21:06:02 +00003534 return DAG.getNode(ISD::ATOMIC_FENCE, dl, MVT::Other, Ops, 3);
3535}
3536
Eli Friedmanff030482011-07-28 21:48:00 +00003537void SelectionDAGBuilder::visitAtomicCmpXchg(const AtomicCmpXchgInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003538 SDLoc dl = getCurSDLoc();
Eli Friedman26689ac2011-08-03 21:06:02 +00003539 AtomicOrdering Order = I.getOrdering();
Eli Friedman327236c2011-08-24 20:50:09 +00003540 SynchronizationScope Scope = I.getSynchScope();
Eli Friedman26689ac2011-08-03 21:06:02 +00003541
3542 SDValue InChain = getRoot();
3543
Bill Wendlingba54bca2013-06-19 21:36:55 +00003544 const TargetLowering *TLI = TM.getTargetLowering();
3545 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003546 InChain = InsertFenceForAtomic(InChain, Order, Scope, true, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003547 DAG, *TLI);
Eli Friedman26689ac2011-08-03 21:06:02 +00003548
Eli Friedman55ba8162011-07-29 03:05:32 +00003549 SDValue L =
Eli Friedman26689ac2011-08-03 21:06:02 +00003550 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl,
Craig Topper0ff11902013-08-15 02:44:19 +00003551 getValue(I.getCompareOperand()).getSimpleValueType(),
Eli Friedman26689ac2011-08-03 21:06:02 +00003552 InChain,
Eli Friedman55ba8162011-07-29 03:05:32 +00003553 getValue(I.getPointerOperand()),
3554 getValue(I.getCompareOperand()),
3555 getValue(I.getNewValOperand()),
3556 MachinePointerInfo(I.getPointerOperand()), 0 /* Alignment */,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003557 TLI->getInsertFencesForAtomic() ? Monotonic : Order,
Eli Friedman327236c2011-08-24 20:50:09 +00003558 Scope);
Eli Friedman26689ac2011-08-03 21:06:02 +00003559
3560 SDValue OutChain = L.getValue(1);
3561
Bill Wendlingba54bca2013-06-19 21:36:55 +00003562 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003563 OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003564 DAG, *TLI);
Eli Friedman26689ac2011-08-03 21:06:02 +00003565
Eli Friedman55ba8162011-07-29 03:05:32 +00003566 setValue(&I, L);
Eli Friedman26689ac2011-08-03 21:06:02 +00003567 DAG.setRoot(OutChain);
Eli Friedmanff030482011-07-28 21:48:00 +00003568}
3569
3570void SelectionDAGBuilder::visitAtomicRMW(const AtomicRMWInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003571 SDLoc dl = getCurSDLoc();
Eli Friedman55ba8162011-07-29 03:05:32 +00003572 ISD::NodeType NT;
3573 switch (I.getOperation()) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003574 default: llvm_unreachable("Unknown atomicrmw operation");
Eli Friedman55ba8162011-07-29 03:05:32 +00003575 case AtomicRMWInst::Xchg: NT = ISD::ATOMIC_SWAP; break;
3576 case AtomicRMWInst::Add: NT = ISD::ATOMIC_LOAD_ADD; break;
3577 case AtomicRMWInst::Sub: NT = ISD::ATOMIC_LOAD_SUB; break;
3578 case AtomicRMWInst::And: NT = ISD::ATOMIC_LOAD_AND; break;
3579 case AtomicRMWInst::Nand: NT = ISD::ATOMIC_LOAD_NAND; break;
3580 case AtomicRMWInst::Or: NT = ISD::ATOMIC_LOAD_OR; break;
3581 case AtomicRMWInst::Xor: NT = ISD::ATOMIC_LOAD_XOR; break;
3582 case AtomicRMWInst::Max: NT = ISD::ATOMIC_LOAD_MAX; break;
3583 case AtomicRMWInst::Min: NT = ISD::ATOMIC_LOAD_MIN; break;
3584 case AtomicRMWInst::UMax: NT = ISD::ATOMIC_LOAD_UMAX; break;
3585 case AtomicRMWInst::UMin: NT = ISD::ATOMIC_LOAD_UMIN; break;
3586 }
Eli Friedman26689ac2011-08-03 21:06:02 +00003587 AtomicOrdering Order = I.getOrdering();
Eli Friedman327236c2011-08-24 20:50:09 +00003588 SynchronizationScope Scope = I.getSynchScope();
Eli Friedman26689ac2011-08-03 21:06:02 +00003589
3590 SDValue InChain = getRoot();
3591
Bill Wendlingba54bca2013-06-19 21:36:55 +00003592 const TargetLowering *TLI = TM.getTargetLowering();
3593 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003594 InChain = InsertFenceForAtomic(InChain, Order, Scope, true, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003595 DAG, *TLI);
Eli Friedman26689ac2011-08-03 21:06:02 +00003596
Eli Friedman55ba8162011-07-29 03:05:32 +00003597 SDValue L =
Eli Friedman26689ac2011-08-03 21:06:02 +00003598 DAG.getAtomic(NT, dl,
Craig Topper0ff11902013-08-15 02:44:19 +00003599 getValue(I.getValOperand()).getSimpleValueType(),
Eli Friedman26689ac2011-08-03 21:06:02 +00003600 InChain,
Eli Friedman55ba8162011-07-29 03:05:32 +00003601 getValue(I.getPointerOperand()),
3602 getValue(I.getValOperand()),
3603 I.getPointerOperand(), 0 /* Alignment */,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003604 TLI->getInsertFencesForAtomic() ? Monotonic : Order,
Eli Friedman327236c2011-08-24 20:50:09 +00003605 Scope);
Eli Friedman26689ac2011-08-03 21:06:02 +00003606
3607 SDValue OutChain = L.getValue(1);
3608
Bill Wendlingba54bca2013-06-19 21:36:55 +00003609 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003610 OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003611 DAG, *TLI);
Eli Friedman26689ac2011-08-03 21:06:02 +00003612
Eli Friedman55ba8162011-07-29 03:05:32 +00003613 setValue(&I, L);
Eli Friedman26689ac2011-08-03 21:06:02 +00003614 DAG.setRoot(OutChain);
Eli Friedmanff030482011-07-28 21:48:00 +00003615}
3616
Eli Friedman47f35132011-07-25 23:16:38 +00003617void SelectionDAGBuilder::visitFence(const FenceInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003618 SDLoc dl = getCurSDLoc();
Bill Wendlingba54bca2013-06-19 21:36:55 +00003619 const TargetLowering *TLI = TM.getTargetLowering();
Eli Friedman14648462011-07-27 22:21:52 +00003620 SDValue Ops[3];
3621 Ops[0] = getRoot();
Bill Wendlingba54bca2013-06-19 21:36:55 +00003622 Ops[1] = DAG.getConstant(I.getOrdering(), TLI->getPointerTy());
3623 Ops[2] = DAG.getConstant(I.getSynchScope(), TLI->getPointerTy());
Eli Friedman14648462011-07-27 22:21:52 +00003624 DAG.setRoot(DAG.getNode(ISD::ATOMIC_FENCE, dl, MVT::Other, Ops, 3));
Eli Friedman47f35132011-07-25 23:16:38 +00003625}
3626
Eli Friedman327236c2011-08-24 20:50:09 +00003627void SelectionDAGBuilder::visitAtomicLoad(const LoadInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003628 SDLoc dl = getCurSDLoc();
Eli Friedman327236c2011-08-24 20:50:09 +00003629 AtomicOrdering Order = I.getOrdering();
3630 SynchronizationScope Scope = I.getSynchScope();
3631
3632 SDValue InChain = getRoot();
3633
Bill Wendlingba54bca2013-06-19 21:36:55 +00003634 const TargetLowering *TLI = TM.getTargetLowering();
3635 EVT VT = TLI->getValueType(I.getType());
Eli Friedman327236c2011-08-24 20:50:09 +00003636
Evan Cheng607acd62013-02-06 02:06:33 +00003637 if (I.getAlignment() < VT.getSizeInBits() / 8)
Eli Friedmanfe731212011-09-13 20:50:54 +00003638 report_fatal_error("Cannot generate unaligned atomic load");
3639
Eli Friedman327236c2011-08-24 20:50:09 +00003640 SDValue L =
3641 DAG.getAtomic(ISD::ATOMIC_LOAD, dl, VT, VT, InChain,
3642 getValue(I.getPointerOperand()),
3643 I.getPointerOperand(), I.getAlignment(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00003644 TLI->getInsertFencesForAtomic() ? Monotonic : Order,
Eli Friedman327236c2011-08-24 20:50:09 +00003645 Scope);
3646
3647 SDValue OutChain = L.getValue(1);
3648
Bill Wendlingba54bca2013-06-19 21:36:55 +00003649 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003650 OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003651 DAG, *TLI);
Eli Friedman327236c2011-08-24 20:50:09 +00003652
3653 setValue(&I, L);
3654 DAG.setRoot(OutChain);
3655}
3656
3657void SelectionDAGBuilder::visitAtomicStore(const StoreInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003658 SDLoc dl = getCurSDLoc();
Eli Friedman327236c2011-08-24 20:50:09 +00003659
3660 AtomicOrdering Order = I.getOrdering();
3661 SynchronizationScope Scope = I.getSynchScope();
3662
3663 SDValue InChain = getRoot();
3664
Bill Wendlingba54bca2013-06-19 21:36:55 +00003665 const TargetLowering *TLI = TM.getTargetLowering();
3666 EVT VT = TLI->getValueType(I.getValueOperand()->getType());
Eli Friedmanfe731212011-09-13 20:50:54 +00003667
Evan Cheng607acd62013-02-06 02:06:33 +00003668 if (I.getAlignment() < VT.getSizeInBits() / 8)
Eli Friedmanfe731212011-09-13 20:50:54 +00003669 report_fatal_error("Cannot generate unaligned atomic store");
3670
Bill Wendlingba54bca2013-06-19 21:36:55 +00003671 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003672 InChain = InsertFenceForAtomic(InChain, Order, Scope, true, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003673 DAG, *TLI);
Eli Friedman327236c2011-08-24 20:50:09 +00003674
3675 SDValue OutChain =
Eli Friedmanfe731212011-09-13 20:50:54 +00003676 DAG.getAtomic(ISD::ATOMIC_STORE, dl, VT,
Eli Friedman327236c2011-08-24 20:50:09 +00003677 InChain,
3678 getValue(I.getPointerOperand()),
3679 getValue(I.getValueOperand()),
3680 I.getPointerOperand(), I.getAlignment(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00003681 TLI->getInsertFencesForAtomic() ? Monotonic : Order,
Eli Friedman327236c2011-08-24 20:50:09 +00003682 Scope);
3683
Bill Wendlingba54bca2013-06-19 21:36:55 +00003684 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003685 OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003686 DAG, *TLI);
Eli Friedman327236c2011-08-24 20:50:09 +00003687
3688 DAG.setRoot(OutChain);
3689}
3690
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003691/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
3692/// node.
Dan Gohman46510a72010-04-15 01:51:59 +00003693void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I,
Dan Gohman2048b852009-11-23 18:04:58 +00003694 unsigned Intrinsic) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003695 bool HasChain = !I.doesNotAccessMemory();
3696 bool OnlyLoad = HasChain && I.onlyReadsMemory();
3697
3698 // Build the operand list.
3699 SmallVector<SDValue, 8> Ops;
3700 if (HasChain) { // If this intrinsic has side-effects, chainify it.
3701 if (OnlyLoad) {
3702 // We don't need to serialize loads against other loads.
3703 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003704 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003705 Ops.push_back(getRoot());
3706 }
3707 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003708
3709 // Info is set by getTgtMemInstrinsic
3710 TargetLowering::IntrinsicInfo Info;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003711 const TargetLowering *TLI = TM.getTargetLowering();
3712 bool IsTgtIntrinsic = TLI->getTgtMemIntrinsic(Info, I, Intrinsic);
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003713
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003714 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Bob Wilson65ffec42010-09-21 17:56:22 +00003715 if (!IsTgtIntrinsic || Info.opc == ISD::INTRINSIC_VOID ||
3716 Info.opc == ISD::INTRINSIC_W_CHAIN)
Bill Wendlingba54bca2013-06-19 21:36:55 +00003717 Ops.push_back(DAG.getTargetConstant(Intrinsic, TLI->getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003718
3719 // Add all operands of the call to the operand list.
Gabor Greif0635f352010-06-25 09:38:13 +00003720 for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) {
3721 SDValue Op = getValue(I.getArgOperand(i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003722 Ops.push_back(Op);
3723 }
3724
Owen Andersone50ed302009-08-10 22:56:29 +00003725 SmallVector<EVT, 4> ValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003726 ComputeValueVTs(*TLI, I.getType(), ValueVTs);
Bill Wendling856ff412009-12-22 00:12:37 +00003727
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003728 if (HasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00003729 ValueVTs.push_back(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003730
Bob Wilson8d919552009-07-31 22:41:21 +00003731 SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003732
3733 // Create the node.
3734 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003735 if (IsTgtIntrinsic) {
3736 // This is target intrinsic that touches memory
Andrew Trickac6d9be2013-05-25 02:42:55 +00003737 Result = DAG.getMemIntrinsicNode(Info.opc, getCurSDLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003738 VTs, &Ops[0], Ops.size(),
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00003739 Info.memVT,
3740 MachinePointerInfo(Info.ptrVal, Info.offset),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003741 Info.align, Info.vol,
3742 Info.readMem, Info.writeMem);
Bill Wendling856ff412009-12-22 00:12:37 +00003743 } else if (!HasChain) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003744 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurSDLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003745 VTs, &Ops[0], Ops.size());
Benjamin Kramerf0127052010-01-05 13:12:22 +00003746 } else if (!I.getType()->isVoidTy()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003747 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurSDLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003748 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003749 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003750 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003751 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003752 }
3753
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003754 if (HasChain) {
3755 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
3756 if (OnlyLoad)
3757 PendingLoads.push_back(Chain);
3758 else
3759 DAG.setRoot(Chain);
3760 }
Bill Wendling856ff412009-12-22 00:12:37 +00003761
Benjamin Kramerf0127052010-01-05 13:12:22 +00003762 if (!I.getType()->isVoidTy()) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003763 if (VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00003764 EVT VT = TLI->getValueType(PTy);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003765 Result = DAG.getNode(ISD::BITCAST, getCurSDLoc(), VT, Result);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003766 }
Bill Wendling856ff412009-12-22 00:12:37 +00003767
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003768 setValue(&I, Result);
3769 }
3770}
3771
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003772/// GetSignificand - Get the significand and build it into a floating-point
3773/// number with exponent of 1:
3774///
3775/// Op = (Op & 0x007fffff) | 0x3f800000;
3776///
Matt Beaumont-Gay50e75bf2013-02-25 18:11:18 +00003777/// where Op is the hexadecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003778static SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00003779GetSignificand(SelectionDAG &DAG, SDValue Op, SDLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003780 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3781 DAG.getConstant(0x007fffff, MVT::i32));
3782 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
3783 DAG.getConstant(0x3f800000, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003784 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00003785}
3786
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003787/// GetExponent - Get the exponent:
3788///
Bill Wendlinge9a72862009-01-20 21:17:57 +00003789/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003790///
Matt Beaumont-Gay50e75bf2013-02-25 18:11:18 +00003791/// where Op is the hexadecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003792static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00003793GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003794 SDLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003795 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3796 DAG.getConstant(0x7f800000, MVT::i32));
3797 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Duncan Sands92abc622009-01-31 15:50:11 +00003798 DAG.getConstant(23, TLI.getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00003799 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
3800 DAG.getConstant(127, MVT::i32));
Bill Wendling4533cac2010-01-28 21:51:40 +00003801 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00003802}
3803
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003804/// getF32Constant - Get 32-bit floating point constant.
3805static SDValue
3806getF32Constant(SelectionDAG &DAG, unsigned Flt) {
Tim Northover0a29cb02013-01-22 09:46:31 +00003807 return DAG.getConstantFP(APFloat(APFloat::IEEEsingle, APInt(32, Flt)),
3808 MVT::f32);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003809}
3810
Craig Topper538cd482012-11-24 18:52:06 +00003811/// expandExp - Lower an exp intrinsic. Handles the special sequences for
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003812/// limited-precision mode.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003813static SDValue expandExp(SDLoc dl, SDValue Op, SelectionDAG &DAG,
Craig Topper538cd482012-11-24 18:52:06 +00003814 const TargetLowering &TLI) {
3815 if (Op.getValueType() == MVT::f32 &&
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003816 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003817
3818 // Put the exponent in the right bit position for later addition to the
3819 // final result:
3820 //
3821 // #define LOG2OFe 1.4426950f
3822 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Owen Anderson825b72b2009-08-11 20:47:22 +00003823 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003824 getF32Constant(DAG, 0x3fb8aa3b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003825 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003826
3827 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003828 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3829 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003830
3831 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003832 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003833 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendling856ff412009-12-22 00:12:37 +00003834
Craig Topperb3157722012-11-24 08:22:37 +00003835 SDValue TwoToFracPartOfX;
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003836 if (LimitFloatPrecision <= 6) {
3837 // For floating-point precision of 6:
3838 //
3839 // TwoToFractionalPartOfX =
3840 // 0.997535578f +
3841 // (0.735607626f + 0.252464424f * x) * x;
3842 //
3843 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003844 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003845 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003846 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003847 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003848 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
Craig Topperb3157722012-11-24 08:22:37 +00003849 TwoToFracPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
3850 getF32Constant(DAG, 0x3f7f5e7e));
Craig Topper08ac4692012-11-16 20:01:39 +00003851 } else if (LimitFloatPrecision <= 12) {
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003852 // For floating-point precision of 12:
3853 //
3854 // TwoToFractionalPartOfX =
3855 // 0.999892986f +
3856 // (0.696457318f +
3857 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3858 //
3859 // 0.000107046256 error, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003860 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003861 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003862 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003863 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003864 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3865 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003866 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003867 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
Craig Topperb3157722012-11-24 08:22:37 +00003868 TwoToFracPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
3869 getF32Constant(DAG, 0x3f7ff8fd));
Craig Topper08ac4692012-11-16 20:01:39 +00003870 } else { // LimitFloatPrecision <= 18
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003871 // For floating-point precision of 18:
3872 //
3873 // TwoToFractionalPartOfX =
3874 // 0.999999982f +
3875 // (0.693148872f +
3876 // (0.240227044f +
3877 // (0.554906021e-1f +
3878 // (0.961591928e-2f +
3879 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3880 //
3881 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003882 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003883 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003884 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003885 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003886 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3887 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003888 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003889 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3890 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003891 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003892 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3893 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003894 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003895 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3896 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003897 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003898 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
Craig Topperb3157722012-11-24 08:22:37 +00003899 TwoToFracPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
3900 getF32Constant(DAG, 0x3f800000));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003901 }
Craig Topperb3157722012-11-24 08:22:37 +00003902
3903 // Add the exponent into the result in integer domain.
3904 SDValue t13 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, TwoToFracPartOfX);
Craig Topper538cd482012-11-24 18:52:06 +00003905 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3906 DAG.getNode(ISD::ADD, dl, MVT::i32,
3907 t13, IntegerPartOfX));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003908 }
3909
Craig Topper538cd482012-11-24 18:52:06 +00003910 // No special expansion.
3911 return DAG.getNode(ISD::FEXP, dl, Op.getValueType(), Op);
Dale Johannesen59e577f2008-09-05 18:38:42 +00003912}
3913
Craig Topper5d1e0892012-11-23 18:38:31 +00003914/// expandLog - Lower a log intrinsic. Handles the special sequences for
Bill Wendling39150252008-09-09 20:39:27 +00003915/// limited-precision mode.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003916static SDValue expandLog(SDLoc dl, SDValue Op, SelectionDAG &DAG,
Craig Topper5d1e0892012-11-23 18:38:31 +00003917 const TargetLowering &TLI) {
3918 if (Op.getValueType() == MVT::f32 &&
Bill Wendling39150252008-09-09 20:39:27 +00003919 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003920 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00003921
3922 // Scale the exponent by log(2) [0.69314718f].
Bill Wendling46ada192010-03-02 01:55:18 +00003923 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003924 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003925 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003926
3927 // Get the significand and build it into a floating-point number with
3928 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003929 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling39150252008-09-09 20:39:27 +00003930
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003931 SDValue LogOfMantissa;
Bill Wendling39150252008-09-09 20:39:27 +00003932 if (LimitFloatPrecision <= 6) {
3933 // For floating-point precision of 6:
3934 //
3935 // LogofMantissa =
3936 // -1.1609546f +
3937 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003938 //
Bill Wendling39150252008-09-09 20:39:27 +00003939 // error 0.0034276066, which is better than 8 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003940 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003941 getF32Constant(DAG, 0xbe74c456));
Owen Anderson825b72b2009-08-11 20:47:22 +00003942 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003943 getF32Constant(DAG, 0x3fb3a2b1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003944 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003945 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
3946 getF32Constant(DAG, 0x3f949a29));
Craig Topper08ac4692012-11-16 20:01:39 +00003947 } else if (LimitFloatPrecision <= 12) {
Bill Wendling39150252008-09-09 20:39:27 +00003948 // For floating-point precision of 12:
3949 //
3950 // LogOfMantissa =
3951 // -1.7417939f +
3952 // (2.8212026f +
3953 // (-1.4699568f +
3954 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3955 //
3956 // error 0.000061011436, which is 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003957 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003958 getF32Constant(DAG, 0xbd67b6d6));
Owen Anderson825b72b2009-08-11 20:47:22 +00003959 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003960 getF32Constant(DAG, 0x3ee4f4b8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003961 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3962 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003963 getF32Constant(DAG, 0x3fbc278b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003964 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3965 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003966 getF32Constant(DAG, 0x40348e95));
Owen Anderson825b72b2009-08-11 20:47:22 +00003967 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003968 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
3969 getF32Constant(DAG, 0x3fdef31a));
Craig Topper08ac4692012-11-16 20:01:39 +00003970 } else { // LimitFloatPrecision <= 18
Bill Wendling39150252008-09-09 20:39:27 +00003971 // For floating-point precision of 18:
3972 //
3973 // LogOfMantissa =
3974 // -2.1072184f +
3975 // (4.2372794f +
3976 // (-3.7029485f +
3977 // (2.2781945f +
3978 // (-0.87823314f +
3979 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3980 //
3981 // error 0.0000023660568, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003982 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003983 getF32Constant(DAG, 0xbc91e5ac));
Owen Anderson825b72b2009-08-11 20:47:22 +00003984 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003985 getF32Constant(DAG, 0x3e4350aa));
Owen Anderson825b72b2009-08-11 20:47:22 +00003986 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3987 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003988 getF32Constant(DAG, 0x3f60d3e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003989 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3990 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003991 getF32Constant(DAG, 0x4011cdf0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003992 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3993 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003994 getF32Constant(DAG, 0x406cfd1c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003995 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3996 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003997 getF32Constant(DAG, 0x408797cb));
Owen Anderson825b72b2009-08-11 20:47:22 +00003998 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003999 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
4000 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00004001 }
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004002
Craig Topper5d1e0892012-11-23 18:38:31 +00004003 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00004004 }
4005
Craig Topper5d1e0892012-11-23 18:38:31 +00004006 // No special expansion.
4007 return DAG.getNode(ISD::FLOG, dl, Op.getValueType(), Op);
Dale Johannesen59e577f2008-09-05 18:38:42 +00004008}
4009
Craig Topper5d1e0892012-11-23 18:38:31 +00004010/// expandLog2 - Lower a log2 intrinsic. Handles the special sequences for
Bill Wendling3eb59402008-09-09 00:28:24 +00004011/// limited-precision mode.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004012static SDValue expandLog2(SDLoc dl, SDValue Op, SelectionDAG &DAG,
Craig Topper5d1e0892012-11-23 18:38:31 +00004013 const TargetLowering &TLI) {
4014 if (Op.getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00004015 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004016 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00004017
Bill Wendling39150252008-09-09 20:39:27 +00004018 // Get the exponent.
Bill Wendling46ada192010-03-02 01:55:18 +00004019 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
Bill Wendling856ff412009-12-22 00:12:37 +00004020
Bill Wendling3eb59402008-09-09 00:28:24 +00004021 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00004022 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00004023 SDValue X = GetSignificand(DAG, Op1, dl);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004024
Bill Wendling3eb59402008-09-09 00:28:24 +00004025 // Different possible minimax approximations of significand in
4026 // floating-point for various degrees of accuracy over [1,2].
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004027 SDValue Log2ofMantissa;
Bill Wendling3eb59402008-09-09 00:28:24 +00004028 if (LimitFloatPrecision <= 6) {
4029 // For floating-point precision of 6:
4030 //
4031 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
4032 //
4033 // error 0.0049451742, which is more than 7 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004034 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004035 getF32Constant(DAG, 0xbeb08fe0));
Owen Anderson825b72b2009-08-11 20:47:22 +00004036 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004037 getF32Constant(DAG, 0x40019463));
Owen Anderson825b72b2009-08-11 20:47:22 +00004038 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004039 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
4040 getF32Constant(DAG, 0x3fd6633d));
Craig Topper08ac4692012-11-16 20:01:39 +00004041 } else if (LimitFloatPrecision <= 12) {
Bill Wendling3eb59402008-09-09 00:28:24 +00004042 // For floating-point precision of 12:
4043 //
4044 // Log2ofMantissa =
4045 // -2.51285454f +
4046 // (4.07009056f +
4047 // (-2.12067489f +
4048 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004049 //
Bill Wendling3eb59402008-09-09 00:28:24 +00004050 // error 0.0000876136000, which is better than 13 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004051 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004052 getF32Constant(DAG, 0xbda7262e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004053 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004054 getF32Constant(DAG, 0x3f25280b));
Owen Anderson825b72b2009-08-11 20:47:22 +00004055 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
4056 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004057 getF32Constant(DAG, 0x4007b923));
Owen Anderson825b72b2009-08-11 20:47:22 +00004058 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4059 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004060 getF32Constant(DAG, 0x40823e2f));
Owen Anderson825b72b2009-08-11 20:47:22 +00004061 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004062 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
4063 getF32Constant(DAG, 0x4020d29c));
Craig Topper08ac4692012-11-16 20:01:39 +00004064 } else { // LimitFloatPrecision <= 18
Bill Wendling3eb59402008-09-09 00:28:24 +00004065 // For floating-point precision of 18:
4066 //
4067 // Log2ofMantissa =
4068 // -3.0400495f +
4069 // (6.1129976f +
4070 // (-5.3420409f +
4071 // (3.2865683f +
4072 // (-1.2669343f +
4073 // (0.27515199f -
4074 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
4075 //
4076 // error 0.0000018516, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004077 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004078 getF32Constant(DAG, 0xbcd2769e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004079 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004080 getF32Constant(DAG, 0x3e8ce0b9));
Owen Anderson825b72b2009-08-11 20:47:22 +00004081 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
4082 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004083 getF32Constant(DAG, 0x3fa22ae7));
Owen Anderson825b72b2009-08-11 20:47:22 +00004084 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4085 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004086 getF32Constant(DAG, 0x40525723));
Owen Anderson825b72b2009-08-11 20:47:22 +00004087 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4088 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004089 getF32Constant(DAG, 0x40aaf200));
Owen Anderson825b72b2009-08-11 20:47:22 +00004090 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
4091 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004092 getF32Constant(DAG, 0x40c39dad));
Owen Anderson825b72b2009-08-11 20:47:22 +00004093 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004094 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
4095 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00004096 }
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004097
Craig Topper5d1e0892012-11-23 18:38:31 +00004098 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log2ofMantissa);
Dale Johannesen853244f2008-09-05 23:49:37 +00004099 }
Bill Wendling3eb59402008-09-09 00:28:24 +00004100
Craig Topper5d1e0892012-11-23 18:38:31 +00004101 // No special expansion.
4102 return DAG.getNode(ISD::FLOG2, dl, Op.getValueType(), Op);
Dale Johannesen59e577f2008-09-05 18:38:42 +00004103}
4104
Craig Topper5d1e0892012-11-23 18:38:31 +00004105/// expandLog10 - Lower a log10 intrinsic. Handles the special sequences for
Bill Wendling3eb59402008-09-09 00:28:24 +00004106/// limited-precision mode.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004107static SDValue expandLog10(SDLoc dl, SDValue Op, SelectionDAG &DAG,
Craig Topper5d1e0892012-11-23 18:38:31 +00004108 const TargetLowering &TLI) {
4109 if (Op.getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00004110 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004111 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00004112
Bill Wendling39150252008-09-09 20:39:27 +00004113 // Scale the exponent by log10(2) [0.30102999f].
Bill Wendling46ada192010-03-02 01:55:18 +00004114 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00004115 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004116 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00004117
4118 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00004119 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00004120 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling3eb59402008-09-09 00:28:24 +00004121
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004122 SDValue Log10ofMantissa;
Bill Wendling3eb59402008-09-09 00:28:24 +00004123 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00004124 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004125 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00004126 // Log10ofMantissa =
4127 // -0.50419619f +
4128 // (0.60948995f - 0.10380950f * x) * x;
4129 //
4130 // error 0.0014886165, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004131 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004132 getF32Constant(DAG, 0xbdd49a13));
Owen Anderson825b72b2009-08-11 20:47:22 +00004133 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004134 getF32Constant(DAG, 0x3f1c0789));
Owen Anderson825b72b2009-08-11 20:47:22 +00004135 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004136 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
4137 getF32Constant(DAG, 0x3f011300));
Craig Topper08ac4692012-11-16 20:01:39 +00004138 } else if (LimitFloatPrecision <= 12) {
Bill Wendling3eb59402008-09-09 00:28:24 +00004139 // For floating-point precision of 12:
4140 //
4141 // Log10ofMantissa =
4142 // -0.64831180f +
4143 // (0.91751397f +
4144 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
4145 //
4146 // error 0.00019228036, which is better than 12 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004147 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004148 getF32Constant(DAG, 0x3d431f31));
Owen Anderson825b72b2009-08-11 20:47:22 +00004149 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004150 getF32Constant(DAG, 0x3ea21fb2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004151 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
4152 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004153 getF32Constant(DAG, 0x3f6ae232));
Owen Anderson825b72b2009-08-11 20:47:22 +00004154 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004155 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
4156 getF32Constant(DAG, 0x3f25f7c3));
Craig Topper08ac4692012-11-16 20:01:39 +00004157 } else { // LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00004158 // For floating-point precision of 18:
4159 //
4160 // Log10ofMantissa =
4161 // -0.84299375f +
4162 // (1.5327582f +
4163 // (-1.0688956f +
4164 // (0.49102474f +
4165 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
4166 //
4167 // error 0.0000037995730, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004168 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004169 getF32Constant(DAG, 0x3c5d51ce));
Owen Anderson825b72b2009-08-11 20:47:22 +00004170 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004171 getF32Constant(DAG, 0x3e00685a));
Owen Anderson825b72b2009-08-11 20:47:22 +00004172 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
4173 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004174 getF32Constant(DAG, 0x3efb6798));
Owen Anderson825b72b2009-08-11 20:47:22 +00004175 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4176 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004177 getF32Constant(DAG, 0x3f88d192));
Owen Anderson825b72b2009-08-11 20:47:22 +00004178 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4179 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004180 getF32Constant(DAG, 0x3fc4316c));
Owen Anderson825b72b2009-08-11 20:47:22 +00004181 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004182 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
4183 getF32Constant(DAG, 0x3f57ce70));
Bill Wendling3eb59402008-09-09 00:28:24 +00004184 }
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004185
Craig Topper5d1e0892012-11-23 18:38:31 +00004186 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log10ofMantissa);
Dale Johannesen852680a2008-09-05 21:27:19 +00004187 }
Bill Wendling3eb59402008-09-09 00:28:24 +00004188
Craig Topper5d1e0892012-11-23 18:38:31 +00004189 // No special expansion.
4190 return DAG.getNode(ISD::FLOG10, dl, Op.getValueType(), Op);
Dale Johannesen59e577f2008-09-05 18:38:42 +00004191}
4192
Craig Topper538cd482012-11-24 18:52:06 +00004193/// expandExp2 - Lower an exp2 intrinsic. Handles the special sequences for
Bill Wendlinge10c8142008-09-09 22:39:21 +00004194/// limited-precision mode.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004195static SDValue expandExp2(SDLoc dl, SDValue Op, SelectionDAG &DAG,
Craig Topper538cd482012-11-24 18:52:06 +00004196 const TargetLowering &TLI) {
4197 if (Op.getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00004198 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004199 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00004200
4201 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00004202 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
4203 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00004204
4205 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00004206 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00004207 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00004208
Craig Topperb3157722012-11-24 08:22:37 +00004209 SDValue TwoToFractionalPartOfX;
Bill Wendlinge10c8142008-09-09 22:39:21 +00004210 if (LimitFloatPrecision <= 6) {
4211 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004212 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00004213 // TwoToFractionalPartOfX =
4214 // 0.997535578f +
4215 // (0.735607626f + 0.252464424f * x) * x;
4216 //
4217 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004218 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004219 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00004220 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004221 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004222 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
Craig Topperb3157722012-11-24 08:22:37 +00004223 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
4224 getF32Constant(DAG, 0x3f7f5e7e));
Craig Topper08ac4692012-11-16 20:01:39 +00004225 } else if (LimitFloatPrecision <= 12) {
Bill Wendlinge10c8142008-09-09 22:39:21 +00004226 // For floating-point precision of 12:
4227 //
4228 // TwoToFractionalPartOfX =
4229 // 0.999892986f +
4230 // (0.696457318f +
4231 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
4232 //
4233 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004234 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004235 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004236 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004237 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004238 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4239 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004240 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00004241 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
Craig Topperb3157722012-11-24 08:22:37 +00004242 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
4243 getF32Constant(DAG, 0x3f7ff8fd));
Craig Topper08ac4692012-11-16 20:01:39 +00004244 } else { // LimitFloatPrecision <= 18
Bill Wendlinge10c8142008-09-09 22:39:21 +00004245 // For floating-point precision of 18:
4246 //
4247 // TwoToFractionalPartOfX =
4248 // 0.999999982f +
4249 // (0.693148872f +
4250 // (0.240227044f +
4251 // (0.554906021e-1f +
4252 // (0.961591928e-2f +
4253 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
4254 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004255 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004256 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004257 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004258 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00004259 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4260 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004261 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00004262 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4263 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004264 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00004265 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
4266 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004267 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00004268 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
4269 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004270 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00004271 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
Craig Topperb3157722012-11-24 08:22:37 +00004272 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
4273 getF32Constant(DAG, 0x3f800000));
Bill Wendlinge10c8142008-09-09 22:39:21 +00004274 }
Craig Topperb3157722012-11-24 08:22:37 +00004275
4276 // Add the exponent into the result in integer domain.
4277 SDValue t13 = DAG.getNode(ISD::BITCAST, dl, MVT::i32,
4278 TwoToFractionalPartOfX);
Craig Topper538cd482012-11-24 18:52:06 +00004279 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
4280 DAG.getNode(ISD::ADD, dl, MVT::i32,
4281 t13, IntegerPartOfX));
Dale Johannesen601d3c02008-09-05 01:48:15 +00004282 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00004283
Craig Topper538cd482012-11-24 18:52:06 +00004284 // No special expansion.
4285 return DAG.getNode(ISD::FEXP2, dl, Op.getValueType(), Op);
Dale Johannesen601d3c02008-09-05 01:48:15 +00004286}
4287
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004288/// visitPow - Lower a pow intrinsic. Handles the special sequences for
4289/// limited-precision mode with x == 10.0f.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004290static SDValue expandPow(SDLoc dl, SDValue LHS, SDValue RHS,
Craig Topper327e4cb2012-11-25 08:08:58 +00004291 SelectionDAG &DAG, const TargetLowering &TLI) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004292 bool IsExp10 = false;
Craig Topper327e4cb2012-11-25 08:08:58 +00004293 if (LHS.getValueType() == MVT::f32 && LHS.getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004294 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Craig Topper327e4cb2012-11-25 08:08:58 +00004295 if (ConstantFPSDNode *LHSC = dyn_cast<ConstantFPSDNode>(LHS)) {
4296 APFloat Ten(10.0f);
4297 IsExp10 = LHSC->isExactlyValue(Ten);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004298 }
4299 }
4300
Craig Topperc1aa6382012-11-25 00:48:58 +00004301 if (IsExp10) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004302 // Put the exponent in the right bit position for later addition to the
4303 // final result:
4304 //
4305 // #define LOG2OF10 3.3219281f
4306 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Craig Topper327e4cb2012-11-25 08:08:58 +00004307 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, RHS,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004308 getF32Constant(DAG, 0x40549a78));
Owen Anderson825b72b2009-08-11 20:47:22 +00004309 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004310
4311 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00004312 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
4313 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004314
4315 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00004316 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00004317 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004318
Craig Topper915562e2012-11-25 00:15:07 +00004319 SDValue TwoToFractionalPartOfX;
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004320 if (LimitFloatPrecision <= 6) {
4321 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004322 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004323 // twoToFractionalPartOfX =
4324 // 0.997535578f +
4325 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004326 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004327 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004328 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004329 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00004330 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004331 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004332 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
Craig Topper915562e2012-11-25 00:15:07 +00004333 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
4334 getF32Constant(DAG, 0x3f7f5e7e));
Craig Topper08ac4692012-11-16 20:01:39 +00004335 } else if (LimitFloatPrecision <= 12) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004336 // For floating-point precision of 12:
4337 //
4338 // TwoToFractionalPartOfX =
4339 // 0.999892986f +
4340 // (0.696457318f +
4341 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
4342 //
4343 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004344 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004345 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004346 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004347 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004348 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4349 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004350 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00004351 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
Craig Topper915562e2012-11-25 00:15:07 +00004352 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
4353 getF32Constant(DAG, 0x3f7ff8fd));
Craig Topper08ac4692012-11-16 20:01:39 +00004354 } else { // LimitFloatPrecision <= 18
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004355 // For floating-point precision of 18:
4356 //
4357 // TwoToFractionalPartOfX =
4358 // 0.999999982f +
4359 // (0.693148872f +
4360 // (0.240227044f +
4361 // (0.554906021e-1f +
4362 // (0.961591928e-2f +
4363 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
4364 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004365 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004366 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004367 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004368 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00004369 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4370 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004371 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00004372 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4373 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004374 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00004375 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
4376 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004377 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00004378 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
4379 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004380 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00004381 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
Craig Topper915562e2012-11-25 00:15:07 +00004382 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
4383 getF32Constant(DAG, 0x3f800000));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004384 }
Craig Topper915562e2012-11-25 00:15:07 +00004385
4386 SDValue t13 = DAG.getNode(ISD::BITCAST, dl,MVT::i32,TwoToFractionalPartOfX);
Craig Topper327e4cb2012-11-25 08:08:58 +00004387 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
4388 DAG.getNode(ISD::ADD, dl, MVT::i32,
4389 t13, IntegerPartOfX));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004390 }
4391
Craig Topper327e4cb2012-11-25 08:08:58 +00004392 // No special expansion.
4393 return DAG.getNode(ISD::FPOW, dl, LHS.getValueType(), LHS, RHS);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004394}
4395
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004396
4397/// ExpandPowI - Expand a llvm.powi intrinsic.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004398static SDValue ExpandPowI(SDLoc DL, SDValue LHS, SDValue RHS,
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004399 SelectionDAG &DAG) {
4400 // If RHS is a constant, we can expand this out to a multiplication tree,
4401 // otherwise we end up lowering to a call to __powidf2 (for example). When
4402 // optimizing for size, we only want to do this if the expansion would produce
4403 // a small number of multiplies, otherwise we do the full expansion.
4404 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
4405 // Get the exponent as a positive value.
4406 unsigned Val = RHSC->getSExtValue();
4407 if ((int)Val < 0) Val = -Val;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004408
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004409 // powi(x, 0) -> 1.0
4410 if (Val == 0)
4411 return DAG.getConstantFP(1.0, LHS.getValueType());
4412
Dan Gohmanae541aa2010-04-15 04:33:49 +00004413 const Function *F = DAG.getMachineFunction().getFunction();
Bill Wendling831737d2012-12-30 10:32:01 +00004414 if (!F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
4415 Attribute::OptimizeForSize) ||
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004416 // If optimizing for size, don't insert too many multiplies. This
4417 // inserts up to 5 multiplies.
4418 CountPopulation_32(Val)+Log2_32(Val) < 7) {
4419 // We use the simple binary decomposition method to generate the multiply
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004420 // sequence. There are more optimal ways to do this (for example,
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004421 // powi(x,15) generates one more multiply than it should), but this has
4422 // the benefit of being both really simple and much better than a libcall.
4423 SDValue Res; // Logically starts equal to 1.0
4424 SDValue CurSquare = LHS;
4425 while (Val) {
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00004426 if (Val & 1) {
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004427 if (Res.getNode())
4428 Res = DAG.getNode(ISD::FMUL, DL,Res.getValueType(), Res, CurSquare);
4429 else
4430 Res = CurSquare; // 1.0*CurSquare.
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00004431 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004432
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004433 CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
4434 CurSquare, CurSquare);
4435 Val >>= 1;
4436 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004437
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004438 // If the original was negative, invert the result, producing 1/(x*x*x).
4439 if (RHSC->getSExtValue() < 0)
4440 Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(),
4441 DAG.getConstantFP(1.0, LHS.getValueType()), Res);
4442 return Res;
4443 }
4444 }
4445
4446 // Otherwise, expand to a libcall.
4447 return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS);
4448}
4449
Devang Patel227dfdb2011-05-16 21:24:05 +00004450// getTruncatedArgReg - Find underlying register used for an truncated
4451// argument.
4452static unsigned getTruncatedArgReg(const SDValue &N) {
4453 if (N.getOpcode() != ISD::TRUNCATE)
4454 return 0;
4455
4456 const SDValue &Ext = N.getOperand(0);
Stephen Lin09f8ca32013-07-06 21:44:25 +00004457 if (Ext.getOpcode() == ISD::AssertZext ||
4458 Ext.getOpcode() == ISD::AssertSext) {
Devang Patel227dfdb2011-05-16 21:24:05 +00004459 const SDValue &CFR = Ext.getOperand(0);
4460 if (CFR.getOpcode() == ISD::CopyFromReg)
4461 return cast<RegisterSDNode>(CFR.getOperand(1))->getReg();
Craig Topper7eb46d82012-04-11 04:55:51 +00004462 if (CFR.getOpcode() == ISD::TRUNCATE)
4463 return getTruncatedArgReg(CFR);
Devang Patel227dfdb2011-05-16 21:24:05 +00004464 }
4465 return 0;
4466}
4467
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004468/// EmitFuncArgumentDbgValue - If the DbgValueInst is a dbg_value of a function
4469/// argument, create the corresponding DBG_VALUE machine instruction for it now.
4470/// At the end of instruction selection, they will be inserted to the entry BB.
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004471bool
Devang Patel78a06e52010-08-25 20:39:26 +00004472SelectionDAGBuilder::EmitFuncArgumentDbgValue(const Value *V, MDNode *Variable,
Michael J. Spencere70c5262010-10-16 08:25:21 +00004473 int64_t Offset,
Dan Gohman5d11ea32010-05-01 00:33:16 +00004474 const SDValue &N) {
Devang Patel0b48ead2010-08-31 22:22:42 +00004475 const Argument *Arg = dyn_cast<Argument>(V);
4476 if (!Arg)
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004477 return false;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004478
Devang Patel719f6a92010-04-29 20:40:36 +00004479 MachineFunction &MF = DAG.getMachineFunction();
Devang Patela90b3052010-11-02 17:01:30 +00004480 const TargetInstrInfo *TII = DAG.getTarget().getInstrInfo();
Devang Patela90b3052010-11-02 17:01:30 +00004481
Devang Patela83ce982010-04-29 18:50:36 +00004482 // Ignore inlined function arguments here.
4483 DIVariable DV(Variable);
Devang Patel719f6a92010-04-29 20:40:36 +00004484 if (DV.isInlinedFnArgument(MF.getFunction()))
Devang Patela83ce982010-04-29 18:50:36 +00004485 return false;
4486
David Blaikie6d9dbd52013-06-16 20:34:15 +00004487 Optional<MachineOperand> Op;
Devang Patel9aee3352011-09-08 22:59:09 +00004488 // Some arguments' frame index is recorded during argument lowering.
David Blaikie6d9dbd52013-06-16 20:34:15 +00004489 if (int FI = FuncInfo.getArgumentFrameIndex(Arg))
4490 Op = MachineOperand::CreateFI(FI);
Devang Patel0b48ead2010-08-31 22:22:42 +00004491
David Blaikie6d9dbd52013-06-16 20:34:15 +00004492 if (!Op && N.getNode()) {
4493 unsigned Reg;
Devang Patel227dfdb2011-05-16 21:24:05 +00004494 if (N.getOpcode() == ISD::CopyFromReg)
4495 Reg = cast<RegisterSDNode>(N.getOperand(1))->getReg();
4496 else
4497 Reg = getTruncatedArgReg(N);
4498 if (Reg && TargetRegisterInfo::isVirtualRegister(Reg)) {
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004499 MachineRegisterInfo &RegInfo = MF.getRegInfo();
4500 unsigned PR = RegInfo.getLiveInPhysReg(Reg);
4501 if (PR)
4502 Reg = PR;
4503 }
David Blaikie6d9dbd52013-06-16 20:34:15 +00004504 if (Reg)
4505 Op = MachineOperand::CreateReg(Reg, false);
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004506 }
4507
David Blaikie6d9dbd52013-06-16 20:34:15 +00004508 if (!Op) {
Devang Patela90b3052010-11-02 17:01:30 +00004509 // Check if ValueMap has reg number.
Evan Chenga36acad2010-04-29 06:33:38 +00004510 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
Devang Patel8bc9ef72010-11-02 17:19:03 +00004511 if (VMI != FuncInfo.ValueMap.end())
David Blaikie6d9dbd52013-06-16 20:34:15 +00004512 Op = MachineOperand::CreateReg(VMI->second, false);
Evan Chenga36acad2010-04-29 06:33:38 +00004513 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004514
David Blaikie6d9dbd52013-06-16 20:34:15 +00004515 if (!Op && N.getNode())
Devang Patela90b3052010-11-02 17:01:30 +00004516 // Check if frame index is available.
4517 if (LoadSDNode *LNode = dyn_cast<LoadSDNode>(N.getNode()))
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004518 if (FrameIndexSDNode *FINode =
David Blaikie6d9dbd52013-06-16 20:34:15 +00004519 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
4520 Op = MachineOperand::CreateFI(FINode->getIndex());
Devang Patel8bc9ef72010-11-02 17:19:03 +00004521
David Blaikie6d9dbd52013-06-16 20:34:15 +00004522 if (!Op)
Devang Patel8bc9ef72010-11-02 17:19:03 +00004523 return false;
Devang Patela90b3052010-11-02 17:01:30 +00004524
Adrian Prantl893ae832013-07-10 01:53:30 +00004525 // FIXME: This does not handle register-indirect values at offset 0.
4526 bool IsIndirect = Offset != 0;
David Blaikie6d9dbd52013-06-16 20:34:15 +00004527 if (Op->isReg())
Adrian Prantl35176402013-07-09 20:28:37 +00004528 FuncInfo.ArgDbgValues.push_back(BuildMI(MF, getCurDebugLoc(),
4529 TII->get(TargetOpcode::DBG_VALUE),
Adrian Prantl893ae832013-07-10 01:53:30 +00004530 IsIndirect,
Adrian Prantl35176402013-07-09 20:28:37 +00004531 Op->getReg(), Offset, Variable));
4532 else
4533 FuncInfo.ArgDbgValues.push_back(
David Blaikie6d9dbd52013-06-16 20:34:15 +00004534 BuildMI(MF, getCurDebugLoc(), TII->get(TargetOpcode::DBG_VALUE))
4535 .addOperand(*Op).addImm(Offset).addMetadata(Variable));
Adrian Prantl35176402013-07-09 20:28:37 +00004536
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004537 return true;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004538}
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004539
Douglas Gregor7d9663c2010-05-11 06:17:44 +00004540// VisualStudio defines setjmp as _setjmp
Michael J. Spencer1f409602010-09-24 19:48:47 +00004541#if defined(_MSC_VER) && defined(setjmp) && \
4542 !defined(setjmp_undefined_for_msvc)
4543# pragma push_macro("setjmp")
4544# undef setjmp
4545# define setjmp_undefined_for_msvc
Douglas Gregor7d9663c2010-05-11 06:17:44 +00004546#endif
4547
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004548/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
4549/// we want to emit this as a call to a named external function, return the name
4550/// otherwise lower it and return null.
4551const char *
Dan Gohman46510a72010-04-15 01:51:59 +00004552SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, unsigned Intrinsic) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00004553 const TargetLowering *TLI = TM.getTargetLowering();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004554 SDLoc sdl = getCurSDLoc();
Dale Johannesen66978ee2009-01-31 02:22:37 +00004555 DebugLoc dl = getCurDebugLoc();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004556 SDValue Res;
4557
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004558 switch (Intrinsic) {
4559 default:
4560 // By default, turn this into a target intrinsic node.
4561 visitTargetIntrinsic(I, Intrinsic);
4562 return 0;
4563 case Intrinsic::vastart: visitVAStart(I); return 0;
4564 case Intrinsic::vaend: visitVAEnd(I); return 0;
4565 case Intrinsic::vacopy: visitVACopy(I); return 0;
4566 case Intrinsic::returnaddress:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004567 setValue(&I, DAG.getNode(ISD::RETURNADDR, sdl, TLI->getPointerTy(),
Gabor Greif0635f352010-06-25 09:38:13 +00004568 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004569 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00004570 case Intrinsic::frameaddress:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004571 setValue(&I, DAG.getNode(ISD::FRAMEADDR, sdl, TLI->getPointerTy(),
Gabor Greif0635f352010-06-25 09:38:13 +00004572 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004573 return 0;
4574 case Intrinsic::setjmp:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004575 return &"_setjmp"[!TLI->usesUnderscoreSetJmp()];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004576 case Intrinsic::longjmp:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004577 return &"_longjmp"[!TLI->usesUnderscoreLongJmp()];
Chris Lattner824b9582008-11-21 16:42:48 +00004578 case Intrinsic::memcpy: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00004579 // Assert for address < 256 since we support only user defined address
4580 // spaces.
Gabor Greif0635f352010-06-25 09:38:13 +00004581 assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004582 < 256 &&
Gabor Greif0635f352010-06-25 09:38:13 +00004583 cast<PointerType>(I.getArgOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004584 < 256 &&
4585 "Unknown address space");
Gabor Greif0635f352010-06-25 09:38:13 +00004586 SDValue Op1 = getValue(I.getArgOperand(0));
4587 SDValue Op2 = getValue(I.getArgOperand(1));
4588 SDValue Op3 = getValue(I.getArgOperand(2));
4589 unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
Chandler Carruthaf23f8e2013-02-25 14:20:21 +00004590 if (!Align)
4591 Align = 1; // @llvm.memcpy defines 0 and 1 to both mean no alignment.
Gabor Greif0635f352010-06-25 09:38:13 +00004592 bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004593 DAG.setRoot(DAG.getMemcpy(getRoot(), sdl, Op1, Op2, Op3, Align, isVol, false,
Chris Lattnere72f2022010-09-21 05:40:29 +00004594 MachinePointerInfo(I.getArgOperand(0)),
4595 MachinePointerInfo(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004596 return 0;
4597 }
Chris Lattner824b9582008-11-21 16:42:48 +00004598 case Intrinsic::memset: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00004599 // Assert for address < 256 since we support only user defined address
4600 // spaces.
Gabor Greif0635f352010-06-25 09:38:13 +00004601 assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004602 < 256 &&
4603 "Unknown address space");
Gabor Greif0635f352010-06-25 09:38:13 +00004604 SDValue Op1 = getValue(I.getArgOperand(0));
4605 SDValue Op2 = getValue(I.getArgOperand(1));
4606 SDValue Op3 = getValue(I.getArgOperand(2));
4607 unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
Chandler Carruthaf23f8e2013-02-25 14:20:21 +00004608 if (!Align)
4609 Align = 1; // @llvm.memset defines 0 and 1 to both mean no alignment.
Gabor Greif0635f352010-06-25 09:38:13 +00004610 bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004611 DAG.setRoot(DAG.getMemset(getRoot(), sdl, Op1, Op2, Op3, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004612 MachinePointerInfo(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004613 return 0;
4614 }
Chris Lattner824b9582008-11-21 16:42:48 +00004615 case Intrinsic::memmove: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00004616 // Assert for address < 256 since we support only user defined address
4617 // spaces.
Gabor Greif0635f352010-06-25 09:38:13 +00004618 assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004619 < 256 &&
Gabor Greif0635f352010-06-25 09:38:13 +00004620 cast<PointerType>(I.getArgOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004621 < 256 &&
4622 "Unknown address space");
Gabor Greif0635f352010-06-25 09:38:13 +00004623 SDValue Op1 = getValue(I.getArgOperand(0));
4624 SDValue Op2 = getValue(I.getArgOperand(1));
4625 SDValue Op3 = getValue(I.getArgOperand(2));
4626 unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
Chandler Carruthaf23f8e2013-02-25 14:20:21 +00004627 if (!Align)
4628 Align = 1; // @llvm.memmove defines 0 and 1 to both mean no alignment.
Gabor Greif0635f352010-06-25 09:38:13 +00004629 bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004630 DAG.setRoot(DAG.getMemmove(getRoot(), sdl, Op1, Op2, Op3, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004631 MachinePointerInfo(I.getArgOperand(0)),
4632 MachinePointerInfo(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004633 return 0;
4634 }
Bill Wendling92c1e122009-02-13 02:16:35 +00004635 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00004636 const DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
Devang Patelac1ceb32009-10-09 22:42:28 +00004637 MDNode *Variable = DI.getVariable();
Dan Gohman46510a72010-04-15 01:51:59 +00004638 const Value *Address = DI.getAddress();
Manman Rencbafae62013-06-28 05:43:10 +00004639 DIVariable DIVar(Variable);
4640 assert((!DIVar || DIVar.isVariable()) &&
4641 "Variable in DbgDeclareInst should be either null or a DIVariable.");
4642 if (!Address || !DIVar) {
Eric Christopher8f2a88d2012-03-15 21:33:41 +00004643 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
Dale Johannesen8ac38f22010-02-08 21:53:27 +00004644 return 0;
Eric Christopher8f2a88d2012-03-15 21:33:41 +00004645 }
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004646
Devang Patel3f74a112010-09-02 21:29:42 +00004647 // Check if address has undef value.
4648 if (isa<UndefValue>(Address) ||
4649 (Address->use_empty() && !isa<Argument>(Address))) {
Eric Christopher24413672012-02-23 03:39:39 +00004650 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
Devang Patel3f74a112010-09-02 21:29:42 +00004651 return 0;
4652 }
4653
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004654 SDValue &N = NodeMap[Address];
Devang Patel0b48ead2010-08-31 22:22:42 +00004655 if (!N.getNode() && isa<Argument>(Address))
4656 // Check unused arguments map.
4657 N = UnusedArgNodeMap[Address];
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004658 SDDbgValue *SDV;
4659 if (N.getNode()) {
Devang Patel8e741ed2010-09-02 21:02:27 +00004660 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
4661 Address = BCI->getOperand(0);
Eric Christopher178606d2012-02-24 01:59:08 +00004662 // Parameters are handled specially.
4663 bool isParameter =
4664 (DIVariable(Variable).getTag() == dwarf::DW_TAG_arg_variable ||
4665 isa<Argument>(Address));
4666
Devang Patel8e741ed2010-09-02 21:02:27 +00004667 const AllocaInst *AI = dyn_cast<AllocaInst>(Address);
4668
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004669 if (isParameter && !AI) {
4670 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N.getNode());
4671 if (FINode)
4672 // Byval parameter. We have a frame index at this point.
4673 SDV = DAG.getDbgValue(Variable, FINode->getIndex(),
4674 0, dl, SDNodeOrder);
Devang Patelafeaae72010-12-06 22:39:26 +00004675 else {
Devang Patel227dfdb2011-05-16 21:24:05 +00004676 // Address is an argument, so try to emit its dbg value using
4677 // virtual register info from the FuncInfo.ValueMap.
4678 EmitFuncArgumentDbgValue(Address, Variable, 0, N);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004679 return 0;
Devang Patelafeaae72010-12-06 22:39:26 +00004680 }
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004681 } else if (AI)
4682 SDV = DAG.getDbgValue(Variable, N.getNode(), N.getResNo(),
4683 0, dl, SDNodeOrder);
Devang Patelafeaae72010-12-06 22:39:26 +00004684 else {
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004685 // Can't do anything with other non-AI cases yet.
Eric Christopher24413672012-02-23 03:39:39 +00004686 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
Eric Christopher178606d2012-02-24 01:59:08 +00004687 DEBUG(dbgs() << "non-AllocaInst issue for Address: \n\t");
4688 DEBUG(Address->dump());
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004689 return 0;
Devang Patelafeaae72010-12-06 22:39:26 +00004690 }
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004691 DAG.AddDbgValue(SDV, N.getNode(), isParameter);
4692 } else {
Gabor Greiffb4032f2010-10-01 10:32:19 +00004693 // If Address is an argument then try to emit its dbg value using
Michael J. Spencere70c5262010-10-16 08:25:21 +00004694 // virtual register info from the FuncInfo.ValueMap.
Devang Patel6cd467b2010-08-26 22:53:27 +00004695 if (!EmitFuncArgumentDbgValue(Address, Variable, 0, N)) {
Devang Patel1397fdc2010-09-15 14:48:53 +00004696 // If variable is pinned by a alloca in dominating bb then
4697 // use StaticAllocaMap.
4698 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Address)) {
Devang Patel27ede1b2010-09-15 18:13:55 +00004699 if (AI->getParent() != DI.getParent()) {
4700 DenseMap<const AllocaInst*, int>::iterator SI =
4701 FuncInfo.StaticAllocaMap.find(AI);
4702 if (SI != FuncInfo.StaticAllocaMap.end()) {
4703 SDV = DAG.getDbgValue(Variable, SI->second,
4704 0, dl, SDNodeOrder);
4705 DAG.AddDbgValue(SDV, 0, false);
4706 return 0;
4707 }
Devang Patel1397fdc2010-09-15 14:48:53 +00004708 }
4709 }
Eric Christopher0822e012012-02-23 03:39:43 +00004710 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
Devang Patel6cd467b2010-08-26 22:53:27 +00004711 }
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004712 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004713 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00004714 }
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004715 case Intrinsic::dbg_value: {
Dan Gohman46510a72010-04-15 01:51:59 +00004716 const DbgValueInst &DI = cast<DbgValueInst>(I);
Manman Rencbafae62013-06-28 05:43:10 +00004717 DIVariable DIVar(DI.getVariable());
4718 assert((!DIVar || DIVar.isVariable()) &&
4719 "Variable in DbgValueInst should be either null or a DIVariable.");
4720 if (!DIVar)
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004721 return 0;
4722
4723 MDNode *Variable = DI.getVariable();
Devang Patel00190342010-03-15 19:15:44 +00004724 uint64_t Offset = DI.getOffset();
Dan Gohman46510a72010-04-15 01:51:59 +00004725 const Value *V = DI.getValue();
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004726 if (!V)
4727 return 0;
Devang Patel00190342010-03-15 19:15:44 +00004728
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004729 SDDbgValue *SDV;
Devang Patel57871242011-08-03 23:13:55 +00004730 if (isa<ConstantInt>(V) || isa<ConstantFP>(V) || isa<UndefValue>(V)) {
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004731 SDV = DAG.getDbgValue(Variable, V, Offset, dl, SDNodeOrder);
4732 DAG.AddDbgValue(SDV, 0, false);
Devang Patel00190342010-03-15 19:15:44 +00004733 } else {
Dale Johannesenbdc09d92010-07-16 00:02:08 +00004734 // Do not use getValue() in here; we don't want to generate code at
4735 // this point if it hasn't been done yet.
Devang Patel9126c0d2010-06-01 19:59:01 +00004736 SDValue N = NodeMap[V];
4737 if (!N.getNode() && isa<Argument>(V))
4738 // Check unused arguments map.
4739 N = UnusedArgNodeMap[V];
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004740 if (N.getNode()) {
Devang Patel78a06e52010-08-25 20:39:26 +00004741 if (!EmitFuncArgumentDbgValue(V, Variable, Offset, N)) {
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004742 SDV = DAG.getDbgValue(Variable, N.getNode(),
4743 N.getResNo(), Offset, dl, SDNodeOrder);
4744 DAG.AddDbgValue(SDV, N.getNode(), false);
4745 }
Devang Patela778f5c2011-02-18 22:43:42 +00004746 } else if (!V->use_empty() ) {
Dale Johannesenbdc09d92010-07-16 00:02:08 +00004747 // Do not call getValue(V) yet, as we don't want to generate code.
4748 // Remember it for later.
4749 DanglingDebugInfo DDI(&DI, dl, SDNodeOrder);
4750 DanglingDebugInfoMap[V] = DDI;
Devang Patel0991dfb2010-08-27 22:25:51 +00004751 } else {
Devang Patel00190342010-03-15 19:15:44 +00004752 // We may expand this to cover more cases. One case where we have no
Devang Patelafeaae72010-12-06 22:39:26 +00004753 // data available is an unreferenced parameter.
Eric Christopher0822e012012-02-23 03:39:43 +00004754 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004755 }
Devang Patel00190342010-03-15 19:15:44 +00004756 }
4757
4758 // Build a debug info table entry.
Dan Gohman46510a72010-04-15 01:51:59 +00004759 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(V))
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004760 V = BCI->getOperand(0);
Dan Gohman46510a72010-04-15 01:51:59 +00004761 const AllocaInst *AI = dyn_cast<AllocaInst>(V);
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004762 // Don't handle byval struct arguments or VLAs, for example.
Eric Christopher7e1e18f2012-03-26 06:10:32 +00004763 if (!AI) {
Eric Christopher9fc5c832012-03-28 07:34:36 +00004764 DEBUG(dbgs() << "Dropping debug location info for:\n " << DI << "\n");
4765 DEBUG(dbgs() << " Last seen at:\n " << *V << "\n");
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004766 return 0;
Eric Christopher7e1e18f2012-03-26 06:10:32 +00004767 }
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004768 DenseMap<const AllocaInst*, int>::iterator SI =
4769 FuncInfo.StaticAllocaMap.find(AI);
4770 if (SI == FuncInfo.StaticAllocaMap.end())
4771 return 0; // VLAs.
4772 int FI = SI->second;
Michael J. Spencere70c5262010-10-16 08:25:21 +00004773
Chris Lattner512063d2010-04-05 06:19:28 +00004774 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
4775 if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo())
4776 MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc());
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004777 return 0;
4778 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004779
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004780 case Intrinsic::eh_typeid_for: {
Chris Lattner512063d2010-04-05 06:19:28 +00004781 // Find the type id for the given typeinfo.
Gabor Greif0635f352010-06-25 09:38:13 +00004782 GlobalVariable *GV = ExtractTypeInfo(I.getArgOperand(0));
Chris Lattner512063d2010-04-05 06:19:28 +00004783 unsigned TypeID = DAG.getMachineFunction().getMMI().getTypeIDFor(GV);
4784 Res = DAG.getConstant(TypeID, MVT::i32);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004785 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004786 return 0;
4787 }
4788
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004789 case Intrinsic::eh_return_i32:
4790 case Intrinsic::eh_return_i64:
Chris Lattner512063d2010-04-05 06:19:28 +00004791 DAG.getMachineFunction().getMMI().setCallsEHReturn(true);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004792 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, sdl,
Chris Lattner512063d2010-04-05 06:19:28 +00004793 MVT::Other,
4794 getControlRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00004795 getValue(I.getArgOperand(0)),
4796 getValue(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004797 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004798 case Intrinsic::eh_unwind_init:
Chris Lattner512063d2010-04-05 06:19:28 +00004799 DAG.getMachineFunction().getMMI().setCallsUnwindInit(true);
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004800 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004801 case Intrinsic::eh_dwarf_cfa: {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004802 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getArgOperand(0)), sdl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00004803 TLI->getPointerTy());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004804 SDValue Offset = DAG.getNode(ISD::ADD, sdl,
Tom Stellardedd08f72013-08-26 15:06:10 +00004805 CfaArg.getValueType(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004806 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, sdl,
Tom Stellardedd08f72013-08-26 15:06:10 +00004807 CfaArg.getValueType()),
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004808 CfaArg);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004809 SDValue FA = DAG.getNode(ISD::FRAMEADDR, sdl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00004810 TLI->getPointerTy(),
4811 DAG.getConstant(0, TLI->getPointerTy()));
Tom Stellardedd08f72013-08-26 15:06:10 +00004812 setValue(&I, DAG.getNode(ISD::ADD, sdl, FA.getValueType(),
Bill Wendling4533cac2010-01-28 21:51:40 +00004813 FA, Offset));
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004814 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004815 }
Jim Grosbachca752c92010-01-28 01:45:32 +00004816 case Intrinsic::eh_sjlj_callsite: {
Chris Lattner512063d2010-04-05 06:19:28 +00004817 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Gabor Greif0635f352010-06-25 09:38:13 +00004818 ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(0));
Jim Grosbachca752c92010-01-28 01:45:32 +00004819 assert(CI && "Non-constant call site value in eh.sjlj.callsite!");
Chris Lattner512063d2010-04-05 06:19:28 +00004820 assert(MMI.getCurrentCallSite() == 0 && "Overlapping call sites!");
Jim Grosbachca752c92010-01-28 01:45:32 +00004821
Chris Lattner512063d2010-04-05 06:19:28 +00004822 MMI.setCurrentCallSite(CI->getZExtValue());
Jim Grosbachca752c92010-01-28 01:45:32 +00004823 return 0;
4824 }
Bill Wendling6ef94172011-09-28 03:36:43 +00004825 case Intrinsic::eh_sjlj_functioncontext: {
4826 // Get and store the index of the function context.
4827 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bill Wendlingadbf7b22011-09-28 03:52:41 +00004828 AllocaInst *FnCtx =
4829 cast<AllocaInst>(I.getArgOperand(0)->stripPointerCasts());
Bill Wendling6ef94172011-09-28 03:36:43 +00004830 int FI = FuncInfo.StaticAllocaMap[FnCtx];
4831 MFI->setFunctionContextIndex(FI);
4832 return 0;
4833 }
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004834 case Intrinsic::eh_sjlj_setjmp: {
Bill Wendlingce370cf2011-10-07 21:25:38 +00004835 SDValue Ops[2];
4836 Ops[0] = getRoot();
4837 Ops[1] = getValue(I.getArgOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004838 SDValue Op = DAG.getNode(ISD::EH_SJLJ_SETJMP, sdl,
Bill Wendlingce370cf2011-10-07 21:25:38 +00004839 DAG.getVTList(MVT::i32, MVT::Other),
4840 Ops, 2);
4841 setValue(&I, Op.getValue(0));
4842 DAG.setRoot(Op.getValue(1));
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004843 return 0;
4844 }
Jim Grosbach5eb19512010-05-22 01:06:18 +00004845 case Intrinsic::eh_sjlj_longjmp: {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004846 DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_LONGJMP, sdl, MVT::Other,
Jim Grosbache4ad3872010-10-19 23:27:08 +00004847 getRoot(), getValue(I.getArgOperand(0))));
4848 return 0;
4849 }
Jim Grosbachca752c92010-01-28 01:45:32 +00004850
Dale Johannesen0488fb62010-09-30 23:57:10 +00004851 case Intrinsic::x86_mmx_pslli_w:
4852 case Intrinsic::x86_mmx_pslli_d:
4853 case Intrinsic::x86_mmx_pslli_q:
4854 case Intrinsic::x86_mmx_psrli_w:
4855 case Intrinsic::x86_mmx_psrli_d:
4856 case Intrinsic::x86_mmx_psrli_q:
4857 case Intrinsic::x86_mmx_psrai_w:
4858 case Intrinsic::x86_mmx_psrai_d: {
4859 SDValue ShAmt = getValue(I.getArgOperand(1));
4860 if (isa<ConstantSDNode>(ShAmt)) {
4861 visitTargetIntrinsic(I, Intrinsic);
4862 return 0;
4863 }
4864 unsigned NewIntrinsic = 0;
4865 EVT ShAmtVT = MVT::v2i32;
4866 switch (Intrinsic) {
4867 case Intrinsic::x86_mmx_pslli_w:
4868 NewIntrinsic = Intrinsic::x86_mmx_psll_w;
4869 break;
4870 case Intrinsic::x86_mmx_pslli_d:
4871 NewIntrinsic = Intrinsic::x86_mmx_psll_d;
4872 break;
4873 case Intrinsic::x86_mmx_pslli_q:
4874 NewIntrinsic = Intrinsic::x86_mmx_psll_q;
4875 break;
4876 case Intrinsic::x86_mmx_psrli_w:
4877 NewIntrinsic = Intrinsic::x86_mmx_psrl_w;
4878 break;
4879 case Intrinsic::x86_mmx_psrli_d:
4880 NewIntrinsic = Intrinsic::x86_mmx_psrl_d;
4881 break;
4882 case Intrinsic::x86_mmx_psrli_q:
4883 NewIntrinsic = Intrinsic::x86_mmx_psrl_q;
4884 break;
4885 case Intrinsic::x86_mmx_psrai_w:
4886 NewIntrinsic = Intrinsic::x86_mmx_psra_w;
4887 break;
4888 case Intrinsic::x86_mmx_psrai_d:
4889 NewIntrinsic = Intrinsic::x86_mmx_psra_d;
4890 break;
4891 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
4892 }
4893
4894 // The vector shift intrinsics with scalars uses 32b shift amounts but
4895 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
4896 // to be zero.
4897 // We must do this early because v2i32 is not a legal type.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004898 SDValue ShOps[2];
4899 ShOps[0] = ShAmt;
4900 ShOps[1] = DAG.getConstant(0, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004901 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, sdl, ShAmtVT, &ShOps[0], 2);
Bill Wendlingba54bca2013-06-19 21:36:55 +00004902 EVT DestVT = TLI->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004903 ShAmt = DAG.getNode(ISD::BITCAST, sdl, DestVT, ShAmt);
4904 Res = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, sdl, DestVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00004905 DAG.getConstant(NewIntrinsic, MVT::i32),
4906 getValue(I.getArgOperand(0)), ShAmt);
4907 setValue(&I, Res);
4908 return 0;
4909 }
Pete Cooperd18134f2012-02-24 03:51:49 +00004910 case Intrinsic::x86_avx_vinsertf128_pd_256:
4911 case Intrinsic::x86_avx_vinsertf128_ps_256:
Craig Topperb45c9692012-04-07 22:32:29 +00004912 case Intrinsic::x86_avx_vinsertf128_si_256:
4913 case Intrinsic::x86_avx2_vinserti128: {
Bill Wendlingba54bca2013-06-19 21:36:55 +00004914 EVT DestVT = TLI->getValueType(I.getType());
4915 EVT ElVT = TLI->getValueType(I.getArgOperand(1)->getType());
Pete Cooperd18134f2012-02-24 03:51:49 +00004916 uint64_t Idx = (cast<ConstantInt>(I.getArgOperand(2))->getZExtValue() & 1) *
4917 ElVT.getVectorNumElements();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004918 Res = DAG.getNode(ISD::INSERT_SUBVECTOR, sdl, DestVT,
Pete Cooperd18134f2012-02-24 03:51:49 +00004919 getValue(I.getArgOperand(0)),
4920 getValue(I.getArgOperand(1)),
Tom Stellard425b76c2013-08-05 22:22:01 +00004921 DAG.getConstant(Idx, TLI->getVectorIdxTy()));
Craig Topperf6dc7922012-09-05 05:48:09 +00004922 setValue(&I, Res);
4923 return 0;
4924 }
4925 case Intrinsic::x86_avx_vextractf128_pd_256:
4926 case Intrinsic::x86_avx_vextractf128_ps_256:
4927 case Intrinsic::x86_avx_vextractf128_si_256:
4928 case Intrinsic::x86_avx2_vextracti128: {
Bill Wendlingba54bca2013-06-19 21:36:55 +00004929 EVT DestVT = TLI->getValueType(I.getType());
Craig Topperf6dc7922012-09-05 05:48:09 +00004930 uint64_t Idx = (cast<ConstantInt>(I.getArgOperand(1))->getZExtValue() & 1) *
4931 DestVT.getVectorNumElements();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004932 Res = DAG.getNode(ISD::EXTRACT_SUBVECTOR, sdl, DestVT,
Craig Topperf6dc7922012-09-05 05:48:09 +00004933 getValue(I.getArgOperand(0)),
Tom Stellard425b76c2013-08-05 22:22:01 +00004934 DAG.getConstant(Idx, TLI->getVectorIdxTy()));
Pete Cooperd18134f2012-02-24 03:51:49 +00004935 setValue(&I, Res);
4936 return 0;
4937 }
Mon P Wang77cdf302008-11-10 20:54:11 +00004938 case Intrinsic::convertff:
4939 case Intrinsic::convertfsi:
4940 case Intrinsic::convertfui:
4941 case Intrinsic::convertsif:
4942 case Intrinsic::convertuif:
4943 case Intrinsic::convertss:
4944 case Intrinsic::convertsu:
4945 case Intrinsic::convertus:
4946 case Intrinsic::convertuu: {
4947 ISD::CvtCode Code = ISD::CVT_INVALID;
4948 switch (Intrinsic) {
Craig Topperc42e6402012-04-11 04:34:11 +00004949 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Mon P Wang77cdf302008-11-10 20:54:11 +00004950 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
4951 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
4952 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
4953 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
4954 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
4955 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
4956 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
4957 case Intrinsic::convertus: Code = ISD::CVT_US; break;
4958 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
4959 }
Bill Wendlingba54bca2013-06-19 21:36:55 +00004960 EVT DestVT = TLI->getValueType(I.getType());
Gabor Greif0635f352010-06-25 09:38:13 +00004961 const Value *Op1 = I.getArgOperand(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004962 Res = DAG.getConvertRndSat(DestVT, sdl, getValue(Op1),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004963 DAG.getValueType(DestVT),
4964 DAG.getValueType(getValue(Op1).getValueType()),
Gabor Greif0635f352010-06-25 09:38:13 +00004965 getValue(I.getArgOperand(1)),
4966 getValue(I.getArgOperand(2)),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004967 Code);
4968 setValue(&I, Res);
Mon P Wang77cdf302008-11-10 20:54:11 +00004969 return 0;
4970 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004971 case Intrinsic::powi:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004972 setValue(&I, ExpandPowI(sdl, getValue(I.getArgOperand(0)),
Gabor Greif0635f352010-06-25 09:38:13 +00004973 getValue(I.getArgOperand(1)), DAG));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004974 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004975 case Intrinsic::log:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004976 setValue(&I, expandLog(sdl, getValue(I.getArgOperand(0)), DAG, *TLI));
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004977 return 0;
4978 case Intrinsic::log2:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004979 setValue(&I, expandLog2(sdl, getValue(I.getArgOperand(0)), DAG, *TLI));
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004980 return 0;
4981 case Intrinsic::log10:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004982 setValue(&I, expandLog10(sdl, getValue(I.getArgOperand(0)), DAG, *TLI));
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004983 return 0;
4984 case Intrinsic::exp:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004985 setValue(&I, expandExp(sdl, getValue(I.getArgOperand(0)), DAG, *TLI));
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004986 return 0;
4987 case Intrinsic::exp2:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004988 setValue(&I, expandExp2(sdl, getValue(I.getArgOperand(0)), DAG, *TLI));
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004989 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004990 case Intrinsic::pow:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004991 setValue(&I, expandPow(sdl, getValue(I.getArgOperand(0)),
Bill Wendlingba54bca2013-06-19 21:36:55 +00004992 getValue(I.getArgOperand(1)), DAG, *TLI));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004993 return 0;
Craig Topper9bd4dd72012-11-16 07:48:23 +00004994 case Intrinsic::sqrt:
Peter Collingbourneb34d3aa2012-05-28 21:48:37 +00004995 case Intrinsic::fabs:
Craig Topper9bd4dd72012-11-16 07:48:23 +00004996 case Intrinsic::sin:
4997 case Intrinsic::cos:
Dan Gohman27db99f2012-07-26 17:43:27 +00004998 case Intrinsic::floor:
Craig Topper49010472012-11-15 06:51:10 +00004999 case Intrinsic::ceil:
Craig Topper49010472012-11-15 06:51:10 +00005000 case Intrinsic::trunc:
Craig Topper49010472012-11-15 06:51:10 +00005001 case Intrinsic::rint:
Hal Finkel41418d12013-08-07 22:49:12 +00005002 case Intrinsic::nearbyint:
5003 case Intrinsic::round: {
Craig Topper9bd4dd72012-11-16 07:48:23 +00005004 unsigned Opcode;
5005 switch (Intrinsic) {
5006 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
5007 case Intrinsic::sqrt: Opcode = ISD::FSQRT; break;
5008 case Intrinsic::fabs: Opcode = ISD::FABS; break;
5009 case Intrinsic::sin: Opcode = ISD::FSIN; break;
5010 case Intrinsic::cos: Opcode = ISD::FCOS; break;
5011 case Intrinsic::floor: Opcode = ISD::FFLOOR; break;
5012 case Intrinsic::ceil: Opcode = ISD::FCEIL; break;
5013 case Intrinsic::trunc: Opcode = ISD::FTRUNC; break;
5014 case Intrinsic::rint: Opcode = ISD::FRINT; break;
5015 case Intrinsic::nearbyint: Opcode = ISD::FNEARBYINT; break;
Hal Finkel41418d12013-08-07 22:49:12 +00005016 case Intrinsic::round: Opcode = ISD::FROUND; break;
Craig Topper9bd4dd72012-11-16 07:48:23 +00005017 }
5018
Andrew Trickac6d9be2013-05-25 02:42:55 +00005019 setValue(&I, DAG.getNode(Opcode, sdl,
Craig Topper49010472012-11-15 06:51:10 +00005020 getValue(I.getArgOperand(0)).getValueType(),
5021 getValue(I.getArgOperand(0))));
5022 return 0;
Craig Topper9bd4dd72012-11-16 07:48:23 +00005023 }
Hal Finkel66d1fa62013-08-19 23:35:46 +00005024 case Intrinsic::copysign:
5025 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, sdl,
5026 getValue(I.getArgOperand(0)).getValueType(),
5027 getValue(I.getArgOperand(0)),
5028 getValue(I.getArgOperand(1))));
5029 return 0;
Cameron Zwarich33390842011-07-08 21:39:21 +00005030 case Intrinsic::fma:
Andrew Trickac6d9be2013-05-25 02:42:55 +00005031 setValue(&I, DAG.getNode(ISD::FMA, sdl,
Cameron Zwarich33390842011-07-08 21:39:21 +00005032 getValue(I.getArgOperand(0)).getValueType(),
5033 getValue(I.getArgOperand(0)),
5034 getValue(I.getArgOperand(1)),
5035 getValue(I.getArgOperand(2))));
5036 return 0;
Lang Hames5afba6f2012-06-05 19:07:46 +00005037 case Intrinsic::fmuladd: {
Bill Wendlingba54bca2013-06-19 21:36:55 +00005038 EVT VT = TLI->getValueType(I.getType());
Lang Hamese0231412012-06-22 01:09:09 +00005039 if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict &&
Stephen Line54885a2013-07-09 18:16:56 +00005040 TLI->isFMAFasterThanFMulAndFAdd(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005041 setValue(&I, DAG.getNode(ISD::FMA, sdl,
Lang Hames5afba6f2012-06-05 19:07:46 +00005042 getValue(I.getArgOperand(0)).getValueType(),
5043 getValue(I.getArgOperand(0)),
5044 getValue(I.getArgOperand(1)),
5045 getValue(I.getArgOperand(2))));
5046 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005047 SDValue Mul = DAG.getNode(ISD::FMUL, sdl,
Lang Hames5afba6f2012-06-05 19:07:46 +00005048 getValue(I.getArgOperand(0)).getValueType(),
5049 getValue(I.getArgOperand(0)),
5050 getValue(I.getArgOperand(1)));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005051 SDValue Add = DAG.getNode(ISD::FADD, sdl,
Lang Hames5afba6f2012-06-05 19:07:46 +00005052 getValue(I.getArgOperand(0)).getValueType(),
5053 Mul,
5054 getValue(I.getArgOperand(2)));
5055 setValue(&I, Add);
5056 }
5057 return 0;
5058 }
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00005059 case Intrinsic::convert_to_fp16:
Andrew Trickac6d9be2013-05-25 02:42:55 +00005060 setValue(&I, DAG.getNode(ISD::FP32_TO_FP16, sdl,
Gabor Greif0635f352010-06-25 09:38:13 +00005061 MVT::i16, getValue(I.getArgOperand(0))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00005062 return 0;
5063 case Intrinsic::convert_from_fp16:
Andrew Trickac6d9be2013-05-25 02:42:55 +00005064 setValue(&I, DAG.getNode(ISD::FP16_TO_FP32, sdl,
Gabor Greif0635f352010-06-25 09:38:13 +00005065 MVT::f32, getValue(I.getArgOperand(0))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00005066 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005067 case Intrinsic::pcmarker: {
Gabor Greif0635f352010-06-25 09:38:13 +00005068 SDValue Tmp = getValue(I.getArgOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005069 DAG.setRoot(DAG.getNode(ISD::PCMARKER, sdl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005070 return 0;
5071 }
5072 case Intrinsic::readcyclecounter: {
5073 SDValue Op = getRoot();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005074 Res = DAG.getNode(ISD::READCYCLECOUNTER, sdl,
Bill Wendlingd0283fa2009-12-22 00:40:51 +00005075 DAG.getVTList(MVT::i64, MVT::Other),
5076 &Op, 1);
5077 setValue(&I, Res);
5078 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005079 return 0;
5080 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005081 case Intrinsic::bswap:
Andrew Trickac6d9be2013-05-25 02:42:55 +00005082 setValue(&I, DAG.getNode(ISD::BSWAP, sdl,
Gabor Greif0635f352010-06-25 09:38:13 +00005083 getValue(I.getArgOperand(0)).getValueType(),
5084 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005085 return 0;
5086 case Intrinsic::cttz: {
Gabor Greif0635f352010-06-25 09:38:13 +00005087 SDValue Arg = getValue(I.getArgOperand(0));
Chandler Carruth63974b22011-12-13 01:56:10 +00005088 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00005089 EVT Ty = Arg.getValueType();
Chandler Carruth63974b22011-12-13 01:56:10 +00005090 setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTTZ : ISD::CTTZ_ZERO_UNDEF,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005091 sdl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005092 return 0;
5093 }
5094 case Intrinsic::ctlz: {
Gabor Greif0635f352010-06-25 09:38:13 +00005095 SDValue Arg = getValue(I.getArgOperand(0));
Chandler Carruth63974b22011-12-13 01:56:10 +00005096 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00005097 EVT Ty = Arg.getValueType();
Chandler Carruth63974b22011-12-13 01:56:10 +00005098 setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTLZ : ISD::CTLZ_ZERO_UNDEF,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005099 sdl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005100 return 0;
5101 }
5102 case Intrinsic::ctpop: {
Gabor Greif0635f352010-06-25 09:38:13 +00005103 SDValue Arg = getValue(I.getArgOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00005104 EVT Ty = Arg.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005105 setValue(&I, DAG.getNode(ISD::CTPOP, sdl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005106 return 0;
5107 }
5108 case Intrinsic::stacksave: {
5109 SDValue Op = getRoot();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005110 Res = DAG.getNode(ISD::STACKSAVE, sdl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00005111 DAG.getVTList(TLI->getPointerTy(), MVT::Other), &Op, 1);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00005112 setValue(&I, Res);
5113 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005114 return 0;
5115 }
5116 case Intrinsic::stackrestore: {
Gabor Greif0635f352010-06-25 09:38:13 +00005117 Res = getValue(I.getArgOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005118 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, sdl, MVT::Other, getRoot(), Res));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005119 return 0;
5120 }
Bill Wendling57344502008-11-18 11:01:33 +00005121 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00005122 // Emit code into the DAG to store the stack guard onto the stack.
5123 MachineFunction &MF = DAG.getMachineFunction();
5124 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendlingba54bca2013-06-19 21:36:55 +00005125 EVT PtrTy = TLI->getPointerTy();
Bill Wendlingb2a42982008-11-06 02:29:10 +00005126
Gabor Greif0635f352010-06-25 09:38:13 +00005127 SDValue Src = getValue(I.getArgOperand(0)); // The guard's value.
5128 AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Bill Wendlingb2a42982008-11-06 02:29:10 +00005129
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00005130 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00005131 MFI->setStackProtectorIndex(FI);
5132
5133 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
5134
5135 // Store the stack protector onto the stack.
Andrew Trickac6d9be2013-05-25 02:42:55 +00005136 Res = DAG.getStore(getRoot(), sdl, Src, FIN,
Chris Lattner84bd98a2010-09-21 18:58:22 +00005137 MachinePointerInfo::getFixedStack(FI),
5138 true, false, 0);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00005139 setValue(&I, Res);
5140 DAG.setRoot(Res);
Bill Wendlingb2a42982008-11-06 02:29:10 +00005141 return 0;
5142 }
Eric Christopher7b5e6172009-10-27 00:52:25 +00005143 case Intrinsic::objectsize: {
5144 // If we don't know by now, we're never going to know.
Gabor Greif0635f352010-06-25 09:38:13 +00005145 ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(1));
Eric Christopher7b5e6172009-10-27 00:52:25 +00005146
5147 assert(CI && "Non-constant type in __builtin_object_size?");
5148
Gabor Greif0635f352010-06-25 09:38:13 +00005149 SDValue Arg = getValue(I.getCalledValue());
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00005150 EVT Ty = Arg.getValueType();
5151
Dan Gohmane368b462010-06-18 14:22:04 +00005152 if (CI->isZero())
Bill Wendlingd0283fa2009-12-22 00:40:51 +00005153 Res = DAG.getConstant(-1ULL, Ty);
Eric Christopher7b5e6172009-10-27 00:52:25 +00005154 else
Bill Wendlingd0283fa2009-12-22 00:40:51 +00005155 Res = DAG.getConstant(0, Ty);
5156
5157 setValue(&I, Res);
Eric Christopher7b5e6172009-10-27 00:52:25 +00005158 return 0;
5159 }
Justin Holewinskic2b7f5f2013-05-21 14:37:16 +00005160 case Intrinsic::annotation:
5161 case Intrinsic::ptr_annotation:
5162 // Drop the intrinsic, but forward the value
5163 setValue(&I, getValue(I.getOperand(0)));
5164 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005165 case Intrinsic::var_annotation:
5166 // Discard annotate attributes
5167 return 0;
5168
5169 case Intrinsic::init_trampoline: {
Gabor Greif0635f352010-06-25 09:38:13 +00005170 const Function *F = cast<Function>(I.getArgOperand(1)->stripPointerCasts());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005171
5172 SDValue Ops[6];
5173 Ops[0] = getRoot();
Gabor Greif0635f352010-06-25 09:38:13 +00005174 Ops[1] = getValue(I.getArgOperand(0));
5175 Ops[2] = getValue(I.getArgOperand(1));
5176 Ops[3] = getValue(I.getArgOperand(2));
5177 Ops[4] = DAG.getSrcValue(I.getArgOperand(0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005178 Ops[5] = DAG.getSrcValue(F);
5179
Andrew Trickac6d9be2013-05-25 02:42:55 +00005180 Res = DAG.getNode(ISD::INIT_TRAMPOLINE, sdl, MVT::Other, Ops, 6);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005181
Duncan Sands4a544a72011-09-06 13:37:06 +00005182 DAG.setRoot(Res);
5183 return 0;
5184 }
5185 case Intrinsic::adjust_trampoline: {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005186 setValue(&I, DAG.getNode(ISD::ADJUST_TRAMPOLINE, sdl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00005187 TLI->getPointerTy(),
Duncan Sands4a544a72011-09-06 13:37:06 +00005188 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005189 return 0;
5190 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005191 case Intrinsic::gcroot:
5192 if (GFI) {
Bill Wendling95dd4422012-05-01 22:50:45 +00005193 const Value *Alloca = I.getArgOperand(0)->stripPointerCasts();
Gabor Greif0635f352010-06-25 09:38:13 +00005194 const Constant *TypeMap = cast<Constant>(I.getArgOperand(1));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005195
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005196 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
5197 GFI->addStackRoot(FI->getIndex(), TypeMap);
5198 }
5199 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005200 case Intrinsic::gcread:
5201 case Intrinsic::gcwrite:
Torok Edwinc23197a2009-07-14 16:55:14 +00005202 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
Bill Wendlingd0283fa2009-12-22 00:40:51 +00005203 case Intrinsic::flt_rounds:
Andrew Trickac6d9be2013-05-25 02:42:55 +00005204 setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, sdl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005205 return 0;
Jakub Staszak9da99342011-07-06 18:22:43 +00005206
5207 case Intrinsic::expect: {
5208 // Just replace __builtin_expect(exp, c) with EXP.
5209 setValue(&I, getValue(I.getArgOperand(0)));
5210 return 0;
5211 }
5212
Shuxin Yang970755e2012-10-19 20:11:16 +00005213 case Intrinsic::debugtrap:
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005214 case Intrinsic::trap: {
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005215 StringRef TrapFuncName = TM.Options.getTrapFunctionName();
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005216 if (TrapFuncName.empty()) {
Stephen Lin155615d2013-07-08 00:37:03 +00005217 ISD::NodeType Op = (Intrinsic == Intrinsic::trap) ?
Shuxin Yang970755e2012-10-19 20:11:16 +00005218 ISD::TRAP : ISD::DEBUGTRAP;
Andrew Trickac6d9be2013-05-25 02:42:55 +00005219 DAG.setRoot(DAG.getNode(Op, sdl,MVT::Other, getRoot()));
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005220 return 0;
5221 }
5222 TargetLowering::ArgListTy Args;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00005223 TargetLowering::
5224 CallLoweringInfo CLI(getRoot(), I.getType(),
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005225 false, false, false, false, 0, CallingConv::C,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00005226 /*isTailCall=*/false,
5227 /*doesNotRet=*/false, /*isReturnValueUsed=*/true,
Bill Wendlingba54bca2013-06-19 21:36:55 +00005228 DAG.getExternalSymbol(TrapFuncName.data(),
5229 TLI->getPointerTy()),
Andrew Trickac6d9be2013-05-25 02:42:55 +00005230 Args, DAG, sdl);
Bill Wendlingba54bca2013-06-19 21:36:55 +00005231 std::pair<SDValue, SDValue> Result = TLI->LowerCallTo(CLI);
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005232 DAG.setRoot(Result.second);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005233 return 0;
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005234 }
Shuxin Yang970755e2012-10-19 20:11:16 +00005235
Bill Wendlingef375462008-11-21 02:38:44 +00005236 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00005237 case Intrinsic::sadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00005238 case Intrinsic::usub_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00005239 case Intrinsic::ssub_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00005240 case Intrinsic::umul_with_overflow:
Craig Topperc42e6402012-04-11 04:34:11 +00005241 case Intrinsic::smul_with_overflow: {
5242 ISD::NodeType Op;
5243 switch (Intrinsic) {
5244 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
5245 case Intrinsic::uadd_with_overflow: Op = ISD::UADDO; break;
5246 case Intrinsic::sadd_with_overflow: Op = ISD::SADDO; break;
5247 case Intrinsic::usub_with_overflow: Op = ISD::USUBO; break;
5248 case Intrinsic::ssub_with_overflow: Op = ISD::SSUBO; break;
5249 case Intrinsic::umul_with_overflow: Op = ISD::UMULO; break;
5250 case Intrinsic::smul_with_overflow: Op = ISD::SMULO; break;
5251 }
5252 SDValue Op1 = getValue(I.getArgOperand(0));
5253 SDValue Op2 = getValue(I.getArgOperand(1));
Bill Wendling7cdc3c82008-11-21 02:03:52 +00005254
Craig Topperc42e6402012-04-11 04:34:11 +00005255 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005256 setValue(&I, DAG.getNode(Op, sdl, VTs, Op1, Op2));
Craig Topperc42e6402012-04-11 04:34:11 +00005257 return 0;
5258 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005259 case Intrinsic::prefetch: {
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00005260 SDValue Ops[5];
Dale Johannesen1de4aa92010-10-26 23:11:10 +00005261 unsigned rw = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005262 Ops[0] = getRoot();
Gabor Greif0635f352010-06-25 09:38:13 +00005263 Ops[1] = getValue(I.getArgOperand(0));
5264 Ops[2] = getValue(I.getArgOperand(1));
5265 Ops[3] = getValue(I.getArgOperand(2));
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00005266 Ops[4] = getValue(I.getArgOperand(3));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005267 DAG.setRoot(DAG.getMemIntrinsicNode(ISD::PREFETCH, sdl,
Dale Johannesen1de4aa92010-10-26 23:11:10 +00005268 DAG.getVTList(MVT::Other),
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00005269 &Ops[0], 5,
Dale Johannesen1de4aa92010-10-26 23:11:10 +00005270 EVT::getIntegerVT(*Context, 8),
5271 MachinePointerInfo(I.getArgOperand(0)),
5272 0, /* align */
5273 false, /* volatile */
5274 rw==0, /* read */
5275 rw==1)); /* write */
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005276 return 0;
5277 }
Duncan Sandsf07c9492009-11-10 09:08:09 +00005278 case Intrinsic::lifetime_start:
Nadav Rotemc05d3062012-09-06 09:17:37 +00005279 case Intrinsic::lifetime_end: {
Nadav Rotemc05d3062012-09-06 09:17:37 +00005280 bool IsStart = (Intrinsic == Intrinsic::lifetime_start);
Nadav Rotem9a2ae002012-09-10 08:43:23 +00005281 // Stack coloring is not enabled in O0, discard region information.
5282 if (TM.getOptLevel() == CodeGenOpt::None)
5283 return 0;
Nadav Rotemc05d3062012-09-06 09:17:37 +00005284
Nadav Rotem9a2ae002012-09-10 08:43:23 +00005285 SmallVector<Value *, 4> Allocas;
5286 GetUnderlyingObjects(I.getArgOperand(1), Allocas, TD);
5287
Craig Topperf22fd3f2013-07-03 05:11:49 +00005288 for (SmallVectorImpl<Value*>::iterator Object = Allocas.begin(),
5289 E = Allocas.end(); Object != E; ++Object) {
Nadav Rotem9a2ae002012-09-10 08:43:23 +00005290 AllocaInst *LifetimeObject = dyn_cast_or_null<AllocaInst>(*Object);
5291
5292 // Could not find an Alloca.
5293 if (!LifetimeObject)
5294 continue;
5295
5296 int FI = FuncInfo.StaticAllocaMap[LifetimeObject];
5297
5298 SDValue Ops[2];
5299 Ops[0] = getRoot();
Bill Wendlingba54bca2013-06-19 21:36:55 +00005300 Ops[1] = DAG.getFrameIndex(FI, TLI->getPointerTy(), true);
Nadav Rotem9a2ae002012-09-10 08:43:23 +00005301 unsigned Opcode = (IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END);
5302
Andrew Trickac6d9be2013-05-25 02:42:55 +00005303 Res = DAG.getNode(Opcode, sdl, MVT::Other, Ops, 2);
Nadav Rotem9a2ae002012-09-10 08:43:23 +00005304 DAG.setRoot(Res);
5305 }
Nadav Rotem5882e562013-02-01 19:25:23 +00005306 return 0;
Nadav Rotemc05d3062012-09-06 09:17:37 +00005307 }
5308 case Intrinsic::invariant_start:
Duncan Sandsf07c9492009-11-10 09:08:09 +00005309 // Discard region information.
Bill Wendlingba54bca2013-06-19 21:36:55 +00005310 setValue(&I, DAG.getUNDEF(TLI->getPointerTy()));
Duncan Sandsf07c9492009-11-10 09:08:09 +00005311 return 0;
5312 case Intrinsic::invariant_end:
Duncan Sandsf07c9492009-11-10 09:08:09 +00005313 // Discard region information.
5314 return 0;
Michael Gottesman657484f2013-08-20 07:00:16 +00005315 case Intrinsic::stackprotectorcheck: {
5316 // Do not actually emit anything for this basic block. Instead we initialize
5317 // the stack protector descriptor and export the guard variable so we can
5318 // access it in FinishBasicBlock.
5319 const BasicBlock *BB = I.getParent();
5320 SPDescriptor.initialize(BB, FuncInfo.MBBMap[BB], I);
5321 ExportFromCurrentBlock(SPDescriptor.getGuard());
5322
5323 // Flush our exports since we are going to process a terminator.
5324 (void)getControlRoot();
5325 return 0;
5326 }
Nuno Lopes85b40892012-06-28 22:30:12 +00005327 case Intrinsic::donothing:
5328 // ignore
5329 return 0;
Andrew Trick2343e3b2013-10-31 17:18:24 +00005330 case Intrinsic::experimental_stackmap: {
5331 visitStackmap(I);
5332 return 0;
5333 }
5334 case Intrinsic::experimental_patchpoint_void:
5335 case Intrinsic::experimental_patchpoint_i64: {
5336 visitPatchpoint(I);
5337 return 0;
5338 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005339 }
5340}
5341
Dan Gohman46510a72010-04-15 01:51:59 +00005342void SelectionDAGBuilder::LowerCallTo(ImmutableCallSite CS, SDValue Callee,
Dan Gohman2048b852009-11-23 18:04:58 +00005343 bool isTailCall,
5344 MachineBasicBlock *LandingPad) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005345 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
5346 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
5347 Type *RetTy = FTy->getReturnType();
Chris Lattner512063d2010-04-05 06:19:28 +00005348 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Chris Lattner16112732010-03-14 01:41:15 +00005349 MCSymbol *BeginLabel = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005350
5351 TargetLowering::ArgListTy Args;
5352 TargetLowering::ArgListEntry Entry;
5353 Args.reserve(CS.arg_size());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005354
5355 // Check whether the function can return without sret-demotion.
Dan Gohman84023e02010-07-10 09:00:22 +00005356 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00005357 const TargetLowering *TLI = TM.getTargetLowering();
5358 GetReturnInfo(RetTy, CS.getAttributes(), Outs, *TLI);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005359
Bill Wendlingba54bca2013-06-19 21:36:55 +00005360 bool CanLowerReturn = TLI->CanLowerReturn(CS.getCallingConv(),
5361 DAG.getMachineFunction(),
5362 FTy->isVarArg(), Outs,
5363 FTy->getContext());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005364
5365 SDValue DemoteStackSlot;
Chris Lattnerecf42c42010-09-21 16:36:31 +00005366 int DemoteStackIdx = -100;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005367
5368 if (!CanLowerReturn) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00005369 uint64_t TySize = TLI->getDataLayout()->getTypeAllocSize(
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005370 FTy->getReturnType());
Bill Wendlingba54bca2013-06-19 21:36:55 +00005371 unsigned Align = TLI->getDataLayout()->getPrefTypeAlignment(
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005372 FTy->getReturnType());
5373 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattnerecf42c42010-09-21 16:36:31 +00005374 DemoteStackIdx = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005375 Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005376
Bill Wendlingba54bca2013-06-19 21:36:55 +00005377 DemoteStackSlot = DAG.getFrameIndex(DemoteStackIdx, TLI->getPointerTy());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005378 Entry.Node = DemoteStackSlot;
5379 Entry.Ty = StackSlotPtrType;
5380 Entry.isSExt = false;
5381 Entry.isZExt = false;
5382 Entry.isInReg = false;
5383 Entry.isSRet = true;
5384 Entry.isNest = false;
5385 Entry.isByVal = false;
Stephen Lin456ca042013-04-20 05:14:40 +00005386 Entry.isReturned = false;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005387 Entry.Alignment = Align;
5388 Args.push_back(Entry);
5389 RetTy = Type::getVoidTy(FTy->getContext());
5390 }
5391
Dan Gohman46510a72010-04-15 01:51:59 +00005392 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005393 i != e; ++i) {
Rafael Espindola3fa82832011-05-13 15:18:06 +00005394 const Value *V = *i;
5395
5396 // Skip empty types
5397 if (V->getType()->isEmptyTy())
5398 continue;
5399
5400 SDValue ArgNode = getValue(V);
5401 Entry.Node = ArgNode; Entry.Ty = V->getType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005402
Andrew Trick2343e3b2013-10-31 17:18:24 +00005403 // Skip the first return-type Attribute to get to params.
5404 Entry.setAttributes(&CS, i - CS.arg_begin() + 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005405 Args.push_back(Entry);
5406 }
5407
Chris Lattner512063d2010-04-05 06:19:28 +00005408 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005409 // Insert a label before the invoke call to mark the try range. This can be
5410 // used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00005411 BeginLabel = MMI.getContext().CreateTempSymbol();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00005412
Jim Grosbachca752c92010-01-28 01:45:32 +00005413 // For SjLj, keep track of which landing pads go with which invokes
5414 // so as to maintain the ordering of pads in the LSDA.
Chris Lattner512063d2010-04-05 06:19:28 +00005415 unsigned CallSiteIndex = MMI.getCurrentCallSite();
Jim Grosbachca752c92010-01-28 01:45:32 +00005416 if (CallSiteIndex) {
Chris Lattner512063d2010-04-05 06:19:28 +00005417 MMI.setCallSiteBeginLabel(BeginLabel, CallSiteIndex);
Bill Wendling30e67402011-10-05 22:24:35 +00005418 LPadToCallSiteMap[LandingPad].push_back(CallSiteIndex);
Bill Wendlinga8512ed2011-10-04 22:00:35 +00005419
Jim Grosbachca752c92010-01-28 01:45:32 +00005420 // Now that the call site is handled, stop tracking it.
Chris Lattner512063d2010-04-05 06:19:28 +00005421 MMI.setCurrentCallSite(0);
Jim Grosbachca752c92010-01-28 01:45:32 +00005422 }
5423
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005424 // Both PendingLoads and PendingExports must be flushed here;
5425 // this call might not return.
5426 (void)getRoot();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005427 DAG.setRoot(DAG.getEHLabel(getCurSDLoc(), getControlRoot(), BeginLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005428 }
5429
Dan Gohman98ca4f22009-08-05 01:29:28 +00005430 // Check if target-independent constraints permit a tail call here.
Bill Wendlingba54bca2013-06-19 21:36:55 +00005431 // Target-dependent constraints are checked within TLI->LowerCallTo.
5432 if (isTailCall && !isInTailCallPosition(CS, *TLI))
Dan Gohman98ca4f22009-08-05 01:29:28 +00005433 isTailCall = false;
5434
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00005435 TargetLowering::
5436 CallLoweringInfo CLI(getRoot(), RetTy, FTy, isTailCall, Callee, Args, DAG,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005437 getCurSDLoc(), CS);
Bill Wendlingba54bca2013-06-19 21:36:55 +00005438 std::pair<SDValue,SDValue> Result = TLI->LowerCallTo(CLI);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005439 assert((isTailCall || Result.second.getNode()) &&
5440 "Non-null chain expected with non-tail call!");
5441 assert((Result.second.getNode() || !Result.first.getNode()) &&
5442 "Null value expected with tail call!");
Bill Wendlinge80ae832009-12-22 00:50:32 +00005443 if (Result.first.getNode()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005444 setValue(CS.getInstruction(), Result.first);
Bill Wendlinge80ae832009-12-22 00:50:32 +00005445 } else if (!CanLowerReturn && Result.second.getNode()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005446 // The instruction result is the result of loading from the
5447 // hidden sret parameter.
5448 SmallVector<EVT, 1> PVTs;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005449 Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005450
Bill Wendlingba54bca2013-06-19 21:36:55 +00005451 ComputeValueVTs(*TLI, PtrRetTy, PVTs);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005452 assert(PVTs.size() == 1 && "Pointers should fit in one register");
5453 EVT PtrVT = PVTs[0];
Eli Friedman2db0e9e2012-05-25 00:09:29 +00005454
5455 SmallVector<EVT, 4> RetTys;
5456 SmallVector<uint64_t, 4> Offsets;
5457 RetTy = FTy->getReturnType();
Bill Wendlingba54bca2013-06-19 21:36:55 +00005458 ComputeValueVTs(*TLI, RetTy, RetTys, &Offsets);
Eli Friedman2db0e9e2012-05-25 00:09:29 +00005459
5460 unsigned NumValues = RetTys.size();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005461 SmallVector<SDValue, 4> Values(NumValues);
5462 SmallVector<SDValue, 4> Chains(NumValues);
5463
5464 for (unsigned i = 0; i < NumValues; ++i) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005465 SDValue Add = DAG.getNode(ISD::ADD, getCurSDLoc(), PtrVT,
Bill Wendlinge80ae832009-12-22 00:50:32 +00005466 DemoteStackSlot,
5467 DAG.getConstant(Offsets[i], PtrVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005468 SDValue L = DAG.getLoad(RetTys[i], getCurSDLoc(), Result.second, Add,
Chris Lattnerecf42c42010-09-21 16:36:31 +00005469 MachinePointerInfo::getFixedStack(DemoteStackIdx, Offsets[i]),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005470 false, false, false, 1);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005471 Values[i] = L;
5472 Chains[i] = L.getValue(1);
5473 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00005474
Andrew Trickac6d9be2013-05-25 02:42:55 +00005475 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005476 MVT::Other, &Chains[0], NumValues);
5477 PendingLoads.push_back(Chain);
Michael J. Spencere70c5262010-10-16 08:25:21 +00005478
Bill Wendling4533cac2010-01-28 21:51:40 +00005479 setValue(CS.getInstruction(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00005480 DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00005481 DAG.getVTList(&RetTys[0], RetTys.size()),
Eli Friedman2db0e9e2012-05-25 00:09:29 +00005482 &Values[0], Values.size()));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005483 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00005484
Evan Cheng8380c032011-04-01 19:42:22 +00005485 if (!Result.second.getNode()) {
Andrew Trick2343e3b2013-10-31 17:18:24 +00005486 // As a special case, a null chain means that a tail call has been emitted
5487 // and the DAG root is already updated.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005488 HasTailCall = true;
Tim Northovere5a81a12013-07-06 12:58:45 +00005489
5490 // Since there's no actual continuation from this block, nothing can be
5491 // relying on us setting vregs for them.
5492 PendingExports.clear();
Evan Cheng8380c032011-04-01 19:42:22 +00005493 } else {
5494 DAG.setRoot(Result.second);
Evan Cheng8380c032011-04-01 19:42:22 +00005495 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005496
Chris Lattner512063d2010-04-05 06:19:28 +00005497 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005498 // Insert a label at the end of the invoke call to mark the try range. This
5499 // can be used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00005500 MCSymbol *EndLabel = MMI.getContext().CreateTempSymbol();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005501 DAG.setRoot(DAG.getEHLabel(getCurSDLoc(), getRoot(), EndLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005502
5503 // Inform MachineModuleInfo of range.
Chris Lattner512063d2010-04-05 06:19:28 +00005504 MMI.addInvoke(LandingPad, BeginLabel, EndLabel);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005505 }
5506}
5507
Chris Lattner8047d9a2009-12-24 00:37:38 +00005508/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
5509/// value is equal or not-equal to zero.
Dan Gohman46510a72010-04-15 01:51:59 +00005510static bool IsOnlyUsedInZeroEqualityComparison(const Value *V) {
5511 for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end();
Chris Lattner8047d9a2009-12-24 00:37:38 +00005512 UI != E; ++UI) {
Dan Gohman46510a72010-04-15 01:51:59 +00005513 if (const ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
Chris Lattner8047d9a2009-12-24 00:37:38 +00005514 if (IC->isEquality())
Dan Gohman46510a72010-04-15 01:51:59 +00005515 if (const Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
Chris Lattner8047d9a2009-12-24 00:37:38 +00005516 if (C->isNullValue())
5517 continue;
5518 // Unknown instruction.
5519 return false;
5520 }
5521 return true;
5522}
5523
Dan Gohman46510a72010-04-15 01:51:59 +00005524static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005525 Type *LoadTy,
Chris Lattner8047d9a2009-12-24 00:37:38 +00005526 SelectionDAGBuilder &Builder) {
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005527
Chris Lattner8047d9a2009-12-24 00:37:38 +00005528 // Check to see if this load can be trivially constant folded, e.g. if the
5529 // input is from a string literal.
Dan Gohman46510a72010-04-15 01:51:59 +00005530 if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00005531 // Cast pointer to the type we really want to load.
Dan Gohman46510a72010-04-15 01:51:59 +00005532 LoadInput = ConstantExpr::getBitCast(const_cast<Constant *>(LoadInput),
Chris Lattner8047d9a2009-12-24 00:37:38 +00005533 PointerType::getUnqual(LoadTy));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005534
Dan Gohman46510a72010-04-15 01:51:59 +00005535 if (const Constant *LoadCst =
5536 ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput),
5537 Builder.TD))
Chris Lattner8047d9a2009-12-24 00:37:38 +00005538 return Builder.getValue(LoadCst);
5539 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005540
Chris Lattner8047d9a2009-12-24 00:37:38 +00005541 // Otherwise, we have to emit the load. If the pointer is to unfoldable but
5542 // still constant memory, the input chain can be the entry node.
5543 SDValue Root;
5544 bool ConstantMemory = false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005545
Chris Lattner8047d9a2009-12-24 00:37:38 +00005546 // Do not serialize (non-volatile) loads of constant memory with anything.
5547 if (Builder.AA->pointsToConstantMemory(PtrVal)) {
5548 Root = Builder.DAG.getEntryNode();
5549 ConstantMemory = true;
5550 } else {
5551 // Do not serialize non-volatile loads against each other.
5552 Root = Builder.DAG.getRoot();
5553 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005554
Chris Lattner8047d9a2009-12-24 00:37:38 +00005555 SDValue Ptr = Builder.getValue(PtrVal);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005556 SDValue LoadVal = Builder.DAG.getLoad(LoadVT, Builder.getCurSDLoc(), Root,
Chris Lattnerecf42c42010-09-21 16:36:31 +00005557 Ptr, MachinePointerInfo(PtrVal),
David Greene1e559442010-02-15 17:00:31 +00005558 false /*volatile*/,
Stephen Lin155615d2013-07-08 00:37:03 +00005559 false /*nontemporal*/,
Pete Cooperd752e0f2011-11-08 18:42:53 +00005560 false /*isinvariant*/, 1 /* align=1 */);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005561
Chris Lattner8047d9a2009-12-24 00:37:38 +00005562 if (!ConstantMemory)
5563 Builder.PendingLoads.push_back(LoadVal.getValue(1));
5564 return LoadVal;
5565}
5566
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005567/// processIntegerCallValue - Record the value for an instruction that
5568/// produces an integer result, converting the type where necessary.
5569void SelectionDAGBuilder::processIntegerCallValue(const Instruction &I,
5570 SDValue Value,
5571 bool IsSigned) {
5572 EVT VT = TM.getTargetLowering()->getValueType(I.getType(), true);
5573 if (IsSigned)
5574 Value = DAG.getSExtOrTrunc(Value, getCurSDLoc(), VT);
5575 else
5576 Value = DAG.getZExtOrTrunc(Value, getCurSDLoc(), VT);
5577 setValue(&I, Value);
5578}
Chris Lattner8047d9a2009-12-24 00:37:38 +00005579
5580/// visitMemCmpCall - See if we can lower a call to memcmp in an optimized form.
5581/// If so, return true and lower it, otherwise return false and it will be
5582/// lowered like a normal call.
Dan Gohman46510a72010-04-15 01:51:59 +00005583bool SelectionDAGBuilder::visitMemCmpCall(const CallInst &I) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00005584 // Verify that the prototype makes sense. int memcmp(void*,void*,size_t)
Gabor Greif37387d52010-06-30 12:55:46 +00005585 if (I.getNumArgOperands() != 3)
Chris Lattner8047d9a2009-12-24 00:37:38 +00005586 return false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005587
Gabor Greif0635f352010-06-25 09:38:13 +00005588 const Value *LHS = I.getArgOperand(0), *RHS = I.getArgOperand(1);
Duncan Sands1df98592010-02-16 11:11:14 +00005589 if (!LHS->getType()->isPointerTy() || !RHS->getType()->isPointerTy() ||
Gabor Greif0635f352010-06-25 09:38:13 +00005590 !I.getArgOperand(2)->getType()->isIntegerTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +00005591 !I.getType()->isIntegerTy())
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005592 return false;
5593
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005594 const Value *Size = I.getArgOperand(2);
5595 const ConstantInt *CSize = dyn_cast<ConstantInt>(Size);
5596 if (CSize && CSize->getZExtValue() == 0) {
Richard Sandifordac168b82013-08-12 10:28:10 +00005597 EVT CallVT = TM.getTargetLowering()->getValueType(I.getType(), true);
5598 setValue(&I, DAG.getConstant(0, CallVT));
5599 return true;
5600 }
5601
Richard Sandifordac168b82013-08-12 10:28:10 +00005602 const TargetSelectionDAGInfo &TSI = DAG.getSelectionDAGInfo();
5603 std::pair<SDValue, SDValue> Res =
5604 TSI.EmitTargetCodeForMemcmp(DAG, getCurSDLoc(), DAG.getRoot(),
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005605 getValue(LHS), getValue(RHS), getValue(Size),
5606 MachinePointerInfo(LHS),
5607 MachinePointerInfo(RHS));
Richard Sandifordac168b82013-08-12 10:28:10 +00005608 if (Res.first.getNode()) {
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005609 processIntegerCallValue(I, Res.first, true);
5610 PendingLoads.push_back(Res.second);
Richard Sandifordac168b82013-08-12 10:28:10 +00005611 return true;
5612 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005613
Chris Lattner8047d9a2009-12-24 00:37:38 +00005614 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
5615 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005616 if (CSize && IsOnlyUsedInZeroEqualityComparison(&I)) {
Chris Lattner04b091a2009-12-24 01:07:17 +00005617 bool ActuallyDoIt = true;
5618 MVT LoadVT;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005619 Type *LoadTy;
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005620 switch (CSize->getZExtValue()) {
Chris Lattner04b091a2009-12-24 01:07:17 +00005621 default:
5622 LoadVT = MVT::Other;
5623 LoadTy = 0;
5624 ActuallyDoIt = false;
5625 break;
5626 case 2:
5627 LoadVT = MVT::i16;
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005628 LoadTy = Type::getInt16Ty(CSize->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005629 break;
5630 case 4:
5631 LoadVT = MVT::i32;
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005632 LoadTy = Type::getInt32Ty(CSize->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005633 break;
5634 case 8:
5635 LoadVT = MVT::i64;
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005636 LoadTy = Type::getInt64Ty(CSize->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005637 break;
5638 /*
5639 case 16:
5640 LoadVT = MVT::v4i32;
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005641 LoadTy = Type::getInt32Ty(CSize->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005642 LoadTy = VectorType::get(LoadTy, 4);
5643 break;
5644 */
5645 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005646
Chris Lattner04b091a2009-12-24 01:07:17 +00005647 // This turns into unaligned loads. We only do this if the target natively
5648 // supports the MVT we'll be loading or if it is small enough (<= 4) that
5649 // we'll only produce a small number of byte loads.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005650
Chris Lattner04b091a2009-12-24 01:07:17 +00005651 // Require that we can find a legal MVT, and only do this if the target
5652 // supports unaligned loads of that type. Expanding into byte loads would
5653 // bloat the code.
Bill Wendlingba54bca2013-06-19 21:36:55 +00005654 const TargetLowering *TLI = TM.getTargetLowering();
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005655 if (ActuallyDoIt && CSize->getZExtValue() > 4) {
Chris Lattner04b091a2009-12-24 01:07:17 +00005656 // TODO: Handle 5 byte compare as 4-byte + 1 byte.
5657 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
Bill Wendlingba54bca2013-06-19 21:36:55 +00005658 if (!TLI->isTypeLegal(LoadVT) ||!TLI->allowsUnalignedMemoryAccesses(LoadVT))
Chris Lattner04b091a2009-12-24 01:07:17 +00005659 ActuallyDoIt = false;
5660 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005661
Chris Lattner04b091a2009-12-24 01:07:17 +00005662 if (ActuallyDoIt) {
5663 SDValue LHSVal = getMemCmpLoad(LHS, LoadVT, LoadTy, *this);
5664 SDValue RHSVal = getMemCmpLoad(RHS, LoadVT, LoadTy, *this);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005665
Andrew Trickac6d9be2013-05-25 02:42:55 +00005666 SDValue Res = DAG.getSetCC(getCurSDLoc(), MVT::i1, LHSVal, RHSVal,
Chris Lattner04b091a2009-12-24 01:07:17 +00005667 ISD::SETNE);
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005668 processIntegerCallValue(I, Res, false);
Chris Lattner04b091a2009-12-24 01:07:17 +00005669 return true;
5670 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00005671 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005672
5673
Chris Lattner8047d9a2009-12-24 00:37:38 +00005674 return false;
5675}
5676
Richard Sandiford8c201582013-08-20 09:38:48 +00005677/// visitMemChrCall -- See if we can lower a memchr call into an optimized
5678/// form. If so, return true and lower it, otherwise return false and it
5679/// will be lowered like a normal call.
5680bool SelectionDAGBuilder::visitMemChrCall(const CallInst &I) {
5681 // Verify that the prototype makes sense. void *memchr(void *, int, size_t)
5682 if (I.getNumArgOperands() != 3)
5683 return false;
5684
5685 const Value *Src = I.getArgOperand(0);
5686 const Value *Char = I.getArgOperand(1);
5687 const Value *Length = I.getArgOperand(2);
5688 if (!Src->getType()->isPointerTy() ||
5689 !Char->getType()->isIntegerTy() ||
5690 !Length->getType()->isIntegerTy() ||
5691 !I.getType()->isPointerTy())
5692 return false;
5693
5694 const TargetSelectionDAGInfo &TSI = DAG.getSelectionDAGInfo();
5695 std::pair<SDValue, SDValue> Res =
5696 TSI.EmitTargetCodeForMemchr(DAG, getCurSDLoc(), DAG.getRoot(),
5697 getValue(Src), getValue(Char), getValue(Length),
5698 MachinePointerInfo(Src));
5699 if (Res.first.getNode()) {
5700 setValue(&I, Res.first);
5701 PendingLoads.push_back(Res.second);
5702 return true;
5703 }
5704
5705 return false;
5706}
5707
Richard Sandiford4fc73552013-08-16 11:29:37 +00005708/// visitStrCpyCall -- See if we can lower a strcpy or stpcpy call into an
5709/// optimized form. If so, return true and lower it, otherwise return false
5710/// and it will be lowered like a normal call.
5711bool SelectionDAGBuilder::visitStrCpyCall(const CallInst &I, bool isStpcpy) {
5712 // Verify that the prototype makes sense. char *strcpy(char *, char *)
5713 if (I.getNumArgOperands() != 2)
5714 return false;
5715
5716 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
5717 if (!Arg0->getType()->isPointerTy() ||
5718 !Arg1->getType()->isPointerTy() ||
5719 !I.getType()->isPointerTy())
5720 return false;
5721
5722 const TargetSelectionDAGInfo &TSI = DAG.getSelectionDAGInfo();
5723 std::pair<SDValue, SDValue> Res =
5724 TSI.EmitTargetCodeForStrcpy(DAG, getCurSDLoc(), getRoot(),
5725 getValue(Arg0), getValue(Arg1),
5726 MachinePointerInfo(Arg0),
5727 MachinePointerInfo(Arg1), isStpcpy);
5728 if (Res.first.getNode()) {
5729 setValue(&I, Res.first);
5730 DAG.setRoot(Res.second);
5731 return true;
5732 }
5733
5734 return false;
5735}
5736
Richard Sandiforde1b2af72013-08-16 11:21:54 +00005737/// visitStrCmpCall - See if we can lower a call to strcmp in an optimized form.
5738/// If so, return true and lower it, otherwise return false and it will be
5739/// lowered like a normal call.
5740bool SelectionDAGBuilder::visitStrCmpCall(const CallInst &I) {
5741 // Verify that the prototype makes sense. int strcmp(void*,void*)
5742 if (I.getNumArgOperands() != 2)
5743 return false;
5744
5745 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
5746 if (!Arg0->getType()->isPointerTy() ||
5747 !Arg1->getType()->isPointerTy() ||
5748 !I.getType()->isIntegerTy())
5749 return false;
5750
5751 const TargetSelectionDAGInfo &TSI = DAG.getSelectionDAGInfo();
5752 std::pair<SDValue, SDValue> Res =
5753 TSI.EmitTargetCodeForStrcmp(DAG, getCurSDLoc(), DAG.getRoot(),
5754 getValue(Arg0), getValue(Arg1),
5755 MachinePointerInfo(Arg0),
5756 MachinePointerInfo(Arg1));
5757 if (Res.first.getNode()) {
5758 processIntegerCallValue(I, Res.first, true);
5759 PendingLoads.push_back(Res.second);
5760 return true;
5761 }
5762
5763 return false;
5764}
5765
Richard Sandiford19262ee2013-08-16 11:41:43 +00005766/// visitStrLenCall -- See if we can lower a strlen call into an optimized
5767/// form. If so, return true and lower it, otherwise return false and it
5768/// will be lowered like a normal call.
5769bool SelectionDAGBuilder::visitStrLenCall(const CallInst &I) {
5770 // Verify that the prototype makes sense. size_t strlen(char *)
5771 if (I.getNumArgOperands() != 1)
5772 return false;
5773
5774 const Value *Arg0 = I.getArgOperand(0);
5775 if (!Arg0->getType()->isPointerTy() || !I.getType()->isIntegerTy())
5776 return false;
5777
5778 const TargetSelectionDAGInfo &TSI = DAG.getSelectionDAGInfo();
5779 std::pair<SDValue, SDValue> Res =
5780 TSI.EmitTargetCodeForStrlen(DAG, getCurSDLoc(), DAG.getRoot(),
5781 getValue(Arg0), MachinePointerInfo(Arg0));
5782 if (Res.first.getNode()) {
5783 processIntegerCallValue(I, Res.first, false);
5784 PendingLoads.push_back(Res.second);
5785 return true;
5786 }
5787
5788 return false;
5789}
5790
5791/// visitStrNLenCall -- See if we can lower a strnlen call into an optimized
5792/// form. If so, return true and lower it, otherwise return false and it
5793/// will be lowered like a normal call.
5794bool SelectionDAGBuilder::visitStrNLenCall(const CallInst &I) {
5795 // Verify that the prototype makes sense. size_t strnlen(char *, size_t)
5796 if (I.getNumArgOperands() != 2)
5797 return false;
5798
5799 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
5800 if (!Arg0->getType()->isPointerTy() ||
5801 !Arg1->getType()->isIntegerTy() ||
5802 !I.getType()->isIntegerTy())
5803 return false;
5804
5805 const TargetSelectionDAGInfo &TSI = DAG.getSelectionDAGInfo();
5806 std::pair<SDValue, SDValue> Res =
5807 TSI.EmitTargetCodeForStrnlen(DAG, getCurSDLoc(), DAG.getRoot(),
5808 getValue(Arg0), getValue(Arg1),
5809 MachinePointerInfo(Arg0));
5810 if (Res.first.getNode()) {
5811 processIntegerCallValue(I, Res.first, false);
5812 PendingLoads.push_back(Res.second);
5813 return true;
5814 }
5815
5816 return false;
5817}
5818
Bob Wilson53624a22012-08-03 23:29:17 +00005819/// visitUnaryFloatCall - If a call instruction is a unary floating-point
5820/// operation (as expected), translate it to an SDNode with the specified opcode
5821/// and return true.
5822bool SelectionDAGBuilder::visitUnaryFloatCall(const CallInst &I,
5823 unsigned Opcode) {
5824 // Sanity check that it really is a unary floating-point call.
5825 if (I.getNumArgOperands() != 1 ||
5826 !I.getArgOperand(0)->getType()->isFloatingPointTy() ||
5827 I.getType() != I.getArgOperand(0)->getType() ||
5828 !I.onlyReadsMemory())
5829 return false;
5830
5831 SDValue Tmp = getValue(I.getArgOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005832 setValue(&I, DAG.getNode(Opcode, getCurSDLoc(), Tmp.getValueType(), Tmp));
Bob Wilson53624a22012-08-03 23:29:17 +00005833 return true;
5834}
Chris Lattner8047d9a2009-12-24 00:37:38 +00005835
Dan Gohman46510a72010-04-15 01:51:59 +00005836void SelectionDAGBuilder::visitCall(const CallInst &I) {
Chris Lattner598751e2010-07-05 05:36:21 +00005837 // Handle inline assembly differently.
5838 if (isa<InlineAsm>(I.getCalledValue())) {
5839 visitInlineAsm(&I);
5840 return;
5841 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00005842
Michael J. Spencer391b43b2010-10-21 20:49:23 +00005843 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Michael J. Spencerc9c137b2012-02-22 19:06:13 +00005844 ComputeUsesVAFloatArgument(I, &MMI);
Michael J. Spencer391b43b2010-10-21 20:49:23 +00005845
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005846 const char *RenameFn = 0;
5847 if (Function *F = I.getCalledFunction()) {
5848 if (F->isDeclaration()) {
Chris Lattner598751e2010-07-05 05:36:21 +00005849 if (const TargetIntrinsicInfo *II = TM.getIntrinsicInfo()) {
Dale Johannesen49de9822009-02-05 01:49:45 +00005850 if (unsigned IID = II->getIntrinsicID(F)) {
5851 RenameFn = visitIntrinsicCall(I, IID);
5852 if (!RenameFn)
5853 return;
5854 }
5855 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005856 if (unsigned IID = F->getIntrinsicID()) {
5857 RenameFn = visitIntrinsicCall(I, IID);
5858 if (!RenameFn)
5859 return;
5860 }
5861 }
5862
5863 // Check for well-known libc/libm calls. If the function is internal, it
5864 // can't be a library call.
Bob Wilson982dc842012-08-03 21:26:24 +00005865 LibFunc::Func Func;
5866 if (!F->hasLocalLinkage() && F->hasName() &&
5867 LibInfo->getLibFunc(F->getName(), Func) &&
5868 LibInfo->hasOptimizedCodeGen(Func)) {
5869 switch (Func) {
5870 default: break;
5871 case LibFunc::copysign:
5872 case LibFunc::copysignf:
5873 case LibFunc::copysignl:
Gabor Greif37387d52010-06-30 12:55:46 +00005874 if (I.getNumArgOperands() == 2 && // Basic sanity checks.
Gabor Greif0635f352010-06-25 09:38:13 +00005875 I.getArgOperand(0)->getType()->isFloatingPointTy() &&
5876 I.getType() == I.getArgOperand(0)->getType() &&
Bob Wilson53624a22012-08-03 23:29:17 +00005877 I.getType() == I.getArgOperand(1)->getType() &&
5878 I.onlyReadsMemory()) {
Gabor Greif0635f352010-06-25 09:38:13 +00005879 SDValue LHS = getValue(I.getArgOperand(0));
5880 SDValue RHS = getValue(I.getArgOperand(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005881 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurSDLoc(),
Bill Wendling0d580132009-12-23 01:28:19 +00005882 LHS.getValueType(), LHS, RHS));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005883 return;
5884 }
Bob Wilson982dc842012-08-03 21:26:24 +00005885 break;
5886 case LibFunc::fabs:
5887 case LibFunc::fabsf:
5888 case LibFunc::fabsl:
Bob Wilson53624a22012-08-03 23:29:17 +00005889 if (visitUnaryFloatCall(I, ISD::FABS))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005890 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005891 break;
5892 case LibFunc::sin:
5893 case LibFunc::sinf:
5894 case LibFunc::sinl:
Bob Wilson53624a22012-08-03 23:29:17 +00005895 if (visitUnaryFloatCall(I, ISD::FSIN))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005896 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005897 break;
5898 case LibFunc::cos:
5899 case LibFunc::cosf:
5900 case LibFunc::cosl:
Bob Wilson53624a22012-08-03 23:29:17 +00005901 if (visitUnaryFloatCall(I, ISD::FCOS))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005902 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005903 break;
5904 case LibFunc::sqrt:
5905 case LibFunc::sqrtf:
5906 case LibFunc::sqrtl:
Preston Gurdb704d232013-05-27 15:44:35 +00005907 case LibFunc::sqrt_finite:
5908 case LibFunc::sqrtf_finite:
5909 case LibFunc::sqrtl_finite:
Bob Wilson53624a22012-08-03 23:29:17 +00005910 if (visitUnaryFloatCall(I, ISD::FSQRT))
Dale Johannesen52fb79b2009-09-25 17:23:22 +00005911 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005912 break;
5913 case LibFunc::floor:
5914 case LibFunc::floorf:
5915 case LibFunc::floorl:
Bob Wilson53624a22012-08-03 23:29:17 +00005916 if (visitUnaryFloatCall(I, ISD::FFLOOR))
Owen Anderson4a4fdf32011-12-08 19:32:14 +00005917 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005918 break;
5919 case LibFunc::nearbyint:
5920 case LibFunc::nearbyintf:
5921 case LibFunc::nearbyintl:
Bob Wilson53624a22012-08-03 23:29:17 +00005922 if (visitUnaryFloatCall(I, ISD::FNEARBYINT))
Owen Anderson4a4fdf32011-12-08 19:32:14 +00005923 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005924 break;
5925 case LibFunc::ceil:
5926 case LibFunc::ceilf:
5927 case LibFunc::ceill:
Bob Wilson53624a22012-08-03 23:29:17 +00005928 if (visitUnaryFloatCall(I, ISD::FCEIL))
Owen Anderson4a4fdf32011-12-08 19:32:14 +00005929 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005930 break;
5931 case LibFunc::rint:
5932 case LibFunc::rintf:
5933 case LibFunc::rintl:
Bob Wilson53624a22012-08-03 23:29:17 +00005934 if (visitUnaryFloatCall(I, ISD::FRINT))
Owen Anderson4a4fdf32011-12-08 19:32:14 +00005935 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005936 break;
Hal Finkel41418d12013-08-07 22:49:12 +00005937 case LibFunc::round:
5938 case LibFunc::roundf:
5939 case LibFunc::roundl:
5940 if (visitUnaryFloatCall(I, ISD::FROUND))
5941 return;
5942 break;
Bob Wilson982dc842012-08-03 21:26:24 +00005943 case LibFunc::trunc:
5944 case LibFunc::truncf:
5945 case LibFunc::truncl:
Bob Wilson53624a22012-08-03 23:29:17 +00005946 if (visitUnaryFloatCall(I, ISD::FTRUNC))
Owen Anderson4a4fdf32011-12-08 19:32:14 +00005947 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005948 break;
5949 case LibFunc::log2:
5950 case LibFunc::log2f:
5951 case LibFunc::log2l:
Bob Wilson53624a22012-08-03 23:29:17 +00005952 if (visitUnaryFloatCall(I, ISD::FLOG2))
Owen Anderson4e0adfa2011-12-15 00:54:12 +00005953 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005954 break;
5955 case LibFunc::exp2:
5956 case LibFunc::exp2f:
5957 case LibFunc::exp2l:
Bob Wilson53624a22012-08-03 23:29:17 +00005958 if (visitUnaryFloatCall(I, ISD::FEXP2))
Owen Anderson4e0adfa2011-12-15 00:54:12 +00005959 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005960 break;
5961 case LibFunc::memcmp:
Chris Lattner8047d9a2009-12-24 00:37:38 +00005962 if (visitMemCmpCall(I))
5963 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005964 break;
Richard Sandiford8c201582013-08-20 09:38:48 +00005965 case LibFunc::memchr:
5966 if (visitMemChrCall(I))
5967 return;
5968 break;
Richard Sandiford4fc73552013-08-16 11:29:37 +00005969 case LibFunc::strcpy:
5970 if (visitStrCpyCall(I, false))
5971 return;
5972 break;
5973 case LibFunc::stpcpy:
5974 if (visitStrCpyCall(I, true))
5975 return;
5976 break;
Richard Sandiforde1b2af72013-08-16 11:21:54 +00005977 case LibFunc::strcmp:
5978 if (visitStrCmpCall(I))
5979 return;
5980 break;
Richard Sandiford19262ee2013-08-16 11:41:43 +00005981 case LibFunc::strlen:
5982 if (visitStrLenCall(I))
5983 return;
5984 break;
5985 case LibFunc::strnlen:
5986 if (visitStrNLenCall(I))
5987 return;
5988 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005989 }
5990 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005991 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00005992
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005993 SDValue Callee;
5994 if (!RenameFn)
Gabor Greif0635f352010-06-25 09:38:13 +00005995 Callee = getValue(I.getCalledValue());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005996 else
Bill Wendlingba54bca2013-06-19 21:36:55 +00005997 Callee = DAG.getExternalSymbol(RenameFn,
5998 TM.getTargetLowering()->getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005999
Bill Wendling0d580132009-12-23 01:28:19 +00006000 // Check if we can potentially perform a tail call. More detailed checking is
6001 // be done within LowerCallTo, after more information about the call is known.
Evan Cheng11e67932010-01-26 23:13:04 +00006002 LowerCallTo(&I, Callee, I.isTailCall());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006003}
6004
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00006005namespace {
Dan Gohman462f6b52010-05-29 17:53:24 +00006006
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006007/// AsmOperandInfo - This contains information for each constraint that we are
6008/// lowering.
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00006009class SDISelAsmOperandInfo : public TargetLowering::AsmOperandInfo {
Cedric Venetaff9c272009-02-14 16:06:42 +00006010public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006011 /// CallOperand - If this is the result output operand or a clobber
6012 /// this is null, otherwise it is the incoming operand to the CallInst.
6013 /// This gets modified as the asm is processed.
6014 SDValue CallOperand;
6015
6016 /// AssignedRegs - If this is a register or register class operand, this
6017 /// contains the set of register corresponding to the operand.
6018 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006019
John Thompsoneac6e1d2010-09-13 18:15:37 +00006020 explicit SDISelAsmOperandInfo(const TargetLowering::AsmOperandInfo &info)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006021 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
6022 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006023
Owen Andersone50ed302009-08-10 22:56:29 +00006024 /// getCallOperandValEVT - Return the EVT of the Value* that this operand
Chris Lattner81249c92008-10-17 17:05:25 +00006025 /// corresponds to. If there is no Value* for this operand, it returns
Owen Anderson825b72b2009-08-11 20:47:22 +00006026 /// MVT::Other.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00006027 EVT getCallOperandValEVT(LLVMContext &Context,
Owen Anderson1d0be152009-08-13 21:58:54 +00006028 const TargetLowering &TLI,
Micah Villmow3574eca2012-10-08 16:38:25 +00006029 const DataLayout *TD) const {
Owen Anderson825b72b2009-08-11 20:47:22 +00006030 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006031
Chris Lattner81249c92008-10-17 17:05:25 +00006032 if (isa<BasicBlock>(CallOperandVal))
6033 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006034
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006035 llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006036
Eric Christophercef81b72011-05-09 20:04:43 +00006037 // FIXME: code duplicated from TargetLowering::ParseConstraints().
Chris Lattner81249c92008-10-17 17:05:25 +00006038 // If this is an indirect operand, the operand is a pointer to the
6039 // accessed type.
Bob Wilsone261b0c2009-12-22 18:34:19 +00006040 if (isIndirect) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006041 llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
Bob Wilsone261b0c2009-12-22 18:34:19 +00006042 if (!PtrTy)
Chris Lattner75361b62010-04-07 22:58:41 +00006043 report_fatal_error("Indirect operand for inline asm not a pointer!");
Bob Wilsone261b0c2009-12-22 18:34:19 +00006044 OpTy = PtrTy->getElementType();
6045 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006046
Eric Christophercef81b72011-05-09 20:04:43 +00006047 // Look for vector wrapped in a struct. e.g. { <16 x i8> }.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006048 if (StructType *STy = dyn_cast<StructType>(OpTy))
Eric Christophercef81b72011-05-09 20:04:43 +00006049 if (STy->getNumElements() == 1)
6050 OpTy = STy->getElementType(0);
6051
Chris Lattner81249c92008-10-17 17:05:25 +00006052 // If OpTy is not a single value, it may be a struct/union that we
6053 // can tile with integers.
6054 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
6055 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
6056 switch (BitSize) {
6057 default: break;
6058 case 1:
6059 case 8:
6060 case 16:
6061 case 32:
6062 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00006063 case 128:
Owen Anderson1d0be152009-08-13 21:58:54 +00006064 OpTy = IntegerType::get(Context, BitSize);
Chris Lattner81249c92008-10-17 17:05:25 +00006065 break;
6066 }
6067 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006068
Chris Lattner81249c92008-10-17 17:05:25 +00006069 return TLI.getValueType(OpTy, true);
6070 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006071};
Dan Gohman462f6b52010-05-29 17:53:24 +00006072
John Thompson44ab89e2010-10-29 17:29:13 +00006073typedef SmallVector<SDISelAsmOperandInfo,16> SDISelAsmOperandInfoVector;
6074
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00006075} // end anonymous namespace
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006076
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006077/// GetRegistersForValue - Assign registers (virtual or physical) for the
6078/// specified operand. We prefer to assign virtual registers, to allow the
Bob Wilson266d9452009-12-17 05:07:36 +00006079/// register allocator to handle the assignment process. However, if the asm
6080/// uses features that we can't model on machineinstrs, we have SDISel do the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006081/// allocation. This produces generally horrible, but correct, code.
6082///
6083/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006084///
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00006085static void GetRegistersForValue(SelectionDAG &DAG,
6086 const TargetLowering &TLI,
Andrew Trickac6d9be2013-05-25 02:42:55 +00006087 SDLoc DL,
Benjamin Kramer8b93ff22012-02-24 14:01:17 +00006088 SDISelAsmOperandInfo &OpInfo) {
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00006089 LLVMContext &Context = *DAG.getContext();
Owen Anderson23b9b192009-08-12 00:36:31 +00006090
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006091 MachineFunction &MF = DAG.getMachineFunction();
6092 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006093
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006094 // If this is a constraint for a single physreg, or a constraint for a
6095 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006096 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006097 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
6098 OpInfo.ConstraintVT);
6099
6100 unsigned NumRegs = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00006101 if (OpInfo.ConstraintVT != MVT::Other) {
Chris Lattner01426e12008-10-21 00:45:36 +00006102 // If this is a FP input in an integer register (or visa versa) insert a bit
6103 // cast of the input value. More generally, handle any case where the input
6104 // value disagrees with the register class we plan to stick this in.
6105 if (OpInfo.Type == InlineAsm::isInput &&
6106 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00006107 // Try to convert to the first EVT that the reg class contains. If the
Chris Lattner01426e12008-10-21 00:45:36 +00006108 // types are identical size, use a bitcast to convert (e.g. two differing
6109 // vector types).
Patrik Hagglund8963fec2012-12-19 12:23:01 +00006110 MVT RegVT = *PhysReg.second->vt_begin();
Chris Lattner01426e12008-10-21 00:45:36 +00006111 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00006112 OpInfo.CallOperand = DAG.getNode(ISD::BITCAST, DL,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00006113 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00006114 OpInfo.ConstraintVT = RegVT;
6115 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
6116 // If the input is a FP value and we want it in FP registers, do a
6117 // bitcast to the corresponding integer type. This turns an f64 value
6118 // into i64, which can be passed with two i32 values on a 32-bit
6119 // machine.
Patrik Hagglund8963fec2012-12-19 12:23:01 +00006120 RegVT = MVT::getIntegerVT(OpInfo.ConstraintVT.getSizeInBits());
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00006121 OpInfo.CallOperand = DAG.getNode(ISD::BITCAST, DL,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00006122 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00006123 OpInfo.ConstraintVT = RegVT;
6124 }
6125 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006126
Owen Anderson23b9b192009-08-12 00:36:31 +00006127 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00006128 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006129
Patrik Hagglund8963fec2012-12-19 12:23:01 +00006130 MVT RegVT;
Owen Andersone50ed302009-08-10 22:56:29 +00006131 EVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006132
6133 // If this is a constraint for a specific physical register, like {r17},
6134 // assign it now.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00006135 if (unsigned AssignedReg = PhysReg.first) {
6136 const TargetRegisterClass *RC = PhysReg.second;
Owen Anderson825b72b2009-08-11 20:47:22 +00006137 if (OpInfo.ConstraintVT == MVT::Other)
Chris Lattnere2f7bf82009-03-24 15:27:37 +00006138 ValueVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006139
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006140 // Get the actual register value type. This is important, because the user
6141 // may have asked for (e.g.) the AX register in i32 type. We need to
6142 // remember that AX is actually i16 to get the right extension.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00006143 RegVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006144
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006145 // This is a explicit reference to a physical register.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00006146 Regs.push_back(AssignedReg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006147
6148 // If this is an expanded reference, add the rest of the regs to Regs.
6149 if (NumRegs != 1) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00006150 TargetRegisterClass::iterator I = RC->begin();
6151 for (; *I != AssignedReg; ++I)
6152 assert(I != RC->end() && "Didn't find reg!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006153
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006154 // Already added the first reg.
6155 --NumRegs; ++I;
6156 for (; NumRegs; --NumRegs, ++I) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00006157 assert(I != RC->end() && "Ran out of registers to allocate!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006158 Regs.push_back(*I);
6159 }
6160 }
Bill Wendling651ad132009-12-22 01:25:10 +00006161
Dan Gohman7451d3e2010-05-29 17:03:36 +00006162 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006163 return;
6164 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006165
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006166 // Otherwise, if this was a reference to an LLVM register class, create vregs
6167 // for this reference.
Chris Lattnerb3b44842009-03-24 15:25:07 +00006168 if (const TargetRegisterClass *RC = PhysReg.second) {
6169 RegVT = *RC->vt_begin();
Owen Anderson825b72b2009-08-11 20:47:22 +00006170 if (OpInfo.ConstraintVT == MVT::Other)
Evan Chengfb112882009-03-23 08:01:15 +00006171 ValueVT = RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006172
Evan Chengfb112882009-03-23 08:01:15 +00006173 // Create the appropriate number of virtual registers.
6174 MachineRegisterInfo &RegInfo = MF.getRegInfo();
6175 for (; NumRegs; --NumRegs)
Chris Lattnerb3b44842009-03-24 15:25:07 +00006176 Regs.push_back(RegInfo.createVirtualRegister(RC));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006177
Dan Gohman7451d3e2010-05-29 17:03:36 +00006178 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
Evan Chengfb112882009-03-23 08:01:15 +00006179 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006180 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00006181
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006182 // Otherwise, we couldn't allocate enough registers for this.
6183}
6184
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006185/// visitInlineAsm - Handle a call to an InlineAsm object.
6186///
Dan Gohman46510a72010-04-15 01:51:59 +00006187void SelectionDAGBuilder::visitInlineAsm(ImmutableCallSite CS) {
6188 const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006189
6190 /// ConstraintOperands - Information about all of the constraints.
John Thompson44ab89e2010-10-29 17:29:13 +00006191 SDISelAsmOperandInfoVector ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006192
Bill Wendlingba54bca2013-06-19 21:36:55 +00006193 const TargetLowering *TLI = TM.getTargetLowering();
Evan Chengce1cdac2011-05-06 20:52:23 +00006194 TargetLowering::AsmOperandInfoVector
Bill Wendlingba54bca2013-06-19 21:36:55 +00006195 TargetConstraints = TLI->ParseConstraints(CS);
Evan Chengce1cdac2011-05-06 20:52:23 +00006196
John Thompsoneac6e1d2010-09-13 18:15:37 +00006197 bool hasMemory = false;
Michael J. Spencere70c5262010-10-16 08:25:21 +00006198
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006199 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
6200 unsigned ResNo = 0; // ResNo - The result number of the next output.
John Thompsoneac6e1d2010-09-13 18:15:37 +00006201 for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) {
6202 ConstraintOperands.push_back(SDISelAsmOperandInfo(TargetConstraints[i]));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006203 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Michael J. Spencere70c5262010-10-16 08:25:21 +00006204
Patrik Hagglundc698d3a2012-12-19 15:19:11 +00006205 MVT OpVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006206
6207 // Compute the value type for each operand.
6208 switch (OpInfo.Type) {
6209 case InlineAsm::isOutput:
6210 // Indirect outputs just consume an argument.
6211 if (OpInfo.isIndirect) {
Dan Gohman46510a72010-04-15 01:51:59 +00006212 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006213 break;
6214 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006215
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006216 // The return value of the call is this value. As such, there is no
6217 // corresponding argument.
Nick Lewycky8de34002011-09-30 22:19:53 +00006218 assert(!CS.getType()->isVoidTy() && "Bad inline asm!");
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006219 if (StructType *STy = dyn_cast<StructType>(CS.getType())) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00006220 OpVT = TLI->getSimpleValueType(STy->getElementType(ResNo));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006221 } else {
6222 assert(ResNo == 0 && "Asm only has one result!");
Bill Wendlingba54bca2013-06-19 21:36:55 +00006223 OpVT = TLI->getSimpleValueType(CS.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006224 }
6225 ++ResNo;
6226 break;
6227 case InlineAsm::isInput:
Dan Gohman46510a72010-04-15 01:51:59 +00006228 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006229 break;
6230 case InlineAsm::isClobber:
6231 // Nothing to do.
6232 break;
6233 }
6234
6235 // If this is an input or an indirect output, process the call argument.
6236 // BasicBlocks are labels, currently appearing only in asm's.
6237 if (OpInfo.CallOperandVal) {
Dan Gohman46510a72010-04-15 01:51:59 +00006238 if (const BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006239 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00006240 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006241 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006242 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006243
Bill Wendlingba54bca2013-06-19 21:36:55 +00006244 OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), *TLI, TD).
Patrik Hagglundc698d3a2012-12-19 15:19:11 +00006245 getSimpleVT();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006246 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006247
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006248 OpInfo.ConstraintVT = OpVT;
Michael J. Spencere70c5262010-10-16 08:25:21 +00006249
John Thompsoneac6e1d2010-09-13 18:15:37 +00006250 // Indirect operand accesses access memory.
6251 if (OpInfo.isIndirect)
6252 hasMemory = true;
6253 else {
6254 for (unsigned j = 0, ee = OpInfo.Codes.size(); j != ee; ++j) {
Evan Chengce1cdac2011-05-06 20:52:23 +00006255 TargetLowering::ConstraintType
Bill Wendlingba54bca2013-06-19 21:36:55 +00006256 CType = TLI->getConstraintType(OpInfo.Codes[j]);
John Thompsoneac6e1d2010-09-13 18:15:37 +00006257 if (CType == TargetLowering::C_Memory) {
6258 hasMemory = true;
6259 break;
6260 }
6261 }
6262 }
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006263 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006264
John Thompsoneac6e1d2010-09-13 18:15:37 +00006265 SDValue Chain, Flag;
6266
6267 // We won't need to flush pending loads if this asm doesn't touch
6268 // memory and is nonvolatile.
6269 if (hasMemory || IA->hasSideEffects())
6270 Chain = getRoot();
6271 else
6272 Chain = DAG.getRoot();
6273
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006274 // Second pass over the constraints: compute which constraint option to use
6275 // and assign registers to constraints that want a specific physreg.
John Thompsoneac6e1d2010-09-13 18:15:37 +00006276 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006277 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006278
John Thompson54584742010-09-24 22:24:05 +00006279 // If this is an output operand with a matching input operand, look up the
6280 // matching input. If their types mismatch, e.g. one is an integer, the
6281 // other is floating point, or their sizes are different, flag it as an
6282 // error.
6283 if (OpInfo.hasMatchingInput()) {
6284 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
Michael J. Spencere70c5262010-10-16 08:25:21 +00006285
John Thompson54584742010-09-24 22:24:05 +00006286 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Bill Wendling96cb1122012-07-19 00:04:14 +00006287 std::pair<unsigned, const TargetRegisterClass*> MatchRC =
Bill Wendlingba54bca2013-06-19 21:36:55 +00006288 TLI->getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
6289 OpInfo.ConstraintVT);
Bill Wendling96cb1122012-07-19 00:04:14 +00006290 std::pair<unsigned, const TargetRegisterClass*> InputRC =
Bill Wendlingba54bca2013-06-19 21:36:55 +00006291 TLI->getRegForInlineAsmConstraint(Input.ConstraintCode,
6292 Input.ConstraintVT);
John Thompson54584742010-09-24 22:24:05 +00006293 if ((OpInfo.ConstraintVT.isInteger() !=
6294 Input.ConstraintVT.isInteger()) ||
Eric Christopher5427ede2011-07-14 20:13:52 +00006295 (MatchRC.second != InputRC.second)) {
John Thompson54584742010-09-24 22:24:05 +00006296 report_fatal_error("Unsupported asm: input constraint"
6297 " with a matching output constraint of"
6298 " incompatible type!");
6299 }
6300 Input.ConstraintVT = OpInfo.ConstraintVT;
6301 }
6302 }
6303
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006304 // Compute the constraint code and ConstraintType to use.
Bill Wendlingba54bca2013-06-19 21:36:55 +00006305 TLI->ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006306
Eric Christopherfffe3632013-01-11 18:12:39 +00006307 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
6308 OpInfo.Type == InlineAsm::isClobber)
6309 continue;
6310
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006311 // If this is a memory input, and if the operand is not indirect, do what we
6312 // need to to provide an address for the memory input.
6313 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
6314 !OpInfo.isIndirect) {
Evan Chengce1cdac2011-05-06 20:52:23 +00006315 assert((OpInfo.isMultipleAlternative ||
6316 (OpInfo.Type == InlineAsm::isInput)) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006317 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006318
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006319 // Memory operands really want the address of the value. If we don't have
6320 // an indirect input, put it in the constpool if we can, otherwise spill
6321 // it to a stack slot.
Eric Christophere0b42c02011-06-03 17:21:23 +00006322 // TODO: This isn't quite right. We need to handle these according to
6323 // the addressing mode that the constraint wants. Also, this may take
6324 // an additional register for the computation and we don't want that
6325 // either.
Eric Christopher471e4222011-06-08 23:55:35 +00006326
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006327 // If the operand is a float, integer, or vector constant, spill to a
6328 // constant pool entry to get its address.
Dan Gohman46510a72010-04-15 01:51:59 +00006329 const Value *OpVal = OpInfo.CallOperandVal;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006330 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
Chris Lattnera78fa8c2012-01-27 03:08:05 +00006331 isa<ConstantVector>(OpVal) || isa<ConstantDataVector>(OpVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006332 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
Bill Wendlingba54bca2013-06-19 21:36:55 +00006333 TLI->getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006334 } else {
6335 // Otherwise, create a stack slot and emit a store to it before the
6336 // asm.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006337 Type *Ty = OpVal->getType();
Bill Wendlingba54bca2013-06-19 21:36:55 +00006338 uint64_t TySize = TLI->getDataLayout()->getTypeAllocSize(Ty);
6339 unsigned Align = TLI->getDataLayout()->getPrefTypeAlignment(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006340 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006341 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Bill Wendlingba54bca2013-06-19 21:36:55 +00006342 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI->getPointerTy());
Andrew Trickac6d9be2013-05-25 02:42:55 +00006343 Chain = DAG.getStore(Chain, getCurSDLoc(),
Chris Lattnerecf42c42010-09-21 16:36:31 +00006344 OpInfo.CallOperand, StackSlot,
6345 MachinePointerInfo::getFixedStack(SSFI),
David Greene1e559442010-02-15 17:00:31 +00006346 false, false, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006347 OpInfo.CallOperand = StackSlot;
6348 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006349
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006350 // There is no longer a Value* corresponding to this operand.
6351 OpInfo.CallOperandVal = 0;
Bill Wendling651ad132009-12-22 01:25:10 +00006352
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006353 // It is now an indirect operand.
6354 OpInfo.isIndirect = true;
6355 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006356
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006357 // If this constraint is for a specific register, allocate it before
6358 // anything else.
6359 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Bill Wendlingba54bca2013-06-19 21:36:55 +00006360 GetRegistersForValue(DAG, *TLI, getCurSDLoc(), OpInfo);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006361 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006362
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006363 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00006364 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006365 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
6366 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006367
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006368 // C_Register operands have already been allocated, Other/Memory don't need
6369 // to be.
6370 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Bill Wendlingba54bca2013-06-19 21:36:55 +00006371 GetRegistersForValue(DAG, *TLI, getCurSDLoc(), OpInfo);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006372 }
6373
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006374 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
6375 std::vector<SDValue> AsmNodeOperands;
6376 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
6377 AsmNodeOperands.push_back(
Dan Gohmanf2d7fb32010-01-04 21:00:54 +00006378 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00006379 TLI->getPointerTy()));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006380
Chris Lattnerdecc2672010-04-07 05:20:54 +00006381 // If we have a !srcloc metadata node associated with it, we want to attach
6382 // this to the ultimately generated inline asm machineinstr. To do this, we
6383 // pass in the third operand as this (potentially null) inline asm MDNode.
6384 const MDNode *SrcLoc = CS.getInstruction()->getMetadata("srcloc");
6385 AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006386
Chad Rosier3d716882012-10-30 19:11:54 +00006387 // Remember the HasSideEffect, AlignStack, AsmDialect, MayLoad and MayStore
6388 // bits as operand 3.
Evan Chengc36b7062011-01-07 23:50:32 +00006389 unsigned ExtraInfo = 0;
6390 if (IA->hasSideEffects())
6391 ExtraInfo |= InlineAsm::Extra_HasSideEffects;
6392 if (IA->isAlignStack())
6393 ExtraInfo |= InlineAsm::Extra_IsAlignStack;
Chad Rosier77fffa62012-09-05 22:17:43 +00006394 // Set the asm dialect.
Chad Rosier2f1d8152012-09-05 22:40:13 +00006395 ExtraInfo |= IA->getDialect() * InlineAsm::Extra_AsmDialect;
Chad Rosier3d716882012-10-30 19:11:54 +00006396
6397 // Determine if this InlineAsm MayLoad or MayStore based on the constraints.
6398 for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) {
6399 TargetLowering::AsmOperandInfo &OpInfo = TargetConstraints[i];
6400
6401 // Compute the constraint code and ConstraintType to use.
Bill Wendlingba54bca2013-06-19 21:36:55 +00006402 TLI->ComputeConstraintToUse(OpInfo, SDValue());
Chad Rosier3d716882012-10-30 19:11:54 +00006403
Chad Rosierdfa4cec2012-10-30 20:01:12 +00006404 // Ideally, we would only check against memory constraints. However, the
6405 // meaning of an other constraint can be target-specific and we can't easily
6406 // reason about it. Therefore, be conservative and set MayLoad/MayStore
6407 // for other constriants as well.
Chad Rosier3d716882012-10-30 19:11:54 +00006408 if (OpInfo.ConstraintType == TargetLowering::C_Memory ||
6409 OpInfo.ConstraintType == TargetLowering::C_Other) {
6410 if (OpInfo.Type == InlineAsm::isInput)
6411 ExtraInfo |= InlineAsm::Extra_MayLoad;
6412 else if (OpInfo.Type == InlineAsm::isOutput)
6413 ExtraInfo |= InlineAsm::Extra_MayStore;
Eric Christopherfffe3632013-01-11 18:12:39 +00006414 else if (OpInfo.Type == InlineAsm::isClobber)
6415 ExtraInfo |= (InlineAsm::Extra_MayLoad | InlineAsm::Extra_MayStore);
Chad Rosier3d716882012-10-30 19:11:54 +00006416 }
6417 }
6418
Evan Chengc36b7062011-01-07 23:50:32 +00006419 AsmNodeOperands.push_back(DAG.getTargetConstant(ExtraInfo,
Bill Wendlingba54bca2013-06-19 21:36:55 +00006420 TLI->getPointerTy()));
Dale Johannesenf1e309e2010-07-02 20:16:09 +00006421
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006422 // Loop over all of the inputs, copying the operand values into the
6423 // appropriate registers and processing the output regs.
6424 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006425
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006426 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
6427 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006428
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006429 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
6430 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
6431
6432 switch (OpInfo.Type) {
6433 case InlineAsm::isOutput: {
6434 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
6435 OpInfo.ConstraintType != TargetLowering::C_Register) {
6436 // Memory output, or 'other' output (e.g. 'X' constraint).
6437 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
6438
6439 // Add information to the INLINEASM node to know about this output.
Chris Lattnerdecc2672010-04-07 05:20:54 +00006440 unsigned OpFlags = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
6441 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags,
Bill Wendlingba54bca2013-06-19 21:36:55 +00006442 TLI->getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006443 AsmNodeOperands.push_back(OpInfo.CallOperand);
6444 break;
6445 }
6446
6447 // Otherwise, this is a register or register class output.
6448
6449 // Copy the output from the appropriate register. Find a register that
6450 // we can use.
Chris Lattnerfcd70902012-01-03 23:51:01 +00006451 if (OpInfo.AssignedRegs.Regs.empty()) {
6452 LLVMContext &Ctx = *DAG.getContext();
Stephen Lin155615d2013-07-08 00:37:03 +00006453 Ctx.emitError(CS.getInstruction(),
Chris Lattnerfcd70902012-01-03 23:51:01 +00006454 "couldn't allocate output register for constraint '" +
Eric Christopher1a54c572013-07-31 01:26:24 +00006455 Twine(OpInfo.ConstraintCode) + "'");
6456 return;
Chris Lattnerfcd70902012-01-03 23:51:01 +00006457 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006458
6459 // If this is an indirect operand, store through the pointer after the
6460 // asm.
6461 if (OpInfo.isIndirect) {
6462 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
6463 OpInfo.CallOperandVal));
6464 } else {
6465 // This is the result value of the call.
Benjamin Kramerf0127052010-01-05 13:12:22 +00006466 assert(!CS.getType()->isVoidTy() && "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006467 // Concatenate this output onto the outputs list.
6468 RetValRegs.append(OpInfo.AssignedRegs);
6469 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006470
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006471 // Add information to the INLINEASM node to know that this register is
6472 // set.
Eric Christopherb0bee812013-07-30 22:50:44 +00006473 OpInfo.AssignedRegs
6474 .AddInlineAsmOperands(OpInfo.isEarlyClobber
6475 ? InlineAsm::Kind_RegDefEarlyClobber
6476 : InlineAsm::Kind_RegDef,
6477 false, 0, DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006478 break;
6479 }
6480 case InlineAsm::isInput: {
6481 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006482
Chris Lattner6bdcda32008-10-17 16:47:46 +00006483 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006484 // If this is required to match an output register we have already set,
6485 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00006486 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006487
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006488 // Scan until we find the definition we already emitted of this operand.
6489 // When we find it, create a RegsForValue operand.
Chris Lattnerdecc2672010-04-07 05:20:54 +00006490 unsigned CurOp = InlineAsm::Op_FirstOperand;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006491 for (; OperandNo; --OperandNo) {
6492 // Advance to the next operand.
Evan Cheng697cbbf2009-03-20 18:03:34 +00006493 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006494 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00006495 assert((InlineAsm::isRegDefKind(OpFlag) ||
6496 InlineAsm::isRegDefEarlyClobberKind(OpFlag) ||
6497 InlineAsm::isMemKind(OpFlag)) && "Skipped past definitions?");
Evan Cheng697cbbf2009-03-20 18:03:34 +00006498 CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006499 }
6500
Evan Cheng697cbbf2009-03-20 18:03:34 +00006501 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006502 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00006503 if (InlineAsm::isRegDefKind(OpFlag) ||
6504 InlineAsm::isRegDefEarlyClobberKind(OpFlag)) {
Evan Cheng697cbbf2009-03-20 18:03:34 +00006505 // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
Chris Lattner6129c372010-04-08 00:09:16 +00006506 if (OpInfo.isIndirect) {
6507 // This happens on gcc/testsuite/gcc.dg/pr8788-1.c
Dan Gohman99be8ae2010-04-19 22:41:47 +00006508 LLVMContext &Ctx = *DAG.getContext();
Eric Christopher1a54c572013-07-31 01:26:24 +00006509 Ctx.emitError(CS.getInstruction(), "inline asm not supported yet:"
6510 " don't know how to handle tied "
6511 "indirect register inputs");
6512 return;
Chris Lattner6129c372010-04-08 00:09:16 +00006513 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00006514
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006515 RegsForValue MatchedRegs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006516 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00006517 MVT RegVT = AsmNodeOperands[CurOp+1].getSimpleValueType();
Evan Chengfb112882009-03-23 08:01:15 +00006518 MatchedRegs.RegVTs.push_back(RegVT);
6519 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
Evan Cheng697cbbf2009-03-20 18:03:34 +00006520 for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag);
Chad Rosier2871ba92013-04-24 22:53:10 +00006521 i != e; ++i) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00006522 if (const TargetRegisterClass *RC = TLI->getRegClassFor(RegVT))
Chad Rosier2871ba92013-04-24 22:53:10 +00006523 MatchedRegs.Regs.push_back(RegInfo.createVirtualRegister(RC));
6524 else {
6525 LLVMContext &Ctx = *DAG.getContext();
Eric Christopher1a54c572013-07-31 01:26:24 +00006526 Ctx.emitError(CS.getInstruction(),
6527 "inline asm error: This value"
Chad Rosier2871ba92013-04-24 22:53:10 +00006528 " type register class is not natively supported!");
Eric Christopher1a54c572013-07-31 01:26:24 +00006529 return;
Chad Rosier2871ba92013-04-24 22:53:10 +00006530 }
6531 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006532 // Use the produced MatchedRegs object to
Andrew Trickac6d9be2013-05-25 02:42:55 +00006533 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurSDLoc(),
Bill Wendlingf18eb582012-09-26 06:16:18 +00006534 Chain, &Flag, CS.getInstruction());
Chris Lattnerdecc2672010-04-07 05:20:54 +00006535 MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse,
Evan Chengfb112882009-03-23 08:01:15 +00006536 true, OpInfo.getMatchedOperand(),
Bill Wendling46ada192010-03-02 01:55:18 +00006537 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006538 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006539 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00006540
Chris Lattnerdecc2672010-04-07 05:20:54 +00006541 assert(InlineAsm::isMemKind(OpFlag) && "Unknown matching constraint!");
6542 assert(InlineAsm::getNumOperandRegisters(OpFlag) == 1 &&
6543 "Unexpected number of operands");
6544 // Add information to the INLINEASM node to know about this input.
6545 // See InlineAsm.h isUseOperandTiedToDef.
6546 OpFlag = InlineAsm::getFlagWordForMatchingOp(OpFlag,
6547 OpInfo.getMatchedOperand());
6548 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag,
Bill Wendlingba54bca2013-06-19 21:36:55 +00006549 TLI->getPointerTy()));
Chris Lattnerdecc2672010-04-07 05:20:54 +00006550 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
6551 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006552 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006553
Dale Johannesenb5611a62010-07-13 20:17:05 +00006554 // Treat indirect 'X' constraint as memory.
Michael J. Spencere70c5262010-10-16 08:25:21 +00006555 if (OpInfo.ConstraintType == TargetLowering::C_Other &&
6556 OpInfo.isIndirect)
Dale Johannesenb5611a62010-07-13 20:17:05 +00006557 OpInfo.ConstraintType = TargetLowering::C_Memory;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006558
Dale Johannesenb5611a62010-07-13 20:17:05 +00006559 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006560 std::vector<SDValue> Ops;
Bill Wendlingba54bca2013-06-19 21:36:55 +00006561 TLI->LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode,
6562 Ops, DAG);
Chris Lattnerfcd70902012-01-03 23:51:01 +00006563 if (Ops.empty()) {
6564 LLVMContext &Ctx = *DAG.getContext();
6565 Ctx.emitError(CS.getInstruction(),
6566 "invalid operand for inline asm constraint '" +
Eric Christopher1a54c572013-07-31 01:26:24 +00006567 Twine(OpInfo.ConstraintCode) + "'");
6568 return;
Chris Lattnerfcd70902012-01-03 23:51:01 +00006569 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006570
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006571 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00006572 unsigned ResOpType =
6573 InlineAsm::getFlagWord(InlineAsm::Kind_Imm, Ops.size());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006574 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Bill Wendlingba54bca2013-06-19 21:36:55 +00006575 TLI->getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006576 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
6577 break;
Chris Lattnerdecc2672010-04-07 05:20:54 +00006578 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00006579
Chris Lattnerdecc2672010-04-07 05:20:54 +00006580 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006581 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
Bill Wendlingba54bca2013-06-19 21:36:55 +00006582 assert(InOperandVal.getValueType() == TLI->getPointerTy() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006583 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006584
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006585 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00006586 unsigned ResOpType = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
Dale Johannesen86b49f82008-09-24 01:07:17 +00006587 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Bill Wendlingba54bca2013-06-19 21:36:55 +00006588 TLI->getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006589 AsmNodeOperands.push_back(InOperandVal);
6590 break;
6591 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006592
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006593 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
6594 OpInfo.ConstraintType == TargetLowering::C_Register) &&
6595 "Unknown constraint type!");
Eric Christopher9eb4f8a2012-07-02 21:16:43 +00006596
6597 // TODO: Support this.
6598 if (OpInfo.isIndirect) {
6599 LLVMContext &Ctx = *DAG.getContext();
6600 Ctx.emitError(CS.getInstruction(),
6601 "Don't know how to handle indirect register inputs yet "
Eric Christopher1a54c572013-07-31 01:26:24 +00006602 "for constraint '" +
6603 Twine(OpInfo.ConstraintCode) + "'");
6604 return;
Eric Christopher9eb4f8a2012-07-02 21:16:43 +00006605 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006606
6607 // Copy the input into the appropriate registers.
Chris Lattnerfcd70902012-01-03 23:51:01 +00006608 if (OpInfo.AssignedRegs.Regs.empty()) {
6609 LLVMContext &Ctx = *DAG.getContext();
Stephen Lin155615d2013-07-08 00:37:03 +00006610 Ctx.emitError(CS.getInstruction(),
Chris Lattnerfcd70902012-01-03 23:51:01 +00006611 "couldn't allocate input reg for constraint '" +
Eric Christopher1a54c572013-07-31 01:26:24 +00006612 Twine(OpInfo.ConstraintCode) + "'");
6613 return;
Chris Lattnerfcd70902012-01-03 23:51:01 +00006614 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006615
Andrew Trickac6d9be2013-05-25 02:42:55 +00006616 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurSDLoc(),
Bill Wendlingf18eb582012-09-26 06:16:18 +00006617 Chain, &Flag, CS.getInstruction());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006618
Chris Lattnerdecc2672010-04-07 05:20:54 +00006619 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse, false, 0,
Bill Wendling46ada192010-03-02 01:55:18 +00006620 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006621 break;
6622 }
6623 case InlineAsm::isClobber: {
6624 // Add the clobbered value to the operand list, so that the register
6625 // allocator is aware that the physreg got clobbered.
6626 if (!OpInfo.AssignedRegs.Regs.empty())
Jakob Stoklund Olesenf792fa92011-06-27 04:08:33 +00006627 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_Clobber,
Bill Wendling46ada192010-03-02 01:55:18 +00006628 false, 0, DAG,
Bill Wendling651ad132009-12-22 01:25:10 +00006629 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006630 break;
6631 }
6632 }
6633 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006634
Chris Lattnerdecc2672010-04-07 05:20:54 +00006635 // Finish up input operands. Set the input chain and add the flag last.
Dale Johannesenf1e309e2010-07-02 20:16:09 +00006636 AsmNodeOperands[InlineAsm::Op_InputChain] = Chain;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006637 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006638
Andrew Trickac6d9be2013-05-25 02:42:55 +00006639 Chain = DAG.getNode(ISD::INLINEASM, getCurSDLoc(),
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006640 DAG.getVTList(MVT::Other, MVT::Glue),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006641 &AsmNodeOperands[0], AsmNodeOperands.size());
6642 Flag = Chain.getValue(1);
6643
6644 // If this asm returns a register value, copy the result from that register
6645 // and set it as the value of the call.
6646 if (!RetValRegs.Regs.empty()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006647 SDValue Val = RetValRegs.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(),
Bill Wendling12931302012-09-26 04:04:19 +00006648 Chain, &Flag, CS.getInstruction());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006649
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006650 // FIXME: Why don't we do this for inline asms with MRVs?
6651 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00006652 EVT ResultType = TLI->getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006653
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006654 // If any of the results of the inline asm is a vector, it may have the
6655 // wrong width/num elts. This can happen for register classes that can
6656 // contain multiple different value types. The preg or vreg allocated may
6657 // not have the same VT as was expected. Convert it to the right type
6658 // with bit_convert.
6659 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006660 Val = DAG.getNode(ISD::BITCAST, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00006661 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00006662
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006663 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006664 ResultType.isInteger() && Val.getValueType().isInteger()) {
6665 // If a result value was tied to an input value, the computed result may
6666 // have a wider width than the expected result. Extract the relevant
6667 // portion.
Andrew Trickac6d9be2013-05-25 02:42:55 +00006668 Val = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00006669 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006670
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006671 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Chris Lattner0c526442008-10-17 17:52:49 +00006672 }
Dan Gohman95915732008-10-18 01:03:45 +00006673
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006674 setValue(CS.getInstruction(), Val);
Dale Johannesenec65a7d2009-04-14 00:56:56 +00006675 // Don't need to use this as a chain in this case.
6676 if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty())
6677 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006678 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006679
Dan Gohman46510a72010-04-15 01:51:59 +00006680 std::vector<std::pair<SDValue, const Value *> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006681
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006682 // Process indirect outputs, first output all of the flagged copies out of
6683 // physregs.
6684 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
6685 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
Dan Gohman46510a72010-04-15 01:51:59 +00006686 const Value *Ptr = IndirectStoresToEmit[i].second;
Andrew Trickac6d9be2013-05-25 02:42:55 +00006687 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(),
Bill Wendling12931302012-09-26 04:04:19 +00006688 Chain, &Flag, IA);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006689 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
6690 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006691
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006692 // Emit the non-flagged stores from the physregs.
6693 SmallVector<SDValue, 8> OutChains;
Bill Wendling651ad132009-12-22 01:25:10 +00006694 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006695 SDValue Val = DAG.getStore(Chain, getCurSDLoc(),
Bill Wendling651ad132009-12-22 01:25:10 +00006696 StoresToEmit[i].first,
6697 getValue(StoresToEmit[i].second),
Chris Lattner84bd98a2010-09-21 18:58:22 +00006698 MachinePointerInfo(StoresToEmit[i].second),
David Greene1e559442010-02-15 17:00:31 +00006699 false, false, 0);
Bill Wendling651ad132009-12-22 01:25:10 +00006700 OutChains.push_back(Val);
Bill Wendling651ad132009-12-22 01:25:10 +00006701 }
6702
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006703 if (!OutChains.empty())
Andrew Trickac6d9be2013-05-25 02:42:55 +00006704 Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006705 &OutChains[0], OutChains.size());
Bill Wendling651ad132009-12-22 01:25:10 +00006706
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006707 DAG.setRoot(Chain);
6708}
6709
Dan Gohman46510a72010-04-15 01:51:59 +00006710void SelectionDAGBuilder::visitVAStart(const CallInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006711 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurSDLoc(),
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006712 MVT::Other, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00006713 getValue(I.getArgOperand(0)),
6714 DAG.getSrcValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006715}
6716
Dan Gohman46510a72010-04-15 01:51:59 +00006717void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00006718 const TargetLowering *TLI = TM.getTargetLowering();
6719 const DataLayout &TD = *TLI->getDataLayout();
6720 SDValue V = DAG.getVAArg(TLI->getValueType(I.getType()), getCurSDLoc(),
Dale Johannesena04b7572009-02-03 23:04:43 +00006721 getRoot(), getValue(I.getOperand(0)),
Rafael Espindolacbeeae22010-07-11 04:01:49 +00006722 DAG.getSrcValue(I.getOperand(0)),
Rafael Espindola9d544d02010-07-12 18:11:17 +00006723 TD.getABITypeAlignment(I.getType()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006724 setValue(&I, V);
6725 DAG.setRoot(V.getValue(1));
6726}
6727
Dan Gohman46510a72010-04-15 01:51:59 +00006728void SelectionDAGBuilder::visitVAEnd(const CallInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006729 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurSDLoc(),
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006730 MVT::Other, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00006731 getValue(I.getArgOperand(0)),
6732 DAG.getSrcValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006733}
6734
Dan Gohman46510a72010-04-15 01:51:59 +00006735void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006736 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurSDLoc(),
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006737 MVT::Other, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00006738 getValue(I.getArgOperand(0)),
6739 getValue(I.getArgOperand(1)),
6740 DAG.getSrcValue(I.getArgOperand(0)),
6741 DAG.getSrcValue(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006742}
6743
Andrew Trick2343e3b2013-10-31 17:18:24 +00006744/// \brief Lower an argument list according to the target calling convention.
6745///
6746/// \return A tuple of <return-value, token-chain>
6747///
6748/// This is a helper for lowering intrinsics that follow a target calling
6749/// convention or require stack pointer adjustment. Only a subset of the
6750/// intrinsic's operands need to participate in the calling convention.
6751std::pair<SDValue, SDValue>
6752SelectionDAGBuilder::LowerCallOperands(const CallInst &CI, unsigned ArgIdx,
Juergen Ributzka623d2e62013-11-08 23:28:16 +00006753 unsigned NumArgs, SDValue Callee,
6754 bool useVoidTy) {
Andrew Trick2343e3b2013-10-31 17:18:24 +00006755 TargetLowering::ArgListTy Args;
6756 Args.reserve(NumArgs);
6757
6758 // Populate the argument list.
6759 // Attributes for args start at offset 1, after the return attribute.
6760 ImmutableCallSite CS(&CI);
6761 for (unsigned ArgI = ArgIdx, ArgE = ArgIdx + NumArgs, AttrI = ArgIdx + 1;
6762 ArgI != ArgE; ++ArgI) {
6763 const Value *V = CI.getOperand(ArgI);
6764
6765 assert(!V->getType()->isEmptyTy() && "Empty type passed to intrinsic.");
6766
6767 TargetLowering::ArgListEntry Entry;
6768 Entry.Node = getValue(V);
6769 Entry.Ty = V->getType();
6770 Entry.setAttributes(&CS, AttrI);
6771 Args.push_back(Entry);
6772 }
6773
Juergen Ributzka623d2e62013-11-08 23:28:16 +00006774 Type *retTy = useVoidTy ? Type::getVoidTy(*DAG.getContext()) : CI.getType();
6775 TargetLowering::CallLoweringInfo CLI(getRoot(), retTy, /*retSExt*/ false,
6776 /*retZExt*/ false, /*isVarArg*/ false, /*isInReg*/ false, NumArgs,
6777 CI.getCallingConv(), /*isTailCall*/ false, /*doesNotReturn*/ false,
Andrew Trick2343e3b2013-10-31 17:18:24 +00006778 /*isReturnValueUsed*/ CI.use_empty(), Callee, Args, DAG, getCurSDLoc());
6779
6780 const TargetLowering *TLI = TM.getTargetLowering();
6781 return TLI->LowerCallTo(CLI);
6782}
6783
6784/// \brief Lower llvm.experimental.stackmap directly to its target opcode.
6785void SelectionDAGBuilder::visitStackmap(const CallInst &CI) {
6786 // void @llvm.experimental.stackmap(i32 <id>, i32 <numShadowBytes>,
6787 // [live variables...])
6788
6789 assert(CI.getType()->isVoidTy() && "Stackmap cannot return a value.");
6790
6791 SDValue Callee = getValue(CI.getCalledValue());
6792
6793 // Lower into a call sequence with no args and no return value.
6794 std::pair<SDValue, SDValue> Result = LowerCallOperands(CI, 0, 0, Callee);
6795 // Set the root to the target-lowered call chain.
6796 SDValue Chain = Result.second;
6797 DAG.setRoot(Chain);
6798
6799 /// Get a call instruction from the call sequence chain.
6800 /// Tail calls are not allowed.
6801 SDNode *CallEnd = Chain.getNode();
6802 assert(CallEnd->getOpcode() == ISD::CALLSEQ_END &&
6803 "Expected a callseq node.");
6804 SDNode *Call = CallEnd->getOperand(0).getNode();
6805 bool hasGlue = Call->getGluedNode();
6806
Andrew Trick2343e3b2013-10-31 17:18:24 +00006807 // Replace the target specific call node with the stackmap intrinsic.
6808 SmallVector<SDValue, 8> Ops;
6809
6810 // Add the <id> and <numShadowBytes> constants.
6811 for (unsigned i = 0; i < 2; ++i) {
6812 SDValue tmp = getValue(CI.getOperand(i));
6813 Ops.push_back(DAG.getTargetConstant(
6814 cast<ConstantSDNode>(tmp)->getZExtValue(), MVT::i32));
6815 }
6816 // Push live variables for the stack map.
6817 for (unsigned i = 2, e = CI.getNumArgOperands(); i != e; ++i)
6818 Ops.push_back(getValue(CI.getArgOperand(i)));
6819
6820 // Push the chain (this is originally the first operand of the call, but
6821 // becomes now the last or second to last operand).
6822 Ops.push_back(*(Call->op_begin()));
6823
6824 // Push the glue flag (last operand).
6825 if (hasGlue)
6826 Ops.push_back(*(Call->op_end()-1));
6827
Andrew Trick2343e3b2013-10-31 17:18:24 +00006828 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trickdc8224d2013-11-05 22:44:04 +00006829
6830 // Replace the target specific call node with a STACKMAP node.
6831 MachineSDNode *MN = DAG.getMachineNode(TargetOpcode::STACKMAP, getCurSDLoc(),
6832 NodeTys, Ops);
6833
6834 // StackMap generates no value, so nothing goes in the NodeMap.
6835
6836 // Fixup the consumers of the intrinsic. The chain and glue may be used in the
6837 // call sequence.
6838 DAG.ReplaceAllUsesWith(Call, MN);
6839
6840 DAG.DeleteNode(Call);
Andrew Trick2343e3b2013-10-31 17:18:24 +00006841}
6842
6843/// \brief Lower llvm.experimental.patchpoint directly to its target opcode.
6844void SelectionDAGBuilder::visitPatchpoint(const CallInst &CI) {
6845 // void|i64 @llvm.experimental.patchpoint.void|i64(i32 <id>,
Andrew Trick72cf01c2013-11-14 06:54:10 +00006846 // i32 <numBytes>,
Juergen Ributzka623d2e62013-11-08 23:28:16 +00006847 // i8* <target>,
6848 // i32 <numArgs>,
6849 // [Args...],
6850 // [live variables...])
Andrew Trick2343e3b2013-10-31 17:18:24 +00006851
Juergen Ributzkad4f5a612013-11-09 01:51:33 +00006852 CallingConv::ID CC = CI.getCallingConv();
Juergen Ributzka623d2e62013-11-08 23:28:16 +00006853 bool isAnyRegCC = CC == CallingConv::AnyReg;
6854 bool hasDef = !CI.getType()->isVoidTy();
Andrew Trick2343e3b2013-10-31 17:18:24 +00006855 SDValue Callee = getValue(CI.getOperand(2)); // <target>
6856
6857 // Get the real number of arguments participating in the call <numArgs>
6858 unsigned NumArgs =
Juergen Ributzka623d2e62013-11-08 23:28:16 +00006859 cast<ConstantSDNode>(getValue(CI.getArgOperand(3)))->getZExtValue();
Andrew Trick2343e3b2013-10-31 17:18:24 +00006860
6861 // Skip the four meta args: <id>, <numNopBytes>, <target>, <numArgs>
6862 assert(CI.getNumArgOperands() >= NumArgs + 4 &&
6863 "Not enough arguments provided to the patchpoint intrinsic");
6864
Juergen Ributzka623d2e62013-11-08 23:28:16 +00006865 // For AnyRegCC the arguments are lowered later on manually.
6866 unsigned NumCallArgs = isAnyRegCC ? 0 : NumArgs;
Andrew Trick2343e3b2013-10-31 17:18:24 +00006867 std::pair<SDValue, SDValue> Result =
Juergen Ributzka623d2e62013-11-08 23:28:16 +00006868 LowerCallOperands(CI, 4, NumCallArgs, Callee, isAnyRegCC);
6869
Andrew Trick2343e3b2013-10-31 17:18:24 +00006870 // Set the root to the target-lowered call chain.
6871 SDValue Chain = Result.second;
6872 DAG.setRoot(Chain);
6873
6874 SDNode *CallEnd = Chain.getNode();
Juergen Ributzka623d2e62013-11-08 23:28:16 +00006875 if (hasDef && (CallEnd->getOpcode() == ISD::CopyFromReg))
6876 CallEnd = CallEnd->getOperand(0).getNode();
6877
Andrew Trick2343e3b2013-10-31 17:18:24 +00006878 /// Get a call instruction from the call sequence chain.
6879 /// Tail calls are not allowed.
6880 assert(CallEnd->getOpcode() == ISD::CALLSEQ_END &&
6881 "Expected a callseq node.");
6882 SDNode *Call = CallEnd->getOperand(0).getNode();
6883 bool hasGlue = Call->getGluedNode();
6884
6885 // Replace the target specific call node with the patchable intrinsic.
6886 SmallVector<SDValue, 8> Ops;
6887
6888 // Add the <id> and <numNopBytes> constants.
6889 for (unsigned i = 0; i < 2; ++i) {
6890 SDValue tmp = getValue(CI.getOperand(i));
6891 Ops.push_back(DAG.getTargetConstant(
6892 cast<ConstantSDNode>(tmp)->getZExtValue(), MVT::i32));
6893 }
6894 // Assume that the Callee is a constant address.
6895 Ops.push_back(
Juergen Ributzkad4f5a612013-11-09 01:51:33 +00006896 DAG.getIntPtrConstant(cast<ConstantSDNode>(Callee)->getZExtValue(),
6897 /*isTarget=*/true));
Andrew Trick2343e3b2013-10-31 17:18:24 +00006898
Juergen Ributzka623d2e62013-11-08 23:28:16 +00006899 // Adjust <numArgs> to account for any arguments that have been passed on the
6900 // stack instead.
Andrew Trick2343e3b2013-10-31 17:18:24 +00006901 // Call Node: Chain, Target, {Args}, RegMask, [Glue]
Juergen Ributzka623d2e62013-11-08 23:28:16 +00006902 unsigned NumCallRegArgs = Call->getNumOperands() - (hasGlue ? 4 : 3);
6903 NumCallRegArgs = isAnyRegCC ? NumArgs : NumCallRegArgs;
6904 Ops.push_back(DAG.getTargetConstant(NumCallRegArgs, MVT::i32));
6905
6906 // Add the calling convention
Juergen Ributzkad4f5a612013-11-09 01:51:33 +00006907 Ops.push_back(DAG.getTargetConstant((unsigned)CC, MVT::i32));
Juergen Ributzka623d2e62013-11-08 23:28:16 +00006908
6909 // Add the arguments we omitted previously. The register allocator should
6910 // place these in any free register.
6911 if (isAnyRegCC)
6912 for (unsigned i = 4, e = NumArgs + 4; i != e; ++i)
6913 Ops.push_back(getValue(CI.getArgOperand(i)));
Andrew Trick2343e3b2013-10-31 17:18:24 +00006914
6915 // Push the arguments from the call instruction.
6916 SDNode::op_iterator e = hasGlue ? Call->op_end()-2 : Call->op_end()-1;
6917 for (SDNode::op_iterator i = Call->op_begin()+2; i != e; ++i)
6918 Ops.push_back(*i);
6919
6920 // Push live variables for the stack map.
6921 for (unsigned i = NumArgs + 4, e = CI.getNumArgOperands(); i != e; ++i) {
6922 SDValue OpVal = getValue(CI.getArgOperand(i));
6923 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(OpVal)) {
6924 Ops.push_back(
Andrew Trick3d74dea2013-10-31 22:11:56 +00006925 DAG.getTargetConstant(StackMaps::ConstantOp, MVT::i64));
6926 Ops.push_back(
Andrew Trick2343e3b2013-10-31 17:18:24 +00006927 DAG.getTargetConstant(C->getSExtValue(), MVT::i64));
6928 } else
6929 Ops.push_back(OpVal);
6930 }
6931
6932 // Push the register mask info.
6933 if (hasGlue)
6934 Ops.push_back(*(Call->op_end()-2));
6935 else
6936 Ops.push_back(*(Call->op_end()-1));
6937
6938 // Push the chain (this is originally the first operand of the call, but
6939 // becomes now the last or second to last operand).
6940 Ops.push_back(*(Call->op_begin()));
6941
6942 // Push the glue flag (last operand).
6943 if (hasGlue)
6944 Ops.push_back(*(Call->op_end()-1));
6945
Juergen Ributzka623d2e62013-11-08 23:28:16 +00006946 SDVTList NodeTys;
6947 if (isAnyRegCC && hasDef) {
6948 // Create the return types based on the intrinsic definition
6949 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6950 SmallVector<EVT, 3> ValueVTs;
6951 ComputeValueVTs(TLI, CI.getType(), ValueVTs);
6952 assert(ValueVTs.size() == 1 && "Expected only one return value type.");
Andrew Trickdc8224d2013-11-05 22:44:04 +00006953
Juergen Ributzka623d2e62013-11-08 23:28:16 +00006954 // There is always a chain and a glue type at the end
6955 ValueVTs.push_back(MVT::Other);
6956 ValueVTs.push_back(MVT::Glue);
6957 NodeTys = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
6958 } else
6959 NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
6960
6961 // Replace the target specific call node with a PATCHPOINT node.
Andrew Trickdc8224d2013-11-05 22:44:04 +00006962 MachineSDNode *MN = DAG.getMachineNode(TargetOpcode::PATCHPOINT,
6963 getCurSDLoc(), NodeTys, Ops);
6964
Juergen Ributzka623d2e62013-11-08 23:28:16 +00006965 // Update the NodeMap.
6966 if (hasDef) {
6967 if (isAnyRegCC)
6968 setValue(&CI, SDValue(MN, 0));
6969 else
6970 setValue(&CI, Result.first);
6971 }
Andrew Trickdc8224d2013-11-05 22:44:04 +00006972
6973 // Fixup the consumers of the intrinsic. The chain and glue may be used in the
Juergen Ributzka623d2e62013-11-08 23:28:16 +00006974 // call sequence. Furthermore the location of the chain and glue can change
6975 // when the AnyReg calling convention is used and the intrinsic returns a
6976 // value.
6977 if (isAnyRegCC && hasDef) {
6978 SDValue From[] = {SDValue(Call, 0), SDValue(Call, 1)};
6979 SDValue To[] = {SDValue(MN, 1), SDValue(MN, 2)};
6980 DAG.ReplaceAllUsesOfValuesWith(From, To, 2);
6981 } else
6982 DAG.ReplaceAllUsesWith(Call, MN);
Andrew Trickdc8224d2013-11-05 22:44:04 +00006983 DAG.DeleteNode(Call);
Andrew Trick2343e3b2013-10-31 17:18:24 +00006984}
6985
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006986/// TargetLowering::LowerCallTo - This is the default LowerCallTo
Dan Gohman98ca4f22009-08-05 01:29:28 +00006987/// implementation, which just calls LowerCall.
6988/// FIXME: When all targets are
6989/// migrated to using LowerCall, this hook should be integrated into SDISel.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006990std::pair<SDValue, SDValue>
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006991TargetLowering::LowerCallTo(TargetLowering::CallLoweringInfo &CLI) const {
Stephen Lin3484da92013-04-30 22:49:28 +00006992 // Handle the incoming return values from the call.
6993 CLI.Ins.clear();
6994 SmallVector<EVT, 4> RetTys;
6995 ComputeValueVTs(*this, CLI.RetTy, RetTys);
6996 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
6997 EVT VT = RetTys[I];
6998 MVT RegisterVT = getRegisterType(CLI.RetTy->getContext(), VT);
6999 unsigned NumRegs = getNumRegisters(CLI.RetTy->getContext(), VT);
7000 for (unsigned i = 0; i != NumRegs; ++i) {
7001 ISD::InputArg MyFlags;
7002 MyFlags.VT = RegisterVT;
Tom Stellardd0716b02013-10-23 00:44:24 +00007003 MyFlags.ArgVT = VT;
Stephen Lin3484da92013-04-30 22:49:28 +00007004 MyFlags.Used = CLI.IsReturnValueUsed;
7005 if (CLI.RetSExt)
7006 MyFlags.Flags.setSExt();
7007 if (CLI.RetZExt)
7008 MyFlags.Flags.setZExt();
7009 if (CLI.IsInReg)
7010 MyFlags.Flags.setInReg();
7011 CLI.Ins.push_back(MyFlags);
7012 }
7013 }
7014
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007015 // Handle all of the outgoing arguments.
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00007016 CLI.Outs.clear();
7017 CLI.OutVals.clear();
7018 ArgListTy &Args = CLI.Args;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007019 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00007020 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007021 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
7022 for (unsigned Value = 0, NumValues = ValueVTs.size();
7023 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00007024 EVT VT = ValueVTs[Value];
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00007025 Type *ArgTy = VT.getTypeForEVT(CLI.RetTy->getContext());
Chris Lattner2a0b96c2008-10-18 18:49:30 +00007026 SDValue Op = SDValue(Args[i].Node.getNode(),
7027 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007028 ISD::ArgFlagsTy Flags;
7029 unsigned OriginalAlignment =
Micah Villmow3574eca2012-10-08 16:38:25 +00007030 getDataLayout()->getABITypeAlignment(ArgTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007031
7032 if (Args[i].isZExt)
7033 Flags.setZExt();
7034 if (Args[i].isSExt)
7035 Flags.setSExt();
7036 if (Args[i].isInReg)
7037 Flags.setInReg();
7038 if (Args[i].isSRet)
7039 Flags.setSRet();
7040 if (Args[i].isByVal) {
7041 Flags.setByVal();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007042 PointerType *Ty = cast<PointerType>(Args[i].Ty);
7043 Type *ElementTy = Ty->getElementType();
Micah Villmow3574eca2012-10-08 16:38:25 +00007044 Flags.setByValSize(getDataLayout()->getTypeAllocSize(ElementTy));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007045 // For ByVal, alignment should come from FE. BE will guess if this
7046 // info is not there but there are cases it cannot get right.
Chris Lattner9db20f32011-05-22 23:23:02 +00007047 unsigned FrameAlign;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007048 if (Args[i].Alignment)
7049 FrameAlign = Args[i].Alignment;
Chris Lattner9db20f32011-05-22 23:23:02 +00007050 else
7051 FrameAlign = getByValTypeAlignment(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007052 Flags.setByValAlign(FrameAlign);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007053 }
7054 if (Args[i].isNest)
7055 Flags.setNest();
7056 Flags.setOrigAlign(OriginalAlignment);
7057
Patrik Hagglunddfcf33a2012-12-19 11:48:16 +00007058 MVT PartVT = getRegisterType(CLI.RetTy->getContext(), VT);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00007059 unsigned NumParts = getNumRegisters(CLI.RetTy->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007060 SmallVector<SDValue, 4> Parts(NumParts);
7061 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
7062
7063 if (Args[i].isSExt)
7064 ExtendKind = ISD::SIGN_EXTEND;
7065 else if (Args[i].isZExt)
7066 ExtendKind = ISD::ZERO_EXTEND;
7067
Stephen Lin3484da92013-04-30 22:49:28 +00007068 // Conservatively only handle 'returned' on non-vectors for now
7069 if (Args[i].isReturned && !Op.getValueType().isVector()) {
7070 assert(CLI.RetTy == Args[i].Ty && RetTys.size() == NumValues &&
7071 "unexpected use of 'returned'");
7072 // Before passing 'returned' to the target lowering code, ensure that
7073 // either the register MVT and the actual EVT are the same size or that
7074 // the return value and argument are extended in the same way; in these
7075 // cases it's safe to pass the argument register value unchanged as the
7076 // return register value (although it's at the target's option whether
7077 // to do so)
7078 // TODO: allow code generation to take advantage of partially preserved
7079 // registers rather than clobbering the entire register when the
7080 // parameter extension method is not compatible with the return
7081 // extension method
7082 if ((NumParts * PartVT.getSizeInBits() == VT.getSizeInBits()) ||
7083 (ExtendKind != ISD::ANY_EXTEND &&
7084 CLI.RetSExt == Args[i].isSExt && CLI.RetZExt == Args[i].isZExt))
7085 Flags.setReturned();
7086 }
7087
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00007088 getCopyToParts(CLI.DAG, CLI.DL, Op, &Parts[0], NumParts,
Bill Wendlingf18eb582012-09-26 06:16:18 +00007089 PartVT, CLI.CS ? CLI.CS->getInstruction() : 0, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007090
Dan Gohman98ca4f22009-08-05 01:29:28 +00007091 for (unsigned j = 0; j != NumParts; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007092 // if it isn't first piece, alignment must be 1
Tom Stellardd0716b02013-10-23 00:44:24 +00007093 ISD::OutputArg MyFlags(Flags, Parts[j].getValueType(), VT,
Manman Ren0a1544d2012-11-01 23:49:58 +00007094 i < CLI.NumFixedArgs,
7095 i, j*Parts[j].getValueType().getStoreSize());
Dan Gohman98ca4f22009-08-05 01:29:28 +00007096 if (NumParts > 1 && j == 0)
7097 MyFlags.Flags.setSplit();
7098 else if (j != 0)
7099 MyFlags.Flags.setOrigAlign(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007100
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00007101 CLI.Outs.push_back(MyFlags);
7102 CLI.OutVals.push_back(Parts[j]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007103 }
7104 }
7105 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00007106
Dan Gohman98ca4f22009-08-05 01:29:28 +00007107 SmallVector<SDValue, 4> InVals;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00007108 CLI.Chain = LowerCall(CLI, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00007109
7110 // Verify that the target's LowerCall behaved as expected.
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00007111 assert(CLI.Chain.getNode() && CLI.Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00007112 "LowerCall didn't return a valid chain!");
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00007113 assert((!CLI.IsTailCall || InVals.empty()) &&
Dan Gohman5e866062009-08-06 15:37:27 +00007114 "LowerCall emitted a return value for a tail call!");
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00007115 assert((CLI.IsTailCall || InVals.size() == CLI.Ins.size()) &&
Dan Gohman5e866062009-08-06 15:37:27 +00007116 "LowerCall didn't emit the correct number of values!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00007117
7118 // For a tail call, the return value is merely live-out and there aren't
7119 // any nodes in the DAG representing it. Return a special value to
7120 // indicate that a tail call has been emitted and no more Instructions
7121 // should be processed in the current block.
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00007122 if (CLI.IsTailCall) {
7123 CLI.DAG.setRoot(CLI.Chain);
Dan Gohman98ca4f22009-08-05 01:29:28 +00007124 return std::make_pair(SDValue(), SDValue());
7125 }
7126
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00007127 DEBUG(for (unsigned i = 0, e = CLI.Ins.size(); i != e; ++i) {
Evan Chengaf1871f2010-03-11 19:38:18 +00007128 assert(InVals[i].getNode() &&
7129 "LowerCall emitted a null value!");
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00007130 assert(EVT(CLI.Ins[i].VT) == InVals[i].getValueType() &&
Evan Chengaf1871f2010-03-11 19:38:18 +00007131 "LowerCall emitted a value with the wrong type!");
7132 });
7133
Dan Gohman98ca4f22009-08-05 01:29:28 +00007134 // Collect the legal value parts into potentially illegal values
7135 // that correspond to the original function's return values.
7136 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00007137 if (CLI.RetSExt)
Dan Gohman98ca4f22009-08-05 01:29:28 +00007138 AssertOp = ISD::AssertSext;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00007139 else if (CLI.RetZExt)
Dan Gohman98ca4f22009-08-05 01:29:28 +00007140 AssertOp = ISD::AssertZext;
7141 SmallVector<SDValue, 4> ReturnValues;
7142 unsigned CurReg = 0;
7143 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00007144 EVT VT = RetTys[I];
Patrik Hagglunddfcf33a2012-12-19 11:48:16 +00007145 MVT RegisterVT = getRegisterType(CLI.RetTy->getContext(), VT);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00007146 unsigned NumRegs = getNumRegisters(CLI.RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00007147
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00007148 ReturnValues.push_back(getCopyFromParts(CLI.DAG, CLI.DL, &InVals[CurReg],
Bill Wendling12931302012-09-26 04:04:19 +00007149 NumRegs, RegisterVT, VT, NULL,
Bill Wendling4533cac2010-01-28 21:51:40 +00007150 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00007151 CurReg += NumRegs;
7152 }
7153
7154 // For a function returning void, there is no return value. We can't create
7155 // such a node, so we just return a null return value in that case. In
Chris Lattner7a2bdde2011-04-15 05:18:47 +00007156 // that case, nothing will actually look at the value.
Dan Gohman98ca4f22009-08-05 01:29:28 +00007157 if (ReturnValues.empty())
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00007158 return std::make_pair(SDValue(), CLI.Chain);
Dan Gohman98ca4f22009-08-05 01:29:28 +00007159
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00007160 SDValue Res = CLI.DAG.getNode(ISD::MERGE_VALUES, CLI.DL,
7161 CLI.DAG.getVTList(&RetTys[0], RetTys.size()),
Dan Gohman98ca4f22009-08-05 01:29:28 +00007162 &ReturnValues[0], ReturnValues.size());
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00007163 return std::make_pair(Res, CLI.Chain);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007164}
7165
Duncan Sands9fbc7e22009-01-21 09:00:29 +00007166void TargetLowering::LowerOperationWrapper(SDNode *N,
7167 SmallVectorImpl<SDValue> &Results,
Dan Gohmand858e902010-04-17 15:26:15 +00007168 SelectionDAG &DAG) const {
Duncan Sands9fbc7e22009-01-21 09:00:29 +00007169 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00007170 if (Res.getNode())
7171 Results.push_back(Res);
7172}
7173
Dan Gohmand858e902010-04-17 15:26:15 +00007174SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Torok Edwinc23197a2009-07-14 16:55:14 +00007175 llvm_unreachable("LowerOperation not implemented for this target!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007176}
7177
Dan Gohman46510a72010-04-15 01:51:59 +00007178void
7179SelectionDAGBuilder::CopyValueToVirtualRegister(const Value *V, unsigned Reg) {
Dan Gohman28a17352010-07-01 01:59:43 +00007180 SDValue Op = getNonRegisterValue(V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007181 assert((Op.getOpcode() != ISD::CopyFromReg ||
7182 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
7183 "Copy from a reg to the same reg!");
7184 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
7185
Bill Wendlingba54bca2013-06-19 21:36:55 +00007186 const TargetLowering *TLI = TM.getTargetLowering();
7187 RegsForValue RFV(V->getContext(), *TLI, Reg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007188 SDValue Chain = DAG.getEntryNode();
Andrew Trickac6d9be2013-05-25 02:42:55 +00007189 RFV.getCopyToRegs(Op, DAG, getCurSDLoc(), Chain, 0, V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007190 PendingExports.push_back(Chain);
7191}
7192
7193#include "llvm/CodeGen/SelectionDAGISel.h"
7194
Eli Friedman23d32432011-05-05 16:53:34 +00007195/// isOnlyUsedInEntryBlock - If the specified argument is only used in the
7196/// entry block, return true. This includes arguments used by switches, since
7197/// the switch may expand into multiple basic blocks.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00007198static bool isOnlyUsedInEntryBlock(const Argument *A, bool FastISel) {
Eli Friedman23d32432011-05-05 16:53:34 +00007199 // With FastISel active, we may be splitting blocks, so force creation
7200 // of virtual registers for all non-dead arguments.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00007201 if (FastISel)
Eli Friedman23d32432011-05-05 16:53:34 +00007202 return A->use_empty();
7203
7204 const BasicBlock *Entry = A->getParent()->begin();
7205 for (Value::const_use_iterator UI = A->use_begin(), E = A->use_end();
7206 UI != E; ++UI) {
7207 const User *U = *UI;
7208 if (cast<Instruction>(U)->getParent() != Entry || isa<SwitchInst>(U))
7209 return false; // Use not in entry block.
7210 }
7211 return true;
7212}
7213
Eli Bendersky6437d382013-02-28 23:09:18 +00007214void SelectionDAGISel::LowerArguments(const Function &F) {
Dan Gohman2048b852009-11-23 18:04:58 +00007215 SelectionDAG &DAG = SDB->DAG;
Andrew Trickac6d9be2013-05-25 02:42:55 +00007216 SDLoc dl = SDB->getCurSDLoc();
Bill Wendlingba54bca2013-06-19 21:36:55 +00007217 const TargetLowering *TLI = getTargetLowering();
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00007218 const DataLayout *TD = TLI->getDataLayout();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00007219 SmallVector<ISD::InputArg, 16> Ins;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007220
Dan Gohman7451d3e2010-05-29 17:03:36 +00007221 if (!FuncInfo->CanLowerReturn) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00007222 // Put in an sret pointer parameter before all the other parameters.
7223 SmallVector<EVT, 1> ValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00007224 ComputeValueVTs(*getTargetLowering(),
7225 PointerType::getUnqual(F.getReturnType()), ValueVTs);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00007226
7227 // NOTE: Assuming that a pointer will never break down to more than one VT
7228 // or one register.
7229 ISD::ArgFlagsTy Flags;
7230 Flags.setSRet();
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00007231 MVT RegisterVT = TLI->getRegisterType(*DAG.getContext(), ValueVTs[0]);
Tom Stellardd0716b02013-10-23 00:44:24 +00007232 ISD::InputArg RetArg(Flags, RegisterVT, ValueVTs[0], true, 0, 0);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00007233 Ins.push_back(RetArg);
7234 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00007235
Dan Gohman98ca4f22009-08-05 01:29:28 +00007236 // Set up the incoming argument description vector.
Dan Gohman98ca4f22009-08-05 01:29:28 +00007237 unsigned Idx = 1;
Dan Gohman46510a72010-04-15 01:51:59 +00007238 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
Dan Gohman98ca4f22009-08-05 01:29:28 +00007239 I != E; ++I, ++Idx) {
Owen Andersone50ed302009-08-10 22:56:29 +00007240 SmallVector<EVT, 4> ValueVTs;
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00007241 ComputeValueVTs(*TLI, I->getType(), ValueVTs);
Dan Gohman98ca4f22009-08-05 01:29:28 +00007242 bool isArgValueUsed = !I->use_empty();
Tom Stellardd0716b02013-10-23 00:44:24 +00007243 unsigned PartBase = 0;
Dan Gohman98ca4f22009-08-05 01:29:28 +00007244 for (unsigned Value = 0, NumValues = ValueVTs.size();
7245 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00007246 EVT VT = ValueVTs[Value];
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007247 Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00007248 ISD::ArgFlagsTy Flags;
7249 unsigned OriginalAlignment =
7250 TD->getABITypeAlignment(ArgTy);
7251
Bill Wendling39cd0c82012-12-30 12:45:13 +00007252 if (F.getAttributes().hasAttribute(Idx, Attribute::ZExt))
Dan Gohman98ca4f22009-08-05 01:29:28 +00007253 Flags.setZExt();
Bill Wendling39cd0c82012-12-30 12:45:13 +00007254 if (F.getAttributes().hasAttribute(Idx, Attribute::SExt))
Dan Gohman98ca4f22009-08-05 01:29:28 +00007255 Flags.setSExt();
Bill Wendling39cd0c82012-12-30 12:45:13 +00007256 if (F.getAttributes().hasAttribute(Idx, Attribute::InReg))
Dan Gohman98ca4f22009-08-05 01:29:28 +00007257 Flags.setInReg();
Bill Wendling39cd0c82012-12-30 12:45:13 +00007258 if (F.getAttributes().hasAttribute(Idx, Attribute::StructRet))
Dan Gohman98ca4f22009-08-05 01:29:28 +00007259 Flags.setSRet();
Bill Wendling39cd0c82012-12-30 12:45:13 +00007260 if (F.getAttributes().hasAttribute(Idx, Attribute::ByVal)) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00007261 Flags.setByVal();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007262 PointerType *Ty = cast<PointerType>(I->getType());
7263 Type *ElementTy = Ty->getElementType();
Chris Lattner9db20f32011-05-22 23:23:02 +00007264 Flags.setByValSize(TD->getTypeAllocSize(ElementTy));
Dan Gohman98ca4f22009-08-05 01:29:28 +00007265 // For ByVal, alignment should be passed from FE. BE will guess if
7266 // this info is not there but there are cases it cannot get right.
Chris Lattner9db20f32011-05-22 23:23:02 +00007267 unsigned FrameAlign;
Dan Gohman98ca4f22009-08-05 01:29:28 +00007268 if (F.getParamAlignment(Idx))
7269 FrameAlign = F.getParamAlignment(Idx);
Chris Lattner9db20f32011-05-22 23:23:02 +00007270 else
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00007271 FrameAlign = TLI->getByValTypeAlignment(ElementTy);
Dan Gohman98ca4f22009-08-05 01:29:28 +00007272 Flags.setByValAlign(FrameAlign);
Dan Gohman98ca4f22009-08-05 01:29:28 +00007273 }
Bill Wendling39cd0c82012-12-30 12:45:13 +00007274 if (F.getAttributes().hasAttribute(Idx, Attribute::Nest))
Dan Gohman98ca4f22009-08-05 01:29:28 +00007275 Flags.setNest();
7276 Flags.setOrigAlign(OriginalAlignment);
7277
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00007278 MVT RegisterVT = TLI->getRegisterType(*CurDAG->getContext(), VT);
7279 unsigned NumRegs = TLI->getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00007280 for (unsigned i = 0; i != NumRegs; ++i) {
Tom Stellardd0716b02013-10-23 00:44:24 +00007281 ISD::InputArg MyFlags(Flags, RegisterVT, VT, isArgValueUsed,
7282 Idx-1, PartBase+i*RegisterVT.getStoreSize());
Dan Gohman98ca4f22009-08-05 01:29:28 +00007283 if (NumRegs > 1 && i == 0)
7284 MyFlags.Flags.setSplit();
7285 // if it isn't first piece, alignment must be 1
7286 else if (i > 0)
7287 MyFlags.Flags.setOrigAlign(1);
7288 Ins.push_back(MyFlags);
7289 }
Tom Stellardd0716b02013-10-23 00:44:24 +00007290 PartBase += VT.getStoreSize();
Dan Gohman98ca4f22009-08-05 01:29:28 +00007291 }
7292 }
7293
7294 // Call the target to set up the argument values.
7295 SmallVector<SDValue, 8> InVals;
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00007296 SDValue NewRoot = TLI->LowerFormalArguments(DAG.getRoot(), F.getCallingConv(),
7297 F.isVarArg(), Ins,
7298 dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00007299
7300 // Verify that the target's LowerFormalArguments behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00007301 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00007302 "LowerFormalArguments didn't return a valid chain!");
7303 assert(InVals.size() == Ins.size() &&
7304 "LowerFormalArguments didn't emit the correct number of values!");
Bill Wendling3ea58b62009-12-22 21:35:02 +00007305 DEBUG({
7306 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
7307 assert(InVals[i].getNode() &&
7308 "LowerFormalArguments emitted a null value!");
Duncan Sands1440e8b2010-11-03 11:35:31 +00007309 assert(EVT(Ins[i].VT) == InVals[i].getValueType() &&
Bill Wendling3ea58b62009-12-22 21:35:02 +00007310 "LowerFormalArguments emitted a value with the wrong type!");
7311 }
7312 });
Bill Wendling3ea3c242009-12-22 02:10:19 +00007313
Dan Gohman5e866062009-08-06 15:37:27 +00007314 // Update the DAG with the new chain value resulting from argument lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00007315 DAG.setRoot(NewRoot);
7316
7317 // Set up the argument values.
7318 unsigned i = 0;
7319 Idx = 1;
Dan Gohman7451d3e2010-05-29 17:03:36 +00007320 if (!FuncInfo->CanLowerReturn) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00007321 // Create a virtual register for the sret pointer, and put in a copy
7322 // from the sret argument into it.
7323 SmallVector<EVT, 1> ValueVTs;
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00007324 ComputeValueVTs(*TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00007325 MVT VT = ValueVTs[0].getSimpleVT();
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00007326 MVT RegVT = TLI->getRegisterType(*CurDAG->getContext(), VT);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00007327 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Bill Wendling46ada192010-03-02 01:55:18 +00007328 SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1,
Bill Wendling12931302012-09-26 04:04:19 +00007329 RegVT, VT, NULL, AssertOp);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00007330
Dan Gohman2048b852009-11-23 18:04:58 +00007331 MachineFunction& MF = SDB->DAG.getMachineFunction();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00007332 MachineRegisterInfo& RegInfo = MF.getRegInfo();
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00007333 unsigned SRetReg = RegInfo.createVirtualRegister(TLI->getRegClassFor(RegVT));
Dan Gohman7451d3e2010-05-29 17:03:36 +00007334 FuncInfo->DemoteRegister = SRetReg;
Andrew Trickac6d9be2013-05-25 02:42:55 +00007335 NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurSDLoc(),
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00007336 SRetReg, ArgValue);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00007337 DAG.setRoot(NewRoot);
Bill Wendling3ea3c242009-12-22 02:10:19 +00007338
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00007339 // i indexes lowered arguments. Bump it past the hidden sret argument.
7340 // Idx indexes LLVM arguments. Don't touch it.
7341 ++i;
7342 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00007343
Dan Gohman46510a72010-04-15 01:51:59 +00007344 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
Dan Gohman98ca4f22009-08-05 01:29:28 +00007345 ++I, ++Idx) {
7346 SmallVector<SDValue, 4> ArgValues;
Owen Andersone50ed302009-08-10 22:56:29 +00007347 SmallVector<EVT, 4> ValueVTs;
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00007348 ComputeValueVTs(*TLI, I->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007349 unsigned NumValues = ValueVTs.size();
Devang Patel9126c0d2010-06-01 19:59:01 +00007350
7351 // If this argument is unused then remember its value. It is used to generate
7352 // debugging information.
Adrian Prantldf688032013-05-16 23:44:12 +00007353 if (I->use_empty() && NumValues) {
Devang Patel9126c0d2010-06-01 19:59:01 +00007354 SDB->setUnusedArgValue(I, InVals[i]);
7355
Adrian Prantldf688032013-05-16 23:44:12 +00007356 // Also remember any frame index for use in FastISel.
7357 if (FrameIndexSDNode *FI =
7358 dyn_cast<FrameIndexSDNode>(InVals[i].getNode()))
7359 FuncInfo->setArgumentFrameIndex(I, FI->getIndex());
7360 }
7361
Eli Friedman23d32432011-05-05 16:53:34 +00007362 for (unsigned Val = 0; Val != NumValues; ++Val) {
7363 EVT VT = ValueVTs[Val];
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00007364 MVT PartVT = TLI->getRegisterType(*CurDAG->getContext(), VT);
7365 unsigned NumParts = TLI->getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00007366
7367 if (!I->use_empty()) {
7368 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Bill Wendling39cd0c82012-12-30 12:45:13 +00007369 if (F.getAttributes().hasAttribute(Idx, Attribute::SExt))
Dan Gohman98ca4f22009-08-05 01:29:28 +00007370 AssertOp = ISD::AssertSext;
Bill Wendling39cd0c82012-12-30 12:45:13 +00007371 else if (F.getAttributes().hasAttribute(Idx, Attribute::ZExt))
Dan Gohman98ca4f22009-08-05 01:29:28 +00007372 AssertOp = ISD::AssertZext;
7373
Bill Wendling46ada192010-03-02 01:55:18 +00007374 ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i],
Bill Wendling3ea3c242009-12-22 02:10:19 +00007375 NumParts, PartVT, VT,
Bill Wendling12931302012-09-26 04:04:19 +00007376 NULL, AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00007377 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00007378
Dan Gohman98ca4f22009-08-05 01:29:28 +00007379 i += NumParts;
7380 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00007381
Eli Friedman23d32432011-05-05 16:53:34 +00007382 // We don't need to do anything else for unused arguments.
7383 if (ArgValues.empty())
7384 continue;
7385
Devang Patel9aee3352011-09-08 22:59:09 +00007386 // Note down frame index.
7387 if (FrameIndexSDNode *FI =
Bill Wendling96cb1122012-07-19 00:04:14 +00007388 dyn_cast<FrameIndexSDNode>(ArgValues[0].getNode()))
Devang Patel9aee3352011-09-08 22:59:09 +00007389 FuncInfo->setArgumentFrameIndex(I, FI->getIndex());
Devang Patel0b48ead2010-08-31 22:22:42 +00007390
Eli Friedman23d32432011-05-05 16:53:34 +00007391 SDValue Res = DAG.getMergeValues(&ArgValues[0], NumValues,
Andrew Trickac6d9be2013-05-25 02:42:55 +00007392 SDB->getCurSDLoc());
Devang Patel9aee3352011-09-08 22:59:09 +00007393
Eli Friedman23d32432011-05-05 16:53:34 +00007394 SDB->setValue(I, Res);
Nick Lewycky8a8d4792011-12-02 22:16:29 +00007395 if (!TM.Options.EnableFastISel && Res.getOpcode() == ISD::BUILD_PAIR) {
Stephen Lin155615d2013-07-08 00:37:03 +00007396 if (LoadSDNode *LNode =
Devang Patel9aee3352011-09-08 22:59:09 +00007397 dyn_cast<LoadSDNode>(Res.getOperand(0).getNode()))
7398 if (FrameIndexSDNode *FI =
7399 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
7400 FuncInfo->setArgumentFrameIndex(I, FI->getIndex());
7401 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00007402
Eli Friedman23d32432011-05-05 16:53:34 +00007403 // If this argument is live outside of the entry block, insert a copy from
7404 // wherever we got it to the vreg that other BB's will reference it as.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00007405 if (!TM.Options.EnableFastISel && Res.getOpcode() == ISD::CopyFromReg) {
Eli Friedman23d32432011-05-05 16:53:34 +00007406 // If we can, though, try to skip creating an unnecessary vreg.
7407 // FIXME: This isn't very clean... it would be nice to make this more
Eli Friedman7f33d672011-05-10 21:50:58 +00007408 // general. It's also subtly incompatible with the hacks FastISel
7409 // uses with vregs.
Eli Friedman23d32432011-05-05 16:53:34 +00007410 unsigned Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg();
7411 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
7412 FuncInfo->ValueMap[I] = Reg;
7413 continue;
7414 }
7415 }
Nick Lewycky8a8d4792011-12-02 22:16:29 +00007416 if (!isOnlyUsedInEntryBlock(I, TM.Options.EnableFastISel)) {
Eli Friedman23d32432011-05-05 16:53:34 +00007417 FuncInfo->InitializeRegForValue(I);
Dan Gohman2048b852009-11-23 18:04:58 +00007418 SDB->CopyToExportRegsIfNeeded(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007419 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007420 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00007421
Dan Gohman98ca4f22009-08-05 01:29:28 +00007422 assert(i == InVals.size() && "Argument register count mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007423
7424 // Finally, if the target has anything special to do, allow it to do so.
7425 // FIXME: this should insert code into the DAG!
Dan Gohman64652652010-04-14 20:17:22 +00007426 EmitFunctionEntryCode();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007427}
7428
7429/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
7430/// ensure constants are generated when needed. Remember the virtual registers
7431/// that need to be added to the Machine PHI nodes as input. We cannot just
7432/// directly add them, because expansion might result in multiple MBB's for one
7433/// BB. As such, the start of the BB might correspond to a different MBB than
7434/// the end.
7435///
7436void
Dan Gohmanf81eca02010-04-22 20:46:50 +00007437SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
Dan Gohman46510a72010-04-15 01:51:59 +00007438 const TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007439
7440 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
7441
7442 // Check successor nodes' PHI nodes that expect a constant to be available
7443 // from this block.
7444 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
Dan Gohman46510a72010-04-15 01:51:59 +00007445 const BasicBlock *SuccBB = TI->getSuccessor(succ);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007446 if (!isa<PHINode>(SuccBB->begin())) continue;
Dan Gohmanf81eca02010-04-22 20:46:50 +00007447 MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00007448
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007449 // If this terminator has multiple identical successors (common for
7450 // switches), only handle each succ once.
7451 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00007452
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007453 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007454
7455 // At this point we know that there is a 1-1 correspondence between LLVM PHI
7456 // nodes and Machine PHI nodes, but the incoming operands have not been
7457 // emitted yet.
Dan Gohman46510a72010-04-15 01:51:59 +00007458 for (BasicBlock::const_iterator I = SuccBB->begin();
7459 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007460 // Ignore dead phi's.
7461 if (PN->use_empty()) continue;
7462
Rafael Espindola3fa82832011-05-13 15:18:06 +00007463 // Skip empty types
7464 if (PN->getType()->isEmptyTy())
7465 continue;
7466
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007467 unsigned Reg;
Dan Gohman46510a72010-04-15 01:51:59 +00007468 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007469
Dan Gohman46510a72010-04-15 01:51:59 +00007470 if (const Constant *C = dyn_cast<Constant>(PHIOp)) {
Dan Gohmanf81eca02010-04-22 20:46:50 +00007471 unsigned &RegOut = ConstantsOut[C];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007472 if (RegOut == 0) {
Dan Gohman89496d02010-07-02 00:10:16 +00007473 RegOut = FuncInfo.CreateRegs(C->getType());
Dan Gohmanf81eca02010-04-22 20:46:50 +00007474 CopyValueToVirtualRegister(C, RegOut);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007475 }
7476 Reg = RegOut;
7477 } else {
Dan Gohmanc25ad632010-07-01 01:33:21 +00007478 DenseMap<const Value *, unsigned>::iterator I =
7479 FuncInfo.ValueMap.find(PHIOp);
7480 if (I != FuncInfo.ValueMap.end())
7481 Reg = I->second;
7482 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007483 assert(isa<AllocaInst>(PHIOp) &&
Dan Gohmanf81eca02010-04-22 20:46:50 +00007484 FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007485 "Didn't codegen value into a register!??");
Dan Gohman89496d02010-07-02 00:10:16 +00007486 Reg = FuncInfo.CreateRegs(PHIOp->getType());
Dan Gohmanf81eca02010-04-22 20:46:50 +00007487 CopyValueToVirtualRegister(PHIOp, Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007488 }
7489 }
7490
7491 // Remember that this register needs to added to the machine PHI node as
7492 // the input for this MBB.
Owen Andersone50ed302009-08-10 22:56:29 +00007493 SmallVector<EVT, 4> ValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00007494 const TargetLowering *TLI = TM.getTargetLowering();
7495 ComputeValueVTs(*TLI, PN->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007496 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +00007497 EVT VT = ValueVTs[vti];
Bill Wendlingba54bca2013-06-19 21:36:55 +00007498 unsigned NumRegisters = TLI->getNumRegisters(*DAG.getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007499 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
Dan Gohmanf81eca02010-04-22 20:46:50 +00007500 FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007501 Reg += NumRegisters;
7502 }
7503 }
7504 }
Bill Wendlingba54bca2013-06-19 21:36:55 +00007505
Dan Gohmanf81eca02010-04-22 20:46:50 +00007506 ConstantsOut.clear();
Dan Gohman3df24e62008-09-03 23:12:08 +00007507}
Michael Gottesman657484f2013-08-20 07:00:16 +00007508
7509/// Add a successor MBB to ParentMBB< creating a new MachineBB for BB if SuccMBB
7510/// is 0.
7511MachineBasicBlock *
7512SelectionDAGBuilder::StackProtectorDescriptor::
7513AddSuccessorMBB(const BasicBlock *BB,
7514 MachineBasicBlock *ParentMBB,
7515 MachineBasicBlock *SuccMBB) {
7516 // If SuccBB has not been created yet, create it.
7517 if (!SuccMBB) {
7518 MachineFunction *MF = ParentMBB->getParent();
7519 MachineFunction::iterator BBI = ParentMBB;
7520 SuccMBB = MF->CreateMachineBasicBlock(BB);
7521 MF->insert(++BBI, SuccMBB);
7522 }
7523 // Add it as a successor of ParentMBB.
7524 ParentMBB->addSuccessor(SuccMBB);
7525 return SuccMBB;
7526}