blob: 9bb78978e11149add37ba70fda574d6a5eb90e42 [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
1745/// visitBitTestHeader - This function emits necessary code to produce value
1746/// suitable for "bit tests"
Dan Gohman99be8ae2010-04-19 22:41:47 +00001747void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B,
1748 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001749 // Subtract the minimum value
1750 SDValue SwitchOp = getValue(B.SValue);
Patrik Hagglund34525f92012-12-11 11:14:33 +00001751 EVT VT = SwitchOp.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001752 SDValue Sub = DAG.getNode(ISD::SUB, getCurSDLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001753 DAG.getConstant(B.First, VT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001754
1755 // Check range
Bill Wendlingba54bca2013-06-19 21:36:55 +00001756 const TargetLowering *TLI = TM.getTargetLowering();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001757 SDValue RangeCmp = DAG.getSetCC(getCurSDLoc(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00001758 TLI->getSetCCResultType(*DAG.getContext(),
Matt Arsenault225ed702013-05-18 00:21:46 +00001759 Sub.getValueType()),
Bill Wendling87710f02009-12-21 23:47:40 +00001760 Sub, DAG.getConstant(B.Range, VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001761 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001762
Evan Chengd08e5b42011-01-06 01:02:44 +00001763 // Determine the type of the test operands.
1764 bool UsePtrType = false;
Bill Wendlingba54bca2013-06-19 21:36:55 +00001765 if (!TLI->isTypeLegal(VT))
Evan Chengd08e5b42011-01-06 01:02:44 +00001766 UsePtrType = true;
1767 else {
1768 for (unsigned i = 0, e = B.Cases.size(); i != e; ++i)
Eli Friedman5c75af62011-10-12 22:46:45 +00001769 if (!isUIntN(VT.getSizeInBits(), B.Cases[i].Mask)) {
Evan Chengd08e5b42011-01-06 01:02:44 +00001770 // Switch table case range are encoded into series of masks.
1771 // Just use pointer type, it's guaranteed to fit.
1772 UsePtrType = true;
1773 break;
1774 }
1775 }
1776 if (UsePtrType) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00001777 VT = TLI->getPointerTy();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001778 Sub = DAG.getZExtOrTrunc(Sub, getCurSDLoc(), VT);
Evan Chengd08e5b42011-01-06 01:02:44 +00001779 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001780
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00001781 B.RegVT = VT.getSimpleVT();
Patrik Hagglund8963fec2012-12-19 12:23:01 +00001782 B.Reg = FuncInfo.CreateReg(B.RegVT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001783 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurSDLoc(),
Evan Chengd08e5b42011-01-06 01:02:44 +00001784 B.Reg, Sub);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001785
1786 // Set NextBlock to be the MBB immediately after the current one, if any.
1787 // This is used to avoid emitting unnecessary branches to the next block.
1788 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001789 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001790 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001791 NextBlock = BBI;
1792
1793 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1794
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001795 addSuccessorWithWeight(SwitchBB, B.Default);
1796 addSuccessorWithWeight(SwitchBB, MBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001797
Andrew Trickac6d9be2013-05-25 02:42:55 +00001798 SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurSDLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001799 MVT::Other, CopyTo, RangeCmp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001800 DAG.getBasicBlock(B.Default));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001801
Evan Cheng8c1f4322010-09-23 18:32:19 +00001802 if (MBB != NextBlock)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001803 BrRange = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, CopyTo,
Evan Cheng8c1f4322010-09-23 18:32:19 +00001804 DAG.getBasicBlock(MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001805
Bill Wendling87710f02009-12-21 23:47:40 +00001806 DAG.setRoot(BrRange);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001807}
1808
1809/// visitBitTestCase - this function produces one "bit test"
Evan Chengd08e5b42011-01-06 01:02:44 +00001810void SelectionDAGBuilder::visitBitTestCase(BitTestBlock &BB,
1811 MachineBasicBlock* NextMBB,
Manman Ren1a710fd2012-08-24 18:14:27 +00001812 uint32_t BranchWeightToNext,
Dan Gohman2048b852009-11-23 18:04:58 +00001813 unsigned Reg,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001814 BitTestCase &B,
1815 MachineBasicBlock *SwitchBB) {
Patrik Hagglund8963fec2012-12-19 12:23:01 +00001816 MVT VT = BB.RegVT;
Andrew Trickac6d9be2013-05-25 02:42:55 +00001817 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurSDLoc(),
Evan Chengd08e5b42011-01-06 01:02:44 +00001818 Reg, VT);
Dan Gohman8e0163a2010-06-24 02:06:24 +00001819 SDValue Cmp;
Benjamin Kramer3ff25512011-07-14 01:38:42 +00001820 unsigned PopCount = CountPopulation_64(B.Mask);
Bill Wendlingba54bca2013-06-19 21:36:55 +00001821 const TargetLowering *TLI = TM.getTargetLowering();
Benjamin Kramer3ff25512011-07-14 01:38:42 +00001822 if (PopCount == 1) {
Dan Gohman8e0163a2010-06-24 02:06:24 +00001823 // Testing for a single bit; just compare the shift count with what it
1824 // would need to be to shift a 1 bit in that position.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001825 Cmp = DAG.getSetCC(getCurSDLoc(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00001826 TLI->getSetCCResultType(*DAG.getContext(), VT),
Dan Gohman8e0163a2010-06-24 02:06:24 +00001827 ShiftOp,
Michael J. Spencerc6af2432013-05-24 22:23:49 +00001828 DAG.getConstant(countTrailingZeros(B.Mask), VT),
Dan Gohman8e0163a2010-06-24 02:06:24 +00001829 ISD::SETEQ);
Benjamin Kramer3ff25512011-07-14 01:38:42 +00001830 } else if (PopCount == BB.Range) {
1831 // There is only one zero bit in the range, test for it directly.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001832 Cmp = DAG.getSetCC(getCurSDLoc(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00001833 TLI->getSetCCResultType(*DAG.getContext(), VT),
Benjamin Kramer3ff25512011-07-14 01:38:42 +00001834 ShiftOp,
1835 DAG.getConstant(CountTrailingOnes_64(B.Mask), VT),
1836 ISD::SETNE);
Dan Gohman8e0163a2010-06-24 02:06:24 +00001837 } else {
1838 // Make desired shift
Andrew Trickac6d9be2013-05-25 02:42:55 +00001839 SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurSDLoc(), VT,
Evan Chengd08e5b42011-01-06 01:02:44 +00001840 DAG.getConstant(1, VT), ShiftOp);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001841
Dan Gohman8e0163a2010-06-24 02:06:24 +00001842 // Emit bit tests and jumps
Andrew Trickac6d9be2013-05-25 02:42:55 +00001843 SDValue AndOp = DAG.getNode(ISD::AND, getCurSDLoc(),
Evan Chengd08e5b42011-01-06 01:02:44 +00001844 VT, SwitchVal, DAG.getConstant(B.Mask, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00001845 Cmp = DAG.getSetCC(getCurSDLoc(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00001846 TLI->getSetCCResultType(*DAG.getContext(), VT),
Evan Chengd08e5b42011-01-06 01:02:44 +00001847 AndOp, DAG.getConstant(0, VT),
Dan Gohman8e0163a2010-06-24 02:06:24 +00001848 ISD::SETNE);
1849 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001850
Manman Ren1a710fd2012-08-24 18:14:27 +00001851 // The branch weight from SwitchBB to B.TargetBB is B.ExtraWeight.
1852 addSuccessorWithWeight(SwitchBB, B.TargetBB, B.ExtraWeight);
1853 // The branch weight from SwitchBB to NextMBB is BranchWeightToNext.
1854 addSuccessorWithWeight(SwitchBB, NextMBB, BranchWeightToNext);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001855
Andrew Trickac6d9be2013-05-25 02:42:55 +00001856 SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurSDLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001857 MVT::Other, getControlRoot(),
Dan Gohman8e0163a2010-06-24 02:06:24 +00001858 Cmp, DAG.getBasicBlock(B.TargetBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001859
1860 // Set NextBlock to be the MBB immediately after the current one, if any.
1861 // This is used to avoid emitting unnecessary branches to the next block.
1862 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001863 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001864 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001865 NextBlock = BBI;
1866
Evan Cheng8c1f4322010-09-23 18:32:19 +00001867 if (NextMBB != NextBlock)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001868 BrAnd = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, BrAnd,
Evan Cheng8c1f4322010-09-23 18:32:19 +00001869 DAG.getBasicBlock(NextMBB));
Bill Wendling0777e922009-12-21 21:59:52 +00001870
Bill Wendling87710f02009-12-21 23:47:40 +00001871 DAG.setRoot(BrAnd);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001872}
1873
Dan Gohman46510a72010-04-15 01:51:59 +00001874void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) {
Dan Gohman84023e02010-07-10 09:00:22 +00001875 MachineBasicBlock *InvokeMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001876
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001877 // Retrieve successors.
1878 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1879 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1880
Gabor Greifb67e6b32009-01-15 11:10:44 +00001881 const Value *Callee(I.getCalledValue());
Nuno Lopes85b40892012-06-28 22:30:12 +00001882 const Function *Fn = dyn_cast<Function>(Callee);
Gabor Greifb67e6b32009-01-15 11:10:44 +00001883 if (isa<InlineAsm>(Callee))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001884 visitInlineAsm(&I);
Nuno Lopes85b40892012-06-28 22:30:12 +00001885 else if (Fn && Fn->isIntrinsic()) {
1886 assert(Fn->getIntrinsicID() == Intrinsic::donothing);
Nuno Lopes4532bf62012-07-18 00:07:17 +00001887 // Ignore invokes to @llvm.donothing: jump directly to the next BB.
Nuno Lopes85b40892012-06-28 22:30:12 +00001888 } else
Gabor Greifb67e6b32009-01-15 11:10:44 +00001889 LowerCallTo(&I, getValue(Callee), false, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001890
1891 // If the value of the invoke is used outside of its defining block, make it
1892 // available as a virtual register.
Dan Gohmanad62f532009-04-23 23:13:24 +00001893 CopyToExportRegsIfNeeded(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001894
1895 // Update successor info
Chandler Carruthf2645682011-11-22 11:37:46 +00001896 addSuccessorWithWeight(InvokeMBB, Return);
1897 addSuccessorWithWeight(InvokeMBB, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001898
1899 // Drop into normal successor.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001900 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00001901 MVT::Other, getControlRoot(),
1902 DAG.getBasicBlock(Return)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001903}
1904
Bill Wendlingdccc03b2011-07-31 06:30:59 +00001905void SelectionDAGBuilder::visitResume(const ResumeInst &RI) {
1906 llvm_unreachable("SelectionDAGBuilder shouldn't visit resume instructions!");
1907}
1908
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00001909void SelectionDAGBuilder::visitLandingPad(const LandingPadInst &LP) {
1910 assert(FuncInfo.MBB->isLandingPad() &&
1911 "Call to landingpad not in landing pad!");
1912
1913 MachineBasicBlock *MBB = FuncInfo.MBB;
1914 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
1915 AddLandingPadInfo(LP, MMI, MBB);
1916
Bill Wendlingbdf9db62012-02-13 23:47:16 +00001917 // If there aren't registers to copy the values into (e.g., during SjLj
1918 // exceptions), then don't bother to create these DAG nodes.
Bill Wendlingba54bca2013-06-19 21:36:55 +00001919 const TargetLowering *TLI = TM.getTargetLowering();
1920 if (TLI->getExceptionPointerRegister() == 0 &&
1921 TLI->getExceptionSelectorRegister() == 0)
Bill Wendlingbdf9db62012-02-13 23:47:16 +00001922 return;
1923
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00001924 SmallVector<EVT, 2> ValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00001925 ComputeValueVTs(*TLI, LP.getType(), ValueVTs);
Jakob Stoklund Olesen918b7c82013-07-04 04:53:45 +00001926 assert(ValueVTs.size() == 2 && "Only two-valued landingpads are supported");
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00001927
Jakob Stoklund Olesen918b7c82013-07-04 04:53:45 +00001928 // Get the two live-in registers as SDValues. The physregs have already been
1929 // copied into virtual registers.
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00001930 SDValue Ops[2];
Jakob Stoklund Olesen918b7c82013-07-04 04:53:45 +00001931 Ops[0] = DAG.getZExtOrTrunc(
1932 DAG.getCopyFromReg(DAG.getEntryNode(), getCurSDLoc(),
1933 FuncInfo.ExceptionPointerVirtReg, TLI->getPointerTy()),
1934 getCurSDLoc(), ValueVTs[0]);
1935 Ops[1] = DAG.getZExtOrTrunc(
1936 DAG.getCopyFromReg(DAG.getEntryNode(), getCurSDLoc(),
1937 FuncInfo.ExceptionSelectorVirtReg, TLI->getPointerTy()),
1938 getCurSDLoc(), ValueVTs[1]);
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00001939
Jakob Stoklund Olesen918b7c82013-07-04 04:53:45 +00001940 // Merge into one.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001941 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00001942 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
1943 &Ops[0], 2);
Jakob Stoklund Olesen918b7c82013-07-04 04:53:45 +00001944 setValue(&LP, Res);
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00001945}
1946
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001947/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
1948/// small case ranges).
Dan Gohman2048b852009-11-23 18:04:58 +00001949bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR,
1950 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001951 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001952 MachineBasicBlock *Default,
1953 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001954 // Size is the number of Cases represented by this range.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001955 size_t Size = CR.Range.second - CR.Range.first;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001956 if (Size > 3)
Anton Korobeynikov23218582008-12-23 22:25:27 +00001957 return false;
1958
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001959 // Get the MachineFunction which holds the current MBB. This is used when
1960 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001961 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001962
1963 // Figure out which block is immediately after the current one.
1964 MachineBasicBlock *NextBlock = 0;
1965 MachineFunction::iterator BBI = CR.CaseBB;
1966
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001967 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001968 NextBlock = BBI;
1969
Manman Ren1a710fd2012-08-24 18:14:27 +00001970 BranchProbabilityInfo *BPI = FuncInfo.BPI;
Benjamin Kramerce750f02010-11-22 09:45:38 +00001971 // If any two of the cases has the same destination, and if one value
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001972 // is the same as the other, but has one bit unset that the other has set,
1973 // use bit manipulation to do two compares at once. For example:
1974 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
Benjamin Kramerce750f02010-11-22 09:45:38 +00001975 // TODO: This could be extended to merge any 2 cases in switches with 3 cases.
1976 // TODO: Handle cases where CR.CaseBB != SwitchBB.
1977 if (Size == 2 && CR.CaseBB == SwitchBB) {
1978 Case &Small = *CR.Range.first;
1979 Case &Big = *(CR.Range.second-1);
1980
1981 if (Small.Low == Small.High && Big.Low == Big.High && Small.BB == Big.BB) {
1982 const APInt& SmallValue = cast<ConstantInt>(Small.Low)->getValue();
1983 const APInt& BigValue = cast<ConstantInt>(Big.Low)->getValue();
1984
1985 // Check that there is only one bit different.
1986 if (BigValue.countPopulation() == SmallValue.countPopulation() + 1 &&
1987 (SmallValue | BigValue) == BigValue) {
1988 // Isolate the common bit.
1989 APInt CommonBit = BigValue & ~SmallValue;
1990 assert((SmallValue | CommonBit) == BigValue &&
1991 CommonBit.countPopulation() == 1 && "Not a common bit?");
1992
1993 SDValue CondLHS = getValue(SV);
1994 EVT VT = CondLHS.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001995 SDLoc DL = getCurSDLoc();
Benjamin Kramerce750f02010-11-22 09:45:38 +00001996
1997 SDValue Or = DAG.getNode(ISD::OR, DL, VT, CondLHS,
1998 DAG.getConstant(CommonBit, VT));
1999 SDValue Cond = DAG.getSetCC(DL, MVT::i1,
2000 Or, DAG.getConstant(BigValue, VT),
2001 ISD::SETEQ);
2002
2003 // Update successor info.
Manman Ren1a710fd2012-08-24 18:14:27 +00002004 // Both Small and Big will jump to Small.BB, so we sum up the weights.
2005 addSuccessorWithWeight(SwitchBB, Small.BB,
2006 Small.ExtraWeight + Big.ExtraWeight);
2007 addSuccessorWithWeight(SwitchBB, Default,
2008 // The default destination is the first successor in IR.
2009 BPI ? BPI->getEdgeWeight(SwitchBB->getBasicBlock(), (unsigned)0) : 0);
Benjamin Kramerce750f02010-11-22 09:45:38 +00002010
2011 // Insert the true branch.
2012 SDValue BrCond = DAG.getNode(ISD::BRCOND, DL, MVT::Other,
2013 getControlRoot(), Cond,
2014 DAG.getBasicBlock(Small.BB));
2015
2016 // Insert the false branch.
2017 BrCond = DAG.getNode(ISD::BR, DL, MVT::Other, BrCond,
2018 DAG.getBasicBlock(Default));
2019
2020 DAG.setRoot(BrCond);
2021 return true;
2022 }
2023 }
2024 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002025
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002026 // Order cases by weight so the most likely case will be checked first.
Manman Ren1a710fd2012-08-24 18:14:27 +00002027 uint32_t UnhandledWeights = 0;
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002028 if (BPI) {
2029 for (CaseItr I = CR.Range.first, IE = CR.Range.second; I != IE; ++I) {
Manman Ren1a710fd2012-08-24 18:14:27 +00002030 uint32_t IWeight = I->ExtraWeight;
2031 UnhandledWeights += IWeight;
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002032 for (CaseItr J = CR.Range.first; J < I; ++J) {
Manman Ren1a710fd2012-08-24 18:14:27 +00002033 uint32_t JWeight = J->ExtraWeight;
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002034 if (IWeight > JWeight)
2035 std::swap(*I, *J);
2036 }
2037 }
2038 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002039 // Rearrange the case blocks so that the last one falls through if possible.
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002040 Case &BackCase = *(CR.Range.second-1);
Benjamin Kramer5db954d2012-05-26 21:19:12 +00002041 if (Size > 1 &&
2042 NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002043 // The last case block won't fall through into 'NextBlock' if we emit the
2044 // branches in this order. See if rearranging a case value would help.
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002045 // We start at the bottom as it's the case with the least weight.
Stephen Lin09f8ca32013-07-06 21:44:25 +00002046 for (Case *I = &*(CR.Range.second-2), *E = &*CR.Range.first-1; I != E; --I)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002047 if (I->BB == NextBlock) {
2048 std::swap(*I, BackCase);
2049 break;
2050 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002051 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002052
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002053 // Create a CaseBlock record representing a conditional branch to
2054 // the Case's target mbb if the value being switched on SV is equal
2055 // to C.
2056 MachineBasicBlock *CurBlock = CR.CaseBB;
2057 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
2058 MachineBasicBlock *FallThrough;
2059 if (I != E-1) {
2060 FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
2061 CurMF->insert(BBI, FallThrough);
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002062
2063 // Put SV in a virtual register to make it available from the new blocks.
2064 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002065 } else {
2066 // If the last case doesn't match, go to the default block.
2067 FallThrough = Default;
2068 }
2069
Dan Gohman46510a72010-04-15 01:51:59 +00002070 const Value *RHS, *LHS, *MHS;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002071 ISD::CondCode CC;
2072 if (I->High == I->Low) {
2073 // This is just small small case range :) containing exactly 1 case
2074 CC = ISD::SETEQ;
2075 LHS = SV; RHS = I->High; MHS = NULL;
2076 } else {
Stephen Lin155615d2013-07-08 00:37:03 +00002077 CC = ISD::SETCC_INVALID;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002078 LHS = I->Low; MHS = SV; RHS = I->High;
2079 }
Jakub Staszakc8f34de2011-07-29 22:25:21 +00002080
Manman Ren1a710fd2012-08-24 18:14:27 +00002081 // The false weight should be sum of all un-handled cases.
2082 UnhandledWeights -= I->ExtraWeight;
Jakub Staszakc8f34de2011-07-29 22:25:21 +00002083 CaseBlock CB(CC, LHS, RHS, MHS, /* truebb */ I->BB, /* falsebb */ FallThrough,
2084 /* me */ CurBlock,
Manman Ren1a710fd2012-08-24 18:14:27 +00002085 /* trueweight */ I->ExtraWeight,
2086 /* falseweight */ UnhandledWeights);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002087
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002088 // If emitting the first comparison, just call visitSwitchCase to emit the
2089 // code into the current block. Otherwise, push the CaseBlock onto the
2090 // vector to be later processed by SDISel, and insert the node's MBB
2091 // before the next MBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002092 if (CurBlock == SwitchBB)
2093 visitSwitchCase(CB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002094 else
2095 SwitchCases.push_back(CB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002096
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002097 CurBlock = FallThrough;
2098 }
2099
2100 return true;
2101}
2102
2103static inline bool areJTsAllowed(const TargetLowering &TLI) {
Evan Cheng769951f2012-07-02 22:39:56 +00002104 return TLI.supportJumpTables() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00002105 (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
2106 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002107}
Anton Korobeynikov23218582008-12-23 22:25:27 +00002108
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002109static APInt ComputeRange(const APInt &First, const APInt &Last) {
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002110 uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1;
Stepan Dyatkovskiyc187df22012-05-17 08:56:30 +00002111 APInt LastExt = Last.zext(BitWidth), FirstExt = First.zext(BitWidth);
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002112 return (LastExt - FirstExt + 1ULL);
2113}
2114
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002115/// handleJTSwitchCase - Emit jumptable for current switch case range
Chris Lattnerc3ab3882011-09-09 22:06:59 +00002116bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec &CR,
2117 CaseRecVector &WorkList,
2118 const Value *SV,
2119 MachineBasicBlock *Default,
Dan Gohman99be8ae2010-04-19 22:41:47 +00002120 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002121 Case& FrontCase = *CR.Range.first;
2122 Case& BackCase = *(CR.Range.second-1);
2123
Chris Lattnere880efe2009-11-07 07:50:34 +00002124 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
2125 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002126
Chris Lattnere880efe2009-11-07 07:50:34 +00002127 APInt TSize(First.getBitWidth(), 0);
Chris Lattnerc3ab3882011-09-09 22:06:59 +00002128 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002129 TSize += I->size();
2130
Bill Wendlingba54bca2013-06-19 21:36:55 +00002131 const TargetLowering *TLI = TM.getTargetLowering();
2132 if (!areJTsAllowed(*TLI) || TSize.ult(TLI->getMinimumJumpTableEntries()))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002133 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002134
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002135 APInt Range = ComputeRange(First, Last);
Jakob Stoklund Olesen79443912011-10-26 01:47:48 +00002136 // The density is TSize / Range. Require at least 40%.
2137 // It should not be possible for IntTSize to saturate for sane code, but make
2138 // sure we handle Range saturation correctly.
2139 uint64_t IntRange = Range.getLimitedValue(UINT64_MAX/10);
2140 uint64_t IntTSize = TSize.getLimitedValue(UINT64_MAX/10);
2141 if (IntTSize * 10 < IntRange * 4)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002142 return false;
2143
David Greene4b69d992010-01-05 01:24:57 +00002144 DEBUG(dbgs() << "Lowering jump table\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002145 << "First entry: " << First << ". Last entry: " << Last << '\n'
Jakob Stoklund Olesen79443912011-10-26 01:47:48 +00002146 << "Range: " << Range << ". Size: " << TSize << ".\n\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002147
2148 // Get the MachineFunction which holds the current MBB. This is used when
2149 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002150 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002151
2152 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002153 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00002154 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002155
2156 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
2157
2158 // Create a new basic block to hold the code for loading the address
2159 // of the jump table, and jumping to it. Update successor information;
2160 // we will either branch to the default case for the switch, or the jump
2161 // table.
2162 MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2163 CurMF->insert(BBI, JumpTableBB);
Jakub Staszak7cc2b072011-06-16 20:22:37 +00002164
2165 addSuccessorWithWeight(CR.CaseBB, Default);
2166 addSuccessorWithWeight(CR.CaseBB, JumpTableBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002167
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002168 // Build a vector of destination BBs, corresponding to each target
2169 // of the jump table. If the value of the jump table slot corresponds to
2170 // a case statement, push the case's BB onto the vector, otherwise, push
2171 // the default BB.
2172 std::vector<MachineBasicBlock*> DestBBs;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002173 APInt TEI = First;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002174 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
Chris Lattner071c62f2010-01-25 23:26:13 +00002175 const APInt &Low = cast<ConstantInt>(I->Low)->getValue();
2176 const APInt &High = cast<ConstantInt>(I->High)->getValue();
Anton Korobeynikov23218582008-12-23 22:25:27 +00002177
Stepan Dyatkovskiyc187df22012-05-17 08:56:30 +00002178 if (Low.ule(TEI) && TEI.ule(High)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002179 DestBBs.push_back(I->BB);
2180 if (TEI==High)
2181 ++I;
2182 } else {
2183 DestBBs.push_back(Default);
2184 }
2185 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002186
Manman Ren1a710fd2012-08-24 18:14:27 +00002187 // Calculate weight for each unique destination in CR.
2188 DenseMap<MachineBasicBlock*, uint32_t> DestWeights;
2189 if (FuncInfo.BPI)
2190 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
2191 DenseMap<MachineBasicBlock*, uint32_t>::iterator Itr =
2192 DestWeights.find(I->BB);
Stephen Lin155615d2013-07-08 00:37:03 +00002193 if (Itr != DestWeights.end())
Manman Ren1a710fd2012-08-24 18:14:27 +00002194 Itr->second += I->ExtraWeight;
2195 else
2196 DestWeights[I->BB] = I->ExtraWeight;
2197 }
2198
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002199 // Update successor info. Add one edge to each unique successor.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002200 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
2201 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002202 E = DestBBs.end(); I != E; ++I) {
2203 if (!SuccsHandled[(*I)->getNumber()]) {
2204 SuccsHandled[(*I)->getNumber()] = true;
Manman Ren1a710fd2012-08-24 18:14:27 +00002205 DenseMap<MachineBasicBlock*, uint32_t>::iterator Itr =
2206 DestWeights.find(*I);
2207 addSuccessorWithWeight(JumpTableBB, *I,
2208 Itr != DestWeights.end() ? Itr->second : 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002209 }
2210 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002211
Bob Wilsond1ec31d2010-03-18 18:42:41 +00002212 // Create a jump table index for this jump table.
Bill Wendlingba54bca2013-06-19 21:36:55 +00002213 unsigned JTEncoding = TLI->getJumpTableEncoding();
Chris Lattner071c62f2010-01-25 23:26:13 +00002214 unsigned JTI = CurMF->getOrCreateJumpTableInfo(JTEncoding)
Bob Wilsond1ec31d2010-03-18 18:42:41 +00002215 ->createJumpTableIndex(DestBBs);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002216
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002217 // Set the jump table information so that we can codegen it as a second
2218 // MachineBasicBlock
2219 JumpTable JT(-1U, JTI, JumpTableBB, Default);
Dan Gohman99be8ae2010-04-19 22:41:47 +00002220 JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == SwitchBB));
2221 if (CR.CaseBB == SwitchBB)
2222 visitJumpTableHeader(JT, JTH, SwitchBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002223
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002224 JTCases.push_back(JumpTableBlock(JTH, JT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002225 return true;
2226}
2227
2228/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
2229/// 2 subtrees.
Dan Gohman2048b852009-11-23 18:04:58 +00002230bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR,
2231 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00002232 const Value* SV,
Stephen Lin09f8ca32013-07-06 21:44:25 +00002233 MachineBasicBlock* Default,
2234 MachineBasicBlock* SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002235 // Get the MachineFunction which holds the current MBB. This is used when
2236 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002237 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002238
2239 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002240 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00002241 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002242
2243 Case& FrontCase = *CR.Range.first;
2244 Case& BackCase = *(CR.Range.second-1);
2245 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
2246
2247 // Size is the number of Cases represented by this range.
2248 unsigned Size = CR.Range.second - CR.Range.first;
2249
Chris Lattnere880efe2009-11-07 07:50:34 +00002250 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
2251 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002252 double FMetric = 0;
2253 CaseItr Pivot = CR.Range.first + Size/2;
2254
2255 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
2256 // (heuristically) allow us to emit JumpTable's later.
Chris Lattnere880efe2009-11-07 07:50:34 +00002257 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002258 for (CaseItr I = CR.Range.first, E = CR.Range.second;
2259 I!=E; ++I)
2260 TSize += I->size();
2261
Chris Lattnere880efe2009-11-07 07:50:34 +00002262 APInt LSize = FrontCase.size();
2263 APInt RSize = TSize-LSize;
David Greene4b69d992010-01-05 01:24:57 +00002264 DEBUG(dbgs() << "Selecting best pivot: \n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002265 << "First: " << First << ", Last: " << Last <<'\n'
2266 << "LSize: " << LSize << ", RSize: " << RSize << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002267 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
2268 J!=E; ++I, ++J) {
Chris Lattnere880efe2009-11-07 07:50:34 +00002269 const APInt &LEnd = cast<ConstantInt>(I->High)->getValue();
2270 const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002271 APInt Range = ComputeRange(LEnd, RBegin);
Stepan Dyatkovskiyc2c52a62012-05-15 06:50:18 +00002272 assert((Range - 2ULL).isNonNegative() &&
2273 "Invalid case distance");
Chris Lattnerc3e4e592011-04-09 06:57:13 +00002274 // Use volatile double here to avoid excess precision issues on some hosts,
2275 // e.g. that use 80-bit X87 registers.
2276 volatile double LDensity =
2277 (double)LSize.roundToDouble() /
Chris Lattnere880efe2009-11-07 07:50:34 +00002278 (LEnd - First + 1ULL).roundToDouble();
Chris Lattnerc3e4e592011-04-09 06:57:13 +00002279 volatile double RDensity =
2280 (double)RSize.roundToDouble() /
Chris Lattnere880efe2009-11-07 07:50:34 +00002281 (Last - RBegin + 1ULL).roundToDouble();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002282 double Metric = Range.logBase2()*(LDensity+RDensity);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002283 // Should always split in some non-trivial place
David Greene4b69d992010-01-05 01:24:57 +00002284 DEBUG(dbgs() <<"=>Step\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002285 << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n'
2286 << "LDensity: " << LDensity
2287 << ", RDensity: " << RDensity << '\n'
2288 << "Metric: " << Metric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002289 if (FMetric < Metric) {
2290 Pivot = J;
2291 FMetric = Metric;
David Greene4b69d992010-01-05 01:24:57 +00002292 DEBUG(dbgs() << "Current metric set to: " << FMetric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002293 }
2294
2295 LSize += J->size();
2296 RSize -= J->size();
2297 }
Bill Wendlingba54bca2013-06-19 21:36:55 +00002298
2299 const TargetLowering *TLI = TM.getTargetLowering();
2300 if (areJTsAllowed(*TLI)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002301 // If our case is dense we *really* should handle it earlier!
2302 assert((FMetric > 0) && "Should handle dense range earlier!");
2303 } else {
2304 Pivot = CR.Range.first + Size/2;
2305 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002306
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002307 CaseRange LHSR(CR.Range.first, Pivot);
2308 CaseRange RHSR(Pivot, CR.Range.second);
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00002309 const Constant *C = Pivot->Low;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002310 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002311
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002312 // We know that we branch to the LHS if the Value being switched on is
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002313 // less than the Pivot value, C. We use this to optimize our binary
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002314 // tree a bit, by recognizing that if SV is greater than or equal to the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002315 // LHS's Case Value, and that Case Value is exactly one less than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002316 // Pivot's Value, then we can branch directly to the LHS's Target,
2317 // rather than creating a leaf node for it.
2318 if ((LHSR.second - LHSR.first) == 1 &&
2319 LHSR.first->High == CR.GE &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00002320 cast<ConstantInt>(C)->getValue() ==
2321 (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002322 TrueBB = LHSR.first->BB;
2323 } else {
2324 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2325 CurMF->insert(BBI, TrueBB);
2326 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002327
2328 // Put SV in a virtual register to make it available from the new blocks.
2329 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002330 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002331
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002332 // Similar to the optimization above, if the Value being switched on is
2333 // known to be less than the Constant CR.LT, and the current Case Value
2334 // is CR.LT - 1, then we can branch directly to the target block for
2335 // the current Case Value, rather than emitting a RHS leaf node for it.
2336 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00002337 cast<ConstantInt>(RHSR.first->Low)->getValue() ==
2338 (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002339 FalseBB = RHSR.first->BB;
2340 } else {
2341 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2342 CurMF->insert(BBI, FalseBB);
2343 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002344
2345 // Put SV in a virtual register to make it available from the new blocks.
2346 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002347 }
2348
2349 // Create a CaseBlock record representing a conditional branch to
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002350 // the LHS node if the value being switched on SV is less than C.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002351 // Otherwise, branch to LHS.
Stepan Dyatkovskiyc187df22012-05-17 08:56:30 +00002352 CaseBlock CB(ISD::SETULT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002353
Dan Gohman99be8ae2010-04-19 22:41:47 +00002354 if (CR.CaseBB == SwitchBB)
2355 visitSwitchCase(CB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002356 else
2357 SwitchCases.push_back(CB);
2358
2359 return true;
2360}
2361
2362/// handleBitTestsSwitchCase - if current case range has few destination and
2363/// range span less, than machine word bitwidth, encode case range into series
2364/// of masks and emit bit tests with these masks.
Dan Gohman2048b852009-11-23 18:04:58 +00002365bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR,
2366 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00002367 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00002368 MachineBasicBlock* Default,
Stephen Lin09f8ca32013-07-06 21:44:25 +00002369 MachineBasicBlock* SwitchBB) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00002370 const TargetLowering *TLI = TM.getTargetLowering();
2371 EVT PTy = TLI->getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002372 unsigned IntPtrBits = PTy.getSizeInBits();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002373
2374 Case& FrontCase = *CR.Range.first;
2375 Case& BackCase = *(CR.Range.second-1);
2376
2377 // Get the MachineFunction which holds the current MBB. This is used when
2378 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002379 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002380
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00002381 // If target does not have legal shift left, do not emit bit tests at all.
Bill Wendlingba54bca2013-06-19 21:36:55 +00002382 if (!TLI->isOperationLegal(ISD::SHL, TLI->getPointerTy()))
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00002383 return false;
2384
Anton Korobeynikov23218582008-12-23 22:25:27 +00002385 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002386 for (CaseItr I = CR.Range.first, E = CR.Range.second;
2387 I!=E; ++I) {
2388 // Single case counts one, case range - two.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002389 numCmps += (I->Low == I->High ? 1 : 2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002390 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002391
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002392 // Count unique destinations
2393 SmallSet<MachineBasicBlock*, 4> Dests;
2394 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
2395 Dests.insert(I->BB);
2396 if (Dests.size() > 3)
2397 // Don't bother the code below, if there are too much unique destinations
2398 return false;
2399 }
David Greene4b69d992010-01-05 01:24:57 +00002400 DEBUG(dbgs() << "Total number of unique destinations: "
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002401 << Dests.size() << '\n'
2402 << "Total number of comparisons: " << numCmps << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00002403
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002404 // Compute span of values.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002405 const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue();
2406 const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002407 APInt cmpRange = maxValue - minValue;
2408
David Greene4b69d992010-01-05 01:24:57 +00002409 DEBUG(dbgs() << "Compare range: " << cmpRange << '\n'
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002410 << "Low bound: " << minValue << '\n'
2411 << "High bound: " << maxValue << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00002412
Dan Gohmane0567812010-04-08 23:03:40 +00002413 if (cmpRange.uge(IntPtrBits) ||
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002414 (!(Dests.size() == 1 && numCmps >= 3) &&
2415 !(Dests.size() == 2 && numCmps >= 5) &&
2416 !(Dests.size() >= 3 && numCmps >= 6)))
2417 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002418
David Greene4b69d992010-01-05 01:24:57 +00002419 DEBUG(dbgs() << "Emitting bit tests\n");
Anton Korobeynikov23218582008-12-23 22:25:27 +00002420 APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth());
2421
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002422 // Optimize the case where all the case values fit in a
2423 // word without having to subtract minValue. In this case,
2424 // we can optimize away the subtraction.
Stepan Dyatkovskiyc187df22012-05-17 08:56:30 +00002425 if (maxValue.ult(IntPtrBits)) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002426 cmpRange = maxValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002427 } else {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002428 lowBound = minValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002429 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002430
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002431 CaseBitsVector CasesBits;
2432 unsigned i, count = 0;
2433
2434 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
2435 MachineBasicBlock* Dest = I->BB;
2436 for (i = 0; i < count; ++i)
2437 if (Dest == CasesBits[i].BB)
2438 break;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002439
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002440 if (i == count) {
2441 assert((count < 3) && "Too much destinations to test!");
Manman Ren1a710fd2012-08-24 18:14:27 +00002442 CasesBits.push_back(CaseBits(0, Dest, 0, 0/*Weight*/));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002443 count++;
2444 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002445
2446 const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue();
2447 const APInt& highValue = cast<ConstantInt>(I->High)->getValue();
2448
2449 uint64_t lo = (lowValue - lowBound).getZExtValue();
2450 uint64_t hi = (highValue - lowBound).getZExtValue();
Manman Ren1a710fd2012-08-24 18:14:27 +00002451 CasesBits[i].ExtraWeight += I->ExtraWeight;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002452
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002453 for (uint64_t j = lo; j <= hi; j++) {
2454 CasesBits[i].Mask |= 1ULL << j;
2455 CasesBits[i].Bits++;
2456 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002457
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002458 }
2459 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
Anton Korobeynikov23218582008-12-23 22:25:27 +00002460
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002461 BitTestInfo BTC;
2462
2463 // Figure out which block is immediately after the current one.
2464 MachineFunction::iterator BBI = CR.CaseBB;
2465 ++BBI;
2466
2467 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
2468
David Greene4b69d992010-01-05 01:24:57 +00002469 DEBUG(dbgs() << "Cases:\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002470 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
David Greene4b69d992010-01-05 01:24:57 +00002471 DEBUG(dbgs() << "Mask: " << CasesBits[i].Mask
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002472 << ", Bits: " << CasesBits[i].Bits
2473 << ", BB: " << CasesBits[i].BB << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002474
2475 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2476 CurMF->insert(BBI, CaseBB);
2477 BTC.push_back(BitTestCase(CasesBits[i].Mask,
2478 CaseBB,
Manman Ren1a710fd2012-08-24 18:14:27 +00002479 CasesBits[i].BB, CasesBits[i].ExtraWeight));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002480
2481 // Put SV in a virtual register to make it available from the new blocks.
2482 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002483 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002484
2485 BitTestBlock BTB(lowBound, cmpRange, SV,
Evan Chengd08e5b42011-01-06 01:02:44 +00002486 -1U, MVT::Other, (CR.CaseBB == SwitchBB),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002487 CR.CaseBB, Default, BTC);
2488
Dan Gohman99be8ae2010-04-19 22:41:47 +00002489 if (CR.CaseBB == SwitchBB)
2490 visitBitTestHeader(BTB, SwitchBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002491
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002492 BitTestCases.push_back(BTB);
2493
2494 return true;
2495}
2496
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002497/// Clusterify - Transform simple list of Cases into list of CaseRange's
Dan Gohman2048b852009-11-23 18:04:58 +00002498size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases,
2499 const SwitchInst& SI) {
Stephen Lin155615d2013-07-08 00:37:03 +00002500
Stepan Dyatkovskiy05cfe2e2012-05-18 08:32:28 +00002501 /// Use a shorter form of declaration, and also
2502 /// show the we want to use CRSBuilder as Clusterifier.
Stepan Dyatkovskiy4319a552012-06-02 07:26:00 +00002503 typedef IntegersSubsetMapping<MachineBasicBlock> Clusterifier;
Stephen Lin155615d2013-07-08 00:37:03 +00002504
Stepan Dyatkovskiy05cfe2e2012-05-18 08:32:28 +00002505 Clusterifier TheClusterifier;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002506
Manman Ren1a710fd2012-08-24 18:14:27 +00002507 BranchProbabilityInfo *BPI = FuncInfo.BPI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002508 // Start with "simple" cases
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002509 for (SwitchInst::ConstCaseIt i = SI.case_begin(), e = SI.case_end();
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002510 i != e; ++i) {
2511 const BasicBlock *SuccBB = i.getCaseSuccessor();
Jakub Staszakc8f34de2011-07-29 22:25:21 +00002512 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SuccBB];
2513
Stephen Lin155615d2013-07-08 00:37:03 +00002514 TheClusterifier.add(i.getCaseValueEx(), SMBB,
Manman Ren1a710fd2012-08-24 18:14:27 +00002515 BPI ? BPI->getEdgeWeight(SI.getParent(), i.getSuccessorIndex()) : 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002516 }
Stephen Lin155615d2013-07-08 00:37:03 +00002517
Stepan Dyatkovskiy05cfe2e2012-05-18 08:32:28 +00002518 TheClusterifier.optimize();
Stephen Lin155615d2013-07-08 00:37:03 +00002519
Stepan Dyatkovskiy05cfe2e2012-05-18 08:32:28 +00002520 size_t numCmps = 0;
2521 for (Clusterifier::RangeIterator i = TheClusterifier.begin(),
2522 e = TheClusterifier.end(); i != e; ++i, ++numCmps) {
Stepan Dyatkovskiy66d79ce2012-07-04 05:53:05 +00002523 Clusterifier::Cluster &C = *i;
Manman Ren1a710fd2012-08-24 18:14:27 +00002524 // Update edge weight for the cluster.
2525 unsigned W = C.first.Weight;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002526
Stepan Dyatkovskiy484fc932012-05-28 12:39:09 +00002527 // FIXME: Currently work with ConstantInt based numbers.
2528 // Changing it to APInt based is a pretty heavy for this commit.
Stepan Dyatkovskiy66d79ce2012-07-04 05:53:05 +00002529 Cases.push_back(Case(C.first.getLow().toConstantInt(),
2530 C.first.getHigh().toConstantInt(), C.second, W));
Stephen Lin155615d2013-07-08 00:37:03 +00002531
Stepan Dyatkovskiy66d79ce2012-07-04 05:53:05 +00002532 if (C.first.getLow() != C.first.getHigh())
Stepan Dyatkovskiy05cfe2e2012-05-18 08:32:28 +00002533 // A range counts double, since it requires two compares.
2534 ++numCmps;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002535 }
2536
2537 return numCmps;
2538}
2539
Jakob Stoklund Olesen2622f462010-09-30 19:44:31 +00002540void SelectionDAGBuilder::UpdateSplitBlock(MachineBasicBlock *First,
2541 MachineBasicBlock *Last) {
2542 // Update JTCases.
2543 for (unsigned i = 0, e = JTCases.size(); i != e; ++i)
2544 if (JTCases[i].first.HeaderBB == First)
2545 JTCases[i].first.HeaderBB = Last;
2546
2547 // Update BitTestCases.
2548 for (unsigned i = 0, e = BitTestCases.size(); i != e; ++i)
2549 if (BitTestCases[i].Parent == First)
2550 BitTestCases[i].Parent = Last;
2551}
2552
Dan Gohman46510a72010-04-15 01:51:59 +00002553void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) {
Dan Gohman84023e02010-07-10 09:00:22 +00002554 MachineBasicBlock *SwitchMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00002555
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002556 // Figure out which block is immediately after the current one.
2557 MachineBasicBlock *NextBlock = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002558 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
2559
2560 // If there is only the default destination, branch to it if it is not the
2561 // next basic block. Otherwise, just fall through.
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00002562 if (!SI.getNumCases()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002563 // Update machine-CFG edges.
2564
2565 // If this is not a fall-through branch, emit the branch.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002566 SwitchMBB->addSuccessor(Default);
Bill Wendling4533cac2010-01-28 21:51:40 +00002567 if (Default != NextBlock)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002568 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002569 MVT::Other, getControlRoot(),
2570 DAG.getBasicBlock(Default)));
Bill Wendling49fcff82009-12-21 22:30:11 +00002571
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002572 return;
2573 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002574
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002575 // If there are any non-default case statements, create a vector of Cases
2576 // representing each one, and sort the vector so that we can efficiently
2577 // create a binary search tree from them.
2578 CaseVector Cases;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002579 size_t numCmps = Clusterify(Cases, SI);
David Greene4b69d992010-01-05 01:24:57 +00002580 DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size()
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002581 << ". Total compares: " << numCmps << '\n');
Duncan Sands17001ce2011-10-18 12:44:00 +00002582 (void)numCmps;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002583
2584 // Get the Value to be switched on and default basic blocks, which will be
2585 // inserted into CaseBlock records, representing basic blocks in the binary
2586 // search tree.
Eli Friedmanbb5a7442011-09-29 20:21:17 +00002587 const Value *SV = SI.getCondition();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002588
2589 // Push the initial CaseRec onto the worklist
2590 CaseRecVector WorkList;
Dan Gohman99be8ae2010-04-19 22:41:47 +00002591 WorkList.push_back(CaseRec(SwitchMBB,0,0,
2592 CaseRange(Cases.begin(),Cases.end())));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002593
2594 while (!WorkList.empty()) {
2595 // Grab a record representing a case range to process off the worklist
2596 CaseRec CR = WorkList.back();
2597 WorkList.pop_back();
2598
Dan Gohman99be8ae2010-04-19 22:41:47 +00002599 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002600 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002601
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002602 // If the range has few cases (two or less) emit a series of specific
2603 // tests.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002604 if (handleSmallSwitchRange(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002605 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002606
Sebastian Pop1a37d7e2012-09-25 20:35:36 +00002607 // If the switch has more than N blocks, and is at least 40% dense, and the
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002608 // target supports indirect branches, then emit a jump table rather than
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002609 // lowering the switch to a binary tree of conditional branches.
Sebastian Pop1a37d7e2012-09-25 20:35:36 +00002610 // N defaults to 4 and is controlled via TLS.getMinimumJumpTableEntries().
Dan Gohman99be8ae2010-04-19 22:41:47 +00002611 if (handleJTSwitchCase(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002612 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002613
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002614 // Emit binary tree. We need to pick a pivot, and push left and right ranges
2615 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002616 handleBTSplitSwitchCase(CR, WorkList, SV, Default, SwitchMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002617 }
2618}
2619
Dan Gohman46510a72010-04-15 01:51:59 +00002620void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) {
Dan Gohman84023e02010-07-10 09:00:22 +00002621 MachineBasicBlock *IndirectBrMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00002622
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002623 // Update machine-CFG edges with unique successors.
Nadav Rotemee0ce152012-10-23 21:05:33 +00002624 SmallSet<BasicBlock*, 32> Done;
2625 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i) {
2626 BasicBlock *BB = I.getSuccessor(i);
2627 bool Inserted = Done.insert(BB);
2628 if (!Inserted)
2629 continue;
2630
2631 MachineBasicBlock *Succ = FuncInfo.MBBMap[BB];
Jakub Staszak7cc2b072011-06-16 20:22:37 +00002632 addSuccessorWithWeight(IndirectBrMBB, Succ);
2633 }
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002634
Andrew Trickac6d9be2013-05-25 02:42:55 +00002635 DAG.setRoot(DAG.getNode(ISD::BRIND, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002636 MVT::Other, getControlRoot(),
2637 getValue(I.getAddress())));
Bill Wendling49fcff82009-12-21 22:30:11 +00002638}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002639
Dan Gohman46510a72010-04-15 01:51:59 +00002640void SelectionDAGBuilder::visitFSub(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002641 // -0.0 - X --> fneg
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002642 Type *Ty = I.getType();
Chris Lattner2ca5c862011-02-15 00:14:00 +00002643 if (isa<Constant>(I.getOperand(0)) &&
2644 I.getOperand(0) == ConstantFP::getZeroValueForNegation(Ty)) {
2645 SDValue Op2 = getValue(I.getOperand(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00002646 setValue(&I, DAG.getNode(ISD::FNEG, getCurSDLoc(),
Chris Lattner2ca5c862011-02-15 00:14:00 +00002647 Op2.getValueType(), Op2));
2648 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002649 }
Bill Wendling49fcff82009-12-21 22:30:11 +00002650
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002651 visitBinary(I, ISD::FSUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002652}
2653
Dan Gohman46510a72010-04-15 01:51:59 +00002654void SelectionDAGBuilder::visitBinary(const User &I, unsigned OpCode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002655 SDValue Op1 = getValue(I.getOperand(0));
2656 SDValue Op2 = getValue(I.getOperand(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00002657 setValue(&I, DAG.getNode(OpCode, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002658 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002659}
2660
Dan Gohman46510a72010-04-15 01:51:59 +00002661void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002662 SDValue Op1 = getValue(I.getOperand(0));
2663 SDValue Op2 = getValue(I.getOperand(1));
Owen Anderson95771af2011-02-25 21:41:48 +00002664
Bill Wendlingba54bca2013-06-19 21:36:55 +00002665 EVT ShiftTy = TM.getTargetLowering()->getShiftAmountTy(Op2.getValueType());
Owen Anderson95771af2011-02-25 21:41:48 +00002666
Chris Lattnerd3027732011-02-13 09:02:52 +00002667 // Coerce the shift amount to the right type if we can.
2668 if (!I.getType()->isVectorTy() && Op2.getValueType() != ShiftTy) {
Chris Lattner915eeb42011-02-13 09:10:56 +00002669 unsigned ShiftSize = ShiftTy.getSizeInBits();
2670 unsigned Op2Size = Op2.getValueType().getSizeInBits();
Andrew Trickac6d9be2013-05-25 02:42:55 +00002671 SDLoc DL = getCurSDLoc();
Owen Anderson95771af2011-02-25 21:41:48 +00002672
Dan Gohman57fc82d2009-04-09 03:51:29 +00002673 // If the operand is smaller than the shift count type, promote it.
Chris Lattnerd3027732011-02-13 09:02:52 +00002674 if (ShiftSize > Op2Size)
2675 Op2 = DAG.getNode(ISD::ZERO_EXTEND, DL, ShiftTy, Op2);
Owen Anderson95771af2011-02-25 21:41:48 +00002676
Dan Gohman57fc82d2009-04-09 03:51:29 +00002677 // If the operand is larger than the shift count type but the shift
2678 // count type has enough bits to represent any shift value, truncate
2679 // it now. This is a common case and it exposes the truncate to
2680 // optimization early.
Chris Lattnerd3027732011-02-13 09:02:52 +00002681 else if (ShiftSize >= Log2_32_Ceil(Op2.getValueType().getSizeInBits()))
2682 Op2 = DAG.getNode(ISD::TRUNCATE, DL, ShiftTy, Op2);
2683 // Otherwise we'll need to temporarily settle for some other convenient
Chris Lattnere0751182011-02-13 19:09:16 +00002684 // type. Type legalization will make adjustments once the shiftee is split.
Chris Lattnerd3027732011-02-13 09:02:52 +00002685 else
Chris Lattnere0751182011-02-13 19:09:16 +00002686 Op2 = DAG.getZExtOrTrunc(Op2, DL, MVT::i32);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002687 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002688
Andrew Trickac6d9be2013-05-25 02:42:55 +00002689 setValue(&I, DAG.getNode(Opcode, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002690 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002691}
2692
Benjamin Kramer9c640302011-07-08 10:31:30 +00002693void SelectionDAGBuilder::visitSDiv(const User &I) {
Benjamin Kramer9c640302011-07-08 10:31:30 +00002694 SDValue Op1 = getValue(I.getOperand(0));
2695 SDValue Op2 = getValue(I.getOperand(1));
2696
2697 // Turn exact SDivs into multiplications.
2698 // FIXME: This should be in DAGCombiner, but it doesn't have access to the
2699 // exact bit.
Benjamin Kramer3492a4a2011-07-08 12:08:24 +00002700 if (isa<BinaryOperator>(&I) && cast<BinaryOperator>(&I)->isExact() &&
2701 !isa<ConstantSDNode>(Op1) &&
Benjamin Kramer9c640302011-07-08 10:31:30 +00002702 isa<ConstantSDNode>(Op2) && !cast<ConstantSDNode>(Op2)->isNullValue())
Bill Wendlingba54bca2013-06-19 21:36:55 +00002703 setValue(&I, TM.getTargetLowering()->BuildExactSDIV(Op1, Op2,
2704 getCurSDLoc(), DAG));
Benjamin Kramer9c640302011-07-08 10:31:30 +00002705 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00002706 setValue(&I, DAG.getNode(ISD::SDIV, getCurSDLoc(), Op1.getValueType(),
Benjamin Kramer9c640302011-07-08 10:31:30 +00002707 Op1, Op2));
2708}
2709
Dan Gohman46510a72010-04-15 01:51:59 +00002710void SelectionDAGBuilder::visitICmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002711 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002712 if (const ICmpInst *IC = dyn_cast<ICmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002713 predicate = IC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002714 else if (const ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002715 predicate = ICmpInst::Predicate(IC->getPredicate());
2716 SDValue Op1 = getValue(I.getOperand(0));
2717 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002718 ISD::CondCode Opcode = getICmpCondCode(predicate);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002719
Bill Wendlingba54bca2013-06-19 21:36:55 +00002720 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002721 setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Opcode));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002722}
2723
Dan Gohman46510a72010-04-15 01:51:59 +00002724void SelectionDAGBuilder::visitFCmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002725 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002726 if (const FCmpInst *FC = dyn_cast<FCmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002727 predicate = FC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002728 else if (const ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002729 predicate = FCmpInst::Predicate(FC->getPredicate());
2730 SDValue Op1 = getValue(I.getOperand(0));
2731 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002732 ISD::CondCode Condition = getFCmpCondCode(predicate);
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002733 if (TM.Options.NoNaNsFPMath)
2734 Condition = getFCmpCodeWithoutNaN(Condition);
Bill Wendlingba54bca2013-06-19 21:36:55 +00002735 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002736 setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Condition));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002737}
2738
Dan Gohman46510a72010-04-15 01:51:59 +00002739void SelectionDAGBuilder::visitSelect(const User &I) {
Owen Andersone50ed302009-08-10 22:56:29 +00002740 SmallVector<EVT, 4> ValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00002741 ComputeValueVTs(*TM.getTargetLowering(), I.getType(), ValueVTs);
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002742 unsigned NumValues = ValueVTs.size();
Bill Wendling49fcff82009-12-21 22:30:11 +00002743 if (NumValues == 0) return;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002744
Bill Wendling49fcff82009-12-21 22:30:11 +00002745 SmallVector<SDValue, 4> Values(NumValues);
2746 SDValue Cond = getValue(I.getOperand(0));
2747 SDValue TrueVal = getValue(I.getOperand(1));
2748 SDValue FalseVal = getValue(I.getOperand(2));
Duncan Sands28b77e92011-09-06 19:07:46 +00002749 ISD::NodeType OpCode = Cond.getValueType().isVector() ?
2750 ISD::VSELECT : ISD::SELECT;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002751
Bill Wendling4533cac2010-01-28 21:51:40 +00002752 for (unsigned i = 0; i != NumValues; ++i)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002753 Values[i] = DAG.getNode(OpCode, getCurSDLoc(),
Duncan Sands28b77e92011-09-06 19:07:46 +00002754 TrueVal.getNode()->getValueType(TrueVal.getResNo()+i),
Chris Lattnerb3e87b22010-03-12 07:15:36 +00002755 Cond,
Bill Wendling49fcff82009-12-21 22:30:11 +00002756 SDValue(TrueVal.getNode(),
2757 TrueVal.getResNo() + i),
2758 SDValue(FalseVal.getNode(),
2759 FalseVal.getResNo() + i));
2760
Andrew Trickac6d9be2013-05-25 02:42:55 +00002761 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002762 DAG.getVTList(&ValueVTs[0], NumValues),
2763 &Values[0], NumValues));
Bill Wendling49fcff82009-12-21 22:30:11 +00002764}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002765
Dan Gohman46510a72010-04-15 01:51:59 +00002766void SelectionDAGBuilder::visitTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002767 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2768 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002769 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002770 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002771}
2772
Dan Gohman46510a72010-04-15 01:51:59 +00002773void SelectionDAGBuilder::visitZExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002774 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2775 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2776 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002777 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002778 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002779}
2780
Dan Gohman46510a72010-04-15 01:51:59 +00002781void SelectionDAGBuilder::visitSExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002782 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2783 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2784 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002785 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002786 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002787}
2788
Dan Gohman46510a72010-04-15 01:51:59 +00002789void SelectionDAGBuilder::visitFPTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002790 // FPTrunc is never a no-op cast, no need to check
2791 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002792 const TargetLowering *TLI = TM.getTargetLowering();
2793 EVT DestVT = TLI->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002794 setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurSDLoc(),
Pete Cooperf57e1c22012-01-17 01:54:07 +00002795 DestVT, N,
Bill Wendlingba54bca2013-06-19 21:36:55 +00002796 DAG.getTargetConstant(0, TLI->getPointerTy())));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002797}
2798
Stephen Lin09f8ca32013-07-06 21:44:25 +00002799void SelectionDAGBuilder::visitFPExt(const User &I) {
Hal Finkel46bb70c2011-10-18 03:51:57 +00002800 // FPExt is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002801 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002802 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002803 setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002804}
2805
Dan Gohman46510a72010-04-15 01:51:59 +00002806void SelectionDAGBuilder::visitFPToUI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002807 // FPToUI is never a no-op cast, no need to check
2808 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002809 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002810 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002811}
2812
Dan Gohman46510a72010-04-15 01:51:59 +00002813void SelectionDAGBuilder::visitFPToSI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002814 // FPToSI is never a no-op cast, no need to check
2815 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002816 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002817 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002818}
2819
Dan Gohman46510a72010-04-15 01:51:59 +00002820void SelectionDAGBuilder::visitUIToFP(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002821 // UIToFP is never a no-op cast, no need to check
2822 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002823 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002824 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002825}
2826
Stephen Lin09f8ca32013-07-06 21:44:25 +00002827void SelectionDAGBuilder::visitSIToFP(const User &I) {
Bill Wendling181b6272008-10-19 20:34:04 +00002828 // SIToFP is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002829 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002830 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002831 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002832}
2833
Dan Gohman46510a72010-04-15 01:51:59 +00002834void SelectionDAGBuilder::visitPtrToInt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002835 // What to do depends on the size of the integer and the size of the pointer.
2836 // We can either truncate, zero extend, or no-op, accordingly.
2837 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002838 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002839 setValue(&I, DAG.getZExtOrTrunc(N, getCurSDLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002840}
2841
Dan Gohman46510a72010-04-15 01:51:59 +00002842void SelectionDAGBuilder::visitIntToPtr(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002843 // What to do depends on the size of the integer and the size of the pointer.
2844 // We can either truncate, zero extend, or no-op, accordingly.
2845 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002846 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002847 setValue(&I, DAG.getZExtOrTrunc(N, getCurSDLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002848}
2849
Dan Gohman46510a72010-04-15 01:51:59 +00002850void SelectionDAGBuilder::visitBitCast(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002851 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002852 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002853
Bill Wendling49fcff82009-12-21 22:30:11 +00002854 // BitCast assures us that source and destination are the same size so this is
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002855 // either a BITCAST or a no-op.
Bill Wendling4533cac2010-01-28 21:51:40 +00002856 if (DestVT != N.getValueType())
Andrew Trickac6d9be2013-05-25 02:42:55 +00002857 setValue(&I, DAG.getNode(ISD::BITCAST, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002858 DestVT, N)); // convert types.
2859 else
Bill Wendling49fcff82009-12-21 22:30:11 +00002860 setValue(&I, N); // noop cast.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002861}
2862
Dan Gohman46510a72010-04-15 01:51:59 +00002863void SelectionDAGBuilder::visitInsertElement(const User &I) {
Tom Stellard425b76c2013-08-05 22:22:01 +00002864 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002865 SDValue InVec = getValue(I.getOperand(0));
2866 SDValue InVal = getValue(I.getOperand(1));
Tom Stellard425b76c2013-08-05 22:22:01 +00002867 SDValue InIdx = DAG.getSExtOrTrunc(getValue(I.getOperand(2)),
2868 getCurSDLoc(), TLI.getVectorIdxTy());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002869 setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurSDLoc(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00002870 TM.getTargetLowering()->getValueType(I.getType()),
Bill Wendling4533cac2010-01-28 21:51:40 +00002871 InVec, InVal, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002872}
2873
Dan Gohman46510a72010-04-15 01:51:59 +00002874void SelectionDAGBuilder::visitExtractElement(const User &I) {
Tom Stellard425b76c2013-08-05 22:22:01 +00002875 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002876 SDValue InVec = getValue(I.getOperand(0));
Tom Stellard425b76c2013-08-05 22:22:01 +00002877 SDValue InIdx = DAG.getSExtOrTrunc(getValue(I.getOperand(1)),
2878 getCurSDLoc(), TLI.getVectorIdxTy());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002879 setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurSDLoc(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00002880 TM.getTargetLowering()->getValueType(I.getType()),
2881 InVec, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002882}
2883
Craig Topper51578342012-01-04 09:23:09 +00002884// Utility for visitShuffleVector - Return true if every element in Mask,
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00002885// beginning from position Pos and ending in Pos+Size, falls within the
Craig Topper51578342012-01-04 09:23:09 +00002886// specified sequential range [L, L+Pos). or is undef.
2887static bool isSequentialInRange(const SmallVectorImpl<int> &Mask,
Craig Topper23de31b2012-04-11 03:06:35 +00002888 unsigned Pos, unsigned Size, int Low) {
2889 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Craig Topper51578342012-01-04 09:23:09 +00002890 if (Mask[i] >= 0 && Mask[i] != Low)
Nate Begeman9008ca62009-04-27 18:41:29 +00002891 return false;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002892 return true;
2893}
2894
Dan Gohman46510a72010-04-15 01:51:59 +00002895void SelectionDAGBuilder::visitShuffleVector(const User &I) {
Mon P Wang230e4fa2008-11-21 04:25:21 +00002896 SDValue Src1 = getValue(I.getOperand(0));
2897 SDValue Src2 = getValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002898
Chris Lattner56243b82012-01-26 02:51:13 +00002899 SmallVector<int, 8> Mask;
2900 ShuffleVectorInst::getShuffleMask(cast<Constant>(I.getOperand(2)), Mask);
2901 unsigned MaskNumElts = Mask.size();
Bill Wendlingba54bca2013-06-19 21:36:55 +00002902
2903 const TargetLowering *TLI = TM.getTargetLowering();
2904 EVT VT = TLI->getValueType(I.getType());
Owen Andersone50ed302009-08-10 22:56:29 +00002905 EVT SrcVT = Src1.getValueType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00002906 unsigned SrcNumElts = SrcVT.getVectorNumElements();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002907
Mon P Wangc7849c22008-11-16 05:06:27 +00002908 if (SrcNumElts == MaskNumElts) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002909 setValue(&I, DAG.getVectorShuffle(VT, getCurSDLoc(), Src1, Src2,
Bill Wendling4533cac2010-01-28 21:51:40 +00002910 &Mask[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002911 return;
2912 }
2913
2914 // Normalize the shuffle vector since mask and vector length don't match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002915 if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) {
2916 // Mask is longer than the source vectors and is a multiple of the source
2917 // vectors. We can use concatenate vector to make the mask and vectors
Mon P Wang230e4fa2008-11-21 04:25:21 +00002918 // lengths match.
Craig Topper51578342012-01-04 09:23:09 +00002919 if (SrcNumElts*2 == MaskNumElts) {
2920 // First check for Src1 in low and Src2 in high
2921 if (isSequentialInRange(Mask, 0, SrcNumElts, 0) &&
2922 isSequentialInRange(Mask, SrcNumElts, SrcNumElts, SrcNumElts)) {
2923 // The shuffle is concatenating two vectors together.
Andrew Trickac6d9be2013-05-25 02:42:55 +00002924 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurSDLoc(),
Craig Topper51578342012-01-04 09:23:09 +00002925 VT, Src1, Src2));
2926 return;
2927 }
2928 // Then check for Src2 in low and Src1 in high
2929 if (isSequentialInRange(Mask, 0, SrcNumElts, SrcNumElts) &&
2930 isSequentialInRange(Mask, SrcNumElts, SrcNumElts, 0)) {
2931 // The shuffle is concatenating two vectors together.
Andrew Trickac6d9be2013-05-25 02:42:55 +00002932 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurSDLoc(),
Craig Topper51578342012-01-04 09:23:09 +00002933 VT, Src2, Src1));
2934 return;
2935 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002936 }
2937
Mon P Wangc7849c22008-11-16 05:06:27 +00002938 // Pad both vectors with undefs to make them the same length as the mask.
2939 unsigned NumConcat = MaskNumElts / SrcNumElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00002940 bool Src1U = Src1.getOpcode() == ISD::UNDEF;
2941 bool Src2U = Src2.getOpcode() == ISD::UNDEF;
Dale Johannesene8d72302009-02-06 23:05:02 +00002942 SDValue UndefVal = DAG.getUNDEF(SrcVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002943
Nate Begeman9008ca62009-04-27 18:41:29 +00002944 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
2945 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002946 MOps1[0] = Src1;
2947 MOps2[0] = Src2;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002948
2949 Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002950 getCurSDLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002951 &MOps1[0], NumConcat);
2952 Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002953 getCurSDLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002954 &MOps2[0], NumConcat);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002955
Mon P Wangaeb06d22008-11-10 04:46:22 +00002956 // Readjust mask for new input vector length.
Nate Begeman9008ca62009-04-27 18:41:29 +00002957 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002958 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002959 int Idx = Mask[i];
Craig Topper23de31b2012-04-11 03:06:35 +00002960 if (Idx >= (int)SrcNumElts)
2961 Idx -= SrcNumElts - MaskNumElts;
2962 MappedOps.push_back(Idx);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002963 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002964
Andrew Trickac6d9be2013-05-25 02:42:55 +00002965 setValue(&I, DAG.getVectorShuffle(VT, getCurSDLoc(), Src1, Src2,
Bill Wendling4533cac2010-01-28 21:51:40 +00002966 &MappedOps[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002967 return;
2968 }
2969
Mon P Wangc7849c22008-11-16 05:06:27 +00002970 if (SrcNumElts > MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002971 // Analyze the access pattern of the vector to see if we can extract
2972 // two subvectors and do the shuffle. The analysis is done by calculating
2973 // the range of elements the mask access on both vectors.
Craig Topper10612dc2012-04-08 23:15:04 +00002974 int MinRange[2] = { static_cast<int>(SrcNumElts),
2975 static_cast<int>(SrcNumElts)};
Mon P Wangc7849c22008-11-16 05:06:27 +00002976 int MaxRange[2] = {-1, -1};
2977
Nate Begeman5a5ca152009-04-29 05:20:52 +00002978 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002979 int Idx = Mask[i];
Craig Topper10612dc2012-04-08 23:15:04 +00002980 unsigned Input = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00002981 if (Idx < 0)
2982 continue;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002983
Nate Begeman5a5ca152009-04-29 05:20:52 +00002984 if (Idx >= (int)SrcNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002985 Input = 1;
2986 Idx -= SrcNumElts;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002987 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002988 if (Idx > MaxRange[Input])
2989 MaxRange[Input] = Idx;
2990 if (Idx < MinRange[Input])
2991 MinRange[Input] = Idx;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002992 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002993
Mon P Wangc7849c22008-11-16 05:06:27 +00002994 // Check if the access is smaller than the vector size and can we find
2995 // a reasonable extract index.
Craig Topper10612dc2012-04-08 23:15:04 +00002996 int RangeUse[2] = { -1, -1 }; // 0 = Unused, 1 = Extract, -1 = Can not
2997 // Extract.
Mon P Wangc7849c22008-11-16 05:06:27 +00002998 int StartIdx[2]; // StartIdx to extract from
Craig Topper10612dc2012-04-08 23:15:04 +00002999 for (unsigned Input = 0; Input < 2; ++Input) {
3000 if (MinRange[Input] >= (int)SrcNumElts && MaxRange[Input] < 0) {
Mon P Wangc7849c22008-11-16 05:06:27 +00003001 RangeUse[Input] = 0; // Unused
3002 StartIdx[Input] = 0;
Craig Topperf873dde2012-04-08 17:53:33 +00003003 continue;
Mon P Wang230e4fa2008-11-21 04:25:21 +00003004 }
Craig Topperf873dde2012-04-08 17:53:33 +00003005
3006 // Find a good start index that is a multiple of the mask length. Then
3007 // see if the rest of the elements are in range.
3008 StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts;
3009 if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts &&
3010 StartIdx[Input] + MaskNumElts <= SrcNumElts)
3011 RangeUse[Input] = 1; // Extract from a multiple of the mask length.
Mon P Wangc7849c22008-11-16 05:06:27 +00003012 }
3013
Bill Wendling636e2582009-08-21 18:16:06 +00003014 if (RangeUse[0] == 0 && RangeUse[1] == 0) {
Bill Wendling4533cac2010-01-28 21:51:40 +00003015 setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
Mon P Wangc7849c22008-11-16 05:06:27 +00003016 return;
3017 }
Craig Topper10612dc2012-04-08 23:15:04 +00003018 if (RangeUse[0] >= 0 && RangeUse[1] >= 0) {
Mon P Wangc7849c22008-11-16 05:06:27 +00003019 // Extract appropriate subvector and generate a vector shuffle
Craig Topper10612dc2012-04-08 23:15:04 +00003020 for (unsigned Input = 0; Input < 2; ++Input) {
Bill Wendling87710f02009-12-21 23:47:40 +00003021 SDValue &Src = Input == 0 ? Src1 : Src2;
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003022 if (RangeUse[Input] == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00003023 Src = DAG.getUNDEF(VT);
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003024 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00003025 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurSDLoc(), VT,
Tom Stellard425b76c2013-08-05 22:22:01 +00003026 Src, DAG.getConstant(StartIdx[Input],
3027 TLI->getVectorIdxTy()));
Mon P Wangaeb06d22008-11-10 04:46:22 +00003028 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003029
Mon P Wangc7849c22008-11-16 05:06:27 +00003030 // Calculate new mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00003031 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003032 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003033 int Idx = Mask[i];
Craig Topper23de31b2012-04-11 03:06:35 +00003034 if (Idx >= 0) {
3035 if (Idx < (int)SrcNumElts)
3036 Idx -= StartIdx[0];
3037 else
3038 Idx -= SrcNumElts + StartIdx[1] - MaskNumElts;
3039 }
3040 MappedOps.push_back(Idx);
Mon P Wangc7849c22008-11-16 05:06:27 +00003041 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003042
Andrew Trickac6d9be2013-05-25 02:42:55 +00003043 setValue(&I, DAG.getVectorShuffle(VT, getCurSDLoc(), Src1, Src2,
Bill Wendling4533cac2010-01-28 21:51:40 +00003044 &MappedOps[0]));
Mon P Wangc7849c22008-11-16 05:06:27 +00003045 return;
Mon P Wangaeb06d22008-11-10 04:46:22 +00003046 }
3047 }
3048
Mon P Wangc7849c22008-11-16 05:06:27 +00003049 // We can't use either concat vectors or extract subvectors so fall back to
3050 // replacing the shuffle with extract and build vector.
3051 // to insert and build vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003052 EVT EltVT = VT.getVectorElementType();
Tom Stellard425b76c2013-08-05 22:22:01 +00003053 EVT IdxVT = TLI->getVectorIdxTy();
Mon P Wangaeb06d22008-11-10 04:46:22 +00003054 SmallVector<SDValue,8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003055 for (unsigned i = 0; i != MaskNumElts; ++i) {
Craig Topper23de31b2012-04-11 03:06:35 +00003056 int Idx = Mask[i];
3057 SDValue Res;
3058
3059 if (Idx < 0) {
3060 Res = DAG.getUNDEF(EltVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00003061 } else {
Craig Topper23de31b2012-04-11 03:06:35 +00003062 SDValue &Src = Idx < (int)SrcNumElts ? Src1 : Src2;
3063 if (Idx >= (int)SrcNumElts) Idx -= SrcNumElts;
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003064
Andrew Trickac6d9be2013-05-25 02:42:55 +00003065 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurSDLoc(),
Tom Stellard425b76c2013-08-05 22:22:01 +00003066 EltVT, Src, DAG.getConstant(Idx, IdxVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00003067 }
Craig Topper23de31b2012-04-11 03:06:35 +00003068
3069 Ops.push_back(Res);
Mon P Wangaeb06d22008-11-10 04:46:22 +00003070 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003071
Andrew Trickac6d9be2013-05-25 02:42:55 +00003072 setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00003073 VT, &Ops[0], Ops.size()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003074}
3075
Dan Gohman46510a72010-04-15 01:51:59 +00003076void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003077 const Value *Op0 = I.getOperand(0);
3078 const Value *Op1 = I.getOperand(1);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003079 Type *AggTy = I.getType();
3080 Type *ValTy = Op1->getType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003081 bool IntoUndef = isa<UndefValue>(Op0);
3082 bool FromUndef = isa<UndefValue>(Op1);
3083
Jay Foadfc6d3a42011-07-13 10:26:04 +00003084 unsigned LinearIndex = ComputeLinearIndex(AggTy, I.getIndices());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003085
Bill Wendlingba54bca2013-06-19 21:36:55 +00003086 const TargetLowering *TLI = TM.getTargetLowering();
Owen Andersone50ed302009-08-10 22:56:29 +00003087 SmallVector<EVT, 4> AggValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003088 ComputeValueVTs(*TLI, AggTy, AggValueVTs);
Owen Andersone50ed302009-08-10 22:56:29 +00003089 SmallVector<EVT, 4> ValValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003090 ComputeValueVTs(*TLI, ValTy, ValValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003091
3092 unsigned NumAggValues = AggValueVTs.size();
3093 unsigned NumValValues = ValValueVTs.size();
3094 SmallVector<SDValue, 4> Values(NumAggValues);
3095
3096 SDValue Agg = getValue(Op0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003097 unsigned i = 0;
3098 // Copy the beginning value(s) from the original aggregate.
3099 for (; i != LinearIndex; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00003100 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003101 SDValue(Agg.getNode(), Agg.getResNo() + i);
3102 // Copy values from the inserted value(s).
Rafael Espindola3fa82832011-05-13 15:18:06 +00003103 if (NumValValues) {
3104 SDValue Val = getValue(Op1);
3105 for (; i != LinearIndex + NumValValues; ++i)
3106 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
3107 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
3108 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003109 // Copy remaining value(s) from the original aggregate.
3110 for (; i != NumAggValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00003111 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003112 SDValue(Agg.getNode(), Agg.getResNo() + i);
3113
Andrew Trickac6d9be2013-05-25 02:42:55 +00003114 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00003115 DAG.getVTList(&AggValueVTs[0], NumAggValues),
3116 &Values[0], NumAggValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003117}
3118
Dan Gohman46510a72010-04-15 01:51:59 +00003119void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003120 const Value *Op0 = I.getOperand(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003121 Type *AggTy = Op0->getType();
3122 Type *ValTy = I.getType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003123 bool OutOfUndef = isa<UndefValue>(Op0);
3124
Jay Foadfc6d3a42011-07-13 10:26:04 +00003125 unsigned LinearIndex = ComputeLinearIndex(AggTy, I.getIndices());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003126
Bill Wendlingba54bca2013-06-19 21:36:55 +00003127 const TargetLowering *TLI = TM.getTargetLowering();
Owen Andersone50ed302009-08-10 22:56:29 +00003128 SmallVector<EVT, 4> ValValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003129 ComputeValueVTs(*TLI, ValTy, ValValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003130
3131 unsigned NumValValues = ValValueVTs.size();
Rafael Espindola3fa82832011-05-13 15:18:06 +00003132
3133 // Ignore a extractvalue that produces an empty object
3134 if (!NumValValues) {
3135 setValue(&I, DAG.getUNDEF(MVT(MVT::Other)));
3136 return;
3137 }
3138
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003139 SmallVector<SDValue, 4> Values(NumValValues);
3140
3141 SDValue Agg = getValue(Op0);
3142 // Copy out the selected value(s).
3143 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
3144 Values[i - LinearIndex] =
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00003145 OutOfUndef ?
Dale Johannesene8d72302009-02-06 23:05:02 +00003146 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00003147 SDValue(Agg.getNode(), Agg.getResNo() + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003148
Andrew Trickac6d9be2013-05-25 02:42:55 +00003149 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00003150 DAG.getVTList(&ValValueVTs[0], NumValValues),
3151 &Values[0], NumValValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003152}
3153
Dan Gohman46510a72010-04-15 01:51:59 +00003154void SelectionDAGBuilder::visitGetElementPtr(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003155 SDValue N = getValue(I.getOperand(0));
Nadav Rotem1c239202012-02-28 14:13:19 +00003156 // Note that the pointer operand may be a vector of pointers. Take the scalar
3157 // element which holds a pointer.
3158 Type *Ty = I.getOperand(0)->getType()->getScalarType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003159
Dan Gohman46510a72010-04-15 01:51:59 +00003160 for (GetElementPtrInst::const_op_iterator OI = I.op_begin()+1, E = I.op_end();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003161 OI != E; ++OI) {
Dan Gohman46510a72010-04-15 01:51:59 +00003162 const Value *Idx = *OI;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003163 if (StructType *StTy = dyn_cast<StructType>(Ty)) {
Duncan Sandsb2df01a2012-11-13 13:01:58 +00003164 unsigned Field = cast<Constant>(Idx)->getUniqueInteger().getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003165 if (Field) {
3166 // N = N + Offset
3167 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003168 N = DAG.getNode(ISD::ADD, getCurSDLoc(), N.getValueType(), N,
Duncan Sandsb2df01a2012-11-13 13:01:58 +00003169 DAG.getConstant(Offset, N.getValueType()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003170 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00003171
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003172 Ty = StTy->getElementType(Field);
3173 } else {
3174 Ty = cast<SequentialType>(Ty)->getElementType();
3175
3176 // If this is a constant subscript, handle it quickly.
Bill Wendlingba54bca2013-06-19 21:36:55 +00003177 const TargetLowering *TLI = TM.getTargetLowering();
Dan Gohman46510a72010-04-15 01:51:59 +00003178 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Dan Gohmane368b462010-06-18 14:22:04 +00003179 if (CI->isZero()) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003180 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +00003181 TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Evan Cheng65b52df2009-02-09 21:01:06 +00003182 SDValue OffsVal;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003183 EVT PTy = TLI->getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00003184 unsigned PtrBits = PTy.getSizeInBits();
Bill Wendlinge1a90422009-12-21 23:10:19 +00003185 if (PtrBits < 64)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003186 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00003187 TLI->getPointerTy(),
Owen Anderson825b72b2009-08-11 20:47:22 +00003188 DAG.getConstant(Offs, MVT::i64));
Bill Wendlinge1a90422009-12-21 23:10:19 +00003189 else
Evan Chengb1032a82009-02-09 20:54:38 +00003190 OffsVal = DAG.getIntPtrConstant(Offs);
Bill Wendlinge1a90422009-12-21 23:10:19 +00003191
Andrew Trickac6d9be2013-05-25 02:42:55 +00003192 N = DAG.getNode(ISD::ADD, getCurSDLoc(), N.getValueType(), N,
Evan Chengb1032a82009-02-09 20:54:38 +00003193 OffsVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003194 continue;
3195 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003196
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003197 // N = N + Idx * ElementSize;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003198 APInt ElementSize = APInt(TLI->getPointerTy().getSizeInBits(),
Dan Gohman7abbd042009-10-23 17:57:43 +00003199 TD->getTypeAllocSize(Ty));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003200 SDValue IdxN = getValue(Idx);
3201
3202 // If the index is smaller or larger than intptr_t, truncate or extend
3203 // it.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003204 IdxN = DAG.getSExtOrTrunc(IdxN, getCurSDLoc(), N.getValueType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003205
3206 // If this is a multiply by a power of two, turn it into a shl
3207 // immediately. This is a very common case.
3208 if (ElementSize != 1) {
Dan Gohman7abbd042009-10-23 17:57:43 +00003209 if (ElementSize.isPowerOf2()) {
3210 unsigned Amt = ElementSize.logBase2();
Andrew Trickac6d9be2013-05-25 02:42:55 +00003211 IdxN = DAG.getNode(ISD::SHL, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003212 N.getValueType(), IdxN,
Nadav Rotem16087692011-12-05 06:29:09 +00003213 DAG.getConstant(Amt, IdxN.getValueType()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003214 } else {
Duncan Sandsb2df01a2012-11-13 13:01:58 +00003215 SDValue Scale = DAG.getConstant(ElementSize, IdxN.getValueType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003216 IdxN = DAG.getNode(ISD::MUL, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003217 N.getValueType(), IdxN, Scale);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003218 }
3219 }
3220
Andrew Trickac6d9be2013-05-25 02:42:55 +00003221 N = DAG.getNode(ISD::ADD, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003222 N.getValueType(), N, IdxN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003223 }
3224 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00003225
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003226 setValue(&I, N);
3227}
3228
Dan Gohman46510a72010-04-15 01:51:59 +00003229void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003230 // If this is a fixed sized alloca in the entry block of the function,
3231 // allocate it statically on the stack.
3232 if (FuncInfo.StaticAllocaMap.count(&I))
3233 return; // getValue will auto-populate this.
3234
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003235 Type *Ty = I.getAllocatedType();
Bill Wendlingba54bca2013-06-19 21:36:55 +00003236 const TargetLowering *TLI = TM.getTargetLowering();
3237 uint64_t TySize = TLI->getDataLayout()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003238 unsigned Align =
Bill Wendlingba54bca2013-06-19 21:36:55 +00003239 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003240 I.getAlignment());
3241
3242 SDValue AllocSize = getValue(I.getArraySize());
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003243
Bill Wendlingba54bca2013-06-19 21:36:55 +00003244 EVT IntPtr = TLI->getPointerTy();
Dan Gohmanf75a7d32010-05-28 01:14:11 +00003245 if (AllocSize.getValueType() != IntPtr)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003246 AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurSDLoc(), IntPtr);
Dan Gohmanf75a7d32010-05-28 01:14:11 +00003247
Andrew Trickac6d9be2013-05-25 02:42:55 +00003248 AllocSize = DAG.getNode(ISD::MUL, getCurSDLoc(), IntPtr,
Dan Gohmanf75a7d32010-05-28 01:14:11 +00003249 AllocSize,
3250 DAG.getConstant(TySize, IntPtr));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003251
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003252 // Handle alignment. If the requested alignment is less than or equal to
3253 // the stack alignment, ignore it. If the size is greater than or equal to
3254 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00003255 unsigned StackAlign = TM.getFrameLowering()->getStackAlignment();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003256 if (Align <= StackAlign)
3257 Align = 0;
3258
3259 // Round the size of the allocation up to the stack alignment size
3260 // by add SA-1 to the size.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003261 AllocSize = DAG.getNode(ISD::ADD, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003262 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003263 DAG.getIntPtrConstant(StackAlign-1));
Bill Wendling856ff412009-12-22 00:12:37 +00003264
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003265 // Mask out the low bits for alignment purposes.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003266 AllocSize = DAG.getNode(ISD::AND, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003267 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003268 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
3269
3270 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Owen Anderson825b72b2009-08-11 20:47:22 +00003271 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003272 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurSDLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003273 VTs, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003274 setValue(&I, DSA);
3275 DAG.setRoot(DSA.getValue(1));
Bill Wendling856ff412009-12-22 00:12:37 +00003276
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003277 // Inform the Frame Information that we have just allocated a variable-sized
3278 // object.
Bob Wilson8f637ad2013-02-08 20:35:15 +00003279 FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject(Align ? Align : 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003280}
3281
Dan Gohman46510a72010-04-15 01:51:59 +00003282void SelectionDAGBuilder::visitLoad(const LoadInst &I) {
Eli Friedman327236c2011-08-24 20:50:09 +00003283 if (I.isAtomic())
3284 return visitAtomicLoad(I);
3285
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003286 const Value *SV = I.getOperand(0);
3287 SDValue Ptr = getValue(SV);
3288
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003289 Type *Ty = I.getType();
David Greene1e559442010-02-15 17:00:31 +00003290
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003291 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00003292 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Pete Cooperd752e0f2011-11-08 18:42:53 +00003293 bool isInvariant = I.getMetadata("invariant.load") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003294 unsigned Alignment = I.getAlignment();
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00003295 const MDNode *TBAAInfo = I.getMetadata(LLVMContext::MD_tbaa);
Rafael Espindola95d594c2012-03-31 18:14:00 +00003296 const MDNode *Ranges = I.getMetadata(LLVMContext::MD_range);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003297
Owen Andersone50ed302009-08-10 22:56:29 +00003298 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003299 SmallVector<uint64_t, 4> Offsets;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003300 ComputeValueVTs(*TM.getTargetLowering(), Ty, ValueVTs, &Offsets);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003301 unsigned NumValues = ValueVTs.size();
3302 if (NumValues == 0)
3303 return;
3304
3305 SDValue Root;
3306 bool ConstantMemory = false;
Andrew Trickde91f3c2010-11-12 17:50:46 +00003307 if (I.isVolatile() || NumValues > MaxParallelChains)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003308 // Serialize volatile loads with other side effects.
3309 Root = getRoot();
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00003310 else if (AA->pointsToConstantMemory(
3311 AliasAnalysis::Location(SV, AA->getTypeStoreSize(Ty), TBAAInfo))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003312 // Do not serialize (non-volatile) loads of constant memory with anything.
3313 Root = DAG.getEntryNode();
3314 ConstantMemory = true;
3315 } else {
3316 // Do not serialize non-volatile loads against each other.
3317 Root = DAG.getRoot();
3318 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003319
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003320 SmallVector<SDValue, 4> Values(NumValues);
Andrew Trickde91f3c2010-11-12 17:50:46 +00003321 SmallVector<SDValue, 4> Chains(std::min(unsigned(MaxParallelChains),
3322 NumValues));
Owen Andersone50ed302009-08-10 22:56:29 +00003323 EVT PtrVT = Ptr.getValueType();
Andrew Trickde91f3c2010-11-12 17:50:46 +00003324 unsigned ChainI = 0;
3325 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
3326 // Serializing loads here may result in excessive register pressure, and
3327 // TokenFactor places arbitrary choke points on the scheduler. SD scheduling
3328 // could recover a bit by hoisting nodes upward in the chain by recognizing
3329 // they are side-effect free or do not alias. The optimizer should really
3330 // avoid this case by converting large object/array copies to llvm.memcpy
3331 // (MaxParallelChains should always remain as failsafe).
3332 if (ChainI == MaxParallelChains) {
3333 assert(PendingLoads.empty() && "PendingLoads must be serialized first");
Andrew Trickac6d9be2013-05-25 02:42:55 +00003334 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003335 MVT::Other, &Chains[0], ChainI);
3336 Root = Chain;
3337 ChainI = 0;
3338 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00003339 SDValue A = DAG.getNode(ISD::ADD, getCurSDLoc(),
Bill Wendling856ff412009-12-22 00:12:37 +00003340 PtrVT, Ptr,
3341 DAG.getConstant(Offsets[i], PtrVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003342 SDValue L = DAG.getLoad(ValueVTs[i], getCurSDLoc(), Root,
Michael J. Spencere70c5262010-10-16 08:25:21 +00003343 A, MachinePointerInfo(SV, Offsets[i]), isVolatile,
Rafael Espindola95d594c2012-03-31 18:14:00 +00003344 isNonTemporal, isInvariant, Alignment, TBAAInfo,
3345 Ranges);
Bill Wendling856ff412009-12-22 00:12:37 +00003346
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003347 Values[i] = L;
Andrew Trickde91f3c2010-11-12 17:50:46 +00003348 Chains[ChainI] = L.getValue(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003349 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003350
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003351 if (!ConstantMemory) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003352 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003353 MVT::Other, &Chains[0], ChainI);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003354 if (isVolatile)
3355 DAG.setRoot(Chain);
3356 else
3357 PendingLoads.push_back(Chain);
3358 }
3359
Andrew Trickac6d9be2013-05-25 02:42:55 +00003360 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00003361 DAG.getVTList(&ValueVTs[0], NumValues),
3362 &Values[0], NumValues));
Bill Wendling856ff412009-12-22 00:12:37 +00003363}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003364
Dan Gohman46510a72010-04-15 01:51:59 +00003365void SelectionDAGBuilder::visitStore(const StoreInst &I) {
Eli Friedman327236c2011-08-24 20:50:09 +00003366 if (I.isAtomic())
3367 return visitAtomicStore(I);
3368
Dan Gohman46510a72010-04-15 01:51:59 +00003369 const Value *SrcV = I.getOperand(0);
3370 const Value *PtrV = I.getOperand(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003371
Owen Andersone50ed302009-08-10 22:56:29 +00003372 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003373 SmallVector<uint64_t, 4> Offsets;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003374 ComputeValueVTs(*TM.getTargetLowering(), SrcV->getType(), ValueVTs, &Offsets);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003375 unsigned NumValues = ValueVTs.size();
3376 if (NumValues == 0)
3377 return;
3378
3379 // Get the lowered operands. Note that we do this after
3380 // checking if NumResults is zero, because with zero results
3381 // the operands won't have values in the map.
3382 SDValue Src = getValue(SrcV);
3383 SDValue Ptr = getValue(PtrV);
3384
3385 SDValue Root = getRoot();
Andrew Trickde91f3c2010-11-12 17:50:46 +00003386 SmallVector<SDValue, 4> Chains(std::min(unsigned(MaxParallelChains),
3387 NumValues));
Owen Andersone50ed302009-08-10 22:56:29 +00003388 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003389 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00003390 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003391 unsigned Alignment = I.getAlignment();
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00003392 const MDNode *TBAAInfo = I.getMetadata(LLVMContext::MD_tbaa);
Bill Wendling856ff412009-12-22 00:12:37 +00003393
Andrew Trickde91f3c2010-11-12 17:50:46 +00003394 unsigned ChainI = 0;
3395 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
3396 // See visitLoad comments.
3397 if (ChainI == MaxParallelChains) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003398 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003399 MVT::Other, &Chains[0], ChainI);
3400 Root = Chain;
3401 ChainI = 0;
3402 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00003403 SDValue Add = DAG.getNode(ISD::ADD, getCurSDLoc(), PtrVT, Ptr,
Bill Wendling856ff412009-12-22 00:12:37 +00003404 DAG.getConstant(Offsets[i], PtrVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003405 SDValue St = DAG.getStore(Root, getCurSDLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003406 SDValue(Src.getNode(), Src.getResNo() + i),
3407 Add, MachinePointerInfo(PtrV, Offsets[i]),
3408 isVolatile, isNonTemporal, Alignment, TBAAInfo);
3409 Chains[ChainI] = St;
Bill Wendling856ff412009-12-22 00:12:37 +00003410 }
3411
Andrew Trickac6d9be2013-05-25 02:42:55 +00003412 SDValue StoreNode = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003413 MVT::Other, &Chains[0], ChainI);
Devang Patel7e13efa2010-10-26 22:14:52 +00003414 DAG.setRoot(StoreNode);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003415}
3416
Eli Friedman26689ac2011-08-03 21:06:02 +00003417static SDValue InsertFenceForAtomic(SDValue Chain, AtomicOrdering Order,
Eli Friedman327236c2011-08-24 20:50:09 +00003418 SynchronizationScope Scope,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003419 bool Before, SDLoc dl,
Eli Friedman26689ac2011-08-03 21:06:02 +00003420 SelectionDAG &DAG,
3421 const TargetLowering &TLI) {
3422 // Fence, if necessary
3423 if (Before) {
Eli Friedman069e2ed2011-08-26 02:59:24 +00003424 if (Order == AcquireRelease || Order == SequentiallyConsistent)
Eli Friedman26689ac2011-08-03 21:06:02 +00003425 Order = Release;
3426 else if (Order == Acquire || Order == Monotonic)
3427 return Chain;
3428 } else {
3429 if (Order == AcquireRelease)
3430 Order = Acquire;
3431 else if (Order == Release || Order == Monotonic)
3432 return Chain;
3433 }
3434 SDValue Ops[3];
3435 Ops[0] = Chain;
Eli Friedman327236c2011-08-24 20:50:09 +00003436 Ops[1] = DAG.getConstant(Order, TLI.getPointerTy());
3437 Ops[2] = DAG.getConstant(Scope, TLI.getPointerTy());
Eli Friedman26689ac2011-08-03 21:06:02 +00003438 return DAG.getNode(ISD::ATOMIC_FENCE, dl, MVT::Other, Ops, 3);
3439}
3440
Eli Friedmanff030482011-07-28 21:48:00 +00003441void SelectionDAGBuilder::visitAtomicCmpXchg(const AtomicCmpXchgInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003442 SDLoc dl = getCurSDLoc();
Eli Friedman26689ac2011-08-03 21:06:02 +00003443 AtomicOrdering Order = I.getOrdering();
Eli Friedman327236c2011-08-24 20:50:09 +00003444 SynchronizationScope Scope = I.getSynchScope();
Eli Friedman26689ac2011-08-03 21:06:02 +00003445
3446 SDValue InChain = getRoot();
3447
Bill Wendlingba54bca2013-06-19 21:36:55 +00003448 const TargetLowering *TLI = TM.getTargetLowering();
3449 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003450 InChain = InsertFenceForAtomic(InChain, Order, Scope, true, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003451 DAG, *TLI);
Eli Friedman26689ac2011-08-03 21:06:02 +00003452
Eli Friedman55ba8162011-07-29 03:05:32 +00003453 SDValue L =
Eli Friedman26689ac2011-08-03 21:06:02 +00003454 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl,
Craig Topper0ff11902013-08-15 02:44:19 +00003455 getValue(I.getCompareOperand()).getSimpleValueType(),
Eli Friedman26689ac2011-08-03 21:06:02 +00003456 InChain,
Eli Friedman55ba8162011-07-29 03:05:32 +00003457 getValue(I.getPointerOperand()),
3458 getValue(I.getCompareOperand()),
3459 getValue(I.getNewValOperand()),
3460 MachinePointerInfo(I.getPointerOperand()), 0 /* Alignment */,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003461 TLI->getInsertFencesForAtomic() ? Monotonic : Order,
Eli Friedman327236c2011-08-24 20:50:09 +00003462 Scope);
Eli Friedman26689ac2011-08-03 21:06:02 +00003463
3464 SDValue OutChain = L.getValue(1);
3465
Bill Wendlingba54bca2013-06-19 21:36:55 +00003466 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003467 OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003468 DAG, *TLI);
Eli Friedman26689ac2011-08-03 21:06:02 +00003469
Eli Friedman55ba8162011-07-29 03:05:32 +00003470 setValue(&I, L);
Eli Friedman26689ac2011-08-03 21:06:02 +00003471 DAG.setRoot(OutChain);
Eli Friedmanff030482011-07-28 21:48:00 +00003472}
3473
3474void SelectionDAGBuilder::visitAtomicRMW(const AtomicRMWInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003475 SDLoc dl = getCurSDLoc();
Eli Friedman55ba8162011-07-29 03:05:32 +00003476 ISD::NodeType NT;
3477 switch (I.getOperation()) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003478 default: llvm_unreachable("Unknown atomicrmw operation");
Eli Friedman55ba8162011-07-29 03:05:32 +00003479 case AtomicRMWInst::Xchg: NT = ISD::ATOMIC_SWAP; break;
3480 case AtomicRMWInst::Add: NT = ISD::ATOMIC_LOAD_ADD; break;
3481 case AtomicRMWInst::Sub: NT = ISD::ATOMIC_LOAD_SUB; break;
3482 case AtomicRMWInst::And: NT = ISD::ATOMIC_LOAD_AND; break;
3483 case AtomicRMWInst::Nand: NT = ISD::ATOMIC_LOAD_NAND; break;
3484 case AtomicRMWInst::Or: NT = ISD::ATOMIC_LOAD_OR; break;
3485 case AtomicRMWInst::Xor: NT = ISD::ATOMIC_LOAD_XOR; break;
3486 case AtomicRMWInst::Max: NT = ISD::ATOMIC_LOAD_MAX; break;
3487 case AtomicRMWInst::Min: NT = ISD::ATOMIC_LOAD_MIN; break;
3488 case AtomicRMWInst::UMax: NT = ISD::ATOMIC_LOAD_UMAX; break;
3489 case AtomicRMWInst::UMin: NT = ISD::ATOMIC_LOAD_UMIN; break;
3490 }
Eli Friedman26689ac2011-08-03 21:06:02 +00003491 AtomicOrdering Order = I.getOrdering();
Eli Friedman327236c2011-08-24 20:50:09 +00003492 SynchronizationScope Scope = I.getSynchScope();
Eli Friedman26689ac2011-08-03 21:06:02 +00003493
3494 SDValue InChain = getRoot();
3495
Bill Wendlingba54bca2013-06-19 21:36:55 +00003496 const TargetLowering *TLI = TM.getTargetLowering();
3497 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003498 InChain = InsertFenceForAtomic(InChain, Order, Scope, true, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003499 DAG, *TLI);
Eli Friedman26689ac2011-08-03 21:06:02 +00003500
Eli Friedman55ba8162011-07-29 03:05:32 +00003501 SDValue L =
Eli Friedman26689ac2011-08-03 21:06:02 +00003502 DAG.getAtomic(NT, dl,
Craig Topper0ff11902013-08-15 02:44:19 +00003503 getValue(I.getValOperand()).getSimpleValueType(),
Eli Friedman26689ac2011-08-03 21:06:02 +00003504 InChain,
Eli Friedman55ba8162011-07-29 03:05:32 +00003505 getValue(I.getPointerOperand()),
3506 getValue(I.getValOperand()),
3507 I.getPointerOperand(), 0 /* Alignment */,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003508 TLI->getInsertFencesForAtomic() ? Monotonic : Order,
Eli Friedman327236c2011-08-24 20:50:09 +00003509 Scope);
Eli Friedman26689ac2011-08-03 21:06:02 +00003510
3511 SDValue OutChain = L.getValue(1);
3512
Bill Wendlingba54bca2013-06-19 21:36:55 +00003513 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003514 OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003515 DAG, *TLI);
Eli Friedman26689ac2011-08-03 21:06:02 +00003516
Eli Friedman55ba8162011-07-29 03:05:32 +00003517 setValue(&I, L);
Eli Friedman26689ac2011-08-03 21:06:02 +00003518 DAG.setRoot(OutChain);
Eli Friedmanff030482011-07-28 21:48:00 +00003519}
3520
Eli Friedman47f35132011-07-25 23:16:38 +00003521void SelectionDAGBuilder::visitFence(const FenceInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003522 SDLoc dl = getCurSDLoc();
Bill Wendlingba54bca2013-06-19 21:36:55 +00003523 const TargetLowering *TLI = TM.getTargetLowering();
Eli Friedman14648462011-07-27 22:21:52 +00003524 SDValue Ops[3];
3525 Ops[0] = getRoot();
Bill Wendlingba54bca2013-06-19 21:36:55 +00003526 Ops[1] = DAG.getConstant(I.getOrdering(), TLI->getPointerTy());
3527 Ops[2] = DAG.getConstant(I.getSynchScope(), TLI->getPointerTy());
Eli Friedman14648462011-07-27 22:21:52 +00003528 DAG.setRoot(DAG.getNode(ISD::ATOMIC_FENCE, dl, MVT::Other, Ops, 3));
Eli Friedman47f35132011-07-25 23:16:38 +00003529}
3530
Eli Friedman327236c2011-08-24 20:50:09 +00003531void SelectionDAGBuilder::visitAtomicLoad(const LoadInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003532 SDLoc dl = getCurSDLoc();
Eli Friedman327236c2011-08-24 20:50:09 +00003533 AtomicOrdering Order = I.getOrdering();
3534 SynchronizationScope Scope = I.getSynchScope();
3535
3536 SDValue InChain = getRoot();
3537
Bill Wendlingba54bca2013-06-19 21:36:55 +00003538 const TargetLowering *TLI = TM.getTargetLowering();
3539 EVT VT = TLI->getValueType(I.getType());
Eli Friedman327236c2011-08-24 20:50:09 +00003540
Evan Cheng607acd62013-02-06 02:06:33 +00003541 if (I.getAlignment() < VT.getSizeInBits() / 8)
Eli Friedmanfe731212011-09-13 20:50:54 +00003542 report_fatal_error("Cannot generate unaligned atomic load");
3543
Eli Friedman327236c2011-08-24 20:50:09 +00003544 SDValue L =
3545 DAG.getAtomic(ISD::ATOMIC_LOAD, dl, VT, VT, InChain,
3546 getValue(I.getPointerOperand()),
3547 I.getPointerOperand(), I.getAlignment(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00003548 TLI->getInsertFencesForAtomic() ? Monotonic : Order,
Eli Friedman327236c2011-08-24 20:50:09 +00003549 Scope);
3550
3551 SDValue OutChain = L.getValue(1);
3552
Bill Wendlingba54bca2013-06-19 21:36:55 +00003553 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003554 OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003555 DAG, *TLI);
Eli Friedman327236c2011-08-24 20:50:09 +00003556
3557 setValue(&I, L);
3558 DAG.setRoot(OutChain);
3559}
3560
3561void SelectionDAGBuilder::visitAtomicStore(const StoreInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003562 SDLoc dl = getCurSDLoc();
Eli Friedman327236c2011-08-24 20:50:09 +00003563
3564 AtomicOrdering Order = I.getOrdering();
3565 SynchronizationScope Scope = I.getSynchScope();
3566
3567 SDValue InChain = getRoot();
3568
Bill Wendlingba54bca2013-06-19 21:36:55 +00003569 const TargetLowering *TLI = TM.getTargetLowering();
3570 EVT VT = TLI->getValueType(I.getValueOperand()->getType());
Eli Friedmanfe731212011-09-13 20:50:54 +00003571
Evan Cheng607acd62013-02-06 02:06:33 +00003572 if (I.getAlignment() < VT.getSizeInBits() / 8)
Eli Friedmanfe731212011-09-13 20:50:54 +00003573 report_fatal_error("Cannot generate unaligned atomic store");
3574
Bill Wendlingba54bca2013-06-19 21:36:55 +00003575 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003576 InChain = InsertFenceForAtomic(InChain, Order, Scope, true, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003577 DAG, *TLI);
Eli Friedman327236c2011-08-24 20:50:09 +00003578
3579 SDValue OutChain =
Eli Friedmanfe731212011-09-13 20:50:54 +00003580 DAG.getAtomic(ISD::ATOMIC_STORE, dl, VT,
Eli Friedman327236c2011-08-24 20:50:09 +00003581 InChain,
3582 getValue(I.getPointerOperand()),
3583 getValue(I.getValueOperand()),
3584 I.getPointerOperand(), I.getAlignment(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00003585 TLI->getInsertFencesForAtomic() ? Monotonic : Order,
Eli Friedman327236c2011-08-24 20:50:09 +00003586 Scope);
3587
Bill Wendlingba54bca2013-06-19 21:36:55 +00003588 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003589 OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003590 DAG, *TLI);
Eli Friedman327236c2011-08-24 20:50:09 +00003591
3592 DAG.setRoot(OutChain);
3593}
3594
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003595/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
3596/// node.
Dan Gohman46510a72010-04-15 01:51:59 +00003597void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I,
Dan Gohman2048b852009-11-23 18:04:58 +00003598 unsigned Intrinsic) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003599 bool HasChain = !I.doesNotAccessMemory();
3600 bool OnlyLoad = HasChain && I.onlyReadsMemory();
3601
3602 // Build the operand list.
3603 SmallVector<SDValue, 8> Ops;
3604 if (HasChain) { // If this intrinsic has side-effects, chainify it.
3605 if (OnlyLoad) {
3606 // We don't need to serialize loads against other loads.
3607 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003608 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003609 Ops.push_back(getRoot());
3610 }
3611 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003612
3613 // Info is set by getTgtMemInstrinsic
3614 TargetLowering::IntrinsicInfo Info;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003615 const TargetLowering *TLI = TM.getTargetLowering();
3616 bool IsTgtIntrinsic = TLI->getTgtMemIntrinsic(Info, I, Intrinsic);
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003617
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003618 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Bob Wilson65ffec42010-09-21 17:56:22 +00003619 if (!IsTgtIntrinsic || Info.opc == ISD::INTRINSIC_VOID ||
3620 Info.opc == ISD::INTRINSIC_W_CHAIN)
Bill Wendlingba54bca2013-06-19 21:36:55 +00003621 Ops.push_back(DAG.getTargetConstant(Intrinsic, TLI->getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003622
3623 // Add all operands of the call to the operand list.
Gabor Greif0635f352010-06-25 09:38:13 +00003624 for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) {
3625 SDValue Op = getValue(I.getArgOperand(i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003626 Ops.push_back(Op);
3627 }
3628
Owen Andersone50ed302009-08-10 22:56:29 +00003629 SmallVector<EVT, 4> ValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003630 ComputeValueVTs(*TLI, I.getType(), ValueVTs);
Bill Wendling856ff412009-12-22 00:12:37 +00003631
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003632 if (HasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00003633 ValueVTs.push_back(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003634
Bob Wilson8d919552009-07-31 22:41:21 +00003635 SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003636
3637 // Create the node.
3638 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003639 if (IsTgtIntrinsic) {
3640 // This is target intrinsic that touches memory
Andrew Trickac6d9be2013-05-25 02:42:55 +00003641 Result = DAG.getMemIntrinsicNode(Info.opc, getCurSDLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003642 VTs, &Ops[0], Ops.size(),
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00003643 Info.memVT,
3644 MachinePointerInfo(Info.ptrVal, Info.offset),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003645 Info.align, Info.vol,
3646 Info.readMem, Info.writeMem);
Bill Wendling856ff412009-12-22 00:12:37 +00003647 } else if (!HasChain) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003648 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurSDLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003649 VTs, &Ops[0], Ops.size());
Benjamin Kramerf0127052010-01-05 13:12:22 +00003650 } else if (!I.getType()->isVoidTy()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003651 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurSDLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003652 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003653 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003654 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003655 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003656 }
3657
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003658 if (HasChain) {
3659 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
3660 if (OnlyLoad)
3661 PendingLoads.push_back(Chain);
3662 else
3663 DAG.setRoot(Chain);
3664 }
Bill Wendling856ff412009-12-22 00:12:37 +00003665
Benjamin Kramerf0127052010-01-05 13:12:22 +00003666 if (!I.getType()->isVoidTy()) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003667 if (VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00003668 EVT VT = TLI->getValueType(PTy);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003669 Result = DAG.getNode(ISD::BITCAST, getCurSDLoc(), VT, Result);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003670 }
Bill Wendling856ff412009-12-22 00:12:37 +00003671
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003672 setValue(&I, Result);
3673 }
3674}
3675
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003676/// GetSignificand - Get the significand and build it into a floating-point
3677/// number with exponent of 1:
3678///
3679/// Op = (Op & 0x007fffff) | 0x3f800000;
3680///
Matt Beaumont-Gay50e75bf2013-02-25 18:11:18 +00003681/// where Op is the hexadecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003682static SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00003683GetSignificand(SelectionDAG &DAG, SDValue Op, SDLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003684 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3685 DAG.getConstant(0x007fffff, MVT::i32));
3686 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
3687 DAG.getConstant(0x3f800000, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003688 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00003689}
3690
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003691/// GetExponent - Get the exponent:
3692///
Bill Wendlinge9a72862009-01-20 21:17:57 +00003693/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003694///
Matt Beaumont-Gay50e75bf2013-02-25 18:11:18 +00003695/// where Op is the hexadecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003696static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00003697GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003698 SDLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003699 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3700 DAG.getConstant(0x7f800000, MVT::i32));
3701 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Duncan Sands92abc622009-01-31 15:50:11 +00003702 DAG.getConstant(23, TLI.getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00003703 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
3704 DAG.getConstant(127, MVT::i32));
Bill Wendling4533cac2010-01-28 21:51:40 +00003705 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00003706}
3707
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003708/// getF32Constant - Get 32-bit floating point constant.
3709static SDValue
3710getF32Constant(SelectionDAG &DAG, unsigned Flt) {
Tim Northover0a29cb02013-01-22 09:46:31 +00003711 return DAG.getConstantFP(APFloat(APFloat::IEEEsingle, APInt(32, Flt)),
3712 MVT::f32);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003713}
3714
Craig Topper538cd482012-11-24 18:52:06 +00003715/// expandExp - Lower an exp intrinsic. Handles the special sequences for
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003716/// limited-precision mode.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003717static SDValue expandExp(SDLoc dl, SDValue Op, SelectionDAG &DAG,
Craig Topper538cd482012-11-24 18:52:06 +00003718 const TargetLowering &TLI) {
3719 if (Op.getValueType() == MVT::f32 &&
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003720 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003721
3722 // Put the exponent in the right bit position for later addition to the
3723 // final result:
3724 //
3725 // #define LOG2OFe 1.4426950f
3726 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Owen Anderson825b72b2009-08-11 20:47:22 +00003727 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003728 getF32Constant(DAG, 0x3fb8aa3b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003729 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003730
3731 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003732 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3733 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003734
3735 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003736 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003737 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendling856ff412009-12-22 00:12:37 +00003738
Craig Topperb3157722012-11-24 08:22:37 +00003739 SDValue TwoToFracPartOfX;
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003740 if (LimitFloatPrecision <= 6) {
3741 // For floating-point precision of 6:
3742 //
3743 // TwoToFractionalPartOfX =
3744 // 0.997535578f +
3745 // (0.735607626f + 0.252464424f * x) * x;
3746 //
3747 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003748 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003749 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003750 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003751 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003752 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
Craig Topperb3157722012-11-24 08:22:37 +00003753 TwoToFracPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
3754 getF32Constant(DAG, 0x3f7f5e7e));
Craig Topper08ac4692012-11-16 20:01:39 +00003755 } else if (LimitFloatPrecision <= 12) {
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003756 // For floating-point precision of 12:
3757 //
3758 // TwoToFractionalPartOfX =
3759 // 0.999892986f +
3760 // (0.696457318f +
3761 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3762 //
3763 // 0.000107046256 error, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003764 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003765 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003766 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003767 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003768 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3769 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003770 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003771 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
Craig Topperb3157722012-11-24 08:22:37 +00003772 TwoToFracPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
3773 getF32Constant(DAG, 0x3f7ff8fd));
Craig Topper08ac4692012-11-16 20:01:39 +00003774 } else { // LimitFloatPrecision <= 18
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003775 // For floating-point precision of 18:
3776 //
3777 // TwoToFractionalPartOfX =
3778 // 0.999999982f +
3779 // (0.693148872f +
3780 // (0.240227044f +
3781 // (0.554906021e-1f +
3782 // (0.961591928e-2f +
3783 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3784 //
3785 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003786 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003787 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003788 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003789 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003790 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3791 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003792 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003793 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3794 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003795 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003796 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3797 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003798 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003799 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3800 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003801 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003802 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
Craig Topperb3157722012-11-24 08:22:37 +00003803 TwoToFracPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
3804 getF32Constant(DAG, 0x3f800000));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003805 }
Craig Topperb3157722012-11-24 08:22:37 +00003806
3807 // Add the exponent into the result in integer domain.
3808 SDValue t13 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, TwoToFracPartOfX);
Craig Topper538cd482012-11-24 18:52:06 +00003809 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3810 DAG.getNode(ISD::ADD, dl, MVT::i32,
3811 t13, IntegerPartOfX));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003812 }
3813
Craig Topper538cd482012-11-24 18:52:06 +00003814 // No special expansion.
3815 return DAG.getNode(ISD::FEXP, dl, Op.getValueType(), Op);
Dale Johannesen59e577f2008-09-05 18:38:42 +00003816}
3817
Craig Topper5d1e0892012-11-23 18:38:31 +00003818/// expandLog - Lower a log intrinsic. Handles the special sequences for
Bill Wendling39150252008-09-09 20:39:27 +00003819/// limited-precision mode.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003820static SDValue expandLog(SDLoc dl, SDValue Op, SelectionDAG &DAG,
Craig Topper5d1e0892012-11-23 18:38:31 +00003821 const TargetLowering &TLI) {
3822 if (Op.getValueType() == MVT::f32 &&
Bill Wendling39150252008-09-09 20:39:27 +00003823 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003824 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00003825
3826 // Scale the exponent by log(2) [0.69314718f].
Bill Wendling46ada192010-03-02 01:55:18 +00003827 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003828 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003829 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003830
3831 // Get the significand and build it into a floating-point number with
3832 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003833 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling39150252008-09-09 20:39:27 +00003834
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003835 SDValue LogOfMantissa;
Bill Wendling39150252008-09-09 20:39:27 +00003836 if (LimitFloatPrecision <= 6) {
3837 // For floating-point precision of 6:
3838 //
3839 // LogofMantissa =
3840 // -1.1609546f +
3841 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003842 //
Bill Wendling39150252008-09-09 20:39:27 +00003843 // error 0.0034276066, which is better than 8 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003844 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003845 getF32Constant(DAG, 0xbe74c456));
Owen Anderson825b72b2009-08-11 20:47:22 +00003846 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003847 getF32Constant(DAG, 0x3fb3a2b1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003848 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003849 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
3850 getF32Constant(DAG, 0x3f949a29));
Craig Topper08ac4692012-11-16 20:01:39 +00003851 } else if (LimitFloatPrecision <= 12) {
Bill Wendling39150252008-09-09 20:39:27 +00003852 // For floating-point precision of 12:
3853 //
3854 // LogOfMantissa =
3855 // -1.7417939f +
3856 // (2.8212026f +
3857 // (-1.4699568f +
3858 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3859 //
3860 // error 0.000061011436, which is 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003861 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003862 getF32Constant(DAG, 0xbd67b6d6));
Owen Anderson825b72b2009-08-11 20:47:22 +00003863 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003864 getF32Constant(DAG, 0x3ee4f4b8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003865 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3866 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003867 getF32Constant(DAG, 0x3fbc278b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003868 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3869 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003870 getF32Constant(DAG, 0x40348e95));
Owen Anderson825b72b2009-08-11 20:47:22 +00003871 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003872 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
3873 getF32Constant(DAG, 0x3fdef31a));
Craig Topper08ac4692012-11-16 20:01:39 +00003874 } else { // LimitFloatPrecision <= 18
Bill Wendling39150252008-09-09 20:39:27 +00003875 // For floating-point precision of 18:
3876 //
3877 // LogOfMantissa =
3878 // -2.1072184f +
3879 // (4.2372794f +
3880 // (-3.7029485f +
3881 // (2.2781945f +
3882 // (-0.87823314f +
3883 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3884 //
3885 // error 0.0000023660568, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003886 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003887 getF32Constant(DAG, 0xbc91e5ac));
Owen Anderson825b72b2009-08-11 20:47:22 +00003888 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003889 getF32Constant(DAG, 0x3e4350aa));
Owen Anderson825b72b2009-08-11 20:47:22 +00003890 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3891 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003892 getF32Constant(DAG, 0x3f60d3e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003893 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3894 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003895 getF32Constant(DAG, 0x4011cdf0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003896 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3897 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003898 getF32Constant(DAG, 0x406cfd1c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003899 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3900 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003901 getF32Constant(DAG, 0x408797cb));
Owen Anderson825b72b2009-08-11 20:47:22 +00003902 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003903 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
3904 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003905 }
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003906
Craig Topper5d1e0892012-11-23 18:38:31 +00003907 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003908 }
3909
Craig Topper5d1e0892012-11-23 18:38:31 +00003910 // No special expansion.
3911 return DAG.getNode(ISD::FLOG, dl, Op.getValueType(), Op);
Dale Johannesen59e577f2008-09-05 18:38:42 +00003912}
3913
Craig Topper5d1e0892012-11-23 18:38:31 +00003914/// expandLog2 - Lower a log2 intrinsic. Handles the special sequences for
Bill Wendling3eb59402008-09-09 00:28:24 +00003915/// limited-precision mode.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003916static SDValue expandLog2(SDLoc dl, SDValue Op, SelectionDAG &DAG,
Craig Topper5d1e0892012-11-23 18:38:31 +00003917 const TargetLowering &TLI) {
3918 if (Op.getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003919 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003920 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003921
Bill Wendling39150252008-09-09 20:39:27 +00003922 // Get the exponent.
Bill Wendling46ada192010-03-02 01:55:18 +00003923 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
Bill Wendling856ff412009-12-22 00:12:37 +00003924
Bill Wendling3eb59402008-09-09 00:28:24 +00003925 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003926 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003927 SDValue X = GetSignificand(DAG, Op1, dl);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003928
Bill Wendling3eb59402008-09-09 00:28:24 +00003929 // Different possible minimax approximations of significand in
3930 // floating-point for various degrees of accuracy over [1,2].
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003931 SDValue Log2ofMantissa;
Bill Wendling3eb59402008-09-09 00:28:24 +00003932 if (LimitFloatPrecision <= 6) {
3933 // For floating-point precision of 6:
3934 //
3935 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
3936 //
3937 // error 0.0049451742, which is more than 7 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003938 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003939 getF32Constant(DAG, 0xbeb08fe0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003940 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003941 getF32Constant(DAG, 0x40019463));
Owen Anderson825b72b2009-08-11 20:47:22 +00003942 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003943 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
3944 getF32Constant(DAG, 0x3fd6633d));
Craig Topper08ac4692012-11-16 20:01:39 +00003945 } else if (LimitFloatPrecision <= 12) {
Bill Wendling3eb59402008-09-09 00:28:24 +00003946 // For floating-point precision of 12:
3947 //
3948 // Log2ofMantissa =
3949 // -2.51285454f +
3950 // (4.07009056f +
3951 // (-2.12067489f +
3952 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003953 //
Bill Wendling3eb59402008-09-09 00:28:24 +00003954 // error 0.0000876136000, which is better than 13 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003955 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003956 getF32Constant(DAG, 0xbda7262e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003957 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003958 getF32Constant(DAG, 0x3f25280b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003959 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3960 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003961 getF32Constant(DAG, 0x4007b923));
Owen Anderson825b72b2009-08-11 20:47:22 +00003962 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3963 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003964 getF32Constant(DAG, 0x40823e2f));
Owen Anderson825b72b2009-08-11 20:47:22 +00003965 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003966 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
3967 getF32Constant(DAG, 0x4020d29c));
Craig Topper08ac4692012-11-16 20:01:39 +00003968 } else { // LimitFloatPrecision <= 18
Bill Wendling3eb59402008-09-09 00:28:24 +00003969 // For floating-point precision of 18:
3970 //
3971 // Log2ofMantissa =
3972 // -3.0400495f +
3973 // (6.1129976f +
3974 // (-5.3420409f +
3975 // (3.2865683f +
3976 // (-1.2669343f +
3977 // (0.27515199f -
3978 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
3979 //
3980 // error 0.0000018516, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003981 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003982 getF32Constant(DAG, 0xbcd2769e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003983 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003984 getF32Constant(DAG, 0x3e8ce0b9));
Owen Anderson825b72b2009-08-11 20:47:22 +00003985 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3986 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003987 getF32Constant(DAG, 0x3fa22ae7));
Owen Anderson825b72b2009-08-11 20:47:22 +00003988 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3989 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003990 getF32Constant(DAG, 0x40525723));
Owen Anderson825b72b2009-08-11 20:47:22 +00003991 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3992 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003993 getF32Constant(DAG, 0x40aaf200));
Owen Anderson825b72b2009-08-11 20:47:22 +00003994 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3995 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003996 getF32Constant(DAG, 0x40c39dad));
Owen Anderson825b72b2009-08-11 20:47:22 +00003997 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003998 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
3999 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00004000 }
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004001
Craig Topper5d1e0892012-11-23 18:38:31 +00004002 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log2ofMantissa);
Dale Johannesen853244f2008-09-05 23:49:37 +00004003 }
Bill Wendling3eb59402008-09-09 00:28:24 +00004004
Craig Topper5d1e0892012-11-23 18:38:31 +00004005 // No special expansion.
4006 return DAG.getNode(ISD::FLOG2, dl, Op.getValueType(), Op);
Dale Johannesen59e577f2008-09-05 18:38:42 +00004007}
4008
Craig Topper5d1e0892012-11-23 18:38:31 +00004009/// expandLog10 - Lower a log10 intrinsic. Handles the special sequences for
Bill Wendling3eb59402008-09-09 00:28:24 +00004010/// limited-precision mode.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004011static SDValue expandLog10(SDLoc dl, SDValue Op, SelectionDAG &DAG,
Craig Topper5d1e0892012-11-23 18:38:31 +00004012 const TargetLowering &TLI) {
4013 if (Op.getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00004014 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004015 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00004016
Bill Wendling39150252008-09-09 20:39:27 +00004017 // Scale the exponent by log10(2) [0.30102999f].
Bill Wendling46ada192010-03-02 01:55:18 +00004018 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00004019 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004020 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00004021
4022 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00004023 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00004024 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling3eb59402008-09-09 00:28:24 +00004025
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004026 SDValue Log10ofMantissa;
Bill Wendling3eb59402008-09-09 00:28:24 +00004027 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00004028 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004029 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00004030 // Log10ofMantissa =
4031 // -0.50419619f +
4032 // (0.60948995f - 0.10380950f * x) * x;
4033 //
4034 // error 0.0014886165, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004035 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004036 getF32Constant(DAG, 0xbdd49a13));
Owen Anderson825b72b2009-08-11 20:47:22 +00004037 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004038 getF32Constant(DAG, 0x3f1c0789));
Owen Anderson825b72b2009-08-11 20:47:22 +00004039 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004040 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
4041 getF32Constant(DAG, 0x3f011300));
Craig Topper08ac4692012-11-16 20:01:39 +00004042 } else if (LimitFloatPrecision <= 12) {
Bill Wendling3eb59402008-09-09 00:28:24 +00004043 // For floating-point precision of 12:
4044 //
4045 // Log10ofMantissa =
4046 // -0.64831180f +
4047 // (0.91751397f +
4048 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
4049 //
4050 // error 0.00019228036, which is better than 12 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004051 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004052 getF32Constant(DAG, 0x3d431f31));
Owen Anderson825b72b2009-08-11 20:47:22 +00004053 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004054 getF32Constant(DAG, 0x3ea21fb2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004055 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
4056 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004057 getF32Constant(DAG, 0x3f6ae232));
Owen Anderson825b72b2009-08-11 20:47:22 +00004058 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004059 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
4060 getF32Constant(DAG, 0x3f25f7c3));
Craig Topper08ac4692012-11-16 20:01:39 +00004061 } else { // LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00004062 // For floating-point precision of 18:
4063 //
4064 // Log10ofMantissa =
4065 // -0.84299375f +
4066 // (1.5327582f +
4067 // (-1.0688956f +
4068 // (0.49102474f +
4069 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
4070 //
4071 // error 0.0000037995730, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004072 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004073 getF32Constant(DAG, 0x3c5d51ce));
Owen Anderson825b72b2009-08-11 20:47:22 +00004074 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004075 getF32Constant(DAG, 0x3e00685a));
Owen Anderson825b72b2009-08-11 20:47:22 +00004076 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
4077 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004078 getF32Constant(DAG, 0x3efb6798));
Owen Anderson825b72b2009-08-11 20:47:22 +00004079 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4080 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004081 getF32Constant(DAG, 0x3f88d192));
Owen Anderson825b72b2009-08-11 20:47:22 +00004082 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4083 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004084 getF32Constant(DAG, 0x3fc4316c));
Owen Anderson825b72b2009-08-11 20:47:22 +00004085 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004086 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
4087 getF32Constant(DAG, 0x3f57ce70));
Bill Wendling3eb59402008-09-09 00:28:24 +00004088 }
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004089
Craig Topper5d1e0892012-11-23 18:38:31 +00004090 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log10ofMantissa);
Dale Johannesen852680a2008-09-05 21:27:19 +00004091 }
Bill Wendling3eb59402008-09-09 00:28:24 +00004092
Craig Topper5d1e0892012-11-23 18:38:31 +00004093 // No special expansion.
4094 return DAG.getNode(ISD::FLOG10, dl, Op.getValueType(), Op);
Dale Johannesen59e577f2008-09-05 18:38:42 +00004095}
4096
Craig Topper538cd482012-11-24 18:52:06 +00004097/// expandExp2 - Lower an exp2 intrinsic. Handles the special sequences for
Bill Wendlinge10c8142008-09-09 22:39:21 +00004098/// limited-precision mode.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004099static SDValue expandExp2(SDLoc dl, SDValue Op, SelectionDAG &DAG,
Craig Topper538cd482012-11-24 18:52:06 +00004100 const TargetLowering &TLI) {
4101 if (Op.getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00004102 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004103 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00004104
4105 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00004106 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
4107 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00004108
4109 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00004110 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00004111 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00004112
Craig Topperb3157722012-11-24 08:22:37 +00004113 SDValue TwoToFractionalPartOfX;
Bill Wendlinge10c8142008-09-09 22:39:21 +00004114 if (LimitFloatPrecision <= 6) {
4115 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004116 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00004117 // TwoToFractionalPartOfX =
4118 // 0.997535578f +
4119 // (0.735607626f + 0.252464424f * x) * x;
4120 //
4121 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004122 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004123 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00004124 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004125 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004126 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
Craig Topperb3157722012-11-24 08:22:37 +00004127 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
4128 getF32Constant(DAG, 0x3f7f5e7e));
Craig Topper08ac4692012-11-16 20:01:39 +00004129 } else if (LimitFloatPrecision <= 12) {
Bill Wendlinge10c8142008-09-09 22:39:21 +00004130 // For floating-point precision of 12:
4131 //
4132 // TwoToFractionalPartOfX =
4133 // 0.999892986f +
4134 // (0.696457318f +
4135 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
4136 //
4137 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004138 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004139 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004140 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004141 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004142 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4143 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004144 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00004145 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
Craig Topperb3157722012-11-24 08:22:37 +00004146 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
4147 getF32Constant(DAG, 0x3f7ff8fd));
Craig Topper08ac4692012-11-16 20:01:39 +00004148 } else { // LimitFloatPrecision <= 18
Bill Wendlinge10c8142008-09-09 22:39:21 +00004149 // For floating-point precision of 18:
4150 //
4151 // TwoToFractionalPartOfX =
4152 // 0.999999982f +
4153 // (0.693148872f +
4154 // (0.240227044f +
4155 // (0.554906021e-1f +
4156 // (0.961591928e-2f +
4157 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
4158 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004159 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004160 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004161 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004162 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00004163 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4164 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004165 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00004166 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4167 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004168 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00004169 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
4170 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004171 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00004172 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
4173 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004174 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00004175 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
Craig Topperb3157722012-11-24 08:22:37 +00004176 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
4177 getF32Constant(DAG, 0x3f800000));
Bill Wendlinge10c8142008-09-09 22:39:21 +00004178 }
Craig Topperb3157722012-11-24 08:22:37 +00004179
4180 // Add the exponent into the result in integer domain.
4181 SDValue t13 = DAG.getNode(ISD::BITCAST, dl, MVT::i32,
4182 TwoToFractionalPartOfX);
Craig Topper538cd482012-11-24 18:52:06 +00004183 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
4184 DAG.getNode(ISD::ADD, dl, MVT::i32,
4185 t13, IntegerPartOfX));
Dale Johannesen601d3c02008-09-05 01:48:15 +00004186 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00004187
Craig Topper538cd482012-11-24 18:52:06 +00004188 // No special expansion.
4189 return DAG.getNode(ISD::FEXP2, dl, Op.getValueType(), Op);
Dale Johannesen601d3c02008-09-05 01:48:15 +00004190}
4191
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004192/// visitPow - Lower a pow intrinsic. Handles the special sequences for
4193/// limited-precision mode with x == 10.0f.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004194static SDValue expandPow(SDLoc dl, SDValue LHS, SDValue RHS,
Craig Topper327e4cb2012-11-25 08:08:58 +00004195 SelectionDAG &DAG, const TargetLowering &TLI) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004196 bool IsExp10 = false;
Craig Topper327e4cb2012-11-25 08:08:58 +00004197 if (LHS.getValueType() == MVT::f32 && LHS.getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004198 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Craig Topper327e4cb2012-11-25 08:08:58 +00004199 if (ConstantFPSDNode *LHSC = dyn_cast<ConstantFPSDNode>(LHS)) {
4200 APFloat Ten(10.0f);
4201 IsExp10 = LHSC->isExactlyValue(Ten);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004202 }
4203 }
4204
Craig Topperc1aa6382012-11-25 00:48:58 +00004205 if (IsExp10) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004206 // Put the exponent in the right bit position for later addition to the
4207 // final result:
4208 //
4209 // #define LOG2OF10 3.3219281f
4210 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Craig Topper327e4cb2012-11-25 08:08:58 +00004211 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, RHS,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004212 getF32Constant(DAG, 0x40549a78));
Owen Anderson825b72b2009-08-11 20:47:22 +00004213 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004214
4215 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00004216 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
4217 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004218
4219 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00004220 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00004221 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004222
Craig Topper915562e2012-11-25 00:15:07 +00004223 SDValue TwoToFractionalPartOfX;
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004224 if (LimitFloatPrecision <= 6) {
4225 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004226 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004227 // twoToFractionalPartOfX =
4228 // 0.997535578f +
4229 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004230 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004231 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004232 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004233 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00004234 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004235 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004236 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
Craig Topper915562e2012-11-25 00:15:07 +00004237 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
4238 getF32Constant(DAG, 0x3f7f5e7e));
Craig Topper08ac4692012-11-16 20:01:39 +00004239 } else if (LimitFloatPrecision <= 12) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004240 // For floating-point precision of 12:
4241 //
4242 // TwoToFractionalPartOfX =
4243 // 0.999892986f +
4244 // (0.696457318f +
4245 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
4246 //
4247 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004248 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004249 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004250 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004251 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004252 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4253 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004254 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00004255 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
Craig Topper915562e2012-11-25 00:15:07 +00004256 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
4257 getF32Constant(DAG, 0x3f7ff8fd));
Craig Topper08ac4692012-11-16 20:01:39 +00004258 } else { // LimitFloatPrecision <= 18
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004259 // For floating-point precision of 18:
4260 //
4261 // TwoToFractionalPartOfX =
4262 // 0.999999982f +
4263 // (0.693148872f +
4264 // (0.240227044f +
4265 // (0.554906021e-1f +
4266 // (0.961591928e-2f +
4267 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
4268 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004269 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004270 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004271 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004272 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00004273 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4274 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004275 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00004276 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4277 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004278 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00004279 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
4280 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004281 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00004282 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
4283 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004284 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00004285 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
Craig Topper915562e2012-11-25 00:15:07 +00004286 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
4287 getF32Constant(DAG, 0x3f800000));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004288 }
Craig Topper915562e2012-11-25 00:15:07 +00004289
4290 SDValue t13 = DAG.getNode(ISD::BITCAST, dl,MVT::i32,TwoToFractionalPartOfX);
Craig Topper327e4cb2012-11-25 08:08:58 +00004291 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
4292 DAG.getNode(ISD::ADD, dl, MVT::i32,
4293 t13, IntegerPartOfX));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004294 }
4295
Craig Topper327e4cb2012-11-25 08:08:58 +00004296 // No special expansion.
4297 return DAG.getNode(ISD::FPOW, dl, LHS.getValueType(), LHS, RHS);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004298}
4299
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004300
4301/// ExpandPowI - Expand a llvm.powi intrinsic.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004302static SDValue ExpandPowI(SDLoc DL, SDValue LHS, SDValue RHS,
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004303 SelectionDAG &DAG) {
4304 // If RHS is a constant, we can expand this out to a multiplication tree,
4305 // otherwise we end up lowering to a call to __powidf2 (for example). When
4306 // optimizing for size, we only want to do this if the expansion would produce
4307 // a small number of multiplies, otherwise we do the full expansion.
4308 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
4309 // Get the exponent as a positive value.
4310 unsigned Val = RHSC->getSExtValue();
4311 if ((int)Val < 0) Val = -Val;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004312
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004313 // powi(x, 0) -> 1.0
4314 if (Val == 0)
4315 return DAG.getConstantFP(1.0, LHS.getValueType());
4316
Dan Gohmanae541aa2010-04-15 04:33:49 +00004317 const Function *F = DAG.getMachineFunction().getFunction();
Bill Wendling831737d2012-12-30 10:32:01 +00004318 if (!F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
4319 Attribute::OptimizeForSize) ||
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004320 // If optimizing for size, don't insert too many multiplies. This
4321 // inserts up to 5 multiplies.
4322 CountPopulation_32(Val)+Log2_32(Val) < 7) {
4323 // We use the simple binary decomposition method to generate the multiply
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004324 // sequence. There are more optimal ways to do this (for example,
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004325 // powi(x,15) generates one more multiply than it should), but this has
4326 // the benefit of being both really simple and much better than a libcall.
4327 SDValue Res; // Logically starts equal to 1.0
4328 SDValue CurSquare = LHS;
4329 while (Val) {
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00004330 if (Val & 1) {
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004331 if (Res.getNode())
4332 Res = DAG.getNode(ISD::FMUL, DL,Res.getValueType(), Res, CurSquare);
4333 else
4334 Res = CurSquare; // 1.0*CurSquare.
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00004335 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004336
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004337 CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
4338 CurSquare, CurSquare);
4339 Val >>= 1;
4340 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004341
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004342 // If the original was negative, invert the result, producing 1/(x*x*x).
4343 if (RHSC->getSExtValue() < 0)
4344 Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(),
4345 DAG.getConstantFP(1.0, LHS.getValueType()), Res);
4346 return Res;
4347 }
4348 }
4349
4350 // Otherwise, expand to a libcall.
4351 return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS);
4352}
4353
Devang Patel227dfdb2011-05-16 21:24:05 +00004354// getTruncatedArgReg - Find underlying register used for an truncated
4355// argument.
4356static unsigned getTruncatedArgReg(const SDValue &N) {
4357 if (N.getOpcode() != ISD::TRUNCATE)
4358 return 0;
4359
4360 const SDValue &Ext = N.getOperand(0);
Stephen Lin09f8ca32013-07-06 21:44:25 +00004361 if (Ext.getOpcode() == ISD::AssertZext ||
4362 Ext.getOpcode() == ISD::AssertSext) {
Devang Patel227dfdb2011-05-16 21:24:05 +00004363 const SDValue &CFR = Ext.getOperand(0);
4364 if (CFR.getOpcode() == ISD::CopyFromReg)
4365 return cast<RegisterSDNode>(CFR.getOperand(1))->getReg();
Craig Topper7eb46d82012-04-11 04:55:51 +00004366 if (CFR.getOpcode() == ISD::TRUNCATE)
4367 return getTruncatedArgReg(CFR);
Devang Patel227dfdb2011-05-16 21:24:05 +00004368 }
4369 return 0;
4370}
4371
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004372/// EmitFuncArgumentDbgValue - If the DbgValueInst is a dbg_value of a function
4373/// argument, create the corresponding DBG_VALUE machine instruction for it now.
4374/// At the end of instruction selection, they will be inserted to the entry BB.
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004375bool
Devang Patel78a06e52010-08-25 20:39:26 +00004376SelectionDAGBuilder::EmitFuncArgumentDbgValue(const Value *V, MDNode *Variable,
Michael J. Spencere70c5262010-10-16 08:25:21 +00004377 int64_t Offset,
Dan Gohman5d11ea32010-05-01 00:33:16 +00004378 const SDValue &N) {
Devang Patel0b48ead2010-08-31 22:22:42 +00004379 const Argument *Arg = dyn_cast<Argument>(V);
4380 if (!Arg)
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004381 return false;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004382
Devang Patel719f6a92010-04-29 20:40:36 +00004383 MachineFunction &MF = DAG.getMachineFunction();
Devang Patela90b3052010-11-02 17:01:30 +00004384 const TargetInstrInfo *TII = DAG.getTarget().getInstrInfo();
Devang Patela90b3052010-11-02 17:01:30 +00004385
Devang Patela83ce982010-04-29 18:50:36 +00004386 // Ignore inlined function arguments here.
4387 DIVariable DV(Variable);
Devang Patel719f6a92010-04-29 20:40:36 +00004388 if (DV.isInlinedFnArgument(MF.getFunction()))
Devang Patela83ce982010-04-29 18:50:36 +00004389 return false;
4390
David Blaikie6d9dbd52013-06-16 20:34:15 +00004391 Optional<MachineOperand> Op;
Devang Patel9aee3352011-09-08 22:59:09 +00004392 // Some arguments' frame index is recorded during argument lowering.
David Blaikie6d9dbd52013-06-16 20:34:15 +00004393 if (int FI = FuncInfo.getArgumentFrameIndex(Arg))
4394 Op = MachineOperand::CreateFI(FI);
Devang Patel0b48ead2010-08-31 22:22:42 +00004395
David Blaikie6d9dbd52013-06-16 20:34:15 +00004396 if (!Op && N.getNode()) {
4397 unsigned Reg;
Devang Patel227dfdb2011-05-16 21:24:05 +00004398 if (N.getOpcode() == ISD::CopyFromReg)
4399 Reg = cast<RegisterSDNode>(N.getOperand(1))->getReg();
4400 else
4401 Reg = getTruncatedArgReg(N);
4402 if (Reg && TargetRegisterInfo::isVirtualRegister(Reg)) {
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004403 MachineRegisterInfo &RegInfo = MF.getRegInfo();
4404 unsigned PR = RegInfo.getLiveInPhysReg(Reg);
4405 if (PR)
4406 Reg = PR;
4407 }
David Blaikie6d9dbd52013-06-16 20:34:15 +00004408 if (Reg)
4409 Op = MachineOperand::CreateReg(Reg, false);
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004410 }
4411
David Blaikie6d9dbd52013-06-16 20:34:15 +00004412 if (!Op) {
Devang Patela90b3052010-11-02 17:01:30 +00004413 // Check if ValueMap has reg number.
Evan Chenga36acad2010-04-29 06:33:38 +00004414 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
Devang Patel8bc9ef72010-11-02 17:19:03 +00004415 if (VMI != FuncInfo.ValueMap.end())
David Blaikie6d9dbd52013-06-16 20:34:15 +00004416 Op = MachineOperand::CreateReg(VMI->second, false);
Evan Chenga36acad2010-04-29 06:33:38 +00004417 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004418
David Blaikie6d9dbd52013-06-16 20:34:15 +00004419 if (!Op && N.getNode())
Devang Patela90b3052010-11-02 17:01:30 +00004420 // Check if frame index is available.
4421 if (LoadSDNode *LNode = dyn_cast<LoadSDNode>(N.getNode()))
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004422 if (FrameIndexSDNode *FINode =
David Blaikie6d9dbd52013-06-16 20:34:15 +00004423 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
4424 Op = MachineOperand::CreateFI(FINode->getIndex());
Devang Patel8bc9ef72010-11-02 17:19:03 +00004425
David Blaikie6d9dbd52013-06-16 20:34:15 +00004426 if (!Op)
Devang Patel8bc9ef72010-11-02 17:19:03 +00004427 return false;
Devang Patela90b3052010-11-02 17:01:30 +00004428
Adrian Prantl893ae832013-07-10 01:53:30 +00004429 // FIXME: This does not handle register-indirect values at offset 0.
4430 bool IsIndirect = Offset != 0;
David Blaikie6d9dbd52013-06-16 20:34:15 +00004431 if (Op->isReg())
Adrian Prantl35176402013-07-09 20:28:37 +00004432 FuncInfo.ArgDbgValues.push_back(BuildMI(MF, getCurDebugLoc(),
4433 TII->get(TargetOpcode::DBG_VALUE),
Adrian Prantl893ae832013-07-10 01:53:30 +00004434 IsIndirect,
Adrian Prantl35176402013-07-09 20:28:37 +00004435 Op->getReg(), Offset, Variable));
4436 else
4437 FuncInfo.ArgDbgValues.push_back(
David Blaikie6d9dbd52013-06-16 20:34:15 +00004438 BuildMI(MF, getCurDebugLoc(), TII->get(TargetOpcode::DBG_VALUE))
4439 .addOperand(*Op).addImm(Offset).addMetadata(Variable));
Adrian Prantl35176402013-07-09 20:28:37 +00004440
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004441 return true;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004442}
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004443
Douglas Gregor7d9663c2010-05-11 06:17:44 +00004444// VisualStudio defines setjmp as _setjmp
Michael J. Spencer1f409602010-09-24 19:48:47 +00004445#if defined(_MSC_VER) && defined(setjmp) && \
4446 !defined(setjmp_undefined_for_msvc)
4447# pragma push_macro("setjmp")
4448# undef setjmp
4449# define setjmp_undefined_for_msvc
Douglas Gregor7d9663c2010-05-11 06:17:44 +00004450#endif
4451
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004452/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
4453/// we want to emit this as a call to a named external function, return the name
4454/// otherwise lower it and return null.
4455const char *
Dan Gohman46510a72010-04-15 01:51:59 +00004456SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, unsigned Intrinsic) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00004457 const TargetLowering *TLI = TM.getTargetLowering();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004458 SDLoc sdl = getCurSDLoc();
Dale Johannesen66978ee2009-01-31 02:22:37 +00004459 DebugLoc dl = getCurDebugLoc();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004460 SDValue Res;
4461
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004462 switch (Intrinsic) {
4463 default:
4464 // By default, turn this into a target intrinsic node.
4465 visitTargetIntrinsic(I, Intrinsic);
4466 return 0;
4467 case Intrinsic::vastart: visitVAStart(I); return 0;
4468 case Intrinsic::vaend: visitVAEnd(I); return 0;
4469 case Intrinsic::vacopy: visitVACopy(I); return 0;
4470 case Intrinsic::returnaddress:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004471 setValue(&I, DAG.getNode(ISD::RETURNADDR, sdl, TLI->getPointerTy(),
Gabor Greif0635f352010-06-25 09:38:13 +00004472 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004473 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00004474 case Intrinsic::frameaddress:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004475 setValue(&I, DAG.getNode(ISD::FRAMEADDR, sdl, TLI->getPointerTy(),
Gabor Greif0635f352010-06-25 09:38:13 +00004476 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004477 return 0;
4478 case Intrinsic::setjmp:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004479 return &"_setjmp"[!TLI->usesUnderscoreSetJmp()];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004480 case Intrinsic::longjmp:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004481 return &"_longjmp"[!TLI->usesUnderscoreLongJmp()];
Chris Lattner824b9582008-11-21 16:42:48 +00004482 case Intrinsic::memcpy: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00004483 // Assert for address < 256 since we support only user defined address
4484 // spaces.
Gabor Greif0635f352010-06-25 09:38:13 +00004485 assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004486 < 256 &&
Gabor Greif0635f352010-06-25 09:38:13 +00004487 cast<PointerType>(I.getArgOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004488 < 256 &&
4489 "Unknown address space");
Gabor Greif0635f352010-06-25 09:38:13 +00004490 SDValue Op1 = getValue(I.getArgOperand(0));
4491 SDValue Op2 = getValue(I.getArgOperand(1));
4492 SDValue Op3 = getValue(I.getArgOperand(2));
4493 unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
Chandler Carruthaf23f8e2013-02-25 14:20:21 +00004494 if (!Align)
4495 Align = 1; // @llvm.memcpy defines 0 and 1 to both mean no alignment.
Gabor Greif0635f352010-06-25 09:38:13 +00004496 bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004497 DAG.setRoot(DAG.getMemcpy(getRoot(), sdl, Op1, Op2, Op3, Align, isVol, false,
Chris Lattnere72f2022010-09-21 05:40:29 +00004498 MachinePointerInfo(I.getArgOperand(0)),
4499 MachinePointerInfo(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004500 return 0;
4501 }
Chris Lattner824b9582008-11-21 16:42:48 +00004502 case Intrinsic::memset: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00004503 // Assert for address < 256 since we support only user defined address
4504 // spaces.
Gabor Greif0635f352010-06-25 09:38:13 +00004505 assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004506 < 256 &&
4507 "Unknown address space");
Gabor Greif0635f352010-06-25 09:38:13 +00004508 SDValue Op1 = getValue(I.getArgOperand(0));
4509 SDValue Op2 = getValue(I.getArgOperand(1));
4510 SDValue Op3 = getValue(I.getArgOperand(2));
4511 unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
Chandler Carruthaf23f8e2013-02-25 14:20:21 +00004512 if (!Align)
4513 Align = 1; // @llvm.memset defines 0 and 1 to both mean no alignment.
Gabor Greif0635f352010-06-25 09:38:13 +00004514 bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004515 DAG.setRoot(DAG.getMemset(getRoot(), sdl, Op1, Op2, Op3, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004516 MachinePointerInfo(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004517 return 0;
4518 }
Chris Lattner824b9582008-11-21 16:42:48 +00004519 case Intrinsic::memmove: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00004520 // Assert for address < 256 since we support only user defined address
4521 // spaces.
Gabor Greif0635f352010-06-25 09:38:13 +00004522 assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004523 < 256 &&
Gabor Greif0635f352010-06-25 09:38:13 +00004524 cast<PointerType>(I.getArgOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004525 < 256 &&
4526 "Unknown address space");
Gabor Greif0635f352010-06-25 09:38:13 +00004527 SDValue Op1 = getValue(I.getArgOperand(0));
4528 SDValue Op2 = getValue(I.getArgOperand(1));
4529 SDValue Op3 = getValue(I.getArgOperand(2));
4530 unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
Chandler Carruthaf23f8e2013-02-25 14:20:21 +00004531 if (!Align)
4532 Align = 1; // @llvm.memmove defines 0 and 1 to both mean no alignment.
Gabor Greif0635f352010-06-25 09:38:13 +00004533 bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004534 DAG.setRoot(DAG.getMemmove(getRoot(), sdl, Op1, Op2, Op3, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004535 MachinePointerInfo(I.getArgOperand(0)),
4536 MachinePointerInfo(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004537 return 0;
4538 }
Bill Wendling92c1e122009-02-13 02:16:35 +00004539 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00004540 const DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
Devang Patelac1ceb32009-10-09 22:42:28 +00004541 MDNode *Variable = DI.getVariable();
Dan Gohman46510a72010-04-15 01:51:59 +00004542 const Value *Address = DI.getAddress();
Manman Rencbafae62013-06-28 05:43:10 +00004543 DIVariable DIVar(Variable);
4544 assert((!DIVar || DIVar.isVariable()) &&
4545 "Variable in DbgDeclareInst should be either null or a DIVariable.");
4546 if (!Address || !DIVar) {
Eric Christopher8f2a88d2012-03-15 21:33:41 +00004547 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
Dale Johannesen8ac38f22010-02-08 21:53:27 +00004548 return 0;
Eric Christopher8f2a88d2012-03-15 21:33:41 +00004549 }
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004550
Devang Patel3f74a112010-09-02 21:29:42 +00004551 // Check if address has undef value.
4552 if (isa<UndefValue>(Address) ||
4553 (Address->use_empty() && !isa<Argument>(Address))) {
Eric Christopher24413672012-02-23 03:39:39 +00004554 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
Devang Patel3f74a112010-09-02 21:29:42 +00004555 return 0;
4556 }
4557
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004558 SDValue &N = NodeMap[Address];
Devang Patel0b48ead2010-08-31 22:22:42 +00004559 if (!N.getNode() && isa<Argument>(Address))
4560 // Check unused arguments map.
4561 N = UnusedArgNodeMap[Address];
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004562 SDDbgValue *SDV;
4563 if (N.getNode()) {
Devang Patel8e741ed2010-09-02 21:02:27 +00004564 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
4565 Address = BCI->getOperand(0);
Eric Christopher178606d2012-02-24 01:59:08 +00004566 // Parameters are handled specially.
4567 bool isParameter =
4568 (DIVariable(Variable).getTag() == dwarf::DW_TAG_arg_variable ||
4569 isa<Argument>(Address));
4570
Devang Patel8e741ed2010-09-02 21:02:27 +00004571 const AllocaInst *AI = dyn_cast<AllocaInst>(Address);
4572
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004573 if (isParameter && !AI) {
4574 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N.getNode());
4575 if (FINode)
4576 // Byval parameter. We have a frame index at this point.
4577 SDV = DAG.getDbgValue(Variable, FINode->getIndex(),
4578 0, dl, SDNodeOrder);
Devang Patelafeaae72010-12-06 22:39:26 +00004579 else {
Devang Patel227dfdb2011-05-16 21:24:05 +00004580 // Address is an argument, so try to emit its dbg value using
4581 // virtual register info from the FuncInfo.ValueMap.
4582 EmitFuncArgumentDbgValue(Address, Variable, 0, N);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004583 return 0;
Devang Patelafeaae72010-12-06 22:39:26 +00004584 }
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004585 } else if (AI)
4586 SDV = DAG.getDbgValue(Variable, N.getNode(), N.getResNo(),
4587 0, dl, SDNodeOrder);
Devang Patelafeaae72010-12-06 22:39:26 +00004588 else {
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004589 // Can't do anything with other non-AI cases yet.
Eric Christopher24413672012-02-23 03:39:39 +00004590 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
Eric Christopher178606d2012-02-24 01:59:08 +00004591 DEBUG(dbgs() << "non-AllocaInst issue for Address: \n\t");
4592 DEBUG(Address->dump());
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004593 return 0;
Devang Patelafeaae72010-12-06 22:39:26 +00004594 }
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004595 DAG.AddDbgValue(SDV, N.getNode(), isParameter);
4596 } else {
Gabor Greiffb4032f2010-10-01 10:32:19 +00004597 // If Address is an argument then try to emit its dbg value using
Michael J. Spencere70c5262010-10-16 08:25:21 +00004598 // virtual register info from the FuncInfo.ValueMap.
Devang Patel6cd467b2010-08-26 22:53:27 +00004599 if (!EmitFuncArgumentDbgValue(Address, Variable, 0, N)) {
Devang Patel1397fdc2010-09-15 14:48:53 +00004600 // If variable is pinned by a alloca in dominating bb then
4601 // use StaticAllocaMap.
4602 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Address)) {
Devang Patel27ede1b2010-09-15 18:13:55 +00004603 if (AI->getParent() != DI.getParent()) {
4604 DenseMap<const AllocaInst*, int>::iterator SI =
4605 FuncInfo.StaticAllocaMap.find(AI);
4606 if (SI != FuncInfo.StaticAllocaMap.end()) {
4607 SDV = DAG.getDbgValue(Variable, SI->second,
4608 0, dl, SDNodeOrder);
4609 DAG.AddDbgValue(SDV, 0, false);
4610 return 0;
4611 }
Devang Patel1397fdc2010-09-15 14:48:53 +00004612 }
4613 }
Eric Christopher0822e012012-02-23 03:39:43 +00004614 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
Devang Patel6cd467b2010-08-26 22:53:27 +00004615 }
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004616 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004617 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00004618 }
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004619 case Intrinsic::dbg_value: {
Dan Gohman46510a72010-04-15 01:51:59 +00004620 const DbgValueInst &DI = cast<DbgValueInst>(I);
Manman Rencbafae62013-06-28 05:43:10 +00004621 DIVariable DIVar(DI.getVariable());
4622 assert((!DIVar || DIVar.isVariable()) &&
4623 "Variable in DbgValueInst should be either null or a DIVariable.");
4624 if (!DIVar)
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004625 return 0;
4626
4627 MDNode *Variable = DI.getVariable();
Devang Patel00190342010-03-15 19:15:44 +00004628 uint64_t Offset = DI.getOffset();
Dan Gohman46510a72010-04-15 01:51:59 +00004629 const Value *V = DI.getValue();
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004630 if (!V)
4631 return 0;
Devang Patel00190342010-03-15 19:15:44 +00004632
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004633 SDDbgValue *SDV;
Devang Patel57871242011-08-03 23:13:55 +00004634 if (isa<ConstantInt>(V) || isa<ConstantFP>(V) || isa<UndefValue>(V)) {
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004635 SDV = DAG.getDbgValue(Variable, V, Offset, dl, SDNodeOrder);
4636 DAG.AddDbgValue(SDV, 0, false);
Devang Patel00190342010-03-15 19:15:44 +00004637 } else {
Dale Johannesenbdc09d92010-07-16 00:02:08 +00004638 // Do not use getValue() in here; we don't want to generate code at
4639 // this point if it hasn't been done yet.
Devang Patel9126c0d2010-06-01 19:59:01 +00004640 SDValue N = NodeMap[V];
4641 if (!N.getNode() && isa<Argument>(V))
4642 // Check unused arguments map.
4643 N = UnusedArgNodeMap[V];
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004644 if (N.getNode()) {
Devang Patel78a06e52010-08-25 20:39:26 +00004645 if (!EmitFuncArgumentDbgValue(V, Variable, Offset, N)) {
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004646 SDV = DAG.getDbgValue(Variable, N.getNode(),
4647 N.getResNo(), Offset, dl, SDNodeOrder);
4648 DAG.AddDbgValue(SDV, N.getNode(), false);
4649 }
Devang Patela778f5c2011-02-18 22:43:42 +00004650 } else if (!V->use_empty() ) {
Dale Johannesenbdc09d92010-07-16 00:02:08 +00004651 // Do not call getValue(V) yet, as we don't want to generate code.
4652 // Remember it for later.
4653 DanglingDebugInfo DDI(&DI, dl, SDNodeOrder);
4654 DanglingDebugInfoMap[V] = DDI;
Devang Patel0991dfb2010-08-27 22:25:51 +00004655 } else {
Devang Patel00190342010-03-15 19:15:44 +00004656 // We may expand this to cover more cases. One case where we have no
Devang Patelafeaae72010-12-06 22:39:26 +00004657 // data available is an unreferenced parameter.
Eric Christopher0822e012012-02-23 03:39:43 +00004658 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004659 }
Devang Patel00190342010-03-15 19:15:44 +00004660 }
4661
4662 // Build a debug info table entry.
Dan Gohman46510a72010-04-15 01:51:59 +00004663 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(V))
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004664 V = BCI->getOperand(0);
Dan Gohman46510a72010-04-15 01:51:59 +00004665 const AllocaInst *AI = dyn_cast<AllocaInst>(V);
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004666 // Don't handle byval struct arguments or VLAs, for example.
Eric Christopher7e1e18f2012-03-26 06:10:32 +00004667 if (!AI) {
Eric Christopher9fc5c832012-03-28 07:34:36 +00004668 DEBUG(dbgs() << "Dropping debug location info for:\n " << DI << "\n");
4669 DEBUG(dbgs() << " Last seen at:\n " << *V << "\n");
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004670 return 0;
Eric Christopher7e1e18f2012-03-26 06:10:32 +00004671 }
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004672 DenseMap<const AllocaInst*, int>::iterator SI =
4673 FuncInfo.StaticAllocaMap.find(AI);
4674 if (SI == FuncInfo.StaticAllocaMap.end())
4675 return 0; // VLAs.
4676 int FI = SI->second;
Michael J. Spencere70c5262010-10-16 08:25:21 +00004677
Chris Lattner512063d2010-04-05 06:19:28 +00004678 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
4679 if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo())
4680 MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc());
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004681 return 0;
4682 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004683
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004684 case Intrinsic::eh_typeid_for: {
Chris Lattner512063d2010-04-05 06:19:28 +00004685 // Find the type id for the given typeinfo.
Gabor Greif0635f352010-06-25 09:38:13 +00004686 GlobalVariable *GV = ExtractTypeInfo(I.getArgOperand(0));
Chris Lattner512063d2010-04-05 06:19:28 +00004687 unsigned TypeID = DAG.getMachineFunction().getMMI().getTypeIDFor(GV);
4688 Res = DAG.getConstant(TypeID, MVT::i32);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004689 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004690 return 0;
4691 }
4692
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004693 case Intrinsic::eh_return_i32:
4694 case Intrinsic::eh_return_i64:
Chris Lattner512063d2010-04-05 06:19:28 +00004695 DAG.getMachineFunction().getMMI().setCallsEHReturn(true);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004696 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, sdl,
Chris Lattner512063d2010-04-05 06:19:28 +00004697 MVT::Other,
4698 getControlRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00004699 getValue(I.getArgOperand(0)),
4700 getValue(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004701 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004702 case Intrinsic::eh_unwind_init:
Chris Lattner512063d2010-04-05 06:19:28 +00004703 DAG.getMachineFunction().getMMI().setCallsUnwindInit(true);
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004704 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004705 case Intrinsic::eh_dwarf_cfa: {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004706 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getArgOperand(0)), sdl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00004707 TLI->getPointerTy());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004708 SDValue Offset = DAG.getNode(ISD::ADD, sdl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00004709 TLI->getPointerTy(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004710 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, sdl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00004711 TLI->getPointerTy()),
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004712 CfaArg);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004713 SDValue FA = DAG.getNode(ISD::FRAMEADDR, sdl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00004714 TLI->getPointerTy(),
4715 DAG.getConstant(0, TLI->getPointerTy()));
4716 setValue(&I, DAG.getNode(ISD::ADD, sdl, TLI->getPointerTy(),
Bill Wendling4533cac2010-01-28 21:51:40 +00004717 FA, Offset));
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004718 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004719 }
Jim Grosbachca752c92010-01-28 01:45:32 +00004720 case Intrinsic::eh_sjlj_callsite: {
Chris Lattner512063d2010-04-05 06:19:28 +00004721 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Gabor Greif0635f352010-06-25 09:38:13 +00004722 ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(0));
Jim Grosbachca752c92010-01-28 01:45:32 +00004723 assert(CI && "Non-constant call site value in eh.sjlj.callsite!");
Chris Lattner512063d2010-04-05 06:19:28 +00004724 assert(MMI.getCurrentCallSite() == 0 && "Overlapping call sites!");
Jim Grosbachca752c92010-01-28 01:45:32 +00004725
Chris Lattner512063d2010-04-05 06:19:28 +00004726 MMI.setCurrentCallSite(CI->getZExtValue());
Jim Grosbachca752c92010-01-28 01:45:32 +00004727 return 0;
4728 }
Bill Wendling6ef94172011-09-28 03:36:43 +00004729 case Intrinsic::eh_sjlj_functioncontext: {
4730 // Get and store the index of the function context.
4731 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bill Wendlingadbf7b22011-09-28 03:52:41 +00004732 AllocaInst *FnCtx =
4733 cast<AllocaInst>(I.getArgOperand(0)->stripPointerCasts());
Bill Wendling6ef94172011-09-28 03:36:43 +00004734 int FI = FuncInfo.StaticAllocaMap[FnCtx];
4735 MFI->setFunctionContextIndex(FI);
4736 return 0;
4737 }
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004738 case Intrinsic::eh_sjlj_setjmp: {
Bill Wendlingce370cf2011-10-07 21:25:38 +00004739 SDValue Ops[2];
4740 Ops[0] = getRoot();
4741 Ops[1] = getValue(I.getArgOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004742 SDValue Op = DAG.getNode(ISD::EH_SJLJ_SETJMP, sdl,
Bill Wendlingce370cf2011-10-07 21:25:38 +00004743 DAG.getVTList(MVT::i32, MVT::Other),
4744 Ops, 2);
4745 setValue(&I, Op.getValue(0));
4746 DAG.setRoot(Op.getValue(1));
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004747 return 0;
4748 }
Jim Grosbach5eb19512010-05-22 01:06:18 +00004749 case Intrinsic::eh_sjlj_longjmp: {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004750 DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_LONGJMP, sdl, MVT::Other,
Jim Grosbache4ad3872010-10-19 23:27:08 +00004751 getRoot(), getValue(I.getArgOperand(0))));
4752 return 0;
4753 }
Jim Grosbachca752c92010-01-28 01:45:32 +00004754
Dale Johannesen0488fb62010-09-30 23:57:10 +00004755 case Intrinsic::x86_mmx_pslli_w:
4756 case Intrinsic::x86_mmx_pslli_d:
4757 case Intrinsic::x86_mmx_pslli_q:
4758 case Intrinsic::x86_mmx_psrli_w:
4759 case Intrinsic::x86_mmx_psrli_d:
4760 case Intrinsic::x86_mmx_psrli_q:
4761 case Intrinsic::x86_mmx_psrai_w:
4762 case Intrinsic::x86_mmx_psrai_d: {
4763 SDValue ShAmt = getValue(I.getArgOperand(1));
4764 if (isa<ConstantSDNode>(ShAmt)) {
4765 visitTargetIntrinsic(I, Intrinsic);
4766 return 0;
4767 }
4768 unsigned NewIntrinsic = 0;
4769 EVT ShAmtVT = MVT::v2i32;
4770 switch (Intrinsic) {
4771 case Intrinsic::x86_mmx_pslli_w:
4772 NewIntrinsic = Intrinsic::x86_mmx_psll_w;
4773 break;
4774 case Intrinsic::x86_mmx_pslli_d:
4775 NewIntrinsic = Intrinsic::x86_mmx_psll_d;
4776 break;
4777 case Intrinsic::x86_mmx_pslli_q:
4778 NewIntrinsic = Intrinsic::x86_mmx_psll_q;
4779 break;
4780 case Intrinsic::x86_mmx_psrli_w:
4781 NewIntrinsic = Intrinsic::x86_mmx_psrl_w;
4782 break;
4783 case Intrinsic::x86_mmx_psrli_d:
4784 NewIntrinsic = Intrinsic::x86_mmx_psrl_d;
4785 break;
4786 case Intrinsic::x86_mmx_psrli_q:
4787 NewIntrinsic = Intrinsic::x86_mmx_psrl_q;
4788 break;
4789 case Intrinsic::x86_mmx_psrai_w:
4790 NewIntrinsic = Intrinsic::x86_mmx_psra_w;
4791 break;
4792 case Intrinsic::x86_mmx_psrai_d:
4793 NewIntrinsic = Intrinsic::x86_mmx_psra_d;
4794 break;
4795 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
4796 }
4797
4798 // The vector shift intrinsics with scalars uses 32b shift amounts but
4799 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
4800 // to be zero.
4801 // We must do this early because v2i32 is not a legal type.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004802 SDValue ShOps[2];
4803 ShOps[0] = ShAmt;
4804 ShOps[1] = DAG.getConstant(0, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004805 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, sdl, ShAmtVT, &ShOps[0], 2);
Bill Wendlingba54bca2013-06-19 21:36:55 +00004806 EVT DestVT = TLI->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004807 ShAmt = DAG.getNode(ISD::BITCAST, sdl, DestVT, ShAmt);
4808 Res = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, sdl, DestVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00004809 DAG.getConstant(NewIntrinsic, MVT::i32),
4810 getValue(I.getArgOperand(0)), ShAmt);
4811 setValue(&I, Res);
4812 return 0;
4813 }
Pete Cooperd18134f2012-02-24 03:51:49 +00004814 case Intrinsic::x86_avx_vinsertf128_pd_256:
4815 case Intrinsic::x86_avx_vinsertf128_ps_256:
Craig Topperb45c9692012-04-07 22:32:29 +00004816 case Intrinsic::x86_avx_vinsertf128_si_256:
4817 case Intrinsic::x86_avx2_vinserti128: {
Bill Wendlingba54bca2013-06-19 21:36:55 +00004818 EVT DestVT = TLI->getValueType(I.getType());
4819 EVT ElVT = TLI->getValueType(I.getArgOperand(1)->getType());
Pete Cooperd18134f2012-02-24 03:51:49 +00004820 uint64_t Idx = (cast<ConstantInt>(I.getArgOperand(2))->getZExtValue() & 1) *
4821 ElVT.getVectorNumElements();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004822 Res = DAG.getNode(ISD::INSERT_SUBVECTOR, sdl, DestVT,
Pete Cooperd18134f2012-02-24 03:51:49 +00004823 getValue(I.getArgOperand(0)),
4824 getValue(I.getArgOperand(1)),
Tom Stellard425b76c2013-08-05 22:22:01 +00004825 DAG.getConstant(Idx, TLI->getVectorIdxTy()));
Craig Topperf6dc7922012-09-05 05:48:09 +00004826 setValue(&I, Res);
4827 return 0;
4828 }
4829 case Intrinsic::x86_avx_vextractf128_pd_256:
4830 case Intrinsic::x86_avx_vextractf128_ps_256:
4831 case Intrinsic::x86_avx_vextractf128_si_256:
4832 case Intrinsic::x86_avx2_vextracti128: {
Bill Wendlingba54bca2013-06-19 21:36:55 +00004833 EVT DestVT = TLI->getValueType(I.getType());
Craig Topperf6dc7922012-09-05 05:48:09 +00004834 uint64_t Idx = (cast<ConstantInt>(I.getArgOperand(1))->getZExtValue() & 1) *
4835 DestVT.getVectorNumElements();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004836 Res = DAG.getNode(ISD::EXTRACT_SUBVECTOR, sdl, DestVT,
Craig Topperf6dc7922012-09-05 05:48:09 +00004837 getValue(I.getArgOperand(0)),
Tom Stellard425b76c2013-08-05 22:22:01 +00004838 DAG.getConstant(Idx, TLI->getVectorIdxTy()));
Pete Cooperd18134f2012-02-24 03:51:49 +00004839 setValue(&I, Res);
4840 return 0;
4841 }
Mon P Wang77cdf302008-11-10 20:54:11 +00004842 case Intrinsic::convertff:
4843 case Intrinsic::convertfsi:
4844 case Intrinsic::convertfui:
4845 case Intrinsic::convertsif:
4846 case Intrinsic::convertuif:
4847 case Intrinsic::convertss:
4848 case Intrinsic::convertsu:
4849 case Intrinsic::convertus:
4850 case Intrinsic::convertuu: {
4851 ISD::CvtCode Code = ISD::CVT_INVALID;
4852 switch (Intrinsic) {
Craig Topperc42e6402012-04-11 04:34:11 +00004853 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Mon P Wang77cdf302008-11-10 20:54:11 +00004854 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
4855 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
4856 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
4857 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
4858 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
4859 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
4860 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
4861 case Intrinsic::convertus: Code = ISD::CVT_US; break;
4862 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
4863 }
Bill Wendlingba54bca2013-06-19 21:36:55 +00004864 EVT DestVT = TLI->getValueType(I.getType());
Gabor Greif0635f352010-06-25 09:38:13 +00004865 const Value *Op1 = I.getArgOperand(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004866 Res = DAG.getConvertRndSat(DestVT, sdl, getValue(Op1),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004867 DAG.getValueType(DestVT),
4868 DAG.getValueType(getValue(Op1).getValueType()),
Gabor Greif0635f352010-06-25 09:38:13 +00004869 getValue(I.getArgOperand(1)),
4870 getValue(I.getArgOperand(2)),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004871 Code);
4872 setValue(&I, Res);
Mon P Wang77cdf302008-11-10 20:54:11 +00004873 return 0;
4874 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004875 case Intrinsic::powi:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004876 setValue(&I, ExpandPowI(sdl, getValue(I.getArgOperand(0)),
Gabor Greif0635f352010-06-25 09:38:13 +00004877 getValue(I.getArgOperand(1)), DAG));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004878 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004879 case Intrinsic::log:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004880 setValue(&I, expandLog(sdl, getValue(I.getArgOperand(0)), DAG, *TLI));
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004881 return 0;
4882 case Intrinsic::log2:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004883 setValue(&I, expandLog2(sdl, getValue(I.getArgOperand(0)), DAG, *TLI));
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004884 return 0;
4885 case Intrinsic::log10:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004886 setValue(&I, expandLog10(sdl, getValue(I.getArgOperand(0)), DAG, *TLI));
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004887 return 0;
4888 case Intrinsic::exp:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004889 setValue(&I, expandExp(sdl, getValue(I.getArgOperand(0)), DAG, *TLI));
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004890 return 0;
4891 case Intrinsic::exp2:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004892 setValue(&I, expandExp2(sdl, getValue(I.getArgOperand(0)), DAG, *TLI));
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004893 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004894 case Intrinsic::pow:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004895 setValue(&I, expandPow(sdl, getValue(I.getArgOperand(0)),
Bill Wendlingba54bca2013-06-19 21:36:55 +00004896 getValue(I.getArgOperand(1)), DAG, *TLI));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004897 return 0;
Craig Topper9bd4dd72012-11-16 07:48:23 +00004898 case Intrinsic::sqrt:
Peter Collingbourneb34d3aa2012-05-28 21:48:37 +00004899 case Intrinsic::fabs:
Craig Topper9bd4dd72012-11-16 07:48:23 +00004900 case Intrinsic::sin:
4901 case Intrinsic::cos:
Dan Gohman27db99f2012-07-26 17:43:27 +00004902 case Intrinsic::floor:
Craig Topper49010472012-11-15 06:51:10 +00004903 case Intrinsic::ceil:
Craig Topper49010472012-11-15 06:51:10 +00004904 case Intrinsic::trunc:
Craig Topper49010472012-11-15 06:51:10 +00004905 case Intrinsic::rint:
Hal Finkel41418d12013-08-07 22:49:12 +00004906 case Intrinsic::nearbyint:
4907 case Intrinsic::round: {
Craig Topper9bd4dd72012-11-16 07:48:23 +00004908 unsigned Opcode;
4909 switch (Intrinsic) {
4910 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
4911 case Intrinsic::sqrt: Opcode = ISD::FSQRT; break;
4912 case Intrinsic::fabs: Opcode = ISD::FABS; break;
4913 case Intrinsic::sin: Opcode = ISD::FSIN; break;
4914 case Intrinsic::cos: Opcode = ISD::FCOS; break;
4915 case Intrinsic::floor: Opcode = ISD::FFLOOR; break;
4916 case Intrinsic::ceil: Opcode = ISD::FCEIL; break;
4917 case Intrinsic::trunc: Opcode = ISD::FTRUNC; break;
4918 case Intrinsic::rint: Opcode = ISD::FRINT; break;
4919 case Intrinsic::nearbyint: Opcode = ISD::FNEARBYINT; break;
Hal Finkel41418d12013-08-07 22:49:12 +00004920 case Intrinsic::round: Opcode = ISD::FROUND; break;
Craig Topper9bd4dd72012-11-16 07:48:23 +00004921 }
4922
Andrew Trickac6d9be2013-05-25 02:42:55 +00004923 setValue(&I, DAG.getNode(Opcode, sdl,
Craig Topper49010472012-11-15 06:51:10 +00004924 getValue(I.getArgOperand(0)).getValueType(),
4925 getValue(I.getArgOperand(0))));
4926 return 0;
Craig Topper9bd4dd72012-11-16 07:48:23 +00004927 }
Cameron Zwarich33390842011-07-08 21:39:21 +00004928 case Intrinsic::fma:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004929 setValue(&I, DAG.getNode(ISD::FMA, sdl,
Cameron Zwarich33390842011-07-08 21:39:21 +00004930 getValue(I.getArgOperand(0)).getValueType(),
4931 getValue(I.getArgOperand(0)),
4932 getValue(I.getArgOperand(1)),
4933 getValue(I.getArgOperand(2))));
4934 return 0;
Lang Hames5afba6f2012-06-05 19:07:46 +00004935 case Intrinsic::fmuladd: {
Bill Wendlingba54bca2013-06-19 21:36:55 +00004936 EVT VT = TLI->getValueType(I.getType());
Lang Hamese0231412012-06-22 01:09:09 +00004937 if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict &&
Stephen Line54885a2013-07-09 18:16:56 +00004938 TLI->isFMAFasterThanFMulAndFAdd(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004939 setValue(&I, DAG.getNode(ISD::FMA, sdl,
Lang Hames5afba6f2012-06-05 19:07:46 +00004940 getValue(I.getArgOperand(0)).getValueType(),
4941 getValue(I.getArgOperand(0)),
4942 getValue(I.getArgOperand(1)),
4943 getValue(I.getArgOperand(2))));
4944 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004945 SDValue Mul = DAG.getNode(ISD::FMUL, sdl,
Lang Hames5afba6f2012-06-05 19:07:46 +00004946 getValue(I.getArgOperand(0)).getValueType(),
4947 getValue(I.getArgOperand(0)),
4948 getValue(I.getArgOperand(1)));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004949 SDValue Add = DAG.getNode(ISD::FADD, sdl,
Lang Hames5afba6f2012-06-05 19:07:46 +00004950 getValue(I.getArgOperand(0)).getValueType(),
4951 Mul,
4952 getValue(I.getArgOperand(2)));
4953 setValue(&I, Add);
4954 }
4955 return 0;
4956 }
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004957 case Intrinsic::convert_to_fp16:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004958 setValue(&I, DAG.getNode(ISD::FP32_TO_FP16, sdl,
Gabor Greif0635f352010-06-25 09:38:13 +00004959 MVT::i16, getValue(I.getArgOperand(0))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004960 return 0;
4961 case Intrinsic::convert_from_fp16:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004962 setValue(&I, DAG.getNode(ISD::FP16_TO_FP32, sdl,
Gabor Greif0635f352010-06-25 09:38:13 +00004963 MVT::f32, getValue(I.getArgOperand(0))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004964 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004965 case Intrinsic::pcmarker: {
Gabor Greif0635f352010-06-25 09:38:13 +00004966 SDValue Tmp = getValue(I.getArgOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004967 DAG.setRoot(DAG.getNode(ISD::PCMARKER, sdl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004968 return 0;
4969 }
4970 case Intrinsic::readcyclecounter: {
4971 SDValue Op = getRoot();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004972 Res = DAG.getNode(ISD::READCYCLECOUNTER, sdl,
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004973 DAG.getVTList(MVT::i64, MVT::Other),
4974 &Op, 1);
4975 setValue(&I, Res);
4976 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004977 return 0;
4978 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004979 case Intrinsic::bswap:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004980 setValue(&I, DAG.getNode(ISD::BSWAP, sdl,
Gabor Greif0635f352010-06-25 09:38:13 +00004981 getValue(I.getArgOperand(0)).getValueType(),
4982 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004983 return 0;
4984 case Intrinsic::cttz: {
Gabor Greif0635f352010-06-25 09:38:13 +00004985 SDValue Arg = getValue(I.getArgOperand(0));
Chandler Carruth63974b22011-12-13 01:56:10 +00004986 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004987 EVT Ty = Arg.getValueType();
Chandler Carruth63974b22011-12-13 01:56:10 +00004988 setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTTZ : ISD::CTTZ_ZERO_UNDEF,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004989 sdl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004990 return 0;
4991 }
4992 case Intrinsic::ctlz: {
Gabor Greif0635f352010-06-25 09:38:13 +00004993 SDValue Arg = getValue(I.getArgOperand(0));
Chandler Carruth63974b22011-12-13 01:56:10 +00004994 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004995 EVT Ty = Arg.getValueType();
Chandler Carruth63974b22011-12-13 01:56:10 +00004996 setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTLZ : ISD::CTLZ_ZERO_UNDEF,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004997 sdl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004998 return 0;
4999 }
5000 case Intrinsic::ctpop: {
Gabor Greif0635f352010-06-25 09:38:13 +00005001 SDValue Arg = getValue(I.getArgOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00005002 EVT Ty = Arg.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005003 setValue(&I, DAG.getNode(ISD::CTPOP, sdl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005004 return 0;
5005 }
5006 case Intrinsic::stacksave: {
5007 SDValue Op = getRoot();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005008 Res = DAG.getNode(ISD::STACKSAVE, sdl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00005009 DAG.getVTList(TLI->getPointerTy(), MVT::Other), &Op, 1);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00005010 setValue(&I, Res);
5011 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005012 return 0;
5013 }
5014 case Intrinsic::stackrestore: {
Gabor Greif0635f352010-06-25 09:38:13 +00005015 Res = getValue(I.getArgOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005016 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, sdl, MVT::Other, getRoot(), Res));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005017 return 0;
5018 }
Bill Wendling57344502008-11-18 11:01:33 +00005019 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00005020 // Emit code into the DAG to store the stack guard onto the stack.
5021 MachineFunction &MF = DAG.getMachineFunction();
5022 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendlingba54bca2013-06-19 21:36:55 +00005023 EVT PtrTy = TLI->getPointerTy();
Bill Wendlingb2a42982008-11-06 02:29:10 +00005024
Gabor Greif0635f352010-06-25 09:38:13 +00005025 SDValue Src = getValue(I.getArgOperand(0)); // The guard's value.
5026 AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Bill Wendlingb2a42982008-11-06 02:29:10 +00005027
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00005028 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00005029 MFI->setStackProtectorIndex(FI);
5030
5031 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
5032
5033 // Store the stack protector onto the stack.
Andrew Trickac6d9be2013-05-25 02:42:55 +00005034 Res = DAG.getStore(getRoot(), sdl, Src, FIN,
Chris Lattner84bd98a2010-09-21 18:58:22 +00005035 MachinePointerInfo::getFixedStack(FI),
5036 true, false, 0);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00005037 setValue(&I, Res);
5038 DAG.setRoot(Res);
Bill Wendlingb2a42982008-11-06 02:29:10 +00005039 return 0;
5040 }
Eric Christopher7b5e6172009-10-27 00:52:25 +00005041 case Intrinsic::objectsize: {
5042 // If we don't know by now, we're never going to know.
Gabor Greif0635f352010-06-25 09:38:13 +00005043 ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(1));
Eric Christopher7b5e6172009-10-27 00:52:25 +00005044
5045 assert(CI && "Non-constant type in __builtin_object_size?");
5046
Gabor Greif0635f352010-06-25 09:38:13 +00005047 SDValue Arg = getValue(I.getCalledValue());
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00005048 EVT Ty = Arg.getValueType();
5049
Dan Gohmane368b462010-06-18 14:22:04 +00005050 if (CI->isZero())
Bill Wendlingd0283fa2009-12-22 00:40:51 +00005051 Res = DAG.getConstant(-1ULL, Ty);
Eric Christopher7b5e6172009-10-27 00:52:25 +00005052 else
Bill Wendlingd0283fa2009-12-22 00:40:51 +00005053 Res = DAG.getConstant(0, Ty);
5054
5055 setValue(&I, Res);
Eric Christopher7b5e6172009-10-27 00:52:25 +00005056 return 0;
5057 }
Justin Holewinskic2b7f5f2013-05-21 14:37:16 +00005058 case Intrinsic::annotation:
5059 case Intrinsic::ptr_annotation:
5060 // Drop the intrinsic, but forward the value
5061 setValue(&I, getValue(I.getOperand(0)));
5062 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005063 case Intrinsic::var_annotation:
5064 // Discard annotate attributes
5065 return 0;
5066
5067 case Intrinsic::init_trampoline: {
Gabor Greif0635f352010-06-25 09:38:13 +00005068 const Function *F = cast<Function>(I.getArgOperand(1)->stripPointerCasts());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005069
5070 SDValue Ops[6];
5071 Ops[0] = getRoot();
Gabor Greif0635f352010-06-25 09:38:13 +00005072 Ops[1] = getValue(I.getArgOperand(0));
5073 Ops[2] = getValue(I.getArgOperand(1));
5074 Ops[3] = getValue(I.getArgOperand(2));
5075 Ops[4] = DAG.getSrcValue(I.getArgOperand(0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005076 Ops[5] = DAG.getSrcValue(F);
5077
Andrew Trickac6d9be2013-05-25 02:42:55 +00005078 Res = DAG.getNode(ISD::INIT_TRAMPOLINE, sdl, MVT::Other, Ops, 6);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005079
Duncan Sands4a544a72011-09-06 13:37:06 +00005080 DAG.setRoot(Res);
5081 return 0;
5082 }
5083 case Intrinsic::adjust_trampoline: {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005084 setValue(&I, DAG.getNode(ISD::ADJUST_TRAMPOLINE, sdl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00005085 TLI->getPointerTy(),
Duncan Sands4a544a72011-09-06 13:37:06 +00005086 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005087 return 0;
5088 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005089 case Intrinsic::gcroot:
5090 if (GFI) {
Bill Wendling95dd4422012-05-01 22:50:45 +00005091 const Value *Alloca = I.getArgOperand(0)->stripPointerCasts();
Gabor Greif0635f352010-06-25 09:38:13 +00005092 const Constant *TypeMap = cast<Constant>(I.getArgOperand(1));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005093
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005094 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
5095 GFI->addStackRoot(FI->getIndex(), TypeMap);
5096 }
5097 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005098 case Intrinsic::gcread:
5099 case Intrinsic::gcwrite:
Torok Edwinc23197a2009-07-14 16:55:14 +00005100 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
Bill Wendlingd0283fa2009-12-22 00:40:51 +00005101 case Intrinsic::flt_rounds:
Andrew Trickac6d9be2013-05-25 02:42:55 +00005102 setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, sdl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005103 return 0;
Jakub Staszak9da99342011-07-06 18:22:43 +00005104
5105 case Intrinsic::expect: {
5106 // Just replace __builtin_expect(exp, c) with EXP.
5107 setValue(&I, getValue(I.getArgOperand(0)));
5108 return 0;
5109 }
5110
Shuxin Yang970755e2012-10-19 20:11:16 +00005111 case Intrinsic::debugtrap:
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005112 case Intrinsic::trap: {
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005113 StringRef TrapFuncName = TM.Options.getTrapFunctionName();
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005114 if (TrapFuncName.empty()) {
Stephen Lin155615d2013-07-08 00:37:03 +00005115 ISD::NodeType Op = (Intrinsic == Intrinsic::trap) ?
Shuxin Yang970755e2012-10-19 20:11:16 +00005116 ISD::TRAP : ISD::DEBUGTRAP;
Andrew Trickac6d9be2013-05-25 02:42:55 +00005117 DAG.setRoot(DAG.getNode(Op, sdl,MVT::Other, getRoot()));
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005118 return 0;
5119 }
5120 TargetLowering::ArgListTy Args;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00005121 TargetLowering::
5122 CallLoweringInfo CLI(getRoot(), I.getType(),
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005123 false, false, false, false, 0, CallingConv::C,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00005124 /*isTailCall=*/false,
5125 /*doesNotRet=*/false, /*isReturnValueUsed=*/true,
Bill Wendlingba54bca2013-06-19 21:36:55 +00005126 DAG.getExternalSymbol(TrapFuncName.data(),
5127 TLI->getPointerTy()),
Andrew Trickac6d9be2013-05-25 02:42:55 +00005128 Args, DAG, sdl);
Bill Wendlingba54bca2013-06-19 21:36:55 +00005129 std::pair<SDValue, SDValue> Result = TLI->LowerCallTo(CLI);
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005130 DAG.setRoot(Result.second);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005131 return 0;
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005132 }
Shuxin Yang970755e2012-10-19 20:11:16 +00005133
Bill Wendlingef375462008-11-21 02:38:44 +00005134 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00005135 case Intrinsic::sadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00005136 case Intrinsic::usub_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00005137 case Intrinsic::ssub_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00005138 case Intrinsic::umul_with_overflow:
Craig Topperc42e6402012-04-11 04:34:11 +00005139 case Intrinsic::smul_with_overflow: {
5140 ISD::NodeType Op;
5141 switch (Intrinsic) {
5142 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
5143 case Intrinsic::uadd_with_overflow: Op = ISD::UADDO; break;
5144 case Intrinsic::sadd_with_overflow: Op = ISD::SADDO; break;
5145 case Intrinsic::usub_with_overflow: Op = ISD::USUBO; break;
5146 case Intrinsic::ssub_with_overflow: Op = ISD::SSUBO; break;
5147 case Intrinsic::umul_with_overflow: Op = ISD::UMULO; break;
5148 case Intrinsic::smul_with_overflow: Op = ISD::SMULO; break;
5149 }
5150 SDValue Op1 = getValue(I.getArgOperand(0));
5151 SDValue Op2 = getValue(I.getArgOperand(1));
Bill Wendling7cdc3c82008-11-21 02:03:52 +00005152
Craig Topperc42e6402012-04-11 04:34:11 +00005153 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005154 setValue(&I, DAG.getNode(Op, sdl, VTs, Op1, Op2));
Craig Topperc42e6402012-04-11 04:34:11 +00005155 return 0;
5156 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005157 case Intrinsic::prefetch: {
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00005158 SDValue Ops[5];
Dale Johannesen1de4aa92010-10-26 23:11:10 +00005159 unsigned rw = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005160 Ops[0] = getRoot();
Gabor Greif0635f352010-06-25 09:38:13 +00005161 Ops[1] = getValue(I.getArgOperand(0));
5162 Ops[2] = getValue(I.getArgOperand(1));
5163 Ops[3] = getValue(I.getArgOperand(2));
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00005164 Ops[4] = getValue(I.getArgOperand(3));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005165 DAG.setRoot(DAG.getMemIntrinsicNode(ISD::PREFETCH, sdl,
Dale Johannesen1de4aa92010-10-26 23:11:10 +00005166 DAG.getVTList(MVT::Other),
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00005167 &Ops[0], 5,
Dale Johannesen1de4aa92010-10-26 23:11:10 +00005168 EVT::getIntegerVT(*Context, 8),
5169 MachinePointerInfo(I.getArgOperand(0)),
5170 0, /* align */
5171 false, /* volatile */
5172 rw==0, /* read */
5173 rw==1)); /* write */
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005174 return 0;
5175 }
Duncan Sandsf07c9492009-11-10 09:08:09 +00005176 case Intrinsic::lifetime_start:
Nadav Rotemc05d3062012-09-06 09:17:37 +00005177 case Intrinsic::lifetime_end: {
Nadav Rotemc05d3062012-09-06 09:17:37 +00005178 bool IsStart = (Intrinsic == Intrinsic::lifetime_start);
Nadav Rotem9a2ae002012-09-10 08:43:23 +00005179 // Stack coloring is not enabled in O0, discard region information.
5180 if (TM.getOptLevel() == CodeGenOpt::None)
5181 return 0;
Nadav Rotemc05d3062012-09-06 09:17:37 +00005182
Nadav Rotem9a2ae002012-09-10 08:43:23 +00005183 SmallVector<Value *, 4> Allocas;
5184 GetUnderlyingObjects(I.getArgOperand(1), Allocas, TD);
5185
Craig Topperf22fd3f2013-07-03 05:11:49 +00005186 for (SmallVectorImpl<Value*>::iterator Object = Allocas.begin(),
5187 E = Allocas.end(); Object != E; ++Object) {
Nadav Rotem9a2ae002012-09-10 08:43:23 +00005188 AllocaInst *LifetimeObject = dyn_cast_or_null<AllocaInst>(*Object);
5189
5190 // Could not find an Alloca.
5191 if (!LifetimeObject)
5192 continue;
5193
5194 int FI = FuncInfo.StaticAllocaMap[LifetimeObject];
5195
5196 SDValue Ops[2];
5197 Ops[0] = getRoot();
Bill Wendlingba54bca2013-06-19 21:36:55 +00005198 Ops[1] = DAG.getFrameIndex(FI, TLI->getPointerTy(), true);
Nadav Rotem9a2ae002012-09-10 08:43:23 +00005199 unsigned Opcode = (IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END);
5200
Andrew Trickac6d9be2013-05-25 02:42:55 +00005201 Res = DAG.getNode(Opcode, sdl, MVT::Other, Ops, 2);
Nadav Rotem9a2ae002012-09-10 08:43:23 +00005202 DAG.setRoot(Res);
5203 }
Nadav Rotem5882e562013-02-01 19:25:23 +00005204 return 0;
Nadav Rotemc05d3062012-09-06 09:17:37 +00005205 }
5206 case Intrinsic::invariant_start:
Duncan Sandsf07c9492009-11-10 09:08:09 +00005207 // Discard region information.
Bill Wendlingba54bca2013-06-19 21:36:55 +00005208 setValue(&I, DAG.getUNDEF(TLI->getPointerTy()));
Duncan Sandsf07c9492009-11-10 09:08:09 +00005209 return 0;
5210 case Intrinsic::invariant_end:
Duncan Sandsf07c9492009-11-10 09:08:09 +00005211 // Discard region information.
5212 return 0;
Nuno Lopes85b40892012-06-28 22:30:12 +00005213 case Intrinsic::donothing:
5214 // ignore
5215 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005216 }
5217}
5218
Dan Gohman46510a72010-04-15 01:51:59 +00005219void SelectionDAGBuilder::LowerCallTo(ImmutableCallSite CS, SDValue Callee,
Dan Gohman2048b852009-11-23 18:04:58 +00005220 bool isTailCall,
5221 MachineBasicBlock *LandingPad) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005222 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
5223 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
5224 Type *RetTy = FTy->getReturnType();
Chris Lattner512063d2010-04-05 06:19:28 +00005225 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Chris Lattner16112732010-03-14 01:41:15 +00005226 MCSymbol *BeginLabel = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005227
5228 TargetLowering::ArgListTy Args;
5229 TargetLowering::ArgListEntry Entry;
5230 Args.reserve(CS.arg_size());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005231
5232 // Check whether the function can return without sret-demotion.
Dan Gohman84023e02010-07-10 09:00:22 +00005233 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00005234 const TargetLowering *TLI = TM.getTargetLowering();
5235 GetReturnInfo(RetTy, CS.getAttributes(), Outs, *TLI);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005236
Bill Wendlingba54bca2013-06-19 21:36:55 +00005237 bool CanLowerReturn = TLI->CanLowerReturn(CS.getCallingConv(),
5238 DAG.getMachineFunction(),
5239 FTy->isVarArg(), Outs,
5240 FTy->getContext());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005241
5242 SDValue DemoteStackSlot;
Chris Lattnerecf42c42010-09-21 16:36:31 +00005243 int DemoteStackIdx = -100;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005244
5245 if (!CanLowerReturn) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00005246 uint64_t TySize = TLI->getDataLayout()->getTypeAllocSize(
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005247 FTy->getReturnType());
Bill Wendlingba54bca2013-06-19 21:36:55 +00005248 unsigned Align = TLI->getDataLayout()->getPrefTypeAlignment(
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005249 FTy->getReturnType());
5250 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattnerecf42c42010-09-21 16:36:31 +00005251 DemoteStackIdx = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005252 Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005253
Bill Wendlingba54bca2013-06-19 21:36:55 +00005254 DemoteStackSlot = DAG.getFrameIndex(DemoteStackIdx, TLI->getPointerTy());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005255 Entry.Node = DemoteStackSlot;
5256 Entry.Ty = StackSlotPtrType;
5257 Entry.isSExt = false;
5258 Entry.isZExt = false;
5259 Entry.isInReg = false;
5260 Entry.isSRet = true;
5261 Entry.isNest = false;
5262 Entry.isByVal = false;
Stephen Lin456ca042013-04-20 05:14:40 +00005263 Entry.isReturned = false;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005264 Entry.Alignment = Align;
5265 Args.push_back(Entry);
5266 RetTy = Type::getVoidTy(FTy->getContext());
5267 }
5268
Dan Gohman46510a72010-04-15 01:51:59 +00005269 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005270 i != e; ++i) {
Rafael Espindola3fa82832011-05-13 15:18:06 +00005271 const Value *V = *i;
5272
5273 // Skip empty types
5274 if (V->getType()->isEmptyTy())
5275 continue;
5276
5277 SDValue ArgNode = getValue(V);
5278 Entry.Node = ArgNode; Entry.Ty = V->getType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005279
5280 unsigned attrInd = i - CS.arg_begin() + 1;
Stephen Lin456ca042013-04-20 05:14:40 +00005281 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
5282 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
5283 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
5284 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
5285 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
5286 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
5287 Entry.isReturned = CS.paramHasAttr(attrInd, Attribute::Returned);
5288 Entry.Alignment = CS.getParamAlignment(attrInd);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005289 Args.push_back(Entry);
5290 }
5291
Chris Lattner512063d2010-04-05 06:19:28 +00005292 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005293 // Insert a label before the invoke call to mark the try range. This can be
5294 // used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00005295 BeginLabel = MMI.getContext().CreateTempSymbol();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00005296
Jim Grosbachca752c92010-01-28 01:45:32 +00005297 // For SjLj, keep track of which landing pads go with which invokes
5298 // so as to maintain the ordering of pads in the LSDA.
Chris Lattner512063d2010-04-05 06:19:28 +00005299 unsigned CallSiteIndex = MMI.getCurrentCallSite();
Jim Grosbachca752c92010-01-28 01:45:32 +00005300 if (CallSiteIndex) {
Chris Lattner512063d2010-04-05 06:19:28 +00005301 MMI.setCallSiteBeginLabel(BeginLabel, CallSiteIndex);
Bill Wendling30e67402011-10-05 22:24:35 +00005302 LPadToCallSiteMap[LandingPad].push_back(CallSiteIndex);
Bill Wendlinga8512ed2011-10-04 22:00:35 +00005303
Jim Grosbachca752c92010-01-28 01:45:32 +00005304 // Now that the call site is handled, stop tracking it.
Chris Lattner512063d2010-04-05 06:19:28 +00005305 MMI.setCurrentCallSite(0);
Jim Grosbachca752c92010-01-28 01:45:32 +00005306 }
5307
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005308 // Both PendingLoads and PendingExports must be flushed here;
5309 // this call might not return.
5310 (void)getRoot();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005311 DAG.setRoot(DAG.getEHLabel(getCurSDLoc(), getControlRoot(), BeginLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005312 }
5313
Dan Gohman98ca4f22009-08-05 01:29:28 +00005314 // Check if target-independent constraints permit a tail call here.
Bill Wendlingba54bca2013-06-19 21:36:55 +00005315 // Target-dependent constraints are checked within TLI->LowerCallTo.
5316 if (isTailCall && !isInTailCallPosition(CS, *TLI))
Dan Gohman98ca4f22009-08-05 01:29:28 +00005317 isTailCall = false;
5318
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00005319 TargetLowering::
5320 CallLoweringInfo CLI(getRoot(), RetTy, FTy, isTailCall, Callee, Args, DAG,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005321 getCurSDLoc(), CS);
Bill Wendlingba54bca2013-06-19 21:36:55 +00005322 std::pair<SDValue,SDValue> Result = TLI->LowerCallTo(CLI);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005323 assert((isTailCall || Result.second.getNode()) &&
5324 "Non-null chain expected with non-tail call!");
5325 assert((Result.second.getNode() || !Result.first.getNode()) &&
5326 "Null value expected with tail call!");
Bill Wendlinge80ae832009-12-22 00:50:32 +00005327 if (Result.first.getNode()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005328 setValue(CS.getInstruction(), Result.first);
Bill Wendlinge80ae832009-12-22 00:50:32 +00005329 } else if (!CanLowerReturn && Result.second.getNode()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005330 // The instruction result is the result of loading from the
5331 // hidden sret parameter.
5332 SmallVector<EVT, 1> PVTs;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005333 Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005334
Bill Wendlingba54bca2013-06-19 21:36:55 +00005335 ComputeValueVTs(*TLI, PtrRetTy, PVTs);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005336 assert(PVTs.size() == 1 && "Pointers should fit in one register");
5337 EVT PtrVT = PVTs[0];
Eli Friedman2db0e9e2012-05-25 00:09:29 +00005338
5339 SmallVector<EVT, 4> RetTys;
5340 SmallVector<uint64_t, 4> Offsets;
5341 RetTy = FTy->getReturnType();
Bill Wendlingba54bca2013-06-19 21:36:55 +00005342 ComputeValueVTs(*TLI, RetTy, RetTys, &Offsets);
Eli Friedman2db0e9e2012-05-25 00:09:29 +00005343
5344 unsigned NumValues = RetTys.size();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005345 SmallVector<SDValue, 4> Values(NumValues);
5346 SmallVector<SDValue, 4> Chains(NumValues);
5347
5348 for (unsigned i = 0; i < NumValues; ++i) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005349 SDValue Add = DAG.getNode(ISD::ADD, getCurSDLoc(), PtrVT,
Bill Wendlinge80ae832009-12-22 00:50:32 +00005350 DemoteStackSlot,
5351 DAG.getConstant(Offsets[i], PtrVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005352 SDValue L = DAG.getLoad(RetTys[i], getCurSDLoc(), Result.second, Add,
Chris Lattnerecf42c42010-09-21 16:36:31 +00005353 MachinePointerInfo::getFixedStack(DemoteStackIdx, Offsets[i]),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005354 false, false, false, 1);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005355 Values[i] = L;
5356 Chains[i] = L.getValue(1);
5357 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00005358
Andrew Trickac6d9be2013-05-25 02:42:55 +00005359 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005360 MVT::Other, &Chains[0], NumValues);
5361 PendingLoads.push_back(Chain);
Michael J. Spencere70c5262010-10-16 08:25:21 +00005362
Bill Wendling4533cac2010-01-28 21:51:40 +00005363 setValue(CS.getInstruction(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00005364 DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00005365 DAG.getVTList(&RetTys[0], RetTys.size()),
Eli Friedman2db0e9e2012-05-25 00:09:29 +00005366 &Values[0], Values.size()));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005367 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00005368
Evan Cheng8380c032011-04-01 19:42:22 +00005369 if (!Result.second.getNode()) {
Evan Chengc249e482011-04-01 19:57:01 +00005370 // As a special case, a null chain means that a tail call has been emitted and
5371 // the DAG root is already updated.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005372 HasTailCall = true;
Tim Northovere5a81a12013-07-06 12:58:45 +00005373
5374 // Since there's no actual continuation from this block, nothing can be
5375 // relying on us setting vregs for them.
5376 PendingExports.clear();
Evan Cheng8380c032011-04-01 19:42:22 +00005377 } else {
5378 DAG.setRoot(Result.second);
Evan Cheng8380c032011-04-01 19:42:22 +00005379 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005380
Chris Lattner512063d2010-04-05 06:19:28 +00005381 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005382 // Insert a label at the end of the invoke call to mark the try range. This
5383 // can be used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00005384 MCSymbol *EndLabel = MMI.getContext().CreateTempSymbol();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005385 DAG.setRoot(DAG.getEHLabel(getCurSDLoc(), getRoot(), EndLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005386
5387 // Inform MachineModuleInfo of range.
Chris Lattner512063d2010-04-05 06:19:28 +00005388 MMI.addInvoke(LandingPad, BeginLabel, EndLabel);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005389 }
5390}
5391
Chris Lattner8047d9a2009-12-24 00:37:38 +00005392/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
5393/// value is equal or not-equal to zero.
Dan Gohman46510a72010-04-15 01:51:59 +00005394static bool IsOnlyUsedInZeroEqualityComparison(const Value *V) {
5395 for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end();
Chris Lattner8047d9a2009-12-24 00:37:38 +00005396 UI != E; ++UI) {
Dan Gohman46510a72010-04-15 01:51:59 +00005397 if (const ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
Chris Lattner8047d9a2009-12-24 00:37:38 +00005398 if (IC->isEquality())
Dan Gohman46510a72010-04-15 01:51:59 +00005399 if (const Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
Chris Lattner8047d9a2009-12-24 00:37:38 +00005400 if (C->isNullValue())
5401 continue;
5402 // Unknown instruction.
5403 return false;
5404 }
5405 return true;
5406}
5407
Dan Gohman46510a72010-04-15 01:51:59 +00005408static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005409 Type *LoadTy,
Chris Lattner8047d9a2009-12-24 00:37:38 +00005410 SelectionDAGBuilder &Builder) {
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005411
Chris Lattner8047d9a2009-12-24 00:37:38 +00005412 // Check to see if this load can be trivially constant folded, e.g. if the
5413 // input is from a string literal.
Dan Gohman46510a72010-04-15 01:51:59 +00005414 if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00005415 // Cast pointer to the type we really want to load.
Dan Gohman46510a72010-04-15 01:51:59 +00005416 LoadInput = ConstantExpr::getBitCast(const_cast<Constant *>(LoadInput),
Chris Lattner8047d9a2009-12-24 00:37:38 +00005417 PointerType::getUnqual(LoadTy));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005418
Dan Gohman46510a72010-04-15 01:51:59 +00005419 if (const Constant *LoadCst =
5420 ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput),
5421 Builder.TD))
Chris Lattner8047d9a2009-12-24 00:37:38 +00005422 return Builder.getValue(LoadCst);
5423 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005424
Chris Lattner8047d9a2009-12-24 00:37:38 +00005425 // Otherwise, we have to emit the load. If the pointer is to unfoldable but
5426 // still constant memory, the input chain can be the entry node.
5427 SDValue Root;
5428 bool ConstantMemory = false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005429
Chris Lattner8047d9a2009-12-24 00:37:38 +00005430 // Do not serialize (non-volatile) loads of constant memory with anything.
5431 if (Builder.AA->pointsToConstantMemory(PtrVal)) {
5432 Root = Builder.DAG.getEntryNode();
5433 ConstantMemory = true;
5434 } else {
5435 // Do not serialize non-volatile loads against each other.
5436 Root = Builder.DAG.getRoot();
5437 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005438
Chris Lattner8047d9a2009-12-24 00:37:38 +00005439 SDValue Ptr = Builder.getValue(PtrVal);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005440 SDValue LoadVal = Builder.DAG.getLoad(LoadVT, Builder.getCurSDLoc(), Root,
Chris Lattnerecf42c42010-09-21 16:36:31 +00005441 Ptr, MachinePointerInfo(PtrVal),
David Greene1e559442010-02-15 17:00:31 +00005442 false /*volatile*/,
Stephen Lin155615d2013-07-08 00:37:03 +00005443 false /*nontemporal*/,
Pete Cooperd752e0f2011-11-08 18:42:53 +00005444 false /*isinvariant*/, 1 /* align=1 */);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005445
Chris Lattner8047d9a2009-12-24 00:37:38 +00005446 if (!ConstantMemory)
5447 Builder.PendingLoads.push_back(LoadVal.getValue(1));
5448 return LoadVal;
5449}
5450
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005451/// processIntegerCallValue - Record the value for an instruction that
5452/// produces an integer result, converting the type where necessary.
5453void SelectionDAGBuilder::processIntegerCallValue(const Instruction &I,
5454 SDValue Value,
5455 bool IsSigned) {
5456 EVT VT = TM.getTargetLowering()->getValueType(I.getType(), true);
5457 if (IsSigned)
5458 Value = DAG.getSExtOrTrunc(Value, getCurSDLoc(), VT);
5459 else
5460 Value = DAG.getZExtOrTrunc(Value, getCurSDLoc(), VT);
5461 setValue(&I, Value);
5462}
Chris Lattner8047d9a2009-12-24 00:37:38 +00005463
5464/// visitMemCmpCall - See if we can lower a call to memcmp in an optimized form.
5465/// If so, return true and lower it, otherwise return false and it will be
5466/// lowered like a normal call.
Dan Gohman46510a72010-04-15 01:51:59 +00005467bool SelectionDAGBuilder::visitMemCmpCall(const CallInst &I) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00005468 // Verify that the prototype makes sense. int memcmp(void*,void*,size_t)
Gabor Greif37387d52010-06-30 12:55:46 +00005469 if (I.getNumArgOperands() != 3)
Chris Lattner8047d9a2009-12-24 00:37:38 +00005470 return false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005471
Gabor Greif0635f352010-06-25 09:38:13 +00005472 const Value *LHS = I.getArgOperand(0), *RHS = I.getArgOperand(1);
Duncan Sands1df98592010-02-16 11:11:14 +00005473 if (!LHS->getType()->isPointerTy() || !RHS->getType()->isPointerTy() ||
Gabor Greif0635f352010-06-25 09:38:13 +00005474 !I.getArgOperand(2)->getType()->isIntegerTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +00005475 !I.getType()->isIntegerTy())
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005476 return false;
5477
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005478 const Value *Size = I.getArgOperand(2);
5479 const ConstantInt *CSize = dyn_cast<ConstantInt>(Size);
5480 if (CSize && CSize->getZExtValue() == 0) {
Richard Sandifordac168b82013-08-12 10:28:10 +00005481 EVT CallVT = TM.getTargetLowering()->getValueType(I.getType(), true);
5482 setValue(&I, DAG.getConstant(0, CallVT));
5483 return true;
5484 }
5485
Richard Sandifordac168b82013-08-12 10:28:10 +00005486 const TargetSelectionDAGInfo &TSI = DAG.getSelectionDAGInfo();
5487 std::pair<SDValue, SDValue> Res =
5488 TSI.EmitTargetCodeForMemcmp(DAG, getCurSDLoc(), DAG.getRoot(),
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005489 getValue(LHS), getValue(RHS), getValue(Size),
5490 MachinePointerInfo(LHS),
5491 MachinePointerInfo(RHS));
Richard Sandifordac168b82013-08-12 10:28:10 +00005492 if (Res.first.getNode()) {
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005493 processIntegerCallValue(I, Res.first, true);
5494 PendingLoads.push_back(Res.second);
Richard Sandifordac168b82013-08-12 10:28:10 +00005495 return true;
5496 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005497
Chris Lattner8047d9a2009-12-24 00:37:38 +00005498 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
5499 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005500 if (CSize && IsOnlyUsedInZeroEqualityComparison(&I)) {
Chris Lattner04b091a2009-12-24 01:07:17 +00005501 bool ActuallyDoIt = true;
5502 MVT LoadVT;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005503 Type *LoadTy;
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005504 switch (CSize->getZExtValue()) {
Chris Lattner04b091a2009-12-24 01:07:17 +00005505 default:
5506 LoadVT = MVT::Other;
5507 LoadTy = 0;
5508 ActuallyDoIt = false;
5509 break;
5510 case 2:
5511 LoadVT = MVT::i16;
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005512 LoadTy = Type::getInt16Ty(CSize->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005513 break;
5514 case 4:
5515 LoadVT = MVT::i32;
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005516 LoadTy = Type::getInt32Ty(CSize->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005517 break;
5518 case 8:
5519 LoadVT = MVT::i64;
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005520 LoadTy = Type::getInt64Ty(CSize->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005521 break;
5522 /*
5523 case 16:
5524 LoadVT = MVT::v4i32;
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005525 LoadTy = Type::getInt32Ty(CSize->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005526 LoadTy = VectorType::get(LoadTy, 4);
5527 break;
5528 */
5529 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005530
Chris Lattner04b091a2009-12-24 01:07:17 +00005531 // This turns into unaligned loads. We only do this if the target natively
5532 // supports the MVT we'll be loading or if it is small enough (<= 4) that
5533 // we'll only produce a small number of byte loads.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005534
Chris Lattner04b091a2009-12-24 01:07:17 +00005535 // Require that we can find a legal MVT, and only do this if the target
5536 // supports unaligned loads of that type. Expanding into byte loads would
5537 // bloat the code.
Bill Wendlingba54bca2013-06-19 21:36:55 +00005538 const TargetLowering *TLI = TM.getTargetLowering();
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005539 if (ActuallyDoIt && CSize->getZExtValue() > 4) {
Chris Lattner04b091a2009-12-24 01:07:17 +00005540 // TODO: Handle 5 byte compare as 4-byte + 1 byte.
5541 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
Bill Wendlingba54bca2013-06-19 21:36:55 +00005542 if (!TLI->isTypeLegal(LoadVT) ||!TLI->allowsUnalignedMemoryAccesses(LoadVT))
Chris Lattner04b091a2009-12-24 01:07:17 +00005543 ActuallyDoIt = false;
5544 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005545
Chris Lattner04b091a2009-12-24 01:07:17 +00005546 if (ActuallyDoIt) {
5547 SDValue LHSVal = getMemCmpLoad(LHS, LoadVT, LoadTy, *this);
5548 SDValue RHSVal = getMemCmpLoad(RHS, LoadVT, LoadTy, *this);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005549
Andrew Trickac6d9be2013-05-25 02:42:55 +00005550 SDValue Res = DAG.getSetCC(getCurSDLoc(), MVT::i1, LHSVal, RHSVal,
Chris Lattner04b091a2009-12-24 01:07:17 +00005551 ISD::SETNE);
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005552 processIntegerCallValue(I, Res, false);
Chris Lattner04b091a2009-12-24 01:07:17 +00005553 return true;
5554 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00005555 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005556
5557
Chris Lattner8047d9a2009-12-24 00:37:38 +00005558 return false;
5559}
5560
Richard Sandiford4fc73552013-08-16 11:29:37 +00005561/// visitStrCpyCall -- See if we can lower a strcpy or stpcpy call into an
5562/// optimized form. If so, return true and lower it, otherwise return false
5563/// and it will be lowered like a normal call.
5564bool SelectionDAGBuilder::visitStrCpyCall(const CallInst &I, bool isStpcpy) {
5565 // Verify that the prototype makes sense. char *strcpy(char *, char *)
5566 if (I.getNumArgOperands() != 2)
5567 return false;
5568
5569 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
5570 if (!Arg0->getType()->isPointerTy() ||
5571 !Arg1->getType()->isPointerTy() ||
5572 !I.getType()->isPointerTy())
5573 return false;
5574
5575 const TargetSelectionDAGInfo &TSI = DAG.getSelectionDAGInfo();
5576 std::pair<SDValue, SDValue> Res =
5577 TSI.EmitTargetCodeForStrcpy(DAG, getCurSDLoc(), getRoot(),
5578 getValue(Arg0), getValue(Arg1),
5579 MachinePointerInfo(Arg0),
5580 MachinePointerInfo(Arg1), isStpcpy);
5581 if (Res.first.getNode()) {
5582 setValue(&I, Res.first);
5583 DAG.setRoot(Res.second);
5584 return true;
5585 }
5586
5587 return false;
5588}
5589
Richard Sandiforde1b2af72013-08-16 11:21:54 +00005590/// visitStrCmpCall - See if we can lower a call to strcmp in an optimized form.
5591/// If so, return true and lower it, otherwise return false and it will be
5592/// lowered like a normal call.
5593bool SelectionDAGBuilder::visitStrCmpCall(const CallInst &I) {
5594 // Verify that the prototype makes sense. int strcmp(void*,void*)
5595 if (I.getNumArgOperands() != 2)
5596 return false;
5597
5598 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
5599 if (!Arg0->getType()->isPointerTy() ||
5600 !Arg1->getType()->isPointerTy() ||
5601 !I.getType()->isIntegerTy())
5602 return false;
5603
5604 const TargetSelectionDAGInfo &TSI = DAG.getSelectionDAGInfo();
5605 std::pair<SDValue, SDValue> Res =
5606 TSI.EmitTargetCodeForStrcmp(DAG, getCurSDLoc(), DAG.getRoot(),
5607 getValue(Arg0), getValue(Arg1),
5608 MachinePointerInfo(Arg0),
5609 MachinePointerInfo(Arg1));
5610 if (Res.first.getNode()) {
5611 processIntegerCallValue(I, Res.first, true);
5612 PendingLoads.push_back(Res.second);
5613 return true;
5614 }
5615
5616 return false;
5617}
5618
Bob Wilson53624a22012-08-03 23:29:17 +00005619/// visitUnaryFloatCall - If a call instruction is a unary floating-point
5620/// operation (as expected), translate it to an SDNode with the specified opcode
5621/// and return true.
5622bool SelectionDAGBuilder::visitUnaryFloatCall(const CallInst &I,
5623 unsigned Opcode) {
5624 // Sanity check that it really is a unary floating-point call.
5625 if (I.getNumArgOperands() != 1 ||
5626 !I.getArgOperand(0)->getType()->isFloatingPointTy() ||
5627 I.getType() != I.getArgOperand(0)->getType() ||
5628 !I.onlyReadsMemory())
5629 return false;
5630
5631 SDValue Tmp = getValue(I.getArgOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005632 setValue(&I, DAG.getNode(Opcode, getCurSDLoc(), Tmp.getValueType(), Tmp));
Bob Wilson53624a22012-08-03 23:29:17 +00005633 return true;
5634}
Chris Lattner8047d9a2009-12-24 00:37:38 +00005635
Dan Gohman46510a72010-04-15 01:51:59 +00005636void SelectionDAGBuilder::visitCall(const CallInst &I) {
Chris Lattner598751e2010-07-05 05:36:21 +00005637 // Handle inline assembly differently.
5638 if (isa<InlineAsm>(I.getCalledValue())) {
5639 visitInlineAsm(&I);
5640 return;
5641 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00005642
Michael J. Spencer391b43b2010-10-21 20:49:23 +00005643 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Michael J. Spencerc9c137b2012-02-22 19:06:13 +00005644 ComputeUsesVAFloatArgument(I, &MMI);
Michael J. Spencer391b43b2010-10-21 20:49:23 +00005645
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005646 const char *RenameFn = 0;
5647 if (Function *F = I.getCalledFunction()) {
5648 if (F->isDeclaration()) {
Chris Lattner598751e2010-07-05 05:36:21 +00005649 if (const TargetIntrinsicInfo *II = TM.getIntrinsicInfo()) {
Dale Johannesen49de9822009-02-05 01:49:45 +00005650 if (unsigned IID = II->getIntrinsicID(F)) {
5651 RenameFn = visitIntrinsicCall(I, IID);
5652 if (!RenameFn)
5653 return;
5654 }
5655 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005656 if (unsigned IID = F->getIntrinsicID()) {
5657 RenameFn = visitIntrinsicCall(I, IID);
5658 if (!RenameFn)
5659 return;
5660 }
5661 }
5662
5663 // Check for well-known libc/libm calls. If the function is internal, it
5664 // can't be a library call.
Bob Wilson982dc842012-08-03 21:26:24 +00005665 LibFunc::Func Func;
5666 if (!F->hasLocalLinkage() && F->hasName() &&
5667 LibInfo->getLibFunc(F->getName(), Func) &&
5668 LibInfo->hasOptimizedCodeGen(Func)) {
5669 switch (Func) {
5670 default: break;
5671 case LibFunc::copysign:
5672 case LibFunc::copysignf:
5673 case LibFunc::copysignl:
Gabor Greif37387d52010-06-30 12:55:46 +00005674 if (I.getNumArgOperands() == 2 && // Basic sanity checks.
Gabor Greif0635f352010-06-25 09:38:13 +00005675 I.getArgOperand(0)->getType()->isFloatingPointTy() &&
5676 I.getType() == I.getArgOperand(0)->getType() &&
Bob Wilson53624a22012-08-03 23:29:17 +00005677 I.getType() == I.getArgOperand(1)->getType() &&
5678 I.onlyReadsMemory()) {
Gabor Greif0635f352010-06-25 09:38:13 +00005679 SDValue LHS = getValue(I.getArgOperand(0));
5680 SDValue RHS = getValue(I.getArgOperand(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005681 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurSDLoc(),
Bill Wendling0d580132009-12-23 01:28:19 +00005682 LHS.getValueType(), LHS, RHS));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005683 return;
5684 }
Bob Wilson982dc842012-08-03 21:26:24 +00005685 break;
5686 case LibFunc::fabs:
5687 case LibFunc::fabsf:
5688 case LibFunc::fabsl:
Bob Wilson53624a22012-08-03 23:29:17 +00005689 if (visitUnaryFloatCall(I, ISD::FABS))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005690 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005691 break;
5692 case LibFunc::sin:
5693 case LibFunc::sinf:
5694 case LibFunc::sinl:
Bob Wilson53624a22012-08-03 23:29:17 +00005695 if (visitUnaryFloatCall(I, ISD::FSIN))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005696 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005697 break;
5698 case LibFunc::cos:
5699 case LibFunc::cosf:
5700 case LibFunc::cosl:
Bob Wilson53624a22012-08-03 23:29:17 +00005701 if (visitUnaryFloatCall(I, ISD::FCOS))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005702 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005703 break;
5704 case LibFunc::sqrt:
5705 case LibFunc::sqrtf:
5706 case LibFunc::sqrtl:
Preston Gurdb704d232013-05-27 15:44:35 +00005707 case LibFunc::sqrt_finite:
5708 case LibFunc::sqrtf_finite:
5709 case LibFunc::sqrtl_finite:
Bob Wilson53624a22012-08-03 23:29:17 +00005710 if (visitUnaryFloatCall(I, ISD::FSQRT))
Dale Johannesen52fb79b2009-09-25 17:23:22 +00005711 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005712 break;
5713 case LibFunc::floor:
5714 case LibFunc::floorf:
5715 case LibFunc::floorl:
Bob Wilson53624a22012-08-03 23:29:17 +00005716 if (visitUnaryFloatCall(I, ISD::FFLOOR))
Owen Anderson4a4fdf32011-12-08 19:32:14 +00005717 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005718 break;
5719 case LibFunc::nearbyint:
5720 case LibFunc::nearbyintf:
5721 case LibFunc::nearbyintl:
Bob Wilson53624a22012-08-03 23:29:17 +00005722 if (visitUnaryFloatCall(I, ISD::FNEARBYINT))
Owen Anderson4a4fdf32011-12-08 19:32:14 +00005723 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005724 break;
5725 case LibFunc::ceil:
5726 case LibFunc::ceilf:
5727 case LibFunc::ceill:
Bob Wilson53624a22012-08-03 23:29:17 +00005728 if (visitUnaryFloatCall(I, ISD::FCEIL))
Owen Anderson4a4fdf32011-12-08 19:32:14 +00005729 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005730 break;
5731 case LibFunc::rint:
5732 case LibFunc::rintf:
5733 case LibFunc::rintl:
Bob Wilson53624a22012-08-03 23:29:17 +00005734 if (visitUnaryFloatCall(I, ISD::FRINT))
Owen Anderson4a4fdf32011-12-08 19:32:14 +00005735 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005736 break;
Hal Finkel41418d12013-08-07 22:49:12 +00005737 case LibFunc::round:
5738 case LibFunc::roundf:
5739 case LibFunc::roundl:
5740 if (visitUnaryFloatCall(I, ISD::FROUND))
5741 return;
5742 break;
Bob Wilson982dc842012-08-03 21:26:24 +00005743 case LibFunc::trunc:
5744 case LibFunc::truncf:
5745 case LibFunc::truncl:
Bob Wilson53624a22012-08-03 23:29:17 +00005746 if (visitUnaryFloatCall(I, ISD::FTRUNC))
Owen Anderson4a4fdf32011-12-08 19:32:14 +00005747 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005748 break;
5749 case LibFunc::log2:
5750 case LibFunc::log2f:
5751 case LibFunc::log2l:
Bob Wilson53624a22012-08-03 23:29:17 +00005752 if (visitUnaryFloatCall(I, ISD::FLOG2))
Owen Anderson4e0adfa2011-12-15 00:54:12 +00005753 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005754 break;
5755 case LibFunc::exp2:
5756 case LibFunc::exp2f:
5757 case LibFunc::exp2l:
Bob Wilson53624a22012-08-03 23:29:17 +00005758 if (visitUnaryFloatCall(I, ISD::FEXP2))
Owen Anderson4e0adfa2011-12-15 00:54:12 +00005759 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005760 break;
5761 case LibFunc::memcmp:
Chris Lattner8047d9a2009-12-24 00:37:38 +00005762 if (visitMemCmpCall(I))
5763 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005764 break;
Richard Sandiford4fc73552013-08-16 11:29:37 +00005765 case LibFunc::strcpy:
5766 if (visitStrCpyCall(I, false))
5767 return;
5768 break;
5769 case LibFunc::stpcpy:
5770 if (visitStrCpyCall(I, true))
5771 return;
5772 break;
Richard Sandiforde1b2af72013-08-16 11:21:54 +00005773 case LibFunc::strcmp:
5774 if (visitStrCmpCall(I))
5775 return;
5776 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005777 }
5778 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005779 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00005780
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005781 SDValue Callee;
5782 if (!RenameFn)
Gabor Greif0635f352010-06-25 09:38:13 +00005783 Callee = getValue(I.getCalledValue());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005784 else
Bill Wendlingba54bca2013-06-19 21:36:55 +00005785 Callee = DAG.getExternalSymbol(RenameFn,
5786 TM.getTargetLowering()->getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005787
Bill Wendling0d580132009-12-23 01:28:19 +00005788 // Check if we can potentially perform a tail call. More detailed checking is
5789 // be done within LowerCallTo, after more information about the call is known.
Evan Cheng11e67932010-01-26 23:13:04 +00005790 LowerCallTo(&I, Callee, I.isTailCall());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005791}
5792
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005793namespace {
Dan Gohman462f6b52010-05-29 17:53:24 +00005794
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005795/// AsmOperandInfo - This contains information for each constraint that we are
5796/// lowering.
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005797class SDISelAsmOperandInfo : public TargetLowering::AsmOperandInfo {
Cedric Venetaff9c272009-02-14 16:06:42 +00005798public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005799 /// CallOperand - If this is the result output operand or a clobber
5800 /// this is null, otherwise it is the incoming operand to the CallInst.
5801 /// This gets modified as the asm is processed.
5802 SDValue CallOperand;
5803
5804 /// AssignedRegs - If this is a register or register class operand, this
5805 /// contains the set of register corresponding to the operand.
5806 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005807
John Thompsoneac6e1d2010-09-13 18:15:37 +00005808 explicit SDISelAsmOperandInfo(const TargetLowering::AsmOperandInfo &info)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005809 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
5810 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005811
Owen Andersone50ed302009-08-10 22:56:29 +00005812 /// getCallOperandValEVT - Return the EVT of the Value* that this operand
Chris Lattner81249c92008-10-17 17:05:25 +00005813 /// corresponds to. If there is no Value* for this operand, it returns
Owen Anderson825b72b2009-08-11 20:47:22 +00005814 /// MVT::Other.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005815 EVT getCallOperandValEVT(LLVMContext &Context,
Owen Anderson1d0be152009-08-13 21:58:54 +00005816 const TargetLowering &TLI,
Micah Villmow3574eca2012-10-08 16:38:25 +00005817 const DataLayout *TD) const {
Owen Anderson825b72b2009-08-11 20:47:22 +00005818 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005819
Chris Lattner81249c92008-10-17 17:05:25 +00005820 if (isa<BasicBlock>(CallOperandVal))
5821 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005822
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005823 llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005824
Eric Christophercef81b72011-05-09 20:04:43 +00005825 // FIXME: code duplicated from TargetLowering::ParseConstraints().
Chris Lattner81249c92008-10-17 17:05:25 +00005826 // If this is an indirect operand, the operand is a pointer to the
5827 // accessed type.
Bob Wilsone261b0c2009-12-22 18:34:19 +00005828 if (isIndirect) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005829 llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
Bob Wilsone261b0c2009-12-22 18:34:19 +00005830 if (!PtrTy)
Chris Lattner75361b62010-04-07 22:58:41 +00005831 report_fatal_error("Indirect operand for inline asm not a pointer!");
Bob Wilsone261b0c2009-12-22 18:34:19 +00005832 OpTy = PtrTy->getElementType();
5833 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005834
Eric Christophercef81b72011-05-09 20:04:43 +00005835 // Look for vector wrapped in a struct. e.g. { <16 x i8> }.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005836 if (StructType *STy = dyn_cast<StructType>(OpTy))
Eric Christophercef81b72011-05-09 20:04:43 +00005837 if (STy->getNumElements() == 1)
5838 OpTy = STy->getElementType(0);
5839
Chris Lattner81249c92008-10-17 17:05:25 +00005840 // If OpTy is not a single value, it may be a struct/union that we
5841 // can tile with integers.
5842 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
5843 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
5844 switch (BitSize) {
5845 default: break;
5846 case 1:
5847 case 8:
5848 case 16:
5849 case 32:
5850 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00005851 case 128:
Owen Anderson1d0be152009-08-13 21:58:54 +00005852 OpTy = IntegerType::get(Context, BitSize);
Chris Lattner81249c92008-10-17 17:05:25 +00005853 break;
5854 }
5855 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005856
Chris Lattner81249c92008-10-17 17:05:25 +00005857 return TLI.getValueType(OpTy, true);
5858 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005859};
Dan Gohman462f6b52010-05-29 17:53:24 +00005860
John Thompson44ab89e2010-10-29 17:29:13 +00005861typedef SmallVector<SDISelAsmOperandInfo,16> SDISelAsmOperandInfoVector;
5862
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005863} // end anonymous namespace
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005864
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005865/// GetRegistersForValue - Assign registers (virtual or physical) for the
5866/// specified operand. We prefer to assign virtual registers, to allow the
Bob Wilson266d9452009-12-17 05:07:36 +00005867/// register allocator to handle the assignment process. However, if the asm
5868/// uses features that we can't model on machineinstrs, we have SDISel do the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005869/// allocation. This produces generally horrible, but correct, code.
5870///
5871/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005872///
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005873static void GetRegistersForValue(SelectionDAG &DAG,
5874 const TargetLowering &TLI,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005875 SDLoc DL,
Benjamin Kramer8b93ff22012-02-24 14:01:17 +00005876 SDISelAsmOperandInfo &OpInfo) {
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005877 LLVMContext &Context = *DAG.getContext();
Owen Anderson23b9b192009-08-12 00:36:31 +00005878
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005879 MachineFunction &MF = DAG.getMachineFunction();
5880 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005881
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005882 // If this is a constraint for a single physreg, or a constraint for a
5883 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005884 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005885 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
5886 OpInfo.ConstraintVT);
5887
5888 unsigned NumRegs = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00005889 if (OpInfo.ConstraintVT != MVT::Other) {
Chris Lattner01426e12008-10-21 00:45:36 +00005890 // If this is a FP input in an integer register (or visa versa) insert a bit
5891 // cast of the input value. More generally, handle any case where the input
5892 // value disagrees with the register class we plan to stick this in.
5893 if (OpInfo.Type == InlineAsm::isInput &&
5894 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00005895 // Try to convert to the first EVT that the reg class contains. If the
Chris Lattner01426e12008-10-21 00:45:36 +00005896 // types are identical size, use a bitcast to convert (e.g. two differing
5897 // vector types).
Patrik Hagglund8963fec2012-12-19 12:23:01 +00005898 MVT RegVT = *PhysReg.second->vt_begin();
Chris Lattner01426e12008-10-21 00:45:36 +00005899 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005900 OpInfo.CallOperand = DAG.getNode(ISD::BITCAST, DL,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005901 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005902 OpInfo.ConstraintVT = RegVT;
5903 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
5904 // If the input is a FP value and we want it in FP registers, do a
5905 // bitcast to the corresponding integer type. This turns an f64 value
5906 // into i64, which can be passed with two i32 values on a 32-bit
5907 // machine.
Patrik Hagglund8963fec2012-12-19 12:23:01 +00005908 RegVT = MVT::getIntegerVT(OpInfo.ConstraintVT.getSizeInBits());
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005909 OpInfo.CallOperand = DAG.getNode(ISD::BITCAST, DL,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005910 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005911 OpInfo.ConstraintVT = RegVT;
5912 }
5913 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005914
Owen Anderson23b9b192009-08-12 00:36:31 +00005915 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00005916 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005917
Patrik Hagglund8963fec2012-12-19 12:23:01 +00005918 MVT RegVT;
Owen Andersone50ed302009-08-10 22:56:29 +00005919 EVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005920
5921 // If this is a constraint for a specific physical register, like {r17},
5922 // assign it now.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005923 if (unsigned AssignedReg = PhysReg.first) {
5924 const TargetRegisterClass *RC = PhysReg.second;
Owen Anderson825b72b2009-08-11 20:47:22 +00005925 if (OpInfo.ConstraintVT == MVT::Other)
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005926 ValueVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005927
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005928 // Get the actual register value type. This is important, because the user
5929 // may have asked for (e.g.) the AX register in i32 type. We need to
5930 // remember that AX is actually i16 to get the right extension.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005931 RegVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005932
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005933 // This is a explicit reference to a physical register.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005934 Regs.push_back(AssignedReg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005935
5936 // If this is an expanded reference, add the rest of the regs to Regs.
5937 if (NumRegs != 1) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005938 TargetRegisterClass::iterator I = RC->begin();
5939 for (; *I != AssignedReg; ++I)
5940 assert(I != RC->end() && "Didn't find reg!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005941
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005942 // Already added the first reg.
5943 --NumRegs; ++I;
5944 for (; NumRegs; --NumRegs, ++I) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005945 assert(I != RC->end() && "Ran out of registers to allocate!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005946 Regs.push_back(*I);
5947 }
5948 }
Bill Wendling651ad132009-12-22 01:25:10 +00005949
Dan Gohman7451d3e2010-05-29 17:03:36 +00005950 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005951 return;
5952 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005953
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005954 // Otherwise, if this was a reference to an LLVM register class, create vregs
5955 // for this reference.
Chris Lattnerb3b44842009-03-24 15:25:07 +00005956 if (const TargetRegisterClass *RC = PhysReg.second) {
5957 RegVT = *RC->vt_begin();
Owen Anderson825b72b2009-08-11 20:47:22 +00005958 if (OpInfo.ConstraintVT == MVT::Other)
Evan Chengfb112882009-03-23 08:01:15 +00005959 ValueVT = RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005960
Evan Chengfb112882009-03-23 08:01:15 +00005961 // Create the appropriate number of virtual registers.
5962 MachineRegisterInfo &RegInfo = MF.getRegInfo();
5963 for (; NumRegs; --NumRegs)
Chris Lattnerb3b44842009-03-24 15:25:07 +00005964 Regs.push_back(RegInfo.createVirtualRegister(RC));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005965
Dan Gohman7451d3e2010-05-29 17:03:36 +00005966 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
Evan Chengfb112882009-03-23 08:01:15 +00005967 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005968 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005969
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005970 // Otherwise, we couldn't allocate enough registers for this.
5971}
5972
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005973/// visitInlineAsm - Handle a call to an InlineAsm object.
5974///
Dan Gohman46510a72010-04-15 01:51:59 +00005975void SelectionDAGBuilder::visitInlineAsm(ImmutableCallSite CS) {
5976 const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005977
5978 /// ConstraintOperands - Information about all of the constraints.
John Thompson44ab89e2010-10-29 17:29:13 +00005979 SDISelAsmOperandInfoVector ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005980
Bill Wendlingba54bca2013-06-19 21:36:55 +00005981 const TargetLowering *TLI = TM.getTargetLowering();
Evan Chengce1cdac2011-05-06 20:52:23 +00005982 TargetLowering::AsmOperandInfoVector
Bill Wendlingba54bca2013-06-19 21:36:55 +00005983 TargetConstraints = TLI->ParseConstraints(CS);
Evan Chengce1cdac2011-05-06 20:52:23 +00005984
John Thompsoneac6e1d2010-09-13 18:15:37 +00005985 bool hasMemory = false;
Michael J. Spencere70c5262010-10-16 08:25:21 +00005986
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005987 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
5988 unsigned ResNo = 0; // ResNo - The result number of the next output.
John Thompsoneac6e1d2010-09-13 18:15:37 +00005989 for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) {
5990 ConstraintOperands.push_back(SDISelAsmOperandInfo(TargetConstraints[i]));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005991 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Michael J. Spencere70c5262010-10-16 08:25:21 +00005992
Patrik Hagglundc698d3a2012-12-19 15:19:11 +00005993 MVT OpVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005994
5995 // Compute the value type for each operand.
5996 switch (OpInfo.Type) {
5997 case InlineAsm::isOutput:
5998 // Indirect outputs just consume an argument.
5999 if (OpInfo.isIndirect) {
Dan Gohman46510a72010-04-15 01:51:59 +00006000 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006001 break;
6002 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006003
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006004 // The return value of the call is this value. As such, there is no
6005 // corresponding argument.
Nick Lewycky8de34002011-09-30 22:19:53 +00006006 assert(!CS.getType()->isVoidTy() && "Bad inline asm!");
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006007 if (StructType *STy = dyn_cast<StructType>(CS.getType())) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00006008 OpVT = TLI->getSimpleValueType(STy->getElementType(ResNo));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006009 } else {
6010 assert(ResNo == 0 && "Asm only has one result!");
Bill Wendlingba54bca2013-06-19 21:36:55 +00006011 OpVT = TLI->getSimpleValueType(CS.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006012 }
6013 ++ResNo;
6014 break;
6015 case InlineAsm::isInput:
Dan Gohman46510a72010-04-15 01:51:59 +00006016 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006017 break;
6018 case InlineAsm::isClobber:
6019 // Nothing to do.
6020 break;
6021 }
6022
6023 // If this is an input or an indirect output, process the call argument.
6024 // BasicBlocks are labels, currently appearing only in asm's.
6025 if (OpInfo.CallOperandVal) {
Dan Gohman46510a72010-04-15 01:51:59 +00006026 if (const BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006027 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00006028 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006029 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006030 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006031
Bill Wendlingba54bca2013-06-19 21:36:55 +00006032 OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), *TLI, TD).
Patrik Hagglundc698d3a2012-12-19 15:19:11 +00006033 getSimpleVT();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006034 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006035
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006036 OpInfo.ConstraintVT = OpVT;
Michael J. Spencere70c5262010-10-16 08:25:21 +00006037
John Thompsoneac6e1d2010-09-13 18:15:37 +00006038 // Indirect operand accesses access memory.
6039 if (OpInfo.isIndirect)
6040 hasMemory = true;
6041 else {
6042 for (unsigned j = 0, ee = OpInfo.Codes.size(); j != ee; ++j) {
Evan Chengce1cdac2011-05-06 20:52:23 +00006043 TargetLowering::ConstraintType
Bill Wendlingba54bca2013-06-19 21:36:55 +00006044 CType = TLI->getConstraintType(OpInfo.Codes[j]);
John Thompsoneac6e1d2010-09-13 18:15:37 +00006045 if (CType == TargetLowering::C_Memory) {
6046 hasMemory = true;
6047 break;
6048 }
6049 }
6050 }
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006051 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006052
John Thompsoneac6e1d2010-09-13 18:15:37 +00006053 SDValue Chain, Flag;
6054
6055 // We won't need to flush pending loads if this asm doesn't touch
6056 // memory and is nonvolatile.
6057 if (hasMemory || IA->hasSideEffects())
6058 Chain = getRoot();
6059 else
6060 Chain = DAG.getRoot();
6061
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006062 // Second pass over the constraints: compute which constraint option to use
6063 // and assign registers to constraints that want a specific physreg.
John Thompsoneac6e1d2010-09-13 18:15:37 +00006064 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006065 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006066
John Thompson54584742010-09-24 22:24:05 +00006067 // If this is an output operand with a matching input operand, look up the
6068 // matching input. If their types mismatch, e.g. one is an integer, the
6069 // other is floating point, or their sizes are different, flag it as an
6070 // error.
6071 if (OpInfo.hasMatchingInput()) {
6072 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
Michael J. Spencere70c5262010-10-16 08:25:21 +00006073
John Thompson54584742010-09-24 22:24:05 +00006074 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Bill Wendling96cb1122012-07-19 00:04:14 +00006075 std::pair<unsigned, const TargetRegisterClass*> MatchRC =
Bill Wendlingba54bca2013-06-19 21:36:55 +00006076 TLI->getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
6077 OpInfo.ConstraintVT);
Bill Wendling96cb1122012-07-19 00:04:14 +00006078 std::pair<unsigned, const TargetRegisterClass*> InputRC =
Bill Wendlingba54bca2013-06-19 21:36:55 +00006079 TLI->getRegForInlineAsmConstraint(Input.ConstraintCode,
6080 Input.ConstraintVT);
John Thompson54584742010-09-24 22:24:05 +00006081 if ((OpInfo.ConstraintVT.isInteger() !=
6082 Input.ConstraintVT.isInteger()) ||
Eric Christopher5427ede2011-07-14 20:13:52 +00006083 (MatchRC.second != InputRC.second)) {
John Thompson54584742010-09-24 22:24:05 +00006084 report_fatal_error("Unsupported asm: input constraint"
6085 " with a matching output constraint of"
6086 " incompatible type!");
6087 }
6088 Input.ConstraintVT = OpInfo.ConstraintVT;
6089 }
6090 }
6091
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006092 // Compute the constraint code and ConstraintType to use.
Bill Wendlingba54bca2013-06-19 21:36:55 +00006093 TLI->ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006094
Eric Christopherfffe3632013-01-11 18:12:39 +00006095 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
6096 OpInfo.Type == InlineAsm::isClobber)
6097 continue;
6098
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006099 // If this is a memory input, and if the operand is not indirect, do what we
6100 // need to to provide an address for the memory input.
6101 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
6102 !OpInfo.isIndirect) {
Evan Chengce1cdac2011-05-06 20:52:23 +00006103 assert((OpInfo.isMultipleAlternative ||
6104 (OpInfo.Type == InlineAsm::isInput)) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006105 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006106
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006107 // Memory operands really want the address of the value. If we don't have
6108 // an indirect input, put it in the constpool if we can, otherwise spill
6109 // it to a stack slot.
Eric Christophere0b42c02011-06-03 17:21:23 +00006110 // TODO: This isn't quite right. We need to handle these according to
6111 // the addressing mode that the constraint wants. Also, this may take
6112 // an additional register for the computation and we don't want that
6113 // either.
Eric Christopher471e4222011-06-08 23:55:35 +00006114
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006115 // If the operand is a float, integer, or vector constant, spill to a
6116 // constant pool entry to get its address.
Dan Gohman46510a72010-04-15 01:51:59 +00006117 const Value *OpVal = OpInfo.CallOperandVal;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006118 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
Chris Lattnera78fa8c2012-01-27 03:08:05 +00006119 isa<ConstantVector>(OpVal) || isa<ConstantDataVector>(OpVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006120 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
Bill Wendlingba54bca2013-06-19 21:36:55 +00006121 TLI->getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006122 } else {
6123 // Otherwise, create a stack slot and emit a store to it before the
6124 // asm.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006125 Type *Ty = OpVal->getType();
Bill Wendlingba54bca2013-06-19 21:36:55 +00006126 uint64_t TySize = TLI->getDataLayout()->getTypeAllocSize(Ty);
6127 unsigned Align = TLI->getDataLayout()->getPrefTypeAlignment(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006128 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006129 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Bill Wendlingba54bca2013-06-19 21:36:55 +00006130 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI->getPointerTy());
Andrew Trickac6d9be2013-05-25 02:42:55 +00006131 Chain = DAG.getStore(Chain, getCurSDLoc(),
Chris Lattnerecf42c42010-09-21 16:36:31 +00006132 OpInfo.CallOperand, StackSlot,
6133 MachinePointerInfo::getFixedStack(SSFI),
David Greene1e559442010-02-15 17:00:31 +00006134 false, false, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006135 OpInfo.CallOperand = StackSlot;
6136 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006137
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006138 // There is no longer a Value* corresponding to this operand.
6139 OpInfo.CallOperandVal = 0;
Bill Wendling651ad132009-12-22 01:25:10 +00006140
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006141 // It is now an indirect operand.
6142 OpInfo.isIndirect = true;
6143 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006144
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006145 // If this constraint is for a specific register, allocate it before
6146 // anything else.
6147 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Bill Wendlingba54bca2013-06-19 21:36:55 +00006148 GetRegistersForValue(DAG, *TLI, getCurSDLoc(), OpInfo);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006149 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006150
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006151 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00006152 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006153 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
6154 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006155
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006156 // C_Register operands have already been allocated, Other/Memory don't need
6157 // to be.
6158 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Bill Wendlingba54bca2013-06-19 21:36:55 +00006159 GetRegistersForValue(DAG, *TLI, getCurSDLoc(), OpInfo);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006160 }
6161
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006162 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
6163 std::vector<SDValue> AsmNodeOperands;
6164 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
6165 AsmNodeOperands.push_back(
Dan Gohmanf2d7fb32010-01-04 21:00:54 +00006166 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00006167 TLI->getPointerTy()));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006168
Chris Lattnerdecc2672010-04-07 05:20:54 +00006169 // If we have a !srcloc metadata node associated with it, we want to attach
6170 // this to the ultimately generated inline asm machineinstr. To do this, we
6171 // pass in the third operand as this (potentially null) inline asm MDNode.
6172 const MDNode *SrcLoc = CS.getInstruction()->getMetadata("srcloc");
6173 AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006174
Chad Rosier3d716882012-10-30 19:11:54 +00006175 // Remember the HasSideEffect, AlignStack, AsmDialect, MayLoad and MayStore
6176 // bits as operand 3.
Evan Chengc36b7062011-01-07 23:50:32 +00006177 unsigned ExtraInfo = 0;
6178 if (IA->hasSideEffects())
6179 ExtraInfo |= InlineAsm::Extra_HasSideEffects;
6180 if (IA->isAlignStack())
6181 ExtraInfo |= InlineAsm::Extra_IsAlignStack;
Chad Rosier77fffa62012-09-05 22:17:43 +00006182 // Set the asm dialect.
Chad Rosier2f1d8152012-09-05 22:40:13 +00006183 ExtraInfo |= IA->getDialect() * InlineAsm::Extra_AsmDialect;
Chad Rosier3d716882012-10-30 19:11:54 +00006184
6185 // Determine if this InlineAsm MayLoad or MayStore based on the constraints.
6186 for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) {
6187 TargetLowering::AsmOperandInfo &OpInfo = TargetConstraints[i];
6188
6189 // Compute the constraint code and ConstraintType to use.
Bill Wendlingba54bca2013-06-19 21:36:55 +00006190 TLI->ComputeConstraintToUse(OpInfo, SDValue());
Chad Rosier3d716882012-10-30 19:11:54 +00006191
Chad Rosierdfa4cec2012-10-30 20:01:12 +00006192 // Ideally, we would only check against memory constraints. However, the
6193 // meaning of an other constraint can be target-specific and we can't easily
6194 // reason about it. Therefore, be conservative and set MayLoad/MayStore
6195 // for other constriants as well.
Chad Rosier3d716882012-10-30 19:11:54 +00006196 if (OpInfo.ConstraintType == TargetLowering::C_Memory ||
6197 OpInfo.ConstraintType == TargetLowering::C_Other) {
6198 if (OpInfo.Type == InlineAsm::isInput)
6199 ExtraInfo |= InlineAsm::Extra_MayLoad;
6200 else if (OpInfo.Type == InlineAsm::isOutput)
6201 ExtraInfo |= InlineAsm::Extra_MayStore;
Eric Christopherfffe3632013-01-11 18:12:39 +00006202 else if (OpInfo.Type == InlineAsm::isClobber)
6203 ExtraInfo |= (InlineAsm::Extra_MayLoad | InlineAsm::Extra_MayStore);
Chad Rosier3d716882012-10-30 19:11:54 +00006204 }
6205 }
6206
Evan Chengc36b7062011-01-07 23:50:32 +00006207 AsmNodeOperands.push_back(DAG.getTargetConstant(ExtraInfo,
Bill Wendlingba54bca2013-06-19 21:36:55 +00006208 TLI->getPointerTy()));
Dale Johannesenf1e309e2010-07-02 20:16:09 +00006209
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006210 // Loop over all of the inputs, copying the operand values into the
6211 // appropriate registers and processing the output regs.
6212 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006213
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006214 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
6215 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006216
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006217 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
6218 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
6219
6220 switch (OpInfo.Type) {
6221 case InlineAsm::isOutput: {
6222 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
6223 OpInfo.ConstraintType != TargetLowering::C_Register) {
6224 // Memory output, or 'other' output (e.g. 'X' constraint).
6225 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
6226
6227 // Add information to the INLINEASM node to know about this output.
Chris Lattnerdecc2672010-04-07 05:20:54 +00006228 unsigned OpFlags = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
6229 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags,
Bill Wendlingba54bca2013-06-19 21:36:55 +00006230 TLI->getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006231 AsmNodeOperands.push_back(OpInfo.CallOperand);
6232 break;
6233 }
6234
6235 // Otherwise, this is a register or register class output.
6236
6237 // Copy the output from the appropriate register. Find a register that
6238 // we can use.
Chris Lattnerfcd70902012-01-03 23:51:01 +00006239 if (OpInfo.AssignedRegs.Regs.empty()) {
6240 LLVMContext &Ctx = *DAG.getContext();
Stephen Lin155615d2013-07-08 00:37:03 +00006241 Ctx.emitError(CS.getInstruction(),
Chris Lattnerfcd70902012-01-03 23:51:01 +00006242 "couldn't allocate output register for constraint '" +
Eric Christopher1a54c572013-07-31 01:26:24 +00006243 Twine(OpInfo.ConstraintCode) + "'");
6244 return;
Chris Lattnerfcd70902012-01-03 23:51:01 +00006245 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006246
6247 // If this is an indirect operand, store through the pointer after the
6248 // asm.
6249 if (OpInfo.isIndirect) {
6250 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
6251 OpInfo.CallOperandVal));
6252 } else {
6253 // This is the result value of the call.
Benjamin Kramerf0127052010-01-05 13:12:22 +00006254 assert(!CS.getType()->isVoidTy() && "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006255 // Concatenate this output onto the outputs list.
6256 RetValRegs.append(OpInfo.AssignedRegs);
6257 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006258
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006259 // Add information to the INLINEASM node to know that this register is
6260 // set.
Eric Christopherb0bee812013-07-30 22:50:44 +00006261 OpInfo.AssignedRegs
6262 .AddInlineAsmOperands(OpInfo.isEarlyClobber
6263 ? InlineAsm::Kind_RegDefEarlyClobber
6264 : InlineAsm::Kind_RegDef,
6265 false, 0, DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006266 break;
6267 }
6268 case InlineAsm::isInput: {
6269 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006270
Chris Lattner6bdcda32008-10-17 16:47:46 +00006271 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006272 // If this is required to match an output register we have already set,
6273 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00006274 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006275
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006276 // Scan until we find the definition we already emitted of this operand.
6277 // When we find it, create a RegsForValue operand.
Chris Lattnerdecc2672010-04-07 05:20:54 +00006278 unsigned CurOp = InlineAsm::Op_FirstOperand;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006279 for (; OperandNo; --OperandNo) {
6280 // Advance to the next operand.
Evan Cheng697cbbf2009-03-20 18:03:34 +00006281 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006282 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00006283 assert((InlineAsm::isRegDefKind(OpFlag) ||
6284 InlineAsm::isRegDefEarlyClobberKind(OpFlag) ||
6285 InlineAsm::isMemKind(OpFlag)) && "Skipped past definitions?");
Evan Cheng697cbbf2009-03-20 18:03:34 +00006286 CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006287 }
6288
Evan Cheng697cbbf2009-03-20 18:03:34 +00006289 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006290 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00006291 if (InlineAsm::isRegDefKind(OpFlag) ||
6292 InlineAsm::isRegDefEarlyClobberKind(OpFlag)) {
Evan Cheng697cbbf2009-03-20 18:03:34 +00006293 // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
Chris Lattner6129c372010-04-08 00:09:16 +00006294 if (OpInfo.isIndirect) {
6295 // This happens on gcc/testsuite/gcc.dg/pr8788-1.c
Dan Gohman99be8ae2010-04-19 22:41:47 +00006296 LLVMContext &Ctx = *DAG.getContext();
Eric Christopher1a54c572013-07-31 01:26:24 +00006297 Ctx.emitError(CS.getInstruction(), "inline asm not supported yet:"
6298 " don't know how to handle tied "
6299 "indirect register inputs");
6300 return;
Chris Lattner6129c372010-04-08 00:09:16 +00006301 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00006302
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006303 RegsForValue MatchedRegs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006304 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00006305 MVT RegVT = AsmNodeOperands[CurOp+1].getSimpleValueType();
Evan Chengfb112882009-03-23 08:01:15 +00006306 MatchedRegs.RegVTs.push_back(RegVT);
6307 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
Evan Cheng697cbbf2009-03-20 18:03:34 +00006308 for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag);
Chad Rosier2871ba92013-04-24 22:53:10 +00006309 i != e; ++i) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00006310 if (const TargetRegisterClass *RC = TLI->getRegClassFor(RegVT))
Chad Rosier2871ba92013-04-24 22:53:10 +00006311 MatchedRegs.Regs.push_back(RegInfo.createVirtualRegister(RC));
6312 else {
6313 LLVMContext &Ctx = *DAG.getContext();
Eric Christopher1a54c572013-07-31 01:26:24 +00006314 Ctx.emitError(CS.getInstruction(),
6315 "inline asm error: This value"
Chad Rosier2871ba92013-04-24 22:53:10 +00006316 " type register class is not natively supported!");
Eric Christopher1a54c572013-07-31 01:26:24 +00006317 return;
Chad Rosier2871ba92013-04-24 22:53:10 +00006318 }
6319 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006320 // Use the produced MatchedRegs object to
Andrew Trickac6d9be2013-05-25 02:42:55 +00006321 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurSDLoc(),
Bill Wendlingf18eb582012-09-26 06:16:18 +00006322 Chain, &Flag, CS.getInstruction());
Chris Lattnerdecc2672010-04-07 05:20:54 +00006323 MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse,
Evan Chengfb112882009-03-23 08:01:15 +00006324 true, OpInfo.getMatchedOperand(),
Bill Wendling46ada192010-03-02 01:55:18 +00006325 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006326 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006327 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00006328
Chris Lattnerdecc2672010-04-07 05:20:54 +00006329 assert(InlineAsm::isMemKind(OpFlag) && "Unknown matching constraint!");
6330 assert(InlineAsm::getNumOperandRegisters(OpFlag) == 1 &&
6331 "Unexpected number of operands");
6332 // Add information to the INLINEASM node to know about this input.
6333 // See InlineAsm.h isUseOperandTiedToDef.
6334 OpFlag = InlineAsm::getFlagWordForMatchingOp(OpFlag,
6335 OpInfo.getMatchedOperand());
6336 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag,
Bill Wendlingba54bca2013-06-19 21:36:55 +00006337 TLI->getPointerTy()));
Chris Lattnerdecc2672010-04-07 05:20:54 +00006338 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
6339 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006340 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006341
Dale Johannesenb5611a62010-07-13 20:17:05 +00006342 // Treat indirect 'X' constraint as memory.
Michael J. Spencere70c5262010-10-16 08:25:21 +00006343 if (OpInfo.ConstraintType == TargetLowering::C_Other &&
6344 OpInfo.isIndirect)
Dale Johannesenb5611a62010-07-13 20:17:05 +00006345 OpInfo.ConstraintType = TargetLowering::C_Memory;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006346
Dale Johannesenb5611a62010-07-13 20:17:05 +00006347 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006348 std::vector<SDValue> Ops;
Bill Wendlingba54bca2013-06-19 21:36:55 +00006349 TLI->LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode,
6350 Ops, DAG);
Chris Lattnerfcd70902012-01-03 23:51:01 +00006351 if (Ops.empty()) {
6352 LLVMContext &Ctx = *DAG.getContext();
6353 Ctx.emitError(CS.getInstruction(),
6354 "invalid operand for inline asm constraint '" +
Eric Christopher1a54c572013-07-31 01:26:24 +00006355 Twine(OpInfo.ConstraintCode) + "'");
6356 return;
Chris Lattnerfcd70902012-01-03 23:51:01 +00006357 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006358
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006359 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00006360 unsigned ResOpType =
6361 InlineAsm::getFlagWord(InlineAsm::Kind_Imm, Ops.size());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006362 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Bill Wendlingba54bca2013-06-19 21:36:55 +00006363 TLI->getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006364 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
6365 break;
Chris Lattnerdecc2672010-04-07 05:20:54 +00006366 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00006367
Chris Lattnerdecc2672010-04-07 05:20:54 +00006368 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006369 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
Bill Wendlingba54bca2013-06-19 21:36:55 +00006370 assert(InOperandVal.getValueType() == TLI->getPointerTy() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006371 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006372
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006373 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00006374 unsigned ResOpType = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
Dale Johannesen86b49f82008-09-24 01:07:17 +00006375 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Bill Wendlingba54bca2013-06-19 21:36:55 +00006376 TLI->getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006377 AsmNodeOperands.push_back(InOperandVal);
6378 break;
6379 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006380
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006381 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
6382 OpInfo.ConstraintType == TargetLowering::C_Register) &&
6383 "Unknown constraint type!");
Eric Christopher9eb4f8a2012-07-02 21:16:43 +00006384
6385 // TODO: Support this.
6386 if (OpInfo.isIndirect) {
6387 LLVMContext &Ctx = *DAG.getContext();
6388 Ctx.emitError(CS.getInstruction(),
6389 "Don't know how to handle indirect register inputs yet "
Eric Christopher1a54c572013-07-31 01:26:24 +00006390 "for constraint '" +
6391 Twine(OpInfo.ConstraintCode) + "'");
6392 return;
Eric Christopher9eb4f8a2012-07-02 21:16:43 +00006393 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006394
6395 // Copy the input into the appropriate registers.
Chris Lattnerfcd70902012-01-03 23:51:01 +00006396 if (OpInfo.AssignedRegs.Regs.empty()) {
6397 LLVMContext &Ctx = *DAG.getContext();
Stephen Lin155615d2013-07-08 00:37:03 +00006398 Ctx.emitError(CS.getInstruction(),
Chris Lattnerfcd70902012-01-03 23:51:01 +00006399 "couldn't allocate input reg for constraint '" +
Eric Christopher1a54c572013-07-31 01:26:24 +00006400 Twine(OpInfo.ConstraintCode) + "'");
6401 return;
Chris Lattnerfcd70902012-01-03 23:51:01 +00006402 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006403
Andrew Trickac6d9be2013-05-25 02:42:55 +00006404 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurSDLoc(),
Bill Wendlingf18eb582012-09-26 06:16:18 +00006405 Chain, &Flag, CS.getInstruction());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006406
Chris Lattnerdecc2672010-04-07 05:20:54 +00006407 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse, false, 0,
Bill Wendling46ada192010-03-02 01:55:18 +00006408 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006409 break;
6410 }
6411 case InlineAsm::isClobber: {
6412 // Add the clobbered value to the operand list, so that the register
6413 // allocator is aware that the physreg got clobbered.
6414 if (!OpInfo.AssignedRegs.Regs.empty())
Jakob Stoklund Olesenf792fa92011-06-27 04:08:33 +00006415 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_Clobber,
Bill Wendling46ada192010-03-02 01:55:18 +00006416 false, 0, DAG,
Bill Wendling651ad132009-12-22 01:25:10 +00006417 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006418 break;
6419 }
6420 }
6421 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006422
Chris Lattnerdecc2672010-04-07 05:20:54 +00006423 // Finish up input operands. Set the input chain and add the flag last.
Dale Johannesenf1e309e2010-07-02 20:16:09 +00006424 AsmNodeOperands[InlineAsm::Op_InputChain] = Chain;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006425 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006426
Andrew Trickac6d9be2013-05-25 02:42:55 +00006427 Chain = DAG.getNode(ISD::INLINEASM, getCurSDLoc(),
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006428 DAG.getVTList(MVT::Other, MVT::Glue),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006429 &AsmNodeOperands[0], AsmNodeOperands.size());
6430 Flag = Chain.getValue(1);
6431
6432 // If this asm returns a register value, copy the result from that register
6433 // and set it as the value of the call.
6434 if (!RetValRegs.Regs.empty()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006435 SDValue Val = RetValRegs.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(),
Bill Wendling12931302012-09-26 04:04:19 +00006436 Chain, &Flag, CS.getInstruction());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006437
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006438 // FIXME: Why don't we do this for inline asms with MRVs?
6439 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00006440 EVT ResultType = TLI->getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006441
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006442 // If any of the results of the inline asm is a vector, it may have the
6443 // wrong width/num elts. This can happen for register classes that can
6444 // contain multiple different value types. The preg or vreg allocated may
6445 // not have the same VT as was expected. Convert it to the right type
6446 // with bit_convert.
6447 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006448 Val = DAG.getNode(ISD::BITCAST, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00006449 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00006450
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006451 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006452 ResultType.isInteger() && Val.getValueType().isInteger()) {
6453 // If a result value was tied to an input value, the computed result may
6454 // have a wider width than the expected result. Extract the relevant
6455 // portion.
Andrew Trickac6d9be2013-05-25 02:42:55 +00006456 Val = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00006457 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006458
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006459 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Chris Lattner0c526442008-10-17 17:52:49 +00006460 }
Dan Gohman95915732008-10-18 01:03:45 +00006461
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006462 setValue(CS.getInstruction(), Val);
Dale Johannesenec65a7d2009-04-14 00:56:56 +00006463 // Don't need to use this as a chain in this case.
6464 if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty())
6465 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006466 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006467
Dan Gohman46510a72010-04-15 01:51:59 +00006468 std::vector<std::pair<SDValue, const Value *> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006469
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006470 // Process indirect outputs, first output all of the flagged copies out of
6471 // physregs.
6472 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
6473 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
Dan Gohman46510a72010-04-15 01:51:59 +00006474 const Value *Ptr = IndirectStoresToEmit[i].second;
Andrew Trickac6d9be2013-05-25 02:42:55 +00006475 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(),
Bill Wendling12931302012-09-26 04:04:19 +00006476 Chain, &Flag, IA);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006477 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
6478 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006479
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006480 // Emit the non-flagged stores from the physregs.
6481 SmallVector<SDValue, 8> OutChains;
Bill Wendling651ad132009-12-22 01:25:10 +00006482 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006483 SDValue Val = DAG.getStore(Chain, getCurSDLoc(),
Bill Wendling651ad132009-12-22 01:25:10 +00006484 StoresToEmit[i].first,
6485 getValue(StoresToEmit[i].second),
Chris Lattner84bd98a2010-09-21 18:58:22 +00006486 MachinePointerInfo(StoresToEmit[i].second),
David Greene1e559442010-02-15 17:00:31 +00006487 false, false, 0);
Bill Wendling651ad132009-12-22 01:25:10 +00006488 OutChains.push_back(Val);
Bill Wendling651ad132009-12-22 01:25:10 +00006489 }
6490
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006491 if (!OutChains.empty())
Andrew Trickac6d9be2013-05-25 02:42:55 +00006492 Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006493 &OutChains[0], OutChains.size());
Bill Wendling651ad132009-12-22 01:25:10 +00006494
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006495 DAG.setRoot(Chain);
6496}
6497
Dan Gohman46510a72010-04-15 01:51:59 +00006498void SelectionDAGBuilder::visitVAStart(const CallInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006499 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurSDLoc(),
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006500 MVT::Other, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00006501 getValue(I.getArgOperand(0)),
6502 DAG.getSrcValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006503}
6504
Dan Gohman46510a72010-04-15 01:51:59 +00006505void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00006506 const TargetLowering *TLI = TM.getTargetLowering();
6507 const DataLayout &TD = *TLI->getDataLayout();
6508 SDValue V = DAG.getVAArg(TLI->getValueType(I.getType()), getCurSDLoc(),
Dale Johannesena04b7572009-02-03 23:04:43 +00006509 getRoot(), getValue(I.getOperand(0)),
Rafael Espindolacbeeae22010-07-11 04:01:49 +00006510 DAG.getSrcValue(I.getOperand(0)),
Rafael Espindola9d544d02010-07-12 18:11:17 +00006511 TD.getABITypeAlignment(I.getType()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006512 setValue(&I, V);
6513 DAG.setRoot(V.getValue(1));
6514}
6515
Dan Gohman46510a72010-04-15 01:51:59 +00006516void SelectionDAGBuilder::visitVAEnd(const CallInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006517 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurSDLoc(),
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006518 MVT::Other, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00006519 getValue(I.getArgOperand(0)),
6520 DAG.getSrcValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006521}
6522
Dan Gohman46510a72010-04-15 01:51:59 +00006523void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006524 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurSDLoc(),
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006525 MVT::Other, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00006526 getValue(I.getArgOperand(0)),
6527 getValue(I.getArgOperand(1)),
6528 DAG.getSrcValue(I.getArgOperand(0)),
6529 DAG.getSrcValue(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006530}
6531
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006532/// TargetLowering::LowerCallTo - This is the default LowerCallTo
Dan Gohman98ca4f22009-08-05 01:29:28 +00006533/// implementation, which just calls LowerCall.
6534/// FIXME: When all targets are
6535/// migrated to using LowerCall, this hook should be integrated into SDISel.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006536std::pair<SDValue, SDValue>
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006537TargetLowering::LowerCallTo(TargetLowering::CallLoweringInfo &CLI) const {
Stephen Lin3484da92013-04-30 22:49:28 +00006538 // Handle the incoming return values from the call.
6539 CLI.Ins.clear();
6540 SmallVector<EVT, 4> RetTys;
6541 ComputeValueVTs(*this, CLI.RetTy, RetTys);
6542 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
6543 EVT VT = RetTys[I];
6544 MVT RegisterVT = getRegisterType(CLI.RetTy->getContext(), VT);
6545 unsigned NumRegs = getNumRegisters(CLI.RetTy->getContext(), VT);
6546 for (unsigned i = 0; i != NumRegs; ++i) {
6547 ISD::InputArg MyFlags;
6548 MyFlags.VT = RegisterVT;
6549 MyFlags.Used = CLI.IsReturnValueUsed;
6550 if (CLI.RetSExt)
6551 MyFlags.Flags.setSExt();
6552 if (CLI.RetZExt)
6553 MyFlags.Flags.setZExt();
6554 if (CLI.IsInReg)
6555 MyFlags.Flags.setInReg();
6556 CLI.Ins.push_back(MyFlags);
6557 }
6558 }
6559
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006560 // Handle all of the outgoing arguments.
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006561 CLI.Outs.clear();
6562 CLI.OutVals.clear();
6563 ArgListTy &Args = CLI.Args;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006564 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00006565 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006566 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
6567 for (unsigned Value = 0, NumValues = ValueVTs.size();
6568 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006569 EVT VT = ValueVTs[Value];
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006570 Type *ArgTy = VT.getTypeForEVT(CLI.RetTy->getContext());
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006571 SDValue Op = SDValue(Args[i].Node.getNode(),
6572 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006573 ISD::ArgFlagsTy Flags;
6574 unsigned OriginalAlignment =
Micah Villmow3574eca2012-10-08 16:38:25 +00006575 getDataLayout()->getABITypeAlignment(ArgTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006576
6577 if (Args[i].isZExt)
6578 Flags.setZExt();
6579 if (Args[i].isSExt)
6580 Flags.setSExt();
6581 if (Args[i].isInReg)
6582 Flags.setInReg();
6583 if (Args[i].isSRet)
6584 Flags.setSRet();
6585 if (Args[i].isByVal) {
6586 Flags.setByVal();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006587 PointerType *Ty = cast<PointerType>(Args[i].Ty);
6588 Type *ElementTy = Ty->getElementType();
Micah Villmow3574eca2012-10-08 16:38:25 +00006589 Flags.setByValSize(getDataLayout()->getTypeAllocSize(ElementTy));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006590 // For ByVal, alignment should come from FE. BE will guess if this
6591 // info is not there but there are cases it cannot get right.
Chris Lattner9db20f32011-05-22 23:23:02 +00006592 unsigned FrameAlign;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006593 if (Args[i].Alignment)
6594 FrameAlign = Args[i].Alignment;
Chris Lattner9db20f32011-05-22 23:23:02 +00006595 else
6596 FrameAlign = getByValTypeAlignment(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006597 Flags.setByValAlign(FrameAlign);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006598 }
6599 if (Args[i].isNest)
6600 Flags.setNest();
6601 Flags.setOrigAlign(OriginalAlignment);
6602
Patrik Hagglunddfcf33a2012-12-19 11:48:16 +00006603 MVT PartVT = getRegisterType(CLI.RetTy->getContext(), VT);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006604 unsigned NumParts = getNumRegisters(CLI.RetTy->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006605 SmallVector<SDValue, 4> Parts(NumParts);
6606 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
6607
6608 if (Args[i].isSExt)
6609 ExtendKind = ISD::SIGN_EXTEND;
6610 else if (Args[i].isZExt)
6611 ExtendKind = ISD::ZERO_EXTEND;
6612
Stephen Lin3484da92013-04-30 22:49:28 +00006613 // Conservatively only handle 'returned' on non-vectors for now
6614 if (Args[i].isReturned && !Op.getValueType().isVector()) {
6615 assert(CLI.RetTy == Args[i].Ty && RetTys.size() == NumValues &&
6616 "unexpected use of 'returned'");
6617 // Before passing 'returned' to the target lowering code, ensure that
6618 // either the register MVT and the actual EVT are the same size or that
6619 // the return value and argument are extended in the same way; in these
6620 // cases it's safe to pass the argument register value unchanged as the
6621 // return register value (although it's at the target's option whether
6622 // to do so)
6623 // TODO: allow code generation to take advantage of partially preserved
6624 // registers rather than clobbering the entire register when the
6625 // parameter extension method is not compatible with the return
6626 // extension method
6627 if ((NumParts * PartVT.getSizeInBits() == VT.getSizeInBits()) ||
6628 (ExtendKind != ISD::ANY_EXTEND &&
6629 CLI.RetSExt == Args[i].isSExt && CLI.RetZExt == Args[i].isZExt))
6630 Flags.setReturned();
6631 }
6632
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006633 getCopyToParts(CLI.DAG, CLI.DL, Op, &Parts[0], NumParts,
Bill Wendlingf18eb582012-09-26 06:16:18 +00006634 PartVT, CLI.CS ? CLI.CS->getInstruction() : 0, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006635
Dan Gohman98ca4f22009-08-05 01:29:28 +00006636 for (unsigned j = 0; j != NumParts; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006637 // if it isn't first piece, alignment must be 1
Dan Gohmanc9403652010-07-07 15:54:55 +00006638 ISD::OutputArg MyFlags(Flags, Parts[j].getValueType(),
Manman Ren0a1544d2012-11-01 23:49:58 +00006639 i < CLI.NumFixedArgs,
6640 i, j*Parts[j].getValueType().getStoreSize());
Dan Gohman98ca4f22009-08-05 01:29:28 +00006641 if (NumParts > 1 && j == 0)
6642 MyFlags.Flags.setSplit();
6643 else if (j != 0)
6644 MyFlags.Flags.setOrigAlign(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006645
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006646 CLI.Outs.push_back(MyFlags);
6647 CLI.OutVals.push_back(Parts[j]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006648 }
6649 }
6650 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006651
Dan Gohman98ca4f22009-08-05 01:29:28 +00006652 SmallVector<SDValue, 4> InVals;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006653 CLI.Chain = LowerCall(CLI, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00006654
6655 // Verify that the target's LowerCall behaved as expected.
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006656 assert(CLI.Chain.getNode() && CLI.Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00006657 "LowerCall didn't return a valid chain!");
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006658 assert((!CLI.IsTailCall || InVals.empty()) &&
Dan Gohman5e866062009-08-06 15:37:27 +00006659 "LowerCall emitted a return value for a tail call!");
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006660 assert((CLI.IsTailCall || InVals.size() == CLI.Ins.size()) &&
Dan Gohman5e866062009-08-06 15:37:27 +00006661 "LowerCall didn't emit the correct number of values!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00006662
6663 // For a tail call, the return value is merely live-out and there aren't
6664 // any nodes in the DAG representing it. Return a special value to
6665 // indicate that a tail call has been emitted and no more Instructions
6666 // should be processed in the current block.
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006667 if (CLI.IsTailCall) {
6668 CLI.DAG.setRoot(CLI.Chain);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006669 return std::make_pair(SDValue(), SDValue());
6670 }
6671
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006672 DEBUG(for (unsigned i = 0, e = CLI.Ins.size(); i != e; ++i) {
Evan Chengaf1871f2010-03-11 19:38:18 +00006673 assert(InVals[i].getNode() &&
6674 "LowerCall emitted a null value!");
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006675 assert(EVT(CLI.Ins[i].VT) == InVals[i].getValueType() &&
Evan Chengaf1871f2010-03-11 19:38:18 +00006676 "LowerCall emitted a value with the wrong type!");
6677 });
6678
Dan Gohman98ca4f22009-08-05 01:29:28 +00006679 // Collect the legal value parts into potentially illegal values
6680 // that correspond to the original function's return values.
6681 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006682 if (CLI.RetSExt)
Dan Gohman98ca4f22009-08-05 01:29:28 +00006683 AssertOp = ISD::AssertSext;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006684 else if (CLI.RetZExt)
Dan Gohman98ca4f22009-08-05 01:29:28 +00006685 AssertOp = ISD::AssertZext;
6686 SmallVector<SDValue, 4> ReturnValues;
6687 unsigned CurReg = 0;
6688 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00006689 EVT VT = RetTys[I];
Patrik Hagglunddfcf33a2012-12-19 11:48:16 +00006690 MVT RegisterVT = getRegisterType(CLI.RetTy->getContext(), VT);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006691 unsigned NumRegs = getNumRegisters(CLI.RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006692
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006693 ReturnValues.push_back(getCopyFromParts(CLI.DAG, CLI.DL, &InVals[CurReg],
Bill Wendling12931302012-09-26 04:04:19 +00006694 NumRegs, RegisterVT, VT, NULL,
Bill Wendling4533cac2010-01-28 21:51:40 +00006695 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00006696 CurReg += NumRegs;
6697 }
6698
6699 // For a function returning void, there is no return value. We can't create
6700 // such a node, so we just return a null return value in that case. In
Chris Lattner7a2bdde2011-04-15 05:18:47 +00006701 // that case, nothing will actually look at the value.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006702 if (ReturnValues.empty())
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006703 return std::make_pair(SDValue(), CLI.Chain);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006704
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006705 SDValue Res = CLI.DAG.getNode(ISD::MERGE_VALUES, CLI.DL,
6706 CLI.DAG.getVTList(&RetTys[0], RetTys.size()),
Dan Gohman98ca4f22009-08-05 01:29:28 +00006707 &ReturnValues[0], ReturnValues.size());
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006708 return std::make_pair(Res, CLI.Chain);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006709}
6710
Duncan Sands9fbc7e22009-01-21 09:00:29 +00006711void TargetLowering::LowerOperationWrapper(SDNode *N,
6712 SmallVectorImpl<SDValue> &Results,
Dan Gohmand858e902010-04-17 15:26:15 +00006713 SelectionDAG &DAG) const {
Duncan Sands9fbc7e22009-01-21 09:00:29 +00006714 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00006715 if (Res.getNode())
6716 Results.push_back(Res);
6717}
6718
Dan Gohmand858e902010-04-17 15:26:15 +00006719SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Torok Edwinc23197a2009-07-14 16:55:14 +00006720 llvm_unreachable("LowerOperation not implemented for this target!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006721}
6722
Dan Gohman46510a72010-04-15 01:51:59 +00006723void
6724SelectionDAGBuilder::CopyValueToVirtualRegister(const Value *V, unsigned Reg) {
Dan Gohman28a17352010-07-01 01:59:43 +00006725 SDValue Op = getNonRegisterValue(V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006726 assert((Op.getOpcode() != ISD::CopyFromReg ||
6727 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
6728 "Copy from a reg to the same reg!");
6729 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
6730
Bill Wendlingba54bca2013-06-19 21:36:55 +00006731 const TargetLowering *TLI = TM.getTargetLowering();
6732 RegsForValue RFV(V->getContext(), *TLI, Reg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006733 SDValue Chain = DAG.getEntryNode();
Andrew Trickac6d9be2013-05-25 02:42:55 +00006734 RFV.getCopyToRegs(Op, DAG, getCurSDLoc(), Chain, 0, V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006735 PendingExports.push_back(Chain);
6736}
6737
6738#include "llvm/CodeGen/SelectionDAGISel.h"
6739
Eli Friedman23d32432011-05-05 16:53:34 +00006740/// isOnlyUsedInEntryBlock - If the specified argument is only used in the
6741/// entry block, return true. This includes arguments used by switches, since
6742/// the switch may expand into multiple basic blocks.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006743static bool isOnlyUsedInEntryBlock(const Argument *A, bool FastISel) {
Eli Friedman23d32432011-05-05 16:53:34 +00006744 // With FastISel active, we may be splitting blocks, so force creation
6745 // of virtual registers for all non-dead arguments.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006746 if (FastISel)
Eli Friedman23d32432011-05-05 16:53:34 +00006747 return A->use_empty();
6748
6749 const BasicBlock *Entry = A->getParent()->begin();
6750 for (Value::const_use_iterator UI = A->use_begin(), E = A->use_end();
6751 UI != E; ++UI) {
6752 const User *U = *UI;
6753 if (cast<Instruction>(U)->getParent() != Entry || isa<SwitchInst>(U))
6754 return false; // Use not in entry block.
6755 }
6756 return true;
6757}
6758
Eli Bendersky6437d382013-02-28 23:09:18 +00006759void SelectionDAGISel::LowerArguments(const Function &F) {
Dan Gohman2048b852009-11-23 18:04:58 +00006760 SelectionDAG &DAG = SDB->DAG;
Andrew Trickac6d9be2013-05-25 02:42:55 +00006761 SDLoc dl = SDB->getCurSDLoc();
Bill Wendlingba54bca2013-06-19 21:36:55 +00006762 const TargetLowering *TLI = getTargetLowering();
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006763 const DataLayout *TD = TLI->getDataLayout();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006764 SmallVector<ISD::InputArg, 16> Ins;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006765
Dan Gohman7451d3e2010-05-29 17:03:36 +00006766 if (!FuncInfo->CanLowerReturn) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006767 // Put in an sret pointer parameter before all the other parameters.
6768 SmallVector<EVT, 1> ValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00006769 ComputeValueVTs(*getTargetLowering(),
6770 PointerType::getUnqual(F.getReturnType()), ValueVTs);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006771
6772 // NOTE: Assuming that a pointer will never break down to more than one VT
6773 // or one register.
6774 ISD::ArgFlagsTy Flags;
6775 Flags.setSRet();
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006776 MVT RegisterVT = TLI->getRegisterType(*DAG.getContext(), ValueVTs[0]);
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00006777 ISD::InputArg RetArg(Flags, RegisterVT, true, 0, 0);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006778 Ins.push_back(RetArg);
6779 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00006780
Dan Gohman98ca4f22009-08-05 01:29:28 +00006781 // Set up the incoming argument description vector.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006782 unsigned Idx = 1;
Dan Gohman46510a72010-04-15 01:51:59 +00006783 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
Dan Gohman98ca4f22009-08-05 01:29:28 +00006784 I != E; ++I, ++Idx) {
Owen Andersone50ed302009-08-10 22:56:29 +00006785 SmallVector<EVT, 4> ValueVTs;
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006786 ComputeValueVTs(*TLI, I->getType(), ValueVTs);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006787 bool isArgValueUsed = !I->use_empty();
6788 for (unsigned Value = 0, NumValues = ValueVTs.size();
6789 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006790 EVT VT = ValueVTs[Value];
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006791 Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00006792 ISD::ArgFlagsTy Flags;
6793 unsigned OriginalAlignment =
6794 TD->getABITypeAlignment(ArgTy);
6795
Bill Wendling39cd0c82012-12-30 12:45:13 +00006796 if (F.getAttributes().hasAttribute(Idx, Attribute::ZExt))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006797 Flags.setZExt();
Bill Wendling39cd0c82012-12-30 12:45:13 +00006798 if (F.getAttributes().hasAttribute(Idx, Attribute::SExt))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006799 Flags.setSExt();
Bill Wendling39cd0c82012-12-30 12:45:13 +00006800 if (F.getAttributes().hasAttribute(Idx, Attribute::InReg))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006801 Flags.setInReg();
Bill Wendling39cd0c82012-12-30 12:45:13 +00006802 if (F.getAttributes().hasAttribute(Idx, Attribute::StructRet))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006803 Flags.setSRet();
Bill Wendling39cd0c82012-12-30 12:45:13 +00006804 if (F.getAttributes().hasAttribute(Idx, Attribute::ByVal)) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00006805 Flags.setByVal();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006806 PointerType *Ty = cast<PointerType>(I->getType());
6807 Type *ElementTy = Ty->getElementType();
Chris Lattner9db20f32011-05-22 23:23:02 +00006808 Flags.setByValSize(TD->getTypeAllocSize(ElementTy));
Dan Gohman98ca4f22009-08-05 01:29:28 +00006809 // For ByVal, alignment should be passed from FE. BE will guess if
6810 // this info is not there but there are cases it cannot get right.
Chris Lattner9db20f32011-05-22 23:23:02 +00006811 unsigned FrameAlign;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006812 if (F.getParamAlignment(Idx))
6813 FrameAlign = F.getParamAlignment(Idx);
Chris Lattner9db20f32011-05-22 23:23:02 +00006814 else
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006815 FrameAlign = TLI->getByValTypeAlignment(ElementTy);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006816 Flags.setByValAlign(FrameAlign);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006817 }
Bill Wendling39cd0c82012-12-30 12:45:13 +00006818 if (F.getAttributes().hasAttribute(Idx, Attribute::Nest))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006819 Flags.setNest();
6820 Flags.setOrigAlign(OriginalAlignment);
6821
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006822 MVT RegisterVT = TLI->getRegisterType(*CurDAG->getContext(), VT);
6823 unsigned NumRegs = TLI->getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006824 for (unsigned i = 0; i != NumRegs; ++i) {
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00006825 ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed,
6826 Idx-1, i*RegisterVT.getStoreSize());
Dan Gohman98ca4f22009-08-05 01:29:28 +00006827 if (NumRegs > 1 && i == 0)
6828 MyFlags.Flags.setSplit();
6829 // if it isn't first piece, alignment must be 1
6830 else if (i > 0)
6831 MyFlags.Flags.setOrigAlign(1);
6832 Ins.push_back(MyFlags);
6833 }
6834 }
6835 }
6836
6837 // Call the target to set up the argument values.
6838 SmallVector<SDValue, 8> InVals;
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006839 SDValue NewRoot = TLI->LowerFormalArguments(DAG.getRoot(), F.getCallingConv(),
6840 F.isVarArg(), Ins,
6841 dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00006842
6843 // Verify that the target's LowerFormalArguments behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00006844 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00006845 "LowerFormalArguments didn't return a valid chain!");
6846 assert(InVals.size() == Ins.size() &&
6847 "LowerFormalArguments didn't emit the correct number of values!");
Bill Wendling3ea58b62009-12-22 21:35:02 +00006848 DEBUG({
6849 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
6850 assert(InVals[i].getNode() &&
6851 "LowerFormalArguments emitted a null value!");
Duncan Sands1440e8b2010-11-03 11:35:31 +00006852 assert(EVT(Ins[i].VT) == InVals[i].getValueType() &&
Bill Wendling3ea58b62009-12-22 21:35:02 +00006853 "LowerFormalArguments emitted a value with the wrong type!");
6854 }
6855 });
Bill Wendling3ea3c242009-12-22 02:10:19 +00006856
Dan Gohman5e866062009-08-06 15:37:27 +00006857 // Update the DAG with the new chain value resulting from argument lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006858 DAG.setRoot(NewRoot);
6859
6860 // Set up the argument values.
6861 unsigned i = 0;
6862 Idx = 1;
Dan Gohman7451d3e2010-05-29 17:03:36 +00006863 if (!FuncInfo->CanLowerReturn) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006864 // Create a virtual register for the sret pointer, and put in a copy
6865 // from the sret argument into it.
6866 SmallVector<EVT, 1> ValueVTs;
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006867 ComputeValueVTs(*TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00006868 MVT VT = ValueVTs[0].getSimpleVT();
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006869 MVT RegVT = TLI->getRegisterType(*CurDAG->getContext(), VT);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006870 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Bill Wendling46ada192010-03-02 01:55:18 +00006871 SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1,
Bill Wendling12931302012-09-26 04:04:19 +00006872 RegVT, VT, NULL, AssertOp);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006873
Dan Gohman2048b852009-11-23 18:04:58 +00006874 MachineFunction& MF = SDB->DAG.getMachineFunction();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006875 MachineRegisterInfo& RegInfo = MF.getRegInfo();
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006876 unsigned SRetReg = RegInfo.createVirtualRegister(TLI->getRegClassFor(RegVT));
Dan Gohman7451d3e2010-05-29 17:03:36 +00006877 FuncInfo->DemoteRegister = SRetReg;
Andrew Trickac6d9be2013-05-25 02:42:55 +00006878 NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurSDLoc(),
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00006879 SRetReg, ArgValue);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006880 DAG.setRoot(NewRoot);
Bill Wendling3ea3c242009-12-22 02:10:19 +00006881
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006882 // i indexes lowered arguments. Bump it past the hidden sret argument.
6883 // Idx indexes LLVM arguments. Don't touch it.
6884 ++i;
6885 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006886
Dan Gohman46510a72010-04-15 01:51:59 +00006887 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006888 ++I, ++Idx) {
6889 SmallVector<SDValue, 4> ArgValues;
Owen Andersone50ed302009-08-10 22:56:29 +00006890 SmallVector<EVT, 4> ValueVTs;
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006891 ComputeValueVTs(*TLI, I->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006892 unsigned NumValues = ValueVTs.size();
Devang Patel9126c0d2010-06-01 19:59:01 +00006893
6894 // If this argument is unused then remember its value. It is used to generate
6895 // debugging information.
Adrian Prantldf688032013-05-16 23:44:12 +00006896 if (I->use_empty() && NumValues) {
Devang Patel9126c0d2010-06-01 19:59:01 +00006897 SDB->setUnusedArgValue(I, InVals[i]);
6898
Adrian Prantldf688032013-05-16 23:44:12 +00006899 // Also remember any frame index for use in FastISel.
6900 if (FrameIndexSDNode *FI =
6901 dyn_cast<FrameIndexSDNode>(InVals[i].getNode()))
6902 FuncInfo->setArgumentFrameIndex(I, FI->getIndex());
6903 }
6904
Eli Friedman23d32432011-05-05 16:53:34 +00006905 for (unsigned Val = 0; Val != NumValues; ++Val) {
6906 EVT VT = ValueVTs[Val];
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006907 MVT PartVT = TLI->getRegisterType(*CurDAG->getContext(), VT);
6908 unsigned NumParts = TLI->getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006909
6910 if (!I->use_empty()) {
6911 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Bill Wendling39cd0c82012-12-30 12:45:13 +00006912 if (F.getAttributes().hasAttribute(Idx, Attribute::SExt))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006913 AssertOp = ISD::AssertSext;
Bill Wendling39cd0c82012-12-30 12:45:13 +00006914 else if (F.getAttributes().hasAttribute(Idx, Attribute::ZExt))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006915 AssertOp = ISD::AssertZext;
6916
Bill Wendling46ada192010-03-02 01:55:18 +00006917 ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i],
Bill Wendling3ea3c242009-12-22 02:10:19 +00006918 NumParts, PartVT, VT,
Bill Wendling12931302012-09-26 04:04:19 +00006919 NULL, AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00006920 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006921
Dan Gohman98ca4f22009-08-05 01:29:28 +00006922 i += NumParts;
6923 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006924
Eli Friedman23d32432011-05-05 16:53:34 +00006925 // We don't need to do anything else for unused arguments.
6926 if (ArgValues.empty())
6927 continue;
6928
Devang Patel9aee3352011-09-08 22:59:09 +00006929 // Note down frame index.
6930 if (FrameIndexSDNode *FI =
Bill Wendling96cb1122012-07-19 00:04:14 +00006931 dyn_cast<FrameIndexSDNode>(ArgValues[0].getNode()))
Devang Patel9aee3352011-09-08 22:59:09 +00006932 FuncInfo->setArgumentFrameIndex(I, FI->getIndex());
Devang Patel0b48ead2010-08-31 22:22:42 +00006933
Eli Friedman23d32432011-05-05 16:53:34 +00006934 SDValue Res = DAG.getMergeValues(&ArgValues[0], NumValues,
Andrew Trickac6d9be2013-05-25 02:42:55 +00006935 SDB->getCurSDLoc());
Devang Patel9aee3352011-09-08 22:59:09 +00006936
Eli Friedman23d32432011-05-05 16:53:34 +00006937 SDB->setValue(I, Res);
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006938 if (!TM.Options.EnableFastISel && Res.getOpcode() == ISD::BUILD_PAIR) {
Stephen Lin155615d2013-07-08 00:37:03 +00006939 if (LoadSDNode *LNode =
Devang Patel9aee3352011-09-08 22:59:09 +00006940 dyn_cast<LoadSDNode>(Res.getOperand(0).getNode()))
6941 if (FrameIndexSDNode *FI =
6942 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
6943 FuncInfo->setArgumentFrameIndex(I, FI->getIndex());
6944 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006945
Eli Friedman23d32432011-05-05 16:53:34 +00006946 // If this argument is live outside of the entry block, insert a copy from
6947 // wherever we got it to the vreg that other BB's will reference it as.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006948 if (!TM.Options.EnableFastISel && Res.getOpcode() == ISD::CopyFromReg) {
Eli Friedman23d32432011-05-05 16:53:34 +00006949 // If we can, though, try to skip creating an unnecessary vreg.
6950 // FIXME: This isn't very clean... it would be nice to make this more
Eli Friedman7f33d672011-05-10 21:50:58 +00006951 // general. It's also subtly incompatible with the hacks FastISel
6952 // uses with vregs.
Eli Friedman23d32432011-05-05 16:53:34 +00006953 unsigned Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg();
6954 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
6955 FuncInfo->ValueMap[I] = Reg;
6956 continue;
6957 }
6958 }
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006959 if (!isOnlyUsedInEntryBlock(I, TM.Options.EnableFastISel)) {
Eli Friedman23d32432011-05-05 16:53:34 +00006960 FuncInfo->InitializeRegForValue(I);
Dan Gohman2048b852009-11-23 18:04:58 +00006961 SDB->CopyToExportRegsIfNeeded(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006962 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006963 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006964
Dan Gohman98ca4f22009-08-05 01:29:28 +00006965 assert(i == InVals.size() && "Argument register count mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006966
6967 // Finally, if the target has anything special to do, allow it to do so.
6968 // FIXME: this should insert code into the DAG!
Dan Gohman64652652010-04-14 20:17:22 +00006969 EmitFunctionEntryCode();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006970}
6971
6972/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
6973/// ensure constants are generated when needed. Remember the virtual registers
6974/// that need to be added to the Machine PHI nodes as input. We cannot just
6975/// directly add them, because expansion might result in multiple MBB's for one
6976/// BB. As such, the start of the BB might correspond to a different MBB than
6977/// the end.
6978///
6979void
Dan Gohmanf81eca02010-04-22 20:46:50 +00006980SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
Dan Gohman46510a72010-04-15 01:51:59 +00006981 const TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006982
6983 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
6984
6985 // Check successor nodes' PHI nodes that expect a constant to be available
6986 // from this block.
6987 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
Dan Gohman46510a72010-04-15 01:51:59 +00006988 const BasicBlock *SuccBB = TI->getSuccessor(succ);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006989 if (!isa<PHINode>(SuccBB->begin())) continue;
Dan Gohmanf81eca02010-04-22 20:46:50 +00006990 MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006991
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006992 // If this terminator has multiple identical successors (common for
6993 // switches), only handle each succ once.
6994 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006995
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006996 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006997
6998 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6999 // nodes and Machine PHI nodes, but the incoming operands have not been
7000 // emitted yet.
Dan Gohman46510a72010-04-15 01:51:59 +00007001 for (BasicBlock::const_iterator I = SuccBB->begin();
7002 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007003 // Ignore dead phi's.
7004 if (PN->use_empty()) continue;
7005
Rafael Espindola3fa82832011-05-13 15:18:06 +00007006 // Skip empty types
7007 if (PN->getType()->isEmptyTy())
7008 continue;
7009
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007010 unsigned Reg;
Dan Gohman46510a72010-04-15 01:51:59 +00007011 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007012
Dan Gohman46510a72010-04-15 01:51:59 +00007013 if (const Constant *C = dyn_cast<Constant>(PHIOp)) {
Dan Gohmanf81eca02010-04-22 20:46:50 +00007014 unsigned &RegOut = ConstantsOut[C];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007015 if (RegOut == 0) {
Dan Gohman89496d02010-07-02 00:10:16 +00007016 RegOut = FuncInfo.CreateRegs(C->getType());
Dan Gohmanf81eca02010-04-22 20:46:50 +00007017 CopyValueToVirtualRegister(C, RegOut);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007018 }
7019 Reg = RegOut;
7020 } else {
Dan Gohmanc25ad632010-07-01 01:33:21 +00007021 DenseMap<const Value *, unsigned>::iterator I =
7022 FuncInfo.ValueMap.find(PHIOp);
7023 if (I != FuncInfo.ValueMap.end())
7024 Reg = I->second;
7025 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007026 assert(isa<AllocaInst>(PHIOp) &&
Dan Gohmanf81eca02010-04-22 20:46:50 +00007027 FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007028 "Didn't codegen value into a register!??");
Dan Gohman89496d02010-07-02 00:10:16 +00007029 Reg = FuncInfo.CreateRegs(PHIOp->getType());
Dan Gohmanf81eca02010-04-22 20:46:50 +00007030 CopyValueToVirtualRegister(PHIOp, Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007031 }
7032 }
7033
7034 // Remember that this register needs to added to the machine PHI node as
7035 // the input for this MBB.
Owen Andersone50ed302009-08-10 22:56:29 +00007036 SmallVector<EVT, 4> ValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00007037 const TargetLowering *TLI = TM.getTargetLowering();
7038 ComputeValueVTs(*TLI, PN->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007039 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +00007040 EVT VT = ValueVTs[vti];
Bill Wendlingba54bca2013-06-19 21:36:55 +00007041 unsigned NumRegisters = TLI->getNumRegisters(*DAG.getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007042 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
Dan Gohmanf81eca02010-04-22 20:46:50 +00007043 FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007044 Reg += NumRegisters;
7045 }
7046 }
7047 }
Bill Wendlingba54bca2013-06-19 21:36:55 +00007048
Dan Gohmanf81eca02010-04-22 20:46:50 +00007049 ConstantsOut.clear();
Dan Gohman3df24e62008-09-03 23:12:08 +00007050}