blob: ef3466b694a77ded7176362668eab66c53f4a58a [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"
Bill Wendling0bcbd1d2012-06-28 00:05:13 +000036#include "llvm/DebugInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000037#include "llvm/IR/CallingConv.h"
38#include "llvm/IR/Constants.h"
39#include "llvm/IR/DataLayout.h"
40#include "llvm/IR/DerivedTypes.h"
41#include "llvm/IR/Function.h"
42#include "llvm/IR/GlobalVariable.h"
43#include "llvm/IR/InlineAsm.h"
44#include "llvm/IR/Instructions.h"
45#include "llvm/IR/IntrinsicInst.h"
46#include "llvm/IR/Intrinsics.h"
47#include "llvm/IR/LLVMContext.h"
48#include "llvm/IR/Module.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000049#include "llvm/Support/CommandLine.h"
50#include "llvm/Support/Debug.h"
51#include "llvm/Support/ErrorHandling.h"
52#include "llvm/Support/IntegersSubsetMapping.h"
53#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
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001067 if (isa<ConstantPointerNull>(C))
Bill Wendlingba54bca2013-06-19 21:36:55 +00001068 return DAG.getConstant(0, TLI->getPointerTy());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001069
Dan Gohman383b5f62010-04-17 15:32:28 +00001070 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Dan Gohman28a17352010-07-01 01:59:43 +00001071 return DAG.getConstantFP(*CFP, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001072
Nate Begeman9008ca62009-04-27 18:41:29 +00001073 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
Dan Gohman28a17352010-07-01 01:59:43 +00001074 return DAG.getUNDEF(VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001075
Dan Gohman383b5f62010-04-17 15:32:28 +00001076 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001077 visit(CE->getOpcode(), *CE);
1078 SDValue N1 = NodeMap[V];
Dan Gohmanac7d05c2010-04-16 16:55:18 +00001079 assert(N1.getNode() && "visit didn't populate the NodeMap!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001080 return N1;
1081 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001082
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001083 if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
1084 SmallVector<SDValue, 4> Constants;
1085 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
1086 OI != OE; ++OI) {
1087 SDNode *Val = getValue(*OI).getNode();
Dan Gohmaned48caf2009-09-08 01:44:02 +00001088 // If the operand is an empty aggregate, there are no values.
1089 if (!Val) continue;
1090 // Add each leaf value from the operand to the Constants list
1091 // to form a flattened list of all the values.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001092 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
1093 Constants.push_back(SDValue(Val, i));
1094 }
Bill Wendling87710f02009-12-21 23:47:40 +00001095
Bill Wendling4533cac2010-01-28 21:51:40 +00001096 return DAG.getMergeValues(&Constants[0], Constants.size(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001097 getCurSDLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001098 }
Stephen Lin155615d2013-07-08 00:37:03 +00001099
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001100 if (const ConstantDataSequential *CDS =
1101 dyn_cast<ConstantDataSequential>(C)) {
1102 SmallVector<SDValue, 4> Ops;
Chris Lattner0f193b82012-01-25 01:27:20 +00001103 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001104 SDNode *Val = getValue(CDS->getElementAsConstant(i)).getNode();
1105 // Add each leaf value from the operand to the Constants list
1106 // to form a flattened list of all the values.
1107 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
1108 Ops.push_back(SDValue(Val, i));
1109 }
1110
1111 if (isa<ArrayType>(CDS->getType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001112 return DAG.getMergeValues(&Ops[0], Ops.size(), getCurSDLoc());
1113 return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurSDLoc(),
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001114 VT, &Ops[0], Ops.size());
1115 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001116
Duncan Sands1df98592010-02-16 11:11:14 +00001117 if (C->getType()->isStructTy() || C->getType()->isArrayTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001118 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
1119 "Unknown struct or array constant!");
1120
Owen Andersone50ed302009-08-10 22:56:29 +00001121 SmallVector<EVT, 4> ValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00001122 ComputeValueVTs(*TLI, C->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001123 unsigned NumElts = ValueVTs.size();
1124 if (NumElts == 0)
1125 return SDValue(); // empty struct
1126 SmallVector<SDValue, 4> Constants(NumElts);
1127 for (unsigned i = 0; i != NumElts; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00001128 EVT EltVT = ValueVTs[i];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001129 if (isa<UndefValue>(C))
Dale Johannesene8d72302009-02-06 23:05:02 +00001130 Constants[i] = DAG.getUNDEF(EltVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001131 else if (EltVT.isFloatingPoint())
1132 Constants[i] = DAG.getConstantFP(0, EltVT);
1133 else
1134 Constants[i] = DAG.getConstant(0, EltVT);
1135 }
Bill Wendling87710f02009-12-21 23:47:40 +00001136
Bill Wendling4533cac2010-01-28 21:51:40 +00001137 return DAG.getMergeValues(&Constants[0], NumElts,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001138 getCurSDLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001139 }
1140
Dan Gohman383b5f62010-04-17 15:32:28 +00001141 if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
Dan Gohman29cbade2009-11-20 23:18:13 +00001142 return DAG.getBlockAddress(BA, VT);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001143
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001144 VectorType *VecTy = cast<VectorType>(V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001145 unsigned NumElements = VecTy->getNumElements();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001146
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001147 // Now that we know the number and type of the elements, get that number of
1148 // elements into the Ops array based on what kind of constant it is.
1149 SmallVector<SDValue, 16> Ops;
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001150 if (const ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001151 for (unsigned i = 0; i != NumElements; ++i)
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001152 Ops.push_back(getValue(CV->getOperand(i)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001153 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00001154 assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!");
Bill Wendlingba54bca2013-06-19 21:36:55 +00001155 EVT EltVT = TLI->getValueType(VecTy->getElementType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001156
1157 SDValue Op;
Nate Begeman9008ca62009-04-27 18:41:29 +00001158 if (EltVT.isFloatingPoint())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001159 Op = DAG.getConstantFP(0, EltVT);
1160 else
1161 Op = DAG.getConstant(0, EltVT);
1162 Ops.assign(NumElements, Op);
1163 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001164
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001165 // Create a BUILD_VECTOR node.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001166 return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00001167 VT, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001168 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001169
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001170 // If this is a static alloca, generate it as the frameindex instead of
1171 // computation.
1172 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
1173 DenseMap<const AllocaInst*, int>::iterator SI =
1174 FuncInfo.StaticAllocaMap.find(AI);
1175 if (SI != FuncInfo.StaticAllocaMap.end())
Bill Wendlingba54bca2013-06-19 21:36:55 +00001176 return DAG.getFrameIndex(SI->second, TLI->getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001177 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001178
Dan Gohman28a17352010-07-01 01:59:43 +00001179 // If this is an instruction which fast-isel has deferred, select it now.
1180 if (const Instruction *Inst = dyn_cast<Instruction>(V)) {
Dan Gohman84023e02010-07-10 09:00:22 +00001181 unsigned InReg = FuncInfo.InitializeRegForValue(Inst);
Bill Wendlingba54bca2013-06-19 21:36:55 +00001182 RegsForValue RFV(*DAG.getContext(), *TLI, InReg, Inst->getType());
Dan Gohman84023e02010-07-10 09:00:22 +00001183 SDValue Chain = DAG.getEntryNode();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001184 return RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, NULL, V);
Dan Gohman28a17352010-07-01 01:59:43 +00001185 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001186
Dan Gohman28a17352010-07-01 01:59:43 +00001187 llvm_unreachable("Can't get register for value!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001188}
1189
Dan Gohman46510a72010-04-15 01:51:59 +00001190void SelectionDAGBuilder::visitRet(const ReturnInst &I) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00001191 const TargetLowering *TLI = TM.getTargetLowering();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001192 SDValue Chain = getControlRoot();
1193 SmallVector<ISD::OutputArg, 8> Outs;
Dan Gohmanc9403652010-07-07 15:54:55 +00001194 SmallVector<SDValue, 8> OutVals;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001195
Dan Gohman7451d3e2010-05-29 17:03:36 +00001196 if (!FuncInfo.CanLowerReturn) {
1197 unsigned DemoteReg = FuncInfo.DemoteRegister;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001198 const Function *F = I.getParent()->getParent();
1199
1200 // Emit a store of the return value through the virtual register.
1201 // Leave Outs empty so that LowerReturn won't try to load return
1202 // registers the usual way.
1203 SmallVector<EVT, 1> PtrValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00001204 ComputeValueVTs(*TLI, PointerType::getUnqual(F->getReturnType()),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001205 PtrValueVTs);
1206
1207 SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]);
1208 SDValue RetOp = getValue(I.getOperand(0));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001209
Owen Andersone50ed302009-08-10 22:56:29 +00001210 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001211 SmallVector<uint64_t, 4> Offsets;
Bill Wendlingba54bca2013-06-19 21:36:55 +00001212 ComputeValueVTs(*TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets);
Dan Gohman7ea1ca62008-10-21 20:00:42 +00001213 unsigned NumValues = ValueVTs.size();
Dan Gohman7ea1ca62008-10-21 20:00:42 +00001214
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001215 SmallVector<SDValue, 4> Chains(NumValues);
Bill Wendling87710f02009-12-21 23:47:40 +00001216 for (unsigned i = 0; i != NumValues; ++i) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001217 SDValue Add = DAG.getNode(ISD::ADD, getCurSDLoc(),
Chris Lattnera13b8602010-08-24 23:10:06 +00001218 RetPtr.getValueType(), RetPtr,
1219 DAG.getIntPtrConstant(Offsets[i]));
Bill Wendling87710f02009-12-21 23:47:40 +00001220 Chains[i] =
Andrew Trickac6d9be2013-05-25 02:42:55 +00001221 DAG.getStore(Chain, getCurSDLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001222 SDValue(RetOp.getNode(), RetOp.getResNo() + i),
Chris Lattner84bd98a2010-09-21 18:58:22 +00001223 // FIXME: better loc info would be nice.
1224 Add, MachinePointerInfo(), false, false, 0);
Bill Wendling87710f02009-12-21 23:47:40 +00001225 }
1226
Andrew Trickac6d9be2013-05-25 02:42:55 +00001227 Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001228 MVT::Other, &Chains[0], NumValues);
Chris Lattner25d58372010-02-28 18:53:13 +00001229 } else if (I.getNumOperands() != 0) {
1230 SmallVector<EVT, 4> ValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00001231 ComputeValueVTs(*TLI, I.getOperand(0)->getType(), ValueVTs);
Chris Lattner25d58372010-02-28 18:53:13 +00001232 unsigned NumValues = ValueVTs.size();
1233 if (NumValues) {
1234 SDValue RetOp = getValue(I.getOperand(0));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001235 for (unsigned j = 0, f = NumValues; j != f; ++j) {
1236 EVT VT = ValueVTs[j];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001237
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001238 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001239
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001240 const Function *F = I.getParent()->getParent();
Bill Wendling8b62abd2012-12-30 13:01:51 +00001241 if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
1242 Attribute::SExt))
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001243 ExtendKind = ISD::SIGN_EXTEND;
Bill Wendling8b62abd2012-12-30 13:01:51 +00001244 else if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
1245 Attribute::ZExt))
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001246 ExtendKind = ISD::ZERO_EXTEND;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001247
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001248 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger())
Bill Wendlingba54bca2013-06-19 21:36:55 +00001249 VT = TLI->getTypeForExtArgOrReturn(VT.getSimpleVT(), ExtendKind);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001250
Bill Wendlingba54bca2013-06-19 21:36:55 +00001251 unsigned NumParts = TLI->getNumRegisters(*DAG.getContext(), VT);
1252 MVT PartVT = TLI->getRegisterType(*DAG.getContext(), VT);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001253 SmallVector<SDValue, 4> Parts(NumParts);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001254 getCopyToParts(DAG, getCurSDLoc(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001255 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
Bill Wendlingf18eb582012-09-26 06:16:18 +00001256 &Parts[0], NumParts, PartVT, &I, ExtendKind);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001257
1258 // 'inreg' on function refers to return value
1259 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
Bill Wendling8b62abd2012-12-30 13:01:51 +00001260 if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
1261 Attribute::InReg))
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001262 Flags.setInReg();
1263
1264 // Propagate extension type if any
Cameron Zwarich8df6bf52011-03-16 22:20:07 +00001265 if (ExtendKind == ISD::SIGN_EXTEND)
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001266 Flags.setSExt();
Cameron Zwarich8df6bf52011-03-16 22:20:07 +00001267 else if (ExtendKind == ISD::ZERO_EXTEND)
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001268 Flags.setZExt();
1269
Dan Gohmanc9403652010-07-07 15:54:55 +00001270 for (unsigned i = 0; i < NumParts; ++i) {
1271 Outs.push_back(ISD::OutputArg(Flags, Parts[i].getValueType(),
Manman Ren0a1544d2012-11-01 23:49:58 +00001272 /*isfixed=*/true, 0, 0));
Dan Gohmanc9403652010-07-07 15:54:55 +00001273 OutVals.push_back(Parts[i]);
1274 }
Evan Cheng3927f432009-03-25 20:20:11 +00001275 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001276 }
1277 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001278
1279 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001280 CallingConv::ID CallConv =
1281 DAG.getMachineFunction().getFunction()->getCallingConv();
Bill Wendlingba54bca2013-06-19 21:36:55 +00001282 Chain = TM.getTargetLowering()->LowerReturn(Chain, CallConv, isVarArg,
1283 Outs, OutVals, getCurSDLoc(),
1284 DAG);
Dan Gohman5e866062009-08-06 15:37:27 +00001285
1286 // Verify that the target's LowerReturn behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00001287 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00001288 "LowerReturn didn't return a valid chain!");
1289
1290 // Update the DAG with the new chain value resulting from return lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001291 DAG.setRoot(Chain);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001292}
1293
Dan Gohmanad62f532009-04-23 23:13:24 +00001294/// CopyToExportRegsIfNeeded - If the given value has virtual registers
1295/// created for it, emit nodes to copy the value into the virtual
1296/// registers.
Dan Gohman46510a72010-04-15 01:51:59 +00001297void SelectionDAGBuilder::CopyToExportRegsIfNeeded(const Value *V) {
Rafael Espindola3fa82832011-05-13 15:18:06 +00001298 // Skip empty types
1299 if (V->getType()->isEmptyTy())
1300 return;
1301
Dan Gohman33b7a292010-04-16 17:15:02 +00001302 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
1303 if (VMI != FuncInfo.ValueMap.end()) {
1304 assert(!V->use_empty() && "Unused value assigned virtual registers!");
1305 CopyValueToVirtualRegister(V, VMI->second);
Dan Gohmanad62f532009-04-23 23:13:24 +00001306 }
1307}
1308
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001309/// ExportFromCurrentBlock - If this condition isn't known to be exported from
1310/// the current basic block, add it to ValueMap now so that we'll get a
1311/// CopyTo/FromReg.
Dan Gohman46510a72010-04-15 01:51:59 +00001312void SelectionDAGBuilder::ExportFromCurrentBlock(const Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001313 // No need to export constants.
1314 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001315
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001316 // Already exported?
1317 if (FuncInfo.isExportedInst(V)) return;
1318
1319 unsigned Reg = FuncInfo.InitializeRegForValue(V);
1320 CopyValueToVirtualRegister(V, Reg);
1321}
1322
Dan Gohman46510a72010-04-15 01:51:59 +00001323bool SelectionDAGBuilder::isExportableFromCurrentBlock(const Value *V,
Dan Gohman2048b852009-11-23 18:04:58 +00001324 const BasicBlock *FromBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001325 // The operands of the setcc have to be in this block. We don't know
1326 // how to export them from some other block.
Dan Gohman46510a72010-04-15 01:51:59 +00001327 if (const Instruction *VI = dyn_cast<Instruction>(V)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001328 // Can export from current BB.
1329 if (VI->getParent() == FromBB)
1330 return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001331
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001332 // Is already exported, noop.
1333 return FuncInfo.isExportedInst(V);
1334 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001335
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001336 // If this is an argument, we can export it if the BB is the entry block or
1337 // if it is already exported.
1338 if (isa<Argument>(V)) {
1339 if (FromBB == &FromBB->getParent()->getEntryBlock())
1340 return true;
1341
1342 // Otherwise, can only export this if it is already exported.
1343 return FuncInfo.isExportedInst(V);
1344 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001345
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001346 // Otherwise, constants can always be exported.
1347 return true;
1348}
1349
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001350/// Return branch probability calculated by BranchProbabilityInfo for IR blocks.
Jakub Staszak25101bb2011-12-20 20:03:10 +00001351uint32_t SelectionDAGBuilder::getEdgeWeight(const MachineBasicBlock *Src,
1352 const MachineBasicBlock *Dst) const {
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001353 BranchProbabilityInfo *BPI = FuncInfo.BPI;
1354 if (!BPI)
1355 return 0;
Jakub Staszak95ece8e2011-07-29 20:05:36 +00001356 const BasicBlock *SrcBB = Src->getBasicBlock();
1357 const BasicBlock *DstBB = Dst->getBasicBlock();
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001358 return BPI->getEdgeWeight(SrcBB, DstBB);
1359}
1360
Jakub Staszakc8f34de2011-07-29 22:25:21 +00001361void SelectionDAGBuilder::
1362addSuccessorWithWeight(MachineBasicBlock *Src, MachineBasicBlock *Dst,
1363 uint32_t Weight /* = 0 */) {
1364 if (!Weight)
1365 Weight = getEdgeWeight(Src, Dst);
1366 Src->addSuccessor(Dst, Weight);
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001367}
1368
1369
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001370static bool InBlock(const Value *V, const BasicBlock *BB) {
1371 if (const Instruction *I = dyn_cast<Instruction>(V))
1372 return I->getParent() == BB;
1373 return true;
1374}
1375
Dan Gohmanc2277342008-10-17 21:16:08 +00001376/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
1377/// This function emits a branch and is used at the leaves of an OR or an
1378/// AND operator tree.
1379///
1380void
Dan Gohman46510a72010-04-15 01:51:59 +00001381SelectionDAGBuilder::EmitBranchForMergedCondition(const Value *Cond,
Dan Gohman2048b852009-11-23 18:04:58 +00001382 MachineBasicBlock *TBB,
1383 MachineBasicBlock *FBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001384 MachineBasicBlock *CurBB,
1385 MachineBasicBlock *SwitchBB) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001386 const BasicBlock *BB = CurBB->getBasicBlock();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001387
Dan Gohmanc2277342008-10-17 21:16:08 +00001388 // If the leaf of the tree is a comparison, merge the condition into
1389 // the caseblock.
Dan Gohman46510a72010-04-15 01:51:59 +00001390 if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001391 // The operands of the cmp have to be in this block. We don't know
1392 // how to export them from some other block. If this is the first block
1393 // of the sequence, no exporting is needed.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001394 if (CurBB == SwitchBB ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001395 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
1396 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001397 ISD::CondCode Condition;
Dan Gohman46510a72010-04-15 01:51:59 +00001398 if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001399 Condition = getICmpCondCode(IC->getPredicate());
Dan Gohman46510a72010-04-15 01:51:59 +00001400 } else if (const FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001401 Condition = getFCmpCondCode(FC->getPredicate());
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001402 if (TM.Options.NoNaNsFPMath)
1403 Condition = getFCmpCodeWithoutNaN(Condition);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001404 } else {
1405 Condition = ISD::SETEQ; // silence warning.
Torok Edwinc23197a2009-07-14 16:55:14 +00001406 llvm_unreachable("Unknown compare instruction");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001407 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001408
1409 CaseBlock CB(Condition, BOp->getOperand(0),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001410 BOp->getOperand(1), NULL, TBB, FBB, CurBB);
1411 SwitchCases.push_back(CB);
1412 return;
1413 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001414 }
1415
1416 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001417 CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanc2277342008-10-17 21:16:08 +00001418 NULL, TBB, FBB, CurBB);
1419 SwitchCases.push_back(CB);
1420}
1421
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001422/// FindMergedConditions - If Cond is an expression like
Dan Gohman46510a72010-04-15 01:51:59 +00001423void SelectionDAGBuilder::FindMergedConditions(const Value *Cond,
Dan Gohman2048b852009-11-23 18:04:58 +00001424 MachineBasicBlock *TBB,
1425 MachineBasicBlock *FBB,
1426 MachineBasicBlock *CurBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001427 MachineBasicBlock *SwitchBB,
Dan Gohman2048b852009-11-23 18:04:58 +00001428 unsigned Opc) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001429 // If this node is not part of the or/and tree, emit it as a branch.
Dan Gohman46510a72010-04-15 01:51:59 +00001430 const Instruction *BOp = dyn_cast<Instruction>(Cond);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001431 if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001432 (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
1433 BOp->getParent() != CurBB->getBasicBlock() ||
1434 !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1435 !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001436 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001437 return;
1438 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001439
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001440 // Create TmpBB after CurBB.
1441 MachineFunction::iterator BBI = CurBB;
1442 MachineFunction &MF = DAG.getMachineFunction();
1443 MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
1444 CurBB->getParent()->insert(++BBI, TmpBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001445
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001446 if (Opc == Instruction::Or) {
1447 // Codegen X | Y as:
1448 // jmp_if_X TBB
1449 // jmp TmpBB
1450 // TmpBB:
1451 // jmp_if_Y TBB
1452 // jmp FBB
1453 //
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001454
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001455 // Emit the LHS condition.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001456 FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, SwitchBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001457
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001458 // Emit the RHS condition into TmpBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001459 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001460 } else {
1461 assert(Opc == Instruction::And && "Unknown merge op!");
1462 // Codegen X & Y as:
1463 // jmp_if_X TmpBB
1464 // jmp FBB
1465 // TmpBB:
1466 // jmp_if_Y TBB
1467 // jmp FBB
1468 //
1469 // This requires creation of TmpBB after CurBB.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001470
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001471 // Emit the LHS condition.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001472 FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, SwitchBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001473
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001474 // Emit the RHS condition into TmpBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001475 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001476 }
1477}
1478
1479/// If the set of cases should be emitted as a series of branches, return true.
1480/// If we should emit this as a bunch of and/or'd together conditions, return
1481/// false.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001482bool
Stephen Lin09f8ca32013-07-06 21:44:25 +00001483SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001484 if (Cases.size() != 2) return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001485
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001486 // If this is two comparisons of the same values or'd or and'd together, they
1487 // will get folded into a single comparison, so don't emit two blocks.
1488 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1489 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1490 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1491 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1492 return false;
1493 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001494
Chris Lattner133ce872010-01-02 00:00:03 +00001495 // Handle: (X != null) | (Y != null) --> (X|Y) != 0
1496 // Handle: (X == null) & (Y == null) --> (X|Y) == 0
1497 if (Cases[0].CmpRHS == Cases[1].CmpRHS &&
1498 Cases[0].CC == Cases[1].CC &&
1499 isa<Constant>(Cases[0].CmpRHS) &&
1500 cast<Constant>(Cases[0].CmpRHS)->isNullValue()) {
1501 if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB)
1502 return false;
1503 if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB)
1504 return false;
1505 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00001506
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001507 return true;
1508}
1509
Dan Gohman46510a72010-04-15 01:51:59 +00001510void SelectionDAGBuilder::visitBr(const BranchInst &I) {
Dan Gohman84023e02010-07-10 09:00:22 +00001511 MachineBasicBlock *BrMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001512
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001513 // Update machine-CFG edges.
1514 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1515
1516 // Figure out which block is immediately after the current one.
1517 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001518 MachineFunction::iterator BBI = BrMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001519 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001520 NextBlock = BBI;
1521
1522 if (I.isUnconditional()) {
1523 // Update machine-CFG edges.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001524 BrMBB->addSuccessor(Succ0MBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001525
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001526 // If this is not a fall-through branch, emit the branch.
Bill Wendling4533cac2010-01-28 21:51:40 +00001527 if (Succ0MBB != NextBlock)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001528 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001529 MVT::Other, getControlRoot(),
Bill Wendling4533cac2010-01-28 21:51:40 +00001530 DAG.getBasicBlock(Succ0MBB)));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001531
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001532 return;
1533 }
1534
1535 // If this condition is one of the special cases we handle, do special stuff
1536 // now.
Dan Gohman46510a72010-04-15 01:51:59 +00001537 const Value *CondVal = I.getCondition();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001538 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1539
1540 // If this is a series of conditions that are or'd or and'd together, emit
1541 // this as a sequence of branches instead of setcc's with and/or operations.
Chris Lattnerde189be2010-11-30 18:12:52 +00001542 // As long as jumps are not expensive, this should improve performance.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001543 // For example, instead of something like:
1544 // cmp A, B
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001545 // C = seteq
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001546 // cmp D, E
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001547 // F = setle
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001548 // or C, F
1549 // jnz foo
1550 // Emit:
1551 // cmp A, B
1552 // je foo
1553 // cmp D, E
1554 // jle foo
1555 //
Dan Gohman46510a72010-04-15 01:51:59 +00001556 if (const BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00001557 if (!TM.getTargetLowering()->isJumpExpensive() &&
Chris Lattnerde189be2010-11-30 18:12:52 +00001558 BOp->hasOneUse() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001559 (BOp->getOpcode() == Instruction::And ||
1560 BOp->getOpcode() == Instruction::Or)) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001561 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, BrMBB, BrMBB,
1562 BOp->getOpcode());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001563 // If the compares in later blocks need to use values not currently
1564 // exported from this block, export them now. This block should always
1565 // be the first entry.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001566 assert(SwitchCases[0].ThisBB == BrMBB && "Unexpected lowering!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001567
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001568 // Allow some cases to be rejected.
1569 if (ShouldEmitAsBranches(SwitchCases)) {
1570 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1571 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1572 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1573 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001574
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001575 // Emit the branch for this block.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001576 visitSwitchCase(SwitchCases[0], BrMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001577 SwitchCases.erase(SwitchCases.begin());
1578 return;
1579 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001580
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001581 // Okay, we decided not to do this, remove any inserted MBB's and clear
1582 // SwitchCases.
1583 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001584 FuncInfo.MF->erase(SwitchCases[i].ThisBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001585
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001586 SwitchCases.clear();
1587 }
1588 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001589
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001590 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001591 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohman99be8ae2010-04-19 22:41:47 +00001592 NULL, Succ0MBB, Succ1MBB, BrMBB);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001593
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001594 // Use visitSwitchCase to actually insert the fast branch sequence for this
1595 // cond branch.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001596 visitSwitchCase(CB, BrMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001597}
1598
1599/// visitSwitchCase - Emits the necessary code to represent a single node in
1600/// the binary search tree resulting from lowering a switch instruction.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001601void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB,
1602 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001603 SDValue Cond;
1604 SDValue CondLHS = getValue(CB.CmpLHS);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001605 SDLoc dl = getCurSDLoc();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001606
1607 // Build the setcc now.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001608 if (CB.CmpMHS == NULL) {
1609 // Fold "(X == true)" to X and "(X == false)" to !X to
1610 // handle common cases produced by branch lowering.
Owen Anderson5defacc2009-07-31 17:39:07 +00001611 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001612 CB.CC == ISD::SETEQ)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001613 Cond = CondLHS;
Owen Anderson5defacc2009-07-31 17:39:07 +00001614 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001615 CB.CC == ISD::SETEQ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001616 SDValue True = DAG.getConstant(1, CondLHS.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001617 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001618 } else
Owen Anderson825b72b2009-08-11 20:47:22 +00001619 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001620 } else {
Stepan Dyatkovskiyc187df22012-05-17 08:56:30 +00001621 assert(CB.CC == ISD::SETCC_INVALID &&
1622 "Condition is undefined for to-the-range belonging check.");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001623
Anton Korobeynikov23218582008-12-23 22:25:27 +00001624 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
1625 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001626
1627 SDValue CmpOp = getValue(CB.CmpMHS);
Owen Andersone50ed302009-08-10 22:56:29 +00001628 EVT VT = CmpOp.getValueType();
Stephen Lin155615d2013-07-08 00:37:03 +00001629
Stepan Dyatkovskiyc187df22012-05-17 08:56:30 +00001630 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(false)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001631 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT),
Stepan Dyatkovskiyc187df22012-05-17 08:56:30 +00001632 ISD::SETULE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001633 } else {
Dale Johannesenf5d97892009-02-04 01:48:28 +00001634 SDValue SUB = DAG.getNode(ISD::SUB, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001635 VT, CmpOp, DAG.getConstant(Low, VT));
Owen Anderson825b72b2009-08-11 20:47:22 +00001636 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001637 DAG.getConstant(High-Low, VT), ISD::SETULE);
1638 }
1639 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001640
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001641 // Update successor info
Jakub Staszakc8f34de2011-07-29 22:25:21 +00001642 addSuccessorWithWeight(SwitchBB, CB.TrueBB, CB.TrueWeight);
Jakob Stoklund Olesene7fdef42012-08-20 21:39:52 +00001643 // TrueBB and FalseBB are always different unless the incoming IR is
1644 // degenerate. This only happens when running llc on weird IR.
1645 if (CB.TrueBB != CB.FalseBB)
1646 addSuccessorWithWeight(SwitchBB, CB.FalseBB, CB.FalseWeight);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001647
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001648 // Set NextBlock to be the MBB immediately after the current one, if any.
1649 // This is used to avoid emitting unnecessary branches to the next block.
1650 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001651 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001652 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001653 NextBlock = BBI;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001654
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001655 // If the lhs block is the next block, invert the condition so that we can
1656 // fall through to the lhs instead of the rhs block.
1657 if (CB.TrueBB == NextBlock) {
1658 std::swap(CB.TrueBB, CB.FalseBB);
1659 SDValue True = DAG.getConstant(1, Cond.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001660 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001661 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001662
Dale Johannesenf5d97892009-02-04 01:48:28 +00001663 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001664 MVT::Other, getControlRoot(), Cond,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001665 DAG.getBasicBlock(CB.TrueBB));
Bill Wendling87710f02009-12-21 23:47:40 +00001666
Evan Cheng266a99d2010-09-23 06:51:55 +00001667 // Insert the false branch. Do this even if it's a fall through branch,
1668 // this makes it easier to do DAG optimizations which require inverting
1669 // the branch condition.
1670 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
1671 DAG.getBasicBlock(CB.FalseBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001672
1673 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001674}
1675
1676/// visitJumpTable - Emit JumpTable node in the current MBB
Dan Gohman2048b852009-11-23 18:04:58 +00001677void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001678 // Emit the code for the jump table
1679 assert(JT.Reg != -1U && "Should lower JT Header first!");
Bill Wendlingba54bca2013-06-19 21:36:55 +00001680 EVT PTy = TM.getTargetLowering()->getPointerTy();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001681 SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurSDLoc(),
Dale Johannesena04b7572009-02-03 23:04:43 +00001682 JT.Reg, PTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001683 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001684 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurSDLoc(),
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001685 MVT::Other, Index.getValue(1),
1686 Table, Index);
1687 DAG.setRoot(BrJumpTable);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001688}
1689
1690/// visitJumpTableHeader - This function emits necessary code to produce index
1691/// in the JumpTable from switch case.
Dan Gohman2048b852009-11-23 18:04:58 +00001692void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001693 JumpTableHeader &JTH,
1694 MachineBasicBlock *SwitchBB) {
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001695 // Subtract the lowest switch case value from the value being switched on and
1696 // conditional branch to default mbb if the result is greater than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001697 // difference between smallest and largest cases.
1698 SDValue SwitchOp = getValue(JTH.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001699 EVT VT = SwitchOp.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001700 SDValue Sub = DAG.getNode(ISD::SUB, getCurSDLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001701 DAG.getConstant(JTH.First, VT));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001702
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001703 // The SDNode we just created, which holds the value being switched on minus
Dan Gohmanf451cb82010-02-10 16:03:48 +00001704 // the smallest case value, needs to be copied to a virtual register so it
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001705 // can be used as an index into the jump table in a subsequent basic block.
1706 // This value may be smaller or larger than the target's pointer type, and
1707 // therefore require extension or truncating.
Bill Wendlingba54bca2013-06-19 21:36:55 +00001708 const TargetLowering *TLI = TM.getTargetLowering();
1709 SwitchOp = DAG.getZExtOrTrunc(Sub, getCurSDLoc(), TLI->getPointerTy());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001710
Bill Wendlingba54bca2013-06-19 21:36:55 +00001711 unsigned JumpTableReg = FuncInfo.CreateReg(TLI->getPointerTy());
Andrew Trickac6d9be2013-05-25 02:42:55 +00001712 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurSDLoc(),
Dale Johannesena04b7572009-02-03 23:04:43 +00001713 JumpTableReg, SwitchOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001714 JT.Reg = JumpTableReg;
1715
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001716 // Emit the range check for the jump table, and branch to the default block
1717 // for the switch statement if the value being switched on exceeds the largest
1718 // case in the switch.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001719 SDValue CMP = DAG.getSetCC(getCurSDLoc(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00001720 TLI->getSetCCResultType(*DAG.getContext(),
1721 Sub.getValueType()),
Matt Arsenault225ed702013-05-18 00:21:46 +00001722 Sub,
1723 DAG.getConstant(JTH.Last - JTH.First,VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001724 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001725
1726 // Set NextBlock to be the MBB immediately after the current one, if any.
1727 // This is used to avoid emitting unnecessary branches to the next block.
1728 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001729 MachineFunction::iterator BBI = SwitchBB;
Bill Wendling87710f02009-12-21 23:47:40 +00001730
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001731 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001732 NextBlock = BBI;
1733
Andrew Trickac6d9be2013-05-25 02:42:55 +00001734 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurSDLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001735 MVT::Other, CopyTo, CMP,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001736 DAG.getBasicBlock(JT.Default));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001737
Bill Wendling4533cac2010-01-28 21:51:40 +00001738 if (JT.MBB != NextBlock)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001739 BrCond = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, BrCond,
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001740 DAG.getBasicBlock(JT.MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001741
Bill Wendling87710f02009-12-21 23:47:40 +00001742 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001743}
1744
Michael Gottesman657484f2013-08-20 07:00:16 +00001745/// Codegen a new tail for a stack protector check ParentMBB which has had its
1746/// tail spliced into a stack protector check success bb.
1747///
1748/// For a high level explanation of how this fits into the stack protector
1749/// generation see the comment on the declaration of class
1750/// StackProtectorDescriptor.
1751void SelectionDAGBuilder::visitSPDescriptorParent(StackProtectorDescriptor &SPD,
1752 MachineBasicBlock *ParentBB) {
1753
1754 // First create the loads to the guard/stack slot for the comparison.
1755 const TargetLowering *TLI = TM.getTargetLowering();
1756 EVT PtrTy = TLI->getPointerTy();
1757
1758 MachineFrameInfo *MFI = ParentBB->getParent()->getFrameInfo();
1759 int FI = MFI->getStackProtectorIndex();
1760
1761 const Value *IRGuard = SPD.getGuard();
1762 SDValue GuardPtr = getValue(IRGuard);
1763 SDValue StackSlotPtr = DAG.getFrameIndex(FI, PtrTy);
1764
1765 unsigned Align =
1766 TLI->getDataLayout()->getPrefTypeAlignment(IRGuard->getType());
1767 SDValue Guard = DAG.getLoad(PtrTy, getCurSDLoc(), DAG.getEntryNode(),
1768 GuardPtr, MachinePointerInfo(IRGuard, 0),
1769 true, false, false, Align);
1770
1771 SDValue StackSlot = DAG.getLoad(PtrTy, getCurSDLoc(), DAG.getEntryNode(),
1772 StackSlotPtr,
1773 MachinePointerInfo::getFixedStack(FI),
1774 true, false, false, Align);
1775
1776 // Perform the comparison via a subtract/getsetcc.
1777 EVT VT = Guard.getValueType();
1778 SDValue Sub = DAG.getNode(ISD::SUB, getCurSDLoc(), VT, Guard, StackSlot);
1779
1780 SDValue Cmp = DAG.getSetCC(getCurSDLoc(),
1781 TLI->getSetCCResultType(*DAG.getContext(),
1782 Sub.getValueType()),
1783 Sub, DAG.getConstant(0, VT),
1784 ISD::SETNE);
1785
1786 // If the sub is not 0, then we know the guard/stackslot do not equal, so
1787 // branch to failure MBB.
1788 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurSDLoc(),
1789 MVT::Other, StackSlot.getOperand(0),
1790 Cmp, DAG.getBasicBlock(SPD.getFailureMBB()));
1791 // Otherwise branch to success MBB.
1792 SDValue Br = DAG.getNode(ISD::BR, getCurSDLoc(),
1793 MVT::Other, BrCond,
1794 DAG.getBasicBlock(SPD.getSuccessMBB()));
1795
1796 DAG.setRoot(Br);
1797}
1798
1799/// Codegen the failure basic block for a stack protector check.
1800///
1801/// A failure stack protector machine basic block consists simply of a call to
1802/// __stack_chk_fail().
1803///
1804/// For a high level explanation of how this fits into the stack protector
1805/// generation see the comment on the declaration of class
1806/// StackProtectorDescriptor.
1807void
1808SelectionDAGBuilder::visitSPDescriptorFailure(StackProtectorDescriptor &SPD) {
1809 const TargetLowering *TLI = TM.getTargetLowering();
1810 SDValue Chain = TLI->makeLibCall(DAG, RTLIB::STACKPROTECTOR_CHECK_FAIL,
1811 MVT::isVoid, 0, 0, false, getCurSDLoc(),
Michael Gottesman58a9b432013-08-22 23:45:24 +00001812 false, false).second;
Michael Gottesman657484f2013-08-20 07:00:16 +00001813 DAG.setRoot(Chain);
1814}
1815
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001816/// visitBitTestHeader - This function emits necessary code to produce value
1817/// suitable for "bit tests"
Dan Gohman99be8ae2010-04-19 22:41:47 +00001818void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B,
1819 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001820 // Subtract the minimum value
1821 SDValue SwitchOp = getValue(B.SValue);
Patrik Hagglund34525f92012-12-11 11:14:33 +00001822 EVT VT = SwitchOp.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001823 SDValue Sub = DAG.getNode(ISD::SUB, getCurSDLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001824 DAG.getConstant(B.First, VT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001825
1826 // Check range
Bill Wendlingba54bca2013-06-19 21:36:55 +00001827 const TargetLowering *TLI = TM.getTargetLowering();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001828 SDValue RangeCmp = DAG.getSetCC(getCurSDLoc(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00001829 TLI->getSetCCResultType(*DAG.getContext(),
Matt Arsenault225ed702013-05-18 00:21:46 +00001830 Sub.getValueType()),
Bill Wendling87710f02009-12-21 23:47:40 +00001831 Sub, DAG.getConstant(B.Range, VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001832 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001833
Evan Chengd08e5b42011-01-06 01:02:44 +00001834 // Determine the type of the test operands.
1835 bool UsePtrType = false;
Bill Wendlingba54bca2013-06-19 21:36:55 +00001836 if (!TLI->isTypeLegal(VT))
Evan Chengd08e5b42011-01-06 01:02:44 +00001837 UsePtrType = true;
1838 else {
1839 for (unsigned i = 0, e = B.Cases.size(); i != e; ++i)
Eli Friedman5c75af62011-10-12 22:46:45 +00001840 if (!isUIntN(VT.getSizeInBits(), B.Cases[i].Mask)) {
Evan Chengd08e5b42011-01-06 01:02:44 +00001841 // Switch table case range are encoded into series of masks.
1842 // Just use pointer type, it's guaranteed to fit.
1843 UsePtrType = true;
1844 break;
1845 }
1846 }
1847 if (UsePtrType) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00001848 VT = TLI->getPointerTy();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001849 Sub = DAG.getZExtOrTrunc(Sub, getCurSDLoc(), VT);
Evan Chengd08e5b42011-01-06 01:02:44 +00001850 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001851
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00001852 B.RegVT = VT.getSimpleVT();
Patrik Hagglund8963fec2012-12-19 12:23:01 +00001853 B.Reg = FuncInfo.CreateReg(B.RegVT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001854 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurSDLoc(),
Evan Chengd08e5b42011-01-06 01:02:44 +00001855 B.Reg, Sub);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001856
1857 // Set NextBlock to be the MBB immediately after the current one, if any.
1858 // This is used to avoid emitting unnecessary branches to the next block.
1859 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001860 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001861 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001862 NextBlock = BBI;
1863
1864 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1865
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001866 addSuccessorWithWeight(SwitchBB, B.Default);
1867 addSuccessorWithWeight(SwitchBB, MBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001868
Andrew Trickac6d9be2013-05-25 02:42:55 +00001869 SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurSDLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001870 MVT::Other, CopyTo, RangeCmp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001871 DAG.getBasicBlock(B.Default));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001872
Evan Cheng8c1f4322010-09-23 18:32:19 +00001873 if (MBB != NextBlock)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001874 BrRange = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, CopyTo,
Evan Cheng8c1f4322010-09-23 18:32:19 +00001875 DAG.getBasicBlock(MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001876
Bill Wendling87710f02009-12-21 23:47:40 +00001877 DAG.setRoot(BrRange);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001878}
1879
1880/// visitBitTestCase - this function produces one "bit test"
Evan Chengd08e5b42011-01-06 01:02:44 +00001881void SelectionDAGBuilder::visitBitTestCase(BitTestBlock &BB,
1882 MachineBasicBlock* NextMBB,
Manman Ren1a710fd2012-08-24 18:14:27 +00001883 uint32_t BranchWeightToNext,
Dan Gohman2048b852009-11-23 18:04:58 +00001884 unsigned Reg,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001885 BitTestCase &B,
1886 MachineBasicBlock *SwitchBB) {
Patrik Hagglund8963fec2012-12-19 12:23:01 +00001887 MVT VT = BB.RegVT;
Andrew Trickac6d9be2013-05-25 02:42:55 +00001888 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurSDLoc(),
Evan Chengd08e5b42011-01-06 01:02:44 +00001889 Reg, VT);
Dan Gohman8e0163a2010-06-24 02:06:24 +00001890 SDValue Cmp;
Benjamin Kramer3ff25512011-07-14 01:38:42 +00001891 unsigned PopCount = CountPopulation_64(B.Mask);
Bill Wendlingba54bca2013-06-19 21:36:55 +00001892 const TargetLowering *TLI = TM.getTargetLowering();
Benjamin Kramer3ff25512011-07-14 01:38:42 +00001893 if (PopCount == 1) {
Dan Gohman8e0163a2010-06-24 02:06:24 +00001894 // Testing for a single bit; just compare the shift count with what it
1895 // would need to be to shift a 1 bit in that position.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001896 Cmp = DAG.getSetCC(getCurSDLoc(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00001897 TLI->getSetCCResultType(*DAG.getContext(), VT),
Dan Gohman8e0163a2010-06-24 02:06:24 +00001898 ShiftOp,
Michael J. Spencerc6af2432013-05-24 22:23:49 +00001899 DAG.getConstant(countTrailingZeros(B.Mask), VT),
Dan Gohman8e0163a2010-06-24 02:06:24 +00001900 ISD::SETEQ);
Benjamin Kramer3ff25512011-07-14 01:38:42 +00001901 } else if (PopCount == BB.Range) {
1902 // There is only one zero bit in the range, test for it directly.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001903 Cmp = DAG.getSetCC(getCurSDLoc(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00001904 TLI->getSetCCResultType(*DAG.getContext(), VT),
Benjamin Kramer3ff25512011-07-14 01:38:42 +00001905 ShiftOp,
1906 DAG.getConstant(CountTrailingOnes_64(B.Mask), VT),
1907 ISD::SETNE);
Dan Gohman8e0163a2010-06-24 02:06:24 +00001908 } else {
1909 // Make desired shift
Andrew Trickac6d9be2013-05-25 02:42:55 +00001910 SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurSDLoc(), VT,
Evan Chengd08e5b42011-01-06 01:02:44 +00001911 DAG.getConstant(1, VT), ShiftOp);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001912
Dan Gohman8e0163a2010-06-24 02:06:24 +00001913 // Emit bit tests and jumps
Andrew Trickac6d9be2013-05-25 02:42:55 +00001914 SDValue AndOp = DAG.getNode(ISD::AND, getCurSDLoc(),
Evan Chengd08e5b42011-01-06 01:02:44 +00001915 VT, SwitchVal, DAG.getConstant(B.Mask, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00001916 Cmp = DAG.getSetCC(getCurSDLoc(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00001917 TLI->getSetCCResultType(*DAG.getContext(), VT),
Evan Chengd08e5b42011-01-06 01:02:44 +00001918 AndOp, DAG.getConstant(0, VT),
Dan Gohman8e0163a2010-06-24 02:06:24 +00001919 ISD::SETNE);
1920 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001921
Manman Ren1a710fd2012-08-24 18:14:27 +00001922 // The branch weight from SwitchBB to B.TargetBB is B.ExtraWeight.
1923 addSuccessorWithWeight(SwitchBB, B.TargetBB, B.ExtraWeight);
1924 // The branch weight from SwitchBB to NextMBB is BranchWeightToNext.
1925 addSuccessorWithWeight(SwitchBB, NextMBB, BranchWeightToNext);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001926
Andrew Trickac6d9be2013-05-25 02:42:55 +00001927 SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurSDLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001928 MVT::Other, getControlRoot(),
Dan Gohman8e0163a2010-06-24 02:06:24 +00001929 Cmp, DAG.getBasicBlock(B.TargetBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001930
1931 // Set NextBlock to be the MBB immediately after the current one, if any.
1932 // This is used to avoid emitting unnecessary branches to the next block.
1933 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001934 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001935 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001936 NextBlock = BBI;
1937
Evan Cheng8c1f4322010-09-23 18:32:19 +00001938 if (NextMBB != NextBlock)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001939 BrAnd = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, BrAnd,
Evan Cheng8c1f4322010-09-23 18:32:19 +00001940 DAG.getBasicBlock(NextMBB));
Bill Wendling0777e922009-12-21 21:59:52 +00001941
Bill Wendling87710f02009-12-21 23:47:40 +00001942 DAG.setRoot(BrAnd);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001943}
1944
Dan Gohman46510a72010-04-15 01:51:59 +00001945void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) {
Dan Gohman84023e02010-07-10 09:00:22 +00001946 MachineBasicBlock *InvokeMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001947
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001948 // Retrieve successors.
1949 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1950 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1951
Gabor Greifb67e6b32009-01-15 11:10:44 +00001952 const Value *Callee(I.getCalledValue());
Nuno Lopes85b40892012-06-28 22:30:12 +00001953 const Function *Fn = dyn_cast<Function>(Callee);
Gabor Greifb67e6b32009-01-15 11:10:44 +00001954 if (isa<InlineAsm>(Callee))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001955 visitInlineAsm(&I);
Nuno Lopes85b40892012-06-28 22:30:12 +00001956 else if (Fn && Fn->isIntrinsic()) {
1957 assert(Fn->getIntrinsicID() == Intrinsic::donothing);
Nuno Lopes4532bf62012-07-18 00:07:17 +00001958 // Ignore invokes to @llvm.donothing: jump directly to the next BB.
Nuno Lopes85b40892012-06-28 22:30:12 +00001959 } else
Gabor Greifb67e6b32009-01-15 11:10:44 +00001960 LowerCallTo(&I, getValue(Callee), false, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001961
1962 // If the value of the invoke is used outside of its defining block, make it
1963 // available as a virtual register.
Dan Gohmanad62f532009-04-23 23:13:24 +00001964 CopyToExportRegsIfNeeded(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001965
1966 // Update successor info
Chandler Carruthf2645682011-11-22 11:37:46 +00001967 addSuccessorWithWeight(InvokeMBB, Return);
1968 addSuccessorWithWeight(InvokeMBB, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001969
1970 // Drop into normal successor.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001971 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00001972 MVT::Other, getControlRoot(),
1973 DAG.getBasicBlock(Return)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001974}
1975
Bill Wendlingdccc03b2011-07-31 06:30:59 +00001976void SelectionDAGBuilder::visitResume(const ResumeInst &RI) {
1977 llvm_unreachable("SelectionDAGBuilder shouldn't visit resume instructions!");
1978}
1979
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00001980void SelectionDAGBuilder::visitLandingPad(const LandingPadInst &LP) {
1981 assert(FuncInfo.MBB->isLandingPad() &&
1982 "Call to landingpad not in landing pad!");
1983
1984 MachineBasicBlock *MBB = FuncInfo.MBB;
1985 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
1986 AddLandingPadInfo(LP, MMI, MBB);
1987
Bill Wendlingbdf9db62012-02-13 23:47:16 +00001988 // If there aren't registers to copy the values into (e.g., during SjLj
1989 // exceptions), then don't bother to create these DAG nodes.
Bill Wendlingba54bca2013-06-19 21:36:55 +00001990 const TargetLowering *TLI = TM.getTargetLowering();
1991 if (TLI->getExceptionPointerRegister() == 0 &&
1992 TLI->getExceptionSelectorRegister() == 0)
Bill Wendlingbdf9db62012-02-13 23:47:16 +00001993 return;
1994
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00001995 SmallVector<EVT, 2> ValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00001996 ComputeValueVTs(*TLI, LP.getType(), ValueVTs);
Jakob Stoklund Olesen918b7c82013-07-04 04:53:45 +00001997 assert(ValueVTs.size() == 2 && "Only two-valued landingpads are supported");
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00001998
Jakob Stoklund Olesen918b7c82013-07-04 04:53:45 +00001999 // Get the two live-in registers as SDValues. The physregs have already been
2000 // copied into virtual registers.
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00002001 SDValue Ops[2];
Jakob Stoklund Olesen918b7c82013-07-04 04:53:45 +00002002 Ops[0] = DAG.getZExtOrTrunc(
2003 DAG.getCopyFromReg(DAG.getEntryNode(), getCurSDLoc(),
2004 FuncInfo.ExceptionPointerVirtReg, TLI->getPointerTy()),
2005 getCurSDLoc(), ValueVTs[0]);
2006 Ops[1] = DAG.getZExtOrTrunc(
2007 DAG.getCopyFromReg(DAG.getEntryNode(), getCurSDLoc(),
2008 FuncInfo.ExceptionSelectorVirtReg, TLI->getPointerTy()),
2009 getCurSDLoc(), ValueVTs[1]);
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00002010
Jakob Stoklund Olesen918b7c82013-07-04 04:53:45 +00002011 // Merge into one.
Andrew Trickac6d9be2013-05-25 02:42:55 +00002012 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00002013 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
2014 &Ops[0], 2);
Jakob Stoklund Olesen918b7c82013-07-04 04:53:45 +00002015 setValue(&LP, Res);
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00002016}
2017
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002018/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
2019/// small case ranges).
Dan Gohman2048b852009-11-23 18:04:58 +00002020bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR,
2021 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00002022 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00002023 MachineBasicBlock *Default,
2024 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002025 // Size is the number of Cases represented by this range.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002026 size_t Size = CR.Range.second - CR.Range.first;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002027 if (Size > 3)
Anton Korobeynikov23218582008-12-23 22:25:27 +00002028 return false;
2029
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002030 // Get the MachineFunction which holds the current MBB. This is used when
2031 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002032 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002033
2034 // Figure out which block is immediately after the current one.
2035 MachineBasicBlock *NextBlock = 0;
2036 MachineFunction::iterator BBI = CR.CaseBB;
2037
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002038 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002039 NextBlock = BBI;
2040
Manman Ren1a710fd2012-08-24 18:14:27 +00002041 BranchProbabilityInfo *BPI = FuncInfo.BPI;
Benjamin Kramerce750f02010-11-22 09:45:38 +00002042 // If any two of the cases has the same destination, and if one value
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002043 // is the same as the other, but has one bit unset that the other has set,
2044 // use bit manipulation to do two compares at once. For example:
2045 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
Benjamin Kramerce750f02010-11-22 09:45:38 +00002046 // TODO: This could be extended to merge any 2 cases in switches with 3 cases.
2047 // TODO: Handle cases where CR.CaseBB != SwitchBB.
2048 if (Size == 2 && CR.CaseBB == SwitchBB) {
2049 Case &Small = *CR.Range.first;
2050 Case &Big = *(CR.Range.second-1);
2051
2052 if (Small.Low == Small.High && Big.Low == Big.High && Small.BB == Big.BB) {
2053 const APInt& SmallValue = cast<ConstantInt>(Small.Low)->getValue();
2054 const APInt& BigValue = cast<ConstantInt>(Big.Low)->getValue();
2055
2056 // Check that there is only one bit different.
2057 if (BigValue.countPopulation() == SmallValue.countPopulation() + 1 &&
2058 (SmallValue | BigValue) == BigValue) {
2059 // Isolate the common bit.
2060 APInt CommonBit = BigValue & ~SmallValue;
2061 assert((SmallValue | CommonBit) == BigValue &&
2062 CommonBit.countPopulation() == 1 && "Not a common bit?");
2063
2064 SDValue CondLHS = getValue(SV);
2065 EVT VT = CondLHS.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00002066 SDLoc DL = getCurSDLoc();
Benjamin Kramerce750f02010-11-22 09:45:38 +00002067
2068 SDValue Or = DAG.getNode(ISD::OR, DL, VT, CondLHS,
2069 DAG.getConstant(CommonBit, VT));
2070 SDValue Cond = DAG.getSetCC(DL, MVT::i1,
2071 Or, DAG.getConstant(BigValue, VT),
2072 ISD::SETEQ);
2073
2074 // Update successor info.
Manman Ren1a710fd2012-08-24 18:14:27 +00002075 // Both Small and Big will jump to Small.BB, so we sum up the weights.
2076 addSuccessorWithWeight(SwitchBB, Small.BB,
2077 Small.ExtraWeight + Big.ExtraWeight);
2078 addSuccessorWithWeight(SwitchBB, Default,
2079 // The default destination is the first successor in IR.
2080 BPI ? BPI->getEdgeWeight(SwitchBB->getBasicBlock(), (unsigned)0) : 0);
Benjamin Kramerce750f02010-11-22 09:45:38 +00002081
2082 // Insert the true branch.
2083 SDValue BrCond = DAG.getNode(ISD::BRCOND, DL, MVT::Other,
2084 getControlRoot(), Cond,
2085 DAG.getBasicBlock(Small.BB));
2086
2087 // Insert the false branch.
2088 BrCond = DAG.getNode(ISD::BR, DL, MVT::Other, BrCond,
2089 DAG.getBasicBlock(Default));
2090
2091 DAG.setRoot(BrCond);
2092 return true;
2093 }
2094 }
2095 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002096
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002097 // Order cases by weight so the most likely case will be checked first.
Manman Ren1a710fd2012-08-24 18:14:27 +00002098 uint32_t UnhandledWeights = 0;
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002099 if (BPI) {
2100 for (CaseItr I = CR.Range.first, IE = CR.Range.second; I != IE; ++I) {
Manman Ren1a710fd2012-08-24 18:14:27 +00002101 uint32_t IWeight = I->ExtraWeight;
2102 UnhandledWeights += IWeight;
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002103 for (CaseItr J = CR.Range.first; J < I; ++J) {
Manman Ren1a710fd2012-08-24 18:14:27 +00002104 uint32_t JWeight = J->ExtraWeight;
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002105 if (IWeight > JWeight)
2106 std::swap(*I, *J);
2107 }
2108 }
2109 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002110 // Rearrange the case blocks so that the last one falls through if possible.
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002111 Case &BackCase = *(CR.Range.second-1);
Benjamin Kramer5db954d2012-05-26 21:19:12 +00002112 if (Size > 1 &&
2113 NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002114 // The last case block won't fall through into 'NextBlock' if we emit the
2115 // branches in this order. See if rearranging a case value would help.
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002116 // We start at the bottom as it's the case with the least weight.
Stephen Lin09f8ca32013-07-06 21:44:25 +00002117 for (Case *I = &*(CR.Range.second-2), *E = &*CR.Range.first-1; I != E; --I)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002118 if (I->BB == NextBlock) {
2119 std::swap(*I, BackCase);
2120 break;
2121 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002122 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002123
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002124 // Create a CaseBlock record representing a conditional branch to
2125 // the Case's target mbb if the value being switched on SV is equal
2126 // to C.
2127 MachineBasicBlock *CurBlock = CR.CaseBB;
2128 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
2129 MachineBasicBlock *FallThrough;
2130 if (I != E-1) {
2131 FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
2132 CurMF->insert(BBI, FallThrough);
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002133
2134 // Put SV in a virtual register to make it available from the new blocks.
2135 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002136 } else {
2137 // If the last case doesn't match, go to the default block.
2138 FallThrough = Default;
2139 }
2140
Dan Gohman46510a72010-04-15 01:51:59 +00002141 const Value *RHS, *LHS, *MHS;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002142 ISD::CondCode CC;
2143 if (I->High == I->Low) {
2144 // This is just small small case range :) containing exactly 1 case
2145 CC = ISD::SETEQ;
2146 LHS = SV; RHS = I->High; MHS = NULL;
2147 } else {
Stephen Lin155615d2013-07-08 00:37:03 +00002148 CC = ISD::SETCC_INVALID;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002149 LHS = I->Low; MHS = SV; RHS = I->High;
2150 }
Jakub Staszakc8f34de2011-07-29 22:25:21 +00002151
Manman Ren1a710fd2012-08-24 18:14:27 +00002152 // The false weight should be sum of all un-handled cases.
2153 UnhandledWeights -= I->ExtraWeight;
Jakub Staszakc8f34de2011-07-29 22:25:21 +00002154 CaseBlock CB(CC, LHS, RHS, MHS, /* truebb */ I->BB, /* falsebb */ FallThrough,
2155 /* me */ CurBlock,
Manman Ren1a710fd2012-08-24 18:14:27 +00002156 /* trueweight */ I->ExtraWeight,
2157 /* falseweight */ UnhandledWeights);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002158
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002159 // If emitting the first comparison, just call visitSwitchCase to emit the
2160 // code into the current block. Otherwise, push the CaseBlock onto the
2161 // vector to be later processed by SDISel, and insert the node's MBB
2162 // before the next MBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002163 if (CurBlock == SwitchBB)
2164 visitSwitchCase(CB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002165 else
2166 SwitchCases.push_back(CB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002167
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002168 CurBlock = FallThrough;
2169 }
2170
2171 return true;
2172}
2173
2174static inline bool areJTsAllowed(const TargetLowering &TLI) {
Evan Cheng769951f2012-07-02 22:39:56 +00002175 return TLI.supportJumpTables() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00002176 (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
2177 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002178}
Anton Korobeynikov23218582008-12-23 22:25:27 +00002179
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002180static APInt ComputeRange(const APInt &First, const APInt &Last) {
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002181 uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1;
Stepan Dyatkovskiyc187df22012-05-17 08:56:30 +00002182 APInt LastExt = Last.zext(BitWidth), FirstExt = First.zext(BitWidth);
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002183 return (LastExt - FirstExt + 1ULL);
2184}
2185
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002186/// handleJTSwitchCase - Emit jumptable for current switch case range
Chris Lattnerc3ab3882011-09-09 22:06:59 +00002187bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec &CR,
2188 CaseRecVector &WorkList,
2189 const Value *SV,
2190 MachineBasicBlock *Default,
Dan Gohman99be8ae2010-04-19 22:41:47 +00002191 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002192 Case& FrontCase = *CR.Range.first;
2193 Case& BackCase = *(CR.Range.second-1);
2194
Chris Lattnere880efe2009-11-07 07:50:34 +00002195 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
2196 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002197
Chris Lattnere880efe2009-11-07 07:50:34 +00002198 APInt TSize(First.getBitWidth(), 0);
Chris Lattnerc3ab3882011-09-09 22:06:59 +00002199 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002200 TSize += I->size();
2201
Bill Wendlingba54bca2013-06-19 21:36:55 +00002202 const TargetLowering *TLI = TM.getTargetLowering();
2203 if (!areJTsAllowed(*TLI) || TSize.ult(TLI->getMinimumJumpTableEntries()))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002204 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002205
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002206 APInt Range = ComputeRange(First, Last);
Jakob Stoklund Olesen79443912011-10-26 01:47:48 +00002207 // The density is TSize / Range. Require at least 40%.
2208 // It should not be possible for IntTSize to saturate for sane code, but make
2209 // sure we handle Range saturation correctly.
2210 uint64_t IntRange = Range.getLimitedValue(UINT64_MAX/10);
2211 uint64_t IntTSize = TSize.getLimitedValue(UINT64_MAX/10);
2212 if (IntTSize * 10 < IntRange * 4)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002213 return false;
2214
David Greene4b69d992010-01-05 01:24:57 +00002215 DEBUG(dbgs() << "Lowering jump table\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002216 << "First entry: " << First << ". Last entry: " << Last << '\n'
Jakob Stoklund Olesen79443912011-10-26 01:47:48 +00002217 << "Range: " << Range << ". Size: " << TSize << ".\n\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002218
2219 // Get the MachineFunction which holds the current MBB. This is used when
2220 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002221 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002222
2223 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002224 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00002225 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002226
2227 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
2228
2229 // Create a new basic block to hold the code for loading the address
2230 // of the jump table, and jumping to it. Update successor information;
2231 // we will either branch to the default case for the switch, or the jump
2232 // table.
2233 MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2234 CurMF->insert(BBI, JumpTableBB);
Jakub Staszak7cc2b072011-06-16 20:22:37 +00002235
2236 addSuccessorWithWeight(CR.CaseBB, Default);
2237 addSuccessorWithWeight(CR.CaseBB, JumpTableBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002238
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002239 // Build a vector of destination BBs, corresponding to each target
2240 // of the jump table. If the value of the jump table slot corresponds to
2241 // a case statement, push the case's BB onto the vector, otherwise, push
2242 // the default BB.
2243 std::vector<MachineBasicBlock*> DestBBs;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002244 APInt TEI = First;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002245 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
Chris Lattner071c62f2010-01-25 23:26:13 +00002246 const APInt &Low = cast<ConstantInt>(I->Low)->getValue();
2247 const APInt &High = cast<ConstantInt>(I->High)->getValue();
Anton Korobeynikov23218582008-12-23 22:25:27 +00002248
Stepan Dyatkovskiyc187df22012-05-17 08:56:30 +00002249 if (Low.ule(TEI) && TEI.ule(High)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002250 DestBBs.push_back(I->BB);
2251 if (TEI==High)
2252 ++I;
2253 } else {
2254 DestBBs.push_back(Default);
2255 }
2256 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002257
Manman Ren1a710fd2012-08-24 18:14:27 +00002258 // Calculate weight for each unique destination in CR.
2259 DenseMap<MachineBasicBlock*, uint32_t> DestWeights;
2260 if (FuncInfo.BPI)
2261 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
2262 DenseMap<MachineBasicBlock*, uint32_t>::iterator Itr =
2263 DestWeights.find(I->BB);
Stephen Lin155615d2013-07-08 00:37:03 +00002264 if (Itr != DestWeights.end())
Manman Ren1a710fd2012-08-24 18:14:27 +00002265 Itr->second += I->ExtraWeight;
2266 else
2267 DestWeights[I->BB] = I->ExtraWeight;
2268 }
2269
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002270 // Update successor info. Add one edge to each unique successor.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002271 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
2272 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002273 E = DestBBs.end(); I != E; ++I) {
2274 if (!SuccsHandled[(*I)->getNumber()]) {
2275 SuccsHandled[(*I)->getNumber()] = true;
Manman Ren1a710fd2012-08-24 18:14:27 +00002276 DenseMap<MachineBasicBlock*, uint32_t>::iterator Itr =
2277 DestWeights.find(*I);
2278 addSuccessorWithWeight(JumpTableBB, *I,
2279 Itr != DestWeights.end() ? Itr->second : 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002280 }
2281 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002282
Bob Wilsond1ec31d2010-03-18 18:42:41 +00002283 // Create a jump table index for this jump table.
Bill Wendlingba54bca2013-06-19 21:36:55 +00002284 unsigned JTEncoding = TLI->getJumpTableEncoding();
Chris Lattner071c62f2010-01-25 23:26:13 +00002285 unsigned JTI = CurMF->getOrCreateJumpTableInfo(JTEncoding)
Bob Wilsond1ec31d2010-03-18 18:42:41 +00002286 ->createJumpTableIndex(DestBBs);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002287
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002288 // Set the jump table information so that we can codegen it as a second
2289 // MachineBasicBlock
2290 JumpTable JT(-1U, JTI, JumpTableBB, Default);
Dan Gohman99be8ae2010-04-19 22:41:47 +00002291 JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == SwitchBB));
2292 if (CR.CaseBB == SwitchBB)
2293 visitJumpTableHeader(JT, JTH, SwitchBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002294
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002295 JTCases.push_back(JumpTableBlock(JTH, JT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002296 return true;
2297}
2298
2299/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
2300/// 2 subtrees.
Dan Gohman2048b852009-11-23 18:04:58 +00002301bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR,
2302 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00002303 const Value* SV,
Stephen Lin09f8ca32013-07-06 21:44:25 +00002304 MachineBasicBlock* Default,
2305 MachineBasicBlock* SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002306 // Get the MachineFunction which holds the current MBB. This is used when
2307 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002308 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002309
2310 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002311 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00002312 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002313
2314 Case& FrontCase = *CR.Range.first;
2315 Case& BackCase = *(CR.Range.second-1);
2316 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
2317
2318 // Size is the number of Cases represented by this range.
2319 unsigned Size = CR.Range.second - CR.Range.first;
2320
Chris Lattnere880efe2009-11-07 07:50:34 +00002321 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
2322 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002323 double FMetric = 0;
2324 CaseItr Pivot = CR.Range.first + Size/2;
2325
2326 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
2327 // (heuristically) allow us to emit JumpTable's later.
Chris Lattnere880efe2009-11-07 07:50:34 +00002328 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002329 for (CaseItr I = CR.Range.first, E = CR.Range.second;
2330 I!=E; ++I)
2331 TSize += I->size();
2332
Chris Lattnere880efe2009-11-07 07:50:34 +00002333 APInt LSize = FrontCase.size();
2334 APInt RSize = TSize-LSize;
David Greene4b69d992010-01-05 01:24:57 +00002335 DEBUG(dbgs() << "Selecting best pivot: \n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002336 << "First: " << First << ", Last: " << Last <<'\n'
2337 << "LSize: " << LSize << ", RSize: " << RSize << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002338 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
2339 J!=E; ++I, ++J) {
Chris Lattnere880efe2009-11-07 07:50:34 +00002340 const APInt &LEnd = cast<ConstantInt>(I->High)->getValue();
2341 const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002342 APInt Range = ComputeRange(LEnd, RBegin);
Stepan Dyatkovskiyc2c52a62012-05-15 06:50:18 +00002343 assert((Range - 2ULL).isNonNegative() &&
2344 "Invalid case distance");
Chris Lattnerc3e4e592011-04-09 06:57:13 +00002345 // Use volatile double here to avoid excess precision issues on some hosts,
2346 // e.g. that use 80-bit X87 registers.
2347 volatile double LDensity =
2348 (double)LSize.roundToDouble() /
Chris Lattnere880efe2009-11-07 07:50:34 +00002349 (LEnd - First + 1ULL).roundToDouble();
Chris Lattnerc3e4e592011-04-09 06:57:13 +00002350 volatile double RDensity =
2351 (double)RSize.roundToDouble() /
Chris Lattnere880efe2009-11-07 07:50:34 +00002352 (Last - RBegin + 1ULL).roundToDouble();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002353 double Metric = Range.logBase2()*(LDensity+RDensity);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002354 // Should always split in some non-trivial place
David Greene4b69d992010-01-05 01:24:57 +00002355 DEBUG(dbgs() <<"=>Step\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002356 << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n'
2357 << "LDensity: " << LDensity
2358 << ", RDensity: " << RDensity << '\n'
2359 << "Metric: " << Metric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002360 if (FMetric < Metric) {
2361 Pivot = J;
2362 FMetric = Metric;
David Greene4b69d992010-01-05 01:24:57 +00002363 DEBUG(dbgs() << "Current metric set to: " << FMetric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002364 }
2365
2366 LSize += J->size();
2367 RSize -= J->size();
2368 }
Bill Wendlingba54bca2013-06-19 21:36:55 +00002369
2370 const TargetLowering *TLI = TM.getTargetLowering();
2371 if (areJTsAllowed(*TLI)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002372 // If our case is dense we *really* should handle it earlier!
2373 assert((FMetric > 0) && "Should handle dense range earlier!");
2374 } else {
2375 Pivot = CR.Range.first + Size/2;
2376 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002377
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002378 CaseRange LHSR(CR.Range.first, Pivot);
2379 CaseRange RHSR(Pivot, CR.Range.second);
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00002380 const Constant *C = Pivot->Low;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002381 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002382
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002383 // We know that we branch to the LHS if the Value being switched on is
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002384 // less than the Pivot value, C. We use this to optimize our binary
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002385 // tree a bit, by recognizing that if SV is greater than or equal to the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002386 // LHS's Case Value, and that Case Value is exactly one less than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002387 // Pivot's Value, then we can branch directly to the LHS's Target,
2388 // rather than creating a leaf node for it.
2389 if ((LHSR.second - LHSR.first) == 1 &&
2390 LHSR.first->High == CR.GE &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00002391 cast<ConstantInt>(C)->getValue() ==
2392 (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002393 TrueBB = LHSR.first->BB;
2394 } else {
2395 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2396 CurMF->insert(BBI, TrueBB);
2397 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002398
2399 // Put SV in a virtual register to make it available from the new blocks.
2400 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002401 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002402
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002403 // Similar to the optimization above, if the Value being switched on is
2404 // known to be less than the Constant CR.LT, and the current Case Value
2405 // is CR.LT - 1, then we can branch directly to the target block for
2406 // the current Case Value, rather than emitting a RHS leaf node for it.
2407 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00002408 cast<ConstantInt>(RHSR.first->Low)->getValue() ==
2409 (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002410 FalseBB = RHSR.first->BB;
2411 } else {
2412 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2413 CurMF->insert(BBI, FalseBB);
2414 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002415
2416 // Put SV in a virtual register to make it available from the new blocks.
2417 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002418 }
2419
2420 // Create a CaseBlock record representing a conditional branch to
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002421 // the LHS node if the value being switched on SV is less than C.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002422 // Otherwise, branch to LHS.
Stepan Dyatkovskiyc187df22012-05-17 08:56:30 +00002423 CaseBlock CB(ISD::SETULT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002424
Dan Gohman99be8ae2010-04-19 22:41:47 +00002425 if (CR.CaseBB == SwitchBB)
2426 visitSwitchCase(CB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002427 else
2428 SwitchCases.push_back(CB);
2429
2430 return true;
2431}
2432
2433/// handleBitTestsSwitchCase - if current case range has few destination and
2434/// range span less, than machine word bitwidth, encode case range into series
2435/// of masks and emit bit tests with these masks.
Dan Gohman2048b852009-11-23 18:04:58 +00002436bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR,
2437 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00002438 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00002439 MachineBasicBlock* Default,
Stephen Lin09f8ca32013-07-06 21:44:25 +00002440 MachineBasicBlock* SwitchBB) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00002441 const TargetLowering *TLI = TM.getTargetLowering();
2442 EVT PTy = TLI->getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002443 unsigned IntPtrBits = PTy.getSizeInBits();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002444
2445 Case& FrontCase = *CR.Range.first;
2446 Case& BackCase = *(CR.Range.second-1);
2447
2448 // Get the MachineFunction which holds the current MBB. This is used when
2449 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002450 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002451
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00002452 // If target does not have legal shift left, do not emit bit tests at all.
Bill Wendlingba54bca2013-06-19 21:36:55 +00002453 if (!TLI->isOperationLegal(ISD::SHL, TLI->getPointerTy()))
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00002454 return false;
2455
Anton Korobeynikov23218582008-12-23 22:25:27 +00002456 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002457 for (CaseItr I = CR.Range.first, E = CR.Range.second;
2458 I!=E; ++I) {
2459 // Single case counts one, case range - two.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002460 numCmps += (I->Low == I->High ? 1 : 2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002461 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002462
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002463 // Count unique destinations
2464 SmallSet<MachineBasicBlock*, 4> Dests;
2465 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
2466 Dests.insert(I->BB);
2467 if (Dests.size() > 3)
2468 // Don't bother the code below, if there are too much unique destinations
2469 return false;
2470 }
David Greene4b69d992010-01-05 01:24:57 +00002471 DEBUG(dbgs() << "Total number of unique destinations: "
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002472 << Dests.size() << '\n'
2473 << "Total number of comparisons: " << numCmps << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00002474
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002475 // Compute span of values.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002476 const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue();
2477 const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002478 APInt cmpRange = maxValue - minValue;
2479
David Greene4b69d992010-01-05 01:24:57 +00002480 DEBUG(dbgs() << "Compare range: " << cmpRange << '\n'
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002481 << "Low bound: " << minValue << '\n'
2482 << "High bound: " << maxValue << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00002483
Dan Gohmane0567812010-04-08 23:03:40 +00002484 if (cmpRange.uge(IntPtrBits) ||
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002485 (!(Dests.size() == 1 && numCmps >= 3) &&
2486 !(Dests.size() == 2 && numCmps >= 5) &&
2487 !(Dests.size() >= 3 && numCmps >= 6)))
2488 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002489
David Greene4b69d992010-01-05 01:24:57 +00002490 DEBUG(dbgs() << "Emitting bit tests\n");
Anton Korobeynikov23218582008-12-23 22:25:27 +00002491 APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth());
2492
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002493 // Optimize the case where all the case values fit in a
2494 // word without having to subtract minValue. In this case,
2495 // we can optimize away the subtraction.
Stepan Dyatkovskiyc187df22012-05-17 08:56:30 +00002496 if (maxValue.ult(IntPtrBits)) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002497 cmpRange = maxValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002498 } else {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002499 lowBound = minValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002500 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002501
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002502 CaseBitsVector CasesBits;
2503 unsigned i, count = 0;
2504
2505 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
2506 MachineBasicBlock* Dest = I->BB;
2507 for (i = 0; i < count; ++i)
2508 if (Dest == CasesBits[i].BB)
2509 break;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002510
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002511 if (i == count) {
2512 assert((count < 3) && "Too much destinations to test!");
Manman Ren1a710fd2012-08-24 18:14:27 +00002513 CasesBits.push_back(CaseBits(0, Dest, 0, 0/*Weight*/));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002514 count++;
2515 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002516
2517 const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue();
2518 const APInt& highValue = cast<ConstantInt>(I->High)->getValue();
2519
2520 uint64_t lo = (lowValue - lowBound).getZExtValue();
2521 uint64_t hi = (highValue - lowBound).getZExtValue();
Manman Ren1a710fd2012-08-24 18:14:27 +00002522 CasesBits[i].ExtraWeight += I->ExtraWeight;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002523
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002524 for (uint64_t j = lo; j <= hi; j++) {
2525 CasesBits[i].Mask |= 1ULL << j;
2526 CasesBits[i].Bits++;
2527 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002528
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002529 }
2530 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
Anton Korobeynikov23218582008-12-23 22:25:27 +00002531
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002532 BitTestInfo BTC;
2533
2534 // Figure out which block is immediately after the current one.
2535 MachineFunction::iterator BBI = CR.CaseBB;
2536 ++BBI;
2537
2538 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
2539
David Greene4b69d992010-01-05 01:24:57 +00002540 DEBUG(dbgs() << "Cases:\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002541 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
David Greene4b69d992010-01-05 01:24:57 +00002542 DEBUG(dbgs() << "Mask: " << CasesBits[i].Mask
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002543 << ", Bits: " << CasesBits[i].Bits
2544 << ", BB: " << CasesBits[i].BB << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002545
2546 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2547 CurMF->insert(BBI, CaseBB);
2548 BTC.push_back(BitTestCase(CasesBits[i].Mask,
2549 CaseBB,
Manman Ren1a710fd2012-08-24 18:14:27 +00002550 CasesBits[i].BB, CasesBits[i].ExtraWeight));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002551
2552 // Put SV in a virtual register to make it available from the new blocks.
2553 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002554 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002555
2556 BitTestBlock BTB(lowBound, cmpRange, SV,
Evan Chengd08e5b42011-01-06 01:02:44 +00002557 -1U, MVT::Other, (CR.CaseBB == SwitchBB),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002558 CR.CaseBB, Default, BTC);
2559
Dan Gohman99be8ae2010-04-19 22:41:47 +00002560 if (CR.CaseBB == SwitchBB)
2561 visitBitTestHeader(BTB, SwitchBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002562
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002563 BitTestCases.push_back(BTB);
2564
2565 return true;
2566}
2567
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002568/// Clusterify - Transform simple list of Cases into list of CaseRange's
Dan Gohman2048b852009-11-23 18:04:58 +00002569size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases,
2570 const SwitchInst& SI) {
Stephen Lin155615d2013-07-08 00:37:03 +00002571
Stepan Dyatkovskiy05cfe2e2012-05-18 08:32:28 +00002572 /// Use a shorter form of declaration, and also
2573 /// show the we want to use CRSBuilder as Clusterifier.
Stepan Dyatkovskiy4319a552012-06-02 07:26:00 +00002574 typedef IntegersSubsetMapping<MachineBasicBlock> Clusterifier;
Stephen Lin155615d2013-07-08 00:37:03 +00002575
Stepan Dyatkovskiy05cfe2e2012-05-18 08:32:28 +00002576 Clusterifier TheClusterifier;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002577
Manman Ren1a710fd2012-08-24 18:14:27 +00002578 BranchProbabilityInfo *BPI = FuncInfo.BPI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002579 // Start with "simple" cases
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002580 for (SwitchInst::ConstCaseIt i = SI.case_begin(), e = SI.case_end();
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002581 i != e; ++i) {
2582 const BasicBlock *SuccBB = i.getCaseSuccessor();
Jakub Staszakc8f34de2011-07-29 22:25:21 +00002583 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SuccBB];
2584
Stephen Lin155615d2013-07-08 00:37:03 +00002585 TheClusterifier.add(i.getCaseValueEx(), SMBB,
Manman Ren1a710fd2012-08-24 18:14:27 +00002586 BPI ? BPI->getEdgeWeight(SI.getParent(), i.getSuccessorIndex()) : 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002587 }
Stephen Lin155615d2013-07-08 00:37:03 +00002588
Stepan Dyatkovskiy05cfe2e2012-05-18 08:32:28 +00002589 TheClusterifier.optimize();
Stephen Lin155615d2013-07-08 00:37:03 +00002590
Stepan Dyatkovskiy05cfe2e2012-05-18 08:32:28 +00002591 size_t numCmps = 0;
2592 for (Clusterifier::RangeIterator i = TheClusterifier.begin(),
2593 e = TheClusterifier.end(); i != e; ++i, ++numCmps) {
Stepan Dyatkovskiy66d79ce2012-07-04 05:53:05 +00002594 Clusterifier::Cluster &C = *i;
Manman Ren1a710fd2012-08-24 18:14:27 +00002595 // Update edge weight for the cluster.
2596 unsigned W = C.first.Weight;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002597
Stepan Dyatkovskiy484fc932012-05-28 12:39:09 +00002598 // FIXME: Currently work with ConstantInt based numbers.
2599 // Changing it to APInt based is a pretty heavy for this commit.
Stepan Dyatkovskiy66d79ce2012-07-04 05:53:05 +00002600 Cases.push_back(Case(C.first.getLow().toConstantInt(),
2601 C.first.getHigh().toConstantInt(), C.second, W));
Stephen Lin155615d2013-07-08 00:37:03 +00002602
Stepan Dyatkovskiy66d79ce2012-07-04 05:53:05 +00002603 if (C.first.getLow() != C.first.getHigh())
Stepan Dyatkovskiy05cfe2e2012-05-18 08:32:28 +00002604 // A range counts double, since it requires two compares.
2605 ++numCmps;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002606 }
2607
2608 return numCmps;
2609}
2610
Jakob Stoklund Olesen2622f462010-09-30 19:44:31 +00002611void SelectionDAGBuilder::UpdateSplitBlock(MachineBasicBlock *First,
2612 MachineBasicBlock *Last) {
2613 // Update JTCases.
2614 for (unsigned i = 0, e = JTCases.size(); i != e; ++i)
2615 if (JTCases[i].first.HeaderBB == First)
2616 JTCases[i].first.HeaderBB = Last;
2617
2618 // Update BitTestCases.
2619 for (unsigned i = 0, e = BitTestCases.size(); i != e; ++i)
2620 if (BitTestCases[i].Parent == First)
2621 BitTestCases[i].Parent = Last;
2622}
2623
Dan Gohman46510a72010-04-15 01:51:59 +00002624void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) {
Dan Gohman84023e02010-07-10 09:00:22 +00002625 MachineBasicBlock *SwitchMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00002626
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002627 // Figure out which block is immediately after the current one.
2628 MachineBasicBlock *NextBlock = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002629 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
2630
2631 // If there is only the default destination, branch to it if it is not the
2632 // next basic block. Otherwise, just fall through.
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00002633 if (!SI.getNumCases()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002634 // Update machine-CFG edges.
2635
2636 // If this is not a fall-through branch, emit the branch.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002637 SwitchMBB->addSuccessor(Default);
Bill Wendling4533cac2010-01-28 21:51:40 +00002638 if (Default != NextBlock)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002639 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002640 MVT::Other, getControlRoot(),
2641 DAG.getBasicBlock(Default)));
Bill Wendling49fcff82009-12-21 22:30:11 +00002642
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002643 return;
2644 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002645
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002646 // If there are any non-default case statements, create a vector of Cases
2647 // representing each one, and sort the vector so that we can efficiently
2648 // create a binary search tree from them.
2649 CaseVector Cases;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002650 size_t numCmps = Clusterify(Cases, SI);
David Greene4b69d992010-01-05 01:24:57 +00002651 DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size()
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002652 << ". Total compares: " << numCmps << '\n');
Duncan Sands17001ce2011-10-18 12:44:00 +00002653 (void)numCmps;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002654
2655 // Get the Value to be switched on and default basic blocks, which will be
2656 // inserted into CaseBlock records, representing basic blocks in the binary
2657 // search tree.
Eli Friedmanbb5a7442011-09-29 20:21:17 +00002658 const Value *SV = SI.getCondition();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002659
2660 // Push the initial CaseRec onto the worklist
2661 CaseRecVector WorkList;
Dan Gohman99be8ae2010-04-19 22:41:47 +00002662 WorkList.push_back(CaseRec(SwitchMBB,0,0,
2663 CaseRange(Cases.begin(),Cases.end())));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002664
2665 while (!WorkList.empty()) {
2666 // Grab a record representing a case range to process off the worklist
2667 CaseRec CR = WorkList.back();
2668 WorkList.pop_back();
2669
Dan Gohman99be8ae2010-04-19 22:41:47 +00002670 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002671 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002672
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002673 // If the range has few cases (two or less) emit a series of specific
2674 // tests.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002675 if (handleSmallSwitchRange(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002676 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002677
Sebastian Pop1a37d7e2012-09-25 20:35:36 +00002678 // If the switch has more than N blocks, and is at least 40% dense, and the
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002679 // target supports indirect branches, then emit a jump table rather than
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002680 // lowering the switch to a binary tree of conditional branches.
Sebastian Pop1a37d7e2012-09-25 20:35:36 +00002681 // N defaults to 4 and is controlled via TLS.getMinimumJumpTableEntries().
Dan Gohman99be8ae2010-04-19 22:41:47 +00002682 if (handleJTSwitchCase(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002683 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002684
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002685 // Emit binary tree. We need to pick a pivot, and push left and right ranges
2686 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002687 handleBTSplitSwitchCase(CR, WorkList, SV, Default, SwitchMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002688 }
2689}
2690
Dan Gohman46510a72010-04-15 01:51:59 +00002691void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) {
Dan Gohman84023e02010-07-10 09:00:22 +00002692 MachineBasicBlock *IndirectBrMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00002693
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002694 // Update machine-CFG edges with unique successors.
Nadav Rotemee0ce152012-10-23 21:05:33 +00002695 SmallSet<BasicBlock*, 32> Done;
2696 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i) {
2697 BasicBlock *BB = I.getSuccessor(i);
2698 bool Inserted = Done.insert(BB);
2699 if (!Inserted)
2700 continue;
2701
2702 MachineBasicBlock *Succ = FuncInfo.MBBMap[BB];
Jakub Staszak7cc2b072011-06-16 20:22:37 +00002703 addSuccessorWithWeight(IndirectBrMBB, Succ);
2704 }
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002705
Andrew Trickac6d9be2013-05-25 02:42:55 +00002706 DAG.setRoot(DAG.getNode(ISD::BRIND, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002707 MVT::Other, getControlRoot(),
2708 getValue(I.getAddress())));
Bill Wendling49fcff82009-12-21 22:30:11 +00002709}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002710
Dan Gohman46510a72010-04-15 01:51:59 +00002711void SelectionDAGBuilder::visitFSub(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002712 // -0.0 - X --> fneg
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002713 Type *Ty = I.getType();
Chris Lattner2ca5c862011-02-15 00:14:00 +00002714 if (isa<Constant>(I.getOperand(0)) &&
2715 I.getOperand(0) == ConstantFP::getZeroValueForNegation(Ty)) {
2716 SDValue Op2 = getValue(I.getOperand(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00002717 setValue(&I, DAG.getNode(ISD::FNEG, getCurSDLoc(),
Chris Lattner2ca5c862011-02-15 00:14:00 +00002718 Op2.getValueType(), Op2));
2719 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002720 }
Bill Wendling49fcff82009-12-21 22:30:11 +00002721
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002722 visitBinary(I, ISD::FSUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002723}
2724
Dan Gohman46510a72010-04-15 01:51:59 +00002725void SelectionDAGBuilder::visitBinary(const User &I, unsigned OpCode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002726 SDValue Op1 = getValue(I.getOperand(0));
2727 SDValue Op2 = getValue(I.getOperand(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00002728 setValue(&I, DAG.getNode(OpCode, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002729 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002730}
2731
Dan Gohman46510a72010-04-15 01:51:59 +00002732void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002733 SDValue Op1 = getValue(I.getOperand(0));
2734 SDValue Op2 = getValue(I.getOperand(1));
Owen Anderson95771af2011-02-25 21:41:48 +00002735
Bill Wendlingba54bca2013-06-19 21:36:55 +00002736 EVT ShiftTy = TM.getTargetLowering()->getShiftAmountTy(Op2.getValueType());
Owen Anderson95771af2011-02-25 21:41:48 +00002737
Chris Lattnerd3027732011-02-13 09:02:52 +00002738 // Coerce the shift amount to the right type if we can.
2739 if (!I.getType()->isVectorTy() && Op2.getValueType() != ShiftTy) {
Chris Lattner915eeb42011-02-13 09:10:56 +00002740 unsigned ShiftSize = ShiftTy.getSizeInBits();
2741 unsigned Op2Size = Op2.getValueType().getSizeInBits();
Andrew Trickac6d9be2013-05-25 02:42:55 +00002742 SDLoc DL = getCurSDLoc();
Owen Anderson95771af2011-02-25 21:41:48 +00002743
Dan Gohman57fc82d2009-04-09 03:51:29 +00002744 // If the operand is smaller than the shift count type, promote it.
Chris Lattnerd3027732011-02-13 09:02:52 +00002745 if (ShiftSize > Op2Size)
2746 Op2 = DAG.getNode(ISD::ZERO_EXTEND, DL, ShiftTy, Op2);
Owen Anderson95771af2011-02-25 21:41:48 +00002747
Dan Gohman57fc82d2009-04-09 03:51:29 +00002748 // If the operand is larger than the shift count type but the shift
2749 // count type has enough bits to represent any shift value, truncate
2750 // it now. This is a common case and it exposes the truncate to
2751 // optimization early.
Chris Lattnerd3027732011-02-13 09:02:52 +00002752 else if (ShiftSize >= Log2_32_Ceil(Op2.getValueType().getSizeInBits()))
2753 Op2 = DAG.getNode(ISD::TRUNCATE, DL, ShiftTy, Op2);
2754 // Otherwise we'll need to temporarily settle for some other convenient
Chris Lattnere0751182011-02-13 19:09:16 +00002755 // type. Type legalization will make adjustments once the shiftee is split.
Chris Lattnerd3027732011-02-13 09:02:52 +00002756 else
Chris Lattnere0751182011-02-13 19:09:16 +00002757 Op2 = DAG.getZExtOrTrunc(Op2, DL, MVT::i32);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002758 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002759
Andrew Trickac6d9be2013-05-25 02:42:55 +00002760 setValue(&I, DAG.getNode(Opcode, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002761 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002762}
2763
Benjamin Kramer9c640302011-07-08 10:31:30 +00002764void SelectionDAGBuilder::visitSDiv(const User &I) {
Benjamin Kramer9c640302011-07-08 10:31:30 +00002765 SDValue Op1 = getValue(I.getOperand(0));
2766 SDValue Op2 = getValue(I.getOperand(1));
2767
2768 // Turn exact SDivs into multiplications.
2769 // FIXME: This should be in DAGCombiner, but it doesn't have access to the
2770 // exact bit.
Benjamin Kramer3492a4a2011-07-08 12:08:24 +00002771 if (isa<BinaryOperator>(&I) && cast<BinaryOperator>(&I)->isExact() &&
2772 !isa<ConstantSDNode>(Op1) &&
Benjamin Kramer9c640302011-07-08 10:31:30 +00002773 isa<ConstantSDNode>(Op2) && !cast<ConstantSDNode>(Op2)->isNullValue())
Bill Wendlingba54bca2013-06-19 21:36:55 +00002774 setValue(&I, TM.getTargetLowering()->BuildExactSDIV(Op1, Op2,
2775 getCurSDLoc(), DAG));
Benjamin Kramer9c640302011-07-08 10:31:30 +00002776 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00002777 setValue(&I, DAG.getNode(ISD::SDIV, getCurSDLoc(), Op1.getValueType(),
Benjamin Kramer9c640302011-07-08 10:31:30 +00002778 Op1, Op2));
2779}
2780
Dan Gohman46510a72010-04-15 01:51:59 +00002781void SelectionDAGBuilder::visitICmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002782 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002783 if (const ICmpInst *IC = dyn_cast<ICmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002784 predicate = IC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002785 else if (const ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002786 predicate = ICmpInst::Predicate(IC->getPredicate());
2787 SDValue Op1 = getValue(I.getOperand(0));
2788 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002789 ISD::CondCode Opcode = getICmpCondCode(predicate);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002790
Bill Wendlingba54bca2013-06-19 21:36:55 +00002791 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002792 setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Opcode));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002793}
2794
Dan Gohman46510a72010-04-15 01:51:59 +00002795void SelectionDAGBuilder::visitFCmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002796 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002797 if (const FCmpInst *FC = dyn_cast<FCmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002798 predicate = FC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002799 else if (const ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002800 predicate = FCmpInst::Predicate(FC->getPredicate());
2801 SDValue Op1 = getValue(I.getOperand(0));
2802 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002803 ISD::CondCode Condition = getFCmpCondCode(predicate);
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002804 if (TM.Options.NoNaNsFPMath)
2805 Condition = getFCmpCodeWithoutNaN(Condition);
Bill Wendlingba54bca2013-06-19 21:36:55 +00002806 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002807 setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Condition));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002808}
2809
Dan Gohman46510a72010-04-15 01:51:59 +00002810void SelectionDAGBuilder::visitSelect(const User &I) {
Owen Andersone50ed302009-08-10 22:56:29 +00002811 SmallVector<EVT, 4> ValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00002812 ComputeValueVTs(*TM.getTargetLowering(), I.getType(), ValueVTs);
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002813 unsigned NumValues = ValueVTs.size();
Bill Wendling49fcff82009-12-21 22:30:11 +00002814 if (NumValues == 0) return;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002815
Bill Wendling49fcff82009-12-21 22:30:11 +00002816 SmallVector<SDValue, 4> Values(NumValues);
2817 SDValue Cond = getValue(I.getOperand(0));
2818 SDValue TrueVal = getValue(I.getOperand(1));
2819 SDValue FalseVal = getValue(I.getOperand(2));
Duncan Sands28b77e92011-09-06 19:07:46 +00002820 ISD::NodeType OpCode = Cond.getValueType().isVector() ?
2821 ISD::VSELECT : ISD::SELECT;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002822
Bill Wendling4533cac2010-01-28 21:51:40 +00002823 for (unsigned i = 0; i != NumValues; ++i)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002824 Values[i] = DAG.getNode(OpCode, getCurSDLoc(),
Duncan Sands28b77e92011-09-06 19:07:46 +00002825 TrueVal.getNode()->getValueType(TrueVal.getResNo()+i),
Chris Lattnerb3e87b22010-03-12 07:15:36 +00002826 Cond,
Bill Wendling49fcff82009-12-21 22:30:11 +00002827 SDValue(TrueVal.getNode(),
2828 TrueVal.getResNo() + i),
2829 SDValue(FalseVal.getNode(),
2830 FalseVal.getResNo() + i));
2831
Andrew Trickac6d9be2013-05-25 02:42:55 +00002832 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002833 DAG.getVTList(&ValueVTs[0], NumValues),
2834 &Values[0], NumValues));
Bill Wendling49fcff82009-12-21 22:30:11 +00002835}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002836
Dan Gohman46510a72010-04-15 01:51:59 +00002837void SelectionDAGBuilder::visitTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002838 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2839 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002840 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002841 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002842}
2843
Dan Gohman46510a72010-04-15 01:51:59 +00002844void SelectionDAGBuilder::visitZExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002845 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2846 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2847 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002848 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002849 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002850}
2851
Dan Gohman46510a72010-04-15 01:51:59 +00002852void SelectionDAGBuilder::visitSExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002853 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2854 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2855 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002856 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002857 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002858}
2859
Dan Gohman46510a72010-04-15 01:51:59 +00002860void SelectionDAGBuilder::visitFPTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002861 // FPTrunc is never a no-op cast, no need to check
2862 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002863 const TargetLowering *TLI = TM.getTargetLowering();
2864 EVT DestVT = TLI->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002865 setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurSDLoc(),
Pete Cooperf57e1c22012-01-17 01:54:07 +00002866 DestVT, N,
Bill Wendlingba54bca2013-06-19 21:36:55 +00002867 DAG.getTargetConstant(0, TLI->getPointerTy())));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002868}
2869
Stephen Lin09f8ca32013-07-06 21:44:25 +00002870void SelectionDAGBuilder::visitFPExt(const User &I) {
Hal Finkel46bb70c2011-10-18 03:51:57 +00002871 // FPExt is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002872 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002873 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002874 setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002875}
2876
Dan Gohman46510a72010-04-15 01:51:59 +00002877void SelectionDAGBuilder::visitFPToUI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002878 // FPToUI is never a no-op cast, no need to check
2879 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002880 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002881 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002882}
2883
Dan Gohman46510a72010-04-15 01:51:59 +00002884void SelectionDAGBuilder::visitFPToSI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002885 // FPToSI is never a no-op cast, no need to check
2886 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002887 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002888 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002889}
2890
Dan Gohman46510a72010-04-15 01:51:59 +00002891void SelectionDAGBuilder::visitUIToFP(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002892 // UIToFP is never a no-op cast, no need to check
2893 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002894 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002895 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002896}
2897
Stephen Lin09f8ca32013-07-06 21:44:25 +00002898void SelectionDAGBuilder::visitSIToFP(const User &I) {
Bill Wendling181b6272008-10-19 20:34:04 +00002899 // SIToFP is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002900 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002901 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002902 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002903}
2904
Dan Gohman46510a72010-04-15 01:51:59 +00002905void SelectionDAGBuilder::visitPtrToInt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002906 // What to do depends on the size of the integer and the size of the pointer.
2907 // We can either truncate, zero extend, or no-op, accordingly.
2908 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002909 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002910 setValue(&I, DAG.getZExtOrTrunc(N, getCurSDLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002911}
2912
Dan Gohman46510a72010-04-15 01:51:59 +00002913void SelectionDAGBuilder::visitIntToPtr(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002914 // What to do depends on the size of the integer and the size of the pointer.
2915 // We can either truncate, zero extend, or no-op, accordingly.
2916 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002917 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002918 setValue(&I, DAG.getZExtOrTrunc(N, getCurSDLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002919}
2920
Dan Gohman46510a72010-04-15 01:51:59 +00002921void SelectionDAGBuilder::visitBitCast(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002922 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002923 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002924
Bill Wendling49fcff82009-12-21 22:30:11 +00002925 // BitCast assures us that source and destination are the same size so this is
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002926 // either a BITCAST or a no-op.
Bill Wendling4533cac2010-01-28 21:51:40 +00002927 if (DestVT != N.getValueType())
Andrew Trickac6d9be2013-05-25 02:42:55 +00002928 setValue(&I, DAG.getNode(ISD::BITCAST, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002929 DestVT, N)); // convert types.
2930 else
Bill Wendling49fcff82009-12-21 22:30:11 +00002931 setValue(&I, N); // noop cast.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002932}
2933
Dan Gohman46510a72010-04-15 01:51:59 +00002934void SelectionDAGBuilder::visitInsertElement(const User &I) {
Tom Stellard425b76c2013-08-05 22:22:01 +00002935 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002936 SDValue InVec = getValue(I.getOperand(0));
2937 SDValue InVal = getValue(I.getOperand(1));
Tom Stellard425b76c2013-08-05 22:22:01 +00002938 SDValue InIdx = DAG.getSExtOrTrunc(getValue(I.getOperand(2)),
2939 getCurSDLoc(), TLI.getVectorIdxTy());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002940 setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurSDLoc(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00002941 TM.getTargetLowering()->getValueType(I.getType()),
Bill Wendling4533cac2010-01-28 21:51:40 +00002942 InVec, InVal, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002943}
2944
Dan Gohman46510a72010-04-15 01:51:59 +00002945void SelectionDAGBuilder::visitExtractElement(const User &I) {
Tom Stellard425b76c2013-08-05 22:22:01 +00002946 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002947 SDValue InVec = getValue(I.getOperand(0));
Tom Stellard425b76c2013-08-05 22:22:01 +00002948 SDValue InIdx = DAG.getSExtOrTrunc(getValue(I.getOperand(1)),
2949 getCurSDLoc(), TLI.getVectorIdxTy());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002950 setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurSDLoc(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00002951 TM.getTargetLowering()->getValueType(I.getType()),
2952 InVec, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002953}
2954
Craig Topper51578342012-01-04 09:23:09 +00002955// Utility for visitShuffleVector - Return true if every element in Mask,
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00002956// beginning from position Pos and ending in Pos+Size, falls within the
Craig Topper51578342012-01-04 09:23:09 +00002957// specified sequential range [L, L+Pos). or is undef.
2958static bool isSequentialInRange(const SmallVectorImpl<int> &Mask,
Craig Topper23de31b2012-04-11 03:06:35 +00002959 unsigned Pos, unsigned Size, int Low) {
2960 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Craig Topper51578342012-01-04 09:23:09 +00002961 if (Mask[i] >= 0 && Mask[i] != Low)
Nate Begeman9008ca62009-04-27 18:41:29 +00002962 return false;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002963 return true;
2964}
2965
Dan Gohman46510a72010-04-15 01:51:59 +00002966void SelectionDAGBuilder::visitShuffleVector(const User &I) {
Mon P Wang230e4fa2008-11-21 04:25:21 +00002967 SDValue Src1 = getValue(I.getOperand(0));
2968 SDValue Src2 = getValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002969
Chris Lattner56243b82012-01-26 02:51:13 +00002970 SmallVector<int, 8> Mask;
2971 ShuffleVectorInst::getShuffleMask(cast<Constant>(I.getOperand(2)), Mask);
2972 unsigned MaskNumElts = Mask.size();
Bill Wendlingba54bca2013-06-19 21:36:55 +00002973
2974 const TargetLowering *TLI = TM.getTargetLowering();
2975 EVT VT = TLI->getValueType(I.getType());
Owen Andersone50ed302009-08-10 22:56:29 +00002976 EVT SrcVT = Src1.getValueType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00002977 unsigned SrcNumElts = SrcVT.getVectorNumElements();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002978
Mon P Wangc7849c22008-11-16 05:06:27 +00002979 if (SrcNumElts == MaskNumElts) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002980 setValue(&I, DAG.getVectorShuffle(VT, getCurSDLoc(), Src1, Src2,
Bill Wendling4533cac2010-01-28 21:51:40 +00002981 &Mask[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002982 return;
2983 }
2984
2985 // Normalize the shuffle vector since mask and vector length don't match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002986 if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) {
2987 // Mask is longer than the source vectors and is a multiple of the source
2988 // vectors. We can use concatenate vector to make the mask and vectors
Mon P Wang230e4fa2008-11-21 04:25:21 +00002989 // lengths match.
Craig Topper51578342012-01-04 09:23:09 +00002990 if (SrcNumElts*2 == MaskNumElts) {
2991 // First check for Src1 in low and Src2 in high
2992 if (isSequentialInRange(Mask, 0, SrcNumElts, 0) &&
2993 isSequentialInRange(Mask, SrcNumElts, SrcNumElts, SrcNumElts)) {
2994 // The shuffle is concatenating two vectors together.
Andrew Trickac6d9be2013-05-25 02:42:55 +00002995 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurSDLoc(),
Craig Topper51578342012-01-04 09:23:09 +00002996 VT, Src1, Src2));
2997 return;
2998 }
2999 // Then check for Src2 in low and Src1 in high
3000 if (isSequentialInRange(Mask, 0, SrcNumElts, SrcNumElts) &&
3001 isSequentialInRange(Mask, SrcNumElts, SrcNumElts, 0)) {
3002 // The shuffle is concatenating two vectors together.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003003 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurSDLoc(),
Craig Topper51578342012-01-04 09:23:09 +00003004 VT, Src2, Src1));
3005 return;
3006 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00003007 }
3008
Mon P Wangc7849c22008-11-16 05:06:27 +00003009 // Pad both vectors with undefs to make them the same length as the mask.
3010 unsigned NumConcat = MaskNumElts / SrcNumElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00003011 bool Src1U = Src1.getOpcode() == ISD::UNDEF;
3012 bool Src2U = Src2.getOpcode() == ISD::UNDEF;
Dale Johannesene8d72302009-02-06 23:05:02 +00003013 SDValue UndefVal = DAG.getUNDEF(SrcVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00003014
Nate Begeman9008ca62009-04-27 18:41:29 +00003015 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
3016 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
Mon P Wang230e4fa2008-11-21 04:25:21 +00003017 MOps1[0] = Src1;
3018 MOps2[0] = Src2;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003019
3020 Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003021 getCurSDLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003022 &MOps1[0], NumConcat);
3023 Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003024 getCurSDLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003025 &MOps2[0], NumConcat);
Mon P Wang230e4fa2008-11-21 04:25:21 +00003026
Mon P Wangaeb06d22008-11-10 04:46:22 +00003027 // Readjust mask for new input vector length.
Nate Begeman9008ca62009-04-27 18:41:29 +00003028 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003029 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003030 int Idx = Mask[i];
Craig Topper23de31b2012-04-11 03:06:35 +00003031 if (Idx >= (int)SrcNumElts)
3032 Idx -= SrcNumElts - MaskNumElts;
3033 MappedOps.push_back(Idx);
Mon P Wangaeb06d22008-11-10 04:46:22 +00003034 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003035
Andrew Trickac6d9be2013-05-25 02:42:55 +00003036 setValue(&I, DAG.getVectorShuffle(VT, getCurSDLoc(), Src1, Src2,
Bill Wendling4533cac2010-01-28 21:51:40 +00003037 &MappedOps[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00003038 return;
3039 }
3040
Mon P Wangc7849c22008-11-16 05:06:27 +00003041 if (SrcNumElts > MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00003042 // Analyze the access pattern of the vector to see if we can extract
3043 // two subvectors and do the shuffle. The analysis is done by calculating
3044 // the range of elements the mask access on both vectors.
Craig Topper10612dc2012-04-08 23:15:04 +00003045 int MinRange[2] = { static_cast<int>(SrcNumElts),
3046 static_cast<int>(SrcNumElts)};
Mon P Wangc7849c22008-11-16 05:06:27 +00003047 int MaxRange[2] = {-1, -1};
3048
Nate Begeman5a5ca152009-04-29 05:20:52 +00003049 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003050 int Idx = Mask[i];
Craig Topper10612dc2012-04-08 23:15:04 +00003051 unsigned Input = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003052 if (Idx < 0)
3053 continue;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003054
Nate Begeman5a5ca152009-04-29 05:20:52 +00003055 if (Idx >= (int)SrcNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003056 Input = 1;
3057 Idx -= SrcNumElts;
Mon P Wangaeb06d22008-11-10 04:46:22 +00003058 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003059 if (Idx > MaxRange[Input])
3060 MaxRange[Input] = Idx;
3061 if (Idx < MinRange[Input])
3062 MinRange[Input] = Idx;
Mon P Wangaeb06d22008-11-10 04:46:22 +00003063 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00003064
Mon P Wangc7849c22008-11-16 05:06:27 +00003065 // Check if the access is smaller than the vector size and can we find
3066 // a reasonable extract index.
Craig Topper10612dc2012-04-08 23:15:04 +00003067 int RangeUse[2] = { -1, -1 }; // 0 = Unused, 1 = Extract, -1 = Can not
3068 // Extract.
Mon P Wangc7849c22008-11-16 05:06:27 +00003069 int StartIdx[2]; // StartIdx to extract from
Craig Topper10612dc2012-04-08 23:15:04 +00003070 for (unsigned Input = 0; Input < 2; ++Input) {
3071 if (MinRange[Input] >= (int)SrcNumElts && MaxRange[Input] < 0) {
Mon P Wangc7849c22008-11-16 05:06:27 +00003072 RangeUse[Input] = 0; // Unused
3073 StartIdx[Input] = 0;
Craig Topperf873dde2012-04-08 17:53:33 +00003074 continue;
Mon P Wang230e4fa2008-11-21 04:25:21 +00003075 }
Craig Topperf873dde2012-04-08 17:53:33 +00003076
3077 // Find a good start index that is a multiple of the mask length. Then
3078 // see if the rest of the elements are in range.
3079 StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts;
3080 if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts &&
3081 StartIdx[Input] + MaskNumElts <= SrcNumElts)
3082 RangeUse[Input] = 1; // Extract from a multiple of the mask length.
Mon P Wangc7849c22008-11-16 05:06:27 +00003083 }
3084
Bill Wendling636e2582009-08-21 18:16:06 +00003085 if (RangeUse[0] == 0 && RangeUse[1] == 0) {
Bill Wendling4533cac2010-01-28 21:51:40 +00003086 setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
Mon P Wangc7849c22008-11-16 05:06:27 +00003087 return;
3088 }
Craig Topper10612dc2012-04-08 23:15:04 +00003089 if (RangeUse[0] >= 0 && RangeUse[1] >= 0) {
Mon P Wangc7849c22008-11-16 05:06:27 +00003090 // Extract appropriate subvector and generate a vector shuffle
Craig Topper10612dc2012-04-08 23:15:04 +00003091 for (unsigned Input = 0; Input < 2; ++Input) {
Bill Wendling87710f02009-12-21 23:47:40 +00003092 SDValue &Src = Input == 0 ? Src1 : Src2;
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003093 if (RangeUse[Input] == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00003094 Src = DAG.getUNDEF(VT);
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003095 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00003096 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurSDLoc(), VT,
Tom Stellard425b76c2013-08-05 22:22:01 +00003097 Src, DAG.getConstant(StartIdx[Input],
3098 TLI->getVectorIdxTy()));
Mon P Wangaeb06d22008-11-10 04:46:22 +00003099 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003100
Mon P Wangc7849c22008-11-16 05:06:27 +00003101 // Calculate new mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00003102 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003103 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003104 int Idx = Mask[i];
Craig Topper23de31b2012-04-11 03:06:35 +00003105 if (Idx >= 0) {
3106 if (Idx < (int)SrcNumElts)
3107 Idx -= StartIdx[0];
3108 else
3109 Idx -= SrcNumElts + StartIdx[1] - MaskNumElts;
3110 }
3111 MappedOps.push_back(Idx);
Mon P Wangc7849c22008-11-16 05:06:27 +00003112 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003113
Andrew Trickac6d9be2013-05-25 02:42:55 +00003114 setValue(&I, DAG.getVectorShuffle(VT, getCurSDLoc(), Src1, Src2,
Bill Wendling4533cac2010-01-28 21:51:40 +00003115 &MappedOps[0]));
Mon P Wangc7849c22008-11-16 05:06:27 +00003116 return;
Mon P Wangaeb06d22008-11-10 04:46:22 +00003117 }
3118 }
3119
Mon P Wangc7849c22008-11-16 05:06:27 +00003120 // We can't use either concat vectors or extract subvectors so fall back to
3121 // replacing the shuffle with extract and build vector.
3122 // to insert and build vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003123 EVT EltVT = VT.getVectorElementType();
Tom Stellard425b76c2013-08-05 22:22:01 +00003124 EVT IdxVT = TLI->getVectorIdxTy();
Mon P Wangaeb06d22008-11-10 04:46:22 +00003125 SmallVector<SDValue,8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003126 for (unsigned i = 0; i != MaskNumElts; ++i) {
Craig Topper23de31b2012-04-11 03:06:35 +00003127 int Idx = Mask[i];
3128 SDValue Res;
3129
3130 if (Idx < 0) {
3131 Res = DAG.getUNDEF(EltVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00003132 } else {
Craig Topper23de31b2012-04-11 03:06:35 +00003133 SDValue &Src = Idx < (int)SrcNumElts ? Src1 : Src2;
3134 if (Idx >= (int)SrcNumElts) Idx -= SrcNumElts;
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003135
Andrew Trickac6d9be2013-05-25 02:42:55 +00003136 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurSDLoc(),
Tom Stellard425b76c2013-08-05 22:22:01 +00003137 EltVT, Src, DAG.getConstant(Idx, IdxVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00003138 }
Craig Topper23de31b2012-04-11 03:06:35 +00003139
3140 Ops.push_back(Res);
Mon P Wangaeb06d22008-11-10 04:46:22 +00003141 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003142
Andrew Trickac6d9be2013-05-25 02:42:55 +00003143 setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00003144 VT, &Ops[0], Ops.size()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003145}
3146
Dan Gohman46510a72010-04-15 01:51:59 +00003147void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003148 const Value *Op0 = I.getOperand(0);
3149 const Value *Op1 = I.getOperand(1);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003150 Type *AggTy = I.getType();
3151 Type *ValTy = Op1->getType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003152 bool IntoUndef = isa<UndefValue>(Op0);
3153 bool FromUndef = isa<UndefValue>(Op1);
3154
Jay Foadfc6d3a42011-07-13 10:26:04 +00003155 unsigned LinearIndex = ComputeLinearIndex(AggTy, I.getIndices());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003156
Bill Wendlingba54bca2013-06-19 21:36:55 +00003157 const TargetLowering *TLI = TM.getTargetLowering();
Owen Andersone50ed302009-08-10 22:56:29 +00003158 SmallVector<EVT, 4> AggValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003159 ComputeValueVTs(*TLI, AggTy, AggValueVTs);
Owen Andersone50ed302009-08-10 22:56:29 +00003160 SmallVector<EVT, 4> ValValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003161 ComputeValueVTs(*TLI, ValTy, ValValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003162
3163 unsigned NumAggValues = AggValueVTs.size();
3164 unsigned NumValValues = ValValueVTs.size();
3165 SmallVector<SDValue, 4> Values(NumAggValues);
3166
3167 SDValue Agg = getValue(Op0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003168 unsigned i = 0;
3169 // Copy the beginning value(s) from the original aggregate.
3170 for (; i != LinearIndex; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00003171 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003172 SDValue(Agg.getNode(), Agg.getResNo() + i);
3173 // Copy values from the inserted value(s).
Rafael Espindola3fa82832011-05-13 15:18:06 +00003174 if (NumValValues) {
3175 SDValue Val = getValue(Op1);
3176 for (; i != LinearIndex + NumValValues; ++i)
3177 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
3178 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
3179 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003180 // Copy remaining value(s) from the original aggregate.
3181 for (; i != NumAggValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00003182 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003183 SDValue(Agg.getNode(), Agg.getResNo() + i);
3184
Andrew Trickac6d9be2013-05-25 02:42:55 +00003185 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00003186 DAG.getVTList(&AggValueVTs[0], NumAggValues),
3187 &Values[0], NumAggValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003188}
3189
Dan Gohman46510a72010-04-15 01:51:59 +00003190void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003191 const Value *Op0 = I.getOperand(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003192 Type *AggTy = Op0->getType();
3193 Type *ValTy = I.getType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003194 bool OutOfUndef = isa<UndefValue>(Op0);
3195
Jay Foadfc6d3a42011-07-13 10:26:04 +00003196 unsigned LinearIndex = ComputeLinearIndex(AggTy, I.getIndices());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003197
Bill Wendlingba54bca2013-06-19 21:36:55 +00003198 const TargetLowering *TLI = TM.getTargetLowering();
Owen Andersone50ed302009-08-10 22:56:29 +00003199 SmallVector<EVT, 4> ValValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003200 ComputeValueVTs(*TLI, ValTy, ValValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003201
3202 unsigned NumValValues = ValValueVTs.size();
Rafael Espindola3fa82832011-05-13 15:18:06 +00003203
3204 // Ignore a extractvalue that produces an empty object
3205 if (!NumValValues) {
3206 setValue(&I, DAG.getUNDEF(MVT(MVT::Other)));
3207 return;
3208 }
3209
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003210 SmallVector<SDValue, 4> Values(NumValValues);
3211
3212 SDValue Agg = getValue(Op0);
3213 // Copy out the selected value(s).
3214 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
3215 Values[i - LinearIndex] =
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00003216 OutOfUndef ?
Dale Johannesene8d72302009-02-06 23:05:02 +00003217 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00003218 SDValue(Agg.getNode(), Agg.getResNo() + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003219
Andrew Trickac6d9be2013-05-25 02:42:55 +00003220 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00003221 DAG.getVTList(&ValValueVTs[0], NumValValues),
3222 &Values[0], NumValValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003223}
3224
Dan Gohman46510a72010-04-15 01:51:59 +00003225void SelectionDAGBuilder::visitGetElementPtr(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003226 SDValue N = getValue(I.getOperand(0));
Nadav Rotem1c239202012-02-28 14:13:19 +00003227 // Note that the pointer operand may be a vector of pointers. Take the scalar
3228 // element which holds a pointer.
3229 Type *Ty = I.getOperand(0)->getType()->getScalarType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003230
Dan Gohman46510a72010-04-15 01:51:59 +00003231 for (GetElementPtrInst::const_op_iterator OI = I.op_begin()+1, E = I.op_end();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003232 OI != E; ++OI) {
Dan Gohman46510a72010-04-15 01:51:59 +00003233 const Value *Idx = *OI;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003234 if (StructType *StTy = dyn_cast<StructType>(Ty)) {
Duncan Sandsb2df01a2012-11-13 13:01:58 +00003235 unsigned Field = cast<Constant>(Idx)->getUniqueInteger().getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003236 if (Field) {
3237 // N = N + Offset
3238 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003239 N = DAG.getNode(ISD::ADD, getCurSDLoc(), N.getValueType(), N,
Duncan Sandsb2df01a2012-11-13 13:01:58 +00003240 DAG.getConstant(Offset, N.getValueType()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003241 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00003242
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003243 Ty = StTy->getElementType(Field);
3244 } else {
Tom Stellardda25cd32013-08-26 15:05:36 +00003245 uint32_t AS = 0;
3246 if (PointerType *PtrType = dyn_cast<PointerType>(Ty)) {
3247 AS = PtrType->getAddressSpace();
3248 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003249 Ty = cast<SequentialType>(Ty)->getElementType();
3250
3251 // If this is a constant subscript, handle it quickly.
Bill Wendlingba54bca2013-06-19 21:36:55 +00003252 const TargetLowering *TLI = TM.getTargetLowering();
Dan Gohman46510a72010-04-15 01:51:59 +00003253 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Dan Gohmane368b462010-06-18 14:22:04 +00003254 if (CI->isZero()) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003255 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +00003256 TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Evan Cheng65b52df2009-02-09 21:01:06 +00003257 SDValue OffsVal;
Tom Stellardda25cd32013-08-26 15:05:36 +00003258 EVT PTy = TLI->getPointerTy(AS);
Owen Anderson77547be2009-08-10 18:56:59 +00003259 unsigned PtrBits = PTy.getSizeInBits();
Bill Wendlinge1a90422009-12-21 23:10:19 +00003260 if (PtrBits < 64)
Tom Stellardda25cd32013-08-26 15:05:36 +00003261 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), PTy,
Owen Anderson825b72b2009-08-11 20:47:22 +00003262 DAG.getConstant(Offs, MVT::i64));
Bill Wendlinge1a90422009-12-21 23:10:19 +00003263 else
Tom Stellardda25cd32013-08-26 15:05:36 +00003264 OffsVal = DAG.getConstant(Offs, PTy);
Bill Wendlinge1a90422009-12-21 23:10:19 +00003265
Andrew Trickac6d9be2013-05-25 02:42:55 +00003266 N = DAG.getNode(ISD::ADD, getCurSDLoc(), N.getValueType(), N,
Evan Chengb1032a82009-02-09 20:54:38 +00003267 OffsVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003268 continue;
3269 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003270
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003271 // N = N + Idx * ElementSize;
Tom Stellardda25cd32013-08-26 15:05:36 +00003272 APInt ElementSize = APInt(TLI->getPointerSizeInBits(AS),
Dan Gohman7abbd042009-10-23 17:57:43 +00003273 TD->getTypeAllocSize(Ty));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003274 SDValue IdxN = getValue(Idx);
3275
3276 // If the index is smaller or larger than intptr_t, truncate or extend
3277 // it.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003278 IdxN = DAG.getSExtOrTrunc(IdxN, getCurSDLoc(), N.getValueType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003279
3280 // If this is a multiply by a power of two, turn it into a shl
3281 // immediately. This is a very common case.
3282 if (ElementSize != 1) {
Dan Gohman7abbd042009-10-23 17:57:43 +00003283 if (ElementSize.isPowerOf2()) {
3284 unsigned Amt = ElementSize.logBase2();
Andrew Trickac6d9be2013-05-25 02:42:55 +00003285 IdxN = DAG.getNode(ISD::SHL, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003286 N.getValueType(), IdxN,
Nadav Rotem16087692011-12-05 06:29:09 +00003287 DAG.getConstant(Amt, IdxN.getValueType()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003288 } else {
Duncan Sandsb2df01a2012-11-13 13:01:58 +00003289 SDValue Scale = DAG.getConstant(ElementSize, IdxN.getValueType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003290 IdxN = DAG.getNode(ISD::MUL, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003291 N.getValueType(), IdxN, Scale);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003292 }
3293 }
3294
Andrew Trickac6d9be2013-05-25 02:42:55 +00003295 N = DAG.getNode(ISD::ADD, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003296 N.getValueType(), N, IdxN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003297 }
3298 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00003299
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003300 setValue(&I, N);
3301}
3302
Dan Gohman46510a72010-04-15 01:51:59 +00003303void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003304 // If this is a fixed sized alloca in the entry block of the function,
3305 // allocate it statically on the stack.
3306 if (FuncInfo.StaticAllocaMap.count(&I))
3307 return; // getValue will auto-populate this.
3308
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003309 Type *Ty = I.getAllocatedType();
Bill Wendlingba54bca2013-06-19 21:36:55 +00003310 const TargetLowering *TLI = TM.getTargetLowering();
3311 uint64_t TySize = TLI->getDataLayout()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003312 unsigned Align =
Bill Wendlingba54bca2013-06-19 21:36:55 +00003313 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003314 I.getAlignment());
3315
3316 SDValue AllocSize = getValue(I.getArraySize());
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003317
Bill Wendlingba54bca2013-06-19 21:36:55 +00003318 EVT IntPtr = TLI->getPointerTy();
Dan Gohmanf75a7d32010-05-28 01:14:11 +00003319 if (AllocSize.getValueType() != IntPtr)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003320 AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurSDLoc(), IntPtr);
Dan Gohmanf75a7d32010-05-28 01:14:11 +00003321
Andrew Trickac6d9be2013-05-25 02:42:55 +00003322 AllocSize = DAG.getNode(ISD::MUL, getCurSDLoc(), IntPtr,
Dan Gohmanf75a7d32010-05-28 01:14:11 +00003323 AllocSize,
3324 DAG.getConstant(TySize, IntPtr));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003325
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003326 // Handle alignment. If the requested alignment is less than or equal to
3327 // the stack alignment, ignore it. If the size is greater than or equal to
3328 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00003329 unsigned StackAlign = TM.getFrameLowering()->getStackAlignment();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003330 if (Align <= StackAlign)
3331 Align = 0;
3332
3333 // Round the size of the allocation up to the stack alignment size
3334 // by add SA-1 to the size.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003335 AllocSize = DAG.getNode(ISD::ADD, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003336 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003337 DAG.getIntPtrConstant(StackAlign-1));
Bill Wendling856ff412009-12-22 00:12:37 +00003338
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003339 // Mask out the low bits for alignment purposes.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003340 AllocSize = DAG.getNode(ISD::AND, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003341 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003342 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
3343
3344 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Owen Anderson825b72b2009-08-11 20:47:22 +00003345 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003346 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurSDLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003347 VTs, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003348 setValue(&I, DSA);
3349 DAG.setRoot(DSA.getValue(1));
Bill Wendling856ff412009-12-22 00:12:37 +00003350
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003351 // Inform the Frame Information that we have just allocated a variable-sized
3352 // object.
Bob Wilson8f637ad2013-02-08 20:35:15 +00003353 FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject(Align ? Align : 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003354}
3355
Dan Gohman46510a72010-04-15 01:51:59 +00003356void SelectionDAGBuilder::visitLoad(const LoadInst &I) {
Eli Friedman327236c2011-08-24 20:50:09 +00003357 if (I.isAtomic())
3358 return visitAtomicLoad(I);
3359
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003360 const Value *SV = I.getOperand(0);
3361 SDValue Ptr = getValue(SV);
3362
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003363 Type *Ty = I.getType();
David Greene1e559442010-02-15 17:00:31 +00003364
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003365 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00003366 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Pete Cooperd752e0f2011-11-08 18:42:53 +00003367 bool isInvariant = I.getMetadata("invariant.load") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003368 unsigned Alignment = I.getAlignment();
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00003369 const MDNode *TBAAInfo = I.getMetadata(LLVMContext::MD_tbaa);
Rafael Espindola95d594c2012-03-31 18:14:00 +00003370 const MDNode *Ranges = I.getMetadata(LLVMContext::MD_range);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003371
Owen Andersone50ed302009-08-10 22:56:29 +00003372 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003373 SmallVector<uint64_t, 4> Offsets;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003374 ComputeValueVTs(*TM.getTargetLowering(), Ty, ValueVTs, &Offsets);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003375 unsigned NumValues = ValueVTs.size();
3376 if (NumValues == 0)
3377 return;
3378
3379 SDValue Root;
3380 bool ConstantMemory = false;
Andrew Trickde91f3c2010-11-12 17:50:46 +00003381 if (I.isVolatile() || NumValues > MaxParallelChains)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003382 // Serialize volatile loads with other side effects.
3383 Root = getRoot();
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00003384 else if (AA->pointsToConstantMemory(
3385 AliasAnalysis::Location(SV, AA->getTypeStoreSize(Ty), TBAAInfo))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003386 // Do not serialize (non-volatile) loads of constant memory with anything.
3387 Root = DAG.getEntryNode();
3388 ConstantMemory = true;
3389 } else {
3390 // Do not serialize non-volatile loads against each other.
3391 Root = DAG.getRoot();
3392 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003393
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003394 SmallVector<SDValue, 4> Values(NumValues);
Andrew Trickde91f3c2010-11-12 17:50:46 +00003395 SmallVector<SDValue, 4> Chains(std::min(unsigned(MaxParallelChains),
3396 NumValues));
Owen Andersone50ed302009-08-10 22:56:29 +00003397 EVT PtrVT = Ptr.getValueType();
Andrew Trickde91f3c2010-11-12 17:50:46 +00003398 unsigned ChainI = 0;
3399 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
3400 // Serializing loads here may result in excessive register pressure, and
3401 // TokenFactor places arbitrary choke points on the scheduler. SD scheduling
3402 // could recover a bit by hoisting nodes upward in the chain by recognizing
3403 // they are side-effect free or do not alias. The optimizer should really
3404 // avoid this case by converting large object/array copies to llvm.memcpy
3405 // (MaxParallelChains should always remain as failsafe).
3406 if (ChainI == MaxParallelChains) {
3407 assert(PendingLoads.empty() && "PendingLoads must be serialized first");
Andrew Trickac6d9be2013-05-25 02:42:55 +00003408 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003409 MVT::Other, &Chains[0], ChainI);
3410 Root = Chain;
3411 ChainI = 0;
3412 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00003413 SDValue A = DAG.getNode(ISD::ADD, getCurSDLoc(),
Bill Wendling856ff412009-12-22 00:12:37 +00003414 PtrVT, Ptr,
3415 DAG.getConstant(Offsets[i], PtrVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003416 SDValue L = DAG.getLoad(ValueVTs[i], getCurSDLoc(), Root,
Michael J. Spencere70c5262010-10-16 08:25:21 +00003417 A, MachinePointerInfo(SV, Offsets[i]), isVolatile,
Rafael Espindola95d594c2012-03-31 18:14:00 +00003418 isNonTemporal, isInvariant, Alignment, TBAAInfo,
3419 Ranges);
Bill Wendling856ff412009-12-22 00:12:37 +00003420
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003421 Values[i] = L;
Andrew Trickde91f3c2010-11-12 17:50:46 +00003422 Chains[ChainI] = L.getValue(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003423 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003424
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003425 if (!ConstantMemory) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003426 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003427 MVT::Other, &Chains[0], ChainI);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003428 if (isVolatile)
3429 DAG.setRoot(Chain);
3430 else
3431 PendingLoads.push_back(Chain);
3432 }
3433
Andrew Trickac6d9be2013-05-25 02:42:55 +00003434 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00003435 DAG.getVTList(&ValueVTs[0], NumValues),
3436 &Values[0], NumValues));
Bill Wendling856ff412009-12-22 00:12:37 +00003437}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003438
Dan Gohman46510a72010-04-15 01:51:59 +00003439void SelectionDAGBuilder::visitStore(const StoreInst &I) {
Eli Friedman327236c2011-08-24 20:50:09 +00003440 if (I.isAtomic())
3441 return visitAtomicStore(I);
3442
Dan Gohman46510a72010-04-15 01:51:59 +00003443 const Value *SrcV = I.getOperand(0);
3444 const Value *PtrV = I.getOperand(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003445
Owen Andersone50ed302009-08-10 22:56:29 +00003446 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003447 SmallVector<uint64_t, 4> Offsets;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003448 ComputeValueVTs(*TM.getTargetLowering(), SrcV->getType(), ValueVTs, &Offsets);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003449 unsigned NumValues = ValueVTs.size();
3450 if (NumValues == 0)
3451 return;
3452
3453 // Get the lowered operands. Note that we do this after
3454 // checking if NumResults is zero, because with zero results
3455 // the operands won't have values in the map.
3456 SDValue Src = getValue(SrcV);
3457 SDValue Ptr = getValue(PtrV);
3458
3459 SDValue Root = getRoot();
Andrew Trickde91f3c2010-11-12 17:50:46 +00003460 SmallVector<SDValue, 4> Chains(std::min(unsigned(MaxParallelChains),
3461 NumValues));
Owen Andersone50ed302009-08-10 22:56:29 +00003462 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003463 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00003464 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003465 unsigned Alignment = I.getAlignment();
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00003466 const MDNode *TBAAInfo = I.getMetadata(LLVMContext::MD_tbaa);
Bill Wendling856ff412009-12-22 00:12:37 +00003467
Andrew Trickde91f3c2010-11-12 17:50:46 +00003468 unsigned ChainI = 0;
3469 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
3470 // See visitLoad comments.
3471 if (ChainI == MaxParallelChains) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003472 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003473 MVT::Other, &Chains[0], ChainI);
3474 Root = Chain;
3475 ChainI = 0;
3476 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00003477 SDValue Add = DAG.getNode(ISD::ADD, getCurSDLoc(), PtrVT, Ptr,
Bill Wendling856ff412009-12-22 00:12:37 +00003478 DAG.getConstant(Offsets[i], PtrVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003479 SDValue St = DAG.getStore(Root, getCurSDLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003480 SDValue(Src.getNode(), Src.getResNo() + i),
3481 Add, MachinePointerInfo(PtrV, Offsets[i]),
3482 isVolatile, isNonTemporal, Alignment, TBAAInfo);
3483 Chains[ChainI] = St;
Bill Wendling856ff412009-12-22 00:12:37 +00003484 }
3485
Andrew Trickac6d9be2013-05-25 02:42:55 +00003486 SDValue StoreNode = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003487 MVT::Other, &Chains[0], ChainI);
Devang Patel7e13efa2010-10-26 22:14:52 +00003488 DAG.setRoot(StoreNode);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003489}
3490
Eli Friedman26689ac2011-08-03 21:06:02 +00003491static SDValue InsertFenceForAtomic(SDValue Chain, AtomicOrdering Order,
Eli Friedman327236c2011-08-24 20:50:09 +00003492 SynchronizationScope Scope,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003493 bool Before, SDLoc dl,
Eli Friedman26689ac2011-08-03 21:06:02 +00003494 SelectionDAG &DAG,
3495 const TargetLowering &TLI) {
3496 // Fence, if necessary
3497 if (Before) {
Eli Friedman069e2ed2011-08-26 02:59:24 +00003498 if (Order == AcquireRelease || Order == SequentiallyConsistent)
Eli Friedman26689ac2011-08-03 21:06:02 +00003499 Order = Release;
3500 else if (Order == Acquire || Order == Monotonic)
3501 return Chain;
3502 } else {
3503 if (Order == AcquireRelease)
3504 Order = Acquire;
3505 else if (Order == Release || Order == Monotonic)
3506 return Chain;
3507 }
3508 SDValue Ops[3];
3509 Ops[0] = Chain;
Eli Friedman327236c2011-08-24 20:50:09 +00003510 Ops[1] = DAG.getConstant(Order, TLI.getPointerTy());
3511 Ops[2] = DAG.getConstant(Scope, TLI.getPointerTy());
Eli Friedman26689ac2011-08-03 21:06:02 +00003512 return DAG.getNode(ISD::ATOMIC_FENCE, dl, MVT::Other, Ops, 3);
3513}
3514
Eli Friedmanff030482011-07-28 21:48:00 +00003515void SelectionDAGBuilder::visitAtomicCmpXchg(const AtomicCmpXchgInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003516 SDLoc dl = getCurSDLoc();
Eli Friedman26689ac2011-08-03 21:06:02 +00003517 AtomicOrdering Order = I.getOrdering();
Eli Friedman327236c2011-08-24 20:50:09 +00003518 SynchronizationScope Scope = I.getSynchScope();
Eli Friedman26689ac2011-08-03 21:06:02 +00003519
3520 SDValue InChain = getRoot();
3521
Bill Wendlingba54bca2013-06-19 21:36:55 +00003522 const TargetLowering *TLI = TM.getTargetLowering();
3523 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003524 InChain = InsertFenceForAtomic(InChain, Order, Scope, true, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003525 DAG, *TLI);
Eli Friedman26689ac2011-08-03 21:06:02 +00003526
Eli Friedman55ba8162011-07-29 03:05:32 +00003527 SDValue L =
Eli Friedman26689ac2011-08-03 21:06:02 +00003528 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl,
Craig Topper0ff11902013-08-15 02:44:19 +00003529 getValue(I.getCompareOperand()).getSimpleValueType(),
Eli Friedman26689ac2011-08-03 21:06:02 +00003530 InChain,
Eli Friedman55ba8162011-07-29 03:05:32 +00003531 getValue(I.getPointerOperand()),
3532 getValue(I.getCompareOperand()),
3533 getValue(I.getNewValOperand()),
3534 MachinePointerInfo(I.getPointerOperand()), 0 /* Alignment */,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003535 TLI->getInsertFencesForAtomic() ? Monotonic : Order,
Eli Friedman327236c2011-08-24 20:50:09 +00003536 Scope);
Eli Friedman26689ac2011-08-03 21:06:02 +00003537
3538 SDValue OutChain = L.getValue(1);
3539
Bill Wendlingba54bca2013-06-19 21:36:55 +00003540 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003541 OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003542 DAG, *TLI);
Eli Friedman26689ac2011-08-03 21:06:02 +00003543
Eli Friedman55ba8162011-07-29 03:05:32 +00003544 setValue(&I, L);
Eli Friedman26689ac2011-08-03 21:06:02 +00003545 DAG.setRoot(OutChain);
Eli Friedmanff030482011-07-28 21:48:00 +00003546}
3547
3548void SelectionDAGBuilder::visitAtomicRMW(const AtomicRMWInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003549 SDLoc dl = getCurSDLoc();
Eli Friedman55ba8162011-07-29 03:05:32 +00003550 ISD::NodeType NT;
3551 switch (I.getOperation()) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003552 default: llvm_unreachable("Unknown atomicrmw operation");
Eli Friedman55ba8162011-07-29 03:05:32 +00003553 case AtomicRMWInst::Xchg: NT = ISD::ATOMIC_SWAP; break;
3554 case AtomicRMWInst::Add: NT = ISD::ATOMIC_LOAD_ADD; break;
3555 case AtomicRMWInst::Sub: NT = ISD::ATOMIC_LOAD_SUB; break;
3556 case AtomicRMWInst::And: NT = ISD::ATOMIC_LOAD_AND; break;
3557 case AtomicRMWInst::Nand: NT = ISD::ATOMIC_LOAD_NAND; break;
3558 case AtomicRMWInst::Or: NT = ISD::ATOMIC_LOAD_OR; break;
3559 case AtomicRMWInst::Xor: NT = ISD::ATOMIC_LOAD_XOR; break;
3560 case AtomicRMWInst::Max: NT = ISD::ATOMIC_LOAD_MAX; break;
3561 case AtomicRMWInst::Min: NT = ISD::ATOMIC_LOAD_MIN; break;
3562 case AtomicRMWInst::UMax: NT = ISD::ATOMIC_LOAD_UMAX; break;
3563 case AtomicRMWInst::UMin: NT = ISD::ATOMIC_LOAD_UMIN; break;
3564 }
Eli Friedman26689ac2011-08-03 21:06:02 +00003565 AtomicOrdering Order = I.getOrdering();
Eli Friedman327236c2011-08-24 20:50:09 +00003566 SynchronizationScope Scope = I.getSynchScope();
Eli Friedman26689ac2011-08-03 21:06:02 +00003567
3568 SDValue InChain = getRoot();
3569
Bill Wendlingba54bca2013-06-19 21:36:55 +00003570 const TargetLowering *TLI = TM.getTargetLowering();
3571 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003572 InChain = InsertFenceForAtomic(InChain, Order, Scope, true, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003573 DAG, *TLI);
Eli Friedman26689ac2011-08-03 21:06:02 +00003574
Eli Friedman55ba8162011-07-29 03:05:32 +00003575 SDValue L =
Eli Friedman26689ac2011-08-03 21:06:02 +00003576 DAG.getAtomic(NT, dl,
Craig Topper0ff11902013-08-15 02:44:19 +00003577 getValue(I.getValOperand()).getSimpleValueType(),
Eli Friedman26689ac2011-08-03 21:06:02 +00003578 InChain,
Eli Friedman55ba8162011-07-29 03:05:32 +00003579 getValue(I.getPointerOperand()),
3580 getValue(I.getValOperand()),
3581 I.getPointerOperand(), 0 /* Alignment */,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003582 TLI->getInsertFencesForAtomic() ? Monotonic : Order,
Eli Friedman327236c2011-08-24 20:50:09 +00003583 Scope);
Eli Friedman26689ac2011-08-03 21:06:02 +00003584
3585 SDValue OutChain = L.getValue(1);
3586
Bill Wendlingba54bca2013-06-19 21:36:55 +00003587 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003588 OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003589 DAG, *TLI);
Eli Friedman26689ac2011-08-03 21:06:02 +00003590
Eli Friedman55ba8162011-07-29 03:05:32 +00003591 setValue(&I, L);
Eli Friedman26689ac2011-08-03 21:06:02 +00003592 DAG.setRoot(OutChain);
Eli Friedmanff030482011-07-28 21:48:00 +00003593}
3594
Eli Friedman47f35132011-07-25 23:16:38 +00003595void SelectionDAGBuilder::visitFence(const FenceInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003596 SDLoc dl = getCurSDLoc();
Bill Wendlingba54bca2013-06-19 21:36:55 +00003597 const TargetLowering *TLI = TM.getTargetLowering();
Eli Friedman14648462011-07-27 22:21:52 +00003598 SDValue Ops[3];
3599 Ops[0] = getRoot();
Bill Wendlingba54bca2013-06-19 21:36:55 +00003600 Ops[1] = DAG.getConstant(I.getOrdering(), TLI->getPointerTy());
3601 Ops[2] = DAG.getConstant(I.getSynchScope(), TLI->getPointerTy());
Eli Friedman14648462011-07-27 22:21:52 +00003602 DAG.setRoot(DAG.getNode(ISD::ATOMIC_FENCE, dl, MVT::Other, Ops, 3));
Eli Friedman47f35132011-07-25 23:16:38 +00003603}
3604
Eli Friedman327236c2011-08-24 20:50:09 +00003605void SelectionDAGBuilder::visitAtomicLoad(const LoadInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003606 SDLoc dl = getCurSDLoc();
Eli Friedman327236c2011-08-24 20:50:09 +00003607 AtomicOrdering Order = I.getOrdering();
3608 SynchronizationScope Scope = I.getSynchScope();
3609
3610 SDValue InChain = getRoot();
3611
Bill Wendlingba54bca2013-06-19 21:36:55 +00003612 const TargetLowering *TLI = TM.getTargetLowering();
3613 EVT VT = TLI->getValueType(I.getType());
Eli Friedman327236c2011-08-24 20:50:09 +00003614
Evan Cheng607acd62013-02-06 02:06:33 +00003615 if (I.getAlignment() < VT.getSizeInBits() / 8)
Eli Friedmanfe731212011-09-13 20:50:54 +00003616 report_fatal_error("Cannot generate unaligned atomic load");
3617
Eli Friedman327236c2011-08-24 20:50:09 +00003618 SDValue L =
3619 DAG.getAtomic(ISD::ATOMIC_LOAD, dl, VT, VT, InChain,
3620 getValue(I.getPointerOperand()),
3621 I.getPointerOperand(), I.getAlignment(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00003622 TLI->getInsertFencesForAtomic() ? Monotonic : Order,
Eli Friedman327236c2011-08-24 20:50:09 +00003623 Scope);
3624
3625 SDValue OutChain = L.getValue(1);
3626
Bill Wendlingba54bca2013-06-19 21:36:55 +00003627 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003628 OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003629 DAG, *TLI);
Eli Friedman327236c2011-08-24 20:50:09 +00003630
3631 setValue(&I, L);
3632 DAG.setRoot(OutChain);
3633}
3634
3635void SelectionDAGBuilder::visitAtomicStore(const StoreInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003636 SDLoc dl = getCurSDLoc();
Eli Friedman327236c2011-08-24 20:50:09 +00003637
3638 AtomicOrdering Order = I.getOrdering();
3639 SynchronizationScope Scope = I.getSynchScope();
3640
3641 SDValue InChain = getRoot();
3642
Bill Wendlingba54bca2013-06-19 21:36:55 +00003643 const TargetLowering *TLI = TM.getTargetLowering();
3644 EVT VT = TLI->getValueType(I.getValueOperand()->getType());
Eli Friedmanfe731212011-09-13 20:50:54 +00003645
Evan Cheng607acd62013-02-06 02:06:33 +00003646 if (I.getAlignment() < VT.getSizeInBits() / 8)
Eli Friedmanfe731212011-09-13 20:50:54 +00003647 report_fatal_error("Cannot generate unaligned atomic store");
3648
Bill Wendlingba54bca2013-06-19 21:36:55 +00003649 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003650 InChain = InsertFenceForAtomic(InChain, Order, Scope, true, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003651 DAG, *TLI);
Eli Friedman327236c2011-08-24 20:50:09 +00003652
3653 SDValue OutChain =
Eli Friedmanfe731212011-09-13 20:50:54 +00003654 DAG.getAtomic(ISD::ATOMIC_STORE, dl, VT,
Eli Friedman327236c2011-08-24 20:50:09 +00003655 InChain,
3656 getValue(I.getPointerOperand()),
3657 getValue(I.getValueOperand()),
3658 I.getPointerOperand(), I.getAlignment(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00003659 TLI->getInsertFencesForAtomic() ? Monotonic : Order,
Eli Friedman327236c2011-08-24 20:50:09 +00003660 Scope);
3661
Bill Wendlingba54bca2013-06-19 21:36:55 +00003662 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003663 OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003664 DAG, *TLI);
Eli Friedman327236c2011-08-24 20:50:09 +00003665
3666 DAG.setRoot(OutChain);
3667}
3668
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003669/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
3670/// node.
Dan Gohman46510a72010-04-15 01:51:59 +00003671void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I,
Dan Gohman2048b852009-11-23 18:04:58 +00003672 unsigned Intrinsic) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003673 bool HasChain = !I.doesNotAccessMemory();
3674 bool OnlyLoad = HasChain && I.onlyReadsMemory();
3675
3676 // Build the operand list.
3677 SmallVector<SDValue, 8> Ops;
3678 if (HasChain) { // If this intrinsic has side-effects, chainify it.
3679 if (OnlyLoad) {
3680 // We don't need to serialize loads against other loads.
3681 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003682 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003683 Ops.push_back(getRoot());
3684 }
3685 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003686
3687 // Info is set by getTgtMemInstrinsic
3688 TargetLowering::IntrinsicInfo Info;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003689 const TargetLowering *TLI = TM.getTargetLowering();
3690 bool IsTgtIntrinsic = TLI->getTgtMemIntrinsic(Info, I, Intrinsic);
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003691
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003692 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Bob Wilson65ffec42010-09-21 17:56:22 +00003693 if (!IsTgtIntrinsic || Info.opc == ISD::INTRINSIC_VOID ||
3694 Info.opc == ISD::INTRINSIC_W_CHAIN)
Bill Wendlingba54bca2013-06-19 21:36:55 +00003695 Ops.push_back(DAG.getTargetConstant(Intrinsic, TLI->getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003696
3697 // Add all operands of the call to the operand list.
Gabor Greif0635f352010-06-25 09:38:13 +00003698 for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) {
3699 SDValue Op = getValue(I.getArgOperand(i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003700 Ops.push_back(Op);
3701 }
3702
Owen Andersone50ed302009-08-10 22:56:29 +00003703 SmallVector<EVT, 4> ValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003704 ComputeValueVTs(*TLI, I.getType(), ValueVTs);
Bill Wendling856ff412009-12-22 00:12:37 +00003705
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003706 if (HasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00003707 ValueVTs.push_back(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003708
Bob Wilson8d919552009-07-31 22:41:21 +00003709 SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003710
3711 // Create the node.
3712 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003713 if (IsTgtIntrinsic) {
3714 // This is target intrinsic that touches memory
Andrew Trickac6d9be2013-05-25 02:42:55 +00003715 Result = DAG.getMemIntrinsicNode(Info.opc, getCurSDLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003716 VTs, &Ops[0], Ops.size(),
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00003717 Info.memVT,
3718 MachinePointerInfo(Info.ptrVal, Info.offset),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003719 Info.align, Info.vol,
3720 Info.readMem, Info.writeMem);
Bill Wendling856ff412009-12-22 00:12:37 +00003721 } else if (!HasChain) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003722 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurSDLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003723 VTs, &Ops[0], Ops.size());
Benjamin Kramerf0127052010-01-05 13:12:22 +00003724 } else if (!I.getType()->isVoidTy()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003725 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurSDLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003726 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003727 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003728 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003729 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003730 }
3731
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003732 if (HasChain) {
3733 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
3734 if (OnlyLoad)
3735 PendingLoads.push_back(Chain);
3736 else
3737 DAG.setRoot(Chain);
3738 }
Bill Wendling856ff412009-12-22 00:12:37 +00003739
Benjamin Kramerf0127052010-01-05 13:12:22 +00003740 if (!I.getType()->isVoidTy()) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003741 if (VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00003742 EVT VT = TLI->getValueType(PTy);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003743 Result = DAG.getNode(ISD::BITCAST, getCurSDLoc(), VT, Result);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003744 }
Bill Wendling856ff412009-12-22 00:12:37 +00003745
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003746 setValue(&I, Result);
3747 }
3748}
3749
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003750/// GetSignificand - Get the significand and build it into a floating-point
3751/// number with exponent of 1:
3752///
3753/// Op = (Op & 0x007fffff) | 0x3f800000;
3754///
Matt Beaumont-Gay50e75bf2013-02-25 18:11:18 +00003755/// where Op is the hexadecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003756static SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00003757GetSignificand(SelectionDAG &DAG, SDValue Op, SDLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003758 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3759 DAG.getConstant(0x007fffff, MVT::i32));
3760 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
3761 DAG.getConstant(0x3f800000, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003762 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00003763}
3764
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003765/// GetExponent - Get the exponent:
3766///
Bill Wendlinge9a72862009-01-20 21:17:57 +00003767/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003768///
Matt Beaumont-Gay50e75bf2013-02-25 18:11:18 +00003769/// where Op is the hexadecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003770static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00003771GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003772 SDLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003773 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3774 DAG.getConstant(0x7f800000, MVT::i32));
3775 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Duncan Sands92abc622009-01-31 15:50:11 +00003776 DAG.getConstant(23, TLI.getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00003777 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
3778 DAG.getConstant(127, MVT::i32));
Bill Wendling4533cac2010-01-28 21:51:40 +00003779 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00003780}
3781
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003782/// getF32Constant - Get 32-bit floating point constant.
3783static SDValue
3784getF32Constant(SelectionDAG &DAG, unsigned Flt) {
Tim Northover0a29cb02013-01-22 09:46:31 +00003785 return DAG.getConstantFP(APFloat(APFloat::IEEEsingle, APInt(32, Flt)),
3786 MVT::f32);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003787}
3788
Craig Topper538cd482012-11-24 18:52:06 +00003789/// expandExp - Lower an exp intrinsic. Handles the special sequences for
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003790/// limited-precision mode.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003791static SDValue expandExp(SDLoc dl, SDValue Op, SelectionDAG &DAG,
Craig Topper538cd482012-11-24 18:52:06 +00003792 const TargetLowering &TLI) {
3793 if (Op.getValueType() == MVT::f32 &&
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003794 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003795
3796 // Put the exponent in the right bit position for later addition to the
3797 // final result:
3798 //
3799 // #define LOG2OFe 1.4426950f
3800 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Owen Anderson825b72b2009-08-11 20:47:22 +00003801 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003802 getF32Constant(DAG, 0x3fb8aa3b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003803 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003804
3805 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003806 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3807 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003808
3809 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003810 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003811 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendling856ff412009-12-22 00:12:37 +00003812
Craig Topperb3157722012-11-24 08:22:37 +00003813 SDValue TwoToFracPartOfX;
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003814 if (LimitFloatPrecision <= 6) {
3815 // For floating-point precision of 6:
3816 //
3817 // TwoToFractionalPartOfX =
3818 // 0.997535578f +
3819 // (0.735607626f + 0.252464424f * x) * x;
3820 //
3821 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003822 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003823 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003824 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003825 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003826 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
Craig Topperb3157722012-11-24 08:22:37 +00003827 TwoToFracPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
3828 getF32Constant(DAG, 0x3f7f5e7e));
Craig Topper08ac4692012-11-16 20:01:39 +00003829 } else if (LimitFloatPrecision <= 12) {
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003830 // For floating-point precision of 12:
3831 //
3832 // TwoToFractionalPartOfX =
3833 // 0.999892986f +
3834 // (0.696457318f +
3835 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3836 //
3837 // 0.000107046256 error, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003838 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003839 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003840 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003841 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003842 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3843 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003844 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003845 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
Craig Topperb3157722012-11-24 08:22:37 +00003846 TwoToFracPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
3847 getF32Constant(DAG, 0x3f7ff8fd));
Craig Topper08ac4692012-11-16 20:01:39 +00003848 } else { // LimitFloatPrecision <= 18
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003849 // For floating-point precision of 18:
3850 //
3851 // TwoToFractionalPartOfX =
3852 // 0.999999982f +
3853 // (0.693148872f +
3854 // (0.240227044f +
3855 // (0.554906021e-1f +
3856 // (0.961591928e-2f +
3857 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3858 //
3859 // error 2.47208000*10^(-7), which is better than 18 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, 0x3924b03e));
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, 0x3ab24b87));
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, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003867 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3868 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003869 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003870 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3871 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003872 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003873 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3874 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003875 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003876 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
Craig Topperb3157722012-11-24 08:22:37 +00003877 TwoToFracPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
3878 getF32Constant(DAG, 0x3f800000));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003879 }
Craig Topperb3157722012-11-24 08:22:37 +00003880
3881 // Add the exponent into the result in integer domain.
3882 SDValue t13 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, TwoToFracPartOfX);
Craig Topper538cd482012-11-24 18:52:06 +00003883 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3884 DAG.getNode(ISD::ADD, dl, MVT::i32,
3885 t13, IntegerPartOfX));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003886 }
3887
Craig Topper538cd482012-11-24 18:52:06 +00003888 // No special expansion.
3889 return DAG.getNode(ISD::FEXP, dl, Op.getValueType(), Op);
Dale Johannesen59e577f2008-09-05 18:38:42 +00003890}
3891
Craig Topper5d1e0892012-11-23 18:38:31 +00003892/// expandLog - Lower a log intrinsic. Handles the special sequences for
Bill Wendling39150252008-09-09 20:39:27 +00003893/// limited-precision mode.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003894static SDValue expandLog(SDLoc dl, SDValue Op, SelectionDAG &DAG,
Craig Topper5d1e0892012-11-23 18:38:31 +00003895 const TargetLowering &TLI) {
3896 if (Op.getValueType() == MVT::f32 &&
Bill Wendling39150252008-09-09 20:39:27 +00003897 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003898 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00003899
3900 // Scale the exponent by log(2) [0.69314718f].
Bill Wendling46ada192010-03-02 01:55:18 +00003901 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003902 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003903 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003904
3905 // Get the significand and build it into a floating-point number with
3906 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003907 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling39150252008-09-09 20:39:27 +00003908
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003909 SDValue LogOfMantissa;
Bill Wendling39150252008-09-09 20:39:27 +00003910 if (LimitFloatPrecision <= 6) {
3911 // For floating-point precision of 6:
3912 //
3913 // LogofMantissa =
3914 // -1.1609546f +
3915 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003916 //
Bill Wendling39150252008-09-09 20:39:27 +00003917 // error 0.0034276066, which is better than 8 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003918 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003919 getF32Constant(DAG, 0xbe74c456));
Owen Anderson825b72b2009-08-11 20:47:22 +00003920 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003921 getF32Constant(DAG, 0x3fb3a2b1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003922 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003923 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
3924 getF32Constant(DAG, 0x3f949a29));
Craig Topper08ac4692012-11-16 20:01:39 +00003925 } else if (LimitFloatPrecision <= 12) {
Bill Wendling39150252008-09-09 20:39:27 +00003926 // For floating-point precision of 12:
3927 //
3928 // LogOfMantissa =
3929 // -1.7417939f +
3930 // (2.8212026f +
3931 // (-1.4699568f +
3932 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3933 //
3934 // error 0.000061011436, which is 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003935 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003936 getF32Constant(DAG, 0xbd67b6d6));
Owen Anderson825b72b2009-08-11 20:47:22 +00003937 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003938 getF32Constant(DAG, 0x3ee4f4b8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003939 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3940 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003941 getF32Constant(DAG, 0x3fbc278b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003942 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3943 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003944 getF32Constant(DAG, 0x40348e95));
Owen Anderson825b72b2009-08-11 20:47:22 +00003945 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003946 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
3947 getF32Constant(DAG, 0x3fdef31a));
Craig Topper08ac4692012-11-16 20:01:39 +00003948 } else { // LimitFloatPrecision <= 18
Bill Wendling39150252008-09-09 20:39:27 +00003949 // For floating-point precision of 18:
3950 //
3951 // LogOfMantissa =
3952 // -2.1072184f +
3953 // (4.2372794f +
3954 // (-3.7029485f +
3955 // (2.2781945f +
3956 // (-0.87823314f +
3957 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3958 //
3959 // error 0.0000023660568, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003960 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003961 getF32Constant(DAG, 0xbc91e5ac));
Owen Anderson825b72b2009-08-11 20:47:22 +00003962 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003963 getF32Constant(DAG, 0x3e4350aa));
Owen Anderson825b72b2009-08-11 20:47:22 +00003964 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3965 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003966 getF32Constant(DAG, 0x3f60d3e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003967 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3968 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003969 getF32Constant(DAG, 0x4011cdf0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003970 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3971 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003972 getF32Constant(DAG, 0x406cfd1c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003973 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3974 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003975 getF32Constant(DAG, 0x408797cb));
Owen Anderson825b72b2009-08-11 20:47:22 +00003976 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003977 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
3978 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003979 }
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003980
Craig Topper5d1e0892012-11-23 18:38:31 +00003981 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003982 }
3983
Craig Topper5d1e0892012-11-23 18:38:31 +00003984 // No special expansion.
3985 return DAG.getNode(ISD::FLOG, dl, Op.getValueType(), Op);
Dale Johannesen59e577f2008-09-05 18:38:42 +00003986}
3987
Craig Topper5d1e0892012-11-23 18:38:31 +00003988/// expandLog2 - Lower a log2 intrinsic. Handles the special sequences for
Bill Wendling3eb59402008-09-09 00:28:24 +00003989/// limited-precision mode.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003990static SDValue expandLog2(SDLoc dl, SDValue Op, SelectionDAG &DAG,
Craig Topper5d1e0892012-11-23 18:38:31 +00003991 const TargetLowering &TLI) {
3992 if (Op.getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003993 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003994 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003995
Bill Wendling39150252008-09-09 20:39:27 +00003996 // Get the exponent.
Bill Wendling46ada192010-03-02 01:55:18 +00003997 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
Bill Wendling856ff412009-12-22 00:12:37 +00003998
Bill Wendling3eb59402008-09-09 00:28:24 +00003999 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00004000 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00004001 SDValue X = GetSignificand(DAG, Op1, dl);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004002
Bill Wendling3eb59402008-09-09 00:28:24 +00004003 // Different possible minimax approximations of significand in
4004 // floating-point for various degrees of accuracy over [1,2].
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004005 SDValue Log2ofMantissa;
Bill Wendling3eb59402008-09-09 00:28:24 +00004006 if (LimitFloatPrecision <= 6) {
4007 // For floating-point precision of 6:
4008 //
4009 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
4010 //
4011 // error 0.0049451742, which is more than 7 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004012 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004013 getF32Constant(DAG, 0xbeb08fe0));
Owen Anderson825b72b2009-08-11 20:47:22 +00004014 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004015 getF32Constant(DAG, 0x40019463));
Owen Anderson825b72b2009-08-11 20:47:22 +00004016 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004017 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
4018 getF32Constant(DAG, 0x3fd6633d));
Craig Topper08ac4692012-11-16 20:01:39 +00004019 } else if (LimitFloatPrecision <= 12) {
Bill Wendling3eb59402008-09-09 00:28:24 +00004020 // For floating-point precision of 12:
4021 //
4022 // Log2ofMantissa =
4023 // -2.51285454f +
4024 // (4.07009056f +
4025 // (-2.12067489f +
4026 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004027 //
Bill Wendling3eb59402008-09-09 00:28:24 +00004028 // error 0.0000876136000, which is better than 13 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004029 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004030 getF32Constant(DAG, 0xbda7262e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004031 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004032 getF32Constant(DAG, 0x3f25280b));
Owen Anderson825b72b2009-08-11 20:47:22 +00004033 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
4034 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004035 getF32Constant(DAG, 0x4007b923));
Owen Anderson825b72b2009-08-11 20:47:22 +00004036 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4037 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004038 getF32Constant(DAG, 0x40823e2f));
Owen Anderson825b72b2009-08-11 20:47:22 +00004039 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004040 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
4041 getF32Constant(DAG, 0x4020d29c));
Craig Topper08ac4692012-11-16 20:01:39 +00004042 } else { // LimitFloatPrecision <= 18
Bill Wendling3eb59402008-09-09 00:28:24 +00004043 // For floating-point precision of 18:
4044 //
4045 // Log2ofMantissa =
4046 // -3.0400495f +
4047 // (6.1129976f +
4048 // (-5.3420409f +
4049 // (3.2865683f +
4050 // (-1.2669343f +
4051 // (0.27515199f -
4052 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
4053 //
4054 // error 0.0000018516, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004055 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004056 getF32Constant(DAG, 0xbcd2769e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004057 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004058 getF32Constant(DAG, 0x3e8ce0b9));
Owen Anderson825b72b2009-08-11 20:47:22 +00004059 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
4060 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004061 getF32Constant(DAG, 0x3fa22ae7));
Owen Anderson825b72b2009-08-11 20:47:22 +00004062 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4063 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004064 getF32Constant(DAG, 0x40525723));
Owen Anderson825b72b2009-08-11 20:47:22 +00004065 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4066 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004067 getF32Constant(DAG, 0x40aaf200));
Owen Anderson825b72b2009-08-11 20:47:22 +00004068 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
4069 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004070 getF32Constant(DAG, 0x40c39dad));
Owen Anderson825b72b2009-08-11 20:47:22 +00004071 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004072 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
4073 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00004074 }
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004075
Craig Topper5d1e0892012-11-23 18:38:31 +00004076 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log2ofMantissa);
Dale Johannesen853244f2008-09-05 23:49:37 +00004077 }
Bill Wendling3eb59402008-09-09 00:28:24 +00004078
Craig Topper5d1e0892012-11-23 18:38:31 +00004079 // No special expansion.
4080 return DAG.getNode(ISD::FLOG2, dl, Op.getValueType(), Op);
Dale Johannesen59e577f2008-09-05 18:38:42 +00004081}
4082
Craig Topper5d1e0892012-11-23 18:38:31 +00004083/// expandLog10 - Lower a log10 intrinsic. Handles the special sequences for
Bill Wendling3eb59402008-09-09 00:28:24 +00004084/// limited-precision mode.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004085static SDValue expandLog10(SDLoc dl, SDValue Op, SelectionDAG &DAG,
Craig Topper5d1e0892012-11-23 18:38:31 +00004086 const TargetLowering &TLI) {
4087 if (Op.getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00004088 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004089 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00004090
Bill Wendling39150252008-09-09 20:39:27 +00004091 // Scale the exponent by log10(2) [0.30102999f].
Bill Wendling46ada192010-03-02 01:55:18 +00004092 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00004093 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004094 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00004095
4096 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00004097 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00004098 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling3eb59402008-09-09 00:28:24 +00004099
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004100 SDValue Log10ofMantissa;
Bill Wendling3eb59402008-09-09 00:28:24 +00004101 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00004102 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004103 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00004104 // Log10ofMantissa =
4105 // -0.50419619f +
4106 // (0.60948995f - 0.10380950f * x) * x;
4107 //
4108 // error 0.0014886165, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004109 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004110 getF32Constant(DAG, 0xbdd49a13));
Owen Anderson825b72b2009-08-11 20:47:22 +00004111 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004112 getF32Constant(DAG, 0x3f1c0789));
Owen Anderson825b72b2009-08-11 20:47:22 +00004113 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004114 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
4115 getF32Constant(DAG, 0x3f011300));
Craig Topper08ac4692012-11-16 20:01:39 +00004116 } else if (LimitFloatPrecision <= 12) {
Bill Wendling3eb59402008-09-09 00:28:24 +00004117 // For floating-point precision of 12:
4118 //
4119 // Log10ofMantissa =
4120 // -0.64831180f +
4121 // (0.91751397f +
4122 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
4123 //
4124 // error 0.00019228036, which is better than 12 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004125 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004126 getF32Constant(DAG, 0x3d431f31));
Owen Anderson825b72b2009-08-11 20:47:22 +00004127 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004128 getF32Constant(DAG, 0x3ea21fb2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004129 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
4130 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004131 getF32Constant(DAG, 0x3f6ae232));
Owen Anderson825b72b2009-08-11 20:47:22 +00004132 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004133 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
4134 getF32Constant(DAG, 0x3f25f7c3));
Craig Topper08ac4692012-11-16 20:01:39 +00004135 } else { // LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00004136 // For floating-point precision of 18:
4137 //
4138 // Log10ofMantissa =
4139 // -0.84299375f +
4140 // (1.5327582f +
4141 // (-1.0688956f +
4142 // (0.49102474f +
4143 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
4144 //
4145 // error 0.0000037995730, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004146 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004147 getF32Constant(DAG, 0x3c5d51ce));
Owen Anderson825b72b2009-08-11 20:47:22 +00004148 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004149 getF32Constant(DAG, 0x3e00685a));
Owen Anderson825b72b2009-08-11 20:47:22 +00004150 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
4151 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004152 getF32Constant(DAG, 0x3efb6798));
Owen Anderson825b72b2009-08-11 20:47:22 +00004153 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4154 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004155 getF32Constant(DAG, 0x3f88d192));
Owen Anderson825b72b2009-08-11 20:47:22 +00004156 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4157 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004158 getF32Constant(DAG, 0x3fc4316c));
Owen Anderson825b72b2009-08-11 20:47:22 +00004159 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004160 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
4161 getF32Constant(DAG, 0x3f57ce70));
Bill Wendling3eb59402008-09-09 00:28:24 +00004162 }
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004163
Craig Topper5d1e0892012-11-23 18:38:31 +00004164 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log10ofMantissa);
Dale Johannesen852680a2008-09-05 21:27:19 +00004165 }
Bill Wendling3eb59402008-09-09 00:28:24 +00004166
Craig Topper5d1e0892012-11-23 18:38:31 +00004167 // No special expansion.
4168 return DAG.getNode(ISD::FLOG10, dl, Op.getValueType(), Op);
Dale Johannesen59e577f2008-09-05 18:38:42 +00004169}
4170
Craig Topper538cd482012-11-24 18:52:06 +00004171/// expandExp2 - Lower an exp2 intrinsic. Handles the special sequences for
Bill Wendlinge10c8142008-09-09 22:39:21 +00004172/// limited-precision mode.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004173static SDValue expandExp2(SDLoc dl, SDValue Op, SelectionDAG &DAG,
Craig Topper538cd482012-11-24 18:52:06 +00004174 const TargetLowering &TLI) {
4175 if (Op.getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00004176 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004177 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00004178
4179 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00004180 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
4181 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00004182
4183 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00004184 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00004185 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00004186
Craig Topperb3157722012-11-24 08:22:37 +00004187 SDValue TwoToFractionalPartOfX;
Bill Wendlinge10c8142008-09-09 22:39:21 +00004188 if (LimitFloatPrecision <= 6) {
4189 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004190 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00004191 // TwoToFractionalPartOfX =
4192 // 0.997535578f +
4193 // (0.735607626f + 0.252464424f * x) * x;
4194 //
4195 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004196 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004197 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00004198 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004199 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004200 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
Craig Topperb3157722012-11-24 08:22:37 +00004201 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
4202 getF32Constant(DAG, 0x3f7f5e7e));
Craig Topper08ac4692012-11-16 20:01:39 +00004203 } else if (LimitFloatPrecision <= 12) {
Bill Wendlinge10c8142008-09-09 22:39:21 +00004204 // For floating-point precision of 12:
4205 //
4206 // TwoToFractionalPartOfX =
4207 // 0.999892986f +
4208 // (0.696457318f +
4209 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
4210 //
4211 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004212 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004213 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004214 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004215 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004216 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4217 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004218 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00004219 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
Craig Topperb3157722012-11-24 08:22:37 +00004220 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
4221 getF32Constant(DAG, 0x3f7ff8fd));
Craig Topper08ac4692012-11-16 20:01:39 +00004222 } else { // LimitFloatPrecision <= 18
Bill Wendlinge10c8142008-09-09 22:39:21 +00004223 // For floating-point precision of 18:
4224 //
4225 // TwoToFractionalPartOfX =
4226 // 0.999999982f +
4227 // (0.693148872f +
4228 // (0.240227044f +
4229 // (0.554906021e-1f +
4230 // (0.961591928e-2f +
4231 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
4232 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004233 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004234 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004235 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004236 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00004237 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4238 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004239 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00004240 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4241 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004242 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00004243 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
4244 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004245 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00004246 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
4247 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004248 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00004249 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
Craig Topperb3157722012-11-24 08:22:37 +00004250 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
4251 getF32Constant(DAG, 0x3f800000));
Bill Wendlinge10c8142008-09-09 22:39:21 +00004252 }
Craig Topperb3157722012-11-24 08:22:37 +00004253
4254 // Add the exponent into the result in integer domain.
4255 SDValue t13 = DAG.getNode(ISD::BITCAST, dl, MVT::i32,
4256 TwoToFractionalPartOfX);
Craig Topper538cd482012-11-24 18:52:06 +00004257 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
4258 DAG.getNode(ISD::ADD, dl, MVT::i32,
4259 t13, IntegerPartOfX));
Dale Johannesen601d3c02008-09-05 01:48:15 +00004260 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00004261
Craig Topper538cd482012-11-24 18:52:06 +00004262 // No special expansion.
4263 return DAG.getNode(ISD::FEXP2, dl, Op.getValueType(), Op);
Dale Johannesen601d3c02008-09-05 01:48:15 +00004264}
4265
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004266/// visitPow - Lower a pow intrinsic. Handles the special sequences for
4267/// limited-precision mode with x == 10.0f.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004268static SDValue expandPow(SDLoc dl, SDValue LHS, SDValue RHS,
Craig Topper327e4cb2012-11-25 08:08:58 +00004269 SelectionDAG &DAG, const TargetLowering &TLI) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004270 bool IsExp10 = false;
Craig Topper327e4cb2012-11-25 08:08:58 +00004271 if (LHS.getValueType() == MVT::f32 && LHS.getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004272 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Craig Topper327e4cb2012-11-25 08:08:58 +00004273 if (ConstantFPSDNode *LHSC = dyn_cast<ConstantFPSDNode>(LHS)) {
4274 APFloat Ten(10.0f);
4275 IsExp10 = LHSC->isExactlyValue(Ten);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004276 }
4277 }
4278
Craig Topperc1aa6382012-11-25 00:48:58 +00004279 if (IsExp10) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004280 // Put the exponent in the right bit position for later addition to the
4281 // final result:
4282 //
4283 // #define LOG2OF10 3.3219281f
4284 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Craig Topper327e4cb2012-11-25 08:08:58 +00004285 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, RHS,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004286 getF32Constant(DAG, 0x40549a78));
Owen Anderson825b72b2009-08-11 20:47:22 +00004287 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004288
4289 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00004290 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
4291 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004292
4293 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00004294 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00004295 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004296
Craig Topper915562e2012-11-25 00:15:07 +00004297 SDValue TwoToFractionalPartOfX;
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004298 if (LimitFloatPrecision <= 6) {
4299 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004300 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004301 // twoToFractionalPartOfX =
4302 // 0.997535578f +
4303 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004304 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004305 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004306 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004307 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00004308 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004309 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004310 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
Craig Topper915562e2012-11-25 00:15:07 +00004311 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
4312 getF32Constant(DAG, 0x3f7f5e7e));
Craig Topper08ac4692012-11-16 20:01:39 +00004313 } else if (LimitFloatPrecision <= 12) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004314 // For floating-point precision of 12:
4315 //
4316 // TwoToFractionalPartOfX =
4317 // 0.999892986f +
4318 // (0.696457318f +
4319 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
4320 //
4321 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004322 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004323 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004324 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004325 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004326 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4327 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004328 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00004329 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
Craig Topper915562e2012-11-25 00:15:07 +00004330 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
4331 getF32Constant(DAG, 0x3f7ff8fd));
Craig Topper08ac4692012-11-16 20:01:39 +00004332 } else { // LimitFloatPrecision <= 18
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004333 // For floating-point precision of 18:
4334 //
4335 // TwoToFractionalPartOfX =
4336 // 0.999999982f +
4337 // (0.693148872f +
4338 // (0.240227044f +
4339 // (0.554906021e-1f +
4340 // (0.961591928e-2f +
4341 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
4342 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004343 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004344 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004345 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004346 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00004347 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4348 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004349 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00004350 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4351 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004352 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00004353 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
4354 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004355 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00004356 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
4357 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004358 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00004359 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
Craig Topper915562e2012-11-25 00:15:07 +00004360 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
4361 getF32Constant(DAG, 0x3f800000));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004362 }
Craig Topper915562e2012-11-25 00:15:07 +00004363
4364 SDValue t13 = DAG.getNode(ISD::BITCAST, dl,MVT::i32,TwoToFractionalPartOfX);
Craig Topper327e4cb2012-11-25 08:08:58 +00004365 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
4366 DAG.getNode(ISD::ADD, dl, MVT::i32,
4367 t13, IntegerPartOfX));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004368 }
4369
Craig Topper327e4cb2012-11-25 08:08:58 +00004370 // No special expansion.
4371 return DAG.getNode(ISD::FPOW, dl, LHS.getValueType(), LHS, RHS);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004372}
4373
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004374
4375/// ExpandPowI - Expand a llvm.powi intrinsic.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004376static SDValue ExpandPowI(SDLoc DL, SDValue LHS, SDValue RHS,
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004377 SelectionDAG &DAG) {
4378 // If RHS is a constant, we can expand this out to a multiplication tree,
4379 // otherwise we end up lowering to a call to __powidf2 (for example). When
4380 // optimizing for size, we only want to do this if the expansion would produce
4381 // a small number of multiplies, otherwise we do the full expansion.
4382 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
4383 // Get the exponent as a positive value.
4384 unsigned Val = RHSC->getSExtValue();
4385 if ((int)Val < 0) Val = -Val;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004386
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004387 // powi(x, 0) -> 1.0
4388 if (Val == 0)
4389 return DAG.getConstantFP(1.0, LHS.getValueType());
4390
Dan Gohmanae541aa2010-04-15 04:33:49 +00004391 const Function *F = DAG.getMachineFunction().getFunction();
Bill Wendling831737d2012-12-30 10:32:01 +00004392 if (!F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
4393 Attribute::OptimizeForSize) ||
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004394 // If optimizing for size, don't insert too many multiplies. This
4395 // inserts up to 5 multiplies.
4396 CountPopulation_32(Val)+Log2_32(Val) < 7) {
4397 // We use the simple binary decomposition method to generate the multiply
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004398 // sequence. There are more optimal ways to do this (for example,
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004399 // powi(x,15) generates one more multiply than it should), but this has
4400 // the benefit of being both really simple and much better than a libcall.
4401 SDValue Res; // Logically starts equal to 1.0
4402 SDValue CurSquare = LHS;
4403 while (Val) {
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00004404 if (Val & 1) {
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004405 if (Res.getNode())
4406 Res = DAG.getNode(ISD::FMUL, DL,Res.getValueType(), Res, CurSquare);
4407 else
4408 Res = CurSquare; // 1.0*CurSquare.
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00004409 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004410
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004411 CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
4412 CurSquare, CurSquare);
4413 Val >>= 1;
4414 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004415
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004416 // If the original was negative, invert the result, producing 1/(x*x*x).
4417 if (RHSC->getSExtValue() < 0)
4418 Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(),
4419 DAG.getConstantFP(1.0, LHS.getValueType()), Res);
4420 return Res;
4421 }
4422 }
4423
4424 // Otherwise, expand to a libcall.
4425 return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS);
4426}
4427
Devang Patel227dfdb2011-05-16 21:24:05 +00004428// getTruncatedArgReg - Find underlying register used for an truncated
4429// argument.
4430static unsigned getTruncatedArgReg(const SDValue &N) {
4431 if (N.getOpcode() != ISD::TRUNCATE)
4432 return 0;
4433
4434 const SDValue &Ext = N.getOperand(0);
Stephen Lin09f8ca32013-07-06 21:44:25 +00004435 if (Ext.getOpcode() == ISD::AssertZext ||
4436 Ext.getOpcode() == ISD::AssertSext) {
Devang Patel227dfdb2011-05-16 21:24:05 +00004437 const SDValue &CFR = Ext.getOperand(0);
4438 if (CFR.getOpcode() == ISD::CopyFromReg)
4439 return cast<RegisterSDNode>(CFR.getOperand(1))->getReg();
Craig Topper7eb46d82012-04-11 04:55:51 +00004440 if (CFR.getOpcode() == ISD::TRUNCATE)
4441 return getTruncatedArgReg(CFR);
Devang Patel227dfdb2011-05-16 21:24:05 +00004442 }
4443 return 0;
4444}
4445
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004446/// EmitFuncArgumentDbgValue - If the DbgValueInst is a dbg_value of a function
4447/// argument, create the corresponding DBG_VALUE machine instruction for it now.
4448/// At the end of instruction selection, they will be inserted to the entry BB.
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004449bool
Devang Patel78a06e52010-08-25 20:39:26 +00004450SelectionDAGBuilder::EmitFuncArgumentDbgValue(const Value *V, MDNode *Variable,
Michael J. Spencere70c5262010-10-16 08:25:21 +00004451 int64_t Offset,
Dan Gohman5d11ea32010-05-01 00:33:16 +00004452 const SDValue &N) {
Devang Patel0b48ead2010-08-31 22:22:42 +00004453 const Argument *Arg = dyn_cast<Argument>(V);
4454 if (!Arg)
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004455 return false;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004456
Devang Patel719f6a92010-04-29 20:40:36 +00004457 MachineFunction &MF = DAG.getMachineFunction();
Devang Patela90b3052010-11-02 17:01:30 +00004458 const TargetInstrInfo *TII = DAG.getTarget().getInstrInfo();
Devang Patela90b3052010-11-02 17:01:30 +00004459
Devang Patela83ce982010-04-29 18:50:36 +00004460 // Ignore inlined function arguments here.
4461 DIVariable DV(Variable);
Devang Patel719f6a92010-04-29 20:40:36 +00004462 if (DV.isInlinedFnArgument(MF.getFunction()))
Devang Patela83ce982010-04-29 18:50:36 +00004463 return false;
4464
David Blaikie6d9dbd52013-06-16 20:34:15 +00004465 Optional<MachineOperand> Op;
Devang Patel9aee3352011-09-08 22:59:09 +00004466 // Some arguments' frame index is recorded during argument lowering.
David Blaikie6d9dbd52013-06-16 20:34:15 +00004467 if (int FI = FuncInfo.getArgumentFrameIndex(Arg))
4468 Op = MachineOperand::CreateFI(FI);
Devang Patel0b48ead2010-08-31 22:22:42 +00004469
David Blaikie6d9dbd52013-06-16 20:34:15 +00004470 if (!Op && N.getNode()) {
4471 unsigned Reg;
Devang Patel227dfdb2011-05-16 21:24:05 +00004472 if (N.getOpcode() == ISD::CopyFromReg)
4473 Reg = cast<RegisterSDNode>(N.getOperand(1))->getReg();
4474 else
4475 Reg = getTruncatedArgReg(N);
4476 if (Reg && TargetRegisterInfo::isVirtualRegister(Reg)) {
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004477 MachineRegisterInfo &RegInfo = MF.getRegInfo();
4478 unsigned PR = RegInfo.getLiveInPhysReg(Reg);
4479 if (PR)
4480 Reg = PR;
4481 }
David Blaikie6d9dbd52013-06-16 20:34:15 +00004482 if (Reg)
4483 Op = MachineOperand::CreateReg(Reg, false);
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004484 }
4485
David Blaikie6d9dbd52013-06-16 20:34:15 +00004486 if (!Op) {
Devang Patela90b3052010-11-02 17:01:30 +00004487 // Check if ValueMap has reg number.
Evan Chenga36acad2010-04-29 06:33:38 +00004488 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
Devang Patel8bc9ef72010-11-02 17:19:03 +00004489 if (VMI != FuncInfo.ValueMap.end())
David Blaikie6d9dbd52013-06-16 20:34:15 +00004490 Op = MachineOperand::CreateReg(VMI->second, false);
Evan Chenga36acad2010-04-29 06:33:38 +00004491 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004492
David Blaikie6d9dbd52013-06-16 20:34:15 +00004493 if (!Op && N.getNode())
Devang Patela90b3052010-11-02 17:01:30 +00004494 // Check if frame index is available.
4495 if (LoadSDNode *LNode = dyn_cast<LoadSDNode>(N.getNode()))
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004496 if (FrameIndexSDNode *FINode =
David Blaikie6d9dbd52013-06-16 20:34:15 +00004497 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
4498 Op = MachineOperand::CreateFI(FINode->getIndex());
Devang Patel8bc9ef72010-11-02 17:19:03 +00004499
David Blaikie6d9dbd52013-06-16 20:34:15 +00004500 if (!Op)
Devang Patel8bc9ef72010-11-02 17:19:03 +00004501 return false;
Devang Patela90b3052010-11-02 17:01:30 +00004502
Adrian Prantl893ae832013-07-10 01:53:30 +00004503 // FIXME: This does not handle register-indirect values at offset 0.
4504 bool IsIndirect = Offset != 0;
David Blaikie6d9dbd52013-06-16 20:34:15 +00004505 if (Op->isReg())
Adrian Prantl35176402013-07-09 20:28:37 +00004506 FuncInfo.ArgDbgValues.push_back(BuildMI(MF, getCurDebugLoc(),
4507 TII->get(TargetOpcode::DBG_VALUE),
Adrian Prantl893ae832013-07-10 01:53:30 +00004508 IsIndirect,
Adrian Prantl35176402013-07-09 20:28:37 +00004509 Op->getReg(), Offset, Variable));
4510 else
4511 FuncInfo.ArgDbgValues.push_back(
David Blaikie6d9dbd52013-06-16 20:34:15 +00004512 BuildMI(MF, getCurDebugLoc(), TII->get(TargetOpcode::DBG_VALUE))
4513 .addOperand(*Op).addImm(Offset).addMetadata(Variable));
Adrian Prantl35176402013-07-09 20:28:37 +00004514
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004515 return true;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004516}
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004517
Douglas Gregor7d9663c2010-05-11 06:17:44 +00004518// VisualStudio defines setjmp as _setjmp
Michael J. Spencer1f409602010-09-24 19:48:47 +00004519#if defined(_MSC_VER) && defined(setjmp) && \
4520 !defined(setjmp_undefined_for_msvc)
4521# pragma push_macro("setjmp")
4522# undef setjmp
4523# define setjmp_undefined_for_msvc
Douglas Gregor7d9663c2010-05-11 06:17:44 +00004524#endif
4525
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004526/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
4527/// we want to emit this as a call to a named external function, return the name
4528/// otherwise lower it and return null.
4529const char *
Dan Gohman46510a72010-04-15 01:51:59 +00004530SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, unsigned Intrinsic) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00004531 const TargetLowering *TLI = TM.getTargetLowering();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004532 SDLoc sdl = getCurSDLoc();
Dale Johannesen66978ee2009-01-31 02:22:37 +00004533 DebugLoc dl = getCurDebugLoc();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004534 SDValue Res;
4535
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004536 switch (Intrinsic) {
4537 default:
4538 // By default, turn this into a target intrinsic node.
4539 visitTargetIntrinsic(I, Intrinsic);
4540 return 0;
4541 case Intrinsic::vastart: visitVAStart(I); return 0;
4542 case Intrinsic::vaend: visitVAEnd(I); return 0;
4543 case Intrinsic::vacopy: visitVACopy(I); return 0;
4544 case Intrinsic::returnaddress:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004545 setValue(&I, DAG.getNode(ISD::RETURNADDR, sdl, TLI->getPointerTy(),
Gabor Greif0635f352010-06-25 09:38:13 +00004546 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004547 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00004548 case Intrinsic::frameaddress:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004549 setValue(&I, DAG.getNode(ISD::FRAMEADDR, sdl, TLI->getPointerTy(),
Gabor Greif0635f352010-06-25 09:38:13 +00004550 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004551 return 0;
4552 case Intrinsic::setjmp:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004553 return &"_setjmp"[!TLI->usesUnderscoreSetJmp()];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004554 case Intrinsic::longjmp:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004555 return &"_longjmp"[!TLI->usesUnderscoreLongJmp()];
Chris Lattner824b9582008-11-21 16:42:48 +00004556 case Intrinsic::memcpy: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00004557 // Assert for address < 256 since we support only user defined address
4558 // spaces.
Gabor Greif0635f352010-06-25 09:38:13 +00004559 assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004560 < 256 &&
Gabor Greif0635f352010-06-25 09:38:13 +00004561 cast<PointerType>(I.getArgOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004562 < 256 &&
4563 "Unknown address space");
Gabor Greif0635f352010-06-25 09:38:13 +00004564 SDValue Op1 = getValue(I.getArgOperand(0));
4565 SDValue Op2 = getValue(I.getArgOperand(1));
4566 SDValue Op3 = getValue(I.getArgOperand(2));
4567 unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
Chandler Carruthaf23f8e2013-02-25 14:20:21 +00004568 if (!Align)
4569 Align = 1; // @llvm.memcpy defines 0 and 1 to both mean no alignment.
Gabor Greif0635f352010-06-25 09:38:13 +00004570 bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004571 DAG.setRoot(DAG.getMemcpy(getRoot(), sdl, Op1, Op2, Op3, Align, isVol, false,
Chris Lattnere72f2022010-09-21 05:40:29 +00004572 MachinePointerInfo(I.getArgOperand(0)),
4573 MachinePointerInfo(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004574 return 0;
4575 }
Chris Lattner824b9582008-11-21 16:42:48 +00004576 case Intrinsic::memset: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00004577 // Assert for address < 256 since we support only user defined address
4578 // spaces.
Gabor Greif0635f352010-06-25 09:38:13 +00004579 assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004580 < 256 &&
4581 "Unknown address space");
Gabor Greif0635f352010-06-25 09:38:13 +00004582 SDValue Op1 = getValue(I.getArgOperand(0));
4583 SDValue Op2 = getValue(I.getArgOperand(1));
4584 SDValue Op3 = getValue(I.getArgOperand(2));
4585 unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
Chandler Carruthaf23f8e2013-02-25 14:20:21 +00004586 if (!Align)
4587 Align = 1; // @llvm.memset defines 0 and 1 to both mean no alignment.
Gabor Greif0635f352010-06-25 09:38:13 +00004588 bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004589 DAG.setRoot(DAG.getMemset(getRoot(), sdl, Op1, Op2, Op3, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004590 MachinePointerInfo(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004591 return 0;
4592 }
Chris Lattner824b9582008-11-21 16:42:48 +00004593 case Intrinsic::memmove: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00004594 // Assert for address < 256 since we support only user defined address
4595 // spaces.
Gabor Greif0635f352010-06-25 09:38:13 +00004596 assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004597 < 256 &&
Gabor Greif0635f352010-06-25 09:38:13 +00004598 cast<PointerType>(I.getArgOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004599 < 256 &&
4600 "Unknown address space");
Gabor Greif0635f352010-06-25 09:38:13 +00004601 SDValue Op1 = getValue(I.getArgOperand(0));
4602 SDValue Op2 = getValue(I.getArgOperand(1));
4603 SDValue Op3 = getValue(I.getArgOperand(2));
4604 unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
Chandler Carruthaf23f8e2013-02-25 14:20:21 +00004605 if (!Align)
4606 Align = 1; // @llvm.memmove defines 0 and 1 to both mean no alignment.
Gabor Greif0635f352010-06-25 09:38:13 +00004607 bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004608 DAG.setRoot(DAG.getMemmove(getRoot(), sdl, Op1, Op2, Op3, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004609 MachinePointerInfo(I.getArgOperand(0)),
4610 MachinePointerInfo(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004611 return 0;
4612 }
Bill Wendling92c1e122009-02-13 02:16:35 +00004613 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00004614 const DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
Devang Patelac1ceb32009-10-09 22:42:28 +00004615 MDNode *Variable = DI.getVariable();
Dan Gohman46510a72010-04-15 01:51:59 +00004616 const Value *Address = DI.getAddress();
Manman Rencbafae62013-06-28 05:43:10 +00004617 DIVariable DIVar(Variable);
4618 assert((!DIVar || DIVar.isVariable()) &&
4619 "Variable in DbgDeclareInst should be either null or a DIVariable.");
4620 if (!Address || !DIVar) {
Eric Christopher8f2a88d2012-03-15 21:33:41 +00004621 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
Dale Johannesen8ac38f22010-02-08 21:53:27 +00004622 return 0;
Eric Christopher8f2a88d2012-03-15 21:33:41 +00004623 }
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004624
Devang Patel3f74a112010-09-02 21:29:42 +00004625 // Check if address has undef value.
4626 if (isa<UndefValue>(Address) ||
4627 (Address->use_empty() && !isa<Argument>(Address))) {
Eric Christopher24413672012-02-23 03:39:39 +00004628 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
Devang Patel3f74a112010-09-02 21:29:42 +00004629 return 0;
4630 }
4631
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004632 SDValue &N = NodeMap[Address];
Devang Patel0b48ead2010-08-31 22:22:42 +00004633 if (!N.getNode() && isa<Argument>(Address))
4634 // Check unused arguments map.
4635 N = UnusedArgNodeMap[Address];
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004636 SDDbgValue *SDV;
4637 if (N.getNode()) {
Devang Patel8e741ed2010-09-02 21:02:27 +00004638 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
4639 Address = BCI->getOperand(0);
Eric Christopher178606d2012-02-24 01:59:08 +00004640 // Parameters are handled specially.
4641 bool isParameter =
4642 (DIVariable(Variable).getTag() == dwarf::DW_TAG_arg_variable ||
4643 isa<Argument>(Address));
4644
Devang Patel8e741ed2010-09-02 21:02:27 +00004645 const AllocaInst *AI = dyn_cast<AllocaInst>(Address);
4646
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004647 if (isParameter && !AI) {
4648 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N.getNode());
4649 if (FINode)
4650 // Byval parameter. We have a frame index at this point.
4651 SDV = DAG.getDbgValue(Variable, FINode->getIndex(),
4652 0, dl, SDNodeOrder);
Devang Patelafeaae72010-12-06 22:39:26 +00004653 else {
Devang Patel227dfdb2011-05-16 21:24:05 +00004654 // Address is an argument, so try to emit its dbg value using
4655 // virtual register info from the FuncInfo.ValueMap.
4656 EmitFuncArgumentDbgValue(Address, Variable, 0, N);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004657 return 0;
Devang Patelafeaae72010-12-06 22:39:26 +00004658 }
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004659 } else if (AI)
4660 SDV = DAG.getDbgValue(Variable, N.getNode(), N.getResNo(),
4661 0, dl, SDNodeOrder);
Devang Patelafeaae72010-12-06 22:39:26 +00004662 else {
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004663 // Can't do anything with other non-AI cases yet.
Eric Christopher24413672012-02-23 03:39:39 +00004664 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
Eric Christopher178606d2012-02-24 01:59:08 +00004665 DEBUG(dbgs() << "non-AllocaInst issue for Address: \n\t");
4666 DEBUG(Address->dump());
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004667 return 0;
Devang Patelafeaae72010-12-06 22:39:26 +00004668 }
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004669 DAG.AddDbgValue(SDV, N.getNode(), isParameter);
4670 } else {
Gabor Greiffb4032f2010-10-01 10:32:19 +00004671 // If Address is an argument then try to emit its dbg value using
Michael J. Spencere70c5262010-10-16 08:25:21 +00004672 // virtual register info from the FuncInfo.ValueMap.
Devang Patel6cd467b2010-08-26 22:53:27 +00004673 if (!EmitFuncArgumentDbgValue(Address, Variable, 0, N)) {
Devang Patel1397fdc2010-09-15 14:48:53 +00004674 // If variable is pinned by a alloca in dominating bb then
4675 // use StaticAllocaMap.
4676 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Address)) {
Devang Patel27ede1b2010-09-15 18:13:55 +00004677 if (AI->getParent() != DI.getParent()) {
4678 DenseMap<const AllocaInst*, int>::iterator SI =
4679 FuncInfo.StaticAllocaMap.find(AI);
4680 if (SI != FuncInfo.StaticAllocaMap.end()) {
4681 SDV = DAG.getDbgValue(Variable, SI->second,
4682 0, dl, SDNodeOrder);
4683 DAG.AddDbgValue(SDV, 0, false);
4684 return 0;
4685 }
Devang Patel1397fdc2010-09-15 14:48:53 +00004686 }
4687 }
Eric Christopher0822e012012-02-23 03:39:43 +00004688 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
Devang Patel6cd467b2010-08-26 22:53:27 +00004689 }
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004690 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004691 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00004692 }
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004693 case Intrinsic::dbg_value: {
Dan Gohman46510a72010-04-15 01:51:59 +00004694 const DbgValueInst &DI = cast<DbgValueInst>(I);
Manman Rencbafae62013-06-28 05:43:10 +00004695 DIVariable DIVar(DI.getVariable());
4696 assert((!DIVar || DIVar.isVariable()) &&
4697 "Variable in DbgValueInst should be either null or a DIVariable.");
4698 if (!DIVar)
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004699 return 0;
4700
4701 MDNode *Variable = DI.getVariable();
Devang Patel00190342010-03-15 19:15:44 +00004702 uint64_t Offset = DI.getOffset();
Dan Gohman46510a72010-04-15 01:51:59 +00004703 const Value *V = DI.getValue();
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004704 if (!V)
4705 return 0;
Devang Patel00190342010-03-15 19:15:44 +00004706
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004707 SDDbgValue *SDV;
Devang Patel57871242011-08-03 23:13:55 +00004708 if (isa<ConstantInt>(V) || isa<ConstantFP>(V) || isa<UndefValue>(V)) {
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004709 SDV = DAG.getDbgValue(Variable, V, Offset, dl, SDNodeOrder);
4710 DAG.AddDbgValue(SDV, 0, false);
Devang Patel00190342010-03-15 19:15:44 +00004711 } else {
Dale Johannesenbdc09d92010-07-16 00:02:08 +00004712 // Do not use getValue() in here; we don't want to generate code at
4713 // this point if it hasn't been done yet.
Devang Patel9126c0d2010-06-01 19:59:01 +00004714 SDValue N = NodeMap[V];
4715 if (!N.getNode() && isa<Argument>(V))
4716 // Check unused arguments map.
4717 N = UnusedArgNodeMap[V];
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004718 if (N.getNode()) {
Devang Patel78a06e52010-08-25 20:39:26 +00004719 if (!EmitFuncArgumentDbgValue(V, Variable, Offset, N)) {
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004720 SDV = DAG.getDbgValue(Variable, N.getNode(),
4721 N.getResNo(), Offset, dl, SDNodeOrder);
4722 DAG.AddDbgValue(SDV, N.getNode(), false);
4723 }
Devang Patela778f5c2011-02-18 22:43:42 +00004724 } else if (!V->use_empty() ) {
Dale Johannesenbdc09d92010-07-16 00:02:08 +00004725 // Do not call getValue(V) yet, as we don't want to generate code.
4726 // Remember it for later.
4727 DanglingDebugInfo DDI(&DI, dl, SDNodeOrder);
4728 DanglingDebugInfoMap[V] = DDI;
Devang Patel0991dfb2010-08-27 22:25:51 +00004729 } else {
Devang Patel00190342010-03-15 19:15:44 +00004730 // We may expand this to cover more cases. One case where we have no
Devang Patelafeaae72010-12-06 22:39:26 +00004731 // data available is an unreferenced parameter.
Eric Christopher0822e012012-02-23 03:39:43 +00004732 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004733 }
Devang Patel00190342010-03-15 19:15:44 +00004734 }
4735
4736 // Build a debug info table entry.
Dan Gohman46510a72010-04-15 01:51:59 +00004737 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(V))
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004738 V = BCI->getOperand(0);
Dan Gohman46510a72010-04-15 01:51:59 +00004739 const AllocaInst *AI = dyn_cast<AllocaInst>(V);
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004740 // Don't handle byval struct arguments or VLAs, for example.
Eric Christopher7e1e18f2012-03-26 06:10:32 +00004741 if (!AI) {
Eric Christopher9fc5c832012-03-28 07:34:36 +00004742 DEBUG(dbgs() << "Dropping debug location info for:\n " << DI << "\n");
4743 DEBUG(dbgs() << " Last seen at:\n " << *V << "\n");
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004744 return 0;
Eric Christopher7e1e18f2012-03-26 06:10:32 +00004745 }
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004746 DenseMap<const AllocaInst*, int>::iterator SI =
4747 FuncInfo.StaticAllocaMap.find(AI);
4748 if (SI == FuncInfo.StaticAllocaMap.end())
4749 return 0; // VLAs.
4750 int FI = SI->second;
Michael J. Spencere70c5262010-10-16 08:25:21 +00004751
Chris Lattner512063d2010-04-05 06:19:28 +00004752 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
4753 if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo())
4754 MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc());
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004755 return 0;
4756 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004757
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004758 case Intrinsic::eh_typeid_for: {
Chris Lattner512063d2010-04-05 06:19:28 +00004759 // Find the type id for the given typeinfo.
Gabor Greif0635f352010-06-25 09:38:13 +00004760 GlobalVariable *GV = ExtractTypeInfo(I.getArgOperand(0));
Chris Lattner512063d2010-04-05 06:19:28 +00004761 unsigned TypeID = DAG.getMachineFunction().getMMI().getTypeIDFor(GV);
4762 Res = DAG.getConstant(TypeID, MVT::i32);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004763 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004764 return 0;
4765 }
4766
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004767 case Intrinsic::eh_return_i32:
4768 case Intrinsic::eh_return_i64:
Chris Lattner512063d2010-04-05 06:19:28 +00004769 DAG.getMachineFunction().getMMI().setCallsEHReturn(true);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004770 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, sdl,
Chris Lattner512063d2010-04-05 06:19:28 +00004771 MVT::Other,
4772 getControlRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00004773 getValue(I.getArgOperand(0)),
4774 getValue(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004775 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004776 case Intrinsic::eh_unwind_init:
Chris Lattner512063d2010-04-05 06:19:28 +00004777 DAG.getMachineFunction().getMMI().setCallsUnwindInit(true);
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004778 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004779 case Intrinsic::eh_dwarf_cfa: {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004780 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getArgOperand(0)), sdl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00004781 TLI->getPointerTy());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004782 SDValue Offset = DAG.getNode(ISD::ADD, sdl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00004783 TLI->getPointerTy(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004784 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, sdl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00004785 TLI->getPointerTy()),
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004786 CfaArg);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004787 SDValue FA = DAG.getNode(ISD::FRAMEADDR, sdl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00004788 TLI->getPointerTy(),
4789 DAG.getConstant(0, TLI->getPointerTy()));
4790 setValue(&I, DAG.getNode(ISD::ADD, sdl, TLI->getPointerTy(),
Bill Wendling4533cac2010-01-28 21:51:40 +00004791 FA, Offset));
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004792 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004793 }
Jim Grosbachca752c92010-01-28 01:45:32 +00004794 case Intrinsic::eh_sjlj_callsite: {
Chris Lattner512063d2010-04-05 06:19:28 +00004795 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Gabor Greif0635f352010-06-25 09:38:13 +00004796 ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(0));
Jim Grosbachca752c92010-01-28 01:45:32 +00004797 assert(CI && "Non-constant call site value in eh.sjlj.callsite!");
Chris Lattner512063d2010-04-05 06:19:28 +00004798 assert(MMI.getCurrentCallSite() == 0 && "Overlapping call sites!");
Jim Grosbachca752c92010-01-28 01:45:32 +00004799
Chris Lattner512063d2010-04-05 06:19:28 +00004800 MMI.setCurrentCallSite(CI->getZExtValue());
Jim Grosbachca752c92010-01-28 01:45:32 +00004801 return 0;
4802 }
Bill Wendling6ef94172011-09-28 03:36:43 +00004803 case Intrinsic::eh_sjlj_functioncontext: {
4804 // Get and store the index of the function context.
4805 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bill Wendlingadbf7b22011-09-28 03:52:41 +00004806 AllocaInst *FnCtx =
4807 cast<AllocaInst>(I.getArgOperand(0)->stripPointerCasts());
Bill Wendling6ef94172011-09-28 03:36:43 +00004808 int FI = FuncInfo.StaticAllocaMap[FnCtx];
4809 MFI->setFunctionContextIndex(FI);
4810 return 0;
4811 }
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004812 case Intrinsic::eh_sjlj_setjmp: {
Bill Wendlingce370cf2011-10-07 21:25:38 +00004813 SDValue Ops[2];
4814 Ops[0] = getRoot();
4815 Ops[1] = getValue(I.getArgOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004816 SDValue Op = DAG.getNode(ISD::EH_SJLJ_SETJMP, sdl,
Bill Wendlingce370cf2011-10-07 21:25:38 +00004817 DAG.getVTList(MVT::i32, MVT::Other),
4818 Ops, 2);
4819 setValue(&I, Op.getValue(0));
4820 DAG.setRoot(Op.getValue(1));
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004821 return 0;
4822 }
Jim Grosbach5eb19512010-05-22 01:06:18 +00004823 case Intrinsic::eh_sjlj_longjmp: {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004824 DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_LONGJMP, sdl, MVT::Other,
Jim Grosbache4ad3872010-10-19 23:27:08 +00004825 getRoot(), getValue(I.getArgOperand(0))));
4826 return 0;
4827 }
Jim Grosbachca752c92010-01-28 01:45:32 +00004828
Dale Johannesen0488fb62010-09-30 23:57:10 +00004829 case Intrinsic::x86_mmx_pslli_w:
4830 case Intrinsic::x86_mmx_pslli_d:
4831 case Intrinsic::x86_mmx_pslli_q:
4832 case Intrinsic::x86_mmx_psrli_w:
4833 case Intrinsic::x86_mmx_psrli_d:
4834 case Intrinsic::x86_mmx_psrli_q:
4835 case Intrinsic::x86_mmx_psrai_w:
4836 case Intrinsic::x86_mmx_psrai_d: {
4837 SDValue ShAmt = getValue(I.getArgOperand(1));
4838 if (isa<ConstantSDNode>(ShAmt)) {
4839 visitTargetIntrinsic(I, Intrinsic);
4840 return 0;
4841 }
4842 unsigned NewIntrinsic = 0;
4843 EVT ShAmtVT = MVT::v2i32;
4844 switch (Intrinsic) {
4845 case Intrinsic::x86_mmx_pslli_w:
4846 NewIntrinsic = Intrinsic::x86_mmx_psll_w;
4847 break;
4848 case Intrinsic::x86_mmx_pslli_d:
4849 NewIntrinsic = Intrinsic::x86_mmx_psll_d;
4850 break;
4851 case Intrinsic::x86_mmx_pslli_q:
4852 NewIntrinsic = Intrinsic::x86_mmx_psll_q;
4853 break;
4854 case Intrinsic::x86_mmx_psrli_w:
4855 NewIntrinsic = Intrinsic::x86_mmx_psrl_w;
4856 break;
4857 case Intrinsic::x86_mmx_psrli_d:
4858 NewIntrinsic = Intrinsic::x86_mmx_psrl_d;
4859 break;
4860 case Intrinsic::x86_mmx_psrli_q:
4861 NewIntrinsic = Intrinsic::x86_mmx_psrl_q;
4862 break;
4863 case Intrinsic::x86_mmx_psrai_w:
4864 NewIntrinsic = Intrinsic::x86_mmx_psra_w;
4865 break;
4866 case Intrinsic::x86_mmx_psrai_d:
4867 NewIntrinsic = Intrinsic::x86_mmx_psra_d;
4868 break;
4869 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
4870 }
4871
4872 // The vector shift intrinsics with scalars uses 32b shift amounts but
4873 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
4874 // to be zero.
4875 // We must do this early because v2i32 is not a legal type.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004876 SDValue ShOps[2];
4877 ShOps[0] = ShAmt;
4878 ShOps[1] = DAG.getConstant(0, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004879 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, sdl, ShAmtVT, &ShOps[0], 2);
Bill Wendlingba54bca2013-06-19 21:36:55 +00004880 EVT DestVT = TLI->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004881 ShAmt = DAG.getNode(ISD::BITCAST, sdl, DestVT, ShAmt);
4882 Res = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, sdl, DestVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00004883 DAG.getConstant(NewIntrinsic, MVT::i32),
4884 getValue(I.getArgOperand(0)), ShAmt);
4885 setValue(&I, Res);
4886 return 0;
4887 }
Pete Cooperd18134f2012-02-24 03:51:49 +00004888 case Intrinsic::x86_avx_vinsertf128_pd_256:
4889 case Intrinsic::x86_avx_vinsertf128_ps_256:
Craig Topperb45c9692012-04-07 22:32:29 +00004890 case Intrinsic::x86_avx_vinsertf128_si_256:
4891 case Intrinsic::x86_avx2_vinserti128: {
Bill Wendlingba54bca2013-06-19 21:36:55 +00004892 EVT DestVT = TLI->getValueType(I.getType());
4893 EVT ElVT = TLI->getValueType(I.getArgOperand(1)->getType());
Pete Cooperd18134f2012-02-24 03:51:49 +00004894 uint64_t Idx = (cast<ConstantInt>(I.getArgOperand(2))->getZExtValue() & 1) *
4895 ElVT.getVectorNumElements();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004896 Res = DAG.getNode(ISD::INSERT_SUBVECTOR, sdl, DestVT,
Pete Cooperd18134f2012-02-24 03:51:49 +00004897 getValue(I.getArgOperand(0)),
4898 getValue(I.getArgOperand(1)),
Tom Stellard425b76c2013-08-05 22:22:01 +00004899 DAG.getConstant(Idx, TLI->getVectorIdxTy()));
Craig Topperf6dc7922012-09-05 05:48:09 +00004900 setValue(&I, Res);
4901 return 0;
4902 }
4903 case Intrinsic::x86_avx_vextractf128_pd_256:
4904 case Intrinsic::x86_avx_vextractf128_ps_256:
4905 case Intrinsic::x86_avx_vextractf128_si_256:
4906 case Intrinsic::x86_avx2_vextracti128: {
Bill Wendlingba54bca2013-06-19 21:36:55 +00004907 EVT DestVT = TLI->getValueType(I.getType());
Craig Topperf6dc7922012-09-05 05:48:09 +00004908 uint64_t Idx = (cast<ConstantInt>(I.getArgOperand(1))->getZExtValue() & 1) *
4909 DestVT.getVectorNumElements();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004910 Res = DAG.getNode(ISD::EXTRACT_SUBVECTOR, sdl, DestVT,
Craig Topperf6dc7922012-09-05 05:48:09 +00004911 getValue(I.getArgOperand(0)),
Tom Stellard425b76c2013-08-05 22:22:01 +00004912 DAG.getConstant(Idx, TLI->getVectorIdxTy()));
Pete Cooperd18134f2012-02-24 03:51:49 +00004913 setValue(&I, Res);
4914 return 0;
4915 }
Mon P Wang77cdf302008-11-10 20:54:11 +00004916 case Intrinsic::convertff:
4917 case Intrinsic::convertfsi:
4918 case Intrinsic::convertfui:
4919 case Intrinsic::convertsif:
4920 case Intrinsic::convertuif:
4921 case Intrinsic::convertss:
4922 case Intrinsic::convertsu:
4923 case Intrinsic::convertus:
4924 case Intrinsic::convertuu: {
4925 ISD::CvtCode Code = ISD::CVT_INVALID;
4926 switch (Intrinsic) {
Craig Topperc42e6402012-04-11 04:34:11 +00004927 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Mon P Wang77cdf302008-11-10 20:54:11 +00004928 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
4929 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
4930 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
4931 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
4932 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
4933 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
4934 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
4935 case Intrinsic::convertus: Code = ISD::CVT_US; break;
4936 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
4937 }
Bill Wendlingba54bca2013-06-19 21:36:55 +00004938 EVT DestVT = TLI->getValueType(I.getType());
Gabor Greif0635f352010-06-25 09:38:13 +00004939 const Value *Op1 = I.getArgOperand(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004940 Res = DAG.getConvertRndSat(DestVT, sdl, getValue(Op1),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004941 DAG.getValueType(DestVT),
4942 DAG.getValueType(getValue(Op1).getValueType()),
Gabor Greif0635f352010-06-25 09:38:13 +00004943 getValue(I.getArgOperand(1)),
4944 getValue(I.getArgOperand(2)),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004945 Code);
4946 setValue(&I, Res);
Mon P Wang77cdf302008-11-10 20:54:11 +00004947 return 0;
4948 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004949 case Intrinsic::powi:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004950 setValue(&I, ExpandPowI(sdl, getValue(I.getArgOperand(0)),
Gabor Greif0635f352010-06-25 09:38:13 +00004951 getValue(I.getArgOperand(1)), DAG));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004952 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004953 case Intrinsic::log:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004954 setValue(&I, expandLog(sdl, getValue(I.getArgOperand(0)), DAG, *TLI));
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004955 return 0;
4956 case Intrinsic::log2:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004957 setValue(&I, expandLog2(sdl, getValue(I.getArgOperand(0)), DAG, *TLI));
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004958 return 0;
4959 case Intrinsic::log10:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004960 setValue(&I, expandLog10(sdl, getValue(I.getArgOperand(0)), DAG, *TLI));
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004961 return 0;
4962 case Intrinsic::exp:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004963 setValue(&I, expandExp(sdl, getValue(I.getArgOperand(0)), DAG, *TLI));
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004964 return 0;
4965 case Intrinsic::exp2:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004966 setValue(&I, expandExp2(sdl, getValue(I.getArgOperand(0)), DAG, *TLI));
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004967 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004968 case Intrinsic::pow:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004969 setValue(&I, expandPow(sdl, getValue(I.getArgOperand(0)),
Bill Wendlingba54bca2013-06-19 21:36:55 +00004970 getValue(I.getArgOperand(1)), DAG, *TLI));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004971 return 0;
Craig Topper9bd4dd72012-11-16 07:48:23 +00004972 case Intrinsic::sqrt:
Peter Collingbourneb34d3aa2012-05-28 21:48:37 +00004973 case Intrinsic::fabs:
Craig Topper9bd4dd72012-11-16 07:48:23 +00004974 case Intrinsic::sin:
4975 case Intrinsic::cos:
Dan Gohman27db99f2012-07-26 17:43:27 +00004976 case Intrinsic::floor:
Craig Topper49010472012-11-15 06:51:10 +00004977 case Intrinsic::ceil:
Craig Topper49010472012-11-15 06:51:10 +00004978 case Intrinsic::trunc:
Craig Topper49010472012-11-15 06:51:10 +00004979 case Intrinsic::rint:
Hal Finkel41418d12013-08-07 22:49:12 +00004980 case Intrinsic::nearbyint:
4981 case Intrinsic::round: {
Craig Topper9bd4dd72012-11-16 07:48:23 +00004982 unsigned Opcode;
4983 switch (Intrinsic) {
4984 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
4985 case Intrinsic::sqrt: Opcode = ISD::FSQRT; break;
4986 case Intrinsic::fabs: Opcode = ISD::FABS; break;
4987 case Intrinsic::sin: Opcode = ISD::FSIN; break;
4988 case Intrinsic::cos: Opcode = ISD::FCOS; break;
4989 case Intrinsic::floor: Opcode = ISD::FFLOOR; break;
4990 case Intrinsic::ceil: Opcode = ISD::FCEIL; break;
4991 case Intrinsic::trunc: Opcode = ISD::FTRUNC; break;
4992 case Intrinsic::rint: Opcode = ISD::FRINT; break;
4993 case Intrinsic::nearbyint: Opcode = ISD::FNEARBYINT; break;
Hal Finkel41418d12013-08-07 22:49:12 +00004994 case Intrinsic::round: Opcode = ISD::FROUND; break;
Craig Topper9bd4dd72012-11-16 07:48:23 +00004995 }
4996
Andrew Trickac6d9be2013-05-25 02:42:55 +00004997 setValue(&I, DAG.getNode(Opcode, sdl,
Craig Topper49010472012-11-15 06:51:10 +00004998 getValue(I.getArgOperand(0)).getValueType(),
4999 getValue(I.getArgOperand(0))));
5000 return 0;
Craig Topper9bd4dd72012-11-16 07:48:23 +00005001 }
Hal Finkel66d1fa62013-08-19 23:35:46 +00005002 case Intrinsic::copysign:
5003 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, sdl,
5004 getValue(I.getArgOperand(0)).getValueType(),
5005 getValue(I.getArgOperand(0)),
5006 getValue(I.getArgOperand(1))));
5007 return 0;
Cameron Zwarich33390842011-07-08 21:39:21 +00005008 case Intrinsic::fma:
Andrew Trickac6d9be2013-05-25 02:42:55 +00005009 setValue(&I, DAG.getNode(ISD::FMA, sdl,
Cameron Zwarich33390842011-07-08 21:39:21 +00005010 getValue(I.getArgOperand(0)).getValueType(),
5011 getValue(I.getArgOperand(0)),
5012 getValue(I.getArgOperand(1)),
5013 getValue(I.getArgOperand(2))));
5014 return 0;
Lang Hames5afba6f2012-06-05 19:07:46 +00005015 case Intrinsic::fmuladd: {
Bill Wendlingba54bca2013-06-19 21:36:55 +00005016 EVT VT = TLI->getValueType(I.getType());
Lang Hamese0231412012-06-22 01:09:09 +00005017 if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict &&
Stephen Line54885a2013-07-09 18:16:56 +00005018 TLI->isFMAFasterThanFMulAndFAdd(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005019 setValue(&I, DAG.getNode(ISD::FMA, sdl,
Lang Hames5afba6f2012-06-05 19:07:46 +00005020 getValue(I.getArgOperand(0)).getValueType(),
5021 getValue(I.getArgOperand(0)),
5022 getValue(I.getArgOperand(1)),
5023 getValue(I.getArgOperand(2))));
5024 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005025 SDValue Mul = DAG.getNode(ISD::FMUL, sdl,
Lang Hames5afba6f2012-06-05 19:07:46 +00005026 getValue(I.getArgOperand(0)).getValueType(),
5027 getValue(I.getArgOperand(0)),
5028 getValue(I.getArgOperand(1)));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005029 SDValue Add = DAG.getNode(ISD::FADD, sdl,
Lang Hames5afba6f2012-06-05 19:07:46 +00005030 getValue(I.getArgOperand(0)).getValueType(),
5031 Mul,
5032 getValue(I.getArgOperand(2)));
5033 setValue(&I, Add);
5034 }
5035 return 0;
5036 }
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00005037 case Intrinsic::convert_to_fp16:
Andrew Trickac6d9be2013-05-25 02:42:55 +00005038 setValue(&I, DAG.getNode(ISD::FP32_TO_FP16, sdl,
Gabor Greif0635f352010-06-25 09:38:13 +00005039 MVT::i16, getValue(I.getArgOperand(0))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00005040 return 0;
5041 case Intrinsic::convert_from_fp16:
Andrew Trickac6d9be2013-05-25 02:42:55 +00005042 setValue(&I, DAG.getNode(ISD::FP16_TO_FP32, sdl,
Gabor Greif0635f352010-06-25 09:38:13 +00005043 MVT::f32, getValue(I.getArgOperand(0))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00005044 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005045 case Intrinsic::pcmarker: {
Gabor Greif0635f352010-06-25 09:38:13 +00005046 SDValue Tmp = getValue(I.getArgOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005047 DAG.setRoot(DAG.getNode(ISD::PCMARKER, sdl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005048 return 0;
5049 }
5050 case Intrinsic::readcyclecounter: {
5051 SDValue Op = getRoot();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005052 Res = DAG.getNode(ISD::READCYCLECOUNTER, sdl,
Bill Wendlingd0283fa2009-12-22 00:40:51 +00005053 DAG.getVTList(MVT::i64, MVT::Other),
5054 &Op, 1);
5055 setValue(&I, Res);
5056 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005057 return 0;
5058 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005059 case Intrinsic::bswap:
Andrew Trickac6d9be2013-05-25 02:42:55 +00005060 setValue(&I, DAG.getNode(ISD::BSWAP, sdl,
Gabor Greif0635f352010-06-25 09:38:13 +00005061 getValue(I.getArgOperand(0)).getValueType(),
5062 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005063 return 0;
5064 case Intrinsic::cttz: {
Gabor Greif0635f352010-06-25 09:38:13 +00005065 SDValue Arg = getValue(I.getArgOperand(0));
Chandler Carruth63974b22011-12-13 01:56:10 +00005066 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00005067 EVT Ty = Arg.getValueType();
Chandler Carruth63974b22011-12-13 01:56:10 +00005068 setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTTZ : ISD::CTTZ_ZERO_UNDEF,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005069 sdl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005070 return 0;
5071 }
5072 case Intrinsic::ctlz: {
Gabor Greif0635f352010-06-25 09:38:13 +00005073 SDValue Arg = getValue(I.getArgOperand(0));
Chandler Carruth63974b22011-12-13 01:56:10 +00005074 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00005075 EVT Ty = Arg.getValueType();
Chandler Carruth63974b22011-12-13 01:56:10 +00005076 setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTLZ : ISD::CTLZ_ZERO_UNDEF,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005077 sdl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005078 return 0;
5079 }
5080 case Intrinsic::ctpop: {
Gabor Greif0635f352010-06-25 09:38:13 +00005081 SDValue Arg = getValue(I.getArgOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00005082 EVT Ty = Arg.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005083 setValue(&I, DAG.getNode(ISD::CTPOP, sdl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005084 return 0;
5085 }
5086 case Intrinsic::stacksave: {
5087 SDValue Op = getRoot();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005088 Res = DAG.getNode(ISD::STACKSAVE, sdl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00005089 DAG.getVTList(TLI->getPointerTy(), MVT::Other), &Op, 1);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00005090 setValue(&I, Res);
5091 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005092 return 0;
5093 }
5094 case Intrinsic::stackrestore: {
Gabor Greif0635f352010-06-25 09:38:13 +00005095 Res = getValue(I.getArgOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005096 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, sdl, MVT::Other, getRoot(), Res));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005097 return 0;
5098 }
Bill Wendling57344502008-11-18 11:01:33 +00005099 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00005100 // Emit code into the DAG to store the stack guard onto the stack.
5101 MachineFunction &MF = DAG.getMachineFunction();
5102 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendlingba54bca2013-06-19 21:36:55 +00005103 EVT PtrTy = TLI->getPointerTy();
Bill Wendlingb2a42982008-11-06 02:29:10 +00005104
Gabor Greif0635f352010-06-25 09:38:13 +00005105 SDValue Src = getValue(I.getArgOperand(0)); // The guard's value.
5106 AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Bill Wendlingb2a42982008-11-06 02:29:10 +00005107
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00005108 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00005109 MFI->setStackProtectorIndex(FI);
5110
5111 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
5112
5113 // Store the stack protector onto the stack.
Andrew Trickac6d9be2013-05-25 02:42:55 +00005114 Res = DAG.getStore(getRoot(), sdl, Src, FIN,
Chris Lattner84bd98a2010-09-21 18:58:22 +00005115 MachinePointerInfo::getFixedStack(FI),
5116 true, false, 0);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00005117 setValue(&I, Res);
5118 DAG.setRoot(Res);
Bill Wendlingb2a42982008-11-06 02:29:10 +00005119 return 0;
5120 }
Eric Christopher7b5e6172009-10-27 00:52:25 +00005121 case Intrinsic::objectsize: {
5122 // If we don't know by now, we're never going to know.
Gabor Greif0635f352010-06-25 09:38:13 +00005123 ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(1));
Eric Christopher7b5e6172009-10-27 00:52:25 +00005124
5125 assert(CI && "Non-constant type in __builtin_object_size?");
5126
Gabor Greif0635f352010-06-25 09:38:13 +00005127 SDValue Arg = getValue(I.getCalledValue());
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00005128 EVT Ty = Arg.getValueType();
5129
Dan Gohmane368b462010-06-18 14:22:04 +00005130 if (CI->isZero())
Bill Wendlingd0283fa2009-12-22 00:40:51 +00005131 Res = DAG.getConstant(-1ULL, Ty);
Eric Christopher7b5e6172009-10-27 00:52:25 +00005132 else
Bill Wendlingd0283fa2009-12-22 00:40:51 +00005133 Res = DAG.getConstant(0, Ty);
5134
5135 setValue(&I, Res);
Eric Christopher7b5e6172009-10-27 00:52:25 +00005136 return 0;
5137 }
Justin Holewinskic2b7f5f2013-05-21 14:37:16 +00005138 case Intrinsic::annotation:
5139 case Intrinsic::ptr_annotation:
5140 // Drop the intrinsic, but forward the value
5141 setValue(&I, getValue(I.getOperand(0)));
5142 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005143 case Intrinsic::var_annotation:
5144 // Discard annotate attributes
5145 return 0;
5146
5147 case Intrinsic::init_trampoline: {
Gabor Greif0635f352010-06-25 09:38:13 +00005148 const Function *F = cast<Function>(I.getArgOperand(1)->stripPointerCasts());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005149
5150 SDValue Ops[6];
5151 Ops[0] = getRoot();
Gabor Greif0635f352010-06-25 09:38:13 +00005152 Ops[1] = getValue(I.getArgOperand(0));
5153 Ops[2] = getValue(I.getArgOperand(1));
5154 Ops[3] = getValue(I.getArgOperand(2));
5155 Ops[4] = DAG.getSrcValue(I.getArgOperand(0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005156 Ops[5] = DAG.getSrcValue(F);
5157
Andrew Trickac6d9be2013-05-25 02:42:55 +00005158 Res = DAG.getNode(ISD::INIT_TRAMPOLINE, sdl, MVT::Other, Ops, 6);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005159
Duncan Sands4a544a72011-09-06 13:37:06 +00005160 DAG.setRoot(Res);
5161 return 0;
5162 }
5163 case Intrinsic::adjust_trampoline: {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005164 setValue(&I, DAG.getNode(ISD::ADJUST_TRAMPOLINE, sdl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00005165 TLI->getPointerTy(),
Duncan Sands4a544a72011-09-06 13:37:06 +00005166 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005167 return 0;
5168 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005169 case Intrinsic::gcroot:
5170 if (GFI) {
Bill Wendling95dd4422012-05-01 22:50:45 +00005171 const Value *Alloca = I.getArgOperand(0)->stripPointerCasts();
Gabor Greif0635f352010-06-25 09:38:13 +00005172 const Constant *TypeMap = cast<Constant>(I.getArgOperand(1));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005173
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005174 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
5175 GFI->addStackRoot(FI->getIndex(), TypeMap);
5176 }
5177 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005178 case Intrinsic::gcread:
5179 case Intrinsic::gcwrite:
Torok Edwinc23197a2009-07-14 16:55:14 +00005180 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
Bill Wendlingd0283fa2009-12-22 00:40:51 +00005181 case Intrinsic::flt_rounds:
Andrew Trickac6d9be2013-05-25 02:42:55 +00005182 setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, sdl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005183 return 0;
Jakub Staszak9da99342011-07-06 18:22:43 +00005184
5185 case Intrinsic::expect: {
5186 // Just replace __builtin_expect(exp, c) with EXP.
5187 setValue(&I, getValue(I.getArgOperand(0)));
5188 return 0;
5189 }
5190
Shuxin Yang970755e2012-10-19 20:11:16 +00005191 case Intrinsic::debugtrap:
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005192 case Intrinsic::trap: {
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005193 StringRef TrapFuncName = TM.Options.getTrapFunctionName();
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005194 if (TrapFuncName.empty()) {
Stephen Lin155615d2013-07-08 00:37:03 +00005195 ISD::NodeType Op = (Intrinsic == Intrinsic::trap) ?
Shuxin Yang970755e2012-10-19 20:11:16 +00005196 ISD::TRAP : ISD::DEBUGTRAP;
Andrew Trickac6d9be2013-05-25 02:42:55 +00005197 DAG.setRoot(DAG.getNode(Op, sdl,MVT::Other, getRoot()));
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005198 return 0;
5199 }
5200 TargetLowering::ArgListTy Args;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00005201 TargetLowering::
5202 CallLoweringInfo CLI(getRoot(), I.getType(),
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005203 false, false, false, false, 0, CallingConv::C,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00005204 /*isTailCall=*/false,
5205 /*doesNotRet=*/false, /*isReturnValueUsed=*/true,
Bill Wendlingba54bca2013-06-19 21:36:55 +00005206 DAG.getExternalSymbol(TrapFuncName.data(),
5207 TLI->getPointerTy()),
Andrew Trickac6d9be2013-05-25 02:42:55 +00005208 Args, DAG, sdl);
Bill Wendlingba54bca2013-06-19 21:36:55 +00005209 std::pair<SDValue, SDValue> Result = TLI->LowerCallTo(CLI);
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005210 DAG.setRoot(Result.second);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005211 return 0;
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005212 }
Shuxin Yang970755e2012-10-19 20:11:16 +00005213
Bill Wendlingef375462008-11-21 02:38:44 +00005214 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00005215 case Intrinsic::sadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00005216 case Intrinsic::usub_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00005217 case Intrinsic::ssub_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00005218 case Intrinsic::umul_with_overflow:
Craig Topperc42e6402012-04-11 04:34:11 +00005219 case Intrinsic::smul_with_overflow: {
5220 ISD::NodeType Op;
5221 switch (Intrinsic) {
5222 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
5223 case Intrinsic::uadd_with_overflow: Op = ISD::UADDO; break;
5224 case Intrinsic::sadd_with_overflow: Op = ISD::SADDO; break;
5225 case Intrinsic::usub_with_overflow: Op = ISD::USUBO; break;
5226 case Intrinsic::ssub_with_overflow: Op = ISD::SSUBO; break;
5227 case Intrinsic::umul_with_overflow: Op = ISD::UMULO; break;
5228 case Intrinsic::smul_with_overflow: Op = ISD::SMULO; break;
5229 }
5230 SDValue Op1 = getValue(I.getArgOperand(0));
5231 SDValue Op2 = getValue(I.getArgOperand(1));
Bill Wendling7cdc3c82008-11-21 02:03:52 +00005232
Craig Topperc42e6402012-04-11 04:34:11 +00005233 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005234 setValue(&I, DAG.getNode(Op, sdl, VTs, Op1, Op2));
Craig Topperc42e6402012-04-11 04:34:11 +00005235 return 0;
5236 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005237 case Intrinsic::prefetch: {
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00005238 SDValue Ops[5];
Dale Johannesen1de4aa92010-10-26 23:11:10 +00005239 unsigned rw = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005240 Ops[0] = getRoot();
Gabor Greif0635f352010-06-25 09:38:13 +00005241 Ops[1] = getValue(I.getArgOperand(0));
5242 Ops[2] = getValue(I.getArgOperand(1));
5243 Ops[3] = getValue(I.getArgOperand(2));
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00005244 Ops[4] = getValue(I.getArgOperand(3));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005245 DAG.setRoot(DAG.getMemIntrinsicNode(ISD::PREFETCH, sdl,
Dale Johannesen1de4aa92010-10-26 23:11:10 +00005246 DAG.getVTList(MVT::Other),
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00005247 &Ops[0], 5,
Dale Johannesen1de4aa92010-10-26 23:11:10 +00005248 EVT::getIntegerVT(*Context, 8),
5249 MachinePointerInfo(I.getArgOperand(0)),
5250 0, /* align */
5251 false, /* volatile */
5252 rw==0, /* read */
5253 rw==1)); /* write */
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005254 return 0;
5255 }
Duncan Sandsf07c9492009-11-10 09:08:09 +00005256 case Intrinsic::lifetime_start:
Nadav Rotemc05d3062012-09-06 09:17:37 +00005257 case Intrinsic::lifetime_end: {
Nadav Rotemc05d3062012-09-06 09:17:37 +00005258 bool IsStart = (Intrinsic == Intrinsic::lifetime_start);
Nadav Rotem9a2ae002012-09-10 08:43:23 +00005259 // Stack coloring is not enabled in O0, discard region information.
5260 if (TM.getOptLevel() == CodeGenOpt::None)
5261 return 0;
Nadav Rotemc05d3062012-09-06 09:17:37 +00005262
Nadav Rotem9a2ae002012-09-10 08:43:23 +00005263 SmallVector<Value *, 4> Allocas;
5264 GetUnderlyingObjects(I.getArgOperand(1), Allocas, TD);
5265
Craig Topperf22fd3f2013-07-03 05:11:49 +00005266 for (SmallVectorImpl<Value*>::iterator Object = Allocas.begin(),
5267 E = Allocas.end(); Object != E; ++Object) {
Nadav Rotem9a2ae002012-09-10 08:43:23 +00005268 AllocaInst *LifetimeObject = dyn_cast_or_null<AllocaInst>(*Object);
5269
5270 // Could not find an Alloca.
5271 if (!LifetimeObject)
5272 continue;
5273
5274 int FI = FuncInfo.StaticAllocaMap[LifetimeObject];
5275
5276 SDValue Ops[2];
5277 Ops[0] = getRoot();
Bill Wendlingba54bca2013-06-19 21:36:55 +00005278 Ops[1] = DAG.getFrameIndex(FI, TLI->getPointerTy(), true);
Nadav Rotem9a2ae002012-09-10 08:43:23 +00005279 unsigned Opcode = (IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END);
5280
Andrew Trickac6d9be2013-05-25 02:42:55 +00005281 Res = DAG.getNode(Opcode, sdl, MVT::Other, Ops, 2);
Nadav Rotem9a2ae002012-09-10 08:43:23 +00005282 DAG.setRoot(Res);
5283 }
Nadav Rotem5882e562013-02-01 19:25:23 +00005284 return 0;
Nadav Rotemc05d3062012-09-06 09:17:37 +00005285 }
5286 case Intrinsic::invariant_start:
Duncan Sandsf07c9492009-11-10 09:08:09 +00005287 // Discard region information.
Bill Wendlingba54bca2013-06-19 21:36:55 +00005288 setValue(&I, DAG.getUNDEF(TLI->getPointerTy()));
Duncan Sandsf07c9492009-11-10 09:08:09 +00005289 return 0;
5290 case Intrinsic::invariant_end:
Duncan Sandsf07c9492009-11-10 09:08:09 +00005291 // Discard region information.
5292 return 0;
Michael Gottesman657484f2013-08-20 07:00:16 +00005293 case Intrinsic::stackprotectorcheck: {
5294 // Do not actually emit anything for this basic block. Instead we initialize
5295 // the stack protector descriptor and export the guard variable so we can
5296 // access it in FinishBasicBlock.
5297 const BasicBlock *BB = I.getParent();
5298 SPDescriptor.initialize(BB, FuncInfo.MBBMap[BB], I);
5299 ExportFromCurrentBlock(SPDescriptor.getGuard());
5300
5301 // Flush our exports since we are going to process a terminator.
5302 (void)getControlRoot();
5303 return 0;
5304 }
Nuno Lopes85b40892012-06-28 22:30:12 +00005305 case Intrinsic::donothing:
5306 // ignore
5307 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005308 }
5309}
5310
Dan Gohman46510a72010-04-15 01:51:59 +00005311void SelectionDAGBuilder::LowerCallTo(ImmutableCallSite CS, SDValue Callee,
Dan Gohman2048b852009-11-23 18:04:58 +00005312 bool isTailCall,
5313 MachineBasicBlock *LandingPad) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005314 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
5315 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
5316 Type *RetTy = FTy->getReturnType();
Chris Lattner512063d2010-04-05 06:19:28 +00005317 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Chris Lattner16112732010-03-14 01:41:15 +00005318 MCSymbol *BeginLabel = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005319
5320 TargetLowering::ArgListTy Args;
5321 TargetLowering::ArgListEntry Entry;
5322 Args.reserve(CS.arg_size());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005323
5324 // Check whether the function can return without sret-demotion.
Dan Gohman84023e02010-07-10 09:00:22 +00005325 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00005326 const TargetLowering *TLI = TM.getTargetLowering();
5327 GetReturnInfo(RetTy, CS.getAttributes(), Outs, *TLI);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005328
Bill Wendlingba54bca2013-06-19 21:36:55 +00005329 bool CanLowerReturn = TLI->CanLowerReturn(CS.getCallingConv(),
5330 DAG.getMachineFunction(),
5331 FTy->isVarArg(), Outs,
5332 FTy->getContext());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005333
5334 SDValue DemoteStackSlot;
Chris Lattnerecf42c42010-09-21 16:36:31 +00005335 int DemoteStackIdx = -100;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005336
5337 if (!CanLowerReturn) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00005338 uint64_t TySize = TLI->getDataLayout()->getTypeAllocSize(
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005339 FTy->getReturnType());
Bill Wendlingba54bca2013-06-19 21:36:55 +00005340 unsigned Align = TLI->getDataLayout()->getPrefTypeAlignment(
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005341 FTy->getReturnType());
5342 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattnerecf42c42010-09-21 16:36:31 +00005343 DemoteStackIdx = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005344 Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005345
Bill Wendlingba54bca2013-06-19 21:36:55 +00005346 DemoteStackSlot = DAG.getFrameIndex(DemoteStackIdx, TLI->getPointerTy());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005347 Entry.Node = DemoteStackSlot;
5348 Entry.Ty = StackSlotPtrType;
5349 Entry.isSExt = false;
5350 Entry.isZExt = false;
5351 Entry.isInReg = false;
5352 Entry.isSRet = true;
5353 Entry.isNest = false;
5354 Entry.isByVal = false;
Stephen Lin456ca042013-04-20 05:14:40 +00005355 Entry.isReturned = false;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005356 Entry.Alignment = Align;
5357 Args.push_back(Entry);
5358 RetTy = Type::getVoidTy(FTy->getContext());
5359 }
5360
Dan Gohman46510a72010-04-15 01:51:59 +00005361 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005362 i != e; ++i) {
Rafael Espindola3fa82832011-05-13 15:18:06 +00005363 const Value *V = *i;
5364
5365 // Skip empty types
5366 if (V->getType()->isEmptyTy())
5367 continue;
5368
5369 SDValue ArgNode = getValue(V);
5370 Entry.Node = ArgNode; Entry.Ty = V->getType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005371
5372 unsigned attrInd = i - CS.arg_begin() + 1;
Stephen Lin456ca042013-04-20 05:14:40 +00005373 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
5374 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
5375 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
5376 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
5377 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
5378 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
5379 Entry.isReturned = CS.paramHasAttr(attrInd, Attribute::Returned);
5380 Entry.Alignment = CS.getParamAlignment(attrInd);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005381 Args.push_back(Entry);
5382 }
5383
Chris Lattner512063d2010-04-05 06:19:28 +00005384 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005385 // Insert a label before the invoke call to mark the try range. This can be
5386 // used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00005387 BeginLabel = MMI.getContext().CreateTempSymbol();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00005388
Jim Grosbachca752c92010-01-28 01:45:32 +00005389 // For SjLj, keep track of which landing pads go with which invokes
5390 // so as to maintain the ordering of pads in the LSDA.
Chris Lattner512063d2010-04-05 06:19:28 +00005391 unsigned CallSiteIndex = MMI.getCurrentCallSite();
Jim Grosbachca752c92010-01-28 01:45:32 +00005392 if (CallSiteIndex) {
Chris Lattner512063d2010-04-05 06:19:28 +00005393 MMI.setCallSiteBeginLabel(BeginLabel, CallSiteIndex);
Bill Wendling30e67402011-10-05 22:24:35 +00005394 LPadToCallSiteMap[LandingPad].push_back(CallSiteIndex);
Bill Wendlinga8512ed2011-10-04 22:00:35 +00005395
Jim Grosbachca752c92010-01-28 01:45:32 +00005396 // Now that the call site is handled, stop tracking it.
Chris Lattner512063d2010-04-05 06:19:28 +00005397 MMI.setCurrentCallSite(0);
Jim Grosbachca752c92010-01-28 01:45:32 +00005398 }
5399
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005400 // Both PendingLoads and PendingExports must be flushed here;
5401 // this call might not return.
5402 (void)getRoot();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005403 DAG.setRoot(DAG.getEHLabel(getCurSDLoc(), getControlRoot(), BeginLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005404 }
5405
Dan Gohman98ca4f22009-08-05 01:29:28 +00005406 // Check if target-independent constraints permit a tail call here.
Bill Wendlingba54bca2013-06-19 21:36:55 +00005407 // Target-dependent constraints are checked within TLI->LowerCallTo.
5408 if (isTailCall && !isInTailCallPosition(CS, *TLI))
Dan Gohman98ca4f22009-08-05 01:29:28 +00005409 isTailCall = false;
5410
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00005411 TargetLowering::
5412 CallLoweringInfo CLI(getRoot(), RetTy, FTy, isTailCall, Callee, Args, DAG,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005413 getCurSDLoc(), CS);
Bill Wendlingba54bca2013-06-19 21:36:55 +00005414 std::pair<SDValue,SDValue> Result = TLI->LowerCallTo(CLI);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005415 assert((isTailCall || Result.second.getNode()) &&
5416 "Non-null chain expected with non-tail call!");
5417 assert((Result.second.getNode() || !Result.first.getNode()) &&
5418 "Null value expected with tail call!");
Bill Wendlinge80ae832009-12-22 00:50:32 +00005419 if (Result.first.getNode()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005420 setValue(CS.getInstruction(), Result.first);
Bill Wendlinge80ae832009-12-22 00:50:32 +00005421 } else if (!CanLowerReturn && Result.second.getNode()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005422 // The instruction result is the result of loading from the
5423 // hidden sret parameter.
5424 SmallVector<EVT, 1> PVTs;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005425 Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005426
Bill Wendlingba54bca2013-06-19 21:36:55 +00005427 ComputeValueVTs(*TLI, PtrRetTy, PVTs);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005428 assert(PVTs.size() == 1 && "Pointers should fit in one register");
5429 EVT PtrVT = PVTs[0];
Eli Friedman2db0e9e2012-05-25 00:09:29 +00005430
5431 SmallVector<EVT, 4> RetTys;
5432 SmallVector<uint64_t, 4> Offsets;
5433 RetTy = FTy->getReturnType();
Bill Wendlingba54bca2013-06-19 21:36:55 +00005434 ComputeValueVTs(*TLI, RetTy, RetTys, &Offsets);
Eli Friedman2db0e9e2012-05-25 00:09:29 +00005435
5436 unsigned NumValues = RetTys.size();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005437 SmallVector<SDValue, 4> Values(NumValues);
5438 SmallVector<SDValue, 4> Chains(NumValues);
5439
5440 for (unsigned i = 0; i < NumValues; ++i) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005441 SDValue Add = DAG.getNode(ISD::ADD, getCurSDLoc(), PtrVT,
Bill Wendlinge80ae832009-12-22 00:50:32 +00005442 DemoteStackSlot,
5443 DAG.getConstant(Offsets[i], PtrVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005444 SDValue L = DAG.getLoad(RetTys[i], getCurSDLoc(), Result.second, Add,
Chris Lattnerecf42c42010-09-21 16:36:31 +00005445 MachinePointerInfo::getFixedStack(DemoteStackIdx, Offsets[i]),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005446 false, false, false, 1);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005447 Values[i] = L;
5448 Chains[i] = L.getValue(1);
5449 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00005450
Andrew Trickac6d9be2013-05-25 02:42:55 +00005451 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005452 MVT::Other, &Chains[0], NumValues);
5453 PendingLoads.push_back(Chain);
Michael J. Spencere70c5262010-10-16 08:25:21 +00005454
Bill Wendling4533cac2010-01-28 21:51:40 +00005455 setValue(CS.getInstruction(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00005456 DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00005457 DAG.getVTList(&RetTys[0], RetTys.size()),
Eli Friedman2db0e9e2012-05-25 00:09:29 +00005458 &Values[0], Values.size()));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005459 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00005460
Evan Cheng8380c032011-04-01 19:42:22 +00005461 if (!Result.second.getNode()) {
Evan Chengc249e482011-04-01 19:57:01 +00005462 // As a special case, a null chain means that a tail call has been emitted and
5463 // the DAG root is already updated.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005464 HasTailCall = true;
Tim Northovere5a81a12013-07-06 12:58:45 +00005465
5466 // Since there's no actual continuation from this block, nothing can be
5467 // relying on us setting vregs for them.
5468 PendingExports.clear();
Evan Cheng8380c032011-04-01 19:42:22 +00005469 } else {
5470 DAG.setRoot(Result.second);
Evan Cheng8380c032011-04-01 19:42:22 +00005471 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005472
Chris Lattner512063d2010-04-05 06:19:28 +00005473 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005474 // Insert a label at the end of the invoke call to mark the try range. This
5475 // can be used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00005476 MCSymbol *EndLabel = MMI.getContext().CreateTempSymbol();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005477 DAG.setRoot(DAG.getEHLabel(getCurSDLoc(), getRoot(), EndLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005478
5479 // Inform MachineModuleInfo of range.
Chris Lattner512063d2010-04-05 06:19:28 +00005480 MMI.addInvoke(LandingPad, BeginLabel, EndLabel);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005481 }
5482}
5483
Chris Lattner8047d9a2009-12-24 00:37:38 +00005484/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
5485/// value is equal or not-equal to zero.
Dan Gohman46510a72010-04-15 01:51:59 +00005486static bool IsOnlyUsedInZeroEqualityComparison(const Value *V) {
5487 for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end();
Chris Lattner8047d9a2009-12-24 00:37:38 +00005488 UI != E; ++UI) {
Dan Gohman46510a72010-04-15 01:51:59 +00005489 if (const ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
Chris Lattner8047d9a2009-12-24 00:37:38 +00005490 if (IC->isEquality())
Dan Gohman46510a72010-04-15 01:51:59 +00005491 if (const Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
Chris Lattner8047d9a2009-12-24 00:37:38 +00005492 if (C->isNullValue())
5493 continue;
5494 // Unknown instruction.
5495 return false;
5496 }
5497 return true;
5498}
5499
Dan Gohman46510a72010-04-15 01:51:59 +00005500static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005501 Type *LoadTy,
Chris Lattner8047d9a2009-12-24 00:37:38 +00005502 SelectionDAGBuilder &Builder) {
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005503
Chris Lattner8047d9a2009-12-24 00:37:38 +00005504 // Check to see if this load can be trivially constant folded, e.g. if the
5505 // input is from a string literal.
Dan Gohman46510a72010-04-15 01:51:59 +00005506 if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00005507 // Cast pointer to the type we really want to load.
Dan Gohman46510a72010-04-15 01:51:59 +00005508 LoadInput = ConstantExpr::getBitCast(const_cast<Constant *>(LoadInput),
Chris Lattner8047d9a2009-12-24 00:37:38 +00005509 PointerType::getUnqual(LoadTy));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005510
Dan Gohman46510a72010-04-15 01:51:59 +00005511 if (const Constant *LoadCst =
5512 ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput),
5513 Builder.TD))
Chris Lattner8047d9a2009-12-24 00:37:38 +00005514 return Builder.getValue(LoadCst);
5515 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005516
Chris Lattner8047d9a2009-12-24 00:37:38 +00005517 // Otherwise, we have to emit the load. If the pointer is to unfoldable but
5518 // still constant memory, the input chain can be the entry node.
5519 SDValue Root;
5520 bool ConstantMemory = false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005521
Chris Lattner8047d9a2009-12-24 00:37:38 +00005522 // Do not serialize (non-volatile) loads of constant memory with anything.
5523 if (Builder.AA->pointsToConstantMemory(PtrVal)) {
5524 Root = Builder.DAG.getEntryNode();
5525 ConstantMemory = true;
5526 } else {
5527 // Do not serialize non-volatile loads against each other.
5528 Root = Builder.DAG.getRoot();
5529 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005530
Chris Lattner8047d9a2009-12-24 00:37:38 +00005531 SDValue Ptr = Builder.getValue(PtrVal);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005532 SDValue LoadVal = Builder.DAG.getLoad(LoadVT, Builder.getCurSDLoc(), Root,
Chris Lattnerecf42c42010-09-21 16:36:31 +00005533 Ptr, MachinePointerInfo(PtrVal),
David Greene1e559442010-02-15 17:00:31 +00005534 false /*volatile*/,
Stephen Lin155615d2013-07-08 00:37:03 +00005535 false /*nontemporal*/,
Pete Cooperd752e0f2011-11-08 18:42:53 +00005536 false /*isinvariant*/, 1 /* align=1 */);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005537
Chris Lattner8047d9a2009-12-24 00:37:38 +00005538 if (!ConstantMemory)
5539 Builder.PendingLoads.push_back(LoadVal.getValue(1));
5540 return LoadVal;
5541}
5542
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005543/// processIntegerCallValue - Record the value for an instruction that
5544/// produces an integer result, converting the type where necessary.
5545void SelectionDAGBuilder::processIntegerCallValue(const Instruction &I,
5546 SDValue Value,
5547 bool IsSigned) {
5548 EVT VT = TM.getTargetLowering()->getValueType(I.getType(), true);
5549 if (IsSigned)
5550 Value = DAG.getSExtOrTrunc(Value, getCurSDLoc(), VT);
5551 else
5552 Value = DAG.getZExtOrTrunc(Value, getCurSDLoc(), VT);
5553 setValue(&I, Value);
5554}
Chris Lattner8047d9a2009-12-24 00:37:38 +00005555
5556/// visitMemCmpCall - See if we can lower a call to memcmp in an optimized form.
5557/// If so, return true and lower it, otherwise return false and it will be
5558/// lowered like a normal call.
Dan Gohman46510a72010-04-15 01:51:59 +00005559bool SelectionDAGBuilder::visitMemCmpCall(const CallInst &I) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00005560 // Verify that the prototype makes sense. int memcmp(void*,void*,size_t)
Gabor Greif37387d52010-06-30 12:55:46 +00005561 if (I.getNumArgOperands() != 3)
Chris Lattner8047d9a2009-12-24 00:37:38 +00005562 return false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005563
Gabor Greif0635f352010-06-25 09:38:13 +00005564 const Value *LHS = I.getArgOperand(0), *RHS = I.getArgOperand(1);
Duncan Sands1df98592010-02-16 11:11:14 +00005565 if (!LHS->getType()->isPointerTy() || !RHS->getType()->isPointerTy() ||
Gabor Greif0635f352010-06-25 09:38:13 +00005566 !I.getArgOperand(2)->getType()->isIntegerTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +00005567 !I.getType()->isIntegerTy())
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005568 return false;
5569
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005570 const Value *Size = I.getArgOperand(2);
5571 const ConstantInt *CSize = dyn_cast<ConstantInt>(Size);
5572 if (CSize && CSize->getZExtValue() == 0) {
Richard Sandifordac168b82013-08-12 10:28:10 +00005573 EVT CallVT = TM.getTargetLowering()->getValueType(I.getType(), true);
5574 setValue(&I, DAG.getConstant(0, CallVT));
5575 return true;
5576 }
5577
Richard Sandifordac168b82013-08-12 10:28:10 +00005578 const TargetSelectionDAGInfo &TSI = DAG.getSelectionDAGInfo();
5579 std::pair<SDValue, SDValue> Res =
5580 TSI.EmitTargetCodeForMemcmp(DAG, getCurSDLoc(), DAG.getRoot(),
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005581 getValue(LHS), getValue(RHS), getValue(Size),
5582 MachinePointerInfo(LHS),
5583 MachinePointerInfo(RHS));
Richard Sandifordac168b82013-08-12 10:28:10 +00005584 if (Res.first.getNode()) {
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005585 processIntegerCallValue(I, Res.first, true);
5586 PendingLoads.push_back(Res.second);
Richard Sandifordac168b82013-08-12 10:28:10 +00005587 return true;
5588 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005589
Chris Lattner8047d9a2009-12-24 00:37:38 +00005590 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
5591 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005592 if (CSize && IsOnlyUsedInZeroEqualityComparison(&I)) {
Chris Lattner04b091a2009-12-24 01:07:17 +00005593 bool ActuallyDoIt = true;
5594 MVT LoadVT;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005595 Type *LoadTy;
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005596 switch (CSize->getZExtValue()) {
Chris Lattner04b091a2009-12-24 01:07:17 +00005597 default:
5598 LoadVT = MVT::Other;
5599 LoadTy = 0;
5600 ActuallyDoIt = false;
5601 break;
5602 case 2:
5603 LoadVT = MVT::i16;
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005604 LoadTy = Type::getInt16Ty(CSize->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005605 break;
5606 case 4:
5607 LoadVT = MVT::i32;
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005608 LoadTy = Type::getInt32Ty(CSize->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005609 break;
5610 case 8:
5611 LoadVT = MVT::i64;
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005612 LoadTy = Type::getInt64Ty(CSize->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005613 break;
5614 /*
5615 case 16:
5616 LoadVT = MVT::v4i32;
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005617 LoadTy = Type::getInt32Ty(CSize->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005618 LoadTy = VectorType::get(LoadTy, 4);
5619 break;
5620 */
5621 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005622
Chris Lattner04b091a2009-12-24 01:07:17 +00005623 // This turns into unaligned loads. We only do this if the target natively
5624 // supports the MVT we'll be loading or if it is small enough (<= 4) that
5625 // we'll only produce a small number of byte loads.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005626
Chris Lattner04b091a2009-12-24 01:07:17 +00005627 // Require that we can find a legal MVT, and only do this if the target
5628 // supports unaligned loads of that type. Expanding into byte loads would
5629 // bloat the code.
Bill Wendlingba54bca2013-06-19 21:36:55 +00005630 const TargetLowering *TLI = TM.getTargetLowering();
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005631 if (ActuallyDoIt && CSize->getZExtValue() > 4) {
Chris Lattner04b091a2009-12-24 01:07:17 +00005632 // TODO: Handle 5 byte compare as 4-byte + 1 byte.
5633 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
Bill Wendlingba54bca2013-06-19 21:36:55 +00005634 if (!TLI->isTypeLegal(LoadVT) ||!TLI->allowsUnalignedMemoryAccesses(LoadVT))
Chris Lattner04b091a2009-12-24 01:07:17 +00005635 ActuallyDoIt = false;
5636 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005637
Chris Lattner04b091a2009-12-24 01:07:17 +00005638 if (ActuallyDoIt) {
5639 SDValue LHSVal = getMemCmpLoad(LHS, LoadVT, LoadTy, *this);
5640 SDValue RHSVal = getMemCmpLoad(RHS, LoadVT, LoadTy, *this);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005641
Andrew Trickac6d9be2013-05-25 02:42:55 +00005642 SDValue Res = DAG.getSetCC(getCurSDLoc(), MVT::i1, LHSVal, RHSVal,
Chris Lattner04b091a2009-12-24 01:07:17 +00005643 ISD::SETNE);
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005644 processIntegerCallValue(I, Res, false);
Chris Lattner04b091a2009-12-24 01:07:17 +00005645 return true;
5646 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00005647 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005648
5649
Chris Lattner8047d9a2009-12-24 00:37:38 +00005650 return false;
5651}
5652
Richard Sandiford8c201582013-08-20 09:38:48 +00005653/// visitMemChrCall -- See if we can lower a memchr call into an optimized
5654/// form. If so, return true and lower it, otherwise return false and it
5655/// will be lowered like a normal call.
5656bool SelectionDAGBuilder::visitMemChrCall(const CallInst &I) {
5657 // Verify that the prototype makes sense. void *memchr(void *, int, size_t)
5658 if (I.getNumArgOperands() != 3)
5659 return false;
5660
5661 const Value *Src = I.getArgOperand(0);
5662 const Value *Char = I.getArgOperand(1);
5663 const Value *Length = I.getArgOperand(2);
5664 if (!Src->getType()->isPointerTy() ||
5665 !Char->getType()->isIntegerTy() ||
5666 !Length->getType()->isIntegerTy() ||
5667 !I.getType()->isPointerTy())
5668 return false;
5669
5670 const TargetSelectionDAGInfo &TSI = DAG.getSelectionDAGInfo();
5671 std::pair<SDValue, SDValue> Res =
5672 TSI.EmitTargetCodeForMemchr(DAG, getCurSDLoc(), DAG.getRoot(),
5673 getValue(Src), getValue(Char), getValue(Length),
5674 MachinePointerInfo(Src));
5675 if (Res.first.getNode()) {
5676 setValue(&I, Res.first);
5677 PendingLoads.push_back(Res.second);
5678 return true;
5679 }
5680
5681 return false;
5682}
5683
Richard Sandiford4fc73552013-08-16 11:29:37 +00005684/// visitStrCpyCall -- See if we can lower a strcpy or stpcpy call into an
5685/// optimized form. If so, return true and lower it, otherwise return false
5686/// and it will be lowered like a normal call.
5687bool SelectionDAGBuilder::visitStrCpyCall(const CallInst &I, bool isStpcpy) {
5688 // Verify that the prototype makes sense. char *strcpy(char *, char *)
5689 if (I.getNumArgOperands() != 2)
5690 return false;
5691
5692 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
5693 if (!Arg0->getType()->isPointerTy() ||
5694 !Arg1->getType()->isPointerTy() ||
5695 !I.getType()->isPointerTy())
5696 return false;
5697
5698 const TargetSelectionDAGInfo &TSI = DAG.getSelectionDAGInfo();
5699 std::pair<SDValue, SDValue> Res =
5700 TSI.EmitTargetCodeForStrcpy(DAG, getCurSDLoc(), getRoot(),
5701 getValue(Arg0), getValue(Arg1),
5702 MachinePointerInfo(Arg0),
5703 MachinePointerInfo(Arg1), isStpcpy);
5704 if (Res.first.getNode()) {
5705 setValue(&I, Res.first);
5706 DAG.setRoot(Res.second);
5707 return true;
5708 }
5709
5710 return false;
5711}
5712
Richard Sandiforde1b2af72013-08-16 11:21:54 +00005713/// visitStrCmpCall - See if we can lower a call to strcmp in an optimized form.
5714/// If so, return true and lower it, otherwise return false and it will be
5715/// lowered like a normal call.
5716bool SelectionDAGBuilder::visitStrCmpCall(const CallInst &I) {
5717 // Verify that the prototype makes sense. int strcmp(void*,void*)
5718 if (I.getNumArgOperands() != 2)
5719 return false;
5720
5721 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
5722 if (!Arg0->getType()->isPointerTy() ||
5723 !Arg1->getType()->isPointerTy() ||
5724 !I.getType()->isIntegerTy())
5725 return false;
5726
5727 const TargetSelectionDAGInfo &TSI = DAG.getSelectionDAGInfo();
5728 std::pair<SDValue, SDValue> Res =
5729 TSI.EmitTargetCodeForStrcmp(DAG, getCurSDLoc(), DAG.getRoot(),
5730 getValue(Arg0), getValue(Arg1),
5731 MachinePointerInfo(Arg0),
5732 MachinePointerInfo(Arg1));
5733 if (Res.first.getNode()) {
5734 processIntegerCallValue(I, Res.first, true);
5735 PendingLoads.push_back(Res.second);
5736 return true;
5737 }
5738
5739 return false;
5740}
5741
Richard Sandiford19262ee2013-08-16 11:41:43 +00005742/// visitStrLenCall -- See if we can lower a strlen call into an optimized
5743/// form. If so, return true and lower it, otherwise return false and it
5744/// will be lowered like a normal call.
5745bool SelectionDAGBuilder::visitStrLenCall(const CallInst &I) {
5746 // Verify that the prototype makes sense. size_t strlen(char *)
5747 if (I.getNumArgOperands() != 1)
5748 return false;
5749
5750 const Value *Arg0 = I.getArgOperand(0);
5751 if (!Arg0->getType()->isPointerTy() || !I.getType()->isIntegerTy())
5752 return false;
5753
5754 const TargetSelectionDAGInfo &TSI = DAG.getSelectionDAGInfo();
5755 std::pair<SDValue, SDValue> Res =
5756 TSI.EmitTargetCodeForStrlen(DAG, getCurSDLoc(), DAG.getRoot(),
5757 getValue(Arg0), MachinePointerInfo(Arg0));
5758 if (Res.first.getNode()) {
5759 processIntegerCallValue(I, Res.first, false);
5760 PendingLoads.push_back(Res.second);
5761 return true;
5762 }
5763
5764 return false;
5765}
5766
5767/// visitStrNLenCall -- See if we can lower a strnlen call into an optimized
5768/// form. If so, return true and lower it, otherwise return false and it
5769/// will be lowered like a normal call.
5770bool SelectionDAGBuilder::visitStrNLenCall(const CallInst &I) {
5771 // Verify that the prototype makes sense. size_t strnlen(char *, size_t)
5772 if (I.getNumArgOperands() != 2)
5773 return false;
5774
5775 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
5776 if (!Arg0->getType()->isPointerTy() ||
5777 !Arg1->getType()->isIntegerTy() ||
5778 !I.getType()->isIntegerTy())
5779 return false;
5780
5781 const TargetSelectionDAGInfo &TSI = DAG.getSelectionDAGInfo();
5782 std::pair<SDValue, SDValue> Res =
5783 TSI.EmitTargetCodeForStrnlen(DAG, getCurSDLoc(), DAG.getRoot(),
5784 getValue(Arg0), getValue(Arg1),
5785 MachinePointerInfo(Arg0));
5786 if (Res.first.getNode()) {
5787 processIntegerCallValue(I, Res.first, false);
5788 PendingLoads.push_back(Res.second);
5789 return true;
5790 }
5791
5792 return false;
5793}
5794
Bob Wilson53624a22012-08-03 23:29:17 +00005795/// visitUnaryFloatCall - If a call instruction is a unary floating-point
5796/// operation (as expected), translate it to an SDNode with the specified opcode
5797/// and return true.
5798bool SelectionDAGBuilder::visitUnaryFloatCall(const CallInst &I,
5799 unsigned Opcode) {
5800 // Sanity check that it really is a unary floating-point call.
5801 if (I.getNumArgOperands() != 1 ||
5802 !I.getArgOperand(0)->getType()->isFloatingPointTy() ||
5803 I.getType() != I.getArgOperand(0)->getType() ||
5804 !I.onlyReadsMemory())
5805 return false;
5806
5807 SDValue Tmp = getValue(I.getArgOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005808 setValue(&I, DAG.getNode(Opcode, getCurSDLoc(), Tmp.getValueType(), Tmp));
Bob Wilson53624a22012-08-03 23:29:17 +00005809 return true;
5810}
Chris Lattner8047d9a2009-12-24 00:37:38 +00005811
Dan Gohman46510a72010-04-15 01:51:59 +00005812void SelectionDAGBuilder::visitCall(const CallInst &I) {
Chris Lattner598751e2010-07-05 05:36:21 +00005813 // Handle inline assembly differently.
5814 if (isa<InlineAsm>(I.getCalledValue())) {
5815 visitInlineAsm(&I);
5816 return;
5817 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00005818
Michael J. Spencer391b43b2010-10-21 20:49:23 +00005819 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Michael J. Spencerc9c137b2012-02-22 19:06:13 +00005820 ComputeUsesVAFloatArgument(I, &MMI);
Michael J. Spencer391b43b2010-10-21 20:49:23 +00005821
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005822 const char *RenameFn = 0;
5823 if (Function *F = I.getCalledFunction()) {
5824 if (F->isDeclaration()) {
Chris Lattner598751e2010-07-05 05:36:21 +00005825 if (const TargetIntrinsicInfo *II = TM.getIntrinsicInfo()) {
Dale Johannesen49de9822009-02-05 01:49:45 +00005826 if (unsigned IID = II->getIntrinsicID(F)) {
5827 RenameFn = visitIntrinsicCall(I, IID);
5828 if (!RenameFn)
5829 return;
5830 }
5831 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005832 if (unsigned IID = F->getIntrinsicID()) {
5833 RenameFn = visitIntrinsicCall(I, IID);
5834 if (!RenameFn)
5835 return;
5836 }
5837 }
5838
5839 // Check for well-known libc/libm calls. If the function is internal, it
5840 // can't be a library call.
Bob Wilson982dc842012-08-03 21:26:24 +00005841 LibFunc::Func Func;
5842 if (!F->hasLocalLinkage() && F->hasName() &&
5843 LibInfo->getLibFunc(F->getName(), Func) &&
5844 LibInfo->hasOptimizedCodeGen(Func)) {
5845 switch (Func) {
5846 default: break;
5847 case LibFunc::copysign:
5848 case LibFunc::copysignf:
5849 case LibFunc::copysignl:
Gabor Greif37387d52010-06-30 12:55:46 +00005850 if (I.getNumArgOperands() == 2 && // Basic sanity checks.
Gabor Greif0635f352010-06-25 09:38:13 +00005851 I.getArgOperand(0)->getType()->isFloatingPointTy() &&
5852 I.getType() == I.getArgOperand(0)->getType() &&
Bob Wilson53624a22012-08-03 23:29:17 +00005853 I.getType() == I.getArgOperand(1)->getType() &&
5854 I.onlyReadsMemory()) {
Gabor Greif0635f352010-06-25 09:38:13 +00005855 SDValue LHS = getValue(I.getArgOperand(0));
5856 SDValue RHS = getValue(I.getArgOperand(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005857 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurSDLoc(),
Bill Wendling0d580132009-12-23 01:28:19 +00005858 LHS.getValueType(), LHS, RHS));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005859 return;
5860 }
Bob Wilson982dc842012-08-03 21:26:24 +00005861 break;
5862 case LibFunc::fabs:
5863 case LibFunc::fabsf:
5864 case LibFunc::fabsl:
Bob Wilson53624a22012-08-03 23:29:17 +00005865 if (visitUnaryFloatCall(I, ISD::FABS))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005866 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005867 break;
5868 case LibFunc::sin:
5869 case LibFunc::sinf:
5870 case LibFunc::sinl:
Bob Wilson53624a22012-08-03 23:29:17 +00005871 if (visitUnaryFloatCall(I, ISD::FSIN))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005872 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005873 break;
5874 case LibFunc::cos:
5875 case LibFunc::cosf:
5876 case LibFunc::cosl:
Bob Wilson53624a22012-08-03 23:29:17 +00005877 if (visitUnaryFloatCall(I, ISD::FCOS))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005878 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005879 break;
5880 case LibFunc::sqrt:
5881 case LibFunc::sqrtf:
5882 case LibFunc::sqrtl:
Preston Gurdb704d232013-05-27 15:44:35 +00005883 case LibFunc::sqrt_finite:
5884 case LibFunc::sqrtf_finite:
5885 case LibFunc::sqrtl_finite:
Bob Wilson53624a22012-08-03 23:29:17 +00005886 if (visitUnaryFloatCall(I, ISD::FSQRT))
Dale Johannesen52fb79b2009-09-25 17:23:22 +00005887 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005888 break;
5889 case LibFunc::floor:
5890 case LibFunc::floorf:
5891 case LibFunc::floorl:
Bob Wilson53624a22012-08-03 23:29:17 +00005892 if (visitUnaryFloatCall(I, ISD::FFLOOR))
Owen Anderson4a4fdf32011-12-08 19:32:14 +00005893 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005894 break;
5895 case LibFunc::nearbyint:
5896 case LibFunc::nearbyintf:
5897 case LibFunc::nearbyintl:
Bob Wilson53624a22012-08-03 23:29:17 +00005898 if (visitUnaryFloatCall(I, ISD::FNEARBYINT))
Owen Anderson4a4fdf32011-12-08 19:32:14 +00005899 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005900 break;
5901 case LibFunc::ceil:
5902 case LibFunc::ceilf:
5903 case LibFunc::ceill:
Bob Wilson53624a22012-08-03 23:29:17 +00005904 if (visitUnaryFloatCall(I, ISD::FCEIL))
Owen Anderson4a4fdf32011-12-08 19:32:14 +00005905 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005906 break;
5907 case LibFunc::rint:
5908 case LibFunc::rintf:
5909 case LibFunc::rintl:
Bob Wilson53624a22012-08-03 23:29:17 +00005910 if (visitUnaryFloatCall(I, ISD::FRINT))
Owen Anderson4a4fdf32011-12-08 19:32:14 +00005911 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005912 break;
Hal Finkel41418d12013-08-07 22:49:12 +00005913 case LibFunc::round:
5914 case LibFunc::roundf:
5915 case LibFunc::roundl:
5916 if (visitUnaryFloatCall(I, ISD::FROUND))
5917 return;
5918 break;
Bob Wilson982dc842012-08-03 21:26:24 +00005919 case LibFunc::trunc:
5920 case LibFunc::truncf:
5921 case LibFunc::truncl:
Bob Wilson53624a22012-08-03 23:29:17 +00005922 if (visitUnaryFloatCall(I, ISD::FTRUNC))
Owen Anderson4a4fdf32011-12-08 19:32:14 +00005923 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005924 break;
5925 case LibFunc::log2:
5926 case LibFunc::log2f:
5927 case LibFunc::log2l:
Bob Wilson53624a22012-08-03 23:29:17 +00005928 if (visitUnaryFloatCall(I, ISD::FLOG2))
Owen Anderson4e0adfa2011-12-15 00:54:12 +00005929 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005930 break;
5931 case LibFunc::exp2:
5932 case LibFunc::exp2f:
5933 case LibFunc::exp2l:
Bob Wilson53624a22012-08-03 23:29:17 +00005934 if (visitUnaryFloatCall(I, ISD::FEXP2))
Owen Anderson4e0adfa2011-12-15 00:54:12 +00005935 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005936 break;
5937 case LibFunc::memcmp:
Chris Lattner8047d9a2009-12-24 00:37:38 +00005938 if (visitMemCmpCall(I))
5939 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005940 break;
Richard Sandiford8c201582013-08-20 09:38:48 +00005941 case LibFunc::memchr:
5942 if (visitMemChrCall(I))
5943 return;
5944 break;
Richard Sandiford4fc73552013-08-16 11:29:37 +00005945 case LibFunc::strcpy:
5946 if (visitStrCpyCall(I, false))
5947 return;
5948 break;
5949 case LibFunc::stpcpy:
5950 if (visitStrCpyCall(I, true))
5951 return;
5952 break;
Richard Sandiforde1b2af72013-08-16 11:21:54 +00005953 case LibFunc::strcmp:
5954 if (visitStrCmpCall(I))
5955 return;
5956 break;
Richard Sandiford19262ee2013-08-16 11:41:43 +00005957 case LibFunc::strlen:
5958 if (visitStrLenCall(I))
5959 return;
5960 break;
5961 case LibFunc::strnlen:
5962 if (visitStrNLenCall(I))
5963 return;
5964 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005965 }
5966 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005967 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00005968
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005969 SDValue Callee;
5970 if (!RenameFn)
Gabor Greif0635f352010-06-25 09:38:13 +00005971 Callee = getValue(I.getCalledValue());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005972 else
Bill Wendlingba54bca2013-06-19 21:36:55 +00005973 Callee = DAG.getExternalSymbol(RenameFn,
5974 TM.getTargetLowering()->getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005975
Bill Wendling0d580132009-12-23 01:28:19 +00005976 // Check if we can potentially perform a tail call. More detailed checking is
5977 // be done within LowerCallTo, after more information about the call is known.
Evan Cheng11e67932010-01-26 23:13:04 +00005978 LowerCallTo(&I, Callee, I.isTailCall());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005979}
5980
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005981namespace {
Dan Gohman462f6b52010-05-29 17:53:24 +00005982
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005983/// AsmOperandInfo - This contains information for each constraint that we are
5984/// lowering.
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005985class SDISelAsmOperandInfo : public TargetLowering::AsmOperandInfo {
Cedric Venetaff9c272009-02-14 16:06:42 +00005986public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005987 /// CallOperand - If this is the result output operand or a clobber
5988 /// this is null, otherwise it is the incoming operand to the CallInst.
5989 /// This gets modified as the asm is processed.
5990 SDValue CallOperand;
5991
5992 /// AssignedRegs - If this is a register or register class operand, this
5993 /// contains the set of register corresponding to the operand.
5994 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005995
John Thompsoneac6e1d2010-09-13 18:15:37 +00005996 explicit SDISelAsmOperandInfo(const TargetLowering::AsmOperandInfo &info)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005997 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
5998 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005999
Owen Andersone50ed302009-08-10 22:56:29 +00006000 /// getCallOperandValEVT - Return the EVT of the Value* that this operand
Chris Lattner81249c92008-10-17 17:05:25 +00006001 /// corresponds to. If there is no Value* for this operand, it returns
Owen Anderson825b72b2009-08-11 20:47:22 +00006002 /// MVT::Other.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00006003 EVT getCallOperandValEVT(LLVMContext &Context,
Owen Anderson1d0be152009-08-13 21:58:54 +00006004 const TargetLowering &TLI,
Micah Villmow3574eca2012-10-08 16:38:25 +00006005 const DataLayout *TD) const {
Owen Anderson825b72b2009-08-11 20:47:22 +00006006 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006007
Chris Lattner81249c92008-10-17 17:05:25 +00006008 if (isa<BasicBlock>(CallOperandVal))
6009 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006010
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006011 llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006012
Eric Christophercef81b72011-05-09 20:04:43 +00006013 // FIXME: code duplicated from TargetLowering::ParseConstraints().
Chris Lattner81249c92008-10-17 17:05:25 +00006014 // If this is an indirect operand, the operand is a pointer to the
6015 // accessed type.
Bob Wilsone261b0c2009-12-22 18:34:19 +00006016 if (isIndirect) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006017 llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
Bob Wilsone261b0c2009-12-22 18:34:19 +00006018 if (!PtrTy)
Chris Lattner75361b62010-04-07 22:58:41 +00006019 report_fatal_error("Indirect operand for inline asm not a pointer!");
Bob Wilsone261b0c2009-12-22 18:34:19 +00006020 OpTy = PtrTy->getElementType();
6021 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006022
Eric Christophercef81b72011-05-09 20:04:43 +00006023 // Look for vector wrapped in a struct. e.g. { <16 x i8> }.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006024 if (StructType *STy = dyn_cast<StructType>(OpTy))
Eric Christophercef81b72011-05-09 20:04:43 +00006025 if (STy->getNumElements() == 1)
6026 OpTy = STy->getElementType(0);
6027
Chris Lattner81249c92008-10-17 17:05:25 +00006028 // If OpTy is not a single value, it may be a struct/union that we
6029 // can tile with integers.
6030 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
6031 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
6032 switch (BitSize) {
6033 default: break;
6034 case 1:
6035 case 8:
6036 case 16:
6037 case 32:
6038 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00006039 case 128:
Owen Anderson1d0be152009-08-13 21:58:54 +00006040 OpTy = IntegerType::get(Context, BitSize);
Chris Lattner81249c92008-10-17 17:05:25 +00006041 break;
6042 }
6043 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006044
Chris Lattner81249c92008-10-17 17:05:25 +00006045 return TLI.getValueType(OpTy, true);
6046 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006047};
Dan Gohman462f6b52010-05-29 17:53:24 +00006048
John Thompson44ab89e2010-10-29 17:29:13 +00006049typedef SmallVector<SDISelAsmOperandInfo,16> SDISelAsmOperandInfoVector;
6050
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00006051} // end anonymous namespace
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006052
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006053/// GetRegistersForValue - Assign registers (virtual or physical) for the
6054/// specified operand. We prefer to assign virtual registers, to allow the
Bob Wilson266d9452009-12-17 05:07:36 +00006055/// register allocator to handle the assignment process. However, if the asm
6056/// uses features that we can't model on machineinstrs, we have SDISel do the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006057/// allocation. This produces generally horrible, but correct, code.
6058///
6059/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006060///
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00006061static void GetRegistersForValue(SelectionDAG &DAG,
6062 const TargetLowering &TLI,
Andrew Trickac6d9be2013-05-25 02:42:55 +00006063 SDLoc DL,
Benjamin Kramer8b93ff22012-02-24 14:01:17 +00006064 SDISelAsmOperandInfo &OpInfo) {
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00006065 LLVMContext &Context = *DAG.getContext();
Owen Anderson23b9b192009-08-12 00:36:31 +00006066
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006067 MachineFunction &MF = DAG.getMachineFunction();
6068 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006069
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006070 // If this is a constraint for a single physreg, or a constraint for a
6071 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006072 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006073 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
6074 OpInfo.ConstraintVT);
6075
6076 unsigned NumRegs = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00006077 if (OpInfo.ConstraintVT != MVT::Other) {
Chris Lattner01426e12008-10-21 00:45:36 +00006078 // If this is a FP input in an integer register (or visa versa) insert a bit
6079 // cast of the input value. More generally, handle any case where the input
6080 // value disagrees with the register class we plan to stick this in.
6081 if (OpInfo.Type == InlineAsm::isInput &&
6082 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00006083 // Try to convert to the first EVT that the reg class contains. If the
Chris Lattner01426e12008-10-21 00:45:36 +00006084 // types are identical size, use a bitcast to convert (e.g. two differing
6085 // vector types).
Patrik Hagglund8963fec2012-12-19 12:23:01 +00006086 MVT RegVT = *PhysReg.second->vt_begin();
Chris Lattner01426e12008-10-21 00:45:36 +00006087 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00006088 OpInfo.CallOperand = DAG.getNode(ISD::BITCAST, DL,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00006089 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00006090 OpInfo.ConstraintVT = RegVT;
6091 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
6092 // If the input is a FP value and we want it in FP registers, do a
6093 // bitcast to the corresponding integer type. This turns an f64 value
6094 // into i64, which can be passed with two i32 values on a 32-bit
6095 // machine.
Patrik Hagglund8963fec2012-12-19 12:23:01 +00006096 RegVT = MVT::getIntegerVT(OpInfo.ConstraintVT.getSizeInBits());
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00006097 OpInfo.CallOperand = DAG.getNode(ISD::BITCAST, DL,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00006098 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00006099 OpInfo.ConstraintVT = RegVT;
6100 }
6101 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006102
Owen Anderson23b9b192009-08-12 00:36:31 +00006103 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00006104 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006105
Patrik Hagglund8963fec2012-12-19 12:23:01 +00006106 MVT RegVT;
Owen Andersone50ed302009-08-10 22:56:29 +00006107 EVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006108
6109 // If this is a constraint for a specific physical register, like {r17},
6110 // assign it now.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00006111 if (unsigned AssignedReg = PhysReg.first) {
6112 const TargetRegisterClass *RC = PhysReg.second;
Owen Anderson825b72b2009-08-11 20:47:22 +00006113 if (OpInfo.ConstraintVT == MVT::Other)
Chris Lattnere2f7bf82009-03-24 15:27:37 +00006114 ValueVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006115
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006116 // Get the actual register value type. This is important, because the user
6117 // may have asked for (e.g.) the AX register in i32 type. We need to
6118 // remember that AX is actually i16 to get the right extension.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00006119 RegVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006120
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006121 // This is a explicit reference to a physical register.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00006122 Regs.push_back(AssignedReg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006123
6124 // If this is an expanded reference, add the rest of the regs to Regs.
6125 if (NumRegs != 1) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00006126 TargetRegisterClass::iterator I = RC->begin();
6127 for (; *I != AssignedReg; ++I)
6128 assert(I != RC->end() && "Didn't find reg!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006129
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006130 // Already added the first reg.
6131 --NumRegs; ++I;
6132 for (; NumRegs; --NumRegs, ++I) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00006133 assert(I != RC->end() && "Ran out of registers to allocate!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006134 Regs.push_back(*I);
6135 }
6136 }
Bill Wendling651ad132009-12-22 01:25:10 +00006137
Dan Gohman7451d3e2010-05-29 17:03:36 +00006138 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006139 return;
6140 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006141
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006142 // Otherwise, if this was a reference to an LLVM register class, create vregs
6143 // for this reference.
Chris Lattnerb3b44842009-03-24 15:25:07 +00006144 if (const TargetRegisterClass *RC = PhysReg.second) {
6145 RegVT = *RC->vt_begin();
Owen Anderson825b72b2009-08-11 20:47:22 +00006146 if (OpInfo.ConstraintVT == MVT::Other)
Evan Chengfb112882009-03-23 08:01:15 +00006147 ValueVT = RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006148
Evan Chengfb112882009-03-23 08:01:15 +00006149 // Create the appropriate number of virtual registers.
6150 MachineRegisterInfo &RegInfo = MF.getRegInfo();
6151 for (; NumRegs; --NumRegs)
Chris Lattnerb3b44842009-03-24 15:25:07 +00006152 Regs.push_back(RegInfo.createVirtualRegister(RC));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006153
Dan Gohman7451d3e2010-05-29 17:03:36 +00006154 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
Evan Chengfb112882009-03-23 08:01:15 +00006155 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006156 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00006157
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006158 // Otherwise, we couldn't allocate enough registers for this.
6159}
6160
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006161/// visitInlineAsm - Handle a call to an InlineAsm object.
6162///
Dan Gohman46510a72010-04-15 01:51:59 +00006163void SelectionDAGBuilder::visitInlineAsm(ImmutableCallSite CS) {
6164 const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006165
6166 /// ConstraintOperands - Information about all of the constraints.
John Thompson44ab89e2010-10-29 17:29:13 +00006167 SDISelAsmOperandInfoVector ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006168
Bill Wendlingba54bca2013-06-19 21:36:55 +00006169 const TargetLowering *TLI = TM.getTargetLowering();
Evan Chengce1cdac2011-05-06 20:52:23 +00006170 TargetLowering::AsmOperandInfoVector
Bill Wendlingba54bca2013-06-19 21:36:55 +00006171 TargetConstraints = TLI->ParseConstraints(CS);
Evan Chengce1cdac2011-05-06 20:52:23 +00006172
John Thompsoneac6e1d2010-09-13 18:15:37 +00006173 bool hasMemory = false;
Michael J. Spencere70c5262010-10-16 08:25:21 +00006174
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006175 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
6176 unsigned ResNo = 0; // ResNo - The result number of the next output.
John Thompsoneac6e1d2010-09-13 18:15:37 +00006177 for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) {
6178 ConstraintOperands.push_back(SDISelAsmOperandInfo(TargetConstraints[i]));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006179 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Michael J. Spencere70c5262010-10-16 08:25:21 +00006180
Patrik Hagglundc698d3a2012-12-19 15:19:11 +00006181 MVT OpVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006182
6183 // Compute the value type for each operand.
6184 switch (OpInfo.Type) {
6185 case InlineAsm::isOutput:
6186 // Indirect outputs just consume an argument.
6187 if (OpInfo.isIndirect) {
Dan Gohman46510a72010-04-15 01:51:59 +00006188 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006189 break;
6190 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006191
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006192 // The return value of the call is this value. As such, there is no
6193 // corresponding argument.
Nick Lewycky8de34002011-09-30 22:19:53 +00006194 assert(!CS.getType()->isVoidTy() && "Bad inline asm!");
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006195 if (StructType *STy = dyn_cast<StructType>(CS.getType())) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00006196 OpVT = TLI->getSimpleValueType(STy->getElementType(ResNo));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006197 } else {
6198 assert(ResNo == 0 && "Asm only has one result!");
Bill Wendlingba54bca2013-06-19 21:36:55 +00006199 OpVT = TLI->getSimpleValueType(CS.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006200 }
6201 ++ResNo;
6202 break;
6203 case InlineAsm::isInput:
Dan Gohman46510a72010-04-15 01:51:59 +00006204 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006205 break;
6206 case InlineAsm::isClobber:
6207 // Nothing to do.
6208 break;
6209 }
6210
6211 // If this is an input or an indirect output, process the call argument.
6212 // BasicBlocks are labels, currently appearing only in asm's.
6213 if (OpInfo.CallOperandVal) {
Dan Gohman46510a72010-04-15 01:51:59 +00006214 if (const BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006215 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00006216 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006217 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006218 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006219
Bill Wendlingba54bca2013-06-19 21:36:55 +00006220 OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), *TLI, TD).
Patrik Hagglundc698d3a2012-12-19 15:19:11 +00006221 getSimpleVT();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006222 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006223
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006224 OpInfo.ConstraintVT = OpVT;
Michael J. Spencere70c5262010-10-16 08:25:21 +00006225
John Thompsoneac6e1d2010-09-13 18:15:37 +00006226 // Indirect operand accesses access memory.
6227 if (OpInfo.isIndirect)
6228 hasMemory = true;
6229 else {
6230 for (unsigned j = 0, ee = OpInfo.Codes.size(); j != ee; ++j) {
Evan Chengce1cdac2011-05-06 20:52:23 +00006231 TargetLowering::ConstraintType
Bill Wendlingba54bca2013-06-19 21:36:55 +00006232 CType = TLI->getConstraintType(OpInfo.Codes[j]);
John Thompsoneac6e1d2010-09-13 18:15:37 +00006233 if (CType == TargetLowering::C_Memory) {
6234 hasMemory = true;
6235 break;
6236 }
6237 }
6238 }
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006239 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006240
John Thompsoneac6e1d2010-09-13 18:15:37 +00006241 SDValue Chain, Flag;
6242
6243 // We won't need to flush pending loads if this asm doesn't touch
6244 // memory and is nonvolatile.
6245 if (hasMemory || IA->hasSideEffects())
6246 Chain = getRoot();
6247 else
6248 Chain = DAG.getRoot();
6249
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006250 // Second pass over the constraints: compute which constraint option to use
6251 // and assign registers to constraints that want a specific physreg.
John Thompsoneac6e1d2010-09-13 18:15:37 +00006252 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006253 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006254
John Thompson54584742010-09-24 22:24:05 +00006255 // If this is an output operand with a matching input operand, look up the
6256 // matching input. If their types mismatch, e.g. one is an integer, the
6257 // other is floating point, or their sizes are different, flag it as an
6258 // error.
6259 if (OpInfo.hasMatchingInput()) {
6260 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
Michael J. Spencere70c5262010-10-16 08:25:21 +00006261
John Thompson54584742010-09-24 22:24:05 +00006262 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Bill Wendling96cb1122012-07-19 00:04:14 +00006263 std::pair<unsigned, const TargetRegisterClass*> MatchRC =
Bill Wendlingba54bca2013-06-19 21:36:55 +00006264 TLI->getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
6265 OpInfo.ConstraintVT);
Bill Wendling96cb1122012-07-19 00:04:14 +00006266 std::pair<unsigned, const TargetRegisterClass*> InputRC =
Bill Wendlingba54bca2013-06-19 21:36:55 +00006267 TLI->getRegForInlineAsmConstraint(Input.ConstraintCode,
6268 Input.ConstraintVT);
John Thompson54584742010-09-24 22:24:05 +00006269 if ((OpInfo.ConstraintVT.isInteger() !=
6270 Input.ConstraintVT.isInteger()) ||
Eric Christopher5427ede2011-07-14 20:13:52 +00006271 (MatchRC.second != InputRC.second)) {
John Thompson54584742010-09-24 22:24:05 +00006272 report_fatal_error("Unsupported asm: input constraint"
6273 " with a matching output constraint of"
6274 " incompatible type!");
6275 }
6276 Input.ConstraintVT = OpInfo.ConstraintVT;
6277 }
6278 }
6279
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006280 // Compute the constraint code and ConstraintType to use.
Bill Wendlingba54bca2013-06-19 21:36:55 +00006281 TLI->ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006282
Eric Christopherfffe3632013-01-11 18:12:39 +00006283 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
6284 OpInfo.Type == InlineAsm::isClobber)
6285 continue;
6286
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006287 // If this is a memory input, and if the operand is not indirect, do what we
6288 // need to to provide an address for the memory input.
6289 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
6290 !OpInfo.isIndirect) {
Evan Chengce1cdac2011-05-06 20:52:23 +00006291 assert((OpInfo.isMultipleAlternative ||
6292 (OpInfo.Type == InlineAsm::isInput)) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006293 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006294
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006295 // Memory operands really want the address of the value. If we don't have
6296 // an indirect input, put it in the constpool if we can, otherwise spill
6297 // it to a stack slot.
Eric Christophere0b42c02011-06-03 17:21:23 +00006298 // TODO: This isn't quite right. We need to handle these according to
6299 // the addressing mode that the constraint wants. Also, this may take
6300 // an additional register for the computation and we don't want that
6301 // either.
Eric Christopher471e4222011-06-08 23:55:35 +00006302
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006303 // If the operand is a float, integer, or vector constant, spill to a
6304 // constant pool entry to get its address.
Dan Gohman46510a72010-04-15 01:51:59 +00006305 const Value *OpVal = OpInfo.CallOperandVal;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006306 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
Chris Lattnera78fa8c2012-01-27 03:08:05 +00006307 isa<ConstantVector>(OpVal) || isa<ConstantDataVector>(OpVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006308 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
Bill Wendlingba54bca2013-06-19 21:36:55 +00006309 TLI->getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006310 } else {
6311 // Otherwise, create a stack slot and emit a store to it before the
6312 // asm.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006313 Type *Ty = OpVal->getType();
Bill Wendlingba54bca2013-06-19 21:36:55 +00006314 uint64_t TySize = TLI->getDataLayout()->getTypeAllocSize(Ty);
6315 unsigned Align = TLI->getDataLayout()->getPrefTypeAlignment(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006316 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006317 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Bill Wendlingba54bca2013-06-19 21:36:55 +00006318 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI->getPointerTy());
Andrew Trickac6d9be2013-05-25 02:42:55 +00006319 Chain = DAG.getStore(Chain, getCurSDLoc(),
Chris Lattnerecf42c42010-09-21 16:36:31 +00006320 OpInfo.CallOperand, StackSlot,
6321 MachinePointerInfo::getFixedStack(SSFI),
David Greene1e559442010-02-15 17:00:31 +00006322 false, false, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006323 OpInfo.CallOperand = StackSlot;
6324 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006325
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006326 // There is no longer a Value* corresponding to this operand.
6327 OpInfo.CallOperandVal = 0;
Bill Wendling651ad132009-12-22 01:25:10 +00006328
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006329 // It is now an indirect operand.
6330 OpInfo.isIndirect = true;
6331 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006332
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006333 // If this constraint is for a specific register, allocate it before
6334 // anything else.
6335 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Bill Wendlingba54bca2013-06-19 21:36:55 +00006336 GetRegistersForValue(DAG, *TLI, getCurSDLoc(), OpInfo);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006337 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006338
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006339 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00006340 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006341 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
6342 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006343
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006344 // C_Register operands have already been allocated, Other/Memory don't need
6345 // to be.
6346 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Bill Wendlingba54bca2013-06-19 21:36:55 +00006347 GetRegistersForValue(DAG, *TLI, getCurSDLoc(), OpInfo);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006348 }
6349
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006350 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
6351 std::vector<SDValue> AsmNodeOperands;
6352 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
6353 AsmNodeOperands.push_back(
Dan Gohmanf2d7fb32010-01-04 21:00:54 +00006354 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00006355 TLI->getPointerTy()));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006356
Chris Lattnerdecc2672010-04-07 05:20:54 +00006357 // If we have a !srcloc metadata node associated with it, we want to attach
6358 // this to the ultimately generated inline asm machineinstr. To do this, we
6359 // pass in the third operand as this (potentially null) inline asm MDNode.
6360 const MDNode *SrcLoc = CS.getInstruction()->getMetadata("srcloc");
6361 AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006362
Chad Rosier3d716882012-10-30 19:11:54 +00006363 // Remember the HasSideEffect, AlignStack, AsmDialect, MayLoad and MayStore
6364 // bits as operand 3.
Evan Chengc36b7062011-01-07 23:50:32 +00006365 unsigned ExtraInfo = 0;
6366 if (IA->hasSideEffects())
6367 ExtraInfo |= InlineAsm::Extra_HasSideEffects;
6368 if (IA->isAlignStack())
6369 ExtraInfo |= InlineAsm::Extra_IsAlignStack;
Chad Rosier77fffa62012-09-05 22:17:43 +00006370 // Set the asm dialect.
Chad Rosier2f1d8152012-09-05 22:40:13 +00006371 ExtraInfo |= IA->getDialect() * InlineAsm::Extra_AsmDialect;
Chad Rosier3d716882012-10-30 19:11:54 +00006372
6373 // Determine if this InlineAsm MayLoad or MayStore based on the constraints.
6374 for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) {
6375 TargetLowering::AsmOperandInfo &OpInfo = TargetConstraints[i];
6376
6377 // Compute the constraint code and ConstraintType to use.
Bill Wendlingba54bca2013-06-19 21:36:55 +00006378 TLI->ComputeConstraintToUse(OpInfo, SDValue());
Chad Rosier3d716882012-10-30 19:11:54 +00006379
Chad Rosierdfa4cec2012-10-30 20:01:12 +00006380 // Ideally, we would only check against memory constraints. However, the
6381 // meaning of an other constraint can be target-specific and we can't easily
6382 // reason about it. Therefore, be conservative and set MayLoad/MayStore
6383 // for other constriants as well.
Chad Rosier3d716882012-10-30 19:11:54 +00006384 if (OpInfo.ConstraintType == TargetLowering::C_Memory ||
6385 OpInfo.ConstraintType == TargetLowering::C_Other) {
6386 if (OpInfo.Type == InlineAsm::isInput)
6387 ExtraInfo |= InlineAsm::Extra_MayLoad;
6388 else if (OpInfo.Type == InlineAsm::isOutput)
6389 ExtraInfo |= InlineAsm::Extra_MayStore;
Eric Christopherfffe3632013-01-11 18:12:39 +00006390 else if (OpInfo.Type == InlineAsm::isClobber)
6391 ExtraInfo |= (InlineAsm::Extra_MayLoad | InlineAsm::Extra_MayStore);
Chad Rosier3d716882012-10-30 19:11:54 +00006392 }
6393 }
6394
Evan Chengc36b7062011-01-07 23:50:32 +00006395 AsmNodeOperands.push_back(DAG.getTargetConstant(ExtraInfo,
Bill Wendlingba54bca2013-06-19 21:36:55 +00006396 TLI->getPointerTy()));
Dale Johannesenf1e309e2010-07-02 20:16:09 +00006397
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006398 // Loop over all of the inputs, copying the operand values into the
6399 // appropriate registers and processing the output regs.
6400 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006401
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006402 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
6403 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006404
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006405 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
6406 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
6407
6408 switch (OpInfo.Type) {
6409 case InlineAsm::isOutput: {
6410 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
6411 OpInfo.ConstraintType != TargetLowering::C_Register) {
6412 // Memory output, or 'other' output (e.g. 'X' constraint).
6413 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
6414
6415 // Add information to the INLINEASM node to know about this output.
Chris Lattnerdecc2672010-04-07 05:20:54 +00006416 unsigned OpFlags = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
6417 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags,
Bill Wendlingba54bca2013-06-19 21:36:55 +00006418 TLI->getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006419 AsmNodeOperands.push_back(OpInfo.CallOperand);
6420 break;
6421 }
6422
6423 // Otherwise, this is a register or register class output.
6424
6425 // Copy the output from the appropriate register. Find a register that
6426 // we can use.
Chris Lattnerfcd70902012-01-03 23:51:01 +00006427 if (OpInfo.AssignedRegs.Regs.empty()) {
6428 LLVMContext &Ctx = *DAG.getContext();
Stephen Lin155615d2013-07-08 00:37:03 +00006429 Ctx.emitError(CS.getInstruction(),
Chris Lattnerfcd70902012-01-03 23:51:01 +00006430 "couldn't allocate output register for constraint '" +
Eric Christopher1a54c572013-07-31 01:26:24 +00006431 Twine(OpInfo.ConstraintCode) + "'");
6432 return;
Chris Lattnerfcd70902012-01-03 23:51:01 +00006433 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006434
6435 // If this is an indirect operand, store through the pointer after the
6436 // asm.
6437 if (OpInfo.isIndirect) {
6438 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
6439 OpInfo.CallOperandVal));
6440 } else {
6441 // This is the result value of the call.
Benjamin Kramerf0127052010-01-05 13:12:22 +00006442 assert(!CS.getType()->isVoidTy() && "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006443 // Concatenate this output onto the outputs list.
6444 RetValRegs.append(OpInfo.AssignedRegs);
6445 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006446
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006447 // Add information to the INLINEASM node to know that this register is
6448 // set.
Eric Christopherb0bee812013-07-30 22:50:44 +00006449 OpInfo.AssignedRegs
6450 .AddInlineAsmOperands(OpInfo.isEarlyClobber
6451 ? InlineAsm::Kind_RegDefEarlyClobber
6452 : InlineAsm::Kind_RegDef,
6453 false, 0, DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006454 break;
6455 }
6456 case InlineAsm::isInput: {
6457 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006458
Chris Lattner6bdcda32008-10-17 16:47:46 +00006459 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006460 // If this is required to match an output register we have already set,
6461 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00006462 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006463
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006464 // Scan until we find the definition we already emitted of this operand.
6465 // When we find it, create a RegsForValue operand.
Chris Lattnerdecc2672010-04-07 05:20:54 +00006466 unsigned CurOp = InlineAsm::Op_FirstOperand;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006467 for (; OperandNo; --OperandNo) {
6468 // Advance to the next operand.
Evan Cheng697cbbf2009-03-20 18:03:34 +00006469 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006470 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00006471 assert((InlineAsm::isRegDefKind(OpFlag) ||
6472 InlineAsm::isRegDefEarlyClobberKind(OpFlag) ||
6473 InlineAsm::isMemKind(OpFlag)) && "Skipped past definitions?");
Evan Cheng697cbbf2009-03-20 18:03:34 +00006474 CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006475 }
6476
Evan Cheng697cbbf2009-03-20 18:03:34 +00006477 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006478 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00006479 if (InlineAsm::isRegDefKind(OpFlag) ||
6480 InlineAsm::isRegDefEarlyClobberKind(OpFlag)) {
Evan Cheng697cbbf2009-03-20 18:03:34 +00006481 // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
Chris Lattner6129c372010-04-08 00:09:16 +00006482 if (OpInfo.isIndirect) {
6483 // This happens on gcc/testsuite/gcc.dg/pr8788-1.c
Dan Gohman99be8ae2010-04-19 22:41:47 +00006484 LLVMContext &Ctx = *DAG.getContext();
Eric Christopher1a54c572013-07-31 01:26:24 +00006485 Ctx.emitError(CS.getInstruction(), "inline asm not supported yet:"
6486 " don't know how to handle tied "
6487 "indirect register inputs");
6488 return;
Chris Lattner6129c372010-04-08 00:09:16 +00006489 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00006490
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006491 RegsForValue MatchedRegs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006492 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00006493 MVT RegVT = AsmNodeOperands[CurOp+1].getSimpleValueType();
Evan Chengfb112882009-03-23 08:01:15 +00006494 MatchedRegs.RegVTs.push_back(RegVT);
6495 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
Evan Cheng697cbbf2009-03-20 18:03:34 +00006496 for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag);
Chad Rosier2871ba92013-04-24 22:53:10 +00006497 i != e; ++i) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00006498 if (const TargetRegisterClass *RC = TLI->getRegClassFor(RegVT))
Chad Rosier2871ba92013-04-24 22:53:10 +00006499 MatchedRegs.Regs.push_back(RegInfo.createVirtualRegister(RC));
6500 else {
6501 LLVMContext &Ctx = *DAG.getContext();
Eric Christopher1a54c572013-07-31 01:26:24 +00006502 Ctx.emitError(CS.getInstruction(),
6503 "inline asm error: This value"
Chad Rosier2871ba92013-04-24 22:53:10 +00006504 " type register class is not natively supported!");
Eric Christopher1a54c572013-07-31 01:26:24 +00006505 return;
Chad Rosier2871ba92013-04-24 22:53:10 +00006506 }
6507 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006508 // Use the produced MatchedRegs object to
Andrew Trickac6d9be2013-05-25 02:42:55 +00006509 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurSDLoc(),
Bill Wendlingf18eb582012-09-26 06:16:18 +00006510 Chain, &Flag, CS.getInstruction());
Chris Lattnerdecc2672010-04-07 05:20:54 +00006511 MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse,
Evan Chengfb112882009-03-23 08:01:15 +00006512 true, OpInfo.getMatchedOperand(),
Bill Wendling46ada192010-03-02 01:55:18 +00006513 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006514 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006515 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00006516
Chris Lattnerdecc2672010-04-07 05:20:54 +00006517 assert(InlineAsm::isMemKind(OpFlag) && "Unknown matching constraint!");
6518 assert(InlineAsm::getNumOperandRegisters(OpFlag) == 1 &&
6519 "Unexpected number of operands");
6520 // Add information to the INLINEASM node to know about this input.
6521 // See InlineAsm.h isUseOperandTiedToDef.
6522 OpFlag = InlineAsm::getFlagWordForMatchingOp(OpFlag,
6523 OpInfo.getMatchedOperand());
6524 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag,
Bill Wendlingba54bca2013-06-19 21:36:55 +00006525 TLI->getPointerTy()));
Chris Lattnerdecc2672010-04-07 05:20:54 +00006526 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
6527 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006528 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006529
Dale Johannesenb5611a62010-07-13 20:17:05 +00006530 // Treat indirect 'X' constraint as memory.
Michael J. Spencere70c5262010-10-16 08:25:21 +00006531 if (OpInfo.ConstraintType == TargetLowering::C_Other &&
6532 OpInfo.isIndirect)
Dale Johannesenb5611a62010-07-13 20:17:05 +00006533 OpInfo.ConstraintType = TargetLowering::C_Memory;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006534
Dale Johannesenb5611a62010-07-13 20:17:05 +00006535 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006536 std::vector<SDValue> Ops;
Bill Wendlingba54bca2013-06-19 21:36:55 +00006537 TLI->LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode,
6538 Ops, DAG);
Chris Lattnerfcd70902012-01-03 23:51:01 +00006539 if (Ops.empty()) {
6540 LLVMContext &Ctx = *DAG.getContext();
6541 Ctx.emitError(CS.getInstruction(),
6542 "invalid operand for inline asm constraint '" +
Eric Christopher1a54c572013-07-31 01:26:24 +00006543 Twine(OpInfo.ConstraintCode) + "'");
6544 return;
Chris Lattnerfcd70902012-01-03 23:51:01 +00006545 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006546
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006547 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00006548 unsigned ResOpType =
6549 InlineAsm::getFlagWord(InlineAsm::Kind_Imm, Ops.size());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006550 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Bill Wendlingba54bca2013-06-19 21:36:55 +00006551 TLI->getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006552 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
6553 break;
Chris Lattnerdecc2672010-04-07 05:20:54 +00006554 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00006555
Chris Lattnerdecc2672010-04-07 05:20:54 +00006556 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006557 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
Bill Wendlingba54bca2013-06-19 21:36:55 +00006558 assert(InOperandVal.getValueType() == TLI->getPointerTy() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006559 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006560
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006561 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00006562 unsigned ResOpType = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
Dale Johannesen86b49f82008-09-24 01:07:17 +00006563 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Bill Wendlingba54bca2013-06-19 21:36:55 +00006564 TLI->getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006565 AsmNodeOperands.push_back(InOperandVal);
6566 break;
6567 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006568
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006569 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
6570 OpInfo.ConstraintType == TargetLowering::C_Register) &&
6571 "Unknown constraint type!");
Eric Christopher9eb4f8a2012-07-02 21:16:43 +00006572
6573 // TODO: Support this.
6574 if (OpInfo.isIndirect) {
6575 LLVMContext &Ctx = *DAG.getContext();
6576 Ctx.emitError(CS.getInstruction(),
6577 "Don't know how to handle indirect register inputs yet "
Eric Christopher1a54c572013-07-31 01:26:24 +00006578 "for constraint '" +
6579 Twine(OpInfo.ConstraintCode) + "'");
6580 return;
Eric Christopher9eb4f8a2012-07-02 21:16:43 +00006581 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006582
6583 // Copy the input into the appropriate registers.
Chris Lattnerfcd70902012-01-03 23:51:01 +00006584 if (OpInfo.AssignedRegs.Regs.empty()) {
6585 LLVMContext &Ctx = *DAG.getContext();
Stephen Lin155615d2013-07-08 00:37:03 +00006586 Ctx.emitError(CS.getInstruction(),
Chris Lattnerfcd70902012-01-03 23:51:01 +00006587 "couldn't allocate input reg for constraint '" +
Eric Christopher1a54c572013-07-31 01:26:24 +00006588 Twine(OpInfo.ConstraintCode) + "'");
6589 return;
Chris Lattnerfcd70902012-01-03 23:51:01 +00006590 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006591
Andrew Trickac6d9be2013-05-25 02:42:55 +00006592 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurSDLoc(),
Bill Wendlingf18eb582012-09-26 06:16:18 +00006593 Chain, &Flag, CS.getInstruction());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006594
Chris Lattnerdecc2672010-04-07 05:20:54 +00006595 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse, false, 0,
Bill Wendling46ada192010-03-02 01:55:18 +00006596 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006597 break;
6598 }
6599 case InlineAsm::isClobber: {
6600 // Add the clobbered value to the operand list, so that the register
6601 // allocator is aware that the physreg got clobbered.
6602 if (!OpInfo.AssignedRegs.Regs.empty())
Jakob Stoklund Olesenf792fa92011-06-27 04:08:33 +00006603 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_Clobber,
Bill Wendling46ada192010-03-02 01:55:18 +00006604 false, 0, DAG,
Bill Wendling651ad132009-12-22 01:25:10 +00006605 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006606 break;
6607 }
6608 }
6609 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006610
Chris Lattnerdecc2672010-04-07 05:20:54 +00006611 // Finish up input operands. Set the input chain and add the flag last.
Dale Johannesenf1e309e2010-07-02 20:16:09 +00006612 AsmNodeOperands[InlineAsm::Op_InputChain] = Chain;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006613 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006614
Andrew Trickac6d9be2013-05-25 02:42:55 +00006615 Chain = DAG.getNode(ISD::INLINEASM, getCurSDLoc(),
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006616 DAG.getVTList(MVT::Other, MVT::Glue),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006617 &AsmNodeOperands[0], AsmNodeOperands.size());
6618 Flag = Chain.getValue(1);
6619
6620 // If this asm returns a register value, copy the result from that register
6621 // and set it as the value of the call.
6622 if (!RetValRegs.Regs.empty()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006623 SDValue Val = RetValRegs.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(),
Bill Wendling12931302012-09-26 04:04:19 +00006624 Chain, &Flag, CS.getInstruction());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006625
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006626 // FIXME: Why don't we do this for inline asms with MRVs?
6627 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00006628 EVT ResultType = TLI->getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006629
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006630 // If any of the results of the inline asm is a vector, it may have the
6631 // wrong width/num elts. This can happen for register classes that can
6632 // contain multiple different value types. The preg or vreg allocated may
6633 // not have the same VT as was expected. Convert it to the right type
6634 // with bit_convert.
6635 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006636 Val = DAG.getNode(ISD::BITCAST, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00006637 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00006638
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006639 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006640 ResultType.isInteger() && Val.getValueType().isInteger()) {
6641 // If a result value was tied to an input value, the computed result may
6642 // have a wider width than the expected result. Extract the relevant
6643 // portion.
Andrew Trickac6d9be2013-05-25 02:42:55 +00006644 Val = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00006645 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006646
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006647 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Chris Lattner0c526442008-10-17 17:52:49 +00006648 }
Dan Gohman95915732008-10-18 01:03:45 +00006649
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006650 setValue(CS.getInstruction(), Val);
Dale Johannesenec65a7d2009-04-14 00:56:56 +00006651 // Don't need to use this as a chain in this case.
6652 if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty())
6653 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006654 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006655
Dan Gohman46510a72010-04-15 01:51:59 +00006656 std::vector<std::pair<SDValue, const Value *> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006657
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006658 // Process indirect outputs, first output all of the flagged copies out of
6659 // physregs.
6660 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
6661 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
Dan Gohman46510a72010-04-15 01:51:59 +00006662 const Value *Ptr = IndirectStoresToEmit[i].second;
Andrew Trickac6d9be2013-05-25 02:42:55 +00006663 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(),
Bill Wendling12931302012-09-26 04:04:19 +00006664 Chain, &Flag, IA);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006665 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
6666 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006667
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006668 // Emit the non-flagged stores from the physregs.
6669 SmallVector<SDValue, 8> OutChains;
Bill Wendling651ad132009-12-22 01:25:10 +00006670 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006671 SDValue Val = DAG.getStore(Chain, getCurSDLoc(),
Bill Wendling651ad132009-12-22 01:25:10 +00006672 StoresToEmit[i].first,
6673 getValue(StoresToEmit[i].second),
Chris Lattner84bd98a2010-09-21 18:58:22 +00006674 MachinePointerInfo(StoresToEmit[i].second),
David Greene1e559442010-02-15 17:00:31 +00006675 false, false, 0);
Bill Wendling651ad132009-12-22 01:25:10 +00006676 OutChains.push_back(Val);
Bill Wendling651ad132009-12-22 01:25:10 +00006677 }
6678
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006679 if (!OutChains.empty())
Andrew Trickac6d9be2013-05-25 02:42:55 +00006680 Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006681 &OutChains[0], OutChains.size());
Bill Wendling651ad132009-12-22 01:25:10 +00006682
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006683 DAG.setRoot(Chain);
6684}
6685
Dan Gohman46510a72010-04-15 01:51:59 +00006686void SelectionDAGBuilder::visitVAStart(const CallInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006687 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurSDLoc(),
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006688 MVT::Other, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00006689 getValue(I.getArgOperand(0)),
6690 DAG.getSrcValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006691}
6692
Dan Gohman46510a72010-04-15 01:51:59 +00006693void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00006694 const TargetLowering *TLI = TM.getTargetLowering();
6695 const DataLayout &TD = *TLI->getDataLayout();
6696 SDValue V = DAG.getVAArg(TLI->getValueType(I.getType()), getCurSDLoc(),
Dale Johannesena04b7572009-02-03 23:04:43 +00006697 getRoot(), getValue(I.getOperand(0)),
Rafael Espindolacbeeae22010-07-11 04:01:49 +00006698 DAG.getSrcValue(I.getOperand(0)),
Rafael Espindola9d544d02010-07-12 18:11:17 +00006699 TD.getABITypeAlignment(I.getType()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006700 setValue(&I, V);
6701 DAG.setRoot(V.getValue(1));
6702}
6703
Dan Gohman46510a72010-04-15 01:51:59 +00006704void SelectionDAGBuilder::visitVAEnd(const CallInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006705 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurSDLoc(),
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006706 MVT::Other, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00006707 getValue(I.getArgOperand(0)),
6708 DAG.getSrcValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006709}
6710
Dan Gohman46510a72010-04-15 01:51:59 +00006711void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006712 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurSDLoc(),
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006713 MVT::Other, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00006714 getValue(I.getArgOperand(0)),
6715 getValue(I.getArgOperand(1)),
6716 DAG.getSrcValue(I.getArgOperand(0)),
6717 DAG.getSrcValue(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006718}
6719
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006720/// TargetLowering::LowerCallTo - This is the default LowerCallTo
Dan Gohman98ca4f22009-08-05 01:29:28 +00006721/// implementation, which just calls LowerCall.
6722/// FIXME: When all targets are
6723/// migrated to using LowerCall, this hook should be integrated into SDISel.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006724std::pair<SDValue, SDValue>
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006725TargetLowering::LowerCallTo(TargetLowering::CallLoweringInfo &CLI) const {
Stephen Lin3484da92013-04-30 22:49:28 +00006726 // Handle the incoming return values from the call.
6727 CLI.Ins.clear();
6728 SmallVector<EVT, 4> RetTys;
6729 ComputeValueVTs(*this, CLI.RetTy, RetTys);
6730 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
6731 EVT VT = RetTys[I];
6732 MVT RegisterVT = getRegisterType(CLI.RetTy->getContext(), VT);
6733 unsigned NumRegs = getNumRegisters(CLI.RetTy->getContext(), VT);
6734 for (unsigned i = 0; i != NumRegs; ++i) {
6735 ISD::InputArg MyFlags;
6736 MyFlags.VT = RegisterVT;
6737 MyFlags.Used = CLI.IsReturnValueUsed;
6738 if (CLI.RetSExt)
6739 MyFlags.Flags.setSExt();
6740 if (CLI.RetZExt)
6741 MyFlags.Flags.setZExt();
6742 if (CLI.IsInReg)
6743 MyFlags.Flags.setInReg();
6744 CLI.Ins.push_back(MyFlags);
6745 }
6746 }
6747
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006748 // Handle all of the outgoing arguments.
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006749 CLI.Outs.clear();
6750 CLI.OutVals.clear();
6751 ArgListTy &Args = CLI.Args;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006752 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00006753 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006754 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
6755 for (unsigned Value = 0, NumValues = ValueVTs.size();
6756 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006757 EVT VT = ValueVTs[Value];
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006758 Type *ArgTy = VT.getTypeForEVT(CLI.RetTy->getContext());
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006759 SDValue Op = SDValue(Args[i].Node.getNode(),
6760 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006761 ISD::ArgFlagsTy Flags;
6762 unsigned OriginalAlignment =
Micah Villmow3574eca2012-10-08 16:38:25 +00006763 getDataLayout()->getABITypeAlignment(ArgTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006764
6765 if (Args[i].isZExt)
6766 Flags.setZExt();
6767 if (Args[i].isSExt)
6768 Flags.setSExt();
6769 if (Args[i].isInReg)
6770 Flags.setInReg();
6771 if (Args[i].isSRet)
6772 Flags.setSRet();
6773 if (Args[i].isByVal) {
6774 Flags.setByVal();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006775 PointerType *Ty = cast<PointerType>(Args[i].Ty);
6776 Type *ElementTy = Ty->getElementType();
Micah Villmow3574eca2012-10-08 16:38:25 +00006777 Flags.setByValSize(getDataLayout()->getTypeAllocSize(ElementTy));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006778 // For ByVal, alignment should come from FE. BE will guess if this
6779 // info is not there but there are cases it cannot get right.
Chris Lattner9db20f32011-05-22 23:23:02 +00006780 unsigned FrameAlign;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006781 if (Args[i].Alignment)
6782 FrameAlign = Args[i].Alignment;
Chris Lattner9db20f32011-05-22 23:23:02 +00006783 else
6784 FrameAlign = getByValTypeAlignment(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006785 Flags.setByValAlign(FrameAlign);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006786 }
6787 if (Args[i].isNest)
6788 Flags.setNest();
6789 Flags.setOrigAlign(OriginalAlignment);
6790
Patrik Hagglunddfcf33a2012-12-19 11:48:16 +00006791 MVT PartVT = getRegisterType(CLI.RetTy->getContext(), VT);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006792 unsigned NumParts = getNumRegisters(CLI.RetTy->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006793 SmallVector<SDValue, 4> Parts(NumParts);
6794 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
6795
6796 if (Args[i].isSExt)
6797 ExtendKind = ISD::SIGN_EXTEND;
6798 else if (Args[i].isZExt)
6799 ExtendKind = ISD::ZERO_EXTEND;
6800
Stephen Lin3484da92013-04-30 22:49:28 +00006801 // Conservatively only handle 'returned' on non-vectors for now
6802 if (Args[i].isReturned && !Op.getValueType().isVector()) {
6803 assert(CLI.RetTy == Args[i].Ty && RetTys.size() == NumValues &&
6804 "unexpected use of 'returned'");
6805 // Before passing 'returned' to the target lowering code, ensure that
6806 // either the register MVT and the actual EVT are the same size or that
6807 // the return value and argument are extended in the same way; in these
6808 // cases it's safe to pass the argument register value unchanged as the
6809 // return register value (although it's at the target's option whether
6810 // to do so)
6811 // TODO: allow code generation to take advantage of partially preserved
6812 // registers rather than clobbering the entire register when the
6813 // parameter extension method is not compatible with the return
6814 // extension method
6815 if ((NumParts * PartVT.getSizeInBits() == VT.getSizeInBits()) ||
6816 (ExtendKind != ISD::ANY_EXTEND &&
6817 CLI.RetSExt == Args[i].isSExt && CLI.RetZExt == Args[i].isZExt))
6818 Flags.setReturned();
6819 }
6820
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006821 getCopyToParts(CLI.DAG, CLI.DL, Op, &Parts[0], NumParts,
Bill Wendlingf18eb582012-09-26 06:16:18 +00006822 PartVT, CLI.CS ? CLI.CS->getInstruction() : 0, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006823
Dan Gohman98ca4f22009-08-05 01:29:28 +00006824 for (unsigned j = 0; j != NumParts; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006825 // if it isn't first piece, alignment must be 1
Dan Gohmanc9403652010-07-07 15:54:55 +00006826 ISD::OutputArg MyFlags(Flags, Parts[j].getValueType(),
Manman Ren0a1544d2012-11-01 23:49:58 +00006827 i < CLI.NumFixedArgs,
6828 i, j*Parts[j].getValueType().getStoreSize());
Dan Gohman98ca4f22009-08-05 01:29:28 +00006829 if (NumParts > 1 && j == 0)
6830 MyFlags.Flags.setSplit();
6831 else if (j != 0)
6832 MyFlags.Flags.setOrigAlign(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006833
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006834 CLI.Outs.push_back(MyFlags);
6835 CLI.OutVals.push_back(Parts[j]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006836 }
6837 }
6838 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006839
Dan Gohman98ca4f22009-08-05 01:29:28 +00006840 SmallVector<SDValue, 4> InVals;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006841 CLI.Chain = LowerCall(CLI, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00006842
6843 // Verify that the target's LowerCall behaved as expected.
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006844 assert(CLI.Chain.getNode() && CLI.Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00006845 "LowerCall didn't return a valid chain!");
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006846 assert((!CLI.IsTailCall || InVals.empty()) &&
Dan Gohman5e866062009-08-06 15:37:27 +00006847 "LowerCall emitted a return value for a tail call!");
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006848 assert((CLI.IsTailCall || InVals.size() == CLI.Ins.size()) &&
Dan Gohman5e866062009-08-06 15:37:27 +00006849 "LowerCall didn't emit the correct number of values!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00006850
6851 // For a tail call, the return value is merely live-out and there aren't
6852 // any nodes in the DAG representing it. Return a special value to
6853 // indicate that a tail call has been emitted and no more Instructions
6854 // should be processed in the current block.
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006855 if (CLI.IsTailCall) {
6856 CLI.DAG.setRoot(CLI.Chain);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006857 return std::make_pair(SDValue(), SDValue());
6858 }
6859
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006860 DEBUG(for (unsigned i = 0, e = CLI.Ins.size(); i != e; ++i) {
Evan Chengaf1871f2010-03-11 19:38:18 +00006861 assert(InVals[i].getNode() &&
6862 "LowerCall emitted a null value!");
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006863 assert(EVT(CLI.Ins[i].VT) == InVals[i].getValueType() &&
Evan Chengaf1871f2010-03-11 19:38:18 +00006864 "LowerCall emitted a value with the wrong type!");
6865 });
6866
Dan Gohman98ca4f22009-08-05 01:29:28 +00006867 // Collect the legal value parts into potentially illegal values
6868 // that correspond to the original function's return values.
6869 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006870 if (CLI.RetSExt)
Dan Gohman98ca4f22009-08-05 01:29:28 +00006871 AssertOp = ISD::AssertSext;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006872 else if (CLI.RetZExt)
Dan Gohman98ca4f22009-08-05 01:29:28 +00006873 AssertOp = ISD::AssertZext;
6874 SmallVector<SDValue, 4> ReturnValues;
6875 unsigned CurReg = 0;
6876 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00006877 EVT VT = RetTys[I];
Patrik Hagglunddfcf33a2012-12-19 11:48:16 +00006878 MVT RegisterVT = getRegisterType(CLI.RetTy->getContext(), VT);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006879 unsigned NumRegs = getNumRegisters(CLI.RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006880
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006881 ReturnValues.push_back(getCopyFromParts(CLI.DAG, CLI.DL, &InVals[CurReg],
Bill Wendling12931302012-09-26 04:04:19 +00006882 NumRegs, RegisterVT, VT, NULL,
Bill Wendling4533cac2010-01-28 21:51:40 +00006883 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00006884 CurReg += NumRegs;
6885 }
6886
6887 // For a function returning void, there is no return value. We can't create
6888 // such a node, so we just return a null return value in that case. In
Chris Lattner7a2bdde2011-04-15 05:18:47 +00006889 // that case, nothing will actually look at the value.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006890 if (ReturnValues.empty())
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006891 return std::make_pair(SDValue(), CLI.Chain);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006892
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006893 SDValue Res = CLI.DAG.getNode(ISD::MERGE_VALUES, CLI.DL,
6894 CLI.DAG.getVTList(&RetTys[0], RetTys.size()),
Dan Gohman98ca4f22009-08-05 01:29:28 +00006895 &ReturnValues[0], ReturnValues.size());
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006896 return std::make_pair(Res, CLI.Chain);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006897}
6898
Duncan Sands9fbc7e22009-01-21 09:00:29 +00006899void TargetLowering::LowerOperationWrapper(SDNode *N,
6900 SmallVectorImpl<SDValue> &Results,
Dan Gohmand858e902010-04-17 15:26:15 +00006901 SelectionDAG &DAG) const {
Duncan Sands9fbc7e22009-01-21 09:00:29 +00006902 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00006903 if (Res.getNode())
6904 Results.push_back(Res);
6905}
6906
Dan Gohmand858e902010-04-17 15:26:15 +00006907SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Torok Edwinc23197a2009-07-14 16:55:14 +00006908 llvm_unreachable("LowerOperation not implemented for this target!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006909}
6910
Dan Gohman46510a72010-04-15 01:51:59 +00006911void
6912SelectionDAGBuilder::CopyValueToVirtualRegister(const Value *V, unsigned Reg) {
Dan Gohman28a17352010-07-01 01:59:43 +00006913 SDValue Op = getNonRegisterValue(V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006914 assert((Op.getOpcode() != ISD::CopyFromReg ||
6915 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
6916 "Copy from a reg to the same reg!");
6917 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
6918
Bill Wendlingba54bca2013-06-19 21:36:55 +00006919 const TargetLowering *TLI = TM.getTargetLowering();
6920 RegsForValue RFV(V->getContext(), *TLI, Reg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006921 SDValue Chain = DAG.getEntryNode();
Andrew Trickac6d9be2013-05-25 02:42:55 +00006922 RFV.getCopyToRegs(Op, DAG, getCurSDLoc(), Chain, 0, V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006923 PendingExports.push_back(Chain);
6924}
6925
6926#include "llvm/CodeGen/SelectionDAGISel.h"
6927
Eli Friedman23d32432011-05-05 16:53:34 +00006928/// isOnlyUsedInEntryBlock - If the specified argument is only used in the
6929/// entry block, return true. This includes arguments used by switches, since
6930/// the switch may expand into multiple basic blocks.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006931static bool isOnlyUsedInEntryBlock(const Argument *A, bool FastISel) {
Eli Friedman23d32432011-05-05 16:53:34 +00006932 // With FastISel active, we may be splitting blocks, so force creation
6933 // of virtual registers for all non-dead arguments.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006934 if (FastISel)
Eli Friedman23d32432011-05-05 16:53:34 +00006935 return A->use_empty();
6936
6937 const BasicBlock *Entry = A->getParent()->begin();
6938 for (Value::const_use_iterator UI = A->use_begin(), E = A->use_end();
6939 UI != E; ++UI) {
6940 const User *U = *UI;
6941 if (cast<Instruction>(U)->getParent() != Entry || isa<SwitchInst>(U))
6942 return false; // Use not in entry block.
6943 }
6944 return true;
6945}
6946
Eli Bendersky6437d382013-02-28 23:09:18 +00006947void SelectionDAGISel::LowerArguments(const Function &F) {
Dan Gohman2048b852009-11-23 18:04:58 +00006948 SelectionDAG &DAG = SDB->DAG;
Andrew Trickac6d9be2013-05-25 02:42:55 +00006949 SDLoc dl = SDB->getCurSDLoc();
Bill Wendlingba54bca2013-06-19 21:36:55 +00006950 const TargetLowering *TLI = getTargetLowering();
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006951 const DataLayout *TD = TLI->getDataLayout();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006952 SmallVector<ISD::InputArg, 16> Ins;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006953
Dan Gohman7451d3e2010-05-29 17:03:36 +00006954 if (!FuncInfo->CanLowerReturn) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006955 // Put in an sret pointer parameter before all the other parameters.
6956 SmallVector<EVT, 1> ValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00006957 ComputeValueVTs(*getTargetLowering(),
6958 PointerType::getUnqual(F.getReturnType()), ValueVTs);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006959
6960 // NOTE: Assuming that a pointer will never break down to more than one VT
6961 // or one register.
6962 ISD::ArgFlagsTy Flags;
6963 Flags.setSRet();
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006964 MVT RegisterVT = TLI->getRegisterType(*DAG.getContext(), ValueVTs[0]);
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00006965 ISD::InputArg RetArg(Flags, RegisterVT, true, 0, 0);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006966 Ins.push_back(RetArg);
6967 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00006968
Dan Gohman98ca4f22009-08-05 01:29:28 +00006969 // Set up the incoming argument description vector.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006970 unsigned Idx = 1;
Dan Gohman46510a72010-04-15 01:51:59 +00006971 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
Dan Gohman98ca4f22009-08-05 01:29:28 +00006972 I != E; ++I, ++Idx) {
Owen Andersone50ed302009-08-10 22:56:29 +00006973 SmallVector<EVT, 4> ValueVTs;
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006974 ComputeValueVTs(*TLI, I->getType(), ValueVTs);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006975 bool isArgValueUsed = !I->use_empty();
6976 for (unsigned Value = 0, NumValues = ValueVTs.size();
6977 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006978 EVT VT = ValueVTs[Value];
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006979 Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00006980 ISD::ArgFlagsTy Flags;
6981 unsigned OriginalAlignment =
6982 TD->getABITypeAlignment(ArgTy);
6983
Bill Wendling39cd0c82012-12-30 12:45:13 +00006984 if (F.getAttributes().hasAttribute(Idx, Attribute::ZExt))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006985 Flags.setZExt();
Bill Wendling39cd0c82012-12-30 12:45:13 +00006986 if (F.getAttributes().hasAttribute(Idx, Attribute::SExt))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006987 Flags.setSExt();
Bill Wendling39cd0c82012-12-30 12:45:13 +00006988 if (F.getAttributes().hasAttribute(Idx, Attribute::InReg))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006989 Flags.setInReg();
Bill Wendling39cd0c82012-12-30 12:45:13 +00006990 if (F.getAttributes().hasAttribute(Idx, Attribute::StructRet))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006991 Flags.setSRet();
Bill Wendling39cd0c82012-12-30 12:45:13 +00006992 if (F.getAttributes().hasAttribute(Idx, Attribute::ByVal)) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00006993 Flags.setByVal();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006994 PointerType *Ty = cast<PointerType>(I->getType());
6995 Type *ElementTy = Ty->getElementType();
Chris Lattner9db20f32011-05-22 23:23:02 +00006996 Flags.setByValSize(TD->getTypeAllocSize(ElementTy));
Dan Gohman98ca4f22009-08-05 01:29:28 +00006997 // For ByVal, alignment should be passed from FE. BE will guess if
6998 // this info is not there but there are cases it cannot get right.
Chris Lattner9db20f32011-05-22 23:23:02 +00006999 unsigned FrameAlign;
Dan Gohman98ca4f22009-08-05 01:29:28 +00007000 if (F.getParamAlignment(Idx))
7001 FrameAlign = F.getParamAlignment(Idx);
Chris Lattner9db20f32011-05-22 23:23:02 +00007002 else
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00007003 FrameAlign = TLI->getByValTypeAlignment(ElementTy);
Dan Gohman98ca4f22009-08-05 01:29:28 +00007004 Flags.setByValAlign(FrameAlign);
Dan Gohman98ca4f22009-08-05 01:29:28 +00007005 }
Bill Wendling39cd0c82012-12-30 12:45:13 +00007006 if (F.getAttributes().hasAttribute(Idx, Attribute::Nest))
Dan Gohman98ca4f22009-08-05 01:29:28 +00007007 Flags.setNest();
7008 Flags.setOrigAlign(OriginalAlignment);
7009
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00007010 MVT RegisterVT = TLI->getRegisterType(*CurDAG->getContext(), VT);
7011 unsigned NumRegs = TLI->getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00007012 for (unsigned i = 0; i != NumRegs; ++i) {
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00007013 ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed,
7014 Idx-1, i*RegisterVT.getStoreSize());
Dan Gohman98ca4f22009-08-05 01:29:28 +00007015 if (NumRegs > 1 && i == 0)
7016 MyFlags.Flags.setSplit();
7017 // if it isn't first piece, alignment must be 1
7018 else if (i > 0)
7019 MyFlags.Flags.setOrigAlign(1);
7020 Ins.push_back(MyFlags);
7021 }
7022 }
7023 }
7024
7025 // Call the target to set up the argument values.
7026 SmallVector<SDValue, 8> InVals;
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00007027 SDValue NewRoot = TLI->LowerFormalArguments(DAG.getRoot(), F.getCallingConv(),
7028 F.isVarArg(), Ins,
7029 dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00007030
7031 // Verify that the target's LowerFormalArguments behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00007032 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00007033 "LowerFormalArguments didn't return a valid chain!");
7034 assert(InVals.size() == Ins.size() &&
7035 "LowerFormalArguments didn't emit the correct number of values!");
Bill Wendling3ea58b62009-12-22 21:35:02 +00007036 DEBUG({
7037 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
7038 assert(InVals[i].getNode() &&
7039 "LowerFormalArguments emitted a null value!");
Duncan Sands1440e8b2010-11-03 11:35:31 +00007040 assert(EVT(Ins[i].VT) == InVals[i].getValueType() &&
Bill Wendling3ea58b62009-12-22 21:35:02 +00007041 "LowerFormalArguments emitted a value with the wrong type!");
7042 }
7043 });
Bill Wendling3ea3c242009-12-22 02:10:19 +00007044
Dan Gohman5e866062009-08-06 15:37:27 +00007045 // Update the DAG with the new chain value resulting from argument lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00007046 DAG.setRoot(NewRoot);
7047
7048 // Set up the argument values.
7049 unsigned i = 0;
7050 Idx = 1;
Dan Gohman7451d3e2010-05-29 17:03:36 +00007051 if (!FuncInfo->CanLowerReturn) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00007052 // Create a virtual register for the sret pointer, and put in a copy
7053 // from the sret argument into it.
7054 SmallVector<EVT, 1> ValueVTs;
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00007055 ComputeValueVTs(*TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00007056 MVT VT = ValueVTs[0].getSimpleVT();
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00007057 MVT RegVT = TLI->getRegisterType(*CurDAG->getContext(), VT);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00007058 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Bill Wendling46ada192010-03-02 01:55:18 +00007059 SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1,
Bill Wendling12931302012-09-26 04:04:19 +00007060 RegVT, VT, NULL, AssertOp);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00007061
Dan Gohman2048b852009-11-23 18:04:58 +00007062 MachineFunction& MF = SDB->DAG.getMachineFunction();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00007063 MachineRegisterInfo& RegInfo = MF.getRegInfo();
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00007064 unsigned SRetReg = RegInfo.createVirtualRegister(TLI->getRegClassFor(RegVT));
Dan Gohman7451d3e2010-05-29 17:03:36 +00007065 FuncInfo->DemoteRegister = SRetReg;
Andrew Trickac6d9be2013-05-25 02:42:55 +00007066 NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurSDLoc(),
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00007067 SRetReg, ArgValue);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00007068 DAG.setRoot(NewRoot);
Bill Wendling3ea3c242009-12-22 02:10:19 +00007069
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00007070 // i indexes lowered arguments. Bump it past the hidden sret argument.
7071 // Idx indexes LLVM arguments. Don't touch it.
7072 ++i;
7073 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00007074
Dan Gohman46510a72010-04-15 01:51:59 +00007075 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
Dan Gohman98ca4f22009-08-05 01:29:28 +00007076 ++I, ++Idx) {
7077 SmallVector<SDValue, 4> ArgValues;
Owen Andersone50ed302009-08-10 22:56:29 +00007078 SmallVector<EVT, 4> ValueVTs;
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00007079 ComputeValueVTs(*TLI, I->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007080 unsigned NumValues = ValueVTs.size();
Devang Patel9126c0d2010-06-01 19:59:01 +00007081
7082 // If this argument is unused then remember its value. It is used to generate
7083 // debugging information.
Adrian Prantldf688032013-05-16 23:44:12 +00007084 if (I->use_empty() && NumValues) {
Devang Patel9126c0d2010-06-01 19:59:01 +00007085 SDB->setUnusedArgValue(I, InVals[i]);
7086
Adrian Prantldf688032013-05-16 23:44:12 +00007087 // Also remember any frame index for use in FastISel.
7088 if (FrameIndexSDNode *FI =
7089 dyn_cast<FrameIndexSDNode>(InVals[i].getNode()))
7090 FuncInfo->setArgumentFrameIndex(I, FI->getIndex());
7091 }
7092
Eli Friedman23d32432011-05-05 16:53:34 +00007093 for (unsigned Val = 0; Val != NumValues; ++Val) {
7094 EVT VT = ValueVTs[Val];
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00007095 MVT PartVT = TLI->getRegisterType(*CurDAG->getContext(), VT);
7096 unsigned NumParts = TLI->getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00007097
7098 if (!I->use_empty()) {
7099 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Bill Wendling39cd0c82012-12-30 12:45:13 +00007100 if (F.getAttributes().hasAttribute(Idx, Attribute::SExt))
Dan Gohman98ca4f22009-08-05 01:29:28 +00007101 AssertOp = ISD::AssertSext;
Bill Wendling39cd0c82012-12-30 12:45:13 +00007102 else if (F.getAttributes().hasAttribute(Idx, Attribute::ZExt))
Dan Gohman98ca4f22009-08-05 01:29:28 +00007103 AssertOp = ISD::AssertZext;
7104
Bill Wendling46ada192010-03-02 01:55:18 +00007105 ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i],
Bill Wendling3ea3c242009-12-22 02:10:19 +00007106 NumParts, PartVT, VT,
Bill Wendling12931302012-09-26 04:04:19 +00007107 NULL, AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00007108 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00007109
Dan Gohman98ca4f22009-08-05 01:29:28 +00007110 i += NumParts;
7111 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00007112
Eli Friedman23d32432011-05-05 16:53:34 +00007113 // We don't need to do anything else for unused arguments.
7114 if (ArgValues.empty())
7115 continue;
7116
Devang Patel9aee3352011-09-08 22:59:09 +00007117 // Note down frame index.
7118 if (FrameIndexSDNode *FI =
Bill Wendling96cb1122012-07-19 00:04:14 +00007119 dyn_cast<FrameIndexSDNode>(ArgValues[0].getNode()))
Devang Patel9aee3352011-09-08 22:59:09 +00007120 FuncInfo->setArgumentFrameIndex(I, FI->getIndex());
Devang Patel0b48ead2010-08-31 22:22:42 +00007121
Eli Friedman23d32432011-05-05 16:53:34 +00007122 SDValue Res = DAG.getMergeValues(&ArgValues[0], NumValues,
Andrew Trickac6d9be2013-05-25 02:42:55 +00007123 SDB->getCurSDLoc());
Devang Patel9aee3352011-09-08 22:59:09 +00007124
Eli Friedman23d32432011-05-05 16:53:34 +00007125 SDB->setValue(I, Res);
Nick Lewycky8a8d4792011-12-02 22:16:29 +00007126 if (!TM.Options.EnableFastISel && Res.getOpcode() == ISD::BUILD_PAIR) {
Stephen Lin155615d2013-07-08 00:37:03 +00007127 if (LoadSDNode *LNode =
Devang Patel9aee3352011-09-08 22:59:09 +00007128 dyn_cast<LoadSDNode>(Res.getOperand(0).getNode()))
7129 if (FrameIndexSDNode *FI =
7130 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
7131 FuncInfo->setArgumentFrameIndex(I, FI->getIndex());
7132 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00007133
Eli Friedman23d32432011-05-05 16:53:34 +00007134 // If this argument is live outside of the entry block, insert a copy from
7135 // wherever we got it to the vreg that other BB's will reference it as.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00007136 if (!TM.Options.EnableFastISel && Res.getOpcode() == ISD::CopyFromReg) {
Eli Friedman23d32432011-05-05 16:53:34 +00007137 // If we can, though, try to skip creating an unnecessary vreg.
7138 // FIXME: This isn't very clean... it would be nice to make this more
Eli Friedman7f33d672011-05-10 21:50:58 +00007139 // general. It's also subtly incompatible with the hacks FastISel
7140 // uses with vregs.
Eli Friedman23d32432011-05-05 16:53:34 +00007141 unsigned Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg();
7142 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
7143 FuncInfo->ValueMap[I] = Reg;
7144 continue;
7145 }
7146 }
Nick Lewycky8a8d4792011-12-02 22:16:29 +00007147 if (!isOnlyUsedInEntryBlock(I, TM.Options.EnableFastISel)) {
Eli Friedman23d32432011-05-05 16:53:34 +00007148 FuncInfo->InitializeRegForValue(I);
Dan Gohman2048b852009-11-23 18:04:58 +00007149 SDB->CopyToExportRegsIfNeeded(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007150 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007151 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00007152
Dan Gohman98ca4f22009-08-05 01:29:28 +00007153 assert(i == InVals.size() && "Argument register count mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007154
7155 // Finally, if the target has anything special to do, allow it to do so.
7156 // FIXME: this should insert code into the DAG!
Dan Gohman64652652010-04-14 20:17:22 +00007157 EmitFunctionEntryCode();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007158}
7159
7160/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
7161/// ensure constants are generated when needed. Remember the virtual registers
7162/// that need to be added to the Machine PHI nodes as input. We cannot just
7163/// directly add them, because expansion might result in multiple MBB's for one
7164/// BB. As such, the start of the BB might correspond to a different MBB than
7165/// the end.
7166///
7167void
Dan Gohmanf81eca02010-04-22 20:46:50 +00007168SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
Dan Gohman46510a72010-04-15 01:51:59 +00007169 const TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007170
7171 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
7172
7173 // Check successor nodes' PHI nodes that expect a constant to be available
7174 // from this block.
7175 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
Dan Gohman46510a72010-04-15 01:51:59 +00007176 const BasicBlock *SuccBB = TI->getSuccessor(succ);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007177 if (!isa<PHINode>(SuccBB->begin())) continue;
Dan Gohmanf81eca02010-04-22 20:46:50 +00007178 MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00007179
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007180 // If this terminator has multiple identical successors (common for
7181 // switches), only handle each succ once.
7182 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00007183
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007184 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007185
7186 // At this point we know that there is a 1-1 correspondence between LLVM PHI
7187 // nodes and Machine PHI nodes, but the incoming operands have not been
7188 // emitted yet.
Dan Gohman46510a72010-04-15 01:51:59 +00007189 for (BasicBlock::const_iterator I = SuccBB->begin();
7190 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007191 // Ignore dead phi's.
7192 if (PN->use_empty()) continue;
7193
Rafael Espindola3fa82832011-05-13 15:18:06 +00007194 // Skip empty types
7195 if (PN->getType()->isEmptyTy())
7196 continue;
7197
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007198 unsigned Reg;
Dan Gohman46510a72010-04-15 01:51:59 +00007199 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007200
Dan Gohman46510a72010-04-15 01:51:59 +00007201 if (const Constant *C = dyn_cast<Constant>(PHIOp)) {
Dan Gohmanf81eca02010-04-22 20:46:50 +00007202 unsigned &RegOut = ConstantsOut[C];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007203 if (RegOut == 0) {
Dan Gohman89496d02010-07-02 00:10:16 +00007204 RegOut = FuncInfo.CreateRegs(C->getType());
Dan Gohmanf81eca02010-04-22 20:46:50 +00007205 CopyValueToVirtualRegister(C, RegOut);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007206 }
7207 Reg = RegOut;
7208 } else {
Dan Gohmanc25ad632010-07-01 01:33:21 +00007209 DenseMap<const Value *, unsigned>::iterator I =
7210 FuncInfo.ValueMap.find(PHIOp);
7211 if (I != FuncInfo.ValueMap.end())
7212 Reg = I->second;
7213 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007214 assert(isa<AllocaInst>(PHIOp) &&
Dan Gohmanf81eca02010-04-22 20:46:50 +00007215 FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007216 "Didn't codegen value into a register!??");
Dan Gohman89496d02010-07-02 00:10:16 +00007217 Reg = FuncInfo.CreateRegs(PHIOp->getType());
Dan Gohmanf81eca02010-04-22 20:46:50 +00007218 CopyValueToVirtualRegister(PHIOp, Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007219 }
7220 }
7221
7222 // Remember that this register needs to added to the machine PHI node as
7223 // the input for this MBB.
Owen Andersone50ed302009-08-10 22:56:29 +00007224 SmallVector<EVT, 4> ValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00007225 const TargetLowering *TLI = TM.getTargetLowering();
7226 ComputeValueVTs(*TLI, PN->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007227 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +00007228 EVT VT = ValueVTs[vti];
Bill Wendlingba54bca2013-06-19 21:36:55 +00007229 unsigned NumRegisters = TLI->getNumRegisters(*DAG.getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007230 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
Dan Gohmanf81eca02010-04-22 20:46:50 +00007231 FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007232 Reg += NumRegisters;
7233 }
7234 }
7235 }
Bill Wendlingba54bca2013-06-19 21:36:55 +00007236
Dan Gohmanf81eca02010-04-22 20:46:50 +00007237 ConstantsOut.clear();
Dan Gohman3df24e62008-09-03 23:12:08 +00007238}
Michael Gottesman657484f2013-08-20 07:00:16 +00007239
7240/// Add a successor MBB to ParentMBB< creating a new MachineBB for BB if SuccMBB
7241/// is 0.
7242MachineBasicBlock *
7243SelectionDAGBuilder::StackProtectorDescriptor::
7244AddSuccessorMBB(const BasicBlock *BB,
7245 MachineBasicBlock *ParentMBB,
7246 MachineBasicBlock *SuccMBB) {
7247 // If SuccBB has not been created yet, create it.
7248 if (!SuccMBB) {
7249 MachineFunction *MF = ParentMBB->getParent();
7250 MachineFunction::iterator BBI = ParentMBB;
7251 SuccMBB = MF->CreateMachineBasicBlock(BB);
7252 MF->insert(++BBI, SuccMBB);
7253 }
7254 // Add it as a successor of ParentMBB.
7255 ParentMBB->addSuccessor(SuccMBB);
7256 return SuccMBB;
7257}