blob: 6feca81dad23cb168ecac36cd5aad0bf8783d953 [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(),
1812 true, false).second;
1813 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 {
3245 Ty = cast<SequentialType>(Ty)->getElementType();
3246
3247 // If this is a constant subscript, handle it quickly.
Bill Wendlingba54bca2013-06-19 21:36:55 +00003248 const TargetLowering *TLI = TM.getTargetLowering();
Dan Gohman46510a72010-04-15 01:51:59 +00003249 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Dan Gohmane368b462010-06-18 14:22:04 +00003250 if (CI->isZero()) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003251 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +00003252 TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Evan Cheng65b52df2009-02-09 21:01:06 +00003253 SDValue OffsVal;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003254 EVT PTy = TLI->getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00003255 unsigned PtrBits = PTy.getSizeInBits();
Bill Wendlinge1a90422009-12-21 23:10:19 +00003256 if (PtrBits < 64)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003257 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00003258 TLI->getPointerTy(),
Owen Anderson825b72b2009-08-11 20:47:22 +00003259 DAG.getConstant(Offs, MVT::i64));
Bill Wendlinge1a90422009-12-21 23:10:19 +00003260 else
Evan Chengb1032a82009-02-09 20:54:38 +00003261 OffsVal = DAG.getIntPtrConstant(Offs);
Bill Wendlinge1a90422009-12-21 23:10:19 +00003262
Andrew Trickac6d9be2013-05-25 02:42:55 +00003263 N = DAG.getNode(ISD::ADD, getCurSDLoc(), N.getValueType(), N,
Evan Chengb1032a82009-02-09 20:54:38 +00003264 OffsVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003265 continue;
3266 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003267
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003268 // N = N + Idx * ElementSize;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003269 APInt ElementSize = APInt(TLI->getPointerTy().getSizeInBits(),
Dan Gohman7abbd042009-10-23 17:57:43 +00003270 TD->getTypeAllocSize(Ty));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003271 SDValue IdxN = getValue(Idx);
3272
3273 // If the index is smaller or larger than intptr_t, truncate or extend
3274 // it.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003275 IdxN = DAG.getSExtOrTrunc(IdxN, getCurSDLoc(), N.getValueType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003276
3277 // If this is a multiply by a power of two, turn it into a shl
3278 // immediately. This is a very common case.
3279 if (ElementSize != 1) {
Dan Gohman7abbd042009-10-23 17:57:43 +00003280 if (ElementSize.isPowerOf2()) {
3281 unsigned Amt = ElementSize.logBase2();
Andrew Trickac6d9be2013-05-25 02:42:55 +00003282 IdxN = DAG.getNode(ISD::SHL, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003283 N.getValueType(), IdxN,
Nadav Rotem16087692011-12-05 06:29:09 +00003284 DAG.getConstant(Amt, IdxN.getValueType()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003285 } else {
Duncan Sandsb2df01a2012-11-13 13:01:58 +00003286 SDValue Scale = DAG.getConstant(ElementSize, IdxN.getValueType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003287 IdxN = DAG.getNode(ISD::MUL, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003288 N.getValueType(), IdxN, Scale);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003289 }
3290 }
3291
Andrew Trickac6d9be2013-05-25 02:42:55 +00003292 N = DAG.getNode(ISD::ADD, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003293 N.getValueType(), N, IdxN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003294 }
3295 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00003296
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003297 setValue(&I, N);
3298}
3299
Dan Gohman46510a72010-04-15 01:51:59 +00003300void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003301 // If this is a fixed sized alloca in the entry block of the function,
3302 // allocate it statically on the stack.
3303 if (FuncInfo.StaticAllocaMap.count(&I))
3304 return; // getValue will auto-populate this.
3305
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003306 Type *Ty = I.getAllocatedType();
Bill Wendlingba54bca2013-06-19 21:36:55 +00003307 const TargetLowering *TLI = TM.getTargetLowering();
3308 uint64_t TySize = TLI->getDataLayout()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003309 unsigned Align =
Bill Wendlingba54bca2013-06-19 21:36:55 +00003310 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003311 I.getAlignment());
3312
3313 SDValue AllocSize = getValue(I.getArraySize());
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003314
Bill Wendlingba54bca2013-06-19 21:36:55 +00003315 EVT IntPtr = TLI->getPointerTy();
Dan Gohmanf75a7d32010-05-28 01:14:11 +00003316 if (AllocSize.getValueType() != IntPtr)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003317 AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurSDLoc(), IntPtr);
Dan Gohmanf75a7d32010-05-28 01:14:11 +00003318
Andrew Trickac6d9be2013-05-25 02:42:55 +00003319 AllocSize = DAG.getNode(ISD::MUL, getCurSDLoc(), IntPtr,
Dan Gohmanf75a7d32010-05-28 01:14:11 +00003320 AllocSize,
3321 DAG.getConstant(TySize, IntPtr));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003322
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003323 // Handle alignment. If the requested alignment is less than or equal to
3324 // the stack alignment, ignore it. If the size is greater than or equal to
3325 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00003326 unsigned StackAlign = TM.getFrameLowering()->getStackAlignment();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003327 if (Align <= StackAlign)
3328 Align = 0;
3329
3330 // Round the size of the allocation up to the stack alignment size
3331 // by add SA-1 to the size.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003332 AllocSize = DAG.getNode(ISD::ADD, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003333 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003334 DAG.getIntPtrConstant(StackAlign-1));
Bill Wendling856ff412009-12-22 00:12:37 +00003335
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003336 // Mask out the low bits for alignment purposes.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003337 AllocSize = DAG.getNode(ISD::AND, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003338 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003339 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
3340
3341 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Owen Anderson825b72b2009-08-11 20:47:22 +00003342 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003343 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurSDLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003344 VTs, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003345 setValue(&I, DSA);
3346 DAG.setRoot(DSA.getValue(1));
Bill Wendling856ff412009-12-22 00:12:37 +00003347
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003348 // Inform the Frame Information that we have just allocated a variable-sized
3349 // object.
Bob Wilson8f637ad2013-02-08 20:35:15 +00003350 FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject(Align ? Align : 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003351}
3352
Dan Gohman46510a72010-04-15 01:51:59 +00003353void SelectionDAGBuilder::visitLoad(const LoadInst &I) {
Eli Friedman327236c2011-08-24 20:50:09 +00003354 if (I.isAtomic())
3355 return visitAtomicLoad(I);
3356
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003357 const Value *SV = I.getOperand(0);
3358 SDValue Ptr = getValue(SV);
3359
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003360 Type *Ty = I.getType();
David Greene1e559442010-02-15 17:00:31 +00003361
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003362 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00003363 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Pete Cooperd752e0f2011-11-08 18:42:53 +00003364 bool isInvariant = I.getMetadata("invariant.load") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003365 unsigned Alignment = I.getAlignment();
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00003366 const MDNode *TBAAInfo = I.getMetadata(LLVMContext::MD_tbaa);
Rafael Espindola95d594c2012-03-31 18:14:00 +00003367 const MDNode *Ranges = I.getMetadata(LLVMContext::MD_range);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003368
Owen Andersone50ed302009-08-10 22:56:29 +00003369 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003370 SmallVector<uint64_t, 4> Offsets;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003371 ComputeValueVTs(*TM.getTargetLowering(), Ty, ValueVTs, &Offsets);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003372 unsigned NumValues = ValueVTs.size();
3373 if (NumValues == 0)
3374 return;
3375
3376 SDValue Root;
3377 bool ConstantMemory = false;
Andrew Trickde91f3c2010-11-12 17:50:46 +00003378 if (I.isVolatile() || NumValues > MaxParallelChains)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003379 // Serialize volatile loads with other side effects.
3380 Root = getRoot();
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00003381 else if (AA->pointsToConstantMemory(
3382 AliasAnalysis::Location(SV, AA->getTypeStoreSize(Ty), TBAAInfo))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003383 // Do not serialize (non-volatile) loads of constant memory with anything.
3384 Root = DAG.getEntryNode();
3385 ConstantMemory = true;
3386 } else {
3387 // Do not serialize non-volatile loads against each other.
3388 Root = DAG.getRoot();
3389 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003390
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003391 SmallVector<SDValue, 4> Values(NumValues);
Andrew Trickde91f3c2010-11-12 17:50:46 +00003392 SmallVector<SDValue, 4> Chains(std::min(unsigned(MaxParallelChains),
3393 NumValues));
Owen Andersone50ed302009-08-10 22:56:29 +00003394 EVT PtrVT = Ptr.getValueType();
Andrew Trickde91f3c2010-11-12 17:50:46 +00003395 unsigned ChainI = 0;
3396 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
3397 // Serializing loads here may result in excessive register pressure, and
3398 // TokenFactor places arbitrary choke points on the scheduler. SD scheduling
3399 // could recover a bit by hoisting nodes upward in the chain by recognizing
3400 // they are side-effect free or do not alias. The optimizer should really
3401 // avoid this case by converting large object/array copies to llvm.memcpy
3402 // (MaxParallelChains should always remain as failsafe).
3403 if (ChainI == MaxParallelChains) {
3404 assert(PendingLoads.empty() && "PendingLoads must be serialized first");
Andrew Trickac6d9be2013-05-25 02:42:55 +00003405 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003406 MVT::Other, &Chains[0], ChainI);
3407 Root = Chain;
3408 ChainI = 0;
3409 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00003410 SDValue A = DAG.getNode(ISD::ADD, getCurSDLoc(),
Bill Wendling856ff412009-12-22 00:12:37 +00003411 PtrVT, Ptr,
3412 DAG.getConstant(Offsets[i], PtrVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003413 SDValue L = DAG.getLoad(ValueVTs[i], getCurSDLoc(), Root,
Michael J. Spencere70c5262010-10-16 08:25:21 +00003414 A, MachinePointerInfo(SV, Offsets[i]), isVolatile,
Rafael Espindola95d594c2012-03-31 18:14:00 +00003415 isNonTemporal, isInvariant, Alignment, TBAAInfo,
3416 Ranges);
Bill Wendling856ff412009-12-22 00:12:37 +00003417
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003418 Values[i] = L;
Andrew Trickde91f3c2010-11-12 17:50:46 +00003419 Chains[ChainI] = L.getValue(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003420 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003421
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003422 if (!ConstantMemory) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003423 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003424 MVT::Other, &Chains[0], ChainI);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003425 if (isVolatile)
3426 DAG.setRoot(Chain);
3427 else
3428 PendingLoads.push_back(Chain);
3429 }
3430
Andrew Trickac6d9be2013-05-25 02:42:55 +00003431 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00003432 DAG.getVTList(&ValueVTs[0], NumValues),
3433 &Values[0], NumValues));
Bill Wendling856ff412009-12-22 00:12:37 +00003434}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003435
Dan Gohman46510a72010-04-15 01:51:59 +00003436void SelectionDAGBuilder::visitStore(const StoreInst &I) {
Eli Friedman327236c2011-08-24 20:50:09 +00003437 if (I.isAtomic())
3438 return visitAtomicStore(I);
3439
Dan Gohman46510a72010-04-15 01:51:59 +00003440 const Value *SrcV = I.getOperand(0);
3441 const Value *PtrV = I.getOperand(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003442
Owen Andersone50ed302009-08-10 22:56:29 +00003443 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003444 SmallVector<uint64_t, 4> Offsets;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003445 ComputeValueVTs(*TM.getTargetLowering(), SrcV->getType(), ValueVTs, &Offsets);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003446 unsigned NumValues = ValueVTs.size();
3447 if (NumValues == 0)
3448 return;
3449
3450 // Get the lowered operands. Note that we do this after
3451 // checking if NumResults is zero, because with zero results
3452 // the operands won't have values in the map.
3453 SDValue Src = getValue(SrcV);
3454 SDValue Ptr = getValue(PtrV);
3455
3456 SDValue Root = getRoot();
Andrew Trickde91f3c2010-11-12 17:50:46 +00003457 SmallVector<SDValue, 4> Chains(std::min(unsigned(MaxParallelChains),
3458 NumValues));
Owen Andersone50ed302009-08-10 22:56:29 +00003459 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003460 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00003461 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003462 unsigned Alignment = I.getAlignment();
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00003463 const MDNode *TBAAInfo = I.getMetadata(LLVMContext::MD_tbaa);
Bill Wendling856ff412009-12-22 00:12:37 +00003464
Andrew Trickde91f3c2010-11-12 17:50:46 +00003465 unsigned ChainI = 0;
3466 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
3467 // See visitLoad comments.
3468 if (ChainI == MaxParallelChains) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003469 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003470 MVT::Other, &Chains[0], ChainI);
3471 Root = Chain;
3472 ChainI = 0;
3473 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00003474 SDValue Add = DAG.getNode(ISD::ADD, getCurSDLoc(), PtrVT, Ptr,
Bill Wendling856ff412009-12-22 00:12:37 +00003475 DAG.getConstant(Offsets[i], PtrVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003476 SDValue St = DAG.getStore(Root, getCurSDLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003477 SDValue(Src.getNode(), Src.getResNo() + i),
3478 Add, MachinePointerInfo(PtrV, Offsets[i]),
3479 isVolatile, isNonTemporal, Alignment, TBAAInfo);
3480 Chains[ChainI] = St;
Bill Wendling856ff412009-12-22 00:12:37 +00003481 }
3482
Andrew Trickac6d9be2013-05-25 02:42:55 +00003483 SDValue StoreNode = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003484 MVT::Other, &Chains[0], ChainI);
Devang Patel7e13efa2010-10-26 22:14:52 +00003485 DAG.setRoot(StoreNode);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003486}
3487
Eli Friedman26689ac2011-08-03 21:06:02 +00003488static SDValue InsertFenceForAtomic(SDValue Chain, AtomicOrdering Order,
Eli Friedman327236c2011-08-24 20:50:09 +00003489 SynchronizationScope Scope,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003490 bool Before, SDLoc dl,
Eli Friedman26689ac2011-08-03 21:06:02 +00003491 SelectionDAG &DAG,
3492 const TargetLowering &TLI) {
3493 // Fence, if necessary
3494 if (Before) {
Eli Friedman069e2ed2011-08-26 02:59:24 +00003495 if (Order == AcquireRelease || Order == SequentiallyConsistent)
Eli Friedman26689ac2011-08-03 21:06:02 +00003496 Order = Release;
3497 else if (Order == Acquire || Order == Monotonic)
3498 return Chain;
3499 } else {
3500 if (Order == AcquireRelease)
3501 Order = Acquire;
3502 else if (Order == Release || Order == Monotonic)
3503 return Chain;
3504 }
3505 SDValue Ops[3];
3506 Ops[0] = Chain;
Eli Friedman327236c2011-08-24 20:50:09 +00003507 Ops[1] = DAG.getConstant(Order, TLI.getPointerTy());
3508 Ops[2] = DAG.getConstant(Scope, TLI.getPointerTy());
Eli Friedman26689ac2011-08-03 21:06:02 +00003509 return DAG.getNode(ISD::ATOMIC_FENCE, dl, MVT::Other, Ops, 3);
3510}
3511
Eli Friedmanff030482011-07-28 21:48:00 +00003512void SelectionDAGBuilder::visitAtomicCmpXchg(const AtomicCmpXchgInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003513 SDLoc dl = getCurSDLoc();
Eli Friedman26689ac2011-08-03 21:06:02 +00003514 AtomicOrdering Order = I.getOrdering();
Eli Friedman327236c2011-08-24 20:50:09 +00003515 SynchronizationScope Scope = I.getSynchScope();
Eli Friedman26689ac2011-08-03 21:06:02 +00003516
3517 SDValue InChain = getRoot();
3518
Bill Wendlingba54bca2013-06-19 21:36:55 +00003519 const TargetLowering *TLI = TM.getTargetLowering();
3520 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003521 InChain = InsertFenceForAtomic(InChain, Order, Scope, true, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003522 DAG, *TLI);
Eli Friedman26689ac2011-08-03 21:06:02 +00003523
Eli Friedman55ba8162011-07-29 03:05:32 +00003524 SDValue L =
Eli Friedman26689ac2011-08-03 21:06:02 +00003525 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl,
Craig Topper0ff11902013-08-15 02:44:19 +00003526 getValue(I.getCompareOperand()).getSimpleValueType(),
Eli Friedman26689ac2011-08-03 21:06:02 +00003527 InChain,
Eli Friedman55ba8162011-07-29 03:05:32 +00003528 getValue(I.getPointerOperand()),
3529 getValue(I.getCompareOperand()),
3530 getValue(I.getNewValOperand()),
3531 MachinePointerInfo(I.getPointerOperand()), 0 /* Alignment */,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003532 TLI->getInsertFencesForAtomic() ? Monotonic : Order,
Eli Friedman327236c2011-08-24 20:50:09 +00003533 Scope);
Eli Friedman26689ac2011-08-03 21:06:02 +00003534
3535 SDValue OutChain = L.getValue(1);
3536
Bill Wendlingba54bca2013-06-19 21:36:55 +00003537 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003538 OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003539 DAG, *TLI);
Eli Friedman26689ac2011-08-03 21:06:02 +00003540
Eli Friedman55ba8162011-07-29 03:05:32 +00003541 setValue(&I, L);
Eli Friedman26689ac2011-08-03 21:06:02 +00003542 DAG.setRoot(OutChain);
Eli Friedmanff030482011-07-28 21:48:00 +00003543}
3544
3545void SelectionDAGBuilder::visitAtomicRMW(const AtomicRMWInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003546 SDLoc dl = getCurSDLoc();
Eli Friedman55ba8162011-07-29 03:05:32 +00003547 ISD::NodeType NT;
3548 switch (I.getOperation()) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003549 default: llvm_unreachable("Unknown atomicrmw operation");
Eli Friedman55ba8162011-07-29 03:05:32 +00003550 case AtomicRMWInst::Xchg: NT = ISD::ATOMIC_SWAP; break;
3551 case AtomicRMWInst::Add: NT = ISD::ATOMIC_LOAD_ADD; break;
3552 case AtomicRMWInst::Sub: NT = ISD::ATOMIC_LOAD_SUB; break;
3553 case AtomicRMWInst::And: NT = ISD::ATOMIC_LOAD_AND; break;
3554 case AtomicRMWInst::Nand: NT = ISD::ATOMIC_LOAD_NAND; break;
3555 case AtomicRMWInst::Or: NT = ISD::ATOMIC_LOAD_OR; break;
3556 case AtomicRMWInst::Xor: NT = ISD::ATOMIC_LOAD_XOR; break;
3557 case AtomicRMWInst::Max: NT = ISD::ATOMIC_LOAD_MAX; break;
3558 case AtomicRMWInst::Min: NT = ISD::ATOMIC_LOAD_MIN; break;
3559 case AtomicRMWInst::UMax: NT = ISD::ATOMIC_LOAD_UMAX; break;
3560 case AtomicRMWInst::UMin: NT = ISD::ATOMIC_LOAD_UMIN; break;
3561 }
Eli Friedman26689ac2011-08-03 21:06:02 +00003562 AtomicOrdering Order = I.getOrdering();
Eli Friedman327236c2011-08-24 20:50:09 +00003563 SynchronizationScope Scope = I.getSynchScope();
Eli Friedman26689ac2011-08-03 21:06:02 +00003564
3565 SDValue InChain = getRoot();
3566
Bill Wendlingba54bca2013-06-19 21:36:55 +00003567 const TargetLowering *TLI = TM.getTargetLowering();
3568 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003569 InChain = InsertFenceForAtomic(InChain, Order, Scope, true, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003570 DAG, *TLI);
Eli Friedman26689ac2011-08-03 21:06:02 +00003571
Eli Friedman55ba8162011-07-29 03:05:32 +00003572 SDValue L =
Eli Friedman26689ac2011-08-03 21:06:02 +00003573 DAG.getAtomic(NT, dl,
Craig Topper0ff11902013-08-15 02:44:19 +00003574 getValue(I.getValOperand()).getSimpleValueType(),
Eli Friedman26689ac2011-08-03 21:06:02 +00003575 InChain,
Eli Friedman55ba8162011-07-29 03:05:32 +00003576 getValue(I.getPointerOperand()),
3577 getValue(I.getValOperand()),
3578 I.getPointerOperand(), 0 /* Alignment */,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003579 TLI->getInsertFencesForAtomic() ? Monotonic : Order,
Eli Friedman327236c2011-08-24 20:50:09 +00003580 Scope);
Eli Friedman26689ac2011-08-03 21:06:02 +00003581
3582 SDValue OutChain = L.getValue(1);
3583
Bill Wendlingba54bca2013-06-19 21:36:55 +00003584 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003585 OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003586 DAG, *TLI);
Eli Friedman26689ac2011-08-03 21:06:02 +00003587
Eli Friedman55ba8162011-07-29 03:05:32 +00003588 setValue(&I, L);
Eli Friedman26689ac2011-08-03 21:06:02 +00003589 DAG.setRoot(OutChain);
Eli Friedmanff030482011-07-28 21:48:00 +00003590}
3591
Eli Friedman47f35132011-07-25 23:16:38 +00003592void SelectionDAGBuilder::visitFence(const FenceInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003593 SDLoc dl = getCurSDLoc();
Bill Wendlingba54bca2013-06-19 21:36:55 +00003594 const TargetLowering *TLI = TM.getTargetLowering();
Eli Friedman14648462011-07-27 22:21:52 +00003595 SDValue Ops[3];
3596 Ops[0] = getRoot();
Bill Wendlingba54bca2013-06-19 21:36:55 +00003597 Ops[1] = DAG.getConstant(I.getOrdering(), TLI->getPointerTy());
3598 Ops[2] = DAG.getConstant(I.getSynchScope(), TLI->getPointerTy());
Eli Friedman14648462011-07-27 22:21:52 +00003599 DAG.setRoot(DAG.getNode(ISD::ATOMIC_FENCE, dl, MVT::Other, Ops, 3));
Eli Friedman47f35132011-07-25 23:16:38 +00003600}
3601
Eli Friedman327236c2011-08-24 20:50:09 +00003602void SelectionDAGBuilder::visitAtomicLoad(const LoadInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003603 SDLoc dl = getCurSDLoc();
Eli Friedman327236c2011-08-24 20:50:09 +00003604 AtomicOrdering Order = I.getOrdering();
3605 SynchronizationScope Scope = I.getSynchScope();
3606
3607 SDValue InChain = getRoot();
3608
Bill Wendlingba54bca2013-06-19 21:36:55 +00003609 const TargetLowering *TLI = TM.getTargetLowering();
3610 EVT VT = TLI->getValueType(I.getType());
Eli Friedman327236c2011-08-24 20:50:09 +00003611
Evan Cheng607acd62013-02-06 02:06:33 +00003612 if (I.getAlignment() < VT.getSizeInBits() / 8)
Eli Friedmanfe731212011-09-13 20:50:54 +00003613 report_fatal_error("Cannot generate unaligned atomic load");
3614
Eli Friedman327236c2011-08-24 20:50:09 +00003615 SDValue L =
3616 DAG.getAtomic(ISD::ATOMIC_LOAD, dl, VT, VT, InChain,
3617 getValue(I.getPointerOperand()),
3618 I.getPointerOperand(), I.getAlignment(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00003619 TLI->getInsertFencesForAtomic() ? Monotonic : Order,
Eli Friedman327236c2011-08-24 20:50:09 +00003620 Scope);
3621
3622 SDValue OutChain = L.getValue(1);
3623
Bill Wendlingba54bca2013-06-19 21:36:55 +00003624 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003625 OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003626 DAG, *TLI);
Eli Friedman327236c2011-08-24 20:50:09 +00003627
3628 setValue(&I, L);
3629 DAG.setRoot(OutChain);
3630}
3631
3632void SelectionDAGBuilder::visitAtomicStore(const StoreInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003633 SDLoc dl = getCurSDLoc();
Eli Friedman327236c2011-08-24 20:50:09 +00003634
3635 AtomicOrdering Order = I.getOrdering();
3636 SynchronizationScope Scope = I.getSynchScope();
3637
3638 SDValue InChain = getRoot();
3639
Bill Wendlingba54bca2013-06-19 21:36:55 +00003640 const TargetLowering *TLI = TM.getTargetLowering();
3641 EVT VT = TLI->getValueType(I.getValueOperand()->getType());
Eli Friedmanfe731212011-09-13 20:50:54 +00003642
Evan Cheng607acd62013-02-06 02:06:33 +00003643 if (I.getAlignment() < VT.getSizeInBits() / 8)
Eli Friedmanfe731212011-09-13 20:50:54 +00003644 report_fatal_error("Cannot generate unaligned atomic store");
3645
Bill Wendlingba54bca2013-06-19 21:36:55 +00003646 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003647 InChain = InsertFenceForAtomic(InChain, Order, Scope, true, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003648 DAG, *TLI);
Eli Friedman327236c2011-08-24 20:50:09 +00003649
3650 SDValue OutChain =
Eli Friedmanfe731212011-09-13 20:50:54 +00003651 DAG.getAtomic(ISD::ATOMIC_STORE, dl, VT,
Eli Friedman327236c2011-08-24 20:50:09 +00003652 InChain,
3653 getValue(I.getPointerOperand()),
3654 getValue(I.getValueOperand()),
3655 I.getPointerOperand(), I.getAlignment(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00003656 TLI->getInsertFencesForAtomic() ? Monotonic : Order,
Eli Friedman327236c2011-08-24 20:50:09 +00003657 Scope);
3658
Bill Wendlingba54bca2013-06-19 21:36:55 +00003659 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003660 OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003661 DAG, *TLI);
Eli Friedman327236c2011-08-24 20:50:09 +00003662
3663 DAG.setRoot(OutChain);
3664}
3665
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003666/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
3667/// node.
Dan Gohman46510a72010-04-15 01:51:59 +00003668void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I,
Dan Gohman2048b852009-11-23 18:04:58 +00003669 unsigned Intrinsic) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003670 bool HasChain = !I.doesNotAccessMemory();
3671 bool OnlyLoad = HasChain && I.onlyReadsMemory();
3672
3673 // Build the operand list.
3674 SmallVector<SDValue, 8> Ops;
3675 if (HasChain) { // If this intrinsic has side-effects, chainify it.
3676 if (OnlyLoad) {
3677 // We don't need to serialize loads against other loads.
3678 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003679 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003680 Ops.push_back(getRoot());
3681 }
3682 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003683
3684 // Info is set by getTgtMemInstrinsic
3685 TargetLowering::IntrinsicInfo Info;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003686 const TargetLowering *TLI = TM.getTargetLowering();
3687 bool IsTgtIntrinsic = TLI->getTgtMemIntrinsic(Info, I, Intrinsic);
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003688
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003689 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Bob Wilson65ffec42010-09-21 17:56:22 +00003690 if (!IsTgtIntrinsic || Info.opc == ISD::INTRINSIC_VOID ||
3691 Info.opc == ISD::INTRINSIC_W_CHAIN)
Bill Wendlingba54bca2013-06-19 21:36:55 +00003692 Ops.push_back(DAG.getTargetConstant(Intrinsic, TLI->getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003693
3694 // Add all operands of the call to the operand list.
Gabor Greif0635f352010-06-25 09:38:13 +00003695 for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) {
3696 SDValue Op = getValue(I.getArgOperand(i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003697 Ops.push_back(Op);
3698 }
3699
Owen Andersone50ed302009-08-10 22:56:29 +00003700 SmallVector<EVT, 4> ValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003701 ComputeValueVTs(*TLI, I.getType(), ValueVTs);
Bill Wendling856ff412009-12-22 00:12:37 +00003702
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003703 if (HasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00003704 ValueVTs.push_back(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003705
Bob Wilson8d919552009-07-31 22:41:21 +00003706 SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003707
3708 // Create the node.
3709 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003710 if (IsTgtIntrinsic) {
3711 // This is target intrinsic that touches memory
Andrew Trickac6d9be2013-05-25 02:42:55 +00003712 Result = DAG.getMemIntrinsicNode(Info.opc, getCurSDLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003713 VTs, &Ops[0], Ops.size(),
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00003714 Info.memVT,
3715 MachinePointerInfo(Info.ptrVal, Info.offset),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003716 Info.align, Info.vol,
3717 Info.readMem, Info.writeMem);
Bill Wendling856ff412009-12-22 00:12:37 +00003718 } else if (!HasChain) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003719 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurSDLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003720 VTs, &Ops[0], Ops.size());
Benjamin Kramerf0127052010-01-05 13:12:22 +00003721 } else if (!I.getType()->isVoidTy()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003722 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurSDLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003723 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003724 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003725 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003726 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003727 }
3728
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003729 if (HasChain) {
3730 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
3731 if (OnlyLoad)
3732 PendingLoads.push_back(Chain);
3733 else
3734 DAG.setRoot(Chain);
3735 }
Bill Wendling856ff412009-12-22 00:12:37 +00003736
Benjamin Kramerf0127052010-01-05 13:12:22 +00003737 if (!I.getType()->isVoidTy()) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003738 if (VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00003739 EVT VT = TLI->getValueType(PTy);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003740 Result = DAG.getNode(ISD::BITCAST, getCurSDLoc(), VT, Result);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003741 }
Bill Wendling856ff412009-12-22 00:12:37 +00003742
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003743 setValue(&I, Result);
3744 }
3745}
3746
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003747/// GetSignificand - Get the significand and build it into a floating-point
3748/// number with exponent of 1:
3749///
3750/// Op = (Op & 0x007fffff) | 0x3f800000;
3751///
Matt Beaumont-Gay50e75bf2013-02-25 18:11:18 +00003752/// where Op is the hexadecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003753static SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00003754GetSignificand(SelectionDAG &DAG, SDValue Op, SDLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003755 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3756 DAG.getConstant(0x007fffff, MVT::i32));
3757 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
3758 DAG.getConstant(0x3f800000, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003759 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00003760}
3761
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003762/// GetExponent - Get the exponent:
3763///
Bill Wendlinge9a72862009-01-20 21:17:57 +00003764/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003765///
Matt Beaumont-Gay50e75bf2013-02-25 18:11:18 +00003766/// where Op is the hexadecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003767static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00003768GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003769 SDLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003770 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3771 DAG.getConstant(0x7f800000, MVT::i32));
3772 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Duncan Sands92abc622009-01-31 15:50:11 +00003773 DAG.getConstant(23, TLI.getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00003774 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
3775 DAG.getConstant(127, MVT::i32));
Bill Wendling4533cac2010-01-28 21:51:40 +00003776 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00003777}
3778
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003779/// getF32Constant - Get 32-bit floating point constant.
3780static SDValue
3781getF32Constant(SelectionDAG &DAG, unsigned Flt) {
Tim Northover0a29cb02013-01-22 09:46:31 +00003782 return DAG.getConstantFP(APFloat(APFloat::IEEEsingle, APInt(32, Flt)),
3783 MVT::f32);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003784}
3785
Craig Topper538cd482012-11-24 18:52:06 +00003786/// expandExp - Lower an exp intrinsic. Handles the special sequences for
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003787/// limited-precision mode.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003788static SDValue expandExp(SDLoc dl, SDValue Op, SelectionDAG &DAG,
Craig Topper538cd482012-11-24 18:52:06 +00003789 const TargetLowering &TLI) {
3790 if (Op.getValueType() == MVT::f32 &&
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003791 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003792
3793 // Put the exponent in the right bit position for later addition to the
3794 // final result:
3795 //
3796 // #define LOG2OFe 1.4426950f
3797 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Owen Anderson825b72b2009-08-11 20:47:22 +00003798 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003799 getF32Constant(DAG, 0x3fb8aa3b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003800 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003801
3802 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003803 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3804 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003805
3806 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003807 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003808 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendling856ff412009-12-22 00:12:37 +00003809
Craig Topperb3157722012-11-24 08:22:37 +00003810 SDValue TwoToFracPartOfX;
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003811 if (LimitFloatPrecision <= 6) {
3812 // For floating-point precision of 6:
3813 //
3814 // TwoToFractionalPartOfX =
3815 // 0.997535578f +
3816 // (0.735607626f + 0.252464424f * x) * x;
3817 //
3818 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003819 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003820 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003821 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003822 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003823 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
Craig Topperb3157722012-11-24 08:22:37 +00003824 TwoToFracPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
3825 getF32Constant(DAG, 0x3f7f5e7e));
Craig Topper08ac4692012-11-16 20:01:39 +00003826 } else if (LimitFloatPrecision <= 12) {
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003827 // For floating-point precision of 12:
3828 //
3829 // TwoToFractionalPartOfX =
3830 // 0.999892986f +
3831 // (0.696457318f +
3832 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3833 //
3834 // 0.000107046256 error, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003835 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003836 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003837 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003838 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003839 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3840 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003841 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003842 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
Craig Topperb3157722012-11-24 08:22:37 +00003843 TwoToFracPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
3844 getF32Constant(DAG, 0x3f7ff8fd));
Craig Topper08ac4692012-11-16 20:01:39 +00003845 } else { // LimitFloatPrecision <= 18
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003846 // For floating-point precision of 18:
3847 //
3848 // TwoToFractionalPartOfX =
3849 // 0.999999982f +
3850 // (0.693148872f +
3851 // (0.240227044f +
3852 // (0.554906021e-1f +
3853 // (0.961591928e-2f +
3854 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3855 //
3856 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003857 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003858 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003859 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003860 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003861 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3862 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003863 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003864 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3865 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003866 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003867 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3868 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003869 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003870 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3871 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003872 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003873 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
Craig Topperb3157722012-11-24 08:22:37 +00003874 TwoToFracPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
3875 getF32Constant(DAG, 0x3f800000));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003876 }
Craig Topperb3157722012-11-24 08:22:37 +00003877
3878 // Add the exponent into the result in integer domain.
3879 SDValue t13 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, TwoToFracPartOfX);
Craig Topper538cd482012-11-24 18:52:06 +00003880 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3881 DAG.getNode(ISD::ADD, dl, MVT::i32,
3882 t13, IntegerPartOfX));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003883 }
3884
Craig Topper538cd482012-11-24 18:52:06 +00003885 // No special expansion.
3886 return DAG.getNode(ISD::FEXP, dl, Op.getValueType(), Op);
Dale Johannesen59e577f2008-09-05 18:38:42 +00003887}
3888
Craig Topper5d1e0892012-11-23 18:38:31 +00003889/// expandLog - Lower a log intrinsic. Handles the special sequences for
Bill Wendling39150252008-09-09 20:39:27 +00003890/// limited-precision mode.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003891static SDValue expandLog(SDLoc dl, SDValue Op, SelectionDAG &DAG,
Craig Topper5d1e0892012-11-23 18:38:31 +00003892 const TargetLowering &TLI) {
3893 if (Op.getValueType() == MVT::f32 &&
Bill Wendling39150252008-09-09 20:39:27 +00003894 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003895 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00003896
3897 // Scale the exponent by log(2) [0.69314718f].
Bill Wendling46ada192010-03-02 01:55:18 +00003898 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003899 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003900 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003901
3902 // Get the significand and build it into a floating-point number with
3903 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003904 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling39150252008-09-09 20:39:27 +00003905
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003906 SDValue LogOfMantissa;
Bill Wendling39150252008-09-09 20:39:27 +00003907 if (LimitFloatPrecision <= 6) {
3908 // For floating-point precision of 6:
3909 //
3910 // LogofMantissa =
3911 // -1.1609546f +
3912 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003913 //
Bill Wendling39150252008-09-09 20:39:27 +00003914 // error 0.0034276066, which is better than 8 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003915 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003916 getF32Constant(DAG, 0xbe74c456));
Owen Anderson825b72b2009-08-11 20:47:22 +00003917 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003918 getF32Constant(DAG, 0x3fb3a2b1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003919 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003920 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
3921 getF32Constant(DAG, 0x3f949a29));
Craig Topper08ac4692012-11-16 20:01:39 +00003922 } else if (LimitFloatPrecision <= 12) {
Bill Wendling39150252008-09-09 20:39:27 +00003923 // For floating-point precision of 12:
3924 //
3925 // LogOfMantissa =
3926 // -1.7417939f +
3927 // (2.8212026f +
3928 // (-1.4699568f +
3929 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3930 //
3931 // error 0.000061011436, which is 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003932 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003933 getF32Constant(DAG, 0xbd67b6d6));
Owen Anderson825b72b2009-08-11 20:47:22 +00003934 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003935 getF32Constant(DAG, 0x3ee4f4b8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003936 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3937 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003938 getF32Constant(DAG, 0x3fbc278b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003939 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3940 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003941 getF32Constant(DAG, 0x40348e95));
Owen Anderson825b72b2009-08-11 20:47:22 +00003942 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003943 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
3944 getF32Constant(DAG, 0x3fdef31a));
Craig Topper08ac4692012-11-16 20:01:39 +00003945 } else { // LimitFloatPrecision <= 18
Bill Wendling39150252008-09-09 20:39:27 +00003946 // For floating-point precision of 18:
3947 //
3948 // LogOfMantissa =
3949 // -2.1072184f +
3950 // (4.2372794f +
3951 // (-3.7029485f +
3952 // (2.2781945f +
3953 // (-0.87823314f +
3954 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3955 //
3956 // error 0.0000023660568, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003957 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003958 getF32Constant(DAG, 0xbc91e5ac));
Owen Anderson825b72b2009-08-11 20:47:22 +00003959 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003960 getF32Constant(DAG, 0x3e4350aa));
Owen Anderson825b72b2009-08-11 20:47:22 +00003961 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3962 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003963 getF32Constant(DAG, 0x3f60d3e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003964 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3965 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003966 getF32Constant(DAG, 0x4011cdf0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003967 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3968 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003969 getF32Constant(DAG, 0x406cfd1c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003970 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3971 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003972 getF32Constant(DAG, 0x408797cb));
Owen Anderson825b72b2009-08-11 20:47:22 +00003973 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003974 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
3975 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003976 }
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003977
Craig Topper5d1e0892012-11-23 18:38:31 +00003978 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003979 }
3980
Craig Topper5d1e0892012-11-23 18:38:31 +00003981 // No special expansion.
3982 return DAG.getNode(ISD::FLOG, dl, Op.getValueType(), Op);
Dale Johannesen59e577f2008-09-05 18:38:42 +00003983}
3984
Craig Topper5d1e0892012-11-23 18:38:31 +00003985/// expandLog2 - Lower a log2 intrinsic. Handles the special sequences for
Bill Wendling3eb59402008-09-09 00:28:24 +00003986/// limited-precision mode.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003987static SDValue expandLog2(SDLoc dl, SDValue Op, SelectionDAG &DAG,
Craig Topper5d1e0892012-11-23 18:38:31 +00003988 const TargetLowering &TLI) {
3989 if (Op.getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003990 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003991 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003992
Bill Wendling39150252008-09-09 20:39:27 +00003993 // Get the exponent.
Bill Wendling46ada192010-03-02 01:55:18 +00003994 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
Bill Wendling856ff412009-12-22 00:12:37 +00003995
Bill Wendling3eb59402008-09-09 00:28:24 +00003996 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003997 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003998 SDValue X = GetSignificand(DAG, Op1, dl);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003999
Bill Wendling3eb59402008-09-09 00:28:24 +00004000 // Different possible minimax approximations of significand in
4001 // floating-point for various degrees of accuracy over [1,2].
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004002 SDValue Log2ofMantissa;
Bill Wendling3eb59402008-09-09 00:28:24 +00004003 if (LimitFloatPrecision <= 6) {
4004 // For floating-point precision of 6:
4005 //
4006 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
4007 //
4008 // error 0.0049451742, which is more than 7 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004009 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004010 getF32Constant(DAG, 0xbeb08fe0));
Owen Anderson825b72b2009-08-11 20:47:22 +00004011 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004012 getF32Constant(DAG, 0x40019463));
Owen Anderson825b72b2009-08-11 20:47:22 +00004013 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004014 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
4015 getF32Constant(DAG, 0x3fd6633d));
Craig Topper08ac4692012-11-16 20:01:39 +00004016 } else if (LimitFloatPrecision <= 12) {
Bill Wendling3eb59402008-09-09 00:28:24 +00004017 // For floating-point precision of 12:
4018 //
4019 // Log2ofMantissa =
4020 // -2.51285454f +
4021 // (4.07009056f +
4022 // (-2.12067489f +
4023 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004024 //
Bill Wendling3eb59402008-09-09 00:28:24 +00004025 // error 0.0000876136000, which is better than 13 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004026 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004027 getF32Constant(DAG, 0xbda7262e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004028 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004029 getF32Constant(DAG, 0x3f25280b));
Owen Anderson825b72b2009-08-11 20:47:22 +00004030 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
4031 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004032 getF32Constant(DAG, 0x4007b923));
Owen Anderson825b72b2009-08-11 20:47:22 +00004033 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4034 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004035 getF32Constant(DAG, 0x40823e2f));
Owen Anderson825b72b2009-08-11 20:47:22 +00004036 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004037 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
4038 getF32Constant(DAG, 0x4020d29c));
Craig Topper08ac4692012-11-16 20:01:39 +00004039 } else { // LimitFloatPrecision <= 18
Bill Wendling3eb59402008-09-09 00:28:24 +00004040 // For floating-point precision of 18:
4041 //
4042 // Log2ofMantissa =
4043 // -3.0400495f +
4044 // (6.1129976f +
4045 // (-5.3420409f +
4046 // (3.2865683f +
4047 // (-1.2669343f +
4048 // (0.27515199f -
4049 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
4050 //
4051 // error 0.0000018516, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004052 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004053 getF32Constant(DAG, 0xbcd2769e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004054 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004055 getF32Constant(DAG, 0x3e8ce0b9));
Owen Anderson825b72b2009-08-11 20:47:22 +00004056 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
4057 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004058 getF32Constant(DAG, 0x3fa22ae7));
Owen Anderson825b72b2009-08-11 20:47:22 +00004059 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4060 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004061 getF32Constant(DAG, 0x40525723));
Owen Anderson825b72b2009-08-11 20:47:22 +00004062 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4063 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004064 getF32Constant(DAG, 0x40aaf200));
Owen Anderson825b72b2009-08-11 20:47:22 +00004065 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
4066 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004067 getF32Constant(DAG, 0x40c39dad));
Owen Anderson825b72b2009-08-11 20:47:22 +00004068 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004069 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
4070 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00004071 }
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004072
Craig Topper5d1e0892012-11-23 18:38:31 +00004073 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log2ofMantissa);
Dale Johannesen853244f2008-09-05 23:49:37 +00004074 }
Bill Wendling3eb59402008-09-09 00:28:24 +00004075
Craig Topper5d1e0892012-11-23 18:38:31 +00004076 // No special expansion.
4077 return DAG.getNode(ISD::FLOG2, dl, Op.getValueType(), Op);
Dale Johannesen59e577f2008-09-05 18:38:42 +00004078}
4079
Craig Topper5d1e0892012-11-23 18:38:31 +00004080/// expandLog10 - Lower a log10 intrinsic. Handles the special sequences for
Bill Wendling3eb59402008-09-09 00:28:24 +00004081/// limited-precision mode.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004082static SDValue expandLog10(SDLoc dl, SDValue Op, SelectionDAG &DAG,
Craig Topper5d1e0892012-11-23 18:38:31 +00004083 const TargetLowering &TLI) {
4084 if (Op.getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00004085 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004086 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00004087
Bill Wendling39150252008-09-09 20:39:27 +00004088 // Scale the exponent by log10(2) [0.30102999f].
Bill Wendling46ada192010-03-02 01:55:18 +00004089 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00004090 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004091 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00004092
4093 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00004094 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00004095 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling3eb59402008-09-09 00:28:24 +00004096
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004097 SDValue Log10ofMantissa;
Bill Wendling3eb59402008-09-09 00:28:24 +00004098 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00004099 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004100 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00004101 // Log10ofMantissa =
4102 // -0.50419619f +
4103 // (0.60948995f - 0.10380950f * x) * x;
4104 //
4105 // error 0.0014886165, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004106 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004107 getF32Constant(DAG, 0xbdd49a13));
Owen Anderson825b72b2009-08-11 20:47:22 +00004108 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004109 getF32Constant(DAG, 0x3f1c0789));
Owen Anderson825b72b2009-08-11 20:47:22 +00004110 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004111 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
4112 getF32Constant(DAG, 0x3f011300));
Craig Topper08ac4692012-11-16 20:01:39 +00004113 } else if (LimitFloatPrecision <= 12) {
Bill Wendling3eb59402008-09-09 00:28:24 +00004114 // For floating-point precision of 12:
4115 //
4116 // Log10ofMantissa =
4117 // -0.64831180f +
4118 // (0.91751397f +
4119 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
4120 //
4121 // error 0.00019228036, which is better than 12 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004122 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004123 getF32Constant(DAG, 0x3d431f31));
Owen Anderson825b72b2009-08-11 20:47:22 +00004124 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004125 getF32Constant(DAG, 0x3ea21fb2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004126 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
4127 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004128 getF32Constant(DAG, 0x3f6ae232));
Owen Anderson825b72b2009-08-11 20:47:22 +00004129 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004130 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
4131 getF32Constant(DAG, 0x3f25f7c3));
Craig Topper08ac4692012-11-16 20:01:39 +00004132 } else { // LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00004133 // For floating-point precision of 18:
4134 //
4135 // Log10ofMantissa =
4136 // -0.84299375f +
4137 // (1.5327582f +
4138 // (-1.0688956f +
4139 // (0.49102474f +
4140 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
4141 //
4142 // error 0.0000037995730, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004143 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004144 getF32Constant(DAG, 0x3c5d51ce));
Owen Anderson825b72b2009-08-11 20:47:22 +00004145 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004146 getF32Constant(DAG, 0x3e00685a));
Owen Anderson825b72b2009-08-11 20:47:22 +00004147 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
4148 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004149 getF32Constant(DAG, 0x3efb6798));
Owen Anderson825b72b2009-08-11 20:47:22 +00004150 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4151 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004152 getF32Constant(DAG, 0x3f88d192));
Owen Anderson825b72b2009-08-11 20:47:22 +00004153 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4154 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004155 getF32Constant(DAG, 0x3fc4316c));
Owen Anderson825b72b2009-08-11 20:47:22 +00004156 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004157 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
4158 getF32Constant(DAG, 0x3f57ce70));
Bill Wendling3eb59402008-09-09 00:28:24 +00004159 }
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004160
Craig Topper5d1e0892012-11-23 18:38:31 +00004161 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log10ofMantissa);
Dale Johannesen852680a2008-09-05 21:27:19 +00004162 }
Bill Wendling3eb59402008-09-09 00:28:24 +00004163
Craig Topper5d1e0892012-11-23 18:38:31 +00004164 // No special expansion.
4165 return DAG.getNode(ISD::FLOG10, dl, Op.getValueType(), Op);
Dale Johannesen59e577f2008-09-05 18:38:42 +00004166}
4167
Craig Topper538cd482012-11-24 18:52:06 +00004168/// expandExp2 - Lower an exp2 intrinsic. Handles the special sequences for
Bill Wendlinge10c8142008-09-09 22:39:21 +00004169/// limited-precision mode.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004170static SDValue expandExp2(SDLoc dl, SDValue Op, SelectionDAG &DAG,
Craig Topper538cd482012-11-24 18:52:06 +00004171 const TargetLowering &TLI) {
4172 if (Op.getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00004173 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004174 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00004175
4176 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00004177 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
4178 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00004179
4180 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00004181 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00004182 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00004183
Craig Topperb3157722012-11-24 08:22:37 +00004184 SDValue TwoToFractionalPartOfX;
Bill Wendlinge10c8142008-09-09 22:39:21 +00004185 if (LimitFloatPrecision <= 6) {
4186 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004187 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00004188 // TwoToFractionalPartOfX =
4189 // 0.997535578f +
4190 // (0.735607626f + 0.252464424f * x) * x;
4191 //
4192 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004193 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004194 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00004195 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004196 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004197 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
Craig Topperb3157722012-11-24 08:22:37 +00004198 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
4199 getF32Constant(DAG, 0x3f7f5e7e));
Craig Topper08ac4692012-11-16 20:01:39 +00004200 } else if (LimitFloatPrecision <= 12) {
Bill Wendlinge10c8142008-09-09 22:39:21 +00004201 // For floating-point precision of 12:
4202 //
4203 // TwoToFractionalPartOfX =
4204 // 0.999892986f +
4205 // (0.696457318f +
4206 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
4207 //
4208 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004209 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004210 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004211 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004212 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004213 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4214 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004215 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00004216 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
Craig Topperb3157722012-11-24 08:22:37 +00004217 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
4218 getF32Constant(DAG, 0x3f7ff8fd));
Craig Topper08ac4692012-11-16 20:01:39 +00004219 } else { // LimitFloatPrecision <= 18
Bill Wendlinge10c8142008-09-09 22:39:21 +00004220 // For floating-point precision of 18:
4221 //
4222 // TwoToFractionalPartOfX =
4223 // 0.999999982f +
4224 // (0.693148872f +
4225 // (0.240227044f +
4226 // (0.554906021e-1f +
4227 // (0.961591928e-2f +
4228 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
4229 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004230 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004231 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004232 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004233 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00004234 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4235 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004236 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00004237 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4238 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004239 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00004240 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
4241 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004242 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00004243 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
4244 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004245 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00004246 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
Craig Topperb3157722012-11-24 08:22:37 +00004247 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
4248 getF32Constant(DAG, 0x3f800000));
Bill Wendlinge10c8142008-09-09 22:39:21 +00004249 }
Craig Topperb3157722012-11-24 08:22:37 +00004250
4251 // Add the exponent into the result in integer domain.
4252 SDValue t13 = DAG.getNode(ISD::BITCAST, dl, MVT::i32,
4253 TwoToFractionalPartOfX);
Craig Topper538cd482012-11-24 18:52:06 +00004254 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
4255 DAG.getNode(ISD::ADD, dl, MVT::i32,
4256 t13, IntegerPartOfX));
Dale Johannesen601d3c02008-09-05 01:48:15 +00004257 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00004258
Craig Topper538cd482012-11-24 18:52:06 +00004259 // No special expansion.
4260 return DAG.getNode(ISD::FEXP2, dl, Op.getValueType(), Op);
Dale Johannesen601d3c02008-09-05 01:48:15 +00004261}
4262
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004263/// visitPow - Lower a pow intrinsic. Handles the special sequences for
4264/// limited-precision mode with x == 10.0f.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004265static SDValue expandPow(SDLoc dl, SDValue LHS, SDValue RHS,
Craig Topper327e4cb2012-11-25 08:08:58 +00004266 SelectionDAG &DAG, const TargetLowering &TLI) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004267 bool IsExp10 = false;
Craig Topper327e4cb2012-11-25 08:08:58 +00004268 if (LHS.getValueType() == MVT::f32 && LHS.getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004269 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Craig Topper327e4cb2012-11-25 08:08:58 +00004270 if (ConstantFPSDNode *LHSC = dyn_cast<ConstantFPSDNode>(LHS)) {
4271 APFloat Ten(10.0f);
4272 IsExp10 = LHSC->isExactlyValue(Ten);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004273 }
4274 }
4275
Craig Topperc1aa6382012-11-25 00:48:58 +00004276 if (IsExp10) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004277 // Put the exponent in the right bit position for later addition to the
4278 // final result:
4279 //
4280 // #define LOG2OF10 3.3219281f
4281 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Craig Topper327e4cb2012-11-25 08:08:58 +00004282 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, RHS,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004283 getF32Constant(DAG, 0x40549a78));
Owen Anderson825b72b2009-08-11 20:47:22 +00004284 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004285
4286 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00004287 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
4288 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004289
4290 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00004291 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00004292 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004293
Craig Topper915562e2012-11-25 00:15:07 +00004294 SDValue TwoToFractionalPartOfX;
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004295 if (LimitFloatPrecision <= 6) {
4296 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004297 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004298 // twoToFractionalPartOfX =
4299 // 0.997535578f +
4300 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004301 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004302 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004303 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004304 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00004305 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004306 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004307 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
Craig Topper915562e2012-11-25 00:15:07 +00004308 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
4309 getF32Constant(DAG, 0x3f7f5e7e));
Craig Topper08ac4692012-11-16 20:01:39 +00004310 } else if (LimitFloatPrecision <= 12) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004311 // For floating-point precision of 12:
4312 //
4313 // TwoToFractionalPartOfX =
4314 // 0.999892986f +
4315 // (0.696457318f +
4316 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
4317 //
4318 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004319 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004320 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004321 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004322 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004323 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4324 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004325 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00004326 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
Craig Topper915562e2012-11-25 00:15:07 +00004327 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
4328 getF32Constant(DAG, 0x3f7ff8fd));
Craig Topper08ac4692012-11-16 20:01:39 +00004329 } else { // LimitFloatPrecision <= 18
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004330 // For floating-point precision of 18:
4331 //
4332 // TwoToFractionalPartOfX =
4333 // 0.999999982f +
4334 // (0.693148872f +
4335 // (0.240227044f +
4336 // (0.554906021e-1f +
4337 // (0.961591928e-2f +
4338 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
4339 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004340 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004341 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004342 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004343 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00004344 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4345 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004346 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00004347 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4348 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004349 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00004350 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
4351 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004352 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00004353 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
4354 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004355 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00004356 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
Craig Topper915562e2012-11-25 00:15:07 +00004357 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
4358 getF32Constant(DAG, 0x3f800000));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004359 }
Craig Topper915562e2012-11-25 00:15:07 +00004360
4361 SDValue t13 = DAG.getNode(ISD::BITCAST, dl,MVT::i32,TwoToFractionalPartOfX);
Craig Topper327e4cb2012-11-25 08:08:58 +00004362 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
4363 DAG.getNode(ISD::ADD, dl, MVT::i32,
4364 t13, IntegerPartOfX));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004365 }
4366
Craig Topper327e4cb2012-11-25 08:08:58 +00004367 // No special expansion.
4368 return DAG.getNode(ISD::FPOW, dl, LHS.getValueType(), LHS, RHS);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004369}
4370
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004371
4372/// ExpandPowI - Expand a llvm.powi intrinsic.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004373static SDValue ExpandPowI(SDLoc DL, SDValue LHS, SDValue RHS,
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004374 SelectionDAG &DAG) {
4375 // If RHS is a constant, we can expand this out to a multiplication tree,
4376 // otherwise we end up lowering to a call to __powidf2 (for example). When
4377 // optimizing for size, we only want to do this if the expansion would produce
4378 // a small number of multiplies, otherwise we do the full expansion.
4379 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
4380 // Get the exponent as a positive value.
4381 unsigned Val = RHSC->getSExtValue();
4382 if ((int)Val < 0) Val = -Val;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004383
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004384 // powi(x, 0) -> 1.0
4385 if (Val == 0)
4386 return DAG.getConstantFP(1.0, LHS.getValueType());
4387
Dan Gohmanae541aa2010-04-15 04:33:49 +00004388 const Function *F = DAG.getMachineFunction().getFunction();
Bill Wendling831737d2012-12-30 10:32:01 +00004389 if (!F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
4390 Attribute::OptimizeForSize) ||
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004391 // If optimizing for size, don't insert too many multiplies. This
4392 // inserts up to 5 multiplies.
4393 CountPopulation_32(Val)+Log2_32(Val) < 7) {
4394 // We use the simple binary decomposition method to generate the multiply
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004395 // sequence. There are more optimal ways to do this (for example,
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004396 // powi(x,15) generates one more multiply than it should), but this has
4397 // the benefit of being both really simple and much better than a libcall.
4398 SDValue Res; // Logically starts equal to 1.0
4399 SDValue CurSquare = LHS;
4400 while (Val) {
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00004401 if (Val & 1) {
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004402 if (Res.getNode())
4403 Res = DAG.getNode(ISD::FMUL, DL,Res.getValueType(), Res, CurSquare);
4404 else
4405 Res = CurSquare; // 1.0*CurSquare.
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00004406 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004407
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004408 CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
4409 CurSquare, CurSquare);
4410 Val >>= 1;
4411 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004412
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004413 // If the original was negative, invert the result, producing 1/(x*x*x).
4414 if (RHSC->getSExtValue() < 0)
4415 Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(),
4416 DAG.getConstantFP(1.0, LHS.getValueType()), Res);
4417 return Res;
4418 }
4419 }
4420
4421 // Otherwise, expand to a libcall.
4422 return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS);
4423}
4424
Devang Patel227dfdb2011-05-16 21:24:05 +00004425// getTruncatedArgReg - Find underlying register used for an truncated
4426// argument.
4427static unsigned getTruncatedArgReg(const SDValue &N) {
4428 if (N.getOpcode() != ISD::TRUNCATE)
4429 return 0;
4430
4431 const SDValue &Ext = N.getOperand(0);
Stephen Lin09f8ca32013-07-06 21:44:25 +00004432 if (Ext.getOpcode() == ISD::AssertZext ||
4433 Ext.getOpcode() == ISD::AssertSext) {
Devang Patel227dfdb2011-05-16 21:24:05 +00004434 const SDValue &CFR = Ext.getOperand(0);
4435 if (CFR.getOpcode() == ISD::CopyFromReg)
4436 return cast<RegisterSDNode>(CFR.getOperand(1))->getReg();
Craig Topper7eb46d82012-04-11 04:55:51 +00004437 if (CFR.getOpcode() == ISD::TRUNCATE)
4438 return getTruncatedArgReg(CFR);
Devang Patel227dfdb2011-05-16 21:24:05 +00004439 }
4440 return 0;
4441}
4442
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004443/// EmitFuncArgumentDbgValue - If the DbgValueInst is a dbg_value of a function
4444/// argument, create the corresponding DBG_VALUE machine instruction for it now.
4445/// At the end of instruction selection, they will be inserted to the entry BB.
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004446bool
Devang Patel78a06e52010-08-25 20:39:26 +00004447SelectionDAGBuilder::EmitFuncArgumentDbgValue(const Value *V, MDNode *Variable,
Michael J. Spencere70c5262010-10-16 08:25:21 +00004448 int64_t Offset,
Dan Gohman5d11ea32010-05-01 00:33:16 +00004449 const SDValue &N) {
Devang Patel0b48ead2010-08-31 22:22:42 +00004450 const Argument *Arg = dyn_cast<Argument>(V);
4451 if (!Arg)
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004452 return false;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004453
Devang Patel719f6a92010-04-29 20:40:36 +00004454 MachineFunction &MF = DAG.getMachineFunction();
Devang Patela90b3052010-11-02 17:01:30 +00004455 const TargetInstrInfo *TII = DAG.getTarget().getInstrInfo();
Devang Patela90b3052010-11-02 17:01:30 +00004456
Devang Patela83ce982010-04-29 18:50:36 +00004457 // Ignore inlined function arguments here.
4458 DIVariable DV(Variable);
Devang Patel719f6a92010-04-29 20:40:36 +00004459 if (DV.isInlinedFnArgument(MF.getFunction()))
Devang Patela83ce982010-04-29 18:50:36 +00004460 return false;
4461
David Blaikie6d9dbd52013-06-16 20:34:15 +00004462 Optional<MachineOperand> Op;
Devang Patel9aee3352011-09-08 22:59:09 +00004463 // Some arguments' frame index is recorded during argument lowering.
David Blaikie6d9dbd52013-06-16 20:34:15 +00004464 if (int FI = FuncInfo.getArgumentFrameIndex(Arg))
4465 Op = MachineOperand::CreateFI(FI);
Devang Patel0b48ead2010-08-31 22:22:42 +00004466
David Blaikie6d9dbd52013-06-16 20:34:15 +00004467 if (!Op && N.getNode()) {
4468 unsigned Reg;
Devang Patel227dfdb2011-05-16 21:24:05 +00004469 if (N.getOpcode() == ISD::CopyFromReg)
4470 Reg = cast<RegisterSDNode>(N.getOperand(1))->getReg();
4471 else
4472 Reg = getTruncatedArgReg(N);
4473 if (Reg && TargetRegisterInfo::isVirtualRegister(Reg)) {
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004474 MachineRegisterInfo &RegInfo = MF.getRegInfo();
4475 unsigned PR = RegInfo.getLiveInPhysReg(Reg);
4476 if (PR)
4477 Reg = PR;
4478 }
David Blaikie6d9dbd52013-06-16 20:34:15 +00004479 if (Reg)
4480 Op = MachineOperand::CreateReg(Reg, false);
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004481 }
4482
David Blaikie6d9dbd52013-06-16 20:34:15 +00004483 if (!Op) {
Devang Patela90b3052010-11-02 17:01:30 +00004484 // Check if ValueMap has reg number.
Evan Chenga36acad2010-04-29 06:33:38 +00004485 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
Devang Patel8bc9ef72010-11-02 17:19:03 +00004486 if (VMI != FuncInfo.ValueMap.end())
David Blaikie6d9dbd52013-06-16 20:34:15 +00004487 Op = MachineOperand::CreateReg(VMI->second, false);
Evan Chenga36acad2010-04-29 06:33:38 +00004488 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004489
David Blaikie6d9dbd52013-06-16 20:34:15 +00004490 if (!Op && N.getNode())
Devang Patela90b3052010-11-02 17:01:30 +00004491 // Check if frame index is available.
4492 if (LoadSDNode *LNode = dyn_cast<LoadSDNode>(N.getNode()))
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004493 if (FrameIndexSDNode *FINode =
David Blaikie6d9dbd52013-06-16 20:34:15 +00004494 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
4495 Op = MachineOperand::CreateFI(FINode->getIndex());
Devang Patel8bc9ef72010-11-02 17:19:03 +00004496
David Blaikie6d9dbd52013-06-16 20:34:15 +00004497 if (!Op)
Devang Patel8bc9ef72010-11-02 17:19:03 +00004498 return false;
Devang Patela90b3052010-11-02 17:01:30 +00004499
Adrian Prantl893ae832013-07-10 01:53:30 +00004500 // FIXME: This does not handle register-indirect values at offset 0.
4501 bool IsIndirect = Offset != 0;
David Blaikie6d9dbd52013-06-16 20:34:15 +00004502 if (Op->isReg())
Adrian Prantl35176402013-07-09 20:28:37 +00004503 FuncInfo.ArgDbgValues.push_back(BuildMI(MF, getCurDebugLoc(),
4504 TII->get(TargetOpcode::DBG_VALUE),
Adrian Prantl893ae832013-07-10 01:53:30 +00004505 IsIndirect,
Adrian Prantl35176402013-07-09 20:28:37 +00004506 Op->getReg(), Offset, Variable));
4507 else
4508 FuncInfo.ArgDbgValues.push_back(
David Blaikie6d9dbd52013-06-16 20:34:15 +00004509 BuildMI(MF, getCurDebugLoc(), TII->get(TargetOpcode::DBG_VALUE))
4510 .addOperand(*Op).addImm(Offset).addMetadata(Variable));
Adrian Prantl35176402013-07-09 20:28:37 +00004511
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004512 return true;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004513}
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004514
Douglas Gregor7d9663c2010-05-11 06:17:44 +00004515// VisualStudio defines setjmp as _setjmp
Michael J. Spencer1f409602010-09-24 19:48:47 +00004516#if defined(_MSC_VER) && defined(setjmp) && \
4517 !defined(setjmp_undefined_for_msvc)
4518# pragma push_macro("setjmp")
4519# undef setjmp
4520# define setjmp_undefined_for_msvc
Douglas Gregor7d9663c2010-05-11 06:17:44 +00004521#endif
4522
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004523/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
4524/// we want to emit this as a call to a named external function, return the name
4525/// otherwise lower it and return null.
4526const char *
Dan Gohman46510a72010-04-15 01:51:59 +00004527SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, unsigned Intrinsic) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00004528 const TargetLowering *TLI = TM.getTargetLowering();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004529 SDLoc sdl = getCurSDLoc();
Dale Johannesen66978ee2009-01-31 02:22:37 +00004530 DebugLoc dl = getCurDebugLoc();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004531 SDValue Res;
4532
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004533 switch (Intrinsic) {
4534 default:
4535 // By default, turn this into a target intrinsic node.
4536 visitTargetIntrinsic(I, Intrinsic);
4537 return 0;
4538 case Intrinsic::vastart: visitVAStart(I); return 0;
4539 case Intrinsic::vaend: visitVAEnd(I); return 0;
4540 case Intrinsic::vacopy: visitVACopy(I); return 0;
4541 case Intrinsic::returnaddress:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004542 setValue(&I, DAG.getNode(ISD::RETURNADDR, sdl, TLI->getPointerTy(),
Gabor Greif0635f352010-06-25 09:38:13 +00004543 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004544 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00004545 case Intrinsic::frameaddress:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004546 setValue(&I, DAG.getNode(ISD::FRAMEADDR, sdl, TLI->getPointerTy(),
Gabor Greif0635f352010-06-25 09:38:13 +00004547 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004548 return 0;
4549 case Intrinsic::setjmp:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004550 return &"_setjmp"[!TLI->usesUnderscoreSetJmp()];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004551 case Intrinsic::longjmp:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004552 return &"_longjmp"[!TLI->usesUnderscoreLongJmp()];
Chris Lattner824b9582008-11-21 16:42:48 +00004553 case Intrinsic::memcpy: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00004554 // Assert for address < 256 since we support only user defined address
4555 // spaces.
Gabor Greif0635f352010-06-25 09:38:13 +00004556 assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004557 < 256 &&
Gabor Greif0635f352010-06-25 09:38:13 +00004558 cast<PointerType>(I.getArgOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004559 < 256 &&
4560 "Unknown address space");
Gabor Greif0635f352010-06-25 09:38:13 +00004561 SDValue Op1 = getValue(I.getArgOperand(0));
4562 SDValue Op2 = getValue(I.getArgOperand(1));
4563 SDValue Op3 = getValue(I.getArgOperand(2));
4564 unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
Chandler Carruthaf23f8e2013-02-25 14:20:21 +00004565 if (!Align)
4566 Align = 1; // @llvm.memcpy defines 0 and 1 to both mean no alignment.
Gabor Greif0635f352010-06-25 09:38:13 +00004567 bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004568 DAG.setRoot(DAG.getMemcpy(getRoot(), sdl, Op1, Op2, Op3, Align, isVol, false,
Chris Lattnere72f2022010-09-21 05:40:29 +00004569 MachinePointerInfo(I.getArgOperand(0)),
4570 MachinePointerInfo(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004571 return 0;
4572 }
Chris Lattner824b9582008-11-21 16:42:48 +00004573 case Intrinsic::memset: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00004574 // Assert for address < 256 since we support only user defined address
4575 // spaces.
Gabor Greif0635f352010-06-25 09:38:13 +00004576 assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004577 < 256 &&
4578 "Unknown address space");
Gabor Greif0635f352010-06-25 09:38:13 +00004579 SDValue Op1 = getValue(I.getArgOperand(0));
4580 SDValue Op2 = getValue(I.getArgOperand(1));
4581 SDValue Op3 = getValue(I.getArgOperand(2));
4582 unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
Chandler Carruthaf23f8e2013-02-25 14:20:21 +00004583 if (!Align)
4584 Align = 1; // @llvm.memset defines 0 and 1 to both mean no alignment.
Gabor Greif0635f352010-06-25 09:38:13 +00004585 bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004586 DAG.setRoot(DAG.getMemset(getRoot(), sdl, Op1, Op2, Op3, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004587 MachinePointerInfo(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004588 return 0;
4589 }
Chris Lattner824b9582008-11-21 16:42:48 +00004590 case Intrinsic::memmove: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00004591 // Assert for address < 256 since we support only user defined address
4592 // spaces.
Gabor Greif0635f352010-06-25 09:38:13 +00004593 assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004594 < 256 &&
Gabor Greif0635f352010-06-25 09:38:13 +00004595 cast<PointerType>(I.getArgOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004596 < 256 &&
4597 "Unknown address space");
Gabor Greif0635f352010-06-25 09:38:13 +00004598 SDValue Op1 = getValue(I.getArgOperand(0));
4599 SDValue Op2 = getValue(I.getArgOperand(1));
4600 SDValue Op3 = getValue(I.getArgOperand(2));
4601 unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
Chandler Carruthaf23f8e2013-02-25 14:20:21 +00004602 if (!Align)
4603 Align = 1; // @llvm.memmove defines 0 and 1 to both mean no alignment.
Gabor Greif0635f352010-06-25 09:38:13 +00004604 bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004605 DAG.setRoot(DAG.getMemmove(getRoot(), sdl, Op1, Op2, Op3, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004606 MachinePointerInfo(I.getArgOperand(0)),
4607 MachinePointerInfo(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004608 return 0;
4609 }
Bill Wendling92c1e122009-02-13 02:16:35 +00004610 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00004611 const DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
Devang Patelac1ceb32009-10-09 22:42:28 +00004612 MDNode *Variable = DI.getVariable();
Dan Gohman46510a72010-04-15 01:51:59 +00004613 const Value *Address = DI.getAddress();
Manman Rencbafae62013-06-28 05:43:10 +00004614 DIVariable DIVar(Variable);
4615 assert((!DIVar || DIVar.isVariable()) &&
4616 "Variable in DbgDeclareInst should be either null or a DIVariable.");
4617 if (!Address || !DIVar) {
Eric Christopher8f2a88d2012-03-15 21:33:41 +00004618 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
Dale Johannesen8ac38f22010-02-08 21:53:27 +00004619 return 0;
Eric Christopher8f2a88d2012-03-15 21:33:41 +00004620 }
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004621
Devang Patel3f74a112010-09-02 21:29:42 +00004622 // Check if address has undef value.
4623 if (isa<UndefValue>(Address) ||
4624 (Address->use_empty() && !isa<Argument>(Address))) {
Eric Christopher24413672012-02-23 03:39:39 +00004625 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
Devang Patel3f74a112010-09-02 21:29:42 +00004626 return 0;
4627 }
4628
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004629 SDValue &N = NodeMap[Address];
Devang Patel0b48ead2010-08-31 22:22:42 +00004630 if (!N.getNode() && isa<Argument>(Address))
4631 // Check unused arguments map.
4632 N = UnusedArgNodeMap[Address];
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004633 SDDbgValue *SDV;
4634 if (N.getNode()) {
Devang Patel8e741ed2010-09-02 21:02:27 +00004635 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
4636 Address = BCI->getOperand(0);
Eric Christopher178606d2012-02-24 01:59:08 +00004637 // Parameters are handled specially.
4638 bool isParameter =
4639 (DIVariable(Variable).getTag() == dwarf::DW_TAG_arg_variable ||
4640 isa<Argument>(Address));
4641
Devang Patel8e741ed2010-09-02 21:02:27 +00004642 const AllocaInst *AI = dyn_cast<AllocaInst>(Address);
4643
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004644 if (isParameter && !AI) {
4645 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N.getNode());
4646 if (FINode)
4647 // Byval parameter. We have a frame index at this point.
4648 SDV = DAG.getDbgValue(Variable, FINode->getIndex(),
4649 0, dl, SDNodeOrder);
Devang Patelafeaae72010-12-06 22:39:26 +00004650 else {
Devang Patel227dfdb2011-05-16 21:24:05 +00004651 // Address is an argument, so try to emit its dbg value using
4652 // virtual register info from the FuncInfo.ValueMap.
4653 EmitFuncArgumentDbgValue(Address, Variable, 0, N);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004654 return 0;
Devang Patelafeaae72010-12-06 22:39:26 +00004655 }
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004656 } else if (AI)
4657 SDV = DAG.getDbgValue(Variable, N.getNode(), N.getResNo(),
4658 0, dl, SDNodeOrder);
Devang Patelafeaae72010-12-06 22:39:26 +00004659 else {
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004660 // Can't do anything with other non-AI cases yet.
Eric Christopher24413672012-02-23 03:39:39 +00004661 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
Eric Christopher178606d2012-02-24 01:59:08 +00004662 DEBUG(dbgs() << "non-AllocaInst issue for Address: \n\t");
4663 DEBUG(Address->dump());
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004664 return 0;
Devang Patelafeaae72010-12-06 22:39:26 +00004665 }
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004666 DAG.AddDbgValue(SDV, N.getNode(), isParameter);
4667 } else {
Gabor Greiffb4032f2010-10-01 10:32:19 +00004668 // If Address is an argument then try to emit its dbg value using
Michael J. Spencere70c5262010-10-16 08:25:21 +00004669 // virtual register info from the FuncInfo.ValueMap.
Devang Patel6cd467b2010-08-26 22:53:27 +00004670 if (!EmitFuncArgumentDbgValue(Address, Variable, 0, N)) {
Devang Patel1397fdc2010-09-15 14:48:53 +00004671 // If variable is pinned by a alloca in dominating bb then
4672 // use StaticAllocaMap.
4673 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Address)) {
Devang Patel27ede1b2010-09-15 18:13:55 +00004674 if (AI->getParent() != DI.getParent()) {
4675 DenseMap<const AllocaInst*, int>::iterator SI =
4676 FuncInfo.StaticAllocaMap.find(AI);
4677 if (SI != FuncInfo.StaticAllocaMap.end()) {
4678 SDV = DAG.getDbgValue(Variable, SI->second,
4679 0, dl, SDNodeOrder);
4680 DAG.AddDbgValue(SDV, 0, false);
4681 return 0;
4682 }
Devang Patel1397fdc2010-09-15 14:48:53 +00004683 }
4684 }
Eric Christopher0822e012012-02-23 03:39:43 +00004685 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
Devang Patel6cd467b2010-08-26 22:53:27 +00004686 }
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004687 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004688 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00004689 }
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004690 case Intrinsic::dbg_value: {
Dan Gohman46510a72010-04-15 01:51:59 +00004691 const DbgValueInst &DI = cast<DbgValueInst>(I);
Manman Rencbafae62013-06-28 05:43:10 +00004692 DIVariable DIVar(DI.getVariable());
4693 assert((!DIVar || DIVar.isVariable()) &&
4694 "Variable in DbgValueInst should be either null or a DIVariable.");
4695 if (!DIVar)
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004696 return 0;
4697
4698 MDNode *Variable = DI.getVariable();
Devang Patel00190342010-03-15 19:15:44 +00004699 uint64_t Offset = DI.getOffset();
Dan Gohman46510a72010-04-15 01:51:59 +00004700 const Value *V = DI.getValue();
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004701 if (!V)
4702 return 0;
Devang Patel00190342010-03-15 19:15:44 +00004703
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004704 SDDbgValue *SDV;
Devang Patel57871242011-08-03 23:13:55 +00004705 if (isa<ConstantInt>(V) || isa<ConstantFP>(V) || isa<UndefValue>(V)) {
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004706 SDV = DAG.getDbgValue(Variable, V, Offset, dl, SDNodeOrder);
4707 DAG.AddDbgValue(SDV, 0, false);
Devang Patel00190342010-03-15 19:15:44 +00004708 } else {
Dale Johannesenbdc09d92010-07-16 00:02:08 +00004709 // Do not use getValue() in here; we don't want to generate code at
4710 // this point if it hasn't been done yet.
Devang Patel9126c0d2010-06-01 19:59:01 +00004711 SDValue N = NodeMap[V];
4712 if (!N.getNode() && isa<Argument>(V))
4713 // Check unused arguments map.
4714 N = UnusedArgNodeMap[V];
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004715 if (N.getNode()) {
Devang Patel78a06e52010-08-25 20:39:26 +00004716 if (!EmitFuncArgumentDbgValue(V, Variable, Offset, N)) {
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004717 SDV = DAG.getDbgValue(Variable, N.getNode(),
4718 N.getResNo(), Offset, dl, SDNodeOrder);
4719 DAG.AddDbgValue(SDV, N.getNode(), false);
4720 }
Devang Patela778f5c2011-02-18 22:43:42 +00004721 } else if (!V->use_empty() ) {
Dale Johannesenbdc09d92010-07-16 00:02:08 +00004722 // Do not call getValue(V) yet, as we don't want to generate code.
4723 // Remember it for later.
4724 DanglingDebugInfo DDI(&DI, dl, SDNodeOrder);
4725 DanglingDebugInfoMap[V] = DDI;
Devang Patel0991dfb2010-08-27 22:25:51 +00004726 } else {
Devang Patel00190342010-03-15 19:15:44 +00004727 // We may expand this to cover more cases. One case where we have no
Devang Patelafeaae72010-12-06 22:39:26 +00004728 // data available is an unreferenced parameter.
Eric Christopher0822e012012-02-23 03:39:43 +00004729 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004730 }
Devang Patel00190342010-03-15 19:15:44 +00004731 }
4732
4733 // Build a debug info table entry.
Dan Gohman46510a72010-04-15 01:51:59 +00004734 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(V))
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004735 V = BCI->getOperand(0);
Dan Gohman46510a72010-04-15 01:51:59 +00004736 const AllocaInst *AI = dyn_cast<AllocaInst>(V);
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004737 // Don't handle byval struct arguments or VLAs, for example.
Eric Christopher7e1e18f2012-03-26 06:10:32 +00004738 if (!AI) {
Eric Christopher9fc5c832012-03-28 07:34:36 +00004739 DEBUG(dbgs() << "Dropping debug location info for:\n " << DI << "\n");
4740 DEBUG(dbgs() << " Last seen at:\n " << *V << "\n");
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004741 return 0;
Eric Christopher7e1e18f2012-03-26 06:10:32 +00004742 }
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004743 DenseMap<const AllocaInst*, int>::iterator SI =
4744 FuncInfo.StaticAllocaMap.find(AI);
4745 if (SI == FuncInfo.StaticAllocaMap.end())
4746 return 0; // VLAs.
4747 int FI = SI->second;
Michael J. Spencere70c5262010-10-16 08:25:21 +00004748
Chris Lattner512063d2010-04-05 06:19:28 +00004749 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
4750 if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo())
4751 MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc());
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004752 return 0;
4753 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004754
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004755 case Intrinsic::eh_typeid_for: {
Chris Lattner512063d2010-04-05 06:19:28 +00004756 // Find the type id for the given typeinfo.
Gabor Greif0635f352010-06-25 09:38:13 +00004757 GlobalVariable *GV = ExtractTypeInfo(I.getArgOperand(0));
Chris Lattner512063d2010-04-05 06:19:28 +00004758 unsigned TypeID = DAG.getMachineFunction().getMMI().getTypeIDFor(GV);
4759 Res = DAG.getConstant(TypeID, MVT::i32);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004760 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004761 return 0;
4762 }
4763
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004764 case Intrinsic::eh_return_i32:
4765 case Intrinsic::eh_return_i64:
Chris Lattner512063d2010-04-05 06:19:28 +00004766 DAG.getMachineFunction().getMMI().setCallsEHReturn(true);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004767 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, sdl,
Chris Lattner512063d2010-04-05 06:19:28 +00004768 MVT::Other,
4769 getControlRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00004770 getValue(I.getArgOperand(0)),
4771 getValue(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004772 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004773 case Intrinsic::eh_unwind_init:
Chris Lattner512063d2010-04-05 06:19:28 +00004774 DAG.getMachineFunction().getMMI().setCallsUnwindInit(true);
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004775 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004776 case Intrinsic::eh_dwarf_cfa: {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004777 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getArgOperand(0)), sdl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00004778 TLI->getPointerTy());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004779 SDValue Offset = DAG.getNode(ISD::ADD, sdl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00004780 TLI->getPointerTy(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004781 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, sdl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00004782 TLI->getPointerTy()),
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004783 CfaArg);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004784 SDValue FA = DAG.getNode(ISD::FRAMEADDR, sdl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00004785 TLI->getPointerTy(),
4786 DAG.getConstant(0, TLI->getPointerTy()));
4787 setValue(&I, DAG.getNode(ISD::ADD, sdl, TLI->getPointerTy(),
Bill Wendling4533cac2010-01-28 21:51:40 +00004788 FA, Offset));
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004789 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004790 }
Jim Grosbachca752c92010-01-28 01:45:32 +00004791 case Intrinsic::eh_sjlj_callsite: {
Chris Lattner512063d2010-04-05 06:19:28 +00004792 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Gabor Greif0635f352010-06-25 09:38:13 +00004793 ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(0));
Jim Grosbachca752c92010-01-28 01:45:32 +00004794 assert(CI && "Non-constant call site value in eh.sjlj.callsite!");
Chris Lattner512063d2010-04-05 06:19:28 +00004795 assert(MMI.getCurrentCallSite() == 0 && "Overlapping call sites!");
Jim Grosbachca752c92010-01-28 01:45:32 +00004796
Chris Lattner512063d2010-04-05 06:19:28 +00004797 MMI.setCurrentCallSite(CI->getZExtValue());
Jim Grosbachca752c92010-01-28 01:45:32 +00004798 return 0;
4799 }
Bill Wendling6ef94172011-09-28 03:36:43 +00004800 case Intrinsic::eh_sjlj_functioncontext: {
4801 // Get and store the index of the function context.
4802 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bill Wendlingadbf7b22011-09-28 03:52:41 +00004803 AllocaInst *FnCtx =
4804 cast<AllocaInst>(I.getArgOperand(0)->stripPointerCasts());
Bill Wendling6ef94172011-09-28 03:36:43 +00004805 int FI = FuncInfo.StaticAllocaMap[FnCtx];
4806 MFI->setFunctionContextIndex(FI);
4807 return 0;
4808 }
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004809 case Intrinsic::eh_sjlj_setjmp: {
Bill Wendlingce370cf2011-10-07 21:25:38 +00004810 SDValue Ops[2];
4811 Ops[0] = getRoot();
4812 Ops[1] = getValue(I.getArgOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004813 SDValue Op = DAG.getNode(ISD::EH_SJLJ_SETJMP, sdl,
Bill Wendlingce370cf2011-10-07 21:25:38 +00004814 DAG.getVTList(MVT::i32, MVT::Other),
4815 Ops, 2);
4816 setValue(&I, Op.getValue(0));
4817 DAG.setRoot(Op.getValue(1));
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004818 return 0;
4819 }
Jim Grosbach5eb19512010-05-22 01:06:18 +00004820 case Intrinsic::eh_sjlj_longjmp: {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004821 DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_LONGJMP, sdl, MVT::Other,
Jim Grosbache4ad3872010-10-19 23:27:08 +00004822 getRoot(), getValue(I.getArgOperand(0))));
4823 return 0;
4824 }
Jim Grosbachca752c92010-01-28 01:45:32 +00004825
Dale Johannesen0488fb62010-09-30 23:57:10 +00004826 case Intrinsic::x86_mmx_pslli_w:
4827 case Intrinsic::x86_mmx_pslli_d:
4828 case Intrinsic::x86_mmx_pslli_q:
4829 case Intrinsic::x86_mmx_psrli_w:
4830 case Intrinsic::x86_mmx_psrli_d:
4831 case Intrinsic::x86_mmx_psrli_q:
4832 case Intrinsic::x86_mmx_psrai_w:
4833 case Intrinsic::x86_mmx_psrai_d: {
4834 SDValue ShAmt = getValue(I.getArgOperand(1));
4835 if (isa<ConstantSDNode>(ShAmt)) {
4836 visitTargetIntrinsic(I, Intrinsic);
4837 return 0;
4838 }
4839 unsigned NewIntrinsic = 0;
4840 EVT ShAmtVT = MVT::v2i32;
4841 switch (Intrinsic) {
4842 case Intrinsic::x86_mmx_pslli_w:
4843 NewIntrinsic = Intrinsic::x86_mmx_psll_w;
4844 break;
4845 case Intrinsic::x86_mmx_pslli_d:
4846 NewIntrinsic = Intrinsic::x86_mmx_psll_d;
4847 break;
4848 case Intrinsic::x86_mmx_pslli_q:
4849 NewIntrinsic = Intrinsic::x86_mmx_psll_q;
4850 break;
4851 case Intrinsic::x86_mmx_psrli_w:
4852 NewIntrinsic = Intrinsic::x86_mmx_psrl_w;
4853 break;
4854 case Intrinsic::x86_mmx_psrli_d:
4855 NewIntrinsic = Intrinsic::x86_mmx_psrl_d;
4856 break;
4857 case Intrinsic::x86_mmx_psrli_q:
4858 NewIntrinsic = Intrinsic::x86_mmx_psrl_q;
4859 break;
4860 case Intrinsic::x86_mmx_psrai_w:
4861 NewIntrinsic = Intrinsic::x86_mmx_psra_w;
4862 break;
4863 case Intrinsic::x86_mmx_psrai_d:
4864 NewIntrinsic = Intrinsic::x86_mmx_psra_d;
4865 break;
4866 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
4867 }
4868
4869 // The vector shift intrinsics with scalars uses 32b shift amounts but
4870 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
4871 // to be zero.
4872 // We must do this early because v2i32 is not a legal type.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004873 SDValue ShOps[2];
4874 ShOps[0] = ShAmt;
4875 ShOps[1] = DAG.getConstant(0, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004876 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, sdl, ShAmtVT, &ShOps[0], 2);
Bill Wendlingba54bca2013-06-19 21:36:55 +00004877 EVT DestVT = TLI->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004878 ShAmt = DAG.getNode(ISD::BITCAST, sdl, DestVT, ShAmt);
4879 Res = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, sdl, DestVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00004880 DAG.getConstant(NewIntrinsic, MVT::i32),
4881 getValue(I.getArgOperand(0)), ShAmt);
4882 setValue(&I, Res);
4883 return 0;
4884 }
Pete Cooperd18134f2012-02-24 03:51:49 +00004885 case Intrinsic::x86_avx_vinsertf128_pd_256:
4886 case Intrinsic::x86_avx_vinsertf128_ps_256:
Craig Topperb45c9692012-04-07 22:32:29 +00004887 case Intrinsic::x86_avx_vinsertf128_si_256:
4888 case Intrinsic::x86_avx2_vinserti128: {
Bill Wendlingba54bca2013-06-19 21:36:55 +00004889 EVT DestVT = TLI->getValueType(I.getType());
4890 EVT ElVT = TLI->getValueType(I.getArgOperand(1)->getType());
Pete Cooperd18134f2012-02-24 03:51:49 +00004891 uint64_t Idx = (cast<ConstantInt>(I.getArgOperand(2))->getZExtValue() & 1) *
4892 ElVT.getVectorNumElements();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004893 Res = DAG.getNode(ISD::INSERT_SUBVECTOR, sdl, DestVT,
Pete Cooperd18134f2012-02-24 03:51:49 +00004894 getValue(I.getArgOperand(0)),
4895 getValue(I.getArgOperand(1)),
Tom Stellard425b76c2013-08-05 22:22:01 +00004896 DAG.getConstant(Idx, TLI->getVectorIdxTy()));
Craig Topperf6dc7922012-09-05 05:48:09 +00004897 setValue(&I, Res);
4898 return 0;
4899 }
4900 case Intrinsic::x86_avx_vextractf128_pd_256:
4901 case Intrinsic::x86_avx_vextractf128_ps_256:
4902 case Intrinsic::x86_avx_vextractf128_si_256:
4903 case Intrinsic::x86_avx2_vextracti128: {
Bill Wendlingba54bca2013-06-19 21:36:55 +00004904 EVT DestVT = TLI->getValueType(I.getType());
Craig Topperf6dc7922012-09-05 05:48:09 +00004905 uint64_t Idx = (cast<ConstantInt>(I.getArgOperand(1))->getZExtValue() & 1) *
4906 DestVT.getVectorNumElements();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004907 Res = DAG.getNode(ISD::EXTRACT_SUBVECTOR, sdl, DestVT,
Craig Topperf6dc7922012-09-05 05:48:09 +00004908 getValue(I.getArgOperand(0)),
Tom Stellard425b76c2013-08-05 22:22:01 +00004909 DAG.getConstant(Idx, TLI->getVectorIdxTy()));
Pete Cooperd18134f2012-02-24 03:51:49 +00004910 setValue(&I, Res);
4911 return 0;
4912 }
Mon P Wang77cdf302008-11-10 20:54:11 +00004913 case Intrinsic::convertff:
4914 case Intrinsic::convertfsi:
4915 case Intrinsic::convertfui:
4916 case Intrinsic::convertsif:
4917 case Intrinsic::convertuif:
4918 case Intrinsic::convertss:
4919 case Intrinsic::convertsu:
4920 case Intrinsic::convertus:
4921 case Intrinsic::convertuu: {
4922 ISD::CvtCode Code = ISD::CVT_INVALID;
4923 switch (Intrinsic) {
Craig Topperc42e6402012-04-11 04:34:11 +00004924 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Mon P Wang77cdf302008-11-10 20:54:11 +00004925 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
4926 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
4927 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
4928 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
4929 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
4930 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
4931 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
4932 case Intrinsic::convertus: Code = ISD::CVT_US; break;
4933 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
4934 }
Bill Wendlingba54bca2013-06-19 21:36:55 +00004935 EVT DestVT = TLI->getValueType(I.getType());
Gabor Greif0635f352010-06-25 09:38:13 +00004936 const Value *Op1 = I.getArgOperand(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004937 Res = DAG.getConvertRndSat(DestVT, sdl, getValue(Op1),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004938 DAG.getValueType(DestVT),
4939 DAG.getValueType(getValue(Op1).getValueType()),
Gabor Greif0635f352010-06-25 09:38:13 +00004940 getValue(I.getArgOperand(1)),
4941 getValue(I.getArgOperand(2)),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004942 Code);
4943 setValue(&I, Res);
Mon P Wang77cdf302008-11-10 20:54:11 +00004944 return 0;
4945 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004946 case Intrinsic::powi:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004947 setValue(&I, ExpandPowI(sdl, getValue(I.getArgOperand(0)),
Gabor Greif0635f352010-06-25 09:38:13 +00004948 getValue(I.getArgOperand(1)), DAG));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004949 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004950 case Intrinsic::log:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004951 setValue(&I, expandLog(sdl, getValue(I.getArgOperand(0)), DAG, *TLI));
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004952 return 0;
4953 case Intrinsic::log2:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004954 setValue(&I, expandLog2(sdl, getValue(I.getArgOperand(0)), DAG, *TLI));
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004955 return 0;
4956 case Intrinsic::log10:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004957 setValue(&I, expandLog10(sdl, getValue(I.getArgOperand(0)), DAG, *TLI));
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004958 return 0;
4959 case Intrinsic::exp:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004960 setValue(&I, expandExp(sdl, getValue(I.getArgOperand(0)), DAG, *TLI));
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004961 return 0;
4962 case Intrinsic::exp2:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004963 setValue(&I, expandExp2(sdl, getValue(I.getArgOperand(0)), DAG, *TLI));
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004964 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004965 case Intrinsic::pow:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004966 setValue(&I, expandPow(sdl, getValue(I.getArgOperand(0)),
Bill Wendlingba54bca2013-06-19 21:36:55 +00004967 getValue(I.getArgOperand(1)), DAG, *TLI));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004968 return 0;
Craig Topper9bd4dd72012-11-16 07:48:23 +00004969 case Intrinsic::sqrt:
Peter Collingbourneb34d3aa2012-05-28 21:48:37 +00004970 case Intrinsic::fabs:
Craig Topper9bd4dd72012-11-16 07:48:23 +00004971 case Intrinsic::sin:
4972 case Intrinsic::cos:
Dan Gohman27db99f2012-07-26 17:43:27 +00004973 case Intrinsic::floor:
Craig Topper49010472012-11-15 06:51:10 +00004974 case Intrinsic::ceil:
Craig Topper49010472012-11-15 06:51:10 +00004975 case Intrinsic::trunc:
Craig Topper49010472012-11-15 06:51:10 +00004976 case Intrinsic::rint:
Hal Finkel41418d12013-08-07 22:49:12 +00004977 case Intrinsic::nearbyint:
4978 case Intrinsic::round: {
Craig Topper9bd4dd72012-11-16 07:48:23 +00004979 unsigned Opcode;
4980 switch (Intrinsic) {
4981 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
4982 case Intrinsic::sqrt: Opcode = ISD::FSQRT; break;
4983 case Intrinsic::fabs: Opcode = ISD::FABS; break;
4984 case Intrinsic::sin: Opcode = ISD::FSIN; break;
4985 case Intrinsic::cos: Opcode = ISD::FCOS; break;
4986 case Intrinsic::floor: Opcode = ISD::FFLOOR; break;
4987 case Intrinsic::ceil: Opcode = ISD::FCEIL; break;
4988 case Intrinsic::trunc: Opcode = ISD::FTRUNC; break;
4989 case Intrinsic::rint: Opcode = ISD::FRINT; break;
4990 case Intrinsic::nearbyint: Opcode = ISD::FNEARBYINT; break;
Hal Finkel41418d12013-08-07 22:49:12 +00004991 case Intrinsic::round: Opcode = ISD::FROUND; break;
Craig Topper9bd4dd72012-11-16 07:48:23 +00004992 }
4993
Andrew Trickac6d9be2013-05-25 02:42:55 +00004994 setValue(&I, DAG.getNode(Opcode, sdl,
Craig Topper49010472012-11-15 06:51:10 +00004995 getValue(I.getArgOperand(0)).getValueType(),
4996 getValue(I.getArgOperand(0))));
4997 return 0;
Craig Topper9bd4dd72012-11-16 07:48:23 +00004998 }
Hal Finkel66d1fa62013-08-19 23:35:46 +00004999 case Intrinsic::copysign:
5000 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, sdl,
5001 getValue(I.getArgOperand(0)).getValueType(),
5002 getValue(I.getArgOperand(0)),
5003 getValue(I.getArgOperand(1))));
5004 return 0;
Cameron Zwarich33390842011-07-08 21:39:21 +00005005 case Intrinsic::fma:
Andrew Trickac6d9be2013-05-25 02:42:55 +00005006 setValue(&I, DAG.getNode(ISD::FMA, sdl,
Cameron Zwarich33390842011-07-08 21:39:21 +00005007 getValue(I.getArgOperand(0)).getValueType(),
5008 getValue(I.getArgOperand(0)),
5009 getValue(I.getArgOperand(1)),
5010 getValue(I.getArgOperand(2))));
5011 return 0;
Lang Hames5afba6f2012-06-05 19:07:46 +00005012 case Intrinsic::fmuladd: {
Bill Wendlingba54bca2013-06-19 21:36:55 +00005013 EVT VT = TLI->getValueType(I.getType());
Lang Hamese0231412012-06-22 01:09:09 +00005014 if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict &&
Stephen Line54885a2013-07-09 18:16:56 +00005015 TLI->isFMAFasterThanFMulAndFAdd(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005016 setValue(&I, DAG.getNode(ISD::FMA, sdl,
Lang Hames5afba6f2012-06-05 19:07:46 +00005017 getValue(I.getArgOperand(0)).getValueType(),
5018 getValue(I.getArgOperand(0)),
5019 getValue(I.getArgOperand(1)),
5020 getValue(I.getArgOperand(2))));
5021 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005022 SDValue Mul = DAG.getNode(ISD::FMUL, sdl,
Lang Hames5afba6f2012-06-05 19:07:46 +00005023 getValue(I.getArgOperand(0)).getValueType(),
5024 getValue(I.getArgOperand(0)),
5025 getValue(I.getArgOperand(1)));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005026 SDValue Add = DAG.getNode(ISD::FADD, sdl,
Lang Hames5afba6f2012-06-05 19:07:46 +00005027 getValue(I.getArgOperand(0)).getValueType(),
5028 Mul,
5029 getValue(I.getArgOperand(2)));
5030 setValue(&I, Add);
5031 }
5032 return 0;
5033 }
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00005034 case Intrinsic::convert_to_fp16:
Andrew Trickac6d9be2013-05-25 02:42:55 +00005035 setValue(&I, DAG.getNode(ISD::FP32_TO_FP16, sdl,
Gabor Greif0635f352010-06-25 09:38:13 +00005036 MVT::i16, getValue(I.getArgOperand(0))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00005037 return 0;
5038 case Intrinsic::convert_from_fp16:
Andrew Trickac6d9be2013-05-25 02:42:55 +00005039 setValue(&I, DAG.getNode(ISD::FP16_TO_FP32, sdl,
Gabor Greif0635f352010-06-25 09:38:13 +00005040 MVT::f32, getValue(I.getArgOperand(0))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00005041 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005042 case Intrinsic::pcmarker: {
Gabor Greif0635f352010-06-25 09:38:13 +00005043 SDValue Tmp = getValue(I.getArgOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005044 DAG.setRoot(DAG.getNode(ISD::PCMARKER, sdl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005045 return 0;
5046 }
5047 case Intrinsic::readcyclecounter: {
5048 SDValue Op = getRoot();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005049 Res = DAG.getNode(ISD::READCYCLECOUNTER, sdl,
Bill Wendlingd0283fa2009-12-22 00:40:51 +00005050 DAG.getVTList(MVT::i64, MVT::Other),
5051 &Op, 1);
5052 setValue(&I, Res);
5053 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005054 return 0;
5055 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005056 case Intrinsic::bswap:
Andrew Trickac6d9be2013-05-25 02:42:55 +00005057 setValue(&I, DAG.getNode(ISD::BSWAP, sdl,
Gabor Greif0635f352010-06-25 09:38:13 +00005058 getValue(I.getArgOperand(0)).getValueType(),
5059 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005060 return 0;
5061 case Intrinsic::cttz: {
Gabor Greif0635f352010-06-25 09:38:13 +00005062 SDValue Arg = getValue(I.getArgOperand(0));
Chandler Carruth63974b22011-12-13 01:56:10 +00005063 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00005064 EVT Ty = Arg.getValueType();
Chandler Carruth63974b22011-12-13 01:56:10 +00005065 setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTTZ : ISD::CTTZ_ZERO_UNDEF,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005066 sdl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005067 return 0;
5068 }
5069 case Intrinsic::ctlz: {
Gabor Greif0635f352010-06-25 09:38:13 +00005070 SDValue Arg = getValue(I.getArgOperand(0));
Chandler Carruth63974b22011-12-13 01:56:10 +00005071 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00005072 EVT Ty = Arg.getValueType();
Chandler Carruth63974b22011-12-13 01:56:10 +00005073 setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTLZ : ISD::CTLZ_ZERO_UNDEF,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005074 sdl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005075 return 0;
5076 }
5077 case Intrinsic::ctpop: {
Gabor Greif0635f352010-06-25 09:38:13 +00005078 SDValue Arg = getValue(I.getArgOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00005079 EVT Ty = Arg.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005080 setValue(&I, DAG.getNode(ISD::CTPOP, sdl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005081 return 0;
5082 }
5083 case Intrinsic::stacksave: {
5084 SDValue Op = getRoot();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005085 Res = DAG.getNode(ISD::STACKSAVE, sdl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00005086 DAG.getVTList(TLI->getPointerTy(), MVT::Other), &Op, 1);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00005087 setValue(&I, Res);
5088 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005089 return 0;
5090 }
5091 case Intrinsic::stackrestore: {
Gabor Greif0635f352010-06-25 09:38:13 +00005092 Res = getValue(I.getArgOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005093 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, sdl, MVT::Other, getRoot(), Res));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005094 return 0;
5095 }
Bill Wendling57344502008-11-18 11:01:33 +00005096 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00005097 // Emit code into the DAG to store the stack guard onto the stack.
5098 MachineFunction &MF = DAG.getMachineFunction();
5099 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendlingba54bca2013-06-19 21:36:55 +00005100 EVT PtrTy = TLI->getPointerTy();
Bill Wendlingb2a42982008-11-06 02:29:10 +00005101
Gabor Greif0635f352010-06-25 09:38:13 +00005102 SDValue Src = getValue(I.getArgOperand(0)); // The guard's value.
5103 AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Bill Wendlingb2a42982008-11-06 02:29:10 +00005104
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00005105 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00005106 MFI->setStackProtectorIndex(FI);
5107
5108 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
5109
5110 // Store the stack protector onto the stack.
Andrew Trickac6d9be2013-05-25 02:42:55 +00005111 Res = DAG.getStore(getRoot(), sdl, Src, FIN,
Chris Lattner84bd98a2010-09-21 18:58:22 +00005112 MachinePointerInfo::getFixedStack(FI),
5113 true, false, 0);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00005114 setValue(&I, Res);
5115 DAG.setRoot(Res);
Bill Wendlingb2a42982008-11-06 02:29:10 +00005116 return 0;
5117 }
Eric Christopher7b5e6172009-10-27 00:52:25 +00005118 case Intrinsic::objectsize: {
5119 // If we don't know by now, we're never going to know.
Gabor Greif0635f352010-06-25 09:38:13 +00005120 ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(1));
Eric Christopher7b5e6172009-10-27 00:52:25 +00005121
5122 assert(CI && "Non-constant type in __builtin_object_size?");
5123
Gabor Greif0635f352010-06-25 09:38:13 +00005124 SDValue Arg = getValue(I.getCalledValue());
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00005125 EVT Ty = Arg.getValueType();
5126
Dan Gohmane368b462010-06-18 14:22:04 +00005127 if (CI->isZero())
Bill Wendlingd0283fa2009-12-22 00:40:51 +00005128 Res = DAG.getConstant(-1ULL, Ty);
Eric Christopher7b5e6172009-10-27 00:52:25 +00005129 else
Bill Wendlingd0283fa2009-12-22 00:40:51 +00005130 Res = DAG.getConstant(0, Ty);
5131
5132 setValue(&I, Res);
Eric Christopher7b5e6172009-10-27 00:52:25 +00005133 return 0;
5134 }
Justin Holewinskic2b7f5f2013-05-21 14:37:16 +00005135 case Intrinsic::annotation:
5136 case Intrinsic::ptr_annotation:
5137 // Drop the intrinsic, but forward the value
5138 setValue(&I, getValue(I.getOperand(0)));
5139 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005140 case Intrinsic::var_annotation:
5141 // Discard annotate attributes
5142 return 0;
5143
5144 case Intrinsic::init_trampoline: {
Gabor Greif0635f352010-06-25 09:38:13 +00005145 const Function *F = cast<Function>(I.getArgOperand(1)->stripPointerCasts());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005146
5147 SDValue Ops[6];
5148 Ops[0] = getRoot();
Gabor Greif0635f352010-06-25 09:38:13 +00005149 Ops[1] = getValue(I.getArgOperand(0));
5150 Ops[2] = getValue(I.getArgOperand(1));
5151 Ops[3] = getValue(I.getArgOperand(2));
5152 Ops[4] = DAG.getSrcValue(I.getArgOperand(0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005153 Ops[5] = DAG.getSrcValue(F);
5154
Andrew Trickac6d9be2013-05-25 02:42:55 +00005155 Res = DAG.getNode(ISD::INIT_TRAMPOLINE, sdl, MVT::Other, Ops, 6);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005156
Duncan Sands4a544a72011-09-06 13:37:06 +00005157 DAG.setRoot(Res);
5158 return 0;
5159 }
5160 case Intrinsic::adjust_trampoline: {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005161 setValue(&I, DAG.getNode(ISD::ADJUST_TRAMPOLINE, sdl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00005162 TLI->getPointerTy(),
Duncan Sands4a544a72011-09-06 13:37:06 +00005163 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005164 return 0;
5165 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005166 case Intrinsic::gcroot:
5167 if (GFI) {
Bill Wendling95dd4422012-05-01 22:50:45 +00005168 const Value *Alloca = I.getArgOperand(0)->stripPointerCasts();
Gabor Greif0635f352010-06-25 09:38:13 +00005169 const Constant *TypeMap = cast<Constant>(I.getArgOperand(1));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005170
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005171 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
5172 GFI->addStackRoot(FI->getIndex(), TypeMap);
5173 }
5174 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005175 case Intrinsic::gcread:
5176 case Intrinsic::gcwrite:
Torok Edwinc23197a2009-07-14 16:55:14 +00005177 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
Bill Wendlingd0283fa2009-12-22 00:40:51 +00005178 case Intrinsic::flt_rounds:
Andrew Trickac6d9be2013-05-25 02:42:55 +00005179 setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, sdl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005180 return 0;
Jakub Staszak9da99342011-07-06 18:22:43 +00005181
5182 case Intrinsic::expect: {
5183 // Just replace __builtin_expect(exp, c) with EXP.
5184 setValue(&I, getValue(I.getArgOperand(0)));
5185 return 0;
5186 }
5187
Shuxin Yang970755e2012-10-19 20:11:16 +00005188 case Intrinsic::debugtrap:
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005189 case Intrinsic::trap: {
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005190 StringRef TrapFuncName = TM.Options.getTrapFunctionName();
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005191 if (TrapFuncName.empty()) {
Stephen Lin155615d2013-07-08 00:37:03 +00005192 ISD::NodeType Op = (Intrinsic == Intrinsic::trap) ?
Shuxin Yang970755e2012-10-19 20:11:16 +00005193 ISD::TRAP : ISD::DEBUGTRAP;
Andrew Trickac6d9be2013-05-25 02:42:55 +00005194 DAG.setRoot(DAG.getNode(Op, sdl,MVT::Other, getRoot()));
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005195 return 0;
5196 }
5197 TargetLowering::ArgListTy Args;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00005198 TargetLowering::
5199 CallLoweringInfo CLI(getRoot(), I.getType(),
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005200 false, false, false, false, 0, CallingConv::C,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00005201 /*isTailCall=*/false,
5202 /*doesNotRet=*/false, /*isReturnValueUsed=*/true,
Bill Wendlingba54bca2013-06-19 21:36:55 +00005203 DAG.getExternalSymbol(TrapFuncName.data(),
5204 TLI->getPointerTy()),
Andrew Trickac6d9be2013-05-25 02:42:55 +00005205 Args, DAG, sdl);
Bill Wendlingba54bca2013-06-19 21:36:55 +00005206 std::pair<SDValue, SDValue> Result = TLI->LowerCallTo(CLI);
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005207 DAG.setRoot(Result.second);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005208 return 0;
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005209 }
Shuxin Yang970755e2012-10-19 20:11:16 +00005210
Bill Wendlingef375462008-11-21 02:38:44 +00005211 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00005212 case Intrinsic::sadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00005213 case Intrinsic::usub_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00005214 case Intrinsic::ssub_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00005215 case Intrinsic::umul_with_overflow:
Craig Topperc42e6402012-04-11 04:34:11 +00005216 case Intrinsic::smul_with_overflow: {
5217 ISD::NodeType Op;
5218 switch (Intrinsic) {
5219 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
5220 case Intrinsic::uadd_with_overflow: Op = ISD::UADDO; break;
5221 case Intrinsic::sadd_with_overflow: Op = ISD::SADDO; break;
5222 case Intrinsic::usub_with_overflow: Op = ISD::USUBO; break;
5223 case Intrinsic::ssub_with_overflow: Op = ISD::SSUBO; break;
5224 case Intrinsic::umul_with_overflow: Op = ISD::UMULO; break;
5225 case Intrinsic::smul_with_overflow: Op = ISD::SMULO; break;
5226 }
5227 SDValue Op1 = getValue(I.getArgOperand(0));
5228 SDValue Op2 = getValue(I.getArgOperand(1));
Bill Wendling7cdc3c82008-11-21 02:03:52 +00005229
Craig Topperc42e6402012-04-11 04:34:11 +00005230 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005231 setValue(&I, DAG.getNode(Op, sdl, VTs, Op1, Op2));
Craig Topperc42e6402012-04-11 04:34:11 +00005232 return 0;
5233 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005234 case Intrinsic::prefetch: {
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00005235 SDValue Ops[5];
Dale Johannesen1de4aa92010-10-26 23:11:10 +00005236 unsigned rw = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005237 Ops[0] = getRoot();
Gabor Greif0635f352010-06-25 09:38:13 +00005238 Ops[1] = getValue(I.getArgOperand(0));
5239 Ops[2] = getValue(I.getArgOperand(1));
5240 Ops[3] = getValue(I.getArgOperand(2));
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00005241 Ops[4] = getValue(I.getArgOperand(3));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005242 DAG.setRoot(DAG.getMemIntrinsicNode(ISD::PREFETCH, sdl,
Dale Johannesen1de4aa92010-10-26 23:11:10 +00005243 DAG.getVTList(MVT::Other),
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00005244 &Ops[0], 5,
Dale Johannesen1de4aa92010-10-26 23:11:10 +00005245 EVT::getIntegerVT(*Context, 8),
5246 MachinePointerInfo(I.getArgOperand(0)),
5247 0, /* align */
5248 false, /* volatile */
5249 rw==0, /* read */
5250 rw==1)); /* write */
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005251 return 0;
5252 }
Duncan Sandsf07c9492009-11-10 09:08:09 +00005253 case Intrinsic::lifetime_start:
Nadav Rotemc05d3062012-09-06 09:17:37 +00005254 case Intrinsic::lifetime_end: {
Nadav Rotemc05d3062012-09-06 09:17:37 +00005255 bool IsStart = (Intrinsic == Intrinsic::lifetime_start);
Nadav Rotem9a2ae002012-09-10 08:43:23 +00005256 // Stack coloring is not enabled in O0, discard region information.
5257 if (TM.getOptLevel() == CodeGenOpt::None)
5258 return 0;
Nadav Rotemc05d3062012-09-06 09:17:37 +00005259
Nadav Rotem9a2ae002012-09-10 08:43:23 +00005260 SmallVector<Value *, 4> Allocas;
5261 GetUnderlyingObjects(I.getArgOperand(1), Allocas, TD);
5262
Craig Topperf22fd3f2013-07-03 05:11:49 +00005263 for (SmallVectorImpl<Value*>::iterator Object = Allocas.begin(),
5264 E = Allocas.end(); Object != E; ++Object) {
Nadav Rotem9a2ae002012-09-10 08:43:23 +00005265 AllocaInst *LifetimeObject = dyn_cast_or_null<AllocaInst>(*Object);
5266
5267 // Could not find an Alloca.
5268 if (!LifetimeObject)
5269 continue;
5270
5271 int FI = FuncInfo.StaticAllocaMap[LifetimeObject];
5272
5273 SDValue Ops[2];
5274 Ops[0] = getRoot();
Bill Wendlingba54bca2013-06-19 21:36:55 +00005275 Ops[1] = DAG.getFrameIndex(FI, TLI->getPointerTy(), true);
Nadav Rotem9a2ae002012-09-10 08:43:23 +00005276 unsigned Opcode = (IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END);
5277
Andrew Trickac6d9be2013-05-25 02:42:55 +00005278 Res = DAG.getNode(Opcode, sdl, MVT::Other, Ops, 2);
Nadav Rotem9a2ae002012-09-10 08:43:23 +00005279 DAG.setRoot(Res);
5280 }
Nadav Rotem5882e562013-02-01 19:25:23 +00005281 return 0;
Nadav Rotemc05d3062012-09-06 09:17:37 +00005282 }
5283 case Intrinsic::invariant_start:
Duncan Sandsf07c9492009-11-10 09:08:09 +00005284 // Discard region information.
Bill Wendlingba54bca2013-06-19 21:36:55 +00005285 setValue(&I, DAG.getUNDEF(TLI->getPointerTy()));
Duncan Sandsf07c9492009-11-10 09:08:09 +00005286 return 0;
5287 case Intrinsic::invariant_end:
Duncan Sandsf07c9492009-11-10 09:08:09 +00005288 // Discard region information.
5289 return 0;
Michael Gottesman657484f2013-08-20 07:00:16 +00005290 case Intrinsic::stackprotectorcheck: {
5291 // Do not actually emit anything for this basic block. Instead we initialize
5292 // the stack protector descriptor and export the guard variable so we can
5293 // access it in FinishBasicBlock.
5294 const BasicBlock *BB = I.getParent();
5295 SPDescriptor.initialize(BB, FuncInfo.MBBMap[BB], I);
5296 ExportFromCurrentBlock(SPDescriptor.getGuard());
5297
5298 // Flush our exports since we are going to process a terminator.
5299 (void)getControlRoot();
5300 return 0;
5301 }
Nuno Lopes85b40892012-06-28 22:30:12 +00005302 case Intrinsic::donothing:
5303 // ignore
5304 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005305 }
5306}
5307
Dan Gohman46510a72010-04-15 01:51:59 +00005308void SelectionDAGBuilder::LowerCallTo(ImmutableCallSite CS, SDValue Callee,
Dan Gohman2048b852009-11-23 18:04:58 +00005309 bool isTailCall,
5310 MachineBasicBlock *LandingPad) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005311 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
5312 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
5313 Type *RetTy = FTy->getReturnType();
Chris Lattner512063d2010-04-05 06:19:28 +00005314 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Chris Lattner16112732010-03-14 01:41:15 +00005315 MCSymbol *BeginLabel = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005316
5317 TargetLowering::ArgListTy Args;
5318 TargetLowering::ArgListEntry Entry;
5319 Args.reserve(CS.arg_size());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005320
5321 // Check whether the function can return without sret-demotion.
Dan Gohman84023e02010-07-10 09:00:22 +00005322 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00005323 const TargetLowering *TLI = TM.getTargetLowering();
5324 GetReturnInfo(RetTy, CS.getAttributes(), Outs, *TLI);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005325
Bill Wendlingba54bca2013-06-19 21:36:55 +00005326 bool CanLowerReturn = TLI->CanLowerReturn(CS.getCallingConv(),
5327 DAG.getMachineFunction(),
5328 FTy->isVarArg(), Outs,
5329 FTy->getContext());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005330
5331 SDValue DemoteStackSlot;
Chris Lattnerecf42c42010-09-21 16:36:31 +00005332 int DemoteStackIdx = -100;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005333
5334 if (!CanLowerReturn) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00005335 uint64_t TySize = TLI->getDataLayout()->getTypeAllocSize(
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005336 FTy->getReturnType());
Bill Wendlingba54bca2013-06-19 21:36:55 +00005337 unsigned Align = TLI->getDataLayout()->getPrefTypeAlignment(
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005338 FTy->getReturnType());
5339 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattnerecf42c42010-09-21 16:36:31 +00005340 DemoteStackIdx = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005341 Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005342
Bill Wendlingba54bca2013-06-19 21:36:55 +00005343 DemoteStackSlot = DAG.getFrameIndex(DemoteStackIdx, TLI->getPointerTy());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005344 Entry.Node = DemoteStackSlot;
5345 Entry.Ty = StackSlotPtrType;
5346 Entry.isSExt = false;
5347 Entry.isZExt = false;
5348 Entry.isInReg = false;
5349 Entry.isSRet = true;
5350 Entry.isNest = false;
5351 Entry.isByVal = false;
Stephen Lin456ca042013-04-20 05:14:40 +00005352 Entry.isReturned = false;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005353 Entry.Alignment = Align;
5354 Args.push_back(Entry);
5355 RetTy = Type::getVoidTy(FTy->getContext());
5356 }
5357
Dan Gohman46510a72010-04-15 01:51:59 +00005358 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005359 i != e; ++i) {
Rafael Espindola3fa82832011-05-13 15:18:06 +00005360 const Value *V = *i;
5361
5362 // Skip empty types
5363 if (V->getType()->isEmptyTy())
5364 continue;
5365
5366 SDValue ArgNode = getValue(V);
5367 Entry.Node = ArgNode; Entry.Ty = V->getType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005368
5369 unsigned attrInd = i - CS.arg_begin() + 1;
Stephen Lin456ca042013-04-20 05:14:40 +00005370 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
5371 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
5372 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
5373 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
5374 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
5375 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
5376 Entry.isReturned = CS.paramHasAttr(attrInd, Attribute::Returned);
5377 Entry.Alignment = CS.getParamAlignment(attrInd);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005378 Args.push_back(Entry);
5379 }
5380
Chris Lattner512063d2010-04-05 06:19:28 +00005381 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005382 // Insert a label before the invoke call to mark the try range. This can be
5383 // used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00005384 BeginLabel = MMI.getContext().CreateTempSymbol();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00005385
Jim Grosbachca752c92010-01-28 01:45:32 +00005386 // For SjLj, keep track of which landing pads go with which invokes
5387 // so as to maintain the ordering of pads in the LSDA.
Chris Lattner512063d2010-04-05 06:19:28 +00005388 unsigned CallSiteIndex = MMI.getCurrentCallSite();
Jim Grosbachca752c92010-01-28 01:45:32 +00005389 if (CallSiteIndex) {
Chris Lattner512063d2010-04-05 06:19:28 +00005390 MMI.setCallSiteBeginLabel(BeginLabel, CallSiteIndex);
Bill Wendling30e67402011-10-05 22:24:35 +00005391 LPadToCallSiteMap[LandingPad].push_back(CallSiteIndex);
Bill Wendlinga8512ed2011-10-04 22:00:35 +00005392
Jim Grosbachca752c92010-01-28 01:45:32 +00005393 // Now that the call site is handled, stop tracking it.
Chris Lattner512063d2010-04-05 06:19:28 +00005394 MMI.setCurrentCallSite(0);
Jim Grosbachca752c92010-01-28 01:45:32 +00005395 }
5396
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005397 // Both PendingLoads and PendingExports must be flushed here;
5398 // this call might not return.
5399 (void)getRoot();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005400 DAG.setRoot(DAG.getEHLabel(getCurSDLoc(), getControlRoot(), BeginLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005401 }
5402
Dan Gohman98ca4f22009-08-05 01:29:28 +00005403 // Check if target-independent constraints permit a tail call here.
Bill Wendlingba54bca2013-06-19 21:36:55 +00005404 // Target-dependent constraints are checked within TLI->LowerCallTo.
5405 if (isTailCall && !isInTailCallPosition(CS, *TLI))
Dan Gohman98ca4f22009-08-05 01:29:28 +00005406 isTailCall = false;
5407
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00005408 TargetLowering::
5409 CallLoweringInfo CLI(getRoot(), RetTy, FTy, isTailCall, Callee, Args, DAG,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005410 getCurSDLoc(), CS);
Bill Wendlingba54bca2013-06-19 21:36:55 +00005411 std::pair<SDValue,SDValue> Result = TLI->LowerCallTo(CLI);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005412 assert((isTailCall || Result.second.getNode()) &&
5413 "Non-null chain expected with non-tail call!");
5414 assert((Result.second.getNode() || !Result.first.getNode()) &&
5415 "Null value expected with tail call!");
Bill Wendlinge80ae832009-12-22 00:50:32 +00005416 if (Result.first.getNode()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005417 setValue(CS.getInstruction(), Result.first);
Bill Wendlinge80ae832009-12-22 00:50:32 +00005418 } else if (!CanLowerReturn && Result.second.getNode()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005419 // The instruction result is the result of loading from the
5420 // hidden sret parameter.
5421 SmallVector<EVT, 1> PVTs;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005422 Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005423
Bill Wendlingba54bca2013-06-19 21:36:55 +00005424 ComputeValueVTs(*TLI, PtrRetTy, PVTs);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005425 assert(PVTs.size() == 1 && "Pointers should fit in one register");
5426 EVT PtrVT = PVTs[0];
Eli Friedman2db0e9e2012-05-25 00:09:29 +00005427
5428 SmallVector<EVT, 4> RetTys;
5429 SmallVector<uint64_t, 4> Offsets;
5430 RetTy = FTy->getReturnType();
Bill Wendlingba54bca2013-06-19 21:36:55 +00005431 ComputeValueVTs(*TLI, RetTy, RetTys, &Offsets);
Eli Friedman2db0e9e2012-05-25 00:09:29 +00005432
5433 unsigned NumValues = RetTys.size();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005434 SmallVector<SDValue, 4> Values(NumValues);
5435 SmallVector<SDValue, 4> Chains(NumValues);
5436
5437 for (unsigned i = 0; i < NumValues; ++i) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005438 SDValue Add = DAG.getNode(ISD::ADD, getCurSDLoc(), PtrVT,
Bill Wendlinge80ae832009-12-22 00:50:32 +00005439 DemoteStackSlot,
5440 DAG.getConstant(Offsets[i], PtrVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005441 SDValue L = DAG.getLoad(RetTys[i], getCurSDLoc(), Result.second, Add,
Chris Lattnerecf42c42010-09-21 16:36:31 +00005442 MachinePointerInfo::getFixedStack(DemoteStackIdx, Offsets[i]),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005443 false, false, false, 1);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005444 Values[i] = L;
5445 Chains[i] = L.getValue(1);
5446 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00005447
Andrew Trickac6d9be2013-05-25 02:42:55 +00005448 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005449 MVT::Other, &Chains[0], NumValues);
5450 PendingLoads.push_back(Chain);
Michael J. Spencere70c5262010-10-16 08:25:21 +00005451
Bill Wendling4533cac2010-01-28 21:51:40 +00005452 setValue(CS.getInstruction(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00005453 DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00005454 DAG.getVTList(&RetTys[0], RetTys.size()),
Eli Friedman2db0e9e2012-05-25 00:09:29 +00005455 &Values[0], Values.size()));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005456 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00005457
Evan Cheng8380c032011-04-01 19:42:22 +00005458 if (!Result.second.getNode()) {
Evan Chengc249e482011-04-01 19:57:01 +00005459 // As a special case, a null chain means that a tail call has been emitted and
5460 // the DAG root is already updated.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005461 HasTailCall = true;
Tim Northovere5a81a12013-07-06 12:58:45 +00005462
5463 // Since there's no actual continuation from this block, nothing can be
5464 // relying on us setting vregs for them.
5465 PendingExports.clear();
Evan Cheng8380c032011-04-01 19:42:22 +00005466 } else {
5467 DAG.setRoot(Result.second);
Evan Cheng8380c032011-04-01 19:42:22 +00005468 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005469
Chris Lattner512063d2010-04-05 06:19:28 +00005470 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005471 // Insert a label at the end of the invoke call to mark the try range. This
5472 // can be used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00005473 MCSymbol *EndLabel = MMI.getContext().CreateTempSymbol();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005474 DAG.setRoot(DAG.getEHLabel(getCurSDLoc(), getRoot(), EndLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005475
5476 // Inform MachineModuleInfo of range.
Chris Lattner512063d2010-04-05 06:19:28 +00005477 MMI.addInvoke(LandingPad, BeginLabel, EndLabel);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005478 }
5479}
5480
Chris Lattner8047d9a2009-12-24 00:37:38 +00005481/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
5482/// value is equal or not-equal to zero.
Dan Gohman46510a72010-04-15 01:51:59 +00005483static bool IsOnlyUsedInZeroEqualityComparison(const Value *V) {
5484 for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end();
Chris Lattner8047d9a2009-12-24 00:37:38 +00005485 UI != E; ++UI) {
Dan Gohman46510a72010-04-15 01:51:59 +00005486 if (const ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
Chris Lattner8047d9a2009-12-24 00:37:38 +00005487 if (IC->isEquality())
Dan Gohman46510a72010-04-15 01:51:59 +00005488 if (const Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
Chris Lattner8047d9a2009-12-24 00:37:38 +00005489 if (C->isNullValue())
5490 continue;
5491 // Unknown instruction.
5492 return false;
5493 }
5494 return true;
5495}
5496
Dan Gohman46510a72010-04-15 01:51:59 +00005497static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005498 Type *LoadTy,
Chris Lattner8047d9a2009-12-24 00:37:38 +00005499 SelectionDAGBuilder &Builder) {
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005500
Chris Lattner8047d9a2009-12-24 00:37:38 +00005501 // Check to see if this load can be trivially constant folded, e.g. if the
5502 // input is from a string literal.
Dan Gohman46510a72010-04-15 01:51:59 +00005503 if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00005504 // Cast pointer to the type we really want to load.
Dan Gohman46510a72010-04-15 01:51:59 +00005505 LoadInput = ConstantExpr::getBitCast(const_cast<Constant *>(LoadInput),
Chris Lattner8047d9a2009-12-24 00:37:38 +00005506 PointerType::getUnqual(LoadTy));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005507
Dan Gohman46510a72010-04-15 01:51:59 +00005508 if (const Constant *LoadCst =
5509 ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput),
5510 Builder.TD))
Chris Lattner8047d9a2009-12-24 00:37:38 +00005511 return Builder.getValue(LoadCst);
5512 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005513
Chris Lattner8047d9a2009-12-24 00:37:38 +00005514 // Otherwise, we have to emit the load. If the pointer is to unfoldable but
5515 // still constant memory, the input chain can be the entry node.
5516 SDValue Root;
5517 bool ConstantMemory = false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005518
Chris Lattner8047d9a2009-12-24 00:37:38 +00005519 // Do not serialize (non-volatile) loads of constant memory with anything.
5520 if (Builder.AA->pointsToConstantMemory(PtrVal)) {
5521 Root = Builder.DAG.getEntryNode();
5522 ConstantMemory = true;
5523 } else {
5524 // Do not serialize non-volatile loads against each other.
5525 Root = Builder.DAG.getRoot();
5526 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005527
Chris Lattner8047d9a2009-12-24 00:37:38 +00005528 SDValue Ptr = Builder.getValue(PtrVal);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005529 SDValue LoadVal = Builder.DAG.getLoad(LoadVT, Builder.getCurSDLoc(), Root,
Chris Lattnerecf42c42010-09-21 16:36:31 +00005530 Ptr, MachinePointerInfo(PtrVal),
David Greene1e559442010-02-15 17:00:31 +00005531 false /*volatile*/,
Stephen Lin155615d2013-07-08 00:37:03 +00005532 false /*nontemporal*/,
Pete Cooperd752e0f2011-11-08 18:42:53 +00005533 false /*isinvariant*/, 1 /* align=1 */);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005534
Chris Lattner8047d9a2009-12-24 00:37:38 +00005535 if (!ConstantMemory)
5536 Builder.PendingLoads.push_back(LoadVal.getValue(1));
5537 return LoadVal;
5538}
5539
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005540/// processIntegerCallValue - Record the value for an instruction that
5541/// produces an integer result, converting the type where necessary.
5542void SelectionDAGBuilder::processIntegerCallValue(const Instruction &I,
5543 SDValue Value,
5544 bool IsSigned) {
5545 EVT VT = TM.getTargetLowering()->getValueType(I.getType(), true);
5546 if (IsSigned)
5547 Value = DAG.getSExtOrTrunc(Value, getCurSDLoc(), VT);
5548 else
5549 Value = DAG.getZExtOrTrunc(Value, getCurSDLoc(), VT);
5550 setValue(&I, Value);
5551}
Chris Lattner8047d9a2009-12-24 00:37:38 +00005552
5553/// visitMemCmpCall - See if we can lower a call to memcmp in an optimized form.
5554/// If so, return true and lower it, otherwise return false and it will be
5555/// lowered like a normal call.
Dan Gohman46510a72010-04-15 01:51:59 +00005556bool SelectionDAGBuilder::visitMemCmpCall(const CallInst &I) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00005557 // Verify that the prototype makes sense. int memcmp(void*,void*,size_t)
Gabor Greif37387d52010-06-30 12:55:46 +00005558 if (I.getNumArgOperands() != 3)
Chris Lattner8047d9a2009-12-24 00:37:38 +00005559 return false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005560
Gabor Greif0635f352010-06-25 09:38:13 +00005561 const Value *LHS = I.getArgOperand(0), *RHS = I.getArgOperand(1);
Duncan Sands1df98592010-02-16 11:11:14 +00005562 if (!LHS->getType()->isPointerTy() || !RHS->getType()->isPointerTy() ||
Gabor Greif0635f352010-06-25 09:38:13 +00005563 !I.getArgOperand(2)->getType()->isIntegerTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +00005564 !I.getType()->isIntegerTy())
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005565 return false;
5566
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005567 const Value *Size = I.getArgOperand(2);
5568 const ConstantInt *CSize = dyn_cast<ConstantInt>(Size);
5569 if (CSize && CSize->getZExtValue() == 0) {
Richard Sandifordac168b82013-08-12 10:28:10 +00005570 EVT CallVT = TM.getTargetLowering()->getValueType(I.getType(), true);
5571 setValue(&I, DAG.getConstant(0, CallVT));
5572 return true;
5573 }
5574
Richard Sandifordac168b82013-08-12 10:28:10 +00005575 const TargetSelectionDAGInfo &TSI = DAG.getSelectionDAGInfo();
5576 std::pair<SDValue, SDValue> Res =
5577 TSI.EmitTargetCodeForMemcmp(DAG, getCurSDLoc(), DAG.getRoot(),
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005578 getValue(LHS), getValue(RHS), getValue(Size),
5579 MachinePointerInfo(LHS),
5580 MachinePointerInfo(RHS));
Richard Sandifordac168b82013-08-12 10:28:10 +00005581 if (Res.first.getNode()) {
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005582 processIntegerCallValue(I, Res.first, true);
5583 PendingLoads.push_back(Res.second);
Richard Sandifordac168b82013-08-12 10:28:10 +00005584 return true;
5585 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005586
Chris Lattner8047d9a2009-12-24 00:37:38 +00005587 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
5588 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005589 if (CSize && IsOnlyUsedInZeroEqualityComparison(&I)) {
Chris Lattner04b091a2009-12-24 01:07:17 +00005590 bool ActuallyDoIt = true;
5591 MVT LoadVT;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005592 Type *LoadTy;
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005593 switch (CSize->getZExtValue()) {
Chris Lattner04b091a2009-12-24 01:07:17 +00005594 default:
5595 LoadVT = MVT::Other;
5596 LoadTy = 0;
5597 ActuallyDoIt = false;
5598 break;
5599 case 2:
5600 LoadVT = MVT::i16;
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005601 LoadTy = Type::getInt16Ty(CSize->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005602 break;
5603 case 4:
5604 LoadVT = MVT::i32;
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005605 LoadTy = Type::getInt32Ty(CSize->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005606 break;
5607 case 8:
5608 LoadVT = MVT::i64;
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005609 LoadTy = Type::getInt64Ty(CSize->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005610 break;
5611 /*
5612 case 16:
5613 LoadVT = MVT::v4i32;
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005614 LoadTy = Type::getInt32Ty(CSize->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005615 LoadTy = VectorType::get(LoadTy, 4);
5616 break;
5617 */
5618 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005619
Chris Lattner04b091a2009-12-24 01:07:17 +00005620 // This turns into unaligned loads. We only do this if the target natively
5621 // supports the MVT we'll be loading or if it is small enough (<= 4) that
5622 // we'll only produce a small number of byte loads.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005623
Chris Lattner04b091a2009-12-24 01:07:17 +00005624 // Require that we can find a legal MVT, and only do this if the target
5625 // supports unaligned loads of that type. Expanding into byte loads would
5626 // bloat the code.
Bill Wendlingba54bca2013-06-19 21:36:55 +00005627 const TargetLowering *TLI = TM.getTargetLowering();
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005628 if (ActuallyDoIt && CSize->getZExtValue() > 4) {
Chris Lattner04b091a2009-12-24 01:07:17 +00005629 // TODO: Handle 5 byte compare as 4-byte + 1 byte.
5630 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
Bill Wendlingba54bca2013-06-19 21:36:55 +00005631 if (!TLI->isTypeLegal(LoadVT) ||!TLI->allowsUnalignedMemoryAccesses(LoadVT))
Chris Lattner04b091a2009-12-24 01:07:17 +00005632 ActuallyDoIt = false;
5633 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005634
Chris Lattner04b091a2009-12-24 01:07:17 +00005635 if (ActuallyDoIt) {
5636 SDValue LHSVal = getMemCmpLoad(LHS, LoadVT, LoadTy, *this);
5637 SDValue RHSVal = getMemCmpLoad(RHS, LoadVT, LoadTy, *this);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005638
Andrew Trickac6d9be2013-05-25 02:42:55 +00005639 SDValue Res = DAG.getSetCC(getCurSDLoc(), MVT::i1, LHSVal, RHSVal,
Chris Lattner04b091a2009-12-24 01:07:17 +00005640 ISD::SETNE);
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005641 processIntegerCallValue(I, Res, false);
Chris Lattner04b091a2009-12-24 01:07:17 +00005642 return true;
5643 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00005644 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005645
5646
Chris Lattner8047d9a2009-12-24 00:37:38 +00005647 return false;
5648}
5649
Richard Sandiford4fc73552013-08-16 11:29:37 +00005650/// visitStrCpyCall -- See if we can lower a strcpy or stpcpy call into an
5651/// optimized form. If so, return true and lower it, otherwise return false
5652/// and it will be lowered like a normal call.
5653bool SelectionDAGBuilder::visitStrCpyCall(const CallInst &I, bool isStpcpy) {
5654 // Verify that the prototype makes sense. char *strcpy(char *, char *)
5655 if (I.getNumArgOperands() != 2)
5656 return false;
5657
5658 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
5659 if (!Arg0->getType()->isPointerTy() ||
5660 !Arg1->getType()->isPointerTy() ||
5661 !I.getType()->isPointerTy())
5662 return false;
5663
5664 const TargetSelectionDAGInfo &TSI = DAG.getSelectionDAGInfo();
5665 std::pair<SDValue, SDValue> Res =
5666 TSI.EmitTargetCodeForStrcpy(DAG, getCurSDLoc(), getRoot(),
5667 getValue(Arg0), getValue(Arg1),
5668 MachinePointerInfo(Arg0),
5669 MachinePointerInfo(Arg1), isStpcpy);
5670 if (Res.first.getNode()) {
5671 setValue(&I, Res.first);
5672 DAG.setRoot(Res.second);
5673 return true;
5674 }
5675
5676 return false;
5677}
5678
Richard Sandiforde1b2af72013-08-16 11:21:54 +00005679/// visitStrCmpCall - See if we can lower a call to strcmp in an optimized form.
5680/// If so, return true and lower it, otherwise return false and it will be
5681/// lowered like a normal call.
5682bool SelectionDAGBuilder::visitStrCmpCall(const CallInst &I) {
5683 // Verify that the prototype makes sense. int strcmp(void*,void*)
5684 if (I.getNumArgOperands() != 2)
5685 return false;
5686
5687 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
5688 if (!Arg0->getType()->isPointerTy() ||
5689 !Arg1->getType()->isPointerTy() ||
5690 !I.getType()->isIntegerTy())
5691 return false;
5692
5693 const TargetSelectionDAGInfo &TSI = DAG.getSelectionDAGInfo();
5694 std::pair<SDValue, SDValue> Res =
5695 TSI.EmitTargetCodeForStrcmp(DAG, getCurSDLoc(), DAG.getRoot(),
5696 getValue(Arg0), getValue(Arg1),
5697 MachinePointerInfo(Arg0),
5698 MachinePointerInfo(Arg1));
5699 if (Res.first.getNode()) {
5700 processIntegerCallValue(I, Res.first, true);
5701 PendingLoads.push_back(Res.second);
5702 return true;
5703 }
5704
5705 return false;
5706}
5707
Richard Sandiford19262ee2013-08-16 11:41:43 +00005708/// visitStrLenCall -- See if we can lower a strlen call into an optimized
5709/// form. If so, return true and lower it, otherwise return false and it
5710/// will be lowered like a normal call.
5711bool SelectionDAGBuilder::visitStrLenCall(const CallInst &I) {
5712 // Verify that the prototype makes sense. size_t strlen(char *)
5713 if (I.getNumArgOperands() != 1)
5714 return false;
5715
5716 const Value *Arg0 = I.getArgOperand(0);
5717 if (!Arg0->getType()->isPointerTy() || !I.getType()->isIntegerTy())
5718 return false;
5719
5720 const TargetSelectionDAGInfo &TSI = DAG.getSelectionDAGInfo();
5721 std::pair<SDValue, SDValue> Res =
5722 TSI.EmitTargetCodeForStrlen(DAG, getCurSDLoc(), DAG.getRoot(),
5723 getValue(Arg0), MachinePointerInfo(Arg0));
5724 if (Res.first.getNode()) {
5725 processIntegerCallValue(I, Res.first, false);
5726 PendingLoads.push_back(Res.second);
5727 return true;
5728 }
5729
5730 return false;
5731}
5732
5733/// visitStrNLenCall -- See if we can lower a strnlen call into an optimized
5734/// form. If so, return true and lower it, otherwise return false and it
5735/// will be lowered like a normal call.
5736bool SelectionDAGBuilder::visitStrNLenCall(const CallInst &I) {
5737 // Verify that the prototype makes sense. size_t strnlen(char *, size_t)
5738 if (I.getNumArgOperands() != 2)
5739 return false;
5740
5741 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
5742 if (!Arg0->getType()->isPointerTy() ||
5743 !Arg1->getType()->isIntegerTy() ||
5744 !I.getType()->isIntegerTy())
5745 return false;
5746
5747 const TargetSelectionDAGInfo &TSI = DAG.getSelectionDAGInfo();
5748 std::pair<SDValue, SDValue> Res =
5749 TSI.EmitTargetCodeForStrnlen(DAG, getCurSDLoc(), DAG.getRoot(),
5750 getValue(Arg0), getValue(Arg1),
5751 MachinePointerInfo(Arg0));
5752 if (Res.first.getNode()) {
5753 processIntegerCallValue(I, Res.first, false);
5754 PendingLoads.push_back(Res.second);
5755 return true;
5756 }
5757
5758 return false;
5759}
5760
Bob Wilson53624a22012-08-03 23:29:17 +00005761/// visitUnaryFloatCall - If a call instruction is a unary floating-point
5762/// operation (as expected), translate it to an SDNode with the specified opcode
5763/// and return true.
5764bool SelectionDAGBuilder::visitUnaryFloatCall(const CallInst &I,
5765 unsigned Opcode) {
5766 // Sanity check that it really is a unary floating-point call.
5767 if (I.getNumArgOperands() != 1 ||
5768 !I.getArgOperand(0)->getType()->isFloatingPointTy() ||
5769 I.getType() != I.getArgOperand(0)->getType() ||
5770 !I.onlyReadsMemory())
5771 return false;
5772
5773 SDValue Tmp = getValue(I.getArgOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005774 setValue(&I, DAG.getNode(Opcode, getCurSDLoc(), Tmp.getValueType(), Tmp));
Bob Wilson53624a22012-08-03 23:29:17 +00005775 return true;
5776}
Chris Lattner8047d9a2009-12-24 00:37:38 +00005777
Dan Gohman46510a72010-04-15 01:51:59 +00005778void SelectionDAGBuilder::visitCall(const CallInst &I) {
Chris Lattner598751e2010-07-05 05:36:21 +00005779 // Handle inline assembly differently.
5780 if (isa<InlineAsm>(I.getCalledValue())) {
5781 visitInlineAsm(&I);
5782 return;
5783 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00005784
Michael J. Spencer391b43b2010-10-21 20:49:23 +00005785 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Michael J. Spencerc9c137b2012-02-22 19:06:13 +00005786 ComputeUsesVAFloatArgument(I, &MMI);
Michael J. Spencer391b43b2010-10-21 20:49:23 +00005787
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005788 const char *RenameFn = 0;
5789 if (Function *F = I.getCalledFunction()) {
5790 if (F->isDeclaration()) {
Chris Lattner598751e2010-07-05 05:36:21 +00005791 if (const TargetIntrinsicInfo *II = TM.getIntrinsicInfo()) {
Dale Johannesen49de9822009-02-05 01:49:45 +00005792 if (unsigned IID = II->getIntrinsicID(F)) {
5793 RenameFn = visitIntrinsicCall(I, IID);
5794 if (!RenameFn)
5795 return;
5796 }
5797 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005798 if (unsigned IID = F->getIntrinsicID()) {
5799 RenameFn = visitIntrinsicCall(I, IID);
5800 if (!RenameFn)
5801 return;
5802 }
5803 }
5804
5805 // Check for well-known libc/libm calls. If the function is internal, it
5806 // can't be a library call.
Bob Wilson982dc842012-08-03 21:26:24 +00005807 LibFunc::Func Func;
5808 if (!F->hasLocalLinkage() && F->hasName() &&
5809 LibInfo->getLibFunc(F->getName(), Func) &&
5810 LibInfo->hasOptimizedCodeGen(Func)) {
5811 switch (Func) {
5812 default: break;
5813 case LibFunc::copysign:
5814 case LibFunc::copysignf:
5815 case LibFunc::copysignl:
Gabor Greif37387d52010-06-30 12:55:46 +00005816 if (I.getNumArgOperands() == 2 && // Basic sanity checks.
Gabor Greif0635f352010-06-25 09:38:13 +00005817 I.getArgOperand(0)->getType()->isFloatingPointTy() &&
5818 I.getType() == I.getArgOperand(0)->getType() &&
Bob Wilson53624a22012-08-03 23:29:17 +00005819 I.getType() == I.getArgOperand(1)->getType() &&
5820 I.onlyReadsMemory()) {
Gabor Greif0635f352010-06-25 09:38:13 +00005821 SDValue LHS = getValue(I.getArgOperand(0));
5822 SDValue RHS = getValue(I.getArgOperand(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005823 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurSDLoc(),
Bill Wendling0d580132009-12-23 01:28:19 +00005824 LHS.getValueType(), LHS, RHS));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005825 return;
5826 }
Bob Wilson982dc842012-08-03 21:26:24 +00005827 break;
5828 case LibFunc::fabs:
5829 case LibFunc::fabsf:
5830 case LibFunc::fabsl:
Bob Wilson53624a22012-08-03 23:29:17 +00005831 if (visitUnaryFloatCall(I, ISD::FABS))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005832 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005833 break;
5834 case LibFunc::sin:
5835 case LibFunc::sinf:
5836 case LibFunc::sinl:
Bob Wilson53624a22012-08-03 23:29:17 +00005837 if (visitUnaryFloatCall(I, ISD::FSIN))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005838 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005839 break;
5840 case LibFunc::cos:
5841 case LibFunc::cosf:
5842 case LibFunc::cosl:
Bob Wilson53624a22012-08-03 23:29:17 +00005843 if (visitUnaryFloatCall(I, ISD::FCOS))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005844 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005845 break;
5846 case LibFunc::sqrt:
5847 case LibFunc::sqrtf:
5848 case LibFunc::sqrtl:
Preston Gurdb704d232013-05-27 15:44:35 +00005849 case LibFunc::sqrt_finite:
5850 case LibFunc::sqrtf_finite:
5851 case LibFunc::sqrtl_finite:
Bob Wilson53624a22012-08-03 23:29:17 +00005852 if (visitUnaryFloatCall(I, ISD::FSQRT))
Dale Johannesen52fb79b2009-09-25 17:23:22 +00005853 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005854 break;
5855 case LibFunc::floor:
5856 case LibFunc::floorf:
5857 case LibFunc::floorl:
Bob Wilson53624a22012-08-03 23:29:17 +00005858 if (visitUnaryFloatCall(I, ISD::FFLOOR))
Owen Anderson4a4fdf32011-12-08 19:32:14 +00005859 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005860 break;
5861 case LibFunc::nearbyint:
5862 case LibFunc::nearbyintf:
5863 case LibFunc::nearbyintl:
Bob Wilson53624a22012-08-03 23:29:17 +00005864 if (visitUnaryFloatCall(I, ISD::FNEARBYINT))
Owen Anderson4a4fdf32011-12-08 19:32:14 +00005865 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005866 break;
5867 case LibFunc::ceil:
5868 case LibFunc::ceilf:
5869 case LibFunc::ceill:
Bob Wilson53624a22012-08-03 23:29:17 +00005870 if (visitUnaryFloatCall(I, ISD::FCEIL))
Owen Anderson4a4fdf32011-12-08 19:32:14 +00005871 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005872 break;
5873 case LibFunc::rint:
5874 case LibFunc::rintf:
5875 case LibFunc::rintl:
Bob Wilson53624a22012-08-03 23:29:17 +00005876 if (visitUnaryFloatCall(I, ISD::FRINT))
Owen Anderson4a4fdf32011-12-08 19:32:14 +00005877 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005878 break;
Hal Finkel41418d12013-08-07 22:49:12 +00005879 case LibFunc::round:
5880 case LibFunc::roundf:
5881 case LibFunc::roundl:
5882 if (visitUnaryFloatCall(I, ISD::FROUND))
5883 return;
5884 break;
Bob Wilson982dc842012-08-03 21:26:24 +00005885 case LibFunc::trunc:
5886 case LibFunc::truncf:
5887 case LibFunc::truncl:
Bob Wilson53624a22012-08-03 23:29:17 +00005888 if (visitUnaryFloatCall(I, ISD::FTRUNC))
Owen Anderson4a4fdf32011-12-08 19:32:14 +00005889 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005890 break;
5891 case LibFunc::log2:
5892 case LibFunc::log2f:
5893 case LibFunc::log2l:
Bob Wilson53624a22012-08-03 23:29:17 +00005894 if (visitUnaryFloatCall(I, ISD::FLOG2))
Owen Anderson4e0adfa2011-12-15 00:54:12 +00005895 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005896 break;
5897 case LibFunc::exp2:
5898 case LibFunc::exp2f:
5899 case LibFunc::exp2l:
Bob Wilson53624a22012-08-03 23:29:17 +00005900 if (visitUnaryFloatCall(I, ISD::FEXP2))
Owen Anderson4e0adfa2011-12-15 00:54:12 +00005901 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005902 break;
5903 case LibFunc::memcmp:
Chris Lattner8047d9a2009-12-24 00:37:38 +00005904 if (visitMemCmpCall(I))
5905 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005906 break;
Richard Sandiford4fc73552013-08-16 11:29:37 +00005907 case LibFunc::strcpy:
5908 if (visitStrCpyCall(I, false))
5909 return;
5910 break;
5911 case LibFunc::stpcpy:
5912 if (visitStrCpyCall(I, true))
5913 return;
5914 break;
Richard Sandiforde1b2af72013-08-16 11:21:54 +00005915 case LibFunc::strcmp:
5916 if (visitStrCmpCall(I))
5917 return;
5918 break;
Richard Sandiford19262ee2013-08-16 11:41:43 +00005919 case LibFunc::strlen:
5920 if (visitStrLenCall(I))
5921 return;
5922 break;
5923 case LibFunc::strnlen:
5924 if (visitStrNLenCall(I))
5925 return;
5926 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005927 }
5928 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005929 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00005930
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005931 SDValue Callee;
5932 if (!RenameFn)
Gabor Greif0635f352010-06-25 09:38:13 +00005933 Callee = getValue(I.getCalledValue());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005934 else
Bill Wendlingba54bca2013-06-19 21:36:55 +00005935 Callee = DAG.getExternalSymbol(RenameFn,
5936 TM.getTargetLowering()->getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005937
Bill Wendling0d580132009-12-23 01:28:19 +00005938 // Check if we can potentially perform a tail call. More detailed checking is
5939 // be done within LowerCallTo, after more information about the call is known.
Evan Cheng11e67932010-01-26 23:13:04 +00005940 LowerCallTo(&I, Callee, I.isTailCall());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005941}
5942
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005943namespace {
Dan Gohman462f6b52010-05-29 17:53:24 +00005944
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005945/// AsmOperandInfo - This contains information for each constraint that we are
5946/// lowering.
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005947class SDISelAsmOperandInfo : public TargetLowering::AsmOperandInfo {
Cedric Venetaff9c272009-02-14 16:06:42 +00005948public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005949 /// CallOperand - If this is the result output operand or a clobber
5950 /// this is null, otherwise it is the incoming operand to the CallInst.
5951 /// This gets modified as the asm is processed.
5952 SDValue CallOperand;
5953
5954 /// AssignedRegs - If this is a register or register class operand, this
5955 /// contains the set of register corresponding to the operand.
5956 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005957
John Thompsoneac6e1d2010-09-13 18:15:37 +00005958 explicit SDISelAsmOperandInfo(const TargetLowering::AsmOperandInfo &info)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005959 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
5960 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005961
Owen Andersone50ed302009-08-10 22:56:29 +00005962 /// getCallOperandValEVT - Return the EVT of the Value* that this operand
Chris Lattner81249c92008-10-17 17:05:25 +00005963 /// corresponds to. If there is no Value* for this operand, it returns
Owen Anderson825b72b2009-08-11 20:47:22 +00005964 /// MVT::Other.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005965 EVT getCallOperandValEVT(LLVMContext &Context,
Owen Anderson1d0be152009-08-13 21:58:54 +00005966 const TargetLowering &TLI,
Micah Villmow3574eca2012-10-08 16:38:25 +00005967 const DataLayout *TD) const {
Owen Anderson825b72b2009-08-11 20:47:22 +00005968 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005969
Chris Lattner81249c92008-10-17 17:05:25 +00005970 if (isa<BasicBlock>(CallOperandVal))
5971 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005972
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005973 llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005974
Eric Christophercef81b72011-05-09 20:04:43 +00005975 // FIXME: code duplicated from TargetLowering::ParseConstraints().
Chris Lattner81249c92008-10-17 17:05:25 +00005976 // If this is an indirect operand, the operand is a pointer to the
5977 // accessed type.
Bob Wilsone261b0c2009-12-22 18:34:19 +00005978 if (isIndirect) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005979 llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
Bob Wilsone261b0c2009-12-22 18:34:19 +00005980 if (!PtrTy)
Chris Lattner75361b62010-04-07 22:58:41 +00005981 report_fatal_error("Indirect operand for inline asm not a pointer!");
Bob Wilsone261b0c2009-12-22 18:34:19 +00005982 OpTy = PtrTy->getElementType();
5983 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005984
Eric Christophercef81b72011-05-09 20:04:43 +00005985 // Look for vector wrapped in a struct. e.g. { <16 x i8> }.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005986 if (StructType *STy = dyn_cast<StructType>(OpTy))
Eric Christophercef81b72011-05-09 20:04:43 +00005987 if (STy->getNumElements() == 1)
5988 OpTy = STy->getElementType(0);
5989
Chris Lattner81249c92008-10-17 17:05:25 +00005990 // If OpTy is not a single value, it may be a struct/union that we
5991 // can tile with integers.
5992 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
5993 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
5994 switch (BitSize) {
5995 default: break;
5996 case 1:
5997 case 8:
5998 case 16:
5999 case 32:
6000 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00006001 case 128:
Owen Anderson1d0be152009-08-13 21:58:54 +00006002 OpTy = IntegerType::get(Context, BitSize);
Chris Lattner81249c92008-10-17 17:05:25 +00006003 break;
6004 }
6005 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006006
Chris Lattner81249c92008-10-17 17:05:25 +00006007 return TLI.getValueType(OpTy, true);
6008 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006009};
Dan Gohman462f6b52010-05-29 17:53:24 +00006010
John Thompson44ab89e2010-10-29 17:29:13 +00006011typedef SmallVector<SDISelAsmOperandInfo,16> SDISelAsmOperandInfoVector;
6012
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00006013} // end anonymous namespace
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006014
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006015/// GetRegistersForValue - Assign registers (virtual or physical) for the
6016/// specified operand. We prefer to assign virtual registers, to allow the
Bob Wilson266d9452009-12-17 05:07:36 +00006017/// register allocator to handle the assignment process. However, if the asm
6018/// uses features that we can't model on machineinstrs, we have SDISel do the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006019/// allocation. This produces generally horrible, but correct, code.
6020///
6021/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006022///
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00006023static void GetRegistersForValue(SelectionDAG &DAG,
6024 const TargetLowering &TLI,
Andrew Trickac6d9be2013-05-25 02:42:55 +00006025 SDLoc DL,
Benjamin Kramer8b93ff22012-02-24 14:01:17 +00006026 SDISelAsmOperandInfo &OpInfo) {
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00006027 LLVMContext &Context = *DAG.getContext();
Owen Anderson23b9b192009-08-12 00:36:31 +00006028
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006029 MachineFunction &MF = DAG.getMachineFunction();
6030 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006031
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006032 // If this is a constraint for a single physreg, or a constraint for a
6033 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006034 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006035 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
6036 OpInfo.ConstraintVT);
6037
6038 unsigned NumRegs = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00006039 if (OpInfo.ConstraintVT != MVT::Other) {
Chris Lattner01426e12008-10-21 00:45:36 +00006040 // If this is a FP input in an integer register (or visa versa) insert a bit
6041 // cast of the input value. More generally, handle any case where the input
6042 // value disagrees with the register class we plan to stick this in.
6043 if (OpInfo.Type == InlineAsm::isInput &&
6044 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00006045 // Try to convert to the first EVT that the reg class contains. If the
Chris Lattner01426e12008-10-21 00:45:36 +00006046 // types are identical size, use a bitcast to convert (e.g. two differing
6047 // vector types).
Patrik Hagglund8963fec2012-12-19 12:23:01 +00006048 MVT RegVT = *PhysReg.second->vt_begin();
Chris Lattner01426e12008-10-21 00:45:36 +00006049 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00006050 OpInfo.CallOperand = DAG.getNode(ISD::BITCAST, DL,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00006051 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00006052 OpInfo.ConstraintVT = RegVT;
6053 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
6054 // If the input is a FP value and we want it in FP registers, do a
6055 // bitcast to the corresponding integer type. This turns an f64 value
6056 // into i64, which can be passed with two i32 values on a 32-bit
6057 // machine.
Patrik Hagglund8963fec2012-12-19 12:23:01 +00006058 RegVT = MVT::getIntegerVT(OpInfo.ConstraintVT.getSizeInBits());
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00006059 OpInfo.CallOperand = DAG.getNode(ISD::BITCAST, DL,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00006060 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00006061 OpInfo.ConstraintVT = RegVT;
6062 }
6063 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006064
Owen Anderson23b9b192009-08-12 00:36:31 +00006065 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00006066 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006067
Patrik Hagglund8963fec2012-12-19 12:23:01 +00006068 MVT RegVT;
Owen Andersone50ed302009-08-10 22:56:29 +00006069 EVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006070
6071 // If this is a constraint for a specific physical register, like {r17},
6072 // assign it now.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00006073 if (unsigned AssignedReg = PhysReg.first) {
6074 const TargetRegisterClass *RC = PhysReg.second;
Owen Anderson825b72b2009-08-11 20:47:22 +00006075 if (OpInfo.ConstraintVT == MVT::Other)
Chris Lattnere2f7bf82009-03-24 15:27:37 +00006076 ValueVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006077
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006078 // Get the actual register value type. This is important, because the user
6079 // may have asked for (e.g.) the AX register in i32 type. We need to
6080 // remember that AX is actually i16 to get the right extension.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00006081 RegVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006082
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006083 // This is a explicit reference to a physical register.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00006084 Regs.push_back(AssignedReg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006085
6086 // If this is an expanded reference, add the rest of the regs to Regs.
6087 if (NumRegs != 1) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00006088 TargetRegisterClass::iterator I = RC->begin();
6089 for (; *I != AssignedReg; ++I)
6090 assert(I != RC->end() && "Didn't find reg!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006091
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006092 // Already added the first reg.
6093 --NumRegs; ++I;
6094 for (; NumRegs; --NumRegs, ++I) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00006095 assert(I != RC->end() && "Ran out of registers to allocate!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006096 Regs.push_back(*I);
6097 }
6098 }
Bill Wendling651ad132009-12-22 01:25:10 +00006099
Dan Gohman7451d3e2010-05-29 17:03:36 +00006100 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006101 return;
6102 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006103
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006104 // Otherwise, if this was a reference to an LLVM register class, create vregs
6105 // for this reference.
Chris Lattnerb3b44842009-03-24 15:25:07 +00006106 if (const TargetRegisterClass *RC = PhysReg.second) {
6107 RegVT = *RC->vt_begin();
Owen Anderson825b72b2009-08-11 20:47:22 +00006108 if (OpInfo.ConstraintVT == MVT::Other)
Evan Chengfb112882009-03-23 08:01:15 +00006109 ValueVT = RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006110
Evan Chengfb112882009-03-23 08:01:15 +00006111 // Create the appropriate number of virtual registers.
6112 MachineRegisterInfo &RegInfo = MF.getRegInfo();
6113 for (; NumRegs; --NumRegs)
Chris Lattnerb3b44842009-03-24 15:25:07 +00006114 Regs.push_back(RegInfo.createVirtualRegister(RC));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006115
Dan Gohman7451d3e2010-05-29 17:03:36 +00006116 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
Evan Chengfb112882009-03-23 08:01:15 +00006117 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006118 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00006119
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006120 // Otherwise, we couldn't allocate enough registers for this.
6121}
6122
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006123/// visitInlineAsm - Handle a call to an InlineAsm object.
6124///
Dan Gohman46510a72010-04-15 01:51:59 +00006125void SelectionDAGBuilder::visitInlineAsm(ImmutableCallSite CS) {
6126 const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006127
6128 /// ConstraintOperands - Information about all of the constraints.
John Thompson44ab89e2010-10-29 17:29:13 +00006129 SDISelAsmOperandInfoVector ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006130
Bill Wendlingba54bca2013-06-19 21:36:55 +00006131 const TargetLowering *TLI = TM.getTargetLowering();
Evan Chengce1cdac2011-05-06 20:52:23 +00006132 TargetLowering::AsmOperandInfoVector
Bill Wendlingba54bca2013-06-19 21:36:55 +00006133 TargetConstraints = TLI->ParseConstraints(CS);
Evan Chengce1cdac2011-05-06 20:52:23 +00006134
John Thompsoneac6e1d2010-09-13 18:15:37 +00006135 bool hasMemory = false;
Michael J. Spencere70c5262010-10-16 08:25:21 +00006136
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006137 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
6138 unsigned ResNo = 0; // ResNo - The result number of the next output.
John Thompsoneac6e1d2010-09-13 18:15:37 +00006139 for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) {
6140 ConstraintOperands.push_back(SDISelAsmOperandInfo(TargetConstraints[i]));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006141 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Michael J. Spencere70c5262010-10-16 08:25:21 +00006142
Patrik Hagglundc698d3a2012-12-19 15:19:11 +00006143 MVT OpVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006144
6145 // Compute the value type for each operand.
6146 switch (OpInfo.Type) {
6147 case InlineAsm::isOutput:
6148 // Indirect outputs just consume an argument.
6149 if (OpInfo.isIndirect) {
Dan Gohman46510a72010-04-15 01:51:59 +00006150 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006151 break;
6152 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006153
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006154 // The return value of the call is this value. As such, there is no
6155 // corresponding argument.
Nick Lewycky8de34002011-09-30 22:19:53 +00006156 assert(!CS.getType()->isVoidTy() && "Bad inline asm!");
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006157 if (StructType *STy = dyn_cast<StructType>(CS.getType())) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00006158 OpVT = TLI->getSimpleValueType(STy->getElementType(ResNo));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006159 } else {
6160 assert(ResNo == 0 && "Asm only has one result!");
Bill Wendlingba54bca2013-06-19 21:36:55 +00006161 OpVT = TLI->getSimpleValueType(CS.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006162 }
6163 ++ResNo;
6164 break;
6165 case InlineAsm::isInput:
Dan Gohman46510a72010-04-15 01:51:59 +00006166 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006167 break;
6168 case InlineAsm::isClobber:
6169 // Nothing to do.
6170 break;
6171 }
6172
6173 // If this is an input or an indirect output, process the call argument.
6174 // BasicBlocks are labels, currently appearing only in asm's.
6175 if (OpInfo.CallOperandVal) {
Dan Gohman46510a72010-04-15 01:51:59 +00006176 if (const BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006177 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00006178 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006179 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006180 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006181
Bill Wendlingba54bca2013-06-19 21:36:55 +00006182 OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), *TLI, TD).
Patrik Hagglundc698d3a2012-12-19 15:19:11 +00006183 getSimpleVT();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006184 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006185
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006186 OpInfo.ConstraintVT = OpVT;
Michael J. Spencere70c5262010-10-16 08:25:21 +00006187
John Thompsoneac6e1d2010-09-13 18:15:37 +00006188 // Indirect operand accesses access memory.
6189 if (OpInfo.isIndirect)
6190 hasMemory = true;
6191 else {
6192 for (unsigned j = 0, ee = OpInfo.Codes.size(); j != ee; ++j) {
Evan Chengce1cdac2011-05-06 20:52:23 +00006193 TargetLowering::ConstraintType
Bill Wendlingba54bca2013-06-19 21:36:55 +00006194 CType = TLI->getConstraintType(OpInfo.Codes[j]);
John Thompsoneac6e1d2010-09-13 18:15:37 +00006195 if (CType == TargetLowering::C_Memory) {
6196 hasMemory = true;
6197 break;
6198 }
6199 }
6200 }
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006201 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006202
John Thompsoneac6e1d2010-09-13 18:15:37 +00006203 SDValue Chain, Flag;
6204
6205 // We won't need to flush pending loads if this asm doesn't touch
6206 // memory and is nonvolatile.
6207 if (hasMemory || IA->hasSideEffects())
6208 Chain = getRoot();
6209 else
6210 Chain = DAG.getRoot();
6211
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006212 // Second pass over the constraints: compute which constraint option to use
6213 // and assign registers to constraints that want a specific physreg.
John Thompsoneac6e1d2010-09-13 18:15:37 +00006214 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006215 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006216
John Thompson54584742010-09-24 22:24:05 +00006217 // If this is an output operand with a matching input operand, look up the
6218 // matching input. If their types mismatch, e.g. one is an integer, the
6219 // other is floating point, or their sizes are different, flag it as an
6220 // error.
6221 if (OpInfo.hasMatchingInput()) {
6222 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
Michael J. Spencere70c5262010-10-16 08:25:21 +00006223
John Thompson54584742010-09-24 22:24:05 +00006224 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Bill Wendling96cb1122012-07-19 00:04:14 +00006225 std::pair<unsigned, const TargetRegisterClass*> MatchRC =
Bill Wendlingba54bca2013-06-19 21:36:55 +00006226 TLI->getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
6227 OpInfo.ConstraintVT);
Bill Wendling96cb1122012-07-19 00:04:14 +00006228 std::pair<unsigned, const TargetRegisterClass*> InputRC =
Bill Wendlingba54bca2013-06-19 21:36:55 +00006229 TLI->getRegForInlineAsmConstraint(Input.ConstraintCode,
6230 Input.ConstraintVT);
John Thompson54584742010-09-24 22:24:05 +00006231 if ((OpInfo.ConstraintVT.isInteger() !=
6232 Input.ConstraintVT.isInteger()) ||
Eric Christopher5427ede2011-07-14 20:13:52 +00006233 (MatchRC.second != InputRC.second)) {
John Thompson54584742010-09-24 22:24:05 +00006234 report_fatal_error("Unsupported asm: input constraint"
6235 " with a matching output constraint of"
6236 " incompatible type!");
6237 }
6238 Input.ConstraintVT = OpInfo.ConstraintVT;
6239 }
6240 }
6241
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006242 // Compute the constraint code and ConstraintType to use.
Bill Wendlingba54bca2013-06-19 21:36:55 +00006243 TLI->ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006244
Eric Christopherfffe3632013-01-11 18:12:39 +00006245 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
6246 OpInfo.Type == InlineAsm::isClobber)
6247 continue;
6248
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006249 // If this is a memory input, and if the operand is not indirect, do what we
6250 // need to to provide an address for the memory input.
6251 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
6252 !OpInfo.isIndirect) {
Evan Chengce1cdac2011-05-06 20:52:23 +00006253 assert((OpInfo.isMultipleAlternative ||
6254 (OpInfo.Type == InlineAsm::isInput)) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006255 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006256
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006257 // Memory operands really want the address of the value. If we don't have
6258 // an indirect input, put it in the constpool if we can, otherwise spill
6259 // it to a stack slot.
Eric Christophere0b42c02011-06-03 17:21:23 +00006260 // TODO: This isn't quite right. We need to handle these according to
6261 // the addressing mode that the constraint wants. Also, this may take
6262 // an additional register for the computation and we don't want that
6263 // either.
Eric Christopher471e4222011-06-08 23:55:35 +00006264
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006265 // If the operand is a float, integer, or vector constant, spill to a
6266 // constant pool entry to get its address.
Dan Gohman46510a72010-04-15 01:51:59 +00006267 const Value *OpVal = OpInfo.CallOperandVal;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006268 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
Chris Lattnera78fa8c2012-01-27 03:08:05 +00006269 isa<ConstantVector>(OpVal) || isa<ConstantDataVector>(OpVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006270 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
Bill Wendlingba54bca2013-06-19 21:36:55 +00006271 TLI->getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006272 } else {
6273 // Otherwise, create a stack slot and emit a store to it before the
6274 // asm.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006275 Type *Ty = OpVal->getType();
Bill Wendlingba54bca2013-06-19 21:36:55 +00006276 uint64_t TySize = TLI->getDataLayout()->getTypeAllocSize(Ty);
6277 unsigned Align = TLI->getDataLayout()->getPrefTypeAlignment(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006278 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006279 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Bill Wendlingba54bca2013-06-19 21:36:55 +00006280 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI->getPointerTy());
Andrew Trickac6d9be2013-05-25 02:42:55 +00006281 Chain = DAG.getStore(Chain, getCurSDLoc(),
Chris Lattnerecf42c42010-09-21 16:36:31 +00006282 OpInfo.CallOperand, StackSlot,
6283 MachinePointerInfo::getFixedStack(SSFI),
David Greene1e559442010-02-15 17:00:31 +00006284 false, false, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006285 OpInfo.CallOperand = StackSlot;
6286 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006287
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006288 // There is no longer a Value* corresponding to this operand.
6289 OpInfo.CallOperandVal = 0;
Bill Wendling651ad132009-12-22 01:25:10 +00006290
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006291 // It is now an indirect operand.
6292 OpInfo.isIndirect = true;
6293 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006294
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006295 // If this constraint is for a specific register, allocate it before
6296 // anything else.
6297 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Bill Wendlingba54bca2013-06-19 21:36:55 +00006298 GetRegistersForValue(DAG, *TLI, getCurSDLoc(), OpInfo);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006299 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006300
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006301 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00006302 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006303 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
6304 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006305
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006306 // C_Register operands have already been allocated, Other/Memory don't need
6307 // to be.
6308 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Bill Wendlingba54bca2013-06-19 21:36:55 +00006309 GetRegistersForValue(DAG, *TLI, getCurSDLoc(), OpInfo);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006310 }
6311
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006312 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
6313 std::vector<SDValue> AsmNodeOperands;
6314 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
6315 AsmNodeOperands.push_back(
Dan Gohmanf2d7fb32010-01-04 21:00:54 +00006316 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00006317 TLI->getPointerTy()));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006318
Chris Lattnerdecc2672010-04-07 05:20:54 +00006319 // If we have a !srcloc metadata node associated with it, we want to attach
6320 // this to the ultimately generated inline asm machineinstr. To do this, we
6321 // pass in the third operand as this (potentially null) inline asm MDNode.
6322 const MDNode *SrcLoc = CS.getInstruction()->getMetadata("srcloc");
6323 AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006324
Chad Rosier3d716882012-10-30 19:11:54 +00006325 // Remember the HasSideEffect, AlignStack, AsmDialect, MayLoad and MayStore
6326 // bits as operand 3.
Evan Chengc36b7062011-01-07 23:50:32 +00006327 unsigned ExtraInfo = 0;
6328 if (IA->hasSideEffects())
6329 ExtraInfo |= InlineAsm::Extra_HasSideEffects;
6330 if (IA->isAlignStack())
6331 ExtraInfo |= InlineAsm::Extra_IsAlignStack;
Chad Rosier77fffa62012-09-05 22:17:43 +00006332 // Set the asm dialect.
Chad Rosier2f1d8152012-09-05 22:40:13 +00006333 ExtraInfo |= IA->getDialect() * InlineAsm::Extra_AsmDialect;
Chad Rosier3d716882012-10-30 19:11:54 +00006334
6335 // Determine if this InlineAsm MayLoad or MayStore based on the constraints.
6336 for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) {
6337 TargetLowering::AsmOperandInfo &OpInfo = TargetConstraints[i];
6338
6339 // Compute the constraint code and ConstraintType to use.
Bill Wendlingba54bca2013-06-19 21:36:55 +00006340 TLI->ComputeConstraintToUse(OpInfo, SDValue());
Chad Rosier3d716882012-10-30 19:11:54 +00006341
Chad Rosierdfa4cec2012-10-30 20:01:12 +00006342 // Ideally, we would only check against memory constraints. However, the
6343 // meaning of an other constraint can be target-specific and we can't easily
6344 // reason about it. Therefore, be conservative and set MayLoad/MayStore
6345 // for other constriants as well.
Chad Rosier3d716882012-10-30 19:11:54 +00006346 if (OpInfo.ConstraintType == TargetLowering::C_Memory ||
6347 OpInfo.ConstraintType == TargetLowering::C_Other) {
6348 if (OpInfo.Type == InlineAsm::isInput)
6349 ExtraInfo |= InlineAsm::Extra_MayLoad;
6350 else if (OpInfo.Type == InlineAsm::isOutput)
6351 ExtraInfo |= InlineAsm::Extra_MayStore;
Eric Christopherfffe3632013-01-11 18:12:39 +00006352 else if (OpInfo.Type == InlineAsm::isClobber)
6353 ExtraInfo |= (InlineAsm::Extra_MayLoad | InlineAsm::Extra_MayStore);
Chad Rosier3d716882012-10-30 19:11:54 +00006354 }
6355 }
6356
Evan Chengc36b7062011-01-07 23:50:32 +00006357 AsmNodeOperands.push_back(DAG.getTargetConstant(ExtraInfo,
Bill Wendlingba54bca2013-06-19 21:36:55 +00006358 TLI->getPointerTy()));
Dale Johannesenf1e309e2010-07-02 20:16:09 +00006359
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006360 // Loop over all of the inputs, copying the operand values into the
6361 // appropriate registers and processing the output regs.
6362 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006363
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006364 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
6365 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006366
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006367 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
6368 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
6369
6370 switch (OpInfo.Type) {
6371 case InlineAsm::isOutput: {
6372 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
6373 OpInfo.ConstraintType != TargetLowering::C_Register) {
6374 // Memory output, or 'other' output (e.g. 'X' constraint).
6375 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
6376
6377 // Add information to the INLINEASM node to know about this output.
Chris Lattnerdecc2672010-04-07 05:20:54 +00006378 unsigned OpFlags = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
6379 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags,
Bill Wendlingba54bca2013-06-19 21:36:55 +00006380 TLI->getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006381 AsmNodeOperands.push_back(OpInfo.CallOperand);
6382 break;
6383 }
6384
6385 // Otherwise, this is a register or register class output.
6386
6387 // Copy the output from the appropriate register. Find a register that
6388 // we can use.
Chris Lattnerfcd70902012-01-03 23:51:01 +00006389 if (OpInfo.AssignedRegs.Regs.empty()) {
6390 LLVMContext &Ctx = *DAG.getContext();
Stephen Lin155615d2013-07-08 00:37:03 +00006391 Ctx.emitError(CS.getInstruction(),
Chris Lattnerfcd70902012-01-03 23:51:01 +00006392 "couldn't allocate output register for constraint '" +
Eric Christopher1a54c572013-07-31 01:26:24 +00006393 Twine(OpInfo.ConstraintCode) + "'");
6394 return;
Chris Lattnerfcd70902012-01-03 23:51:01 +00006395 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006396
6397 // If this is an indirect operand, store through the pointer after the
6398 // asm.
6399 if (OpInfo.isIndirect) {
6400 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
6401 OpInfo.CallOperandVal));
6402 } else {
6403 // This is the result value of the call.
Benjamin Kramerf0127052010-01-05 13:12:22 +00006404 assert(!CS.getType()->isVoidTy() && "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006405 // Concatenate this output onto the outputs list.
6406 RetValRegs.append(OpInfo.AssignedRegs);
6407 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006408
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006409 // Add information to the INLINEASM node to know that this register is
6410 // set.
Eric Christopherb0bee812013-07-30 22:50:44 +00006411 OpInfo.AssignedRegs
6412 .AddInlineAsmOperands(OpInfo.isEarlyClobber
6413 ? InlineAsm::Kind_RegDefEarlyClobber
6414 : InlineAsm::Kind_RegDef,
6415 false, 0, DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006416 break;
6417 }
6418 case InlineAsm::isInput: {
6419 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006420
Chris Lattner6bdcda32008-10-17 16:47:46 +00006421 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006422 // If this is required to match an output register we have already set,
6423 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00006424 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006425
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006426 // Scan until we find the definition we already emitted of this operand.
6427 // When we find it, create a RegsForValue operand.
Chris Lattnerdecc2672010-04-07 05:20:54 +00006428 unsigned CurOp = InlineAsm::Op_FirstOperand;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006429 for (; OperandNo; --OperandNo) {
6430 // Advance to the next operand.
Evan Cheng697cbbf2009-03-20 18:03:34 +00006431 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006432 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00006433 assert((InlineAsm::isRegDefKind(OpFlag) ||
6434 InlineAsm::isRegDefEarlyClobberKind(OpFlag) ||
6435 InlineAsm::isMemKind(OpFlag)) && "Skipped past definitions?");
Evan Cheng697cbbf2009-03-20 18:03:34 +00006436 CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006437 }
6438
Evan Cheng697cbbf2009-03-20 18:03:34 +00006439 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006440 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00006441 if (InlineAsm::isRegDefKind(OpFlag) ||
6442 InlineAsm::isRegDefEarlyClobberKind(OpFlag)) {
Evan Cheng697cbbf2009-03-20 18:03:34 +00006443 // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
Chris Lattner6129c372010-04-08 00:09:16 +00006444 if (OpInfo.isIndirect) {
6445 // This happens on gcc/testsuite/gcc.dg/pr8788-1.c
Dan Gohman99be8ae2010-04-19 22:41:47 +00006446 LLVMContext &Ctx = *DAG.getContext();
Eric Christopher1a54c572013-07-31 01:26:24 +00006447 Ctx.emitError(CS.getInstruction(), "inline asm not supported yet:"
6448 " don't know how to handle tied "
6449 "indirect register inputs");
6450 return;
Chris Lattner6129c372010-04-08 00:09:16 +00006451 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00006452
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006453 RegsForValue MatchedRegs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006454 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00006455 MVT RegVT = AsmNodeOperands[CurOp+1].getSimpleValueType();
Evan Chengfb112882009-03-23 08:01:15 +00006456 MatchedRegs.RegVTs.push_back(RegVT);
6457 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
Evan Cheng697cbbf2009-03-20 18:03:34 +00006458 for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag);
Chad Rosier2871ba92013-04-24 22:53:10 +00006459 i != e; ++i) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00006460 if (const TargetRegisterClass *RC = TLI->getRegClassFor(RegVT))
Chad Rosier2871ba92013-04-24 22:53:10 +00006461 MatchedRegs.Regs.push_back(RegInfo.createVirtualRegister(RC));
6462 else {
6463 LLVMContext &Ctx = *DAG.getContext();
Eric Christopher1a54c572013-07-31 01:26:24 +00006464 Ctx.emitError(CS.getInstruction(),
6465 "inline asm error: This value"
Chad Rosier2871ba92013-04-24 22:53:10 +00006466 " type register class is not natively supported!");
Eric Christopher1a54c572013-07-31 01:26:24 +00006467 return;
Chad Rosier2871ba92013-04-24 22:53:10 +00006468 }
6469 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006470 // Use the produced MatchedRegs object to
Andrew Trickac6d9be2013-05-25 02:42:55 +00006471 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurSDLoc(),
Bill Wendlingf18eb582012-09-26 06:16:18 +00006472 Chain, &Flag, CS.getInstruction());
Chris Lattnerdecc2672010-04-07 05:20:54 +00006473 MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse,
Evan Chengfb112882009-03-23 08:01:15 +00006474 true, OpInfo.getMatchedOperand(),
Bill Wendling46ada192010-03-02 01:55:18 +00006475 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006476 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006477 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00006478
Chris Lattnerdecc2672010-04-07 05:20:54 +00006479 assert(InlineAsm::isMemKind(OpFlag) && "Unknown matching constraint!");
6480 assert(InlineAsm::getNumOperandRegisters(OpFlag) == 1 &&
6481 "Unexpected number of operands");
6482 // Add information to the INLINEASM node to know about this input.
6483 // See InlineAsm.h isUseOperandTiedToDef.
6484 OpFlag = InlineAsm::getFlagWordForMatchingOp(OpFlag,
6485 OpInfo.getMatchedOperand());
6486 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag,
Bill Wendlingba54bca2013-06-19 21:36:55 +00006487 TLI->getPointerTy()));
Chris Lattnerdecc2672010-04-07 05:20:54 +00006488 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
6489 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006490 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006491
Dale Johannesenb5611a62010-07-13 20:17:05 +00006492 // Treat indirect 'X' constraint as memory.
Michael J. Spencere70c5262010-10-16 08:25:21 +00006493 if (OpInfo.ConstraintType == TargetLowering::C_Other &&
6494 OpInfo.isIndirect)
Dale Johannesenb5611a62010-07-13 20:17:05 +00006495 OpInfo.ConstraintType = TargetLowering::C_Memory;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006496
Dale Johannesenb5611a62010-07-13 20:17:05 +00006497 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006498 std::vector<SDValue> Ops;
Bill Wendlingba54bca2013-06-19 21:36:55 +00006499 TLI->LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode,
6500 Ops, DAG);
Chris Lattnerfcd70902012-01-03 23:51:01 +00006501 if (Ops.empty()) {
6502 LLVMContext &Ctx = *DAG.getContext();
6503 Ctx.emitError(CS.getInstruction(),
6504 "invalid operand for inline asm constraint '" +
Eric Christopher1a54c572013-07-31 01:26:24 +00006505 Twine(OpInfo.ConstraintCode) + "'");
6506 return;
Chris Lattnerfcd70902012-01-03 23:51:01 +00006507 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006508
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006509 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00006510 unsigned ResOpType =
6511 InlineAsm::getFlagWord(InlineAsm::Kind_Imm, Ops.size());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006512 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Bill Wendlingba54bca2013-06-19 21:36:55 +00006513 TLI->getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006514 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
6515 break;
Chris Lattnerdecc2672010-04-07 05:20:54 +00006516 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00006517
Chris Lattnerdecc2672010-04-07 05:20:54 +00006518 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006519 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
Bill Wendlingba54bca2013-06-19 21:36:55 +00006520 assert(InOperandVal.getValueType() == TLI->getPointerTy() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006521 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006522
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006523 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00006524 unsigned ResOpType = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
Dale Johannesen86b49f82008-09-24 01:07:17 +00006525 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Bill Wendlingba54bca2013-06-19 21:36:55 +00006526 TLI->getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006527 AsmNodeOperands.push_back(InOperandVal);
6528 break;
6529 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006530
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006531 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
6532 OpInfo.ConstraintType == TargetLowering::C_Register) &&
6533 "Unknown constraint type!");
Eric Christopher9eb4f8a2012-07-02 21:16:43 +00006534
6535 // TODO: Support this.
6536 if (OpInfo.isIndirect) {
6537 LLVMContext &Ctx = *DAG.getContext();
6538 Ctx.emitError(CS.getInstruction(),
6539 "Don't know how to handle indirect register inputs yet "
Eric Christopher1a54c572013-07-31 01:26:24 +00006540 "for constraint '" +
6541 Twine(OpInfo.ConstraintCode) + "'");
6542 return;
Eric Christopher9eb4f8a2012-07-02 21:16:43 +00006543 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006544
6545 // Copy the input into the appropriate registers.
Chris Lattnerfcd70902012-01-03 23:51:01 +00006546 if (OpInfo.AssignedRegs.Regs.empty()) {
6547 LLVMContext &Ctx = *DAG.getContext();
Stephen Lin155615d2013-07-08 00:37:03 +00006548 Ctx.emitError(CS.getInstruction(),
Chris Lattnerfcd70902012-01-03 23:51:01 +00006549 "couldn't allocate input reg for constraint '" +
Eric Christopher1a54c572013-07-31 01:26:24 +00006550 Twine(OpInfo.ConstraintCode) + "'");
6551 return;
Chris Lattnerfcd70902012-01-03 23:51:01 +00006552 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006553
Andrew Trickac6d9be2013-05-25 02:42:55 +00006554 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurSDLoc(),
Bill Wendlingf18eb582012-09-26 06:16:18 +00006555 Chain, &Flag, CS.getInstruction());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006556
Chris Lattnerdecc2672010-04-07 05:20:54 +00006557 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse, false, 0,
Bill Wendling46ada192010-03-02 01:55:18 +00006558 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006559 break;
6560 }
6561 case InlineAsm::isClobber: {
6562 // Add the clobbered value to the operand list, so that the register
6563 // allocator is aware that the physreg got clobbered.
6564 if (!OpInfo.AssignedRegs.Regs.empty())
Jakob Stoklund Olesenf792fa92011-06-27 04:08:33 +00006565 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_Clobber,
Bill Wendling46ada192010-03-02 01:55:18 +00006566 false, 0, DAG,
Bill Wendling651ad132009-12-22 01:25:10 +00006567 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006568 break;
6569 }
6570 }
6571 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006572
Chris Lattnerdecc2672010-04-07 05:20:54 +00006573 // Finish up input operands. Set the input chain and add the flag last.
Dale Johannesenf1e309e2010-07-02 20:16:09 +00006574 AsmNodeOperands[InlineAsm::Op_InputChain] = Chain;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006575 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006576
Andrew Trickac6d9be2013-05-25 02:42:55 +00006577 Chain = DAG.getNode(ISD::INLINEASM, getCurSDLoc(),
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006578 DAG.getVTList(MVT::Other, MVT::Glue),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006579 &AsmNodeOperands[0], AsmNodeOperands.size());
6580 Flag = Chain.getValue(1);
6581
6582 // If this asm returns a register value, copy the result from that register
6583 // and set it as the value of the call.
6584 if (!RetValRegs.Regs.empty()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006585 SDValue Val = RetValRegs.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(),
Bill Wendling12931302012-09-26 04:04:19 +00006586 Chain, &Flag, CS.getInstruction());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006587
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006588 // FIXME: Why don't we do this for inline asms with MRVs?
6589 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00006590 EVT ResultType = TLI->getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006591
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006592 // If any of the results of the inline asm is a vector, it may have the
6593 // wrong width/num elts. This can happen for register classes that can
6594 // contain multiple different value types. The preg or vreg allocated may
6595 // not have the same VT as was expected. Convert it to the right type
6596 // with bit_convert.
6597 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006598 Val = DAG.getNode(ISD::BITCAST, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00006599 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00006600
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006601 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006602 ResultType.isInteger() && Val.getValueType().isInteger()) {
6603 // If a result value was tied to an input value, the computed result may
6604 // have a wider width than the expected result. Extract the relevant
6605 // portion.
Andrew Trickac6d9be2013-05-25 02:42:55 +00006606 Val = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00006607 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006608
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006609 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Chris Lattner0c526442008-10-17 17:52:49 +00006610 }
Dan Gohman95915732008-10-18 01:03:45 +00006611
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006612 setValue(CS.getInstruction(), Val);
Dale Johannesenec65a7d2009-04-14 00:56:56 +00006613 // Don't need to use this as a chain in this case.
6614 if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty())
6615 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006616 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006617
Dan Gohman46510a72010-04-15 01:51:59 +00006618 std::vector<std::pair<SDValue, const Value *> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006619
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006620 // Process indirect outputs, first output all of the flagged copies out of
6621 // physregs.
6622 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
6623 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
Dan Gohman46510a72010-04-15 01:51:59 +00006624 const Value *Ptr = IndirectStoresToEmit[i].second;
Andrew Trickac6d9be2013-05-25 02:42:55 +00006625 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(),
Bill Wendling12931302012-09-26 04:04:19 +00006626 Chain, &Flag, IA);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006627 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
6628 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006629
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006630 // Emit the non-flagged stores from the physregs.
6631 SmallVector<SDValue, 8> OutChains;
Bill Wendling651ad132009-12-22 01:25:10 +00006632 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006633 SDValue Val = DAG.getStore(Chain, getCurSDLoc(),
Bill Wendling651ad132009-12-22 01:25:10 +00006634 StoresToEmit[i].first,
6635 getValue(StoresToEmit[i].second),
Chris Lattner84bd98a2010-09-21 18:58:22 +00006636 MachinePointerInfo(StoresToEmit[i].second),
David Greene1e559442010-02-15 17:00:31 +00006637 false, false, 0);
Bill Wendling651ad132009-12-22 01:25:10 +00006638 OutChains.push_back(Val);
Bill Wendling651ad132009-12-22 01:25:10 +00006639 }
6640
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006641 if (!OutChains.empty())
Andrew Trickac6d9be2013-05-25 02:42:55 +00006642 Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006643 &OutChains[0], OutChains.size());
Bill Wendling651ad132009-12-22 01:25:10 +00006644
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006645 DAG.setRoot(Chain);
6646}
6647
Dan Gohman46510a72010-04-15 01:51:59 +00006648void SelectionDAGBuilder::visitVAStart(const CallInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006649 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurSDLoc(),
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006650 MVT::Other, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00006651 getValue(I.getArgOperand(0)),
6652 DAG.getSrcValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006653}
6654
Dan Gohman46510a72010-04-15 01:51:59 +00006655void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00006656 const TargetLowering *TLI = TM.getTargetLowering();
6657 const DataLayout &TD = *TLI->getDataLayout();
6658 SDValue V = DAG.getVAArg(TLI->getValueType(I.getType()), getCurSDLoc(),
Dale Johannesena04b7572009-02-03 23:04:43 +00006659 getRoot(), getValue(I.getOperand(0)),
Rafael Espindolacbeeae22010-07-11 04:01:49 +00006660 DAG.getSrcValue(I.getOperand(0)),
Rafael Espindola9d544d02010-07-12 18:11:17 +00006661 TD.getABITypeAlignment(I.getType()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006662 setValue(&I, V);
6663 DAG.setRoot(V.getValue(1));
6664}
6665
Dan Gohman46510a72010-04-15 01:51:59 +00006666void SelectionDAGBuilder::visitVAEnd(const CallInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006667 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurSDLoc(),
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006668 MVT::Other, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00006669 getValue(I.getArgOperand(0)),
6670 DAG.getSrcValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006671}
6672
Dan Gohman46510a72010-04-15 01:51:59 +00006673void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006674 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurSDLoc(),
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006675 MVT::Other, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00006676 getValue(I.getArgOperand(0)),
6677 getValue(I.getArgOperand(1)),
6678 DAG.getSrcValue(I.getArgOperand(0)),
6679 DAG.getSrcValue(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006680}
6681
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006682/// TargetLowering::LowerCallTo - This is the default LowerCallTo
Dan Gohman98ca4f22009-08-05 01:29:28 +00006683/// implementation, which just calls LowerCall.
6684/// FIXME: When all targets are
6685/// migrated to using LowerCall, this hook should be integrated into SDISel.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006686std::pair<SDValue, SDValue>
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006687TargetLowering::LowerCallTo(TargetLowering::CallLoweringInfo &CLI) const {
Stephen Lin3484da92013-04-30 22:49:28 +00006688 // Handle the incoming return values from the call.
6689 CLI.Ins.clear();
6690 SmallVector<EVT, 4> RetTys;
6691 ComputeValueVTs(*this, CLI.RetTy, RetTys);
6692 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
6693 EVT VT = RetTys[I];
6694 MVT RegisterVT = getRegisterType(CLI.RetTy->getContext(), VT);
6695 unsigned NumRegs = getNumRegisters(CLI.RetTy->getContext(), VT);
6696 for (unsigned i = 0; i != NumRegs; ++i) {
6697 ISD::InputArg MyFlags;
6698 MyFlags.VT = RegisterVT;
6699 MyFlags.Used = CLI.IsReturnValueUsed;
6700 if (CLI.RetSExt)
6701 MyFlags.Flags.setSExt();
6702 if (CLI.RetZExt)
6703 MyFlags.Flags.setZExt();
6704 if (CLI.IsInReg)
6705 MyFlags.Flags.setInReg();
6706 CLI.Ins.push_back(MyFlags);
6707 }
6708 }
6709
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006710 // Handle all of the outgoing arguments.
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006711 CLI.Outs.clear();
6712 CLI.OutVals.clear();
6713 ArgListTy &Args = CLI.Args;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006714 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00006715 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006716 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
6717 for (unsigned Value = 0, NumValues = ValueVTs.size();
6718 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006719 EVT VT = ValueVTs[Value];
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006720 Type *ArgTy = VT.getTypeForEVT(CLI.RetTy->getContext());
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006721 SDValue Op = SDValue(Args[i].Node.getNode(),
6722 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006723 ISD::ArgFlagsTy Flags;
6724 unsigned OriginalAlignment =
Micah Villmow3574eca2012-10-08 16:38:25 +00006725 getDataLayout()->getABITypeAlignment(ArgTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006726
6727 if (Args[i].isZExt)
6728 Flags.setZExt();
6729 if (Args[i].isSExt)
6730 Flags.setSExt();
6731 if (Args[i].isInReg)
6732 Flags.setInReg();
6733 if (Args[i].isSRet)
6734 Flags.setSRet();
6735 if (Args[i].isByVal) {
6736 Flags.setByVal();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006737 PointerType *Ty = cast<PointerType>(Args[i].Ty);
6738 Type *ElementTy = Ty->getElementType();
Micah Villmow3574eca2012-10-08 16:38:25 +00006739 Flags.setByValSize(getDataLayout()->getTypeAllocSize(ElementTy));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006740 // For ByVal, alignment should come from FE. BE will guess if this
6741 // info is not there but there are cases it cannot get right.
Chris Lattner9db20f32011-05-22 23:23:02 +00006742 unsigned FrameAlign;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006743 if (Args[i].Alignment)
6744 FrameAlign = Args[i].Alignment;
Chris Lattner9db20f32011-05-22 23:23:02 +00006745 else
6746 FrameAlign = getByValTypeAlignment(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006747 Flags.setByValAlign(FrameAlign);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006748 }
6749 if (Args[i].isNest)
6750 Flags.setNest();
6751 Flags.setOrigAlign(OriginalAlignment);
6752
Patrik Hagglunddfcf33a2012-12-19 11:48:16 +00006753 MVT PartVT = getRegisterType(CLI.RetTy->getContext(), VT);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006754 unsigned NumParts = getNumRegisters(CLI.RetTy->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006755 SmallVector<SDValue, 4> Parts(NumParts);
6756 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
6757
6758 if (Args[i].isSExt)
6759 ExtendKind = ISD::SIGN_EXTEND;
6760 else if (Args[i].isZExt)
6761 ExtendKind = ISD::ZERO_EXTEND;
6762
Stephen Lin3484da92013-04-30 22:49:28 +00006763 // Conservatively only handle 'returned' on non-vectors for now
6764 if (Args[i].isReturned && !Op.getValueType().isVector()) {
6765 assert(CLI.RetTy == Args[i].Ty && RetTys.size() == NumValues &&
6766 "unexpected use of 'returned'");
6767 // Before passing 'returned' to the target lowering code, ensure that
6768 // either the register MVT and the actual EVT are the same size or that
6769 // the return value and argument are extended in the same way; in these
6770 // cases it's safe to pass the argument register value unchanged as the
6771 // return register value (although it's at the target's option whether
6772 // to do so)
6773 // TODO: allow code generation to take advantage of partially preserved
6774 // registers rather than clobbering the entire register when the
6775 // parameter extension method is not compatible with the return
6776 // extension method
6777 if ((NumParts * PartVT.getSizeInBits() == VT.getSizeInBits()) ||
6778 (ExtendKind != ISD::ANY_EXTEND &&
6779 CLI.RetSExt == Args[i].isSExt && CLI.RetZExt == Args[i].isZExt))
6780 Flags.setReturned();
6781 }
6782
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006783 getCopyToParts(CLI.DAG, CLI.DL, Op, &Parts[0], NumParts,
Bill Wendlingf18eb582012-09-26 06:16:18 +00006784 PartVT, CLI.CS ? CLI.CS->getInstruction() : 0, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006785
Dan Gohman98ca4f22009-08-05 01:29:28 +00006786 for (unsigned j = 0; j != NumParts; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006787 // if it isn't first piece, alignment must be 1
Dan Gohmanc9403652010-07-07 15:54:55 +00006788 ISD::OutputArg MyFlags(Flags, Parts[j].getValueType(),
Manman Ren0a1544d2012-11-01 23:49:58 +00006789 i < CLI.NumFixedArgs,
6790 i, j*Parts[j].getValueType().getStoreSize());
Dan Gohman98ca4f22009-08-05 01:29:28 +00006791 if (NumParts > 1 && j == 0)
6792 MyFlags.Flags.setSplit();
6793 else if (j != 0)
6794 MyFlags.Flags.setOrigAlign(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006795
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006796 CLI.Outs.push_back(MyFlags);
6797 CLI.OutVals.push_back(Parts[j]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006798 }
6799 }
6800 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006801
Dan Gohman98ca4f22009-08-05 01:29:28 +00006802 SmallVector<SDValue, 4> InVals;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006803 CLI.Chain = LowerCall(CLI, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00006804
6805 // Verify that the target's LowerCall behaved as expected.
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006806 assert(CLI.Chain.getNode() && CLI.Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00006807 "LowerCall didn't return a valid chain!");
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006808 assert((!CLI.IsTailCall || InVals.empty()) &&
Dan Gohman5e866062009-08-06 15:37:27 +00006809 "LowerCall emitted a return value for a tail call!");
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006810 assert((CLI.IsTailCall || InVals.size() == CLI.Ins.size()) &&
Dan Gohman5e866062009-08-06 15:37:27 +00006811 "LowerCall didn't emit the correct number of values!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00006812
6813 // For a tail call, the return value is merely live-out and there aren't
6814 // any nodes in the DAG representing it. Return a special value to
6815 // indicate that a tail call has been emitted and no more Instructions
6816 // should be processed in the current block.
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006817 if (CLI.IsTailCall) {
6818 CLI.DAG.setRoot(CLI.Chain);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006819 return std::make_pair(SDValue(), SDValue());
6820 }
6821
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006822 DEBUG(for (unsigned i = 0, e = CLI.Ins.size(); i != e; ++i) {
Evan Chengaf1871f2010-03-11 19:38:18 +00006823 assert(InVals[i].getNode() &&
6824 "LowerCall emitted a null value!");
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006825 assert(EVT(CLI.Ins[i].VT) == InVals[i].getValueType() &&
Evan Chengaf1871f2010-03-11 19:38:18 +00006826 "LowerCall emitted a value with the wrong type!");
6827 });
6828
Dan Gohman98ca4f22009-08-05 01:29:28 +00006829 // Collect the legal value parts into potentially illegal values
6830 // that correspond to the original function's return values.
6831 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006832 if (CLI.RetSExt)
Dan Gohman98ca4f22009-08-05 01:29:28 +00006833 AssertOp = ISD::AssertSext;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006834 else if (CLI.RetZExt)
Dan Gohman98ca4f22009-08-05 01:29:28 +00006835 AssertOp = ISD::AssertZext;
6836 SmallVector<SDValue, 4> ReturnValues;
6837 unsigned CurReg = 0;
6838 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00006839 EVT VT = RetTys[I];
Patrik Hagglunddfcf33a2012-12-19 11:48:16 +00006840 MVT RegisterVT = getRegisterType(CLI.RetTy->getContext(), VT);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006841 unsigned NumRegs = getNumRegisters(CLI.RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006842
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006843 ReturnValues.push_back(getCopyFromParts(CLI.DAG, CLI.DL, &InVals[CurReg],
Bill Wendling12931302012-09-26 04:04:19 +00006844 NumRegs, RegisterVT, VT, NULL,
Bill Wendling4533cac2010-01-28 21:51:40 +00006845 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00006846 CurReg += NumRegs;
6847 }
6848
6849 // For a function returning void, there is no return value. We can't create
6850 // such a node, so we just return a null return value in that case. In
Chris Lattner7a2bdde2011-04-15 05:18:47 +00006851 // that case, nothing will actually look at the value.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006852 if (ReturnValues.empty())
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006853 return std::make_pair(SDValue(), CLI.Chain);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006854
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006855 SDValue Res = CLI.DAG.getNode(ISD::MERGE_VALUES, CLI.DL,
6856 CLI.DAG.getVTList(&RetTys[0], RetTys.size()),
Dan Gohman98ca4f22009-08-05 01:29:28 +00006857 &ReturnValues[0], ReturnValues.size());
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006858 return std::make_pair(Res, CLI.Chain);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006859}
6860
Duncan Sands9fbc7e22009-01-21 09:00:29 +00006861void TargetLowering::LowerOperationWrapper(SDNode *N,
6862 SmallVectorImpl<SDValue> &Results,
Dan Gohmand858e902010-04-17 15:26:15 +00006863 SelectionDAG &DAG) const {
Duncan Sands9fbc7e22009-01-21 09:00:29 +00006864 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00006865 if (Res.getNode())
6866 Results.push_back(Res);
6867}
6868
Dan Gohmand858e902010-04-17 15:26:15 +00006869SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Torok Edwinc23197a2009-07-14 16:55:14 +00006870 llvm_unreachable("LowerOperation not implemented for this target!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006871}
6872
Dan Gohman46510a72010-04-15 01:51:59 +00006873void
6874SelectionDAGBuilder::CopyValueToVirtualRegister(const Value *V, unsigned Reg) {
Dan Gohman28a17352010-07-01 01:59:43 +00006875 SDValue Op = getNonRegisterValue(V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006876 assert((Op.getOpcode() != ISD::CopyFromReg ||
6877 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
6878 "Copy from a reg to the same reg!");
6879 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
6880
Bill Wendlingba54bca2013-06-19 21:36:55 +00006881 const TargetLowering *TLI = TM.getTargetLowering();
6882 RegsForValue RFV(V->getContext(), *TLI, Reg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006883 SDValue Chain = DAG.getEntryNode();
Andrew Trickac6d9be2013-05-25 02:42:55 +00006884 RFV.getCopyToRegs(Op, DAG, getCurSDLoc(), Chain, 0, V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006885 PendingExports.push_back(Chain);
6886}
6887
6888#include "llvm/CodeGen/SelectionDAGISel.h"
6889
Eli Friedman23d32432011-05-05 16:53:34 +00006890/// isOnlyUsedInEntryBlock - If the specified argument is only used in the
6891/// entry block, return true. This includes arguments used by switches, since
6892/// the switch may expand into multiple basic blocks.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006893static bool isOnlyUsedInEntryBlock(const Argument *A, bool FastISel) {
Eli Friedman23d32432011-05-05 16:53:34 +00006894 // With FastISel active, we may be splitting blocks, so force creation
6895 // of virtual registers for all non-dead arguments.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006896 if (FastISel)
Eli Friedman23d32432011-05-05 16:53:34 +00006897 return A->use_empty();
6898
6899 const BasicBlock *Entry = A->getParent()->begin();
6900 for (Value::const_use_iterator UI = A->use_begin(), E = A->use_end();
6901 UI != E; ++UI) {
6902 const User *U = *UI;
6903 if (cast<Instruction>(U)->getParent() != Entry || isa<SwitchInst>(U))
6904 return false; // Use not in entry block.
6905 }
6906 return true;
6907}
6908
Eli Bendersky6437d382013-02-28 23:09:18 +00006909void SelectionDAGISel::LowerArguments(const Function &F) {
Dan Gohman2048b852009-11-23 18:04:58 +00006910 SelectionDAG &DAG = SDB->DAG;
Andrew Trickac6d9be2013-05-25 02:42:55 +00006911 SDLoc dl = SDB->getCurSDLoc();
Bill Wendlingba54bca2013-06-19 21:36:55 +00006912 const TargetLowering *TLI = getTargetLowering();
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006913 const DataLayout *TD = TLI->getDataLayout();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006914 SmallVector<ISD::InputArg, 16> Ins;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006915
Dan Gohman7451d3e2010-05-29 17:03:36 +00006916 if (!FuncInfo->CanLowerReturn) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006917 // Put in an sret pointer parameter before all the other parameters.
6918 SmallVector<EVT, 1> ValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00006919 ComputeValueVTs(*getTargetLowering(),
6920 PointerType::getUnqual(F.getReturnType()), ValueVTs);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006921
6922 // NOTE: Assuming that a pointer will never break down to more than one VT
6923 // or one register.
6924 ISD::ArgFlagsTy Flags;
6925 Flags.setSRet();
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006926 MVT RegisterVT = TLI->getRegisterType(*DAG.getContext(), ValueVTs[0]);
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00006927 ISD::InputArg RetArg(Flags, RegisterVT, true, 0, 0);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006928 Ins.push_back(RetArg);
6929 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00006930
Dan Gohman98ca4f22009-08-05 01:29:28 +00006931 // Set up the incoming argument description vector.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006932 unsigned Idx = 1;
Dan Gohman46510a72010-04-15 01:51:59 +00006933 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
Dan Gohman98ca4f22009-08-05 01:29:28 +00006934 I != E; ++I, ++Idx) {
Owen Andersone50ed302009-08-10 22:56:29 +00006935 SmallVector<EVT, 4> ValueVTs;
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006936 ComputeValueVTs(*TLI, I->getType(), ValueVTs);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006937 bool isArgValueUsed = !I->use_empty();
6938 for (unsigned Value = 0, NumValues = ValueVTs.size();
6939 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006940 EVT VT = ValueVTs[Value];
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006941 Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00006942 ISD::ArgFlagsTy Flags;
6943 unsigned OriginalAlignment =
6944 TD->getABITypeAlignment(ArgTy);
6945
Bill Wendling39cd0c82012-12-30 12:45:13 +00006946 if (F.getAttributes().hasAttribute(Idx, Attribute::ZExt))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006947 Flags.setZExt();
Bill Wendling39cd0c82012-12-30 12:45:13 +00006948 if (F.getAttributes().hasAttribute(Idx, Attribute::SExt))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006949 Flags.setSExt();
Bill Wendling39cd0c82012-12-30 12:45:13 +00006950 if (F.getAttributes().hasAttribute(Idx, Attribute::InReg))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006951 Flags.setInReg();
Bill Wendling39cd0c82012-12-30 12:45:13 +00006952 if (F.getAttributes().hasAttribute(Idx, Attribute::StructRet))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006953 Flags.setSRet();
Bill Wendling39cd0c82012-12-30 12:45:13 +00006954 if (F.getAttributes().hasAttribute(Idx, Attribute::ByVal)) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00006955 Flags.setByVal();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006956 PointerType *Ty = cast<PointerType>(I->getType());
6957 Type *ElementTy = Ty->getElementType();
Chris Lattner9db20f32011-05-22 23:23:02 +00006958 Flags.setByValSize(TD->getTypeAllocSize(ElementTy));
Dan Gohman98ca4f22009-08-05 01:29:28 +00006959 // For ByVal, alignment should be passed from FE. BE will guess if
6960 // this info is not there but there are cases it cannot get right.
Chris Lattner9db20f32011-05-22 23:23:02 +00006961 unsigned FrameAlign;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006962 if (F.getParamAlignment(Idx))
6963 FrameAlign = F.getParamAlignment(Idx);
Chris Lattner9db20f32011-05-22 23:23:02 +00006964 else
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006965 FrameAlign = TLI->getByValTypeAlignment(ElementTy);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006966 Flags.setByValAlign(FrameAlign);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006967 }
Bill Wendling39cd0c82012-12-30 12:45:13 +00006968 if (F.getAttributes().hasAttribute(Idx, Attribute::Nest))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006969 Flags.setNest();
6970 Flags.setOrigAlign(OriginalAlignment);
6971
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006972 MVT RegisterVT = TLI->getRegisterType(*CurDAG->getContext(), VT);
6973 unsigned NumRegs = TLI->getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006974 for (unsigned i = 0; i != NumRegs; ++i) {
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00006975 ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed,
6976 Idx-1, i*RegisterVT.getStoreSize());
Dan Gohman98ca4f22009-08-05 01:29:28 +00006977 if (NumRegs > 1 && i == 0)
6978 MyFlags.Flags.setSplit();
6979 // if it isn't first piece, alignment must be 1
6980 else if (i > 0)
6981 MyFlags.Flags.setOrigAlign(1);
6982 Ins.push_back(MyFlags);
6983 }
6984 }
6985 }
6986
6987 // Call the target to set up the argument values.
6988 SmallVector<SDValue, 8> InVals;
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006989 SDValue NewRoot = TLI->LowerFormalArguments(DAG.getRoot(), F.getCallingConv(),
6990 F.isVarArg(), Ins,
6991 dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00006992
6993 // Verify that the target's LowerFormalArguments behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00006994 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00006995 "LowerFormalArguments didn't return a valid chain!");
6996 assert(InVals.size() == Ins.size() &&
6997 "LowerFormalArguments didn't emit the correct number of values!");
Bill Wendling3ea58b62009-12-22 21:35:02 +00006998 DEBUG({
6999 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
7000 assert(InVals[i].getNode() &&
7001 "LowerFormalArguments emitted a null value!");
Duncan Sands1440e8b2010-11-03 11:35:31 +00007002 assert(EVT(Ins[i].VT) == InVals[i].getValueType() &&
Bill Wendling3ea58b62009-12-22 21:35:02 +00007003 "LowerFormalArguments emitted a value with the wrong type!");
7004 }
7005 });
Bill Wendling3ea3c242009-12-22 02:10:19 +00007006
Dan Gohman5e866062009-08-06 15:37:27 +00007007 // Update the DAG with the new chain value resulting from argument lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00007008 DAG.setRoot(NewRoot);
7009
7010 // Set up the argument values.
7011 unsigned i = 0;
7012 Idx = 1;
Dan Gohman7451d3e2010-05-29 17:03:36 +00007013 if (!FuncInfo->CanLowerReturn) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00007014 // Create a virtual register for the sret pointer, and put in a copy
7015 // from the sret argument into it.
7016 SmallVector<EVT, 1> ValueVTs;
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00007017 ComputeValueVTs(*TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00007018 MVT VT = ValueVTs[0].getSimpleVT();
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00007019 MVT RegVT = TLI->getRegisterType(*CurDAG->getContext(), VT);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00007020 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Bill Wendling46ada192010-03-02 01:55:18 +00007021 SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1,
Bill Wendling12931302012-09-26 04:04:19 +00007022 RegVT, VT, NULL, AssertOp);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00007023
Dan Gohman2048b852009-11-23 18:04:58 +00007024 MachineFunction& MF = SDB->DAG.getMachineFunction();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00007025 MachineRegisterInfo& RegInfo = MF.getRegInfo();
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00007026 unsigned SRetReg = RegInfo.createVirtualRegister(TLI->getRegClassFor(RegVT));
Dan Gohman7451d3e2010-05-29 17:03:36 +00007027 FuncInfo->DemoteRegister = SRetReg;
Andrew Trickac6d9be2013-05-25 02:42:55 +00007028 NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurSDLoc(),
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00007029 SRetReg, ArgValue);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00007030 DAG.setRoot(NewRoot);
Bill Wendling3ea3c242009-12-22 02:10:19 +00007031
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00007032 // i indexes lowered arguments. Bump it past the hidden sret argument.
7033 // Idx indexes LLVM arguments. Don't touch it.
7034 ++i;
7035 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00007036
Dan Gohman46510a72010-04-15 01:51:59 +00007037 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
Dan Gohman98ca4f22009-08-05 01:29:28 +00007038 ++I, ++Idx) {
7039 SmallVector<SDValue, 4> ArgValues;
Owen Andersone50ed302009-08-10 22:56:29 +00007040 SmallVector<EVT, 4> ValueVTs;
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00007041 ComputeValueVTs(*TLI, I->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007042 unsigned NumValues = ValueVTs.size();
Devang Patel9126c0d2010-06-01 19:59:01 +00007043
7044 // If this argument is unused then remember its value. It is used to generate
7045 // debugging information.
Adrian Prantldf688032013-05-16 23:44:12 +00007046 if (I->use_empty() && NumValues) {
Devang Patel9126c0d2010-06-01 19:59:01 +00007047 SDB->setUnusedArgValue(I, InVals[i]);
7048
Adrian Prantldf688032013-05-16 23:44:12 +00007049 // Also remember any frame index for use in FastISel.
7050 if (FrameIndexSDNode *FI =
7051 dyn_cast<FrameIndexSDNode>(InVals[i].getNode()))
7052 FuncInfo->setArgumentFrameIndex(I, FI->getIndex());
7053 }
7054
Eli Friedman23d32432011-05-05 16:53:34 +00007055 for (unsigned Val = 0; Val != NumValues; ++Val) {
7056 EVT VT = ValueVTs[Val];
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00007057 MVT PartVT = TLI->getRegisterType(*CurDAG->getContext(), VT);
7058 unsigned NumParts = TLI->getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00007059
7060 if (!I->use_empty()) {
7061 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Bill Wendling39cd0c82012-12-30 12:45:13 +00007062 if (F.getAttributes().hasAttribute(Idx, Attribute::SExt))
Dan Gohman98ca4f22009-08-05 01:29:28 +00007063 AssertOp = ISD::AssertSext;
Bill Wendling39cd0c82012-12-30 12:45:13 +00007064 else if (F.getAttributes().hasAttribute(Idx, Attribute::ZExt))
Dan Gohman98ca4f22009-08-05 01:29:28 +00007065 AssertOp = ISD::AssertZext;
7066
Bill Wendling46ada192010-03-02 01:55:18 +00007067 ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i],
Bill Wendling3ea3c242009-12-22 02:10:19 +00007068 NumParts, PartVT, VT,
Bill Wendling12931302012-09-26 04:04:19 +00007069 NULL, AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00007070 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00007071
Dan Gohman98ca4f22009-08-05 01:29:28 +00007072 i += NumParts;
7073 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00007074
Eli Friedman23d32432011-05-05 16:53:34 +00007075 // We don't need to do anything else for unused arguments.
7076 if (ArgValues.empty())
7077 continue;
7078
Devang Patel9aee3352011-09-08 22:59:09 +00007079 // Note down frame index.
7080 if (FrameIndexSDNode *FI =
Bill Wendling96cb1122012-07-19 00:04:14 +00007081 dyn_cast<FrameIndexSDNode>(ArgValues[0].getNode()))
Devang Patel9aee3352011-09-08 22:59:09 +00007082 FuncInfo->setArgumentFrameIndex(I, FI->getIndex());
Devang Patel0b48ead2010-08-31 22:22:42 +00007083
Eli Friedman23d32432011-05-05 16:53:34 +00007084 SDValue Res = DAG.getMergeValues(&ArgValues[0], NumValues,
Andrew Trickac6d9be2013-05-25 02:42:55 +00007085 SDB->getCurSDLoc());
Devang Patel9aee3352011-09-08 22:59:09 +00007086
Eli Friedman23d32432011-05-05 16:53:34 +00007087 SDB->setValue(I, Res);
Nick Lewycky8a8d4792011-12-02 22:16:29 +00007088 if (!TM.Options.EnableFastISel && Res.getOpcode() == ISD::BUILD_PAIR) {
Stephen Lin155615d2013-07-08 00:37:03 +00007089 if (LoadSDNode *LNode =
Devang Patel9aee3352011-09-08 22:59:09 +00007090 dyn_cast<LoadSDNode>(Res.getOperand(0).getNode()))
7091 if (FrameIndexSDNode *FI =
7092 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
7093 FuncInfo->setArgumentFrameIndex(I, FI->getIndex());
7094 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00007095
Eli Friedman23d32432011-05-05 16:53:34 +00007096 // If this argument is live outside of the entry block, insert a copy from
7097 // wherever we got it to the vreg that other BB's will reference it as.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00007098 if (!TM.Options.EnableFastISel && Res.getOpcode() == ISD::CopyFromReg) {
Eli Friedman23d32432011-05-05 16:53:34 +00007099 // If we can, though, try to skip creating an unnecessary vreg.
7100 // FIXME: This isn't very clean... it would be nice to make this more
Eli Friedman7f33d672011-05-10 21:50:58 +00007101 // general. It's also subtly incompatible with the hacks FastISel
7102 // uses with vregs.
Eli Friedman23d32432011-05-05 16:53:34 +00007103 unsigned Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg();
7104 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
7105 FuncInfo->ValueMap[I] = Reg;
7106 continue;
7107 }
7108 }
Nick Lewycky8a8d4792011-12-02 22:16:29 +00007109 if (!isOnlyUsedInEntryBlock(I, TM.Options.EnableFastISel)) {
Eli Friedman23d32432011-05-05 16:53:34 +00007110 FuncInfo->InitializeRegForValue(I);
Dan Gohman2048b852009-11-23 18:04:58 +00007111 SDB->CopyToExportRegsIfNeeded(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007112 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007113 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00007114
Dan Gohman98ca4f22009-08-05 01:29:28 +00007115 assert(i == InVals.size() && "Argument register count mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007116
7117 // Finally, if the target has anything special to do, allow it to do so.
7118 // FIXME: this should insert code into the DAG!
Dan Gohman64652652010-04-14 20:17:22 +00007119 EmitFunctionEntryCode();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007120}
7121
7122/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
7123/// ensure constants are generated when needed. Remember the virtual registers
7124/// that need to be added to the Machine PHI nodes as input. We cannot just
7125/// directly add them, because expansion might result in multiple MBB's for one
7126/// BB. As such, the start of the BB might correspond to a different MBB than
7127/// the end.
7128///
7129void
Dan Gohmanf81eca02010-04-22 20:46:50 +00007130SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
Dan Gohman46510a72010-04-15 01:51:59 +00007131 const TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007132
7133 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
7134
7135 // Check successor nodes' PHI nodes that expect a constant to be available
7136 // from this block.
7137 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
Dan Gohman46510a72010-04-15 01:51:59 +00007138 const BasicBlock *SuccBB = TI->getSuccessor(succ);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007139 if (!isa<PHINode>(SuccBB->begin())) continue;
Dan Gohmanf81eca02010-04-22 20:46:50 +00007140 MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00007141
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007142 // If this terminator has multiple identical successors (common for
7143 // switches), only handle each succ once.
7144 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00007145
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007146 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007147
7148 // At this point we know that there is a 1-1 correspondence between LLVM PHI
7149 // nodes and Machine PHI nodes, but the incoming operands have not been
7150 // emitted yet.
Dan Gohman46510a72010-04-15 01:51:59 +00007151 for (BasicBlock::const_iterator I = SuccBB->begin();
7152 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007153 // Ignore dead phi's.
7154 if (PN->use_empty()) continue;
7155
Rafael Espindola3fa82832011-05-13 15:18:06 +00007156 // Skip empty types
7157 if (PN->getType()->isEmptyTy())
7158 continue;
7159
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007160 unsigned Reg;
Dan Gohman46510a72010-04-15 01:51:59 +00007161 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007162
Dan Gohman46510a72010-04-15 01:51:59 +00007163 if (const Constant *C = dyn_cast<Constant>(PHIOp)) {
Dan Gohmanf81eca02010-04-22 20:46:50 +00007164 unsigned &RegOut = ConstantsOut[C];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007165 if (RegOut == 0) {
Dan Gohman89496d02010-07-02 00:10:16 +00007166 RegOut = FuncInfo.CreateRegs(C->getType());
Dan Gohmanf81eca02010-04-22 20:46:50 +00007167 CopyValueToVirtualRegister(C, RegOut);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007168 }
7169 Reg = RegOut;
7170 } else {
Dan Gohmanc25ad632010-07-01 01:33:21 +00007171 DenseMap<const Value *, unsigned>::iterator I =
7172 FuncInfo.ValueMap.find(PHIOp);
7173 if (I != FuncInfo.ValueMap.end())
7174 Reg = I->second;
7175 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007176 assert(isa<AllocaInst>(PHIOp) &&
Dan Gohmanf81eca02010-04-22 20:46:50 +00007177 FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007178 "Didn't codegen value into a register!??");
Dan Gohman89496d02010-07-02 00:10:16 +00007179 Reg = FuncInfo.CreateRegs(PHIOp->getType());
Dan Gohmanf81eca02010-04-22 20:46:50 +00007180 CopyValueToVirtualRegister(PHIOp, Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007181 }
7182 }
7183
7184 // Remember that this register needs to added to the machine PHI node as
7185 // the input for this MBB.
Owen Andersone50ed302009-08-10 22:56:29 +00007186 SmallVector<EVT, 4> ValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00007187 const TargetLowering *TLI = TM.getTargetLowering();
7188 ComputeValueVTs(*TLI, PN->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007189 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +00007190 EVT VT = ValueVTs[vti];
Bill Wendlingba54bca2013-06-19 21:36:55 +00007191 unsigned NumRegisters = TLI->getNumRegisters(*DAG.getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007192 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
Dan Gohmanf81eca02010-04-22 20:46:50 +00007193 FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007194 Reg += NumRegisters;
7195 }
7196 }
7197 }
Bill Wendlingba54bca2013-06-19 21:36:55 +00007198
Dan Gohmanf81eca02010-04-22 20:46:50 +00007199 ConstantsOut.clear();
Dan Gohman3df24e62008-09-03 23:12:08 +00007200}
Michael Gottesman657484f2013-08-20 07:00:16 +00007201
7202/// Add a successor MBB to ParentMBB< creating a new MachineBB for BB if SuccMBB
7203/// is 0.
7204MachineBasicBlock *
7205SelectionDAGBuilder::StackProtectorDescriptor::
7206AddSuccessorMBB(const BasicBlock *BB,
7207 MachineBasicBlock *ParentMBB,
7208 MachineBasicBlock *SuccMBB) {
7209 // If SuccBB has not been created yet, create it.
7210 if (!SuccMBB) {
7211 MachineFunction *MF = ParentMBB->getParent();
7212 MachineFunction::iterator BBI = ParentMBB;
7213 SuccMBB = MF->CreateMachineBasicBlock(BB);
7214 MF->insert(++BBI, SuccMBB);
7215 }
7216 // Add it as a successor of ParentMBB.
7217 ParentMBB->addSuccessor(SuccMBB);
7218 return SuccMBB;
7219}