blob: e23c5bea224b62da4d5375e98feda19fc1211f07 [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"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000061#include <algorithm>
62using namespace llvm;
63
Dale Johannesen601d3c02008-09-05 01:48:15 +000064/// LimitFloatPrecision - Generate low-precision inline sequences for
65/// some float libcalls (6, 8 or 12 bits).
66static unsigned LimitFloatPrecision;
67
68static cl::opt<unsigned, true>
69LimitFPPrecision("limit-float-precision",
70 cl::desc("Generate low-precision inline sequences "
71 "for some float libcalls"),
72 cl::location(LimitFloatPrecision),
73 cl::init(0));
74
Andrew Trickde91f3c2010-11-12 17:50:46 +000075// Limit the width of DAG chains. This is important in general to prevent
76// prevent DAG-based analysis from blowing up. For example, alias analysis and
77// load clustering may not complete in reasonable time. It is difficult to
78// recognize and avoid this situation within each individual analysis, and
79// future analyses are likely to have the same behavior. Limiting DAG width is
Andrew Trickb9e6fe12010-11-20 07:26:51 +000080// the safe approach, and will be especially important with global DAGs.
Andrew Trickde91f3c2010-11-12 17:50:46 +000081//
82// MaxParallelChains default is arbitrarily high to avoid affecting
83// optimization, but could be lowered to improve compile time. Any ld-ld-st-st
Andrew Trickb9e6fe12010-11-20 07:26:51 +000084// sequence over this should have been converted to llvm.memcpy by the
85// frontend. It easy to induce this behavior with .ll code such as:
86// %buffer = alloca [4096 x i8]
87// %data = load [4096 x i8]* %argPtr
88// store [4096 x i8] %data, [4096 x i8]* %buffer
Andrew Trick778583a2011-03-11 17:46:59 +000089static const unsigned MaxParallelChains = 64;
Andrew Trickde91f3c2010-11-12 17:50:46 +000090
Andrew Trickac6d9be2013-05-25 02:42:55 +000091static SDValue getCopyFromPartsVector(SelectionDAG &DAG, SDLoc DL,
Chris Lattner3ac18842010-08-24 23:20:40 +000092 const SDValue *Parts, unsigned NumParts,
Patrik Hagglundb9e12e52012-12-19 12:33:30 +000093 MVT PartVT, EVT ValueVT, const Value *V);
Michael J. Spencere70c5262010-10-16 08:25:21 +000094
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000095/// getCopyFromParts - Create a value that contains the specified legal parts
96/// combined into the value they represent. If the parts combine to a type
97/// larger then ValueVT then AssertOp can be used to specify whether the extra
98/// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT
99/// (ISD::AssertSext).
Andrew Trickac6d9be2013-05-25 02:42:55 +0000100static SDValue getCopyFromParts(SelectionDAG &DAG, SDLoc DL,
Dale Johannesen66978ee2009-01-31 02:22:37 +0000101 const SDValue *Parts,
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000102 unsigned NumParts, MVT PartVT, EVT ValueVT,
Bill Wendling12931302012-09-26 04:04:19 +0000103 const Value *V,
Duncan Sands0b3aa262009-01-28 14:42:54 +0000104 ISD::NodeType AssertOp = ISD::DELETED_NODE) {
Chris Lattner3ac18842010-08-24 23:20:40 +0000105 if (ValueVT.isVector())
Bill Wendling12931302012-09-26 04:04:19 +0000106 return getCopyFromPartsVector(DAG, DL, Parts, NumParts,
107 PartVT, ValueVT, V);
Michael J. Spencere70c5262010-10-16 08:25:21 +0000108
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000109 assert(NumParts > 0 && "No parts to assemble!");
Dan Gohmane9530ec2009-01-15 16:58:17 +0000110 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000111 SDValue Val = Parts[0];
112
113 if (NumParts > 1) {
114 // Assemble the value from multiple parts.
Chris Lattner3ac18842010-08-24 23:20:40 +0000115 if (ValueVT.isInteger()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000116 unsigned PartBits = PartVT.getSizeInBits();
117 unsigned ValueBits = ValueVT.getSizeInBits();
118
119 // Assemble the power of 2 part.
120 unsigned RoundParts = NumParts & (NumParts - 1) ?
121 1 << Log2_32(NumParts) : NumParts;
122 unsigned RoundBits = PartBits * RoundParts;
Owen Andersone50ed302009-08-10 22:56:29 +0000123 EVT RoundVT = RoundBits == ValueBits ?
Owen Anderson23b9b192009-08-12 00:36:31 +0000124 ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000125 SDValue Lo, Hi;
126
Owen Anderson23b9b192009-08-12 00:36:31 +0000127 EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2);
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000128
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000129 if (RoundParts > 2) {
Chris Lattner3ac18842010-08-24 23:20:40 +0000130 Lo = getCopyFromParts(DAG, DL, Parts, RoundParts / 2,
Bill Wendling12931302012-09-26 04:04:19 +0000131 PartVT, HalfVT, V);
Chris Lattner3ac18842010-08-24 23:20:40 +0000132 Hi = getCopyFromParts(DAG, DL, Parts + RoundParts / 2,
Bill Wendling12931302012-09-26 04:04:19 +0000133 RoundParts / 2, PartVT, HalfVT, V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000134 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000135 Lo = DAG.getNode(ISD::BITCAST, DL, HalfVT, Parts[0]);
136 Hi = DAG.getNode(ISD::BITCAST, DL, HalfVT, Parts[1]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000137 }
Bill Wendling3ea3c242009-12-22 02:10:19 +0000138
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000139 if (TLI.isBigEndian())
140 std::swap(Lo, Hi);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000141
Chris Lattner3ac18842010-08-24 23:20:40 +0000142 Val = DAG.getNode(ISD::BUILD_PAIR, DL, RoundVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000143
144 if (RoundParts < NumParts) {
145 // Assemble the trailing non-power-of-2 part.
146 unsigned OddParts = NumParts - RoundParts;
Owen Anderson23b9b192009-08-12 00:36:31 +0000147 EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits);
Chris Lattner3ac18842010-08-24 23:20:40 +0000148 Hi = getCopyFromParts(DAG, DL,
Bill Wendling12931302012-09-26 04:04:19 +0000149 Parts + RoundParts, OddParts, PartVT, OddVT, V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000150
151 // Combine the round and odd parts.
152 Lo = Val;
153 if (TLI.isBigEndian())
154 std::swap(Lo, Hi);
Owen Anderson23b9b192009-08-12 00:36:31 +0000155 EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Chris Lattner3ac18842010-08-24 23:20:40 +0000156 Hi = DAG.getNode(ISD::ANY_EXTEND, DL, TotalVT, Hi);
157 Hi = DAG.getNode(ISD::SHL, DL, TotalVT, Hi,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000158 DAG.getConstant(Lo.getValueType().getSizeInBits(),
Duncan Sands92abc622009-01-31 15:50:11 +0000159 TLI.getPointerTy()));
Chris Lattner3ac18842010-08-24 23:20:40 +0000160 Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, TotalVT, Lo);
161 Val = DAG.getNode(ISD::OR, DL, TotalVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000162 }
Eli Friedman2ac8b322009-05-20 06:02:09 +0000163 } else if (PartVT.isFloatingPoint()) {
164 // FP split into multiple FP parts (for ppcf128)
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000165 assert(ValueVT == EVT(MVT::ppcf128) && PartVT == MVT::f64 &&
Eli Friedman2ac8b322009-05-20 06:02:09 +0000166 "Unexpected split");
167 SDValue Lo, Hi;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000168 Lo = DAG.getNode(ISD::BITCAST, DL, EVT(MVT::f64), Parts[0]);
169 Hi = DAG.getNode(ISD::BITCAST, DL, EVT(MVT::f64), Parts[1]);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000170 if (TLI.isBigEndian())
171 std::swap(Lo, Hi);
Chris Lattner3ac18842010-08-24 23:20:40 +0000172 Val = DAG.getNode(ISD::BUILD_PAIR, DL, ValueVT, Lo, Hi);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000173 } else {
174 // FP split into integer parts (soft fp)
175 assert(ValueVT.isFloatingPoint() && PartVT.isInteger() &&
176 !PartVT.isVector() && "Unexpected split");
Owen Anderson23b9b192009-08-12 00:36:31 +0000177 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
Bill Wendling12931302012-09-26 04:04:19 +0000178 Val = getCopyFromParts(DAG, DL, Parts, NumParts, PartVT, IntVT, V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000179 }
180 }
181
182 // There is now one part, held in Val. Correct it to match ValueVT.
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000183 EVT PartEVT = Val.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000184
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000185 if (PartEVT == ValueVT)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000186 return Val;
187
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000188 if (PartEVT.isInteger() && ValueVT.isInteger()) {
189 if (ValueVT.bitsLT(PartEVT)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000190 // For a truncate, see if we have any information to
191 // indicate whether the truncated bits will always be
192 // zero or sign-extension.
193 if (AssertOp != ISD::DELETED_NODE)
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000194 Val = DAG.getNode(AssertOp, DL, PartEVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000195 DAG.getValueType(ValueVT));
Chris Lattner3ac18842010-08-24 23:20:40 +0000196 return DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000197 }
Chris Lattner3ac18842010-08-24 23:20:40 +0000198 return DAG.getNode(ISD::ANY_EXTEND, DL, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000199 }
200
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000201 if (PartEVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
Chris Lattner3ac18842010-08-24 23:20:40 +0000202 // FP_ROUND's are always exact here.
203 if (ValueVT.bitsLT(Val.getValueType()))
204 return DAG.getNode(ISD::FP_ROUND, DL, ValueVT, Val,
Pete Cooperf57e1c22012-01-17 01:54:07 +0000205 DAG.getTargetConstant(1, TLI.getPointerTy()));
Bill Wendling3ea3c242009-12-22 02:10:19 +0000206
Chris Lattner3ac18842010-08-24 23:20:40 +0000207 return DAG.getNode(ISD::FP_EXTEND, DL, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000208 }
209
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000210 if (PartEVT.getSizeInBits() == ValueVT.getSizeInBits())
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000211 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000212
Torok Edwinc23197a2009-07-14 16:55:14 +0000213 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000214}
215
Bill Wendling12931302012-09-26 04:04:19 +0000216/// getCopyFromPartsVector - Create a value that contains the specified legal
217/// parts combined into the value they represent. If the parts combine to a
218/// type larger then ValueVT then AssertOp can be used to specify whether the
219/// extra bits are known to be zero (ISD::AssertZext) or sign extended from
220/// ValueVT (ISD::AssertSext).
Andrew Trickac6d9be2013-05-25 02:42:55 +0000221static SDValue getCopyFromPartsVector(SelectionDAG &DAG, SDLoc DL,
Chris Lattner3ac18842010-08-24 23:20:40 +0000222 const SDValue *Parts, unsigned NumParts,
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000223 MVT PartVT, EVT ValueVT, const Value *V) {
Chris Lattner3ac18842010-08-24 23:20:40 +0000224 assert(ValueVT.isVector() && "Not a vector value");
225 assert(NumParts > 0 && "No parts to assemble!");
226 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
227 SDValue Val = Parts[0];
Michael J. Spencere70c5262010-10-16 08:25:21 +0000228
Chris Lattner3ac18842010-08-24 23:20:40 +0000229 // Handle a multi-element vector.
230 if (NumParts > 1) {
Patrik Hagglundee211d22012-12-19 11:53:21 +0000231 EVT IntermediateVT;
232 MVT RegisterVT;
Chris Lattner3ac18842010-08-24 23:20:40 +0000233 unsigned NumIntermediates;
234 unsigned NumRegs =
235 TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT,
236 NumIntermediates, RegisterVT);
237 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
238 NumParts = NumRegs; // Silence a compiler warning.
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000239 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
Patrik Hagglundee211d22012-12-19 11:53:21 +0000240 assert(RegisterVT == Parts[0].getSimpleValueType() &&
Chris Lattner3ac18842010-08-24 23:20:40 +0000241 "Part type doesn't match part!");
Michael J. Spencere70c5262010-10-16 08:25:21 +0000242
Chris Lattner3ac18842010-08-24 23:20:40 +0000243 // Assemble the parts into intermediate operands.
244 SmallVector<SDValue, 8> Ops(NumIntermediates);
245 if (NumIntermediates == NumParts) {
246 // If the register was not expanded, truncate or copy the value,
247 // as appropriate.
248 for (unsigned i = 0; i != NumParts; ++i)
249 Ops[i] = getCopyFromParts(DAG, DL, &Parts[i], 1,
Bill Wendling12931302012-09-26 04:04:19 +0000250 PartVT, IntermediateVT, V);
Chris Lattner3ac18842010-08-24 23:20:40 +0000251 } else if (NumParts > 0) {
252 // If the intermediate type was expanded, build the intermediate
253 // operands from the parts.
254 assert(NumParts % NumIntermediates == 0 &&
255 "Must expand into a divisible number of parts!");
256 unsigned Factor = NumParts / NumIntermediates;
257 for (unsigned i = 0; i != NumIntermediates; ++i)
258 Ops[i] = getCopyFromParts(DAG, DL, &Parts[i * Factor], Factor,
Bill Wendling12931302012-09-26 04:04:19 +0000259 PartVT, IntermediateVT, V);
Chris Lattner3ac18842010-08-24 23:20:40 +0000260 }
Michael J. Spencere70c5262010-10-16 08:25:21 +0000261
Chris Lattner3ac18842010-08-24 23:20:40 +0000262 // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the
263 // intermediate operands.
264 Val = DAG.getNode(IntermediateVT.isVector() ?
265 ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, DL,
266 ValueVT, &Ops[0], NumIntermediates);
267 }
Michael J. Spencere70c5262010-10-16 08:25:21 +0000268
Chris Lattner3ac18842010-08-24 23:20:40 +0000269 // There is now one part, held in Val. Correct it to match ValueVT.
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000270 EVT PartEVT = Val.getValueType();
Michael J. Spencere70c5262010-10-16 08:25:21 +0000271
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000272 if (PartEVT == ValueVT)
Chris Lattner3ac18842010-08-24 23:20:40 +0000273 return Val;
Michael J. Spencere70c5262010-10-16 08:25:21 +0000274
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000275 if (PartEVT.isVector()) {
Chris Lattnere6f7c262010-08-25 22:49:25 +0000276 // If the element type of the source/dest vectors are the same, but the
277 // parts vector has more elements than the value vector, then we have a
278 // vector widening case (e.g. <2 x float> -> <4 x float>). Extract the
279 // elements we want.
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000280 if (PartEVT.getVectorElementType() == ValueVT.getVectorElementType()) {
281 assert(PartEVT.getVectorNumElements() > ValueVT.getVectorNumElements() &&
Chris Lattnere6f7c262010-08-25 22:49:25 +0000282 "Cannot narrow, it would be a lossy transformation");
283 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, ValueVT, Val,
Tom Stellard425b76c2013-08-05 22:22:01 +0000284 DAG.getConstant(0, TLI.getVectorIdxTy()));
Michael J. Spencere70c5262010-10-16 08:25:21 +0000285 }
286
Chris Lattnere6f7c262010-08-25 22:49:25 +0000287 // Vector/Vector bitcast.
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000288 if (ValueVT.getSizeInBits() == PartEVT.getSizeInBits())
Nadav Rotem0b666362011-06-04 20:58:08 +0000289 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
290
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000291 assert(PartEVT.getVectorNumElements() == ValueVT.getVectorNumElements() &&
Nadav Rotem0b666362011-06-04 20:58:08 +0000292 "Cannot handle this kind of promotion");
293 // Promoted vector extract
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000294 bool Smaller = ValueVT.bitsLE(PartEVT);
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000295 return DAG.getNode((Smaller ? ISD::TRUNCATE : ISD::ANY_EXTEND),
296 DL, ValueVT, Val);
Nadav Rotem0b666362011-06-04 20:58:08 +0000297
Chris Lattnere6f7c262010-08-25 22:49:25 +0000298 }
Eric Christopher471e4222011-06-08 23:55:35 +0000299
Eric Christopher9aaa02a2011-06-01 19:55:10 +0000300 // Trivial bitcast if the types are the same size and the destination
301 // vector type is legal.
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000302 if (PartEVT.getSizeInBits() == ValueVT.getSizeInBits() &&
Eric Christopher9aaa02a2011-06-01 19:55:10 +0000303 TLI.isTypeLegal(ValueVT))
304 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
Michael J. Spencere70c5262010-10-16 08:25:21 +0000305
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000306 // Handle cases such as i8 -> <1 x i1>
Bill Wendling12931302012-09-26 04:04:19 +0000307 if (ValueVT.getVectorNumElements() != 1) {
308 LLVMContext &Ctx = *DAG.getContext();
309 Twine ErrMsg("non-trivial scalar-to-vector conversion");
310 if (const Instruction *I = dyn_cast_or_null<Instruction>(V)) {
311 if (const CallInst *CI = dyn_cast<CallInst>(I))
312 if (isa<InlineAsm>(CI->getCalledValue()))
313 ErrMsg = ErrMsg + ", possible invalid constraint for vector type";
314 Ctx.emitError(I, ErrMsg);
315 } else {
316 Ctx.emitError(ErrMsg);
317 }
Chad Rosierf0b07552013-05-01 19:49:26 +0000318 return DAG.getUNDEF(ValueVT);
Bill Wendling12931302012-09-26 04:04:19 +0000319 }
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000320
321 if (ValueVT.getVectorNumElements() == 1 &&
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000322 ValueVT.getVectorElementType() != PartEVT) {
323 bool Smaller = ValueVT.bitsLE(PartEVT);
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000324 Val = DAG.getNode((Smaller ? ISD::TRUNCATE : ISD::ANY_EXTEND),
325 DL, ValueVT.getScalarType(), Val);
326 }
327
Chris Lattner3ac18842010-08-24 23:20:40 +0000328 return DAG.getNode(ISD::BUILD_VECTOR, DL, ValueVT, Val);
329}
330
Andrew Trickac6d9be2013-05-25 02:42:55 +0000331static void getCopyToPartsVector(SelectionDAG &DAG, SDLoc dl,
Chris Lattnera13b8602010-08-24 23:10:06 +0000332 SDValue Val, SDValue *Parts, unsigned NumParts,
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000333 MVT PartVT, const Value *V);
Michael J. Spencere70c5262010-10-16 08:25:21 +0000334
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000335/// getCopyToParts - Create a series of nodes that contain the specified value
336/// split into legal parts. If the parts contain more bits than Val, then, for
337/// integers, ExtendKind can be used to specify how to generate the extra bits.
Andrew Trickac6d9be2013-05-25 02:42:55 +0000338static void getCopyToParts(SelectionDAG &DAG, SDLoc DL,
Bill Wendling3ea3c242009-12-22 02:10:19 +0000339 SDValue Val, SDValue *Parts, unsigned NumParts,
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000340 MVT PartVT, const Value *V,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000341 ISD::NodeType ExtendKind = ISD::ANY_EXTEND) {
Owen Andersone50ed302009-08-10 22:56:29 +0000342 EVT ValueVT = Val.getValueType();
Michael J. Spencere70c5262010-10-16 08:25:21 +0000343
Chris Lattnera13b8602010-08-24 23:10:06 +0000344 // Handle the vector case separately.
345 if (ValueVT.isVector())
Bill Wendlingf18eb582012-09-26 06:16:18 +0000346 return getCopyToPartsVector(DAG, DL, Val, Parts, NumParts, PartVT, V);
Michael J. Spencere70c5262010-10-16 08:25:21 +0000347
Chris Lattnera13b8602010-08-24 23:10:06 +0000348 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000349 unsigned PartBits = PartVT.getSizeInBits();
Dale Johannesen8a36f502009-02-25 22:39:13 +0000350 unsigned OrigNumParts = NumParts;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000351 assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!");
352
Chris Lattnera13b8602010-08-24 23:10:06 +0000353 if (NumParts == 0)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000354 return;
355
Chris Lattnera13b8602010-08-24 23:10:06 +0000356 assert(!ValueVT.isVector() && "Vector case handled elsewhere");
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000357 EVT PartEVT = PartVT;
358 if (PartEVT == ValueVT) {
Chris Lattnera13b8602010-08-24 23:10:06 +0000359 assert(NumParts == 1 && "No-op copy with multiple parts!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000360 Parts[0] = Val;
361 return;
362 }
363
Chris Lattnera13b8602010-08-24 23:10:06 +0000364 if (NumParts * PartBits > ValueVT.getSizeInBits()) {
365 // If the parts cover more bits than the value has, promote the value.
366 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
367 assert(NumParts == 1 && "Do not know what to promote to!");
368 Val = DAG.getNode(ISD::FP_EXTEND, DL, PartVT, Val);
369 } else {
Bill Wendling9e8ceb02012-02-23 23:25:25 +0000370 assert((PartVT.isInteger() || PartVT == MVT::x86mmx) &&
371 ValueVT.isInteger() &&
Michael J. Spencere70c5262010-10-16 08:25:21 +0000372 "Unknown mismatch!");
Chris Lattnera13b8602010-08-24 23:10:06 +0000373 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
374 Val = DAG.getNode(ExtendKind, DL, ValueVT, Val);
Bill Wendling9e8ceb02012-02-23 23:25:25 +0000375 if (PartVT == MVT::x86mmx)
376 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
Chris Lattnera13b8602010-08-24 23:10:06 +0000377 }
378 } else if (PartBits == ValueVT.getSizeInBits()) {
379 // Different types of the same size.
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000380 assert(NumParts == 1 && PartEVT != ValueVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000381 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
Chris Lattnera13b8602010-08-24 23:10:06 +0000382 } else if (NumParts * PartBits < ValueVT.getSizeInBits()) {
383 // If the parts cover less bits than value has, truncate the value.
Bill Wendling9e8ceb02012-02-23 23:25:25 +0000384 assert((PartVT.isInteger() || PartVT == MVT::x86mmx) &&
385 ValueVT.isInteger() &&
Chris Lattnera13b8602010-08-24 23:10:06 +0000386 "Unknown mismatch!");
387 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
388 Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
Bill Wendling9e8ceb02012-02-23 23:25:25 +0000389 if (PartVT == MVT::x86mmx)
390 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
Chris Lattnera13b8602010-08-24 23:10:06 +0000391 }
392
393 // The value may have changed - recompute ValueVT.
394 ValueVT = Val.getValueType();
395 assert(NumParts * PartBits == ValueVT.getSizeInBits() &&
396 "Failed to tile the value with PartVT!");
397
398 if (NumParts == 1) {
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000399 if (PartEVT != ValueVT) {
Bill Wendlingf18eb582012-09-26 06:16:18 +0000400 LLVMContext &Ctx = *DAG.getContext();
401 Twine ErrMsg("scalar-to-vector conversion failed");
402 if (const Instruction *I = dyn_cast_or_null<Instruction>(V)) {
403 if (const CallInst *CI = dyn_cast<CallInst>(I))
404 if (isa<InlineAsm>(CI->getCalledValue()))
405 ErrMsg = ErrMsg + ", possible invalid constraint for vector type";
406 Ctx.emitError(I, ErrMsg);
407 } else {
408 Ctx.emitError(ErrMsg);
409 }
410 }
411
Chris Lattnera13b8602010-08-24 23:10:06 +0000412 Parts[0] = Val;
413 return;
414 }
415
416 // Expand the value into multiple parts.
417 if (NumParts & (NumParts - 1)) {
418 // The number of parts is not a power of 2. Split off and copy the tail.
419 assert(PartVT.isInteger() && ValueVT.isInteger() &&
420 "Do not know what to expand to!");
421 unsigned RoundParts = 1 << Log2_32(NumParts);
422 unsigned RoundBits = RoundParts * PartBits;
423 unsigned OddParts = NumParts - RoundParts;
424 SDValue OddVal = DAG.getNode(ISD::SRL, DL, ValueVT, Val,
425 DAG.getIntPtrConstant(RoundBits));
Bill Wendlingf18eb582012-09-26 06:16:18 +0000426 getCopyToParts(DAG, DL, OddVal, Parts + RoundParts, OddParts, PartVT, V);
Chris Lattnera13b8602010-08-24 23:10:06 +0000427
428 if (TLI.isBigEndian())
429 // The odd parts were reversed by getCopyToParts - unreverse them.
430 std::reverse(Parts + RoundParts, Parts + NumParts);
431
432 NumParts = RoundParts;
433 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
434 Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
435 }
436
437 // The number of parts is a power of 2. Repeatedly bisect the value using
438 // EXTRACT_ELEMENT.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000439 Parts[0] = DAG.getNode(ISD::BITCAST, DL,
Chris Lattnera13b8602010-08-24 23:10:06 +0000440 EVT::getIntegerVT(*DAG.getContext(),
441 ValueVT.getSizeInBits()),
442 Val);
443
444 for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) {
445 for (unsigned i = 0; i < NumParts; i += StepSize) {
446 unsigned ThisBits = StepSize * PartBits / 2;
447 EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits);
448 SDValue &Part0 = Parts[i];
449 SDValue &Part1 = Parts[i+StepSize/2];
450
451 Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL,
452 ThisVT, Part0, DAG.getIntPtrConstant(1));
453 Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL,
454 ThisVT, Part0, DAG.getIntPtrConstant(0));
455
456 if (ThisBits == PartBits && ThisVT != PartVT) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000457 Part0 = DAG.getNode(ISD::BITCAST, DL, PartVT, Part0);
458 Part1 = DAG.getNode(ISD::BITCAST, DL, PartVT, Part1);
Chris Lattnera13b8602010-08-24 23:10:06 +0000459 }
460 }
461 }
462
463 if (TLI.isBigEndian())
464 std::reverse(Parts, Parts + OrigNumParts);
465}
466
467
468/// getCopyToPartsVector - Create a series of nodes that contain the specified
469/// value split into legal parts.
Andrew Trickac6d9be2013-05-25 02:42:55 +0000470static void getCopyToPartsVector(SelectionDAG &DAG, SDLoc DL,
Chris Lattnera13b8602010-08-24 23:10:06 +0000471 SDValue Val, SDValue *Parts, unsigned NumParts,
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000472 MVT PartVT, const Value *V) {
Chris Lattnera13b8602010-08-24 23:10:06 +0000473 EVT ValueVT = Val.getValueType();
474 assert(ValueVT.isVector() && "Not a vector");
475 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Michael J. Spencere70c5262010-10-16 08:25:21 +0000476
Chris Lattnera13b8602010-08-24 23:10:06 +0000477 if (NumParts == 1) {
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000478 EVT PartEVT = PartVT;
479 if (PartEVT == ValueVT) {
Chris Lattnere6f7c262010-08-25 22:49:25 +0000480 // Nothing to do.
481 } else if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) {
482 // Bitconvert vector->vector case.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000483 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
Chris Lattnere6f7c262010-08-25 22:49:25 +0000484 } else if (PartVT.isVector() &&
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000485 PartEVT.getVectorElementType() == ValueVT.getVectorElementType() &&
486 PartEVT.getVectorNumElements() > ValueVT.getVectorNumElements()) {
Chris Lattnere6f7c262010-08-25 22:49:25 +0000487 EVT ElementVT = PartVT.getVectorElementType();
488 // Vector widening case, e.g. <2 x float> -> <4 x float>. Shuffle in
489 // undef elements.
490 SmallVector<SDValue, 16> Ops;
491 for (unsigned i = 0, e = ValueVT.getVectorNumElements(); i != e; ++i)
492 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
Tom Stellard425b76c2013-08-05 22:22:01 +0000493 ElementVT, Val, DAG.getConstant(i,
494 TLI.getVectorIdxTy())));
Michael J. Spencere70c5262010-10-16 08:25:21 +0000495
Chris Lattnere6f7c262010-08-25 22:49:25 +0000496 for (unsigned i = ValueVT.getVectorNumElements(),
497 e = PartVT.getVectorNumElements(); i != e; ++i)
498 Ops.push_back(DAG.getUNDEF(ElementVT));
499
500 Val = DAG.getNode(ISD::BUILD_VECTOR, DL, PartVT, &Ops[0], Ops.size());
501
502 // FIXME: Use CONCAT for 2x -> 4x.
Michael J. Spencere70c5262010-10-16 08:25:21 +0000503
Chris Lattnere6f7c262010-08-25 22:49:25 +0000504 //SDValue UndefElts = DAG.getUNDEF(VectorTy);
505 //Val = DAG.getNode(ISD::CONCAT_VECTORS, DL, PartVT, Val, UndefElts);
Nadav Rotem0b666362011-06-04 20:58:08 +0000506 } else if (PartVT.isVector() &&
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000507 PartEVT.getVectorElementType().bitsGE(
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000508 ValueVT.getVectorElementType()) &&
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000509 PartEVT.getVectorNumElements() == ValueVT.getVectorNumElements()) {
Nadav Rotem0b666362011-06-04 20:58:08 +0000510
511 // Promoted vector extract
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000512 bool Smaller = PartEVT.bitsLE(ValueVT);
Nadav Rotemc6341e62011-06-19 08:49:38 +0000513 Val = DAG.getNode((Smaller ? ISD::TRUNCATE : ISD::ANY_EXTEND),
514 DL, PartVT, Val);
Nadav Rotem0b666362011-06-04 20:58:08 +0000515 } else{
Chris Lattnere6f7c262010-08-25 22:49:25 +0000516 // Vector -> scalar conversion.
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000517 assert(ValueVT.getVectorNumElements() == 1 &&
Chris Lattnere6f7c262010-08-25 22:49:25 +0000518 "Only trivial vector-to-scalar conversions should get here!");
519 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
Tom Stellard425b76c2013-08-05 22:22:01 +0000520 PartVT, Val, DAG.getConstant(0, TLI.getVectorIdxTy()));
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000521
522 bool Smaller = ValueVT.bitsLE(PartVT);
523 Val = DAG.getNode((Smaller ? ISD::TRUNCATE : ISD::ANY_EXTEND),
524 DL, PartVT, Val);
Chris Lattnera13b8602010-08-24 23:10:06 +0000525 }
Michael J. Spencere70c5262010-10-16 08:25:21 +0000526
Chris Lattnera13b8602010-08-24 23:10:06 +0000527 Parts[0] = Val;
528 return;
529 }
Michael J. Spencere70c5262010-10-16 08:25:21 +0000530
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000531 // Handle a multi-element vector.
Patrik Hagglundee211d22012-12-19 11:53:21 +0000532 EVT IntermediateVT;
533 MVT RegisterVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000534 unsigned NumIntermediates;
Owen Anderson23b9b192009-08-12 00:36:31 +0000535 unsigned NumRegs = TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT,
Devang Patel8f09bea2010-08-26 20:32:32 +0000536 IntermediateVT,
537 NumIntermediates, RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000538 unsigned NumElements = ValueVT.getVectorNumElements();
Michael J. Spencere70c5262010-10-16 08:25:21 +0000539
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000540 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
541 NumParts = NumRegs; // Silence a compiler warning.
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000542 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
Michael J. Spencere70c5262010-10-16 08:25:21 +0000543
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000544 // Split the vector into intermediate operands.
545 SmallVector<SDValue, 8> Ops(NumIntermediates);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000546 for (unsigned i = 0; i != NumIntermediates; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000547 if (IntermediateVT.isVector())
Chris Lattnera13b8602010-08-24 23:10:06 +0000548 Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000549 IntermediateVT, Val,
Tom Stellard425b76c2013-08-05 22:22:01 +0000550 DAG.getConstant(i * (NumElements / NumIntermediates),
551 TLI.getVectorIdxTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000552 else
Chris Lattnera13b8602010-08-24 23:10:06 +0000553 Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
Tom Stellard425b76c2013-08-05 22:22:01 +0000554 IntermediateVT, Val,
555 DAG.getConstant(i, TLI.getVectorIdxTy()));
Bill Wendling3ea3c242009-12-22 02:10:19 +0000556 }
Michael J. Spencere70c5262010-10-16 08:25:21 +0000557
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000558 // Split the intermediate operands into legal parts.
559 if (NumParts == NumIntermediates) {
560 // If the register was not expanded, promote or copy the value,
561 // as appropriate.
562 for (unsigned i = 0; i != NumParts; ++i)
Bill Wendlingf18eb582012-09-26 06:16:18 +0000563 getCopyToParts(DAG, DL, Ops[i], &Parts[i], 1, PartVT, V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000564 } else if (NumParts > 0) {
565 // If the intermediate type was expanded, split each the value into
566 // legal parts.
567 assert(NumParts % NumIntermediates == 0 &&
568 "Must expand into a divisible number of parts!");
569 unsigned Factor = NumParts / NumIntermediates;
570 for (unsigned i = 0; i != NumIntermediates; ++i)
Bill Wendlingf18eb582012-09-26 06:16:18 +0000571 getCopyToParts(DAG, DL, Ops[i], &Parts[i*Factor], Factor, PartVT, V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000572 }
573}
574
Dan Gohman462f6b52010-05-29 17:53:24 +0000575namespace {
576 /// RegsForValue - This struct represents the registers (physical or virtual)
577 /// that a particular set of values is assigned, and the type information
578 /// about the value. The most common situation is to represent one value at a
579 /// time, but struct or array values are handled element-wise as multiple
580 /// values. The splitting of aggregates is performed recursively, so that we
581 /// never have aggregate-typed registers. The values at this point do not
582 /// necessarily have legal types, so each value may require one or more
583 /// registers of some legal type.
584 ///
585 struct RegsForValue {
586 /// ValueVTs - The value types of the values, which may not be legal, and
587 /// may need be promoted or synthesized from one or more registers.
588 ///
589 SmallVector<EVT, 4> ValueVTs;
590
591 /// RegVTs - The value types of the registers. This is the same size as
592 /// ValueVTs and it records, for each value, what the type of the assigned
593 /// register or registers are. (Individual values are never synthesized
594 /// from more than one type of register.)
595 ///
596 /// With virtual registers, the contents of RegVTs is redundant with TLI's
597 /// getRegisterType member function, however when with physical registers
598 /// it is necessary to have a separate record of the types.
599 ///
Patrik Hagglund8963fec2012-12-19 12:23:01 +0000600 SmallVector<MVT, 4> RegVTs;
Dan Gohman462f6b52010-05-29 17:53:24 +0000601
602 /// Regs - This list holds the registers assigned to the values.
603 /// Each legal or promoted value requires one register, and each
604 /// expanded value requires multiple registers.
605 ///
606 SmallVector<unsigned, 4> Regs;
607
608 RegsForValue() {}
609
610 RegsForValue(const SmallVector<unsigned, 4> &regs,
Patrik Hagglund8963fec2012-12-19 12:23:01 +0000611 MVT regvt, EVT valuevt)
Dan Gohman462f6b52010-05-29 17:53:24 +0000612 : ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs) {}
613
Dan Gohman462f6b52010-05-29 17:53:24 +0000614 RegsForValue(LLVMContext &Context, const TargetLowering &tli,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000615 unsigned Reg, Type *Ty) {
Dan Gohman462f6b52010-05-29 17:53:24 +0000616 ComputeValueVTs(tli, Ty, ValueVTs);
617
618 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
619 EVT ValueVT = ValueVTs[Value];
620 unsigned NumRegs = tli.getNumRegisters(Context, ValueVT);
Patrik Hagglunddfcf33a2012-12-19 11:48:16 +0000621 MVT RegisterVT = tli.getRegisterType(Context, ValueVT);
Dan Gohman462f6b52010-05-29 17:53:24 +0000622 for (unsigned i = 0; i != NumRegs; ++i)
623 Regs.push_back(Reg + i);
624 RegVTs.push_back(RegisterVT);
625 Reg += NumRegs;
626 }
627 }
628
629 /// areValueTypesLegal - Return true if types of all the values are legal.
630 bool areValueTypesLegal(const TargetLowering &TLI) {
631 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
Patrik Hagglund8963fec2012-12-19 12:23:01 +0000632 MVT RegisterVT = RegVTs[Value];
Dan Gohman462f6b52010-05-29 17:53:24 +0000633 if (!TLI.isTypeLegal(RegisterVT))
634 return false;
635 }
636 return true;
637 }
638
639 /// append - Add the specified values to this one.
640 void append(const RegsForValue &RHS) {
641 ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end());
642 RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end());
643 Regs.append(RHS.Regs.begin(), RHS.Regs.end());
644 }
645
646 /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
647 /// this value and returns the result as a ValueVTs value. This uses
648 /// Chain/Flag as the input and updates them for the output Chain/Flag.
649 /// If the Flag pointer is NULL, no flag is used.
650 SDValue getCopyFromRegs(SelectionDAG &DAG, FunctionLoweringInfo &FuncInfo,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000651 SDLoc dl,
Bill Wendling12931302012-09-26 04:04:19 +0000652 SDValue &Chain, SDValue *Flag,
653 const Value *V = 0) const;
Dan Gohman462f6b52010-05-29 17:53:24 +0000654
655 /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
656 /// specified value into the registers specified by this object. This uses
657 /// Chain/Flag as the input and updates them for the output Chain/Flag.
658 /// If the Flag pointer is NULL, no flag is used.
Andrew Trickac6d9be2013-05-25 02:42:55 +0000659 void getCopyToRegs(SDValue Val, SelectionDAG &DAG, SDLoc dl,
Bill Wendlingf18eb582012-09-26 06:16:18 +0000660 SDValue &Chain, SDValue *Flag, const Value *V) const;
Dan Gohman462f6b52010-05-29 17:53:24 +0000661
662 /// AddInlineAsmOperands - Add this value to the specified inlineasm node
663 /// operand list. This adds the code marker, matching input operand index
664 /// (if applicable), and includes the number of values added into it.
665 void AddInlineAsmOperands(unsigned Kind,
666 bool HasMatching, unsigned MatchingIdx,
667 SelectionDAG &DAG,
668 std::vector<SDValue> &Ops) const;
669 };
670}
671
672/// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
673/// this value and returns the result as a ValueVT value. This uses
674/// Chain/Flag as the input and updates them for the output Chain/Flag.
675/// If the Flag pointer is NULL, no flag is used.
676SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG,
677 FunctionLoweringInfo &FuncInfo,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000678 SDLoc dl,
Bill Wendling12931302012-09-26 04:04:19 +0000679 SDValue &Chain, SDValue *Flag,
680 const Value *V) const {
Dan Gohman7da5d3f2010-07-26 18:15:41 +0000681 // A Value with type {} or [0 x %t] needs no registers.
682 if (ValueVTs.empty())
683 return SDValue();
684
Dan Gohman462f6b52010-05-29 17:53:24 +0000685 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
686
687 // Assemble the legal parts into the final values.
688 SmallVector<SDValue, 4> Values(ValueVTs.size());
689 SmallVector<SDValue, 8> Parts;
690 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
691 // Copy the legal parts from the registers.
692 EVT ValueVT = ValueVTs[Value];
693 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVT);
Patrik Hagglund8963fec2012-12-19 12:23:01 +0000694 MVT RegisterVT = RegVTs[Value];
Dan Gohman462f6b52010-05-29 17:53:24 +0000695
696 Parts.resize(NumRegs);
697 for (unsigned i = 0; i != NumRegs; ++i) {
698 SDValue P;
699 if (Flag == 0) {
700 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
701 } else {
702 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag);
703 *Flag = P.getValue(2);
704 }
705
706 Chain = P.getValue(1);
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000707 Parts[i] = P;
Dan Gohman462f6b52010-05-29 17:53:24 +0000708
709 // If the source register was virtual and if we know something about it,
710 // add an assert node.
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000711 if (!TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) ||
Cameron Zwariche1497b92011-02-24 10:00:08 +0000712 !RegisterVT.isInteger() || RegisterVT.isVector())
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000713 continue;
Cameron Zwariche1497b92011-02-24 10:00:08 +0000714
715 const FunctionLoweringInfo::LiveOutInfo *LOI =
716 FuncInfo.GetLiveOutRegInfo(Regs[Part+i]);
717 if (!LOI)
718 continue;
Dan Gohman462f6b52010-05-29 17:53:24 +0000719
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000720 unsigned RegSize = RegisterVT.getSizeInBits();
Cameron Zwariche1497b92011-02-24 10:00:08 +0000721 unsigned NumSignBits = LOI->NumSignBits;
722 unsigned NumZeroBits = LOI->KnownZero.countLeadingOnes();
Dan Gohman462f6b52010-05-29 17:53:24 +0000723
Quentin Colombeta3fb49c2013-06-18 20:14:39 +0000724 if (NumZeroBits == RegSize) {
725 // The current value is a zero.
726 // Explicitly express that as it would be easier for
727 // optimizations to kick in.
728 Parts[i] = DAG.getConstant(0, RegisterVT);
729 continue;
730 }
731
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000732 // FIXME: We capture more information than the dag can represent. For
733 // now, just use the tightest assertzext/assertsext possible.
734 bool isSExt = true;
735 EVT FromVT(MVT::Other);
736 if (NumSignBits == RegSize)
737 isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1
738 else if (NumZeroBits >= RegSize-1)
739 isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1
740 else if (NumSignBits > RegSize-8)
741 isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8
742 else if (NumZeroBits >= RegSize-8)
743 isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8
744 else if (NumSignBits > RegSize-16)
745 isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16
746 else if (NumZeroBits >= RegSize-16)
747 isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16
748 else if (NumSignBits > RegSize-32)
749 isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32
750 else if (NumZeroBits >= RegSize-32)
751 isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32
752 else
753 continue;
Dan Gohman462f6b52010-05-29 17:53:24 +0000754
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000755 // Add an assertion node.
756 assert(FromVT != MVT::Other);
757 Parts[i] = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
758 RegisterVT, P, DAG.getValueType(FromVT));
Dan Gohman462f6b52010-05-29 17:53:24 +0000759 }
760
761 Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(),
Bill Wendling12931302012-09-26 04:04:19 +0000762 NumRegs, RegisterVT, ValueVT, V);
Dan Gohman462f6b52010-05-29 17:53:24 +0000763 Part += NumRegs;
764 Parts.clear();
765 }
766
767 return DAG.getNode(ISD::MERGE_VALUES, dl,
768 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
769 &Values[0], ValueVTs.size());
770}
771
772/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
773/// specified value into the registers specified by this object. This uses
774/// Chain/Flag as the input and updates them for the output Chain/Flag.
775/// If the Flag pointer is NULL, no flag is used.
Andrew Trickac6d9be2013-05-25 02:42:55 +0000776void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, SDLoc dl,
Bill Wendlingf18eb582012-09-26 06:16:18 +0000777 SDValue &Chain, SDValue *Flag,
778 const Value *V) const {
Dan Gohman462f6b52010-05-29 17:53:24 +0000779 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
780
781 // Get the list of the values's legal parts.
782 unsigned NumRegs = Regs.size();
783 SmallVector<SDValue, 8> Parts(NumRegs);
784 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
785 EVT ValueVT = ValueVTs[Value];
786 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), ValueVT);
Patrik Hagglund8963fec2012-12-19 12:23:01 +0000787 MVT RegisterVT = RegVTs[Value];
Evan Cheng2766a472012-12-06 19:13:27 +0000788 ISD::NodeType ExtendKind =
789 TLI.isZExtFree(Val, RegisterVT)? ISD::ZERO_EXTEND: ISD::ANY_EXTEND;
Dan Gohman462f6b52010-05-29 17:53:24 +0000790
Chris Lattner3ac18842010-08-24 23:20:40 +0000791 getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value),
Evan Cheng2766a472012-12-06 19:13:27 +0000792 &Parts[Part], NumParts, RegisterVT, V, ExtendKind);
Dan Gohman462f6b52010-05-29 17:53:24 +0000793 Part += NumParts;
794 }
795
796 // Copy the parts into the registers.
797 SmallVector<SDValue, 8> Chains(NumRegs);
798 for (unsigned i = 0; i != NumRegs; ++i) {
799 SDValue Part;
800 if (Flag == 0) {
801 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
802 } else {
803 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag);
804 *Flag = Part.getValue(1);
805 }
806
807 Chains[i] = Part.getValue(0);
808 }
809
810 if (NumRegs == 1 || Flag)
811 // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
812 // flagged to it. That is the CopyToReg nodes and the user are considered
813 // a single scheduling unit. If we create a TokenFactor and return it as
814 // chain, then the TokenFactor is both a predecessor (operand) of the
815 // user as well as a successor (the TF operands are flagged to the user).
816 // c1, f1 = CopyToReg
817 // c2, f2 = CopyToReg
818 // c3 = TokenFactor c1, c2
819 // ...
820 // = op c3, ..., f2
821 Chain = Chains[NumRegs-1];
822 else
823 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs);
824}
825
826/// AddInlineAsmOperands - Add this value to the specified inlineasm node
827/// operand list. This adds the code marker and includes the number of
828/// values added into it.
829void RegsForValue::AddInlineAsmOperands(unsigned Code, bool HasMatching,
830 unsigned MatchingIdx,
831 SelectionDAG &DAG,
832 std::vector<SDValue> &Ops) const {
833 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
834
835 unsigned Flag = InlineAsm::getFlagWord(Code, Regs.size());
836 if (HasMatching)
837 Flag = InlineAsm::getFlagWordForMatchingOp(Flag, MatchingIdx);
Jakob Stoklund Olesen459b74b2011-10-12 23:37:29 +0000838 else if (!Regs.empty() &&
839 TargetRegisterInfo::isVirtualRegister(Regs.front())) {
840 // Put the register class of the virtual registers in the flag word. That
841 // way, later passes can recompute register class constraints for inline
842 // assembly as well as normal instructions.
843 // Don't do this for tied operands that can use the regclass information
844 // from the def.
845 const MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
846 const TargetRegisterClass *RC = MRI.getRegClass(Regs.front());
847 Flag = InlineAsm::getFlagWordForRegClass(Flag, RC->getID());
848 }
849
Dan Gohman462f6b52010-05-29 17:53:24 +0000850 SDValue Res = DAG.getTargetConstant(Flag, MVT::i32);
851 Ops.push_back(Res);
852
853 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
854 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVTs[Value]);
Patrik Hagglund8963fec2012-12-19 12:23:01 +0000855 MVT RegisterVT = RegVTs[Value];
Dan Gohman462f6b52010-05-29 17:53:24 +0000856 for (unsigned i = 0; i != NumRegs; ++i) {
857 assert(Reg < Regs.size() && "Mismatch in # registers expected");
858 Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT));
859 }
860 }
861}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000862
Owen Anderson243eb9e2011-12-08 22:15:21 +0000863void SelectionDAGBuilder::init(GCFunctionInfo *gfi, AliasAnalysis &aa,
864 const TargetLibraryInfo *li) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000865 AA = &aa;
866 GFI = gfi;
Owen Anderson243eb9e2011-12-08 22:15:21 +0000867 LibInfo = li;
Micah Villmow3574eca2012-10-08 16:38:25 +0000868 TD = DAG.getTarget().getDataLayout();
Richard Smithcb1f68d2012-08-22 00:42:39 +0000869 Context = DAG.getContext();
Bill Wendling4ed1fb02011-10-15 01:00:26 +0000870 LPadToCallSiteMap.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000871}
872
Dan Gohmanb02b62a2010-04-14 18:24:06 +0000873/// clear - Clear out the current SelectionDAG and the associated
Dan Gohman2048b852009-11-23 18:04:58 +0000874/// state and prepare this SelectionDAGBuilder object to be used
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000875/// for a new block. This doesn't clear out information about
876/// additional blocks that are needed to complete switch lowering
877/// or PHI node updating; that information is cleared out as it is
878/// consumed.
Dan Gohman2048b852009-11-23 18:04:58 +0000879void SelectionDAGBuilder::clear() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000880 NodeMap.clear();
Devang Patel9126c0d2010-06-01 19:59:01 +0000881 UnusedArgNodeMap.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000882 PendingLoads.clear();
883 PendingExports.clear();
Andrew Trickea5db0c2013-05-25 02:20:36 +0000884 CurInst = NULL;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000885 HasTailCall = false;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000886}
887
Devang Patel23385752011-05-23 17:44:13 +0000888/// clearDanglingDebugInfo - Clear the dangling debug information
Benjamin Kramerd9b0b022012-06-02 10:20:22 +0000889/// map. This function is separated from the clear so that debug
Devang Patel23385752011-05-23 17:44:13 +0000890/// information that is dangling in a basic block can be properly
891/// resolved in a different basic block. This allows the
892/// SelectionDAG to resolve dangling debug information attached
893/// to PHI nodes.
894void SelectionDAGBuilder::clearDanglingDebugInfo() {
895 DanglingDebugInfoMap.clear();
896}
897
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000898/// getRoot - Return the current virtual root of the Selection DAG,
899/// flushing any PendingLoad items. This must be done before emitting
900/// a store or any other node that may need to be ordered after any
901/// prior load instructions.
902///
Dan Gohman2048b852009-11-23 18:04:58 +0000903SDValue SelectionDAGBuilder::getRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000904 if (PendingLoads.empty())
905 return DAG.getRoot();
906
907 if (PendingLoads.size() == 1) {
908 SDValue Root = PendingLoads[0];
909 DAG.setRoot(Root);
910 PendingLoads.clear();
911 return Root;
912 }
913
914 // Otherwise, we have to make a token factor node.
Andrew Trickac6d9be2013-05-25 02:42:55 +0000915 SDValue Root = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000916 &PendingLoads[0], PendingLoads.size());
917 PendingLoads.clear();
918 DAG.setRoot(Root);
919 return Root;
920}
921
922/// getControlRoot - Similar to getRoot, but instead of flushing all the
923/// PendingLoad items, flush all the PendingExports items. It is necessary
924/// to do this before emitting a terminator instruction.
925///
Dan Gohman2048b852009-11-23 18:04:58 +0000926SDValue SelectionDAGBuilder::getControlRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000927 SDValue Root = DAG.getRoot();
928
929 if (PendingExports.empty())
930 return Root;
931
932 // Turn all of the CopyToReg chains into one factored node.
933 if (Root.getOpcode() != ISD::EntryToken) {
934 unsigned i = 0, e = PendingExports.size();
935 for (; i != e; ++i) {
936 assert(PendingExports[i].getNode()->getNumOperands() > 1);
937 if (PendingExports[i].getNode()->getOperand(0) == Root)
938 break; // Don't add the root if we already indirectly depend on it.
939 }
940
941 if (i == e)
942 PendingExports.push_back(Root);
943 }
944
Andrew Trickac6d9be2013-05-25 02:42:55 +0000945 Root = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000946 &PendingExports[0],
947 PendingExports.size());
948 PendingExports.clear();
949 DAG.setRoot(Root);
950 return Root;
951}
952
Dan Gohman46510a72010-04-15 01:51:59 +0000953void SelectionDAGBuilder::visit(const Instruction &I) {
Dan Gohmanc105a2b2010-04-22 20:55:53 +0000954 // Set up outgoing PHI node register values before emitting the terminator.
955 if (isa<TerminatorInst>(&I))
956 HandlePHINodesInSuccessorBlocks(I.getParent());
957
Andrew Trickdd0fb012013-05-25 03:08:10 +0000958 ++SDNodeOrder;
959
Andrew Trickea5db0c2013-05-25 02:20:36 +0000960 CurInst = &I;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000961
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000962 visit(I.getOpcode(), I);
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000963
Dan Gohman92884f72010-04-20 15:03:56 +0000964 if (!isa<TerminatorInst>(&I) && !HasTailCall)
965 CopyToExportRegsIfNeeded(&I);
966
Andrew Trickea5db0c2013-05-25 02:20:36 +0000967 CurInst = NULL;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000968}
969
Dan Gohmanba5be5c2010-04-20 15:00:41 +0000970void SelectionDAGBuilder::visitPHI(const PHINode &) {
971 llvm_unreachable("SelectionDAGBuilder shouldn't visit PHI nodes!");
972}
973
Dan Gohman46510a72010-04-15 01:51:59 +0000974void SelectionDAGBuilder::visit(unsigned Opcode, const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000975 // Note: this doesn't use InstVisitor, because it has to work with
976 // ConstantExpr's in addition to instructions.
977 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000978 default: llvm_unreachable("Unknown instruction type encountered!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000979 // Build the switch statement using the Instruction.def file.
980#define HANDLE_INST(NUM, OPCODE, CLASS) \
Galina Kistanova72ea0c92012-07-19 04:50:12 +0000981 case Instruction::OPCODE: visit##OPCODE((const CLASS&)I); break;
Chandler Carruth0b8c9a82013-01-02 11:36:10 +0000982#include "llvm/IR/Instruction.def"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000983 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000984}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000985
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000986// resolveDanglingDebugInfo - if we saw an earlier dbg_value referring to V,
987// generate the debug data structures now that we've seen its definition.
988void SelectionDAGBuilder::resolveDanglingDebugInfo(const Value *V,
989 SDValue Val) {
990 DanglingDebugInfo &DDI = DanglingDebugInfoMap[V];
Devang Patel4cf81c42010-08-26 23:35:15 +0000991 if (DDI.getDI()) {
992 const DbgValueInst *DI = DDI.getDI();
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000993 DebugLoc dl = DDI.getdl();
994 unsigned DbgSDNodeOrder = DDI.getSDNodeOrder();
Devang Patel4cf81c42010-08-26 23:35:15 +0000995 MDNode *Variable = DI->getVariable();
996 uint64_t Offset = DI->getOffset();
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000997 SDDbgValue *SDV;
998 if (Val.getNode()) {
Devang Patel78a06e52010-08-25 20:39:26 +0000999 if (!EmitFuncArgumentDbgValue(V, Variable, Offset, Val)) {
Dale Johannesenbdc09d92010-07-16 00:02:08 +00001000 SDV = DAG.getDbgValue(Variable, Val.getNode(),
1001 Val.getResNo(), Offset, dl, DbgSDNodeOrder);
1002 DAG.AddDbgValue(SDV, Val.getNode(), false);
1003 }
Owen Anderson95771af2011-02-25 21:41:48 +00001004 } else
Adrian Prantl5da4e4f2013-05-22 18:02:19 +00001005 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
Dale Johannesenbdc09d92010-07-16 00:02:08 +00001006 DanglingDebugInfoMap[V] = DanglingDebugInfo();
1007 }
1008}
1009
Nick Lewycky8de34002011-09-30 22:19:53 +00001010/// getValue - Return an SDValue for the given Value.
Dan Gohman2048b852009-11-23 18:04:58 +00001011SDValue SelectionDAGBuilder::getValue(const Value *V) {
Dan Gohman28a17352010-07-01 01:59:43 +00001012 // If we already have an SDValue for this value, use it. It's important
1013 // to do this first, so that we don't create a CopyFromReg if we already
1014 // have a regular SDValue.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001015 SDValue &N = NodeMap[V];
1016 if (N.getNode()) return N;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001017
Dan Gohman28a17352010-07-01 01:59:43 +00001018 // If there's a virtual register allocated and initialized for this
1019 // value, use it.
1020 DenseMap<const Value *, unsigned>::iterator It = FuncInfo.ValueMap.find(V);
1021 if (It != FuncInfo.ValueMap.end()) {
1022 unsigned InReg = It->second;
Bill Wendlingba54bca2013-06-19 21:36:55 +00001023 RegsForValue RFV(*DAG.getContext(), *TM.getTargetLowering(),
1024 InReg, V->getType());
Dan Gohman28a17352010-07-01 01:59:43 +00001025 SDValue Chain = DAG.getEntryNode();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001026 N = RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, NULL, V);
Devang Patel8f314282011-01-25 18:09:58 +00001027 resolveDanglingDebugInfo(V, N);
1028 return N;
Dan Gohman28a17352010-07-01 01:59:43 +00001029 }
1030
1031 // Otherwise create a new SDValue and remember it.
1032 SDValue Val = getValueImpl(V);
1033 NodeMap[V] = Val;
Dale Johannesenbdc09d92010-07-16 00:02:08 +00001034 resolveDanglingDebugInfo(V, Val);
Dan Gohman28a17352010-07-01 01:59:43 +00001035 return Val;
1036}
1037
1038/// getNonRegisterValue - Return an SDValue for the given Value, but
1039/// don't look in FuncInfo.ValueMap for a virtual register.
1040SDValue SelectionDAGBuilder::getNonRegisterValue(const Value *V) {
1041 // If we already have an SDValue for this value, use it.
1042 SDValue &N = NodeMap[V];
1043 if (N.getNode()) return N;
1044
1045 // Otherwise create a new SDValue and remember it.
1046 SDValue Val = getValueImpl(V);
1047 NodeMap[V] = Val;
Dale Johannesenbdc09d92010-07-16 00:02:08 +00001048 resolveDanglingDebugInfo(V, Val);
Dan Gohman28a17352010-07-01 01:59:43 +00001049 return Val;
1050}
1051
Dale Johannesenbdc09d92010-07-16 00:02:08 +00001052/// getValueImpl - Helper function for getValue and getNonRegisterValue.
Dan Gohman28a17352010-07-01 01:59:43 +00001053/// Create an SDValue for the given value.
1054SDValue SelectionDAGBuilder::getValueImpl(const Value *V) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00001055 const TargetLowering *TLI = TM.getTargetLowering();
1056
Dan Gohman383b5f62010-04-17 15:32:28 +00001057 if (const Constant *C = dyn_cast<Constant>(V)) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00001058 EVT VT = TLI->getValueType(V->getType(), true);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001059
Dan Gohman383b5f62010-04-17 15:32:28 +00001060 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C))
Dan Gohman28a17352010-07-01 01:59:43 +00001061 return DAG.getConstant(*CI, VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001062
Dan Gohman383b5f62010-04-17 15:32:28 +00001063 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001064 return DAG.getGlobalAddress(GV, getCurSDLoc(), VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001065
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001066 if (isa<ConstantPointerNull>(C))
Bill Wendlingba54bca2013-06-19 21:36:55 +00001067 return DAG.getConstant(0, TLI->getPointerTy());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001068
Dan Gohman383b5f62010-04-17 15:32:28 +00001069 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Dan Gohman28a17352010-07-01 01:59:43 +00001070 return DAG.getConstantFP(*CFP, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001071
Nate Begeman9008ca62009-04-27 18:41:29 +00001072 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
Dan Gohman28a17352010-07-01 01:59:43 +00001073 return DAG.getUNDEF(VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001074
Dan Gohman383b5f62010-04-17 15:32:28 +00001075 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001076 visit(CE->getOpcode(), *CE);
1077 SDValue N1 = NodeMap[V];
Dan Gohmanac7d05c2010-04-16 16:55:18 +00001078 assert(N1.getNode() && "visit didn't populate the NodeMap!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001079 return N1;
1080 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001081
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001082 if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
1083 SmallVector<SDValue, 4> Constants;
1084 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
1085 OI != OE; ++OI) {
1086 SDNode *Val = getValue(*OI).getNode();
Dan Gohmaned48caf2009-09-08 01:44:02 +00001087 // If the operand is an empty aggregate, there are no values.
1088 if (!Val) continue;
1089 // Add each leaf value from the operand to the Constants list
1090 // to form a flattened list of all the values.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001091 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
1092 Constants.push_back(SDValue(Val, i));
1093 }
Bill Wendling87710f02009-12-21 23:47:40 +00001094
Bill Wendling4533cac2010-01-28 21:51:40 +00001095 return DAG.getMergeValues(&Constants[0], Constants.size(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001096 getCurSDLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001097 }
Stephen Lin155615d2013-07-08 00:37:03 +00001098
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001099 if (const ConstantDataSequential *CDS =
1100 dyn_cast<ConstantDataSequential>(C)) {
1101 SmallVector<SDValue, 4> Ops;
Chris Lattner0f193b82012-01-25 01:27:20 +00001102 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001103 SDNode *Val = getValue(CDS->getElementAsConstant(i)).getNode();
1104 // Add each leaf value from the operand to the Constants list
1105 // to form a flattened list of all the values.
1106 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
1107 Ops.push_back(SDValue(Val, i));
1108 }
1109
1110 if (isa<ArrayType>(CDS->getType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001111 return DAG.getMergeValues(&Ops[0], Ops.size(), getCurSDLoc());
1112 return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurSDLoc(),
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001113 VT, &Ops[0], Ops.size());
1114 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001115
Duncan Sands1df98592010-02-16 11:11:14 +00001116 if (C->getType()->isStructTy() || C->getType()->isArrayTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001117 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
1118 "Unknown struct or array constant!");
1119
Owen Andersone50ed302009-08-10 22:56:29 +00001120 SmallVector<EVT, 4> ValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00001121 ComputeValueVTs(*TLI, C->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001122 unsigned NumElts = ValueVTs.size();
1123 if (NumElts == 0)
1124 return SDValue(); // empty struct
1125 SmallVector<SDValue, 4> Constants(NumElts);
1126 for (unsigned i = 0; i != NumElts; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00001127 EVT EltVT = ValueVTs[i];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001128 if (isa<UndefValue>(C))
Dale Johannesene8d72302009-02-06 23:05:02 +00001129 Constants[i] = DAG.getUNDEF(EltVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001130 else if (EltVT.isFloatingPoint())
1131 Constants[i] = DAG.getConstantFP(0, EltVT);
1132 else
1133 Constants[i] = DAG.getConstant(0, EltVT);
1134 }
Bill Wendling87710f02009-12-21 23:47:40 +00001135
Bill Wendling4533cac2010-01-28 21:51:40 +00001136 return DAG.getMergeValues(&Constants[0], NumElts,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001137 getCurSDLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001138 }
1139
Dan Gohman383b5f62010-04-17 15:32:28 +00001140 if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
Dan Gohman29cbade2009-11-20 23:18:13 +00001141 return DAG.getBlockAddress(BA, VT);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001142
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001143 VectorType *VecTy = cast<VectorType>(V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001144 unsigned NumElements = VecTy->getNumElements();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001145
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001146 // Now that we know the number and type of the elements, get that number of
1147 // elements into the Ops array based on what kind of constant it is.
1148 SmallVector<SDValue, 16> Ops;
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001149 if (const ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001150 for (unsigned i = 0; i != NumElements; ++i)
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001151 Ops.push_back(getValue(CV->getOperand(i)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001152 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00001153 assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!");
Bill Wendlingba54bca2013-06-19 21:36:55 +00001154 EVT EltVT = TLI->getValueType(VecTy->getElementType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001155
1156 SDValue Op;
Nate Begeman9008ca62009-04-27 18:41:29 +00001157 if (EltVT.isFloatingPoint())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001158 Op = DAG.getConstantFP(0, EltVT);
1159 else
1160 Op = DAG.getConstant(0, EltVT);
1161 Ops.assign(NumElements, Op);
1162 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001163
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001164 // Create a BUILD_VECTOR node.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001165 return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00001166 VT, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001167 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001168
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001169 // If this is a static alloca, generate it as the frameindex instead of
1170 // computation.
1171 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
1172 DenseMap<const AllocaInst*, int>::iterator SI =
1173 FuncInfo.StaticAllocaMap.find(AI);
1174 if (SI != FuncInfo.StaticAllocaMap.end())
Bill Wendlingba54bca2013-06-19 21:36:55 +00001175 return DAG.getFrameIndex(SI->second, TLI->getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001176 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001177
Dan Gohman28a17352010-07-01 01:59:43 +00001178 // If this is an instruction which fast-isel has deferred, select it now.
1179 if (const Instruction *Inst = dyn_cast<Instruction>(V)) {
Dan Gohman84023e02010-07-10 09:00:22 +00001180 unsigned InReg = FuncInfo.InitializeRegForValue(Inst);
Bill Wendlingba54bca2013-06-19 21:36:55 +00001181 RegsForValue RFV(*DAG.getContext(), *TLI, InReg, Inst->getType());
Dan Gohman84023e02010-07-10 09:00:22 +00001182 SDValue Chain = DAG.getEntryNode();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001183 return RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, NULL, V);
Dan Gohman28a17352010-07-01 01:59:43 +00001184 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001185
Dan Gohman28a17352010-07-01 01:59:43 +00001186 llvm_unreachable("Can't get register for value!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001187}
1188
Dan Gohman46510a72010-04-15 01:51:59 +00001189void SelectionDAGBuilder::visitRet(const ReturnInst &I) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00001190 const TargetLowering *TLI = TM.getTargetLowering();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001191 SDValue Chain = getControlRoot();
1192 SmallVector<ISD::OutputArg, 8> Outs;
Dan Gohmanc9403652010-07-07 15:54:55 +00001193 SmallVector<SDValue, 8> OutVals;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001194
Dan Gohman7451d3e2010-05-29 17:03:36 +00001195 if (!FuncInfo.CanLowerReturn) {
1196 unsigned DemoteReg = FuncInfo.DemoteRegister;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001197 const Function *F = I.getParent()->getParent();
1198
1199 // Emit a store of the return value through the virtual register.
1200 // Leave Outs empty so that LowerReturn won't try to load return
1201 // registers the usual way.
1202 SmallVector<EVT, 1> PtrValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00001203 ComputeValueVTs(*TLI, PointerType::getUnqual(F->getReturnType()),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001204 PtrValueVTs);
1205
1206 SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]);
1207 SDValue RetOp = getValue(I.getOperand(0));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001208
Owen Andersone50ed302009-08-10 22:56:29 +00001209 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001210 SmallVector<uint64_t, 4> Offsets;
Bill Wendlingba54bca2013-06-19 21:36:55 +00001211 ComputeValueVTs(*TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets);
Dan Gohman7ea1ca62008-10-21 20:00:42 +00001212 unsigned NumValues = ValueVTs.size();
Dan Gohman7ea1ca62008-10-21 20:00:42 +00001213
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001214 SmallVector<SDValue, 4> Chains(NumValues);
Bill Wendling87710f02009-12-21 23:47:40 +00001215 for (unsigned i = 0; i != NumValues; ++i) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001216 SDValue Add = DAG.getNode(ISD::ADD, getCurSDLoc(),
Chris Lattnera13b8602010-08-24 23:10:06 +00001217 RetPtr.getValueType(), RetPtr,
1218 DAG.getIntPtrConstant(Offsets[i]));
Bill Wendling87710f02009-12-21 23:47:40 +00001219 Chains[i] =
Andrew Trickac6d9be2013-05-25 02:42:55 +00001220 DAG.getStore(Chain, getCurSDLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001221 SDValue(RetOp.getNode(), RetOp.getResNo() + i),
Chris Lattner84bd98a2010-09-21 18:58:22 +00001222 // FIXME: better loc info would be nice.
1223 Add, MachinePointerInfo(), false, false, 0);
Bill Wendling87710f02009-12-21 23:47:40 +00001224 }
1225
Andrew Trickac6d9be2013-05-25 02:42:55 +00001226 Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001227 MVT::Other, &Chains[0], NumValues);
Chris Lattner25d58372010-02-28 18:53:13 +00001228 } else if (I.getNumOperands() != 0) {
1229 SmallVector<EVT, 4> ValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00001230 ComputeValueVTs(*TLI, I.getOperand(0)->getType(), ValueVTs);
Chris Lattner25d58372010-02-28 18:53:13 +00001231 unsigned NumValues = ValueVTs.size();
1232 if (NumValues) {
1233 SDValue RetOp = getValue(I.getOperand(0));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001234 for (unsigned j = 0, f = NumValues; j != f; ++j) {
1235 EVT VT = ValueVTs[j];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001236
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001237 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001238
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001239 const Function *F = I.getParent()->getParent();
Bill Wendling8b62abd2012-12-30 13:01:51 +00001240 if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
1241 Attribute::SExt))
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001242 ExtendKind = ISD::SIGN_EXTEND;
Bill Wendling8b62abd2012-12-30 13:01:51 +00001243 else if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
1244 Attribute::ZExt))
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001245 ExtendKind = ISD::ZERO_EXTEND;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001246
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001247 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger())
Bill Wendlingba54bca2013-06-19 21:36:55 +00001248 VT = TLI->getTypeForExtArgOrReturn(VT.getSimpleVT(), ExtendKind);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001249
Bill Wendlingba54bca2013-06-19 21:36:55 +00001250 unsigned NumParts = TLI->getNumRegisters(*DAG.getContext(), VT);
1251 MVT PartVT = TLI->getRegisterType(*DAG.getContext(), VT);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001252 SmallVector<SDValue, 4> Parts(NumParts);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001253 getCopyToParts(DAG, getCurSDLoc(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001254 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
Bill Wendlingf18eb582012-09-26 06:16:18 +00001255 &Parts[0], NumParts, PartVT, &I, ExtendKind);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001256
1257 // 'inreg' on function refers to return value
1258 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
Bill Wendling8b62abd2012-12-30 13:01:51 +00001259 if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
1260 Attribute::InReg))
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001261 Flags.setInReg();
1262
1263 // Propagate extension type if any
Cameron Zwarich8df6bf52011-03-16 22:20:07 +00001264 if (ExtendKind == ISD::SIGN_EXTEND)
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001265 Flags.setSExt();
Cameron Zwarich8df6bf52011-03-16 22:20:07 +00001266 else if (ExtendKind == ISD::ZERO_EXTEND)
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001267 Flags.setZExt();
1268
Dan Gohmanc9403652010-07-07 15:54:55 +00001269 for (unsigned i = 0; i < NumParts; ++i) {
1270 Outs.push_back(ISD::OutputArg(Flags, Parts[i].getValueType(),
Manman Ren0a1544d2012-11-01 23:49:58 +00001271 /*isfixed=*/true, 0, 0));
Dan Gohmanc9403652010-07-07 15:54:55 +00001272 OutVals.push_back(Parts[i]);
1273 }
Evan Cheng3927f432009-03-25 20:20:11 +00001274 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001275 }
1276 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001277
1278 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001279 CallingConv::ID CallConv =
1280 DAG.getMachineFunction().getFunction()->getCallingConv();
Bill Wendlingba54bca2013-06-19 21:36:55 +00001281 Chain = TM.getTargetLowering()->LowerReturn(Chain, CallConv, isVarArg,
1282 Outs, OutVals, getCurSDLoc(),
1283 DAG);
Dan Gohman5e866062009-08-06 15:37:27 +00001284
1285 // Verify that the target's LowerReturn behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00001286 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00001287 "LowerReturn didn't return a valid chain!");
1288
1289 // Update the DAG with the new chain value resulting from return lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001290 DAG.setRoot(Chain);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001291}
1292
Dan Gohmanad62f532009-04-23 23:13:24 +00001293/// CopyToExportRegsIfNeeded - If the given value has virtual registers
1294/// created for it, emit nodes to copy the value into the virtual
1295/// registers.
Dan Gohman46510a72010-04-15 01:51:59 +00001296void SelectionDAGBuilder::CopyToExportRegsIfNeeded(const Value *V) {
Rafael Espindola3fa82832011-05-13 15:18:06 +00001297 // Skip empty types
1298 if (V->getType()->isEmptyTy())
1299 return;
1300
Dan Gohman33b7a292010-04-16 17:15:02 +00001301 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
1302 if (VMI != FuncInfo.ValueMap.end()) {
1303 assert(!V->use_empty() && "Unused value assigned virtual registers!");
1304 CopyValueToVirtualRegister(V, VMI->second);
Dan Gohmanad62f532009-04-23 23:13:24 +00001305 }
1306}
1307
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001308/// ExportFromCurrentBlock - If this condition isn't known to be exported from
1309/// the current basic block, add it to ValueMap now so that we'll get a
1310/// CopyTo/FromReg.
Dan Gohman46510a72010-04-15 01:51:59 +00001311void SelectionDAGBuilder::ExportFromCurrentBlock(const Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001312 // No need to export constants.
1313 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001314
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001315 // Already exported?
1316 if (FuncInfo.isExportedInst(V)) return;
1317
1318 unsigned Reg = FuncInfo.InitializeRegForValue(V);
1319 CopyValueToVirtualRegister(V, Reg);
1320}
1321
Dan Gohman46510a72010-04-15 01:51:59 +00001322bool SelectionDAGBuilder::isExportableFromCurrentBlock(const Value *V,
Dan Gohman2048b852009-11-23 18:04:58 +00001323 const BasicBlock *FromBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001324 // The operands of the setcc have to be in this block. We don't know
1325 // how to export them from some other block.
Dan Gohman46510a72010-04-15 01:51:59 +00001326 if (const Instruction *VI = dyn_cast<Instruction>(V)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001327 // Can export from current BB.
1328 if (VI->getParent() == FromBB)
1329 return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001330
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001331 // Is already exported, noop.
1332 return FuncInfo.isExportedInst(V);
1333 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001334
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001335 // If this is an argument, we can export it if the BB is the entry block or
1336 // if it is already exported.
1337 if (isa<Argument>(V)) {
1338 if (FromBB == &FromBB->getParent()->getEntryBlock())
1339 return true;
1340
1341 // Otherwise, can only export this if it is already exported.
1342 return FuncInfo.isExportedInst(V);
1343 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001344
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001345 // Otherwise, constants can always be exported.
1346 return true;
1347}
1348
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001349/// Return branch probability calculated by BranchProbabilityInfo for IR blocks.
Jakub Staszak25101bb2011-12-20 20:03:10 +00001350uint32_t SelectionDAGBuilder::getEdgeWeight(const MachineBasicBlock *Src,
1351 const MachineBasicBlock *Dst) const {
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001352 BranchProbabilityInfo *BPI = FuncInfo.BPI;
1353 if (!BPI)
1354 return 0;
Jakub Staszak95ece8e2011-07-29 20:05:36 +00001355 const BasicBlock *SrcBB = Src->getBasicBlock();
1356 const BasicBlock *DstBB = Dst->getBasicBlock();
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001357 return BPI->getEdgeWeight(SrcBB, DstBB);
1358}
1359
Jakub Staszakc8f34de2011-07-29 22:25:21 +00001360void SelectionDAGBuilder::
1361addSuccessorWithWeight(MachineBasicBlock *Src, MachineBasicBlock *Dst,
1362 uint32_t Weight /* = 0 */) {
1363 if (!Weight)
1364 Weight = getEdgeWeight(Src, Dst);
1365 Src->addSuccessor(Dst, Weight);
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001366}
1367
1368
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001369static bool InBlock(const Value *V, const BasicBlock *BB) {
1370 if (const Instruction *I = dyn_cast<Instruction>(V))
1371 return I->getParent() == BB;
1372 return true;
1373}
1374
Dan Gohmanc2277342008-10-17 21:16:08 +00001375/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
1376/// This function emits a branch and is used at the leaves of an OR or an
1377/// AND operator tree.
1378///
1379void
Dan Gohman46510a72010-04-15 01:51:59 +00001380SelectionDAGBuilder::EmitBranchForMergedCondition(const Value *Cond,
Dan Gohman2048b852009-11-23 18:04:58 +00001381 MachineBasicBlock *TBB,
1382 MachineBasicBlock *FBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001383 MachineBasicBlock *CurBB,
1384 MachineBasicBlock *SwitchBB) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001385 const BasicBlock *BB = CurBB->getBasicBlock();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001386
Dan Gohmanc2277342008-10-17 21:16:08 +00001387 // If the leaf of the tree is a comparison, merge the condition into
1388 // the caseblock.
Dan Gohman46510a72010-04-15 01:51:59 +00001389 if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001390 // The operands of the cmp have to be in this block. We don't know
1391 // how to export them from some other block. If this is the first block
1392 // of the sequence, no exporting is needed.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001393 if (CurBB == SwitchBB ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001394 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
1395 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001396 ISD::CondCode Condition;
Dan Gohman46510a72010-04-15 01:51:59 +00001397 if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001398 Condition = getICmpCondCode(IC->getPredicate());
Dan Gohman46510a72010-04-15 01:51:59 +00001399 } else if (const FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001400 Condition = getFCmpCondCode(FC->getPredicate());
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001401 if (TM.Options.NoNaNsFPMath)
1402 Condition = getFCmpCodeWithoutNaN(Condition);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001403 } else {
1404 Condition = ISD::SETEQ; // silence warning.
Torok Edwinc23197a2009-07-14 16:55:14 +00001405 llvm_unreachable("Unknown compare instruction");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001406 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001407
1408 CaseBlock CB(Condition, BOp->getOperand(0),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001409 BOp->getOperand(1), NULL, TBB, FBB, CurBB);
1410 SwitchCases.push_back(CB);
1411 return;
1412 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001413 }
1414
1415 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001416 CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanc2277342008-10-17 21:16:08 +00001417 NULL, TBB, FBB, CurBB);
1418 SwitchCases.push_back(CB);
1419}
1420
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001421/// FindMergedConditions - If Cond is an expression like
Dan Gohman46510a72010-04-15 01:51:59 +00001422void SelectionDAGBuilder::FindMergedConditions(const Value *Cond,
Dan Gohman2048b852009-11-23 18:04:58 +00001423 MachineBasicBlock *TBB,
1424 MachineBasicBlock *FBB,
1425 MachineBasicBlock *CurBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001426 MachineBasicBlock *SwitchBB,
Dan Gohman2048b852009-11-23 18:04:58 +00001427 unsigned Opc) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001428 // If this node is not part of the or/and tree, emit it as a branch.
Dan Gohman46510a72010-04-15 01:51:59 +00001429 const Instruction *BOp = dyn_cast<Instruction>(Cond);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001430 if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001431 (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
1432 BOp->getParent() != CurBB->getBasicBlock() ||
1433 !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1434 !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001435 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001436 return;
1437 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001438
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001439 // Create TmpBB after CurBB.
1440 MachineFunction::iterator BBI = CurBB;
1441 MachineFunction &MF = DAG.getMachineFunction();
1442 MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
1443 CurBB->getParent()->insert(++BBI, TmpBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001444
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001445 if (Opc == Instruction::Or) {
1446 // Codegen X | Y as:
1447 // jmp_if_X TBB
1448 // jmp TmpBB
1449 // TmpBB:
1450 // jmp_if_Y TBB
1451 // jmp FBB
1452 //
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001453
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001454 // Emit the LHS condition.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001455 FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, SwitchBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001456
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001457 // Emit the RHS condition into TmpBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001458 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001459 } else {
1460 assert(Opc == Instruction::And && "Unknown merge op!");
1461 // Codegen X & Y as:
1462 // jmp_if_X TmpBB
1463 // jmp FBB
1464 // TmpBB:
1465 // jmp_if_Y TBB
1466 // jmp FBB
1467 //
1468 // This requires creation of TmpBB after CurBB.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001469
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001470 // Emit the LHS condition.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001471 FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, SwitchBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001472
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001473 // Emit the RHS condition into TmpBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001474 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001475 }
1476}
1477
1478/// If the set of cases should be emitted as a series of branches, return true.
1479/// If we should emit this as a bunch of and/or'd together conditions, return
1480/// false.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001481bool
Stephen Lin09f8ca32013-07-06 21:44:25 +00001482SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001483 if (Cases.size() != 2) return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001484
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001485 // If this is two comparisons of the same values or'd or and'd together, they
1486 // will get folded into a single comparison, so don't emit two blocks.
1487 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1488 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1489 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1490 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1491 return false;
1492 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001493
Chris Lattner133ce872010-01-02 00:00:03 +00001494 // Handle: (X != null) | (Y != null) --> (X|Y) != 0
1495 // Handle: (X == null) & (Y == null) --> (X|Y) == 0
1496 if (Cases[0].CmpRHS == Cases[1].CmpRHS &&
1497 Cases[0].CC == Cases[1].CC &&
1498 isa<Constant>(Cases[0].CmpRHS) &&
1499 cast<Constant>(Cases[0].CmpRHS)->isNullValue()) {
1500 if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB)
1501 return false;
1502 if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB)
1503 return false;
1504 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00001505
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001506 return true;
1507}
1508
Dan Gohman46510a72010-04-15 01:51:59 +00001509void SelectionDAGBuilder::visitBr(const BranchInst &I) {
Dan Gohman84023e02010-07-10 09:00:22 +00001510 MachineBasicBlock *BrMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001511
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001512 // Update machine-CFG edges.
1513 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1514
1515 // Figure out which block is immediately after the current one.
1516 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001517 MachineFunction::iterator BBI = BrMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001518 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001519 NextBlock = BBI;
1520
1521 if (I.isUnconditional()) {
1522 // Update machine-CFG edges.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001523 BrMBB->addSuccessor(Succ0MBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001524
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001525 // If this is not a fall-through branch, emit the branch.
Bill Wendling4533cac2010-01-28 21:51:40 +00001526 if (Succ0MBB != NextBlock)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001527 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001528 MVT::Other, getControlRoot(),
Bill Wendling4533cac2010-01-28 21:51:40 +00001529 DAG.getBasicBlock(Succ0MBB)));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001530
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001531 return;
1532 }
1533
1534 // If this condition is one of the special cases we handle, do special stuff
1535 // now.
Dan Gohman46510a72010-04-15 01:51:59 +00001536 const Value *CondVal = I.getCondition();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001537 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1538
1539 // If this is a series of conditions that are or'd or and'd together, emit
1540 // this as a sequence of branches instead of setcc's with and/or operations.
Chris Lattnerde189be2010-11-30 18:12:52 +00001541 // As long as jumps are not expensive, this should improve performance.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001542 // For example, instead of something like:
1543 // cmp A, B
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001544 // C = seteq
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001545 // cmp D, E
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001546 // F = setle
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001547 // or C, F
1548 // jnz foo
1549 // Emit:
1550 // cmp A, B
1551 // je foo
1552 // cmp D, E
1553 // jle foo
1554 //
Dan Gohman46510a72010-04-15 01:51:59 +00001555 if (const BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00001556 if (!TM.getTargetLowering()->isJumpExpensive() &&
Chris Lattnerde189be2010-11-30 18:12:52 +00001557 BOp->hasOneUse() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001558 (BOp->getOpcode() == Instruction::And ||
1559 BOp->getOpcode() == Instruction::Or)) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001560 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, BrMBB, BrMBB,
1561 BOp->getOpcode());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001562 // If the compares in later blocks need to use values not currently
1563 // exported from this block, export them now. This block should always
1564 // be the first entry.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001565 assert(SwitchCases[0].ThisBB == BrMBB && "Unexpected lowering!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001566
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001567 // Allow some cases to be rejected.
1568 if (ShouldEmitAsBranches(SwitchCases)) {
1569 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1570 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1571 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1572 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001573
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001574 // Emit the branch for this block.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001575 visitSwitchCase(SwitchCases[0], BrMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001576 SwitchCases.erase(SwitchCases.begin());
1577 return;
1578 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001579
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001580 // Okay, we decided not to do this, remove any inserted MBB's and clear
1581 // SwitchCases.
1582 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001583 FuncInfo.MF->erase(SwitchCases[i].ThisBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001584
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001585 SwitchCases.clear();
1586 }
1587 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001588
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001589 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001590 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohman99be8ae2010-04-19 22:41:47 +00001591 NULL, Succ0MBB, Succ1MBB, BrMBB);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001592
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001593 // Use visitSwitchCase to actually insert the fast branch sequence for this
1594 // cond branch.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001595 visitSwitchCase(CB, BrMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001596}
1597
1598/// visitSwitchCase - Emits the necessary code to represent a single node in
1599/// the binary search tree resulting from lowering a switch instruction.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001600void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB,
1601 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001602 SDValue Cond;
1603 SDValue CondLHS = getValue(CB.CmpLHS);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001604 SDLoc dl = getCurSDLoc();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001605
1606 // Build the setcc now.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001607 if (CB.CmpMHS == NULL) {
1608 // Fold "(X == true)" to X and "(X == false)" to !X to
1609 // handle common cases produced by branch lowering.
Owen Anderson5defacc2009-07-31 17:39:07 +00001610 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001611 CB.CC == ISD::SETEQ)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001612 Cond = CondLHS;
Owen Anderson5defacc2009-07-31 17:39:07 +00001613 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001614 CB.CC == ISD::SETEQ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001615 SDValue True = DAG.getConstant(1, CondLHS.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001616 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001617 } else
Owen Anderson825b72b2009-08-11 20:47:22 +00001618 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001619 } else {
Stepan Dyatkovskiyc187df22012-05-17 08:56:30 +00001620 assert(CB.CC == ISD::SETCC_INVALID &&
1621 "Condition is undefined for to-the-range belonging check.");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001622
Anton Korobeynikov23218582008-12-23 22:25:27 +00001623 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
1624 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001625
1626 SDValue CmpOp = getValue(CB.CmpMHS);
Owen Andersone50ed302009-08-10 22:56:29 +00001627 EVT VT = CmpOp.getValueType();
Stephen Lin155615d2013-07-08 00:37:03 +00001628
Stepan Dyatkovskiyc187df22012-05-17 08:56:30 +00001629 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(false)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001630 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT),
Stepan Dyatkovskiyc187df22012-05-17 08:56:30 +00001631 ISD::SETULE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001632 } else {
Dale Johannesenf5d97892009-02-04 01:48:28 +00001633 SDValue SUB = DAG.getNode(ISD::SUB, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001634 VT, CmpOp, DAG.getConstant(Low, VT));
Owen Anderson825b72b2009-08-11 20:47:22 +00001635 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001636 DAG.getConstant(High-Low, VT), ISD::SETULE);
1637 }
1638 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001639
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001640 // Update successor info
Jakub Staszakc8f34de2011-07-29 22:25:21 +00001641 addSuccessorWithWeight(SwitchBB, CB.TrueBB, CB.TrueWeight);
Jakob Stoklund Olesene7fdef42012-08-20 21:39:52 +00001642 // TrueBB and FalseBB are always different unless the incoming IR is
1643 // degenerate. This only happens when running llc on weird IR.
1644 if (CB.TrueBB != CB.FalseBB)
1645 addSuccessorWithWeight(SwitchBB, CB.FalseBB, CB.FalseWeight);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001646
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001647 // Set NextBlock to be the MBB immediately after the current one, if any.
1648 // This is used to avoid emitting unnecessary branches to the next block.
1649 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001650 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001651 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001652 NextBlock = BBI;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001653
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001654 // If the lhs block is the next block, invert the condition so that we can
1655 // fall through to the lhs instead of the rhs block.
1656 if (CB.TrueBB == NextBlock) {
1657 std::swap(CB.TrueBB, CB.FalseBB);
1658 SDValue True = DAG.getConstant(1, Cond.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001659 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001660 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001661
Dale Johannesenf5d97892009-02-04 01:48:28 +00001662 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001663 MVT::Other, getControlRoot(), Cond,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001664 DAG.getBasicBlock(CB.TrueBB));
Bill Wendling87710f02009-12-21 23:47:40 +00001665
Evan Cheng266a99d2010-09-23 06:51:55 +00001666 // Insert the false branch. Do this even if it's a fall through branch,
1667 // this makes it easier to do DAG optimizations which require inverting
1668 // the branch condition.
1669 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
1670 DAG.getBasicBlock(CB.FalseBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001671
1672 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001673}
1674
1675/// visitJumpTable - Emit JumpTable node in the current MBB
Dan Gohman2048b852009-11-23 18:04:58 +00001676void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001677 // Emit the code for the jump table
1678 assert(JT.Reg != -1U && "Should lower JT Header first!");
Bill Wendlingba54bca2013-06-19 21:36:55 +00001679 EVT PTy = TM.getTargetLowering()->getPointerTy();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001680 SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurSDLoc(),
Dale Johannesena04b7572009-02-03 23:04:43 +00001681 JT.Reg, PTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001682 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001683 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurSDLoc(),
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001684 MVT::Other, Index.getValue(1),
1685 Table, Index);
1686 DAG.setRoot(BrJumpTable);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001687}
1688
1689/// visitJumpTableHeader - This function emits necessary code to produce index
1690/// in the JumpTable from switch case.
Dan Gohman2048b852009-11-23 18:04:58 +00001691void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001692 JumpTableHeader &JTH,
1693 MachineBasicBlock *SwitchBB) {
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001694 // Subtract the lowest switch case value from the value being switched on and
1695 // conditional branch to default mbb if the result is greater than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001696 // difference between smallest and largest cases.
1697 SDValue SwitchOp = getValue(JTH.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001698 EVT VT = SwitchOp.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001699 SDValue Sub = DAG.getNode(ISD::SUB, getCurSDLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001700 DAG.getConstant(JTH.First, VT));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001701
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001702 // The SDNode we just created, which holds the value being switched on minus
Dan Gohmanf451cb82010-02-10 16:03:48 +00001703 // the smallest case value, needs to be copied to a virtual register so it
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001704 // can be used as an index into the jump table in a subsequent basic block.
1705 // This value may be smaller or larger than the target's pointer type, and
1706 // therefore require extension or truncating.
Bill Wendlingba54bca2013-06-19 21:36:55 +00001707 const TargetLowering *TLI = TM.getTargetLowering();
1708 SwitchOp = DAG.getZExtOrTrunc(Sub, getCurSDLoc(), TLI->getPointerTy());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001709
Bill Wendlingba54bca2013-06-19 21:36:55 +00001710 unsigned JumpTableReg = FuncInfo.CreateReg(TLI->getPointerTy());
Andrew Trickac6d9be2013-05-25 02:42:55 +00001711 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurSDLoc(),
Dale Johannesena04b7572009-02-03 23:04:43 +00001712 JumpTableReg, SwitchOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001713 JT.Reg = JumpTableReg;
1714
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001715 // Emit the range check for the jump table, and branch to the default block
1716 // for the switch statement if the value being switched on exceeds the largest
1717 // case in the switch.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001718 SDValue CMP = DAG.getSetCC(getCurSDLoc(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00001719 TLI->getSetCCResultType(*DAG.getContext(),
1720 Sub.getValueType()),
Matt Arsenault225ed702013-05-18 00:21:46 +00001721 Sub,
1722 DAG.getConstant(JTH.Last - JTH.First,VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001723 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001724
1725 // Set NextBlock to be the MBB immediately after the current one, if any.
1726 // This is used to avoid emitting unnecessary branches to the next block.
1727 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001728 MachineFunction::iterator BBI = SwitchBB;
Bill Wendling87710f02009-12-21 23:47:40 +00001729
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001730 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001731 NextBlock = BBI;
1732
Andrew Trickac6d9be2013-05-25 02:42:55 +00001733 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurSDLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001734 MVT::Other, CopyTo, CMP,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001735 DAG.getBasicBlock(JT.Default));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001736
Bill Wendling4533cac2010-01-28 21:51:40 +00001737 if (JT.MBB != NextBlock)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001738 BrCond = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, BrCond,
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001739 DAG.getBasicBlock(JT.MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001740
Bill Wendling87710f02009-12-21 23:47:40 +00001741 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001742}
1743
1744/// visitBitTestHeader - This function emits necessary code to produce value
1745/// suitable for "bit tests"
Dan Gohman99be8ae2010-04-19 22:41:47 +00001746void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B,
1747 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001748 // Subtract the minimum value
1749 SDValue SwitchOp = getValue(B.SValue);
Patrik Hagglund34525f92012-12-11 11:14:33 +00001750 EVT VT = SwitchOp.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001751 SDValue Sub = DAG.getNode(ISD::SUB, getCurSDLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001752 DAG.getConstant(B.First, VT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001753
1754 // Check range
Bill Wendlingba54bca2013-06-19 21:36:55 +00001755 const TargetLowering *TLI = TM.getTargetLowering();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001756 SDValue RangeCmp = DAG.getSetCC(getCurSDLoc(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00001757 TLI->getSetCCResultType(*DAG.getContext(),
Matt Arsenault225ed702013-05-18 00:21:46 +00001758 Sub.getValueType()),
Bill Wendling87710f02009-12-21 23:47:40 +00001759 Sub, DAG.getConstant(B.Range, VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001760 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001761
Evan Chengd08e5b42011-01-06 01:02:44 +00001762 // Determine the type of the test operands.
1763 bool UsePtrType = false;
Bill Wendlingba54bca2013-06-19 21:36:55 +00001764 if (!TLI->isTypeLegal(VT))
Evan Chengd08e5b42011-01-06 01:02:44 +00001765 UsePtrType = true;
1766 else {
1767 for (unsigned i = 0, e = B.Cases.size(); i != e; ++i)
Eli Friedman5c75af62011-10-12 22:46:45 +00001768 if (!isUIntN(VT.getSizeInBits(), B.Cases[i].Mask)) {
Evan Chengd08e5b42011-01-06 01:02:44 +00001769 // Switch table case range are encoded into series of masks.
1770 // Just use pointer type, it's guaranteed to fit.
1771 UsePtrType = true;
1772 break;
1773 }
1774 }
1775 if (UsePtrType) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00001776 VT = TLI->getPointerTy();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001777 Sub = DAG.getZExtOrTrunc(Sub, getCurSDLoc(), VT);
Evan Chengd08e5b42011-01-06 01:02:44 +00001778 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001779
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00001780 B.RegVT = VT.getSimpleVT();
Patrik Hagglund8963fec2012-12-19 12:23:01 +00001781 B.Reg = FuncInfo.CreateReg(B.RegVT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001782 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurSDLoc(),
Evan Chengd08e5b42011-01-06 01:02:44 +00001783 B.Reg, Sub);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001784
1785 // Set NextBlock to be the MBB immediately after the current one, if any.
1786 // This is used to avoid emitting unnecessary branches to the next block.
1787 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001788 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001789 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001790 NextBlock = BBI;
1791
1792 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1793
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001794 addSuccessorWithWeight(SwitchBB, B.Default);
1795 addSuccessorWithWeight(SwitchBB, MBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001796
Andrew Trickac6d9be2013-05-25 02:42:55 +00001797 SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurSDLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001798 MVT::Other, CopyTo, RangeCmp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001799 DAG.getBasicBlock(B.Default));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001800
Evan Cheng8c1f4322010-09-23 18:32:19 +00001801 if (MBB != NextBlock)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001802 BrRange = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, CopyTo,
Evan Cheng8c1f4322010-09-23 18:32:19 +00001803 DAG.getBasicBlock(MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001804
Bill Wendling87710f02009-12-21 23:47:40 +00001805 DAG.setRoot(BrRange);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001806}
1807
1808/// visitBitTestCase - this function produces one "bit test"
Evan Chengd08e5b42011-01-06 01:02:44 +00001809void SelectionDAGBuilder::visitBitTestCase(BitTestBlock &BB,
1810 MachineBasicBlock* NextMBB,
Manman Ren1a710fd2012-08-24 18:14:27 +00001811 uint32_t BranchWeightToNext,
Dan Gohman2048b852009-11-23 18:04:58 +00001812 unsigned Reg,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001813 BitTestCase &B,
1814 MachineBasicBlock *SwitchBB) {
Patrik Hagglund8963fec2012-12-19 12:23:01 +00001815 MVT VT = BB.RegVT;
Andrew Trickac6d9be2013-05-25 02:42:55 +00001816 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurSDLoc(),
Evan Chengd08e5b42011-01-06 01:02:44 +00001817 Reg, VT);
Dan Gohman8e0163a2010-06-24 02:06:24 +00001818 SDValue Cmp;
Benjamin Kramer3ff25512011-07-14 01:38:42 +00001819 unsigned PopCount = CountPopulation_64(B.Mask);
Bill Wendlingba54bca2013-06-19 21:36:55 +00001820 const TargetLowering *TLI = TM.getTargetLowering();
Benjamin Kramer3ff25512011-07-14 01:38:42 +00001821 if (PopCount == 1) {
Dan Gohman8e0163a2010-06-24 02:06:24 +00001822 // Testing for a single bit; just compare the shift count with what it
1823 // would need to be to shift a 1 bit in that position.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001824 Cmp = DAG.getSetCC(getCurSDLoc(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00001825 TLI->getSetCCResultType(*DAG.getContext(), VT),
Dan Gohman8e0163a2010-06-24 02:06:24 +00001826 ShiftOp,
Michael J. Spencerc6af2432013-05-24 22:23:49 +00001827 DAG.getConstant(countTrailingZeros(B.Mask), VT),
Dan Gohman8e0163a2010-06-24 02:06:24 +00001828 ISD::SETEQ);
Benjamin Kramer3ff25512011-07-14 01:38:42 +00001829 } else if (PopCount == BB.Range) {
1830 // There is only one zero bit in the range, test for it directly.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001831 Cmp = DAG.getSetCC(getCurSDLoc(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00001832 TLI->getSetCCResultType(*DAG.getContext(), VT),
Benjamin Kramer3ff25512011-07-14 01:38:42 +00001833 ShiftOp,
1834 DAG.getConstant(CountTrailingOnes_64(B.Mask), VT),
1835 ISD::SETNE);
Dan Gohman8e0163a2010-06-24 02:06:24 +00001836 } else {
1837 // Make desired shift
Andrew Trickac6d9be2013-05-25 02:42:55 +00001838 SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurSDLoc(), VT,
Evan Chengd08e5b42011-01-06 01:02:44 +00001839 DAG.getConstant(1, VT), ShiftOp);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001840
Dan Gohman8e0163a2010-06-24 02:06:24 +00001841 // Emit bit tests and jumps
Andrew Trickac6d9be2013-05-25 02:42:55 +00001842 SDValue AndOp = DAG.getNode(ISD::AND, getCurSDLoc(),
Evan Chengd08e5b42011-01-06 01:02:44 +00001843 VT, SwitchVal, DAG.getConstant(B.Mask, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00001844 Cmp = DAG.getSetCC(getCurSDLoc(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00001845 TLI->getSetCCResultType(*DAG.getContext(), VT),
Evan Chengd08e5b42011-01-06 01:02:44 +00001846 AndOp, DAG.getConstant(0, VT),
Dan Gohman8e0163a2010-06-24 02:06:24 +00001847 ISD::SETNE);
1848 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001849
Manman Ren1a710fd2012-08-24 18:14:27 +00001850 // The branch weight from SwitchBB to B.TargetBB is B.ExtraWeight.
1851 addSuccessorWithWeight(SwitchBB, B.TargetBB, B.ExtraWeight);
1852 // The branch weight from SwitchBB to NextMBB is BranchWeightToNext.
1853 addSuccessorWithWeight(SwitchBB, NextMBB, BranchWeightToNext);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001854
Andrew Trickac6d9be2013-05-25 02:42:55 +00001855 SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurSDLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001856 MVT::Other, getControlRoot(),
Dan Gohman8e0163a2010-06-24 02:06:24 +00001857 Cmp, DAG.getBasicBlock(B.TargetBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001858
1859 // Set NextBlock to be the MBB immediately after the current one, if any.
1860 // This is used to avoid emitting unnecessary branches to the next block.
1861 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001862 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001863 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001864 NextBlock = BBI;
1865
Evan Cheng8c1f4322010-09-23 18:32:19 +00001866 if (NextMBB != NextBlock)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001867 BrAnd = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, BrAnd,
Evan Cheng8c1f4322010-09-23 18:32:19 +00001868 DAG.getBasicBlock(NextMBB));
Bill Wendling0777e922009-12-21 21:59:52 +00001869
Bill Wendling87710f02009-12-21 23:47:40 +00001870 DAG.setRoot(BrAnd);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001871}
1872
Dan Gohman46510a72010-04-15 01:51:59 +00001873void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) {
Dan Gohman84023e02010-07-10 09:00:22 +00001874 MachineBasicBlock *InvokeMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001875
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001876 // Retrieve successors.
1877 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1878 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1879
Gabor Greifb67e6b32009-01-15 11:10:44 +00001880 const Value *Callee(I.getCalledValue());
Nuno Lopes85b40892012-06-28 22:30:12 +00001881 const Function *Fn = dyn_cast<Function>(Callee);
Gabor Greifb67e6b32009-01-15 11:10:44 +00001882 if (isa<InlineAsm>(Callee))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001883 visitInlineAsm(&I);
Nuno Lopes85b40892012-06-28 22:30:12 +00001884 else if (Fn && Fn->isIntrinsic()) {
1885 assert(Fn->getIntrinsicID() == Intrinsic::donothing);
Nuno Lopes4532bf62012-07-18 00:07:17 +00001886 // Ignore invokes to @llvm.donothing: jump directly to the next BB.
Nuno Lopes85b40892012-06-28 22:30:12 +00001887 } else
Gabor Greifb67e6b32009-01-15 11:10:44 +00001888 LowerCallTo(&I, getValue(Callee), false, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001889
1890 // If the value of the invoke is used outside of its defining block, make it
1891 // available as a virtual register.
Dan Gohmanad62f532009-04-23 23:13:24 +00001892 CopyToExportRegsIfNeeded(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001893
1894 // Update successor info
Chandler Carruthf2645682011-11-22 11:37:46 +00001895 addSuccessorWithWeight(InvokeMBB, Return);
1896 addSuccessorWithWeight(InvokeMBB, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001897
1898 // Drop into normal successor.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001899 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00001900 MVT::Other, getControlRoot(),
1901 DAG.getBasicBlock(Return)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001902}
1903
Bill Wendlingdccc03b2011-07-31 06:30:59 +00001904void SelectionDAGBuilder::visitResume(const ResumeInst &RI) {
1905 llvm_unreachable("SelectionDAGBuilder shouldn't visit resume instructions!");
1906}
1907
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00001908void SelectionDAGBuilder::visitLandingPad(const LandingPadInst &LP) {
1909 assert(FuncInfo.MBB->isLandingPad() &&
1910 "Call to landingpad not in landing pad!");
1911
1912 MachineBasicBlock *MBB = FuncInfo.MBB;
1913 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
1914 AddLandingPadInfo(LP, MMI, MBB);
1915
Bill Wendlingbdf9db62012-02-13 23:47:16 +00001916 // If there aren't registers to copy the values into (e.g., during SjLj
1917 // exceptions), then don't bother to create these DAG nodes.
Bill Wendlingba54bca2013-06-19 21:36:55 +00001918 const TargetLowering *TLI = TM.getTargetLowering();
1919 if (TLI->getExceptionPointerRegister() == 0 &&
1920 TLI->getExceptionSelectorRegister() == 0)
Bill Wendlingbdf9db62012-02-13 23:47:16 +00001921 return;
1922
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00001923 SmallVector<EVT, 2> ValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00001924 ComputeValueVTs(*TLI, LP.getType(), ValueVTs);
Jakob Stoklund Olesen918b7c82013-07-04 04:53:45 +00001925 assert(ValueVTs.size() == 2 && "Only two-valued landingpads are supported");
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00001926
Jakob Stoklund Olesen918b7c82013-07-04 04:53:45 +00001927 // Get the two live-in registers as SDValues. The physregs have already been
1928 // copied into virtual registers.
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00001929 SDValue Ops[2];
Jakob Stoklund Olesen918b7c82013-07-04 04:53:45 +00001930 Ops[0] = DAG.getZExtOrTrunc(
1931 DAG.getCopyFromReg(DAG.getEntryNode(), getCurSDLoc(),
1932 FuncInfo.ExceptionPointerVirtReg, TLI->getPointerTy()),
1933 getCurSDLoc(), ValueVTs[0]);
1934 Ops[1] = DAG.getZExtOrTrunc(
1935 DAG.getCopyFromReg(DAG.getEntryNode(), getCurSDLoc(),
1936 FuncInfo.ExceptionSelectorVirtReg, TLI->getPointerTy()),
1937 getCurSDLoc(), ValueVTs[1]);
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00001938
Jakob Stoklund Olesen918b7c82013-07-04 04:53:45 +00001939 // Merge into one.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001940 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00001941 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
1942 &Ops[0], 2);
Jakob Stoklund Olesen918b7c82013-07-04 04:53:45 +00001943 setValue(&LP, Res);
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00001944}
1945
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001946/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
1947/// small case ranges).
Dan Gohman2048b852009-11-23 18:04:58 +00001948bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR,
1949 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001950 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001951 MachineBasicBlock *Default,
1952 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001953 // Size is the number of Cases represented by this range.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001954 size_t Size = CR.Range.second - CR.Range.first;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001955 if (Size > 3)
Anton Korobeynikov23218582008-12-23 22:25:27 +00001956 return false;
1957
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001958 // Get the MachineFunction which holds the current MBB. This is used when
1959 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001960 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001961
1962 // Figure out which block is immediately after the current one.
1963 MachineBasicBlock *NextBlock = 0;
1964 MachineFunction::iterator BBI = CR.CaseBB;
1965
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001966 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001967 NextBlock = BBI;
1968
Manman Ren1a710fd2012-08-24 18:14:27 +00001969 BranchProbabilityInfo *BPI = FuncInfo.BPI;
Benjamin Kramerce750f02010-11-22 09:45:38 +00001970 // If any two of the cases has the same destination, and if one value
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001971 // is the same as the other, but has one bit unset that the other has set,
1972 // use bit manipulation to do two compares at once. For example:
1973 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
Benjamin Kramerce750f02010-11-22 09:45:38 +00001974 // TODO: This could be extended to merge any 2 cases in switches with 3 cases.
1975 // TODO: Handle cases where CR.CaseBB != SwitchBB.
1976 if (Size == 2 && CR.CaseBB == SwitchBB) {
1977 Case &Small = *CR.Range.first;
1978 Case &Big = *(CR.Range.second-1);
1979
1980 if (Small.Low == Small.High && Big.Low == Big.High && Small.BB == Big.BB) {
1981 const APInt& SmallValue = cast<ConstantInt>(Small.Low)->getValue();
1982 const APInt& BigValue = cast<ConstantInt>(Big.Low)->getValue();
1983
1984 // Check that there is only one bit different.
1985 if (BigValue.countPopulation() == SmallValue.countPopulation() + 1 &&
1986 (SmallValue | BigValue) == BigValue) {
1987 // Isolate the common bit.
1988 APInt CommonBit = BigValue & ~SmallValue;
1989 assert((SmallValue | CommonBit) == BigValue &&
1990 CommonBit.countPopulation() == 1 && "Not a common bit?");
1991
1992 SDValue CondLHS = getValue(SV);
1993 EVT VT = CondLHS.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001994 SDLoc DL = getCurSDLoc();
Benjamin Kramerce750f02010-11-22 09:45:38 +00001995
1996 SDValue Or = DAG.getNode(ISD::OR, DL, VT, CondLHS,
1997 DAG.getConstant(CommonBit, VT));
1998 SDValue Cond = DAG.getSetCC(DL, MVT::i1,
1999 Or, DAG.getConstant(BigValue, VT),
2000 ISD::SETEQ);
2001
2002 // Update successor info.
Manman Ren1a710fd2012-08-24 18:14:27 +00002003 // Both Small and Big will jump to Small.BB, so we sum up the weights.
2004 addSuccessorWithWeight(SwitchBB, Small.BB,
2005 Small.ExtraWeight + Big.ExtraWeight);
2006 addSuccessorWithWeight(SwitchBB, Default,
2007 // The default destination is the first successor in IR.
2008 BPI ? BPI->getEdgeWeight(SwitchBB->getBasicBlock(), (unsigned)0) : 0);
Benjamin Kramerce750f02010-11-22 09:45:38 +00002009
2010 // Insert the true branch.
2011 SDValue BrCond = DAG.getNode(ISD::BRCOND, DL, MVT::Other,
2012 getControlRoot(), Cond,
2013 DAG.getBasicBlock(Small.BB));
2014
2015 // Insert the false branch.
2016 BrCond = DAG.getNode(ISD::BR, DL, MVT::Other, BrCond,
2017 DAG.getBasicBlock(Default));
2018
2019 DAG.setRoot(BrCond);
2020 return true;
2021 }
2022 }
2023 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002024
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002025 // Order cases by weight so the most likely case will be checked first.
Manman Ren1a710fd2012-08-24 18:14:27 +00002026 uint32_t UnhandledWeights = 0;
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002027 if (BPI) {
2028 for (CaseItr I = CR.Range.first, IE = CR.Range.second; I != IE; ++I) {
Manman Ren1a710fd2012-08-24 18:14:27 +00002029 uint32_t IWeight = I->ExtraWeight;
2030 UnhandledWeights += IWeight;
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002031 for (CaseItr J = CR.Range.first; J < I; ++J) {
Manman Ren1a710fd2012-08-24 18:14:27 +00002032 uint32_t JWeight = J->ExtraWeight;
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002033 if (IWeight > JWeight)
2034 std::swap(*I, *J);
2035 }
2036 }
2037 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002038 // Rearrange the case blocks so that the last one falls through if possible.
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002039 Case &BackCase = *(CR.Range.second-1);
Benjamin Kramer5db954d2012-05-26 21:19:12 +00002040 if (Size > 1 &&
2041 NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002042 // The last case block won't fall through into 'NextBlock' if we emit the
2043 // branches in this order. See if rearranging a case value would help.
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002044 // We start at the bottom as it's the case with the least weight.
Stephen Lin09f8ca32013-07-06 21:44:25 +00002045 for (Case *I = &*(CR.Range.second-2), *E = &*CR.Range.first-1; I != E; --I)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002046 if (I->BB == NextBlock) {
2047 std::swap(*I, BackCase);
2048 break;
2049 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002050 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002051
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002052 // Create a CaseBlock record representing a conditional branch to
2053 // the Case's target mbb if the value being switched on SV is equal
2054 // to C.
2055 MachineBasicBlock *CurBlock = CR.CaseBB;
2056 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
2057 MachineBasicBlock *FallThrough;
2058 if (I != E-1) {
2059 FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
2060 CurMF->insert(BBI, FallThrough);
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002061
2062 // Put SV in a virtual register to make it available from the new blocks.
2063 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002064 } else {
2065 // If the last case doesn't match, go to the default block.
2066 FallThrough = Default;
2067 }
2068
Dan Gohman46510a72010-04-15 01:51:59 +00002069 const Value *RHS, *LHS, *MHS;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002070 ISD::CondCode CC;
2071 if (I->High == I->Low) {
2072 // This is just small small case range :) containing exactly 1 case
2073 CC = ISD::SETEQ;
2074 LHS = SV; RHS = I->High; MHS = NULL;
2075 } else {
Stephen Lin155615d2013-07-08 00:37:03 +00002076 CC = ISD::SETCC_INVALID;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002077 LHS = I->Low; MHS = SV; RHS = I->High;
2078 }
Jakub Staszakc8f34de2011-07-29 22:25:21 +00002079
Manman Ren1a710fd2012-08-24 18:14:27 +00002080 // The false weight should be sum of all un-handled cases.
2081 UnhandledWeights -= I->ExtraWeight;
Jakub Staszakc8f34de2011-07-29 22:25:21 +00002082 CaseBlock CB(CC, LHS, RHS, MHS, /* truebb */ I->BB, /* falsebb */ FallThrough,
2083 /* me */ CurBlock,
Manman Ren1a710fd2012-08-24 18:14:27 +00002084 /* trueweight */ I->ExtraWeight,
2085 /* falseweight */ UnhandledWeights);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002086
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002087 // If emitting the first comparison, just call visitSwitchCase to emit the
2088 // code into the current block. Otherwise, push the CaseBlock onto the
2089 // vector to be later processed by SDISel, and insert the node's MBB
2090 // before the next MBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002091 if (CurBlock == SwitchBB)
2092 visitSwitchCase(CB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002093 else
2094 SwitchCases.push_back(CB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002095
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002096 CurBlock = FallThrough;
2097 }
2098
2099 return true;
2100}
2101
2102static inline bool areJTsAllowed(const TargetLowering &TLI) {
Evan Cheng769951f2012-07-02 22:39:56 +00002103 return TLI.supportJumpTables() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00002104 (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
2105 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002106}
Anton Korobeynikov23218582008-12-23 22:25:27 +00002107
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002108static APInt ComputeRange(const APInt &First, const APInt &Last) {
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002109 uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1;
Stepan Dyatkovskiyc187df22012-05-17 08:56:30 +00002110 APInt LastExt = Last.zext(BitWidth), FirstExt = First.zext(BitWidth);
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002111 return (LastExt - FirstExt + 1ULL);
2112}
2113
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002114/// handleJTSwitchCase - Emit jumptable for current switch case range
Chris Lattnerc3ab3882011-09-09 22:06:59 +00002115bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec &CR,
2116 CaseRecVector &WorkList,
2117 const Value *SV,
2118 MachineBasicBlock *Default,
Dan Gohman99be8ae2010-04-19 22:41:47 +00002119 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002120 Case& FrontCase = *CR.Range.first;
2121 Case& BackCase = *(CR.Range.second-1);
2122
Chris Lattnere880efe2009-11-07 07:50:34 +00002123 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
2124 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002125
Chris Lattnere880efe2009-11-07 07:50:34 +00002126 APInt TSize(First.getBitWidth(), 0);
Chris Lattnerc3ab3882011-09-09 22:06:59 +00002127 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002128 TSize += I->size();
2129
Bill Wendlingba54bca2013-06-19 21:36:55 +00002130 const TargetLowering *TLI = TM.getTargetLowering();
2131 if (!areJTsAllowed(*TLI) || TSize.ult(TLI->getMinimumJumpTableEntries()))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002132 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002133
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002134 APInt Range = ComputeRange(First, Last);
Jakob Stoklund Olesen79443912011-10-26 01:47:48 +00002135 // The density is TSize / Range. Require at least 40%.
2136 // It should not be possible for IntTSize to saturate for sane code, but make
2137 // sure we handle Range saturation correctly.
2138 uint64_t IntRange = Range.getLimitedValue(UINT64_MAX/10);
2139 uint64_t IntTSize = TSize.getLimitedValue(UINT64_MAX/10);
2140 if (IntTSize * 10 < IntRange * 4)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002141 return false;
2142
David Greene4b69d992010-01-05 01:24:57 +00002143 DEBUG(dbgs() << "Lowering jump table\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002144 << "First entry: " << First << ". Last entry: " << Last << '\n'
Jakob Stoklund Olesen79443912011-10-26 01:47:48 +00002145 << "Range: " << Range << ". Size: " << TSize << ".\n\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002146
2147 // Get the MachineFunction which holds the current MBB. This is used when
2148 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002149 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002150
2151 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002152 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00002153 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002154
2155 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
2156
2157 // Create a new basic block to hold the code for loading the address
2158 // of the jump table, and jumping to it. Update successor information;
2159 // we will either branch to the default case for the switch, or the jump
2160 // table.
2161 MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2162 CurMF->insert(BBI, JumpTableBB);
Jakub Staszak7cc2b072011-06-16 20:22:37 +00002163
2164 addSuccessorWithWeight(CR.CaseBB, Default);
2165 addSuccessorWithWeight(CR.CaseBB, JumpTableBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002166
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002167 // Build a vector of destination BBs, corresponding to each target
2168 // of the jump table. If the value of the jump table slot corresponds to
2169 // a case statement, push the case's BB onto the vector, otherwise, push
2170 // the default BB.
2171 std::vector<MachineBasicBlock*> DestBBs;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002172 APInt TEI = First;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002173 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
Chris Lattner071c62f2010-01-25 23:26:13 +00002174 const APInt &Low = cast<ConstantInt>(I->Low)->getValue();
2175 const APInt &High = cast<ConstantInt>(I->High)->getValue();
Anton Korobeynikov23218582008-12-23 22:25:27 +00002176
Stepan Dyatkovskiyc187df22012-05-17 08:56:30 +00002177 if (Low.ule(TEI) && TEI.ule(High)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002178 DestBBs.push_back(I->BB);
2179 if (TEI==High)
2180 ++I;
2181 } else {
2182 DestBBs.push_back(Default);
2183 }
2184 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002185
Manman Ren1a710fd2012-08-24 18:14:27 +00002186 // Calculate weight for each unique destination in CR.
2187 DenseMap<MachineBasicBlock*, uint32_t> DestWeights;
2188 if (FuncInfo.BPI)
2189 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
2190 DenseMap<MachineBasicBlock*, uint32_t>::iterator Itr =
2191 DestWeights.find(I->BB);
Stephen Lin155615d2013-07-08 00:37:03 +00002192 if (Itr != DestWeights.end())
Manman Ren1a710fd2012-08-24 18:14:27 +00002193 Itr->second += I->ExtraWeight;
2194 else
2195 DestWeights[I->BB] = I->ExtraWeight;
2196 }
2197
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002198 // Update successor info. Add one edge to each unique successor.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002199 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
2200 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002201 E = DestBBs.end(); I != E; ++I) {
2202 if (!SuccsHandled[(*I)->getNumber()]) {
2203 SuccsHandled[(*I)->getNumber()] = true;
Manman Ren1a710fd2012-08-24 18:14:27 +00002204 DenseMap<MachineBasicBlock*, uint32_t>::iterator Itr =
2205 DestWeights.find(*I);
2206 addSuccessorWithWeight(JumpTableBB, *I,
2207 Itr != DestWeights.end() ? Itr->second : 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002208 }
2209 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002210
Bob Wilsond1ec31d2010-03-18 18:42:41 +00002211 // Create a jump table index for this jump table.
Bill Wendlingba54bca2013-06-19 21:36:55 +00002212 unsigned JTEncoding = TLI->getJumpTableEncoding();
Chris Lattner071c62f2010-01-25 23:26:13 +00002213 unsigned JTI = CurMF->getOrCreateJumpTableInfo(JTEncoding)
Bob Wilsond1ec31d2010-03-18 18:42:41 +00002214 ->createJumpTableIndex(DestBBs);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002215
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002216 // Set the jump table information so that we can codegen it as a second
2217 // MachineBasicBlock
2218 JumpTable JT(-1U, JTI, JumpTableBB, Default);
Dan Gohman99be8ae2010-04-19 22:41:47 +00002219 JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == SwitchBB));
2220 if (CR.CaseBB == SwitchBB)
2221 visitJumpTableHeader(JT, JTH, SwitchBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002222
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002223 JTCases.push_back(JumpTableBlock(JTH, JT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002224 return true;
2225}
2226
2227/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
2228/// 2 subtrees.
Dan Gohman2048b852009-11-23 18:04:58 +00002229bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR,
2230 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00002231 const Value* SV,
Stephen Lin09f8ca32013-07-06 21:44:25 +00002232 MachineBasicBlock* Default,
2233 MachineBasicBlock* SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002234 // Get the MachineFunction which holds the current MBB. This is used when
2235 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002236 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002237
2238 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002239 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00002240 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002241
2242 Case& FrontCase = *CR.Range.first;
2243 Case& BackCase = *(CR.Range.second-1);
2244 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
2245
2246 // Size is the number of Cases represented by this range.
2247 unsigned Size = CR.Range.second - CR.Range.first;
2248
Chris Lattnere880efe2009-11-07 07:50:34 +00002249 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
2250 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002251 double FMetric = 0;
2252 CaseItr Pivot = CR.Range.first + Size/2;
2253
2254 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
2255 // (heuristically) allow us to emit JumpTable's later.
Chris Lattnere880efe2009-11-07 07:50:34 +00002256 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002257 for (CaseItr I = CR.Range.first, E = CR.Range.second;
2258 I!=E; ++I)
2259 TSize += I->size();
2260
Chris Lattnere880efe2009-11-07 07:50:34 +00002261 APInt LSize = FrontCase.size();
2262 APInt RSize = TSize-LSize;
David Greene4b69d992010-01-05 01:24:57 +00002263 DEBUG(dbgs() << "Selecting best pivot: \n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002264 << "First: " << First << ", Last: " << Last <<'\n'
2265 << "LSize: " << LSize << ", RSize: " << RSize << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002266 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
2267 J!=E; ++I, ++J) {
Chris Lattnere880efe2009-11-07 07:50:34 +00002268 const APInt &LEnd = cast<ConstantInt>(I->High)->getValue();
2269 const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002270 APInt Range = ComputeRange(LEnd, RBegin);
Stepan Dyatkovskiyc2c52a62012-05-15 06:50:18 +00002271 assert((Range - 2ULL).isNonNegative() &&
2272 "Invalid case distance");
Chris Lattnerc3e4e592011-04-09 06:57:13 +00002273 // Use volatile double here to avoid excess precision issues on some hosts,
2274 // e.g. that use 80-bit X87 registers.
2275 volatile double LDensity =
2276 (double)LSize.roundToDouble() /
Chris Lattnere880efe2009-11-07 07:50:34 +00002277 (LEnd - First + 1ULL).roundToDouble();
Chris Lattnerc3e4e592011-04-09 06:57:13 +00002278 volatile double RDensity =
2279 (double)RSize.roundToDouble() /
Chris Lattnere880efe2009-11-07 07:50:34 +00002280 (Last - RBegin + 1ULL).roundToDouble();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002281 double Metric = Range.logBase2()*(LDensity+RDensity);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002282 // Should always split in some non-trivial place
David Greene4b69d992010-01-05 01:24:57 +00002283 DEBUG(dbgs() <<"=>Step\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002284 << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n'
2285 << "LDensity: " << LDensity
2286 << ", RDensity: " << RDensity << '\n'
2287 << "Metric: " << Metric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002288 if (FMetric < Metric) {
2289 Pivot = J;
2290 FMetric = Metric;
David Greene4b69d992010-01-05 01:24:57 +00002291 DEBUG(dbgs() << "Current metric set to: " << FMetric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002292 }
2293
2294 LSize += J->size();
2295 RSize -= J->size();
2296 }
Bill Wendlingba54bca2013-06-19 21:36:55 +00002297
2298 const TargetLowering *TLI = TM.getTargetLowering();
2299 if (areJTsAllowed(*TLI)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002300 // If our case is dense we *really* should handle it earlier!
2301 assert((FMetric > 0) && "Should handle dense range earlier!");
2302 } else {
2303 Pivot = CR.Range.first + Size/2;
2304 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002305
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002306 CaseRange LHSR(CR.Range.first, Pivot);
2307 CaseRange RHSR(Pivot, CR.Range.second);
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00002308 const Constant *C = Pivot->Low;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002309 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002310
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002311 // We know that we branch to the LHS if the Value being switched on is
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002312 // less than the Pivot value, C. We use this to optimize our binary
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002313 // tree a bit, by recognizing that if SV is greater than or equal to the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002314 // LHS's Case Value, and that Case Value is exactly one less than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002315 // Pivot's Value, then we can branch directly to the LHS's Target,
2316 // rather than creating a leaf node for it.
2317 if ((LHSR.second - LHSR.first) == 1 &&
2318 LHSR.first->High == CR.GE &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00002319 cast<ConstantInt>(C)->getValue() ==
2320 (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002321 TrueBB = LHSR.first->BB;
2322 } else {
2323 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2324 CurMF->insert(BBI, TrueBB);
2325 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002326
2327 // Put SV in a virtual register to make it available from the new blocks.
2328 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002329 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002330
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002331 // Similar to the optimization above, if the Value being switched on is
2332 // known to be less than the Constant CR.LT, and the current Case Value
2333 // is CR.LT - 1, then we can branch directly to the target block for
2334 // the current Case Value, rather than emitting a RHS leaf node for it.
2335 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00002336 cast<ConstantInt>(RHSR.first->Low)->getValue() ==
2337 (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002338 FalseBB = RHSR.first->BB;
2339 } else {
2340 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2341 CurMF->insert(BBI, FalseBB);
2342 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002343
2344 // Put SV in a virtual register to make it available from the new blocks.
2345 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002346 }
2347
2348 // Create a CaseBlock record representing a conditional branch to
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002349 // the LHS node if the value being switched on SV is less than C.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002350 // Otherwise, branch to LHS.
Stepan Dyatkovskiyc187df22012-05-17 08:56:30 +00002351 CaseBlock CB(ISD::SETULT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002352
Dan Gohman99be8ae2010-04-19 22:41:47 +00002353 if (CR.CaseBB == SwitchBB)
2354 visitSwitchCase(CB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002355 else
2356 SwitchCases.push_back(CB);
2357
2358 return true;
2359}
2360
2361/// handleBitTestsSwitchCase - if current case range has few destination and
2362/// range span less, than machine word bitwidth, encode case range into series
2363/// of masks and emit bit tests with these masks.
Dan Gohman2048b852009-11-23 18:04:58 +00002364bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR,
2365 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00002366 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00002367 MachineBasicBlock* Default,
Stephen Lin09f8ca32013-07-06 21:44:25 +00002368 MachineBasicBlock* SwitchBB) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00002369 const TargetLowering *TLI = TM.getTargetLowering();
2370 EVT PTy = TLI->getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002371 unsigned IntPtrBits = PTy.getSizeInBits();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002372
2373 Case& FrontCase = *CR.Range.first;
2374 Case& BackCase = *(CR.Range.second-1);
2375
2376 // Get the MachineFunction which holds the current MBB. This is used when
2377 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002378 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002379
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00002380 // If target does not have legal shift left, do not emit bit tests at all.
Bill Wendlingba54bca2013-06-19 21:36:55 +00002381 if (!TLI->isOperationLegal(ISD::SHL, TLI->getPointerTy()))
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00002382 return false;
2383
Anton Korobeynikov23218582008-12-23 22:25:27 +00002384 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002385 for (CaseItr I = CR.Range.first, E = CR.Range.second;
2386 I!=E; ++I) {
2387 // Single case counts one, case range - two.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002388 numCmps += (I->Low == I->High ? 1 : 2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002389 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002390
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002391 // Count unique destinations
2392 SmallSet<MachineBasicBlock*, 4> Dests;
2393 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
2394 Dests.insert(I->BB);
2395 if (Dests.size() > 3)
2396 // Don't bother the code below, if there are too much unique destinations
2397 return false;
2398 }
David Greene4b69d992010-01-05 01:24:57 +00002399 DEBUG(dbgs() << "Total number of unique destinations: "
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002400 << Dests.size() << '\n'
2401 << "Total number of comparisons: " << numCmps << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00002402
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002403 // Compute span of values.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002404 const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue();
2405 const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002406 APInt cmpRange = maxValue - minValue;
2407
David Greene4b69d992010-01-05 01:24:57 +00002408 DEBUG(dbgs() << "Compare range: " << cmpRange << '\n'
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002409 << "Low bound: " << minValue << '\n'
2410 << "High bound: " << maxValue << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00002411
Dan Gohmane0567812010-04-08 23:03:40 +00002412 if (cmpRange.uge(IntPtrBits) ||
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002413 (!(Dests.size() == 1 && numCmps >= 3) &&
2414 !(Dests.size() == 2 && numCmps >= 5) &&
2415 !(Dests.size() >= 3 && numCmps >= 6)))
2416 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002417
David Greene4b69d992010-01-05 01:24:57 +00002418 DEBUG(dbgs() << "Emitting bit tests\n");
Anton Korobeynikov23218582008-12-23 22:25:27 +00002419 APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth());
2420
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002421 // Optimize the case where all the case values fit in a
2422 // word without having to subtract minValue. In this case,
2423 // we can optimize away the subtraction.
Stepan Dyatkovskiyc187df22012-05-17 08:56:30 +00002424 if (maxValue.ult(IntPtrBits)) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002425 cmpRange = maxValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002426 } else {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002427 lowBound = minValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002428 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002429
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002430 CaseBitsVector CasesBits;
2431 unsigned i, count = 0;
2432
2433 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
2434 MachineBasicBlock* Dest = I->BB;
2435 for (i = 0; i < count; ++i)
2436 if (Dest == CasesBits[i].BB)
2437 break;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002438
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002439 if (i == count) {
2440 assert((count < 3) && "Too much destinations to test!");
Manman Ren1a710fd2012-08-24 18:14:27 +00002441 CasesBits.push_back(CaseBits(0, Dest, 0, 0/*Weight*/));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002442 count++;
2443 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002444
2445 const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue();
2446 const APInt& highValue = cast<ConstantInt>(I->High)->getValue();
2447
2448 uint64_t lo = (lowValue - lowBound).getZExtValue();
2449 uint64_t hi = (highValue - lowBound).getZExtValue();
Manman Ren1a710fd2012-08-24 18:14:27 +00002450 CasesBits[i].ExtraWeight += I->ExtraWeight;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002451
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002452 for (uint64_t j = lo; j <= hi; j++) {
2453 CasesBits[i].Mask |= 1ULL << j;
2454 CasesBits[i].Bits++;
2455 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002456
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002457 }
2458 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
Anton Korobeynikov23218582008-12-23 22:25:27 +00002459
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002460 BitTestInfo BTC;
2461
2462 // Figure out which block is immediately after the current one.
2463 MachineFunction::iterator BBI = CR.CaseBB;
2464 ++BBI;
2465
2466 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
2467
David Greene4b69d992010-01-05 01:24:57 +00002468 DEBUG(dbgs() << "Cases:\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002469 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
David Greene4b69d992010-01-05 01:24:57 +00002470 DEBUG(dbgs() << "Mask: " << CasesBits[i].Mask
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002471 << ", Bits: " << CasesBits[i].Bits
2472 << ", BB: " << CasesBits[i].BB << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002473
2474 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2475 CurMF->insert(BBI, CaseBB);
2476 BTC.push_back(BitTestCase(CasesBits[i].Mask,
2477 CaseBB,
Manman Ren1a710fd2012-08-24 18:14:27 +00002478 CasesBits[i].BB, CasesBits[i].ExtraWeight));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002479
2480 // Put SV in a virtual register to make it available from the new blocks.
2481 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002482 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002483
2484 BitTestBlock BTB(lowBound, cmpRange, SV,
Evan Chengd08e5b42011-01-06 01:02:44 +00002485 -1U, MVT::Other, (CR.CaseBB == SwitchBB),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002486 CR.CaseBB, Default, BTC);
2487
Dan Gohman99be8ae2010-04-19 22:41:47 +00002488 if (CR.CaseBB == SwitchBB)
2489 visitBitTestHeader(BTB, SwitchBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002490
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002491 BitTestCases.push_back(BTB);
2492
2493 return true;
2494}
2495
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002496/// Clusterify - Transform simple list of Cases into list of CaseRange's
Dan Gohman2048b852009-11-23 18:04:58 +00002497size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases,
2498 const SwitchInst& SI) {
Stephen Lin155615d2013-07-08 00:37:03 +00002499
Stepan Dyatkovskiy05cfe2e2012-05-18 08:32:28 +00002500 /// Use a shorter form of declaration, and also
2501 /// show the we want to use CRSBuilder as Clusterifier.
Stepan Dyatkovskiy4319a552012-06-02 07:26:00 +00002502 typedef IntegersSubsetMapping<MachineBasicBlock> Clusterifier;
Stephen Lin155615d2013-07-08 00:37:03 +00002503
Stepan Dyatkovskiy05cfe2e2012-05-18 08:32:28 +00002504 Clusterifier TheClusterifier;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002505
Manman Ren1a710fd2012-08-24 18:14:27 +00002506 BranchProbabilityInfo *BPI = FuncInfo.BPI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002507 // Start with "simple" cases
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002508 for (SwitchInst::ConstCaseIt i = SI.case_begin(), e = SI.case_end();
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002509 i != e; ++i) {
2510 const BasicBlock *SuccBB = i.getCaseSuccessor();
Jakub Staszakc8f34de2011-07-29 22:25:21 +00002511 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SuccBB];
2512
Stephen Lin155615d2013-07-08 00:37:03 +00002513 TheClusterifier.add(i.getCaseValueEx(), SMBB,
Manman Ren1a710fd2012-08-24 18:14:27 +00002514 BPI ? BPI->getEdgeWeight(SI.getParent(), i.getSuccessorIndex()) : 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002515 }
Stephen Lin155615d2013-07-08 00:37:03 +00002516
Stepan Dyatkovskiy05cfe2e2012-05-18 08:32:28 +00002517 TheClusterifier.optimize();
Stephen Lin155615d2013-07-08 00:37:03 +00002518
Stepan Dyatkovskiy05cfe2e2012-05-18 08:32:28 +00002519 size_t numCmps = 0;
2520 for (Clusterifier::RangeIterator i = TheClusterifier.begin(),
2521 e = TheClusterifier.end(); i != e; ++i, ++numCmps) {
Stepan Dyatkovskiy66d79ce2012-07-04 05:53:05 +00002522 Clusterifier::Cluster &C = *i;
Manman Ren1a710fd2012-08-24 18:14:27 +00002523 // Update edge weight for the cluster.
2524 unsigned W = C.first.Weight;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002525
Stepan Dyatkovskiy484fc932012-05-28 12:39:09 +00002526 // FIXME: Currently work with ConstantInt based numbers.
2527 // Changing it to APInt based is a pretty heavy for this commit.
Stepan Dyatkovskiy66d79ce2012-07-04 05:53:05 +00002528 Cases.push_back(Case(C.first.getLow().toConstantInt(),
2529 C.first.getHigh().toConstantInt(), C.second, W));
Stephen Lin155615d2013-07-08 00:37:03 +00002530
Stepan Dyatkovskiy66d79ce2012-07-04 05:53:05 +00002531 if (C.first.getLow() != C.first.getHigh())
Stepan Dyatkovskiy05cfe2e2012-05-18 08:32:28 +00002532 // A range counts double, since it requires two compares.
2533 ++numCmps;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002534 }
2535
2536 return numCmps;
2537}
2538
Jakob Stoklund Olesen2622f462010-09-30 19:44:31 +00002539void SelectionDAGBuilder::UpdateSplitBlock(MachineBasicBlock *First,
2540 MachineBasicBlock *Last) {
2541 // Update JTCases.
2542 for (unsigned i = 0, e = JTCases.size(); i != e; ++i)
2543 if (JTCases[i].first.HeaderBB == First)
2544 JTCases[i].first.HeaderBB = Last;
2545
2546 // Update BitTestCases.
2547 for (unsigned i = 0, e = BitTestCases.size(); i != e; ++i)
2548 if (BitTestCases[i].Parent == First)
2549 BitTestCases[i].Parent = Last;
2550}
2551
Dan Gohman46510a72010-04-15 01:51:59 +00002552void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) {
Dan Gohman84023e02010-07-10 09:00:22 +00002553 MachineBasicBlock *SwitchMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00002554
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002555 // Figure out which block is immediately after the current one.
2556 MachineBasicBlock *NextBlock = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002557 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
2558
2559 // If there is only the default destination, branch to it if it is not the
2560 // next basic block. Otherwise, just fall through.
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00002561 if (!SI.getNumCases()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002562 // Update machine-CFG edges.
2563
2564 // If this is not a fall-through branch, emit the branch.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002565 SwitchMBB->addSuccessor(Default);
Bill Wendling4533cac2010-01-28 21:51:40 +00002566 if (Default != NextBlock)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002567 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002568 MVT::Other, getControlRoot(),
2569 DAG.getBasicBlock(Default)));
Bill Wendling49fcff82009-12-21 22:30:11 +00002570
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002571 return;
2572 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002573
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002574 // If there are any non-default case statements, create a vector of Cases
2575 // representing each one, and sort the vector so that we can efficiently
2576 // create a binary search tree from them.
2577 CaseVector Cases;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002578 size_t numCmps = Clusterify(Cases, SI);
David Greene4b69d992010-01-05 01:24:57 +00002579 DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size()
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002580 << ". Total compares: " << numCmps << '\n');
Duncan Sands17001ce2011-10-18 12:44:00 +00002581 (void)numCmps;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002582
2583 // Get the Value to be switched on and default basic blocks, which will be
2584 // inserted into CaseBlock records, representing basic blocks in the binary
2585 // search tree.
Eli Friedmanbb5a7442011-09-29 20:21:17 +00002586 const Value *SV = SI.getCondition();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002587
2588 // Push the initial CaseRec onto the worklist
2589 CaseRecVector WorkList;
Dan Gohman99be8ae2010-04-19 22:41:47 +00002590 WorkList.push_back(CaseRec(SwitchMBB,0,0,
2591 CaseRange(Cases.begin(),Cases.end())));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002592
2593 while (!WorkList.empty()) {
2594 // Grab a record representing a case range to process off the worklist
2595 CaseRec CR = WorkList.back();
2596 WorkList.pop_back();
2597
Dan Gohman99be8ae2010-04-19 22:41:47 +00002598 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002599 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002600
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002601 // If the range has few cases (two or less) emit a series of specific
2602 // tests.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002603 if (handleSmallSwitchRange(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002604 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002605
Sebastian Pop1a37d7e2012-09-25 20:35:36 +00002606 // If the switch has more than N blocks, and is at least 40% dense, and the
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002607 // target supports indirect branches, then emit a jump table rather than
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002608 // lowering the switch to a binary tree of conditional branches.
Sebastian Pop1a37d7e2012-09-25 20:35:36 +00002609 // N defaults to 4 and is controlled via TLS.getMinimumJumpTableEntries().
Dan Gohman99be8ae2010-04-19 22:41:47 +00002610 if (handleJTSwitchCase(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002611 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002612
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002613 // Emit binary tree. We need to pick a pivot, and push left and right ranges
2614 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002615 handleBTSplitSwitchCase(CR, WorkList, SV, Default, SwitchMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002616 }
2617}
2618
Dan Gohman46510a72010-04-15 01:51:59 +00002619void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) {
Dan Gohman84023e02010-07-10 09:00:22 +00002620 MachineBasicBlock *IndirectBrMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00002621
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002622 // Update machine-CFG edges with unique successors.
Nadav Rotemee0ce152012-10-23 21:05:33 +00002623 SmallSet<BasicBlock*, 32> Done;
2624 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i) {
2625 BasicBlock *BB = I.getSuccessor(i);
2626 bool Inserted = Done.insert(BB);
2627 if (!Inserted)
2628 continue;
2629
2630 MachineBasicBlock *Succ = FuncInfo.MBBMap[BB];
Jakub Staszak7cc2b072011-06-16 20:22:37 +00002631 addSuccessorWithWeight(IndirectBrMBB, Succ);
2632 }
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002633
Andrew Trickac6d9be2013-05-25 02:42:55 +00002634 DAG.setRoot(DAG.getNode(ISD::BRIND, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002635 MVT::Other, getControlRoot(),
2636 getValue(I.getAddress())));
Bill Wendling49fcff82009-12-21 22:30:11 +00002637}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002638
Dan Gohman46510a72010-04-15 01:51:59 +00002639void SelectionDAGBuilder::visitFSub(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002640 // -0.0 - X --> fneg
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002641 Type *Ty = I.getType();
Chris Lattner2ca5c862011-02-15 00:14:00 +00002642 if (isa<Constant>(I.getOperand(0)) &&
2643 I.getOperand(0) == ConstantFP::getZeroValueForNegation(Ty)) {
2644 SDValue Op2 = getValue(I.getOperand(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00002645 setValue(&I, DAG.getNode(ISD::FNEG, getCurSDLoc(),
Chris Lattner2ca5c862011-02-15 00:14:00 +00002646 Op2.getValueType(), Op2));
2647 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002648 }
Bill Wendling49fcff82009-12-21 22:30:11 +00002649
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002650 visitBinary(I, ISD::FSUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002651}
2652
Dan Gohman46510a72010-04-15 01:51:59 +00002653void SelectionDAGBuilder::visitBinary(const User &I, unsigned OpCode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002654 SDValue Op1 = getValue(I.getOperand(0));
2655 SDValue Op2 = getValue(I.getOperand(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00002656 setValue(&I, DAG.getNode(OpCode, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002657 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002658}
2659
Dan Gohman46510a72010-04-15 01:51:59 +00002660void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002661 SDValue Op1 = getValue(I.getOperand(0));
2662 SDValue Op2 = getValue(I.getOperand(1));
Owen Anderson95771af2011-02-25 21:41:48 +00002663
Bill Wendlingba54bca2013-06-19 21:36:55 +00002664 EVT ShiftTy = TM.getTargetLowering()->getShiftAmountTy(Op2.getValueType());
Owen Anderson95771af2011-02-25 21:41:48 +00002665
Chris Lattnerd3027732011-02-13 09:02:52 +00002666 // Coerce the shift amount to the right type if we can.
2667 if (!I.getType()->isVectorTy() && Op2.getValueType() != ShiftTy) {
Chris Lattner915eeb42011-02-13 09:10:56 +00002668 unsigned ShiftSize = ShiftTy.getSizeInBits();
2669 unsigned Op2Size = Op2.getValueType().getSizeInBits();
Andrew Trickac6d9be2013-05-25 02:42:55 +00002670 SDLoc DL = getCurSDLoc();
Owen Anderson95771af2011-02-25 21:41:48 +00002671
Dan Gohman57fc82d2009-04-09 03:51:29 +00002672 // If the operand is smaller than the shift count type, promote it.
Chris Lattnerd3027732011-02-13 09:02:52 +00002673 if (ShiftSize > Op2Size)
2674 Op2 = DAG.getNode(ISD::ZERO_EXTEND, DL, ShiftTy, Op2);
Owen Anderson95771af2011-02-25 21:41:48 +00002675
Dan Gohman57fc82d2009-04-09 03:51:29 +00002676 // If the operand is larger than the shift count type but the shift
2677 // count type has enough bits to represent any shift value, truncate
2678 // it now. This is a common case and it exposes the truncate to
2679 // optimization early.
Chris Lattnerd3027732011-02-13 09:02:52 +00002680 else if (ShiftSize >= Log2_32_Ceil(Op2.getValueType().getSizeInBits()))
2681 Op2 = DAG.getNode(ISD::TRUNCATE, DL, ShiftTy, Op2);
2682 // Otherwise we'll need to temporarily settle for some other convenient
Chris Lattnere0751182011-02-13 19:09:16 +00002683 // type. Type legalization will make adjustments once the shiftee is split.
Chris Lattnerd3027732011-02-13 09:02:52 +00002684 else
Chris Lattnere0751182011-02-13 19:09:16 +00002685 Op2 = DAG.getZExtOrTrunc(Op2, DL, MVT::i32);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002686 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002687
Andrew Trickac6d9be2013-05-25 02:42:55 +00002688 setValue(&I, DAG.getNode(Opcode, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002689 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002690}
2691
Benjamin Kramer9c640302011-07-08 10:31:30 +00002692void SelectionDAGBuilder::visitSDiv(const User &I) {
Benjamin Kramer9c640302011-07-08 10:31:30 +00002693 SDValue Op1 = getValue(I.getOperand(0));
2694 SDValue Op2 = getValue(I.getOperand(1));
2695
2696 // Turn exact SDivs into multiplications.
2697 // FIXME: This should be in DAGCombiner, but it doesn't have access to the
2698 // exact bit.
Benjamin Kramer3492a4a2011-07-08 12:08:24 +00002699 if (isa<BinaryOperator>(&I) && cast<BinaryOperator>(&I)->isExact() &&
2700 !isa<ConstantSDNode>(Op1) &&
Benjamin Kramer9c640302011-07-08 10:31:30 +00002701 isa<ConstantSDNode>(Op2) && !cast<ConstantSDNode>(Op2)->isNullValue())
Bill Wendlingba54bca2013-06-19 21:36:55 +00002702 setValue(&I, TM.getTargetLowering()->BuildExactSDIV(Op1, Op2,
2703 getCurSDLoc(), DAG));
Benjamin Kramer9c640302011-07-08 10:31:30 +00002704 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00002705 setValue(&I, DAG.getNode(ISD::SDIV, getCurSDLoc(), Op1.getValueType(),
Benjamin Kramer9c640302011-07-08 10:31:30 +00002706 Op1, Op2));
2707}
2708
Dan Gohman46510a72010-04-15 01:51:59 +00002709void SelectionDAGBuilder::visitICmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002710 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002711 if (const ICmpInst *IC = dyn_cast<ICmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002712 predicate = IC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002713 else if (const ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002714 predicate = ICmpInst::Predicate(IC->getPredicate());
2715 SDValue Op1 = getValue(I.getOperand(0));
2716 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002717 ISD::CondCode Opcode = getICmpCondCode(predicate);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002718
Bill Wendlingba54bca2013-06-19 21:36:55 +00002719 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002720 setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Opcode));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002721}
2722
Dan Gohman46510a72010-04-15 01:51:59 +00002723void SelectionDAGBuilder::visitFCmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002724 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002725 if (const FCmpInst *FC = dyn_cast<FCmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002726 predicate = FC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002727 else if (const ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002728 predicate = FCmpInst::Predicate(FC->getPredicate());
2729 SDValue Op1 = getValue(I.getOperand(0));
2730 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002731 ISD::CondCode Condition = getFCmpCondCode(predicate);
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002732 if (TM.Options.NoNaNsFPMath)
2733 Condition = getFCmpCodeWithoutNaN(Condition);
Bill Wendlingba54bca2013-06-19 21:36:55 +00002734 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002735 setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Condition));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002736}
2737
Dan Gohman46510a72010-04-15 01:51:59 +00002738void SelectionDAGBuilder::visitSelect(const User &I) {
Owen Andersone50ed302009-08-10 22:56:29 +00002739 SmallVector<EVT, 4> ValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00002740 ComputeValueVTs(*TM.getTargetLowering(), I.getType(), ValueVTs);
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002741 unsigned NumValues = ValueVTs.size();
Bill Wendling49fcff82009-12-21 22:30:11 +00002742 if (NumValues == 0) return;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002743
Bill Wendling49fcff82009-12-21 22:30:11 +00002744 SmallVector<SDValue, 4> Values(NumValues);
2745 SDValue Cond = getValue(I.getOperand(0));
2746 SDValue TrueVal = getValue(I.getOperand(1));
2747 SDValue FalseVal = getValue(I.getOperand(2));
Duncan Sands28b77e92011-09-06 19:07:46 +00002748 ISD::NodeType OpCode = Cond.getValueType().isVector() ?
2749 ISD::VSELECT : ISD::SELECT;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002750
Bill Wendling4533cac2010-01-28 21:51:40 +00002751 for (unsigned i = 0; i != NumValues; ++i)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002752 Values[i] = DAG.getNode(OpCode, getCurSDLoc(),
Duncan Sands28b77e92011-09-06 19:07:46 +00002753 TrueVal.getNode()->getValueType(TrueVal.getResNo()+i),
Chris Lattnerb3e87b22010-03-12 07:15:36 +00002754 Cond,
Bill Wendling49fcff82009-12-21 22:30:11 +00002755 SDValue(TrueVal.getNode(),
2756 TrueVal.getResNo() + i),
2757 SDValue(FalseVal.getNode(),
2758 FalseVal.getResNo() + i));
2759
Andrew Trickac6d9be2013-05-25 02:42:55 +00002760 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002761 DAG.getVTList(&ValueVTs[0], NumValues),
2762 &Values[0], NumValues));
Bill Wendling49fcff82009-12-21 22:30:11 +00002763}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002764
Dan Gohman46510a72010-04-15 01:51:59 +00002765void SelectionDAGBuilder::visitTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002766 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2767 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002768 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002769 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002770}
2771
Dan Gohman46510a72010-04-15 01:51:59 +00002772void SelectionDAGBuilder::visitZExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002773 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2774 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2775 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002776 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002777 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002778}
2779
Dan Gohman46510a72010-04-15 01:51:59 +00002780void SelectionDAGBuilder::visitSExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002781 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2782 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2783 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002784 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002785 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002786}
2787
Dan Gohman46510a72010-04-15 01:51:59 +00002788void SelectionDAGBuilder::visitFPTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002789 // FPTrunc is never a no-op cast, no need to check
2790 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002791 const TargetLowering *TLI = TM.getTargetLowering();
2792 EVT DestVT = TLI->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002793 setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurSDLoc(),
Pete Cooperf57e1c22012-01-17 01:54:07 +00002794 DestVT, N,
Bill Wendlingba54bca2013-06-19 21:36:55 +00002795 DAG.getTargetConstant(0, TLI->getPointerTy())));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002796}
2797
Stephen Lin09f8ca32013-07-06 21:44:25 +00002798void SelectionDAGBuilder::visitFPExt(const User &I) {
Hal Finkel46bb70c2011-10-18 03:51:57 +00002799 // FPExt is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002800 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002801 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002802 setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002803}
2804
Dan Gohman46510a72010-04-15 01:51:59 +00002805void SelectionDAGBuilder::visitFPToUI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002806 // FPToUI is never a no-op cast, no need to check
2807 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002808 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002809 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002810}
2811
Dan Gohman46510a72010-04-15 01:51:59 +00002812void SelectionDAGBuilder::visitFPToSI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002813 // FPToSI is never a no-op cast, no need to check
2814 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002815 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002816 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002817}
2818
Dan Gohman46510a72010-04-15 01:51:59 +00002819void SelectionDAGBuilder::visitUIToFP(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002820 // UIToFP is never a no-op cast, no need to check
2821 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002822 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002823 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002824}
2825
Stephen Lin09f8ca32013-07-06 21:44:25 +00002826void SelectionDAGBuilder::visitSIToFP(const User &I) {
Bill Wendling181b6272008-10-19 20:34:04 +00002827 // SIToFP is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002828 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002829 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002830 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002831}
2832
Dan Gohman46510a72010-04-15 01:51:59 +00002833void SelectionDAGBuilder::visitPtrToInt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002834 // What to do depends on the size of the integer and the size of the pointer.
2835 // We can either truncate, zero extend, or no-op, accordingly.
2836 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002837 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002838 setValue(&I, DAG.getZExtOrTrunc(N, getCurSDLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002839}
2840
Dan Gohman46510a72010-04-15 01:51:59 +00002841void SelectionDAGBuilder::visitIntToPtr(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002842 // What to do depends on the size of the integer and the size of the pointer.
2843 // We can either truncate, zero extend, or no-op, accordingly.
2844 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002845 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002846 setValue(&I, DAG.getZExtOrTrunc(N, getCurSDLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002847}
2848
Dan Gohman46510a72010-04-15 01:51:59 +00002849void SelectionDAGBuilder::visitBitCast(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002850 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002851 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002852
Bill Wendling49fcff82009-12-21 22:30:11 +00002853 // BitCast assures us that source and destination are the same size so this is
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002854 // either a BITCAST or a no-op.
Bill Wendling4533cac2010-01-28 21:51:40 +00002855 if (DestVT != N.getValueType())
Andrew Trickac6d9be2013-05-25 02:42:55 +00002856 setValue(&I, DAG.getNode(ISD::BITCAST, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002857 DestVT, N)); // convert types.
2858 else
Bill Wendling49fcff82009-12-21 22:30:11 +00002859 setValue(&I, N); // noop cast.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002860}
2861
Dan Gohman46510a72010-04-15 01:51:59 +00002862void SelectionDAGBuilder::visitInsertElement(const User &I) {
Tom Stellard425b76c2013-08-05 22:22:01 +00002863 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002864 SDValue InVec = getValue(I.getOperand(0));
2865 SDValue InVal = getValue(I.getOperand(1));
Tom Stellard425b76c2013-08-05 22:22:01 +00002866 SDValue InIdx = DAG.getSExtOrTrunc(getValue(I.getOperand(2)),
2867 getCurSDLoc(), TLI.getVectorIdxTy());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002868 setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurSDLoc(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00002869 TM.getTargetLowering()->getValueType(I.getType()),
Bill Wendling4533cac2010-01-28 21:51:40 +00002870 InVec, InVal, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002871}
2872
Dan Gohman46510a72010-04-15 01:51:59 +00002873void SelectionDAGBuilder::visitExtractElement(const User &I) {
Tom Stellard425b76c2013-08-05 22:22:01 +00002874 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002875 SDValue InVec = getValue(I.getOperand(0));
Tom Stellard425b76c2013-08-05 22:22:01 +00002876 SDValue InIdx = DAG.getSExtOrTrunc(getValue(I.getOperand(1)),
2877 getCurSDLoc(), TLI.getVectorIdxTy());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002878 setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurSDLoc(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00002879 TM.getTargetLowering()->getValueType(I.getType()),
2880 InVec, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002881}
2882
Craig Topper51578342012-01-04 09:23:09 +00002883// Utility for visitShuffleVector - Return true if every element in Mask,
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00002884// beginning from position Pos and ending in Pos+Size, falls within the
Craig Topper51578342012-01-04 09:23:09 +00002885// specified sequential range [L, L+Pos). or is undef.
2886static bool isSequentialInRange(const SmallVectorImpl<int> &Mask,
Craig Topper23de31b2012-04-11 03:06:35 +00002887 unsigned Pos, unsigned Size, int Low) {
2888 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Craig Topper51578342012-01-04 09:23:09 +00002889 if (Mask[i] >= 0 && Mask[i] != Low)
Nate Begeman9008ca62009-04-27 18:41:29 +00002890 return false;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002891 return true;
2892}
2893
Dan Gohman46510a72010-04-15 01:51:59 +00002894void SelectionDAGBuilder::visitShuffleVector(const User &I) {
Mon P Wang230e4fa2008-11-21 04:25:21 +00002895 SDValue Src1 = getValue(I.getOperand(0));
2896 SDValue Src2 = getValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002897
Chris Lattner56243b82012-01-26 02:51:13 +00002898 SmallVector<int, 8> Mask;
2899 ShuffleVectorInst::getShuffleMask(cast<Constant>(I.getOperand(2)), Mask);
2900 unsigned MaskNumElts = Mask.size();
Bill Wendlingba54bca2013-06-19 21:36:55 +00002901
2902 const TargetLowering *TLI = TM.getTargetLowering();
2903 EVT VT = TLI->getValueType(I.getType());
Owen Andersone50ed302009-08-10 22:56:29 +00002904 EVT SrcVT = Src1.getValueType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00002905 unsigned SrcNumElts = SrcVT.getVectorNumElements();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002906
Mon P Wangc7849c22008-11-16 05:06:27 +00002907 if (SrcNumElts == MaskNumElts) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002908 setValue(&I, DAG.getVectorShuffle(VT, getCurSDLoc(), Src1, Src2,
Bill Wendling4533cac2010-01-28 21:51:40 +00002909 &Mask[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002910 return;
2911 }
2912
2913 // Normalize the shuffle vector since mask and vector length don't match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002914 if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) {
2915 // Mask is longer than the source vectors and is a multiple of the source
2916 // vectors. We can use concatenate vector to make the mask and vectors
Mon P Wang230e4fa2008-11-21 04:25:21 +00002917 // lengths match.
Craig Topper51578342012-01-04 09:23:09 +00002918 if (SrcNumElts*2 == MaskNumElts) {
2919 // First check for Src1 in low and Src2 in high
2920 if (isSequentialInRange(Mask, 0, SrcNumElts, 0) &&
2921 isSequentialInRange(Mask, SrcNumElts, SrcNumElts, SrcNumElts)) {
2922 // The shuffle is concatenating two vectors together.
Andrew Trickac6d9be2013-05-25 02:42:55 +00002923 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurSDLoc(),
Craig Topper51578342012-01-04 09:23:09 +00002924 VT, Src1, Src2));
2925 return;
2926 }
2927 // Then check for Src2 in low and Src1 in high
2928 if (isSequentialInRange(Mask, 0, SrcNumElts, SrcNumElts) &&
2929 isSequentialInRange(Mask, SrcNumElts, SrcNumElts, 0)) {
2930 // The shuffle is concatenating two vectors together.
Andrew Trickac6d9be2013-05-25 02:42:55 +00002931 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurSDLoc(),
Craig Topper51578342012-01-04 09:23:09 +00002932 VT, Src2, Src1));
2933 return;
2934 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002935 }
2936
Mon P Wangc7849c22008-11-16 05:06:27 +00002937 // Pad both vectors with undefs to make them the same length as the mask.
2938 unsigned NumConcat = MaskNumElts / SrcNumElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00002939 bool Src1U = Src1.getOpcode() == ISD::UNDEF;
2940 bool Src2U = Src2.getOpcode() == ISD::UNDEF;
Dale Johannesene8d72302009-02-06 23:05:02 +00002941 SDValue UndefVal = DAG.getUNDEF(SrcVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002942
Nate Begeman9008ca62009-04-27 18:41:29 +00002943 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
2944 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002945 MOps1[0] = Src1;
2946 MOps2[0] = Src2;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002947
2948 Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002949 getCurSDLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002950 &MOps1[0], NumConcat);
2951 Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002952 getCurSDLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002953 &MOps2[0], NumConcat);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002954
Mon P Wangaeb06d22008-11-10 04:46:22 +00002955 // Readjust mask for new input vector length.
Nate Begeman9008ca62009-04-27 18:41:29 +00002956 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002957 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002958 int Idx = Mask[i];
Craig Topper23de31b2012-04-11 03:06:35 +00002959 if (Idx >= (int)SrcNumElts)
2960 Idx -= SrcNumElts - MaskNumElts;
2961 MappedOps.push_back(Idx);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002962 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002963
Andrew Trickac6d9be2013-05-25 02:42:55 +00002964 setValue(&I, DAG.getVectorShuffle(VT, getCurSDLoc(), Src1, Src2,
Bill Wendling4533cac2010-01-28 21:51:40 +00002965 &MappedOps[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002966 return;
2967 }
2968
Mon P Wangc7849c22008-11-16 05:06:27 +00002969 if (SrcNumElts > MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002970 // Analyze the access pattern of the vector to see if we can extract
2971 // two subvectors and do the shuffle. The analysis is done by calculating
2972 // the range of elements the mask access on both vectors.
Craig Topper10612dc2012-04-08 23:15:04 +00002973 int MinRange[2] = { static_cast<int>(SrcNumElts),
2974 static_cast<int>(SrcNumElts)};
Mon P Wangc7849c22008-11-16 05:06:27 +00002975 int MaxRange[2] = {-1, -1};
2976
Nate Begeman5a5ca152009-04-29 05:20:52 +00002977 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002978 int Idx = Mask[i];
Craig Topper10612dc2012-04-08 23:15:04 +00002979 unsigned Input = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00002980 if (Idx < 0)
2981 continue;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002982
Nate Begeman5a5ca152009-04-29 05:20:52 +00002983 if (Idx >= (int)SrcNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002984 Input = 1;
2985 Idx -= SrcNumElts;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002986 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002987 if (Idx > MaxRange[Input])
2988 MaxRange[Input] = Idx;
2989 if (Idx < MinRange[Input])
2990 MinRange[Input] = Idx;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002991 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002992
Mon P Wangc7849c22008-11-16 05:06:27 +00002993 // Check if the access is smaller than the vector size and can we find
2994 // a reasonable extract index.
Craig Topper10612dc2012-04-08 23:15:04 +00002995 int RangeUse[2] = { -1, -1 }; // 0 = Unused, 1 = Extract, -1 = Can not
2996 // Extract.
Mon P Wangc7849c22008-11-16 05:06:27 +00002997 int StartIdx[2]; // StartIdx to extract from
Craig Topper10612dc2012-04-08 23:15:04 +00002998 for (unsigned Input = 0; Input < 2; ++Input) {
2999 if (MinRange[Input] >= (int)SrcNumElts && MaxRange[Input] < 0) {
Mon P Wangc7849c22008-11-16 05:06:27 +00003000 RangeUse[Input] = 0; // Unused
3001 StartIdx[Input] = 0;
Craig Topperf873dde2012-04-08 17:53:33 +00003002 continue;
Mon P Wang230e4fa2008-11-21 04:25:21 +00003003 }
Craig Topperf873dde2012-04-08 17:53:33 +00003004
3005 // Find a good start index that is a multiple of the mask length. Then
3006 // see if the rest of the elements are in range.
3007 StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts;
3008 if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts &&
3009 StartIdx[Input] + MaskNumElts <= SrcNumElts)
3010 RangeUse[Input] = 1; // Extract from a multiple of the mask length.
Mon P Wangc7849c22008-11-16 05:06:27 +00003011 }
3012
Bill Wendling636e2582009-08-21 18:16:06 +00003013 if (RangeUse[0] == 0 && RangeUse[1] == 0) {
Bill Wendling4533cac2010-01-28 21:51:40 +00003014 setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
Mon P Wangc7849c22008-11-16 05:06:27 +00003015 return;
3016 }
Craig Topper10612dc2012-04-08 23:15:04 +00003017 if (RangeUse[0] >= 0 && RangeUse[1] >= 0) {
Mon P Wangc7849c22008-11-16 05:06:27 +00003018 // Extract appropriate subvector and generate a vector shuffle
Craig Topper10612dc2012-04-08 23:15:04 +00003019 for (unsigned Input = 0; Input < 2; ++Input) {
Bill Wendling87710f02009-12-21 23:47:40 +00003020 SDValue &Src = Input == 0 ? Src1 : Src2;
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003021 if (RangeUse[Input] == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00003022 Src = DAG.getUNDEF(VT);
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003023 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00003024 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurSDLoc(), VT,
Tom Stellard425b76c2013-08-05 22:22:01 +00003025 Src, DAG.getConstant(StartIdx[Input],
3026 TLI->getVectorIdxTy()));
Mon P Wangaeb06d22008-11-10 04:46:22 +00003027 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003028
Mon P Wangc7849c22008-11-16 05:06:27 +00003029 // Calculate new mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00003030 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003031 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003032 int Idx = Mask[i];
Craig Topper23de31b2012-04-11 03:06:35 +00003033 if (Idx >= 0) {
3034 if (Idx < (int)SrcNumElts)
3035 Idx -= StartIdx[0];
3036 else
3037 Idx -= SrcNumElts + StartIdx[1] - MaskNumElts;
3038 }
3039 MappedOps.push_back(Idx);
Mon P Wangc7849c22008-11-16 05:06:27 +00003040 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003041
Andrew Trickac6d9be2013-05-25 02:42:55 +00003042 setValue(&I, DAG.getVectorShuffle(VT, getCurSDLoc(), Src1, Src2,
Bill Wendling4533cac2010-01-28 21:51:40 +00003043 &MappedOps[0]));
Mon P Wangc7849c22008-11-16 05:06:27 +00003044 return;
Mon P Wangaeb06d22008-11-10 04:46:22 +00003045 }
3046 }
3047
Mon P Wangc7849c22008-11-16 05:06:27 +00003048 // We can't use either concat vectors or extract subvectors so fall back to
3049 // replacing the shuffle with extract and build vector.
3050 // to insert and build vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003051 EVT EltVT = VT.getVectorElementType();
Tom Stellard425b76c2013-08-05 22:22:01 +00003052 EVT IdxVT = TLI->getVectorIdxTy();
Mon P Wangaeb06d22008-11-10 04:46:22 +00003053 SmallVector<SDValue,8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003054 for (unsigned i = 0; i != MaskNumElts; ++i) {
Craig Topper23de31b2012-04-11 03:06:35 +00003055 int Idx = Mask[i];
3056 SDValue Res;
3057
3058 if (Idx < 0) {
3059 Res = DAG.getUNDEF(EltVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00003060 } else {
Craig Topper23de31b2012-04-11 03:06:35 +00003061 SDValue &Src = Idx < (int)SrcNumElts ? Src1 : Src2;
3062 if (Idx >= (int)SrcNumElts) Idx -= SrcNumElts;
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003063
Andrew Trickac6d9be2013-05-25 02:42:55 +00003064 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurSDLoc(),
Tom Stellard425b76c2013-08-05 22:22:01 +00003065 EltVT, Src, DAG.getConstant(Idx, IdxVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00003066 }
Craig Topper23de31b2012-04-11 03:06:35 +00003067
3068 Ops.push_back(Res);
Mon P Wangaeb06d22008-11-10 04:46:22 +00003069 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003070
Andrew Trickac6d9be2013-05-25 02:42:55 +00003071 setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00003072 VT, &Ops[0], Ops.size()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003073}
3074
Dan Gohman46510a72010-04-15 01:51:59 +00003075void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003076 const Value *Op0 = I.getOperand(0);
3077 const Value *Op1 = I.getOperand(1);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003078 Type *AggTy = I.getType();
3079 Type *ValTy = Op1->getType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003080 bool IntoUndef = isa<UndefValue>(Op0);
3081 bool FromUndef = isa<UndefValue>(Op1);
3082
Jay Foadfc6d3a42011-07-13 10:26:04 +00003083 unsigned LinearIndex = ComputeLinearIndex(AggTy, I.getIndices());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003084
Bill Wendlingba54bca2013-06-19 21:36:55 +00003085 const TargetLowering *TLI = TM.getTargetLowering();
Owen Andersone50ed302009-08-10 22:56:29 +00003086 SmallVector<EVT, 4> AggValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003087 ComputeValueVTs(*TLI, AggTy, AggValueVTs);
Owen Andersone50ed302009-08-10 22:56:29 +00003088 SmallVector<EVT, 4> ValValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003089 ComputeValueVTs(*TLI, ValTy, ValValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003090
3091 unsigned NumAggValues = AggValueVTs.size();
3092 unsigned NumValValues = ValValueVTs.size();
3093 SmallVector<SDValue, 4> Values(NumAggValues);
3094
3095 SDValue Agg = getValue(Op0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003096 unsigned i = 0;
3097 // Copy the beginning value(s) from the original aggregate.
3098 for (; i != LinearIndex; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00003099 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003100 SDValue(Agg.getNode(), Agg.getResNo() + i);
3101 // Copy values from the inserted value(s).
Rafael Espindola3fa82832011-05-13 15:18:06 +00003102 if (NumValValues) {
3103 SDValue Val = getValue(Op1);
3104 for (; i != LinearIndex + NumValValues; ++i)
3105 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
3106 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
3107 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003108 // Copy remaining value(s) from the original aggregate.
3109 for (; i != NumAggValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00003110 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003111 SDValue(Agg.getNode(), Agg.getResNo() + i);
3112
Andrew Trickac6d9be2013-05-25 02:42:55 +00003113 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00003114 DAG.getVTList(&AggValueVTs[0], NumAggValues),
3115 &Values[0], NumAggValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003116}
3117
Dan Gohman46510a72010-04-15 01:51:59 +00003118void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003119 const Value *Op0 = I.getOperand(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003120 Type *AggTy = Op0->getType();
3121 Type *ValTy = I.getType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003122 bool OutOfUndef = isa<UndefValue>(Op0);
3123
Jay Foadfc6d3a42011-07-13 10:26:04 +00003124 unsigned LinearIndex = ComputeLinearIndex(AggTy, I.getIndices());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003125
Bill Wendlingba54bca2013-06-19 21:36:55 +00003126 const TargetLowering *TLI = TM.getTargetLowering();
Owen Andersone50ed302009-08-10 22:56:29 +00003127 SmallVector<EVT, 4> ValValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003128 ComputeValueVTs(*TLI, ValTy, ValValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003129
3130 unsigned NumValValues = ValValueVTs.size();
Rafael Espindola3fa82832011-05-13 15:18:06 +00003131
3132 // Ignore a extractvalue that produces an empty object
3133 if (!NumValValues) {
3134 setValue(&I, DAG.getUNDEF(MVT(MVT::Other)));
3135 return;
3136 }
3137
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003138 SmallVector<SDValue, 4> Values(NumValValues);
3139
3140 SDValue Agg = getValue(Op0);
3141 // Copy out the selected value(s).
3142 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
3143 Values[i - LinearIndex] =
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00003144 OutOfUndef ?
Dale Johannesene8d72302009-02-06 23:05:02 +00003145 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00003146 SDValue(Agg.getNode(), Agg.getResNo() + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003147
Andrew Trickac6d9be2013-05-25 02:42:55 +00003148 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00003149 DAG.getVTList(&ValValueVTs[0], NumValValues),
3150 &Values[0], NumValValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003151}
3152
Dan Gohman46510a72010-04-15 01:51:59 +00003153void SelectionDAGBuilder::visitGetElementPtr(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003154 SDValue N = getValue(I.getOperand(0));
Nadav Rotem1c239202012-02-28 14:13:19 +00003155 // Note that the pointer operand may be a vector of pointers. Take the scalar
3156 // element which holds a pointer.
3157 Type *Ty = I.getOperand(0)->getType()->getScalarType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003158
Dan Gohman46510a72010-04-15 01:51:59 +00003159 for (GetElementPtrInst::const_op_iterator OI = I.op_begin()+1, E = I.op_end();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003160 OI != E; ++OI) {
Dan Gohman46510a72010-04-15 01:51:59 +00003161 const Value *Idx = *OI;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003162 if (StructType *StTy = dyn_cast<StructType>(Ty)) {
Duncan Sandsb2df01a2012-11-13 13:01:58 +00003163 unsigned Field = cast<Constant>(Idx)->getUniqueInteger().getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003164 if (Field) {
3165 // N = N + Offset
3166 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003167 N = DAG.getNode(ISD::ADD, getCurSDLoc(), N.getValueType(), N,
Duncan Sandsb2df01a2012-11-13 13:01:58 +00003168 DAG.getConstant(Offset, N.getValueType()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003169 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00003170
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003171 Ty = StTy->getElementType(Field);
3172 } else {
3173 Ty = cast<SequentialType>(Ty)->getElementType();
3174
3175 // If this is a constant subscript, handle it quickly.
Bill Wendlingba54bca2013-06-19 21:36:55 +00003176 const TargetLowering *TLI = TM.getTargetLowering();
Dan Gohman46510a72010-04-15 01:51:59 +00003177 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Dan Gohmane368b462010-06-18 14:22:04 +00003178 if (CI->isZero()) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003179 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +00003180 TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Evan Cheng65b52df2009-02-09 21:01:06 +00003181 SDValue OffsVal;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003182 EVT PTy = TLI->getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00003183 unsigned PtrBits = PTy.getSizeInBits();
Bill Wendlinge1a90422009-12-21 23:10:19 +00003184 if (PtrBits < 64)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003185 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00003186 TLI->getPointerTy(),
Owen Anderson825b72b2009-08-11 20:47:22 +00003187 DAG.getConstant(Offs, MVT::i64));
Bill Wendlinge1a90422009-12-21 23:10:19 +00003188 else
Evan Chengb1032a82009-02-09 20:54:38 +00003189 OffsVal = DAG.getIntPtrConstant(Offs);
Bill Wendlinge1a90422009-12-21 23:10:19 +00003190
Andrew Trickac6d9be2013-05-25 02:42:55 +00003191 N = DAG.getNode(ISD::ADD, getCurSDLoc(), N.getValueType(), N,
Evan Chengb1032a82009-02-09 20:54:38 +00003192 OffsVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003193 continue;
3194 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003195
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003196 // N = N + Idx * ElementSize;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003197 APInt ElementSize = APInt(TLI->getPointerTy().getSizeInBits(),
Dan Gohman7abbd042009-10-23 17:57:43 +00003198 TD->getTypeAllocSize(Ty));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003199 SDValue IdxN = getValue(Idx);
3200
3201 // If the index is smaller or larger than intptr_t, truncate or extend
3202 // it.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003203 IdxN = DAG.getSExtOrTrunc(IdxN, getCurSDLoc(), N.getValueType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003204
3205 // If this is a multiply by a power of two, turn it into a shl
3206 // immediately. This is a very common case.
3207 if (ElementSize != 1) {
Dan Gohman7abbd042009-10-23 17:57:43 +00003208 if (ElementSize.isPowerOf2()) {
3209 unsigned Amt = ElementSize.logBase2();
Andrew Trickac6d9be2013-05-25 02:42:55 +00003210 IdxN = DAG.getNode(ISD::SHL, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003211 N.getValueType(), IdxN,
Nadav Rotem16087692011-12-05 06:29:09 +00003212 DAG.getConstant(Amt, IdxN.getValueType()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003213 } else {
Duncan Sandsb2df01a2012-11-13 13:01:58 +00003214 SDValue Scale = DAG.getConstant(ElementSize, IdxN.getValueType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003215 IdxN = DAG.getNode(ISD::MUL, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003216 N.getValueType(), IdxN, Scale);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003217 }
3218 }
3219
Andrew Trickac6d9be2013-05-25 02:42:55 +00003220 N = DAG.getNode(ISD::ADD, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003221 N.getValueType(), N, IdxN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003222 }
3223 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00003224
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003225 setValue(&I, N);
3226}
3227
Dan Gohman46510a72010-04-15 01:51:59 +00003228void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003229 // If this is a fixed sized alloca in the entry block of the function,
3230 // allocate it statically on the stack.
3231 if (FuncInfo.StaticAllocaMap.count(&I))
3232 return; // getValue will auto-populate this.
3233
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003234 Type *Ty = I.getAllocatedType();
Bill Wendlingba54bca2013-06-19 21:36:55 +00003235 const TargetLowering *TLI = TM.getTargetLowering();
3236 uint64_t TySize = TLI->getDataLayout()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003237 unsigned Align =
Bill Wendlingba54bca2013-06-19 21:36:55 +00003238 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003239 I.getAlignment());
3240
3241 SDValue AllocSize = getValue(I.getArraySize());
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003242
Bill Wendlingba54bca2013-06-19 21:36:55 +00003243 EVT IntPtr = TLI->getPointerTy();
Dan Gohmanf75a7d32010-05-28 01:14:11 +00003244 if (AllocSize.getValueType() != IntPtr)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003245 AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurSDLoc(), IntPtr);
Dan Gohmanf75a7d32010-05-28 01:14:11 +00003246
Andrew Trickac6d9be2013-05-25 02:42:55 +00003247 AllocSize = DAG.getNode(ISD::MUL, getCurSDLoc(), IntPtr,
Dan Gohmanf75a7d32010-05-28 01:14:11 +00003248 AllocSize,
3249 DAG.getConstant(TySize, IntPtr));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003250
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003251 // Handle alignment. If the requested alignment is less than or equal to
3252 // the stack alignment, ignore it. If the size is greater than or equal to
3253 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00003254 unsigned StackAlign = TM.getFrameLowering()->getStackAlignment();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003255 if (Align <= StackAlign)
3256 Align = 0;
3257
3258 // Round the size of the allocation up to the stack alignment size
3259 // by add SA-1 to the size.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003260 AllocSize = DAG.getNode(ISD::ADD, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003261 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003262 DAG.getIntPtrConstant(StackAlign-1));
Bill Wendling856ff412009-12-22 00:12:37 +00003263
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003264 // Mask out the low bits for alignment purposes.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003265 AllocSize = DAG.getNode(ISD::AND, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003266 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003267 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
3268
3269 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Owen Anderson825b72b2009-08-11 20:47:22 +00003270 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003271 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurSDLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003272 VTs, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003273 setValue(&I, DSA);
3274 DAG.setRoot(DSA.getValue(1));
Bill Wendling856ff412009-12-22 00:12:37 +00003275
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003276 // Inform the Frame Information that we have just allocated a variable-sized
3277 // object.
Bob Wilson8f637ad2013-02-08 20:35:15 +00003278 FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject(Align ? Align : 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003279}
3280
Dan Gohman46510a72010-04-15 01:51:59 +00003281void SelectionDAGBuilder::visitLoad(const LoadInst &I) {
Eli Friedman327236c2011-08-24 20:50:09 +00003282 if (I.isAtomic())
3283 return visitAtomicLoad(I);
3284
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003285 const Value *SV = I.getOperand(0);
3286 SDValue Ptr = getValue(SV);
3287
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003288 Type *Ty = I.getType();
David Greene1e559442010-02-15 17:00:31 +00003289
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003290 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00003291 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Pete Cooperd752e0f2011-11-08 18:42:53 +00003292 bool isInvariant = I.getMetadata("invariant.load") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003293 unsigned Alignment = I.getAlignment();
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00003294 const MDNode *TBAAInfo = I.getMetadata(LLVMContext::MD_tbaa);
Rafael Espindola95d594c2012-03-31 18:14:00 +00003295 const MDNode *Ranges = I.getMetadata(LLVMContext::MD_range);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003296
Owen Andersone50ed302009-08-10 22:56:29 +00003297 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003298 SmallVector<uint64_t, 4> Offsets;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003299 ComputeValueVTs(*TM.getTargetLowering(), Ty, ValueVTs, &Offsets);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003300 unsigned NumValues = ValueVTs.size();
3301 if (NumValues == 0)
3302 return;
3303
3304 SDValue Root;
3305 bool ConstantMemory = false;
Andrew Trickde91f3c2010-11-12 17:50:46 +00003306 if (I.isVolatile() || NumValues > MaxParallelChains)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003307 // Serialize volatile loads with other side effects.
3308 Root = getRoot();
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00003309 else if (AA->pointsToConstantMemory(
3310 AliasAnalysis::Location(SV, AA->getTypeStoreSize(Ty), TBAAInfo))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003311 // Do not serialize (non-volatile) loads of constant memory with anything.
3312 Root = DAG.getEntryNode();
3313 ConstantMemory = true;
3314 } else {
3315 // Do not serialize non-volatile loads against each other.
3316 Root = DAG.getRoot();
3317 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003318
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003319 SmallVector<SDValue, 4> Values(NumValues);
Andrew Trickde91f3c2010-11-12 17:50:46 +00003320 SmallVector<SDValue, 4> Chains(std::min(unsigned(MaxParallelChains),
3321 NumValues));
Owen Andersone50ed302009-08-10 22:56:29 +00003322 EVT PtrVT = Ptr.getValueType();
Andrew Trickde91f3c2010-11-12 17:50:46 +00003323 unsigned ChainI = 0;
3324 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
3325 // Serializing loads here may result in excessive register pressure, and
3326 // TokenFactor places arbitrary choke points on the scheduler. SD scheduling
3327 // could recover a bit by hoisting nodes upward in the chain by recognizing
3328 // they are side-effect free or do not alias. The optimizer should really
3329 // avoid this case by converting large object/array copies to llvm.memcpy
3330 // (MaxParallelChains should always remain as failsafe).
3331 if (ChainI == MaxParallelChains) {
3332 assert(PendingLoads.empty() && "PendingLoads must be serialized first");
Andrew Trickac6d9be2013-05-25 02:42:55 +00003333 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003334 MVT::Other, &Chains[0], ChainI);
3335 Root = Chain;
3336 ChainI = 0;
3337 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00003338 SDValue A = DAG.getNode(ISD::ADD, getCurSDLoc(),
Bill Wendling856ff412009-12-22 00:12:37 +00003339 PtrVT, Ptr,
3340 DAG.getConstant(Offsets[i], PtrVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003341 SDValue L = DAG.getLoad(ValueVTs[i], getCurSDLoc(), Root,
Michael J. Spencere70c5262010-10-16 08:25:21 +00003342 A, MachinePointerInfo(SV, Offsets[i]), isVolatile,
Rafael Espindola95d594c2012-03-31 18:14:00 +00003343 isNonTemporal, isInvariant, Alignment, TBAAInfo,
3344 Ranges);
Bill Wendling856ff412009-12-22 00:12:37 +00003345
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003346 Values[i] = L;
Andrew Trickde91f3c2010-11-12 17:50:46 +00003347 Chains[ChainI] = L.getValue(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003348 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003349
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003350 if (!ConstantMemory) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003351 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003352 MVT::Other, &Chains[0], ChainI);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003353 if (isVolatile)
3354 DAG.setRoot(Chain);
3355 else
3356 PendingLoads.push_back(Chain);
3357 }
3358
Andrew Trickac6d9be2013-05-25 02:42:55 +00003359 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00003360 DAG.getVTList(&ValueVTs[0], NumValues),
3361 &Values[0], NumValues));
Bill Wendling856ff412009-12-22 00:12:37 +00003362}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003363
Dan Gohman46510a72010-04-15 01:51:59 +00003364void SelectionDAGBuilder::visitStore(const StoreInst &I) {
Eli Friedman327236c2011-08-24 20:50:09 +00003365 if (I.isAtomic())
3366 return visitAtomicStore(I);
3367
Dan Gohman46510a72010-04-15 01:51:59 +00003368 const Value *SrcV = I.getOperand(0);
3369 const Value *PtrV = I.getOperand(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003370
Owen Andersone50ed302009-08-10 22:56:29 +00003371 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003372 SmallVector<uint64_t, 4> Offsets;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003373 ComputeValueVTs(*TM.getTargetLowering(), SrcV->getType(), ValueVTs, &Offsets);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003374 unsigned NumValues = ValueVTs.size();
3375 if (NumValues == 0)
3376 return;
3377
3378 // Get the lowered operands. Note that we do this after
3379 // checking if NumResults is zero, because with zero results
3380 // the operands won't have values in the map.
3381 SDValue Src = getValue(SrcV);
3382 SDValue Ptr = getValue(PtrV);
3383
3384 SDValue Root = getRoot();
Andrew Trickde91f3c2010-11-12 17:50:46 +00003385 SmallVector<SDValue, 4> Chains(std::min(unsigned(MaxParallelChains),
3386 NumValues));
Owen Andersone50ed302009-08-10 22:56:29 +00003387 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003388 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00003389 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003390 unsigned Alignment = I.getAlignment();
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00003391 const MDNode *TBAAInfo = I.getMetadata(LLVMContext::MD_tbaa);
Bill Wendling856ff412009-12-22 00:12:37 +00003392
Andrew Trickde91f3c2010-11-12 17:50:46 +00003393 unsigned ChainI = 0;
3394 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
3395 // See visitLoad comments.
3396 if (ChainI == MaxParallelChains) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003397 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003398 MVT::Other, &Chains[0], ChainI);
3399 Root = Chain;
3400 ChainI = 0;
3401 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00003402 SDValue Add = DAG.getNode(ISD::ADD, getCurSDLoc(), PtrVT, Ptr,
Bill Wendling856ff412009-12-22 00:12:37 +00003403 DAG.getConstant(Offsets[i], PtrVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003404 SDValue St = DAG.getStore(Root, getCurSDLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003405 SDValue(Src.getNode(), Src.getResNo() + i),
3406 Add, MachinePointerInfo(PtrV, Offsets[i]),
3407 isVolatile, isNonTemporal, Alignment, TBAAInfo);
3408 Chains[ChainI] = St;
Bill Wendling856ff412009-12-22 00:12:37 +00003409 }
3410
Andrew Trickac6d9be2013-05-25 02:42:55 +00003411 SDValue StoreNode = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003412 MVT::Other, &Chains[0], ChainI);
Devang Patel7e13efa2010-10-26 22:14:52 +00003413 DAG.setRoot(StoreNode);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003414}
3415
Eli Friedman26689ac2011-08-03 21:06:02 +00003416static SDValue InsertFenceForAtomic(SDValue Chain, AtomicOrdering Order,
Eli Friedman327236c2011-08-24 20:50:09 +00003417 SynchronizationScope Scope,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003418 bool Before, SDLoc dl,
Eli Friedman26689ac2011-08-03 21:06:02 +00003419 SelectionDAG &DAG,
3420 const TargetLowering &TLI) {
3421 // Fence, if necessary
3422 if (Before) {
Eli Friedman069e2ed2011-08-26 02:59:24 +00003423 if (Order == AcquireRelease || Order == SequentiallyConsistent)
Eli Friedman26689ac2011-08-03 21:06:02 +00003424 Order = Release;
3425 else if (Order == Acquire || Order == Monotonic)
3426 return Chain;
3427 } else {
3428 if (Order == AcquireRelease)
3429 Order = Acquire;
3430 else if (Order == Release || Order == Monotonic)
3431 return Chain;
3432 }
3433 SDValue Ops[3];
3434 Ops[0] = Chain;
Eli Friedman327236c2011-08-24 20:50:09 +00003435 Ops[1] = DAG.getConstant(Order, TLI.getPointerTy());
3436 Ops[2] = DAG.getConstant(Scope, TLI.getPointerTy());
Eli Friedman26689ac2011-08-03 21:06:02 +00003437 return DAG.getNode(ISD::ATOMIC_FENCE, dl, MVT::Other, Ops, 3);
3438}
3439
Eli Friedmanff030482011-07-28 21:48:00 +00003440void SelectionDAGBuilder::visitAtomicCmpXchg(const AtomicCmpXchgInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003441 SDLoc dl = getCurSDLoc();
Eli Friedman26689ac2011-08-03 21:06:02 +00003442 AtomicOrdering Order = I.getOrdering();
Eli Friedman327236c2011-08-24 20:50:09 +00003443 SynchronizationScope Scope = I.getSynchScope();
Eli Friedman26689ac2011-08-03 21:06:02 +00003444
3445 SDValue InChain = getRoot();
3446
Bill Wendlingba54bca2013-06-19 21:36:55 +00003447 const TargetLowering *TLI = TM.getTargetLowering();
3448 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003449 InChain = InsertFenceForAtomic(InChain, Order, Scope, true, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003450 DAG, *TLI);
Eli Friedman26689ac2011-08-03 21:06:02 +00003451
Eli Friedman55ba8162011-07-29 03:05:32 +00003452 SDValue L =
Eli Friedman26689ac2011-08-03 21:06:02 +00003453 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl,
Eli Friedman55ba8162011-07-29 03:05:32 +00003454 getValue(I.getCompareOperand()).getValueType().getSimpleVT(),
Eli Friedman26689ac2011-08-03 21:06:02 +00003455 InChain,
Eli Friedman55ba8162011-07-29 03:05:32 +00003456 getValue(I.getPointerOperand()),
3457 getValue(I.getCompareOperand()),
3458 getValue(I.getNewValOperand()),
3459 MachinePointerInfo(I.getPointerOperand()), 0 /* Alignment */,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003460 TLI->getInsertFencesForAtomic() ? Monotonic : Order,
Eli Friedman327236c2011-08-24 20:50:09 +00003461 Scope);
Eli Friedman26689ac2011-08-03 21:06:02 +00003462
3463 SDValue OutChain = L.getValue(1);
3464
Bill Wendlingba54bca2013-06-19 21:36:55 +00003465 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003466 OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003467 DAG, *TLI);
Eli Friedman26689ac2011-08-03 21:06:02 +00003468
Eli Friedman55ba8162011-07-29 03:05:32 +00003469 setValue(&I, L);
Eli Friedman26689ac2011-08-03 21:06:02 +00003470 DAG.setRoot(OutChain);
Eli Friedmanff030482011-07-28 21:48:00 +00003471}
3472
3473void SelectionDAGBuilder::visitAtomicRMW(const AtomicRMWInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003474 SDLoc dl = getCurSDLoc();
Eli Friedman55ba8162011-07-29 03:05:32 +00003475 ISD::NodeType NT;
3476 switch (I.getOperation()) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003477 default: llvm_unreachable("Unknown atomicrmw operation");
Eli Friedman55ba8162011-07-29 03:05:32 +00003478 case AtomicRMWInst::Xchg: NT = ISD::ATOMIC_SWAP; break;
3479 case AtomicRMWInst::Add: NT = ISD::ATOMIC_LOAD_ADD; break;
3480 case AtomicRMWInst::Sub: NT = ISD::ATOMIC_LOAD_SUB; break;
3481 case AtomicRMWInst::And: NT = ISD::ATOMIC_LOAD_AND; break;
3482 case AtomicRMWInst::Nand: NT = ISD::ATOMIC_LOAD_NAND; break;
3483 case AtomicRMWInst::Or: NT = ISD::ATOMIC_LOAD_OR; break;
3484 case AtomicRMWInst::Xor: NT = ISD::ATOMIC_LOAD_XOR; break;
3485 case AtomicRMWInst::Max: NT = ISD::ATOMIC_LOAD_MAX; break;
3486 case AtomicRMWInst::Min: NT = ISD::ATOMIC_LOAD_MIN; break;
3487 case AtomicRMWInst::UMax: NT = ISD::ATOMIC_LOAD_UMAX; break;
3488 case AtomicRMWInst::UMin: NT = ISD::ATOMIC_LOAD_UMIN; break;
3489 }
Eli Friedman26689ac2011-08-03 21:06:02 +00003490 AtomicOrdering Order = I.getOrdering();
Eli Friedman327236c2011-08-24 20:50:09 +00003491 SynchronizationScope Scope = I.getSynchScope();
Eli Friedman26689ac2011-08-03 21:06:02 +00003492
3493 SDValue InChain = getRoot();
3494
Bill Wendlingba54bca2013-06-19 21:36:55 +00003495 const TargetLowering *TLI = TM.getTargetLowering();
3496 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003497 InChain = InsertFenceForAtomic(InChain, Order, Scope, true, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003498 DAG, *TLI);
Eli Friedman26689ac2011-08-03 21:06:02 +00003499
Eli Friedman55ba8162011-07-29 03:05:32 +00003500 SDValue L =
Eli Friedman26689ac2011-08-03 21:06:02 +00003501 DAG.getAtomic(NT, dl,
Eli Friedman55ba8162011-07-29 03:05:32 +00003502 getValue(I.getValOperand()).getValueType().getSimpleVT(),
Eli Friedman26689ac2011-08-03 21:06:02 +00003503 InChain,
Eli Friedman55ba8162011-07-29 03:05:32 +00003504 getValue(I.getPointerOperand()),
3505 getValue(I.getValOperand()),
3506 I.getPointerOperand(), 0 /* Alignment */,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003507 TLI->getInsertFencesForAtomic() ? Monotonic : Order,
Eli Friedman327236c2011-08-24 20:50:09 +00003508 Scope);
Eli Friedman26689ac2011-08-03 21:06:02 +00003509
3510 SDValue OutChain = L.getValue(1);
3511
Bill Wendlingba54bca2013-06-19 21:36:55 +00003512 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003513 OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003514 DAG, *TLI);
Eli Friedman26689ac2011-08-03 21:06:02 +00003515
Eli Friedman55ba8162011-07-29 03:05:32 +00003516 setValue(&I, L);
Eli Friedman26689ac2011-08-03 21:06:02 +00003517 DAG.setRoot(OutChain);
Eli Friedmanff030482011-07-28 21:48:00 +00003518}
3519
Eli Friedman47f35132011-07-25 23:16:38 +00003520void SelectionDAGBuilder::visitFence(const FenceInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003521 SDLoc dl = getCurSDLoc();
Bill Wendlingba54bca2013-06-19 21:36:55 +00003522 const TargetLowering *TLI = TM.getTargetLowering();
Eli Friedman14648462011-07-27 22:21:52 +00003523 SDValue Ops[3];
3524 Ops[0] = getRoot();
Bill Wendlingba54bca2013-06-19 21:36:55 +00003525 Ops[1] = DAG.getConstant(I.getOrdering(), TLI->getPointerTy());
3526 Ops[2] = DAG.getConstant(I.getSynchScope(), TLI->getPointerTy());
Eli Friedman14648462011-07-27 22:21:52 +00003527 DAG.setRoot(DAG.getNode(ISD::ATOMIC_FENCE, dl, MVT::Other, Ops, 3));
Eli Friedman47f35132011-07-25 23:16:38 +00003528}
3529
Eli Friedman327236c2011-08-24 20:50:09 +00003530void SelectionDAGBuilder::visitAtomicLoad(const LoadInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003531 SDLoc dl = getCurSDLoc();
Eli Friedman327236c2011-08-24 20:50:09 +00003532 AtomicOrdering Order = I.getOrdering();
3533 SynchronizationScope Scope = I.getSynchScope();
3534
3535 SDValue InChain = getRoot();
3536
Bill Wendlingba54bca2013-06-19 21:36:55 +00003537 const TargetLowering *TLI = TM.getTargetLowering();
3538 EVT VT = TLI->getValueType(I.getType());
Eli Friedman327236c2011-08-24 20:50:09 +00003539
Evan Cheng607acd62013-02-06 02:06:33 +00003540 if (I.getAlignment() < VT.getSizeInBits() / 8)
Eli Friedmanfe731212011-09-13 20:50:54 +00003541 report_fatal_error("Cannot generate unaligned atomic load");
3542
Eli Friedman327236c2011-08-24 20:50:09 +00003543 SDValue L =
3544 DAG.getAtomic(ISD::ATOMIC_LOAD, dl, VT, VT, InChain,
3545 getValue(I.getPointerOperand()),
3546 I.getPointerOperand(), I.getAlignment(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00003547 TLI->getInsertFencesForAtomic() ? Monotonic : Order,
Eli Friedman327236c2011-08-24 20:50:09 +00003548 Scope);
3549
3550 SDValue OutChain = L.getValue(1);
3551
Bill Wendlingba54bca2013-06-19 21:36:55 +00003552 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003553 OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003554 DAG, *TLI);
Eli Friedman327236c2011-08-24 20:50:09 +00003555
3556 setValue(&I, L);
3557 DAG.setRoot(OutChain);
3558}
3559
3560void SelectionDAGBuilder::visitAtomicStore(const StoreInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003561 SDLoc dl = getCurSDLoc();
Eli Friedman327236c2011-08-24 20:50:09 +00003562
3563 AtomicOrdering Order = I.getOrdering();
3564 SynchronizationScope Scope = I.getSynchScope();
3565
3566 SDValue InChain = getRoot();
3567
Bill Wendlingba54bca2013-06-19 21:36:55 +00003568 const TargetLowering *TLI = TM.getTargetLowering();
3569 EVT VT = TLI->getValueType(I.getValueOperand()->getType());
Eli Friedmanfe731212011-09-13 20:50:54 +00003570
Evan Cheng607acd62013-02-06 02:06:33 +00003571 if (I.getAlignment() < VT.getSizeInBits() / 8)
Eli Friedmanfe731212011-09-13 20:50:54 +00003572 report_fatal_error("Cannot generate unaligned atomic store");
3573
Bill Wendlingba54bca2013-06-19 21:36:55 +00003574 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003575 InChain = InsertFenceForAtomic(InChain, Order, Scope, true, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003576 DAG, *TLI);
Eli Friedman327236c2011-08-24 20:50:09 +00003577
3578 SDValue OutChain =
Eli Friedmanfe731212011-09-13 20:50:54 +00003579 DAG.getAtomic(ISD::ATOMIC_STORE, dl, VT,
Eli Friedman327236c2011-08-24 20:50:09 +00003580 InChain,
3581 getValue(I.getPointerOperand()),
3582 getValue(I.getValueOperand()),
3583 I.getPointerOperand(), I.getAlignment(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00003584 TLI->getInsertFencesForAtomic() ? Monotonic : Order,
Eli Friedman327236c2011-08-24 20:50:09 +00003585 Scope);
3586
Bill Wendlingba54bca2013-06-19 21:36:55 +00003587 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003588 OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003589 DAG, *TLI);
Eli Friedman327236c2011-08-24 20:50:09 +00003590
3591 DAG.setRoot(OutChain);
3592}
3593
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003594/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
3595/// node.
Dan Gohman46510a72010-04-15 01:51:59 +00003596void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I,
Dan Gohman2048b852009-11-23 18:04:58 +00003597 unsigned Intrinsic) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003598 bool HasChain = !I.doesNotAccessMemory();
3599 bool OnlyLoad = HasChain && I.onlyReadsMemory();
3600
3601 // Build the operand list.
3602 SmallVector<SDValue, 8> Ops;
3603 if (HasChain) { // If this intrinsic has side-effects, chainify it.
3604 if (OnlyLoad) {
3605 // We don't need to serialize loads against other loads.
3606 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003607 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003608 Ops.push_back(getRoot());
3609 }
3610 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003611
3612 // Info is set by getTgtMemInstrinsic
3613 TargetLowering::IntrinsicInfo Info;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003614 const TargetLowering *TLI = TM.getTargetLowering();
3615 bool IsTgtIntrinsic = TLI->getTgtMemIntrinsic(Info, I, Intrinsic);
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003616
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003617 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Bob Wilson65ffec42010-09-21 17:56:22 +00003618 if (!IsTgtIntrinsic || Info.opc == ISD::INTRINSIC_VOID ||
3619 Info.opc == ISD::INTRINSIC_W_CHAIN)
Bill Wendlingba54bca2013-06-19 21:36:55 +00003620 Ops.push_back(DAG.getTargetConstant(Intrinsic, TLI->getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003621
3622 // Add all operands of the call to the operand list.
Gabor Greif0635f352010-06-25 09:38:13 +00003623 for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) {
3624 SDValue Op = getValue(I.getArgOperand(i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003625 Ops.push_back(Op);
3626 }
3627
Owen Andersone50ed302009-08-10 22:56:29 +00003628 SmallVector<EVT, 4> ValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003629 ComputeValueVTs(*TLI, I.getType(), ValueVTs);
Bill Wendling856ff412009-12-22 00:12:37 +00003630
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003631 if (HasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00003632 ValueVTs.push_back(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003633
Bob Wilson8d919552009-07-31 22:41:21 +00003634 SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003635
3636 // Create the node.
3637 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003638 if (IsTgtIntrinsic) {
3639 // This is target intrinsic that touches memory
Andrew Trickac6d9be2013-05-25 02:42:55 +00003640 Result = DAG.getMemIntrinsicNode(Info.opc, getCurSDLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003641 VTs, &Ops[0], Ops.size(),
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00003642 Info.memVT,
3643 MachinePointerInfo(Info.ptrVal, Info.offset),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003644 Info.align, Info.vol,
3645 Info.readMem, Info.writeMem);
Bill Wendling856ff412009-12-22 00:12:37 +00003646 } else if (!HasChain) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003647 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurSDLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003648 VTs, &Ops[0], Ops.size());
Benjamin Kramerf0127052010-01-05 13:12:22 +00003649 } else if (!I.getType()->isVoidTy()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003650 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurSDLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003651 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003652 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003653 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003654 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003655 }
3656
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003657 if (HasChain) {
3658 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
3659 if (OnlyLoad)
3660 PendingLoads.push_back(Chain);
3661 else
3662 DAG.setRoot(Chain);
3663 }
Bill Wendling856ff412009-12-22 00:12:37 +00003664
Benjamin Kramerf0127052010-01-05 13:12:22 +00003665 if (!I.getType()->isVoidTy()) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003666 if (VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00003667 EVT VT = TLI->getValueType(PTy);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003668 Result = DAG.getNode(ISD::BITCAST, getCurSDLoc(), VT, Result);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003669 }
Bill Wendling856ff412009-12-22 00:12:37 +00003670
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003671 setValue(&I, Result);
3672 }
3673}
3674
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003675/// GetSignificand - Get the significand and build it into a floating-point
3676/// number with exponent of 1:
3677///
3678/// Op = (Op & 0x007fffff) | 0x3f800000;
3679///
Matt Beaumont-Gay50e75bf2013-02-25 18:11:18 +00003680/// where Op is the hexadecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003681static SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00003682GetSignificand(SelectionDAG &DAG, SDValue Op, SDLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003683 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3684 DAG.getConstant(0x007fffff, MVT::i32));
3685 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
3686 DAG.getConstant(0x3f800000, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003687 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00003688}
3689
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003690/// GetExponent - Get the exponent:
3691///
Bill Wendlinge9a72862009-01-20 21:17:57 +00003692/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003693///
Matt Beaumont-Gay50e75bf2013-02-25 18:11:18 +00003694/// where Op is the hexadecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003695static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00003696GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003697 SDLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003698 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3699 DAG.getConstant(0x7f800000, MVT::i32));
3700 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Duncan Sands92abc622009-01-31 15:50:11 +00003701 DAG.getConstant(23, TLI.getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00003702 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
3703 DAG.getConstant(127, MVT::i32));
Bill Wendling4533cac2010-01-28 21:51:40 +00003704 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00003705}
3706
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003707/// getF32Constant - Get 32-bit floating point constant.
3708static SDValue
3709getF32Constant(SelectionDAG &DAG, unsigned Flt) {
Tim Northover0a29cb02013-01-22 09:46:31 +00003710 return DAG.getConstantFP(APFloat(APFloat::IEEEsingle, APInt(32, Flt)),
3711 MVT::f32);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003712}
3713
Craig Topper538cd482012-11-24 18:52:06 +00003714/// expandExp - Lower an exp intrinsic. Handles the special sequences for
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003715/// limited-precision mode.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003716static SDValue expandExp(SDLoc dl, SDValue Op, SelectionDAG &DAG,
Craig Topper538cd482012-11-24 18:52:06 +00003717 const TargetLowering &TLI) {
3718 if (Op.getValueType() == MVT::f32 &&
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003719 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003720
3721 // Put the exponent in the right bit position for later addition to the
3722 // final result:
3723 //
3724 // #define LOG2OFe 1.4426950f
3725 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Owen Anderson825b72b2009-08-11 20:47:22 +00003726 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003727 getF32Constant(DAG, 0x3fb8aa3b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003728 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003729
3730 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003731 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3732 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003733
3734 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003735 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003736 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendling856ff412009-12-22 00:12:37 +00003737
Craig Topperb3157722012-11-24 08:22:37 +00003738 SDValue TwoToFracPartOfX;
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003739 if (LimitFloatPrecision <= 6) {
3740 // For floating-point precision of 6:
3741 //
3742 // TwoToFractionalPartOfX =
3743 // 0.997535578f +
3744 // (0.735607626f + 0.252464424f * x) * x;
3745 //
3746 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003747 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003748 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003749 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003750 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003751 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
Craig Topperb3157722012-11-24 08:22:37 +00003752 TwoToFracPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
3753 getF32Constant(DAG, 0x3f7f5e7e));
Craig Topper08ac4692012-11-16 20:01:39 +00003754 } else if (LimitFloatPrecision <= 12) {
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003755 // For floating-point precision of 12:
3756 //
3757 // TwoToFractionalPartOfX =
3758 // 0.999892986f +
3759 // (0.696457318f +
3760 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3761 //
3762 // 0.000107046256 error, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003763 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003764 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003765 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003766 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003767 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3768 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003769 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003770 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
Craig Topperb3157722012-11-24 08:22:37 +00003771 TwoToFracPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
3772 getF32Constant(DAG, 0x3f7ff8fd));
Craig Topper08ac4692012-11-16 20:01:39 +00003773 } else { // LimitFloatPrecision <= 18
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003774 // For floating-point precision of 18:
3775 //
3776 // TwoToFractionalPartOfX =
3777 // 0.999999982f +
3778 // (0.693148872f +
3779 // (0.240227044f +
3780 // (0.554906021e-1f +
3781 // (0.961591928e-2f +
3782 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3783 //
3784 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003785 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003786 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003787 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003788 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003789 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3790 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003791 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003792 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3793 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003794 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003795 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3796 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003797 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003798 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3799 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003800 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003801 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
Craig Topperb3157722012-11-24 08:22:37 +00003802 TwoToFracPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
3803 getF32Constant(DAG, 0x3f800000));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003804 }
Craig Topperb3157722012-11-24 08:22:37 +00003805
3806 // Add the exponent into the result in integer domain.
3807 SDValue t13 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, TwoToFracPartOfX);
Craig Topper538cd482012-11-24 18:52:06 +00003808 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3809 DAG.getNode(ISD::ADD, dl, MVT::i32,
3810 t13, IntegerPartOfX));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003811 }
3812
Craig Topper538cd482012-11-24 18:52:06 +00003813 // No special expansion.
3814 return DAG.getNode(ISD::FEXP, dl, Op.getValueType(), Op);
Dale Johannesen59e577f2008-09-05 18:38:42 +00003815}
3816
Craig Topper5d1e0892012-11-23 18:38:31 +00003817/// expandLog - Lower a log intrinsic. Handles the special sequences for
Bill Wendling39150252008-09-09 20:39:27 +00003818/// limited-precision mode.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003819static SDValue expandLog(SDLoc dl, SDValue Op, SelectionDAG &DAG,
Craig Topper5d1e0892012-11-23 18:38:31 +00003820 const TargetLowering &TLI) {
3821 if (Op.getValueType() == MVT::f32 &&
Bill Wendling39150252008-09-09 20:39:27 +00003822 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003823 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00003824
3825 // Scale the exponent by log(2) [0.69314718f].
Bill Wendling46ada192010-03-02 01:55:18 +00003826 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003827 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003828 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003829
3830 // Get the significand and build it into a floating-point number with
3831 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003832 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling39150252008-09-09 20:39:27 +00003833
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003834 SDValue LogOfMantissa;
Bill Wendling39150252008-09-09 20:39:27 +00003835 if (LimitFloatPrecision <= 6) {
3836 // For floating-point precision of 6:
3837 //
3838 // LogofMantissa =
3839 // -1.1609546f +
3840 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003841 //
Bill Wendling39150252008-09-09 20:39:27 +00003842 // error 0.0034276066, which is better than 8 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003843 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003844 getF32Constant(DAG, 0xbe74c456));
Owen Anderson825b72b2009-08-11 20:47:22 +00003845 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003846 getF32Constant(DAG, 0x3fb3a2b1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003847 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003848 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
3849 getF32Constant(DAG, 0x3f949a29));
Craig Topper08ac4692012-11-16 20:01:39 +00003850 } else if (LimitFloatPrecision <= 12) {
Bill Wendling39150252008-09-09 20:39:27 +00003851 // For floating-point precision of 12:
3852 //
3853 // LogOfMantissa =
3854 // -1.7417939f +
3855 // (2.8212026f +
3856 // (-1.4699568f +
3857 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3858 //
3859 // error 0.000061011436, which is 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003860 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003861 getF32Constant(DAG, 0xbd67b6d6));
Owen Anderson825b72b2009-08-11 20:47:22 +00003862 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003863 getF32Constant(DAG, 0x3ee4f4b8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003864 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3865 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003866 getF32Constant(DAG, 0x3fbc278b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003867 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3868 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003869 getF32Constant(DAG, 0x40348e95));
Owen Anderson825b72b2009-08-11 20:47:22 +00003870 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003871 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
3872 getF32Constant(DAG, 0x3fdef31a));
Craig Topper08ac4692012-11-16 20:01:39 +00003873 } else { // LimitFloatPrecision <= 18
Bill Wendling39150252008-09-09 20:39:27 +00003874 // For floating-point precision of 18:
3875 //
3876 // LogOfMantissa =
3877 // -2.1072184f +
3878 // (4.2372794f +
3879 // (-3.7029485f +
3880 // (2.2781945f +
3881 // (-0.87823314f +
3882 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3883 //
3884 // error 0.0000023660568, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003885 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003886 getF32Constant(DAG, 0xbc91e5ac));
Owen Anderson825b72b2009-08-11 20:47:22 +00003887 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003888 getF32Constant(DAG, 0x3e4350aa));
Owen Anderson825b72b2009-08-11 20:47:22 +00003889 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3890 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003891 getF32Constant(DAG, 0x3f60d3e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003892 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3893 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003894 getF32Constant(DAG, 0x4011cdf0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003895 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3896 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003897 getF32Constant(DAG, 0x406cfd1c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003898 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3899 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003900 getF32Constant(DAG, 0x408797cb));
Owen Anderson825b72b2009-08-11 20:47:22 +00003901 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003902 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
3903 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003904 }
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003905
Craig Topper5d1e0892012-11-23 18:38:31 +00003906 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003907 }
3908
Craig Topper5d1e0892012-11-23 18:38:31 +00003909 // No special expansion.
3910 return DAG.getNode(ISD::FLOG, dl, Op.getValueType(), Op);
Dale Johannesen59e577f2008-09-05 18:38:42 +00003911}
3912
Craig Topper5d1e0892012-11-23 18:38:31 +00003913/// expandLog2 - Lower a log2 intrinsic. Handles the special sequences for
Bill Wendling3eb59402008-09-09 00:28:24 +00003914/// limited-precision mode.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003915static SDValue expandLog2(SDLoc dl, SDValue Op, SelectionDAG &DAG,
Craig Topper5d1e0892012-11-23 18:38:31 +00003916 const TargetLowering &TLI) {
3917 if (Op.getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003918 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003919 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003920
Bill Wendling39150252008-09-09 20:39:27 +00003921 // Get the exponent.
Bill Wendling46ada192010-03-02 01:55:18 +00003922 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
Bill Wendling856ff412009-12-22 00:12:37 +00003923
Bill Wendling3eb59402008-09-09 00:28:24 +00003924 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003925 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003926 SDValue X = GetSignificand(DAG, Op1, dl);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003927
Bill Wendling3eb59402008-09-09 00:28:24 +00003928 // Different possible minimax approximations of significand in
3929 // floating-point for various degrees of accuracy over [1,2].
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003930 SDValue Log2ofMantissa;
Bill Wendling3eb59402008-09-09 00:28:24 +00003931 if (LimitFloatPrecision <= 6) {
3932 // For floating-point precision of 6:
3933 //
3934 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
3935 //
3936 // error 0.0049451742, which is more than 7 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003937 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003938 getF32Constant(DAG, 0xbeb08fe0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003939 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003940 getF32Constant(DAG, 0x40019463));
Owen Anderson825b72b2009-08-11 20:47:22 +00003941 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003942 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
3943 getF32Constant(DAG, 0x3fd6633d));
Craig Topper08ac4692012-11-16 20:01:39 +00003944 } else if (LimitFloatPrecision <= 12) {
Bill Wendling3eb59402008-09-09 00:28:24 +00003945 // For floating-point precision of 12:
3946 //
3947 // Log2ofMantissa =
3948 // -2.51285454f +
3949 // (4.07009056f +
3950 // (-2.12067489f +
3951 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003952 //
Bill Wendling3eb59402008-09-09 00:28:24 +00003953 // error 0.0000876136000, which is better than 13 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003954 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003955 getF32Constant(DAG, 0xbda7262e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003956 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003957 getF32Constant(DAG, 0x3f25280b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003958 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3959 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003960 getF32Constant(DAG, 0x4007b923));
Owen Anderson825b72b2009-08-11 20:47:22 +00003961 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3962 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003963 getF32Constant(DAG, 0x40823e2f));
Owen Anderson825b72b2009-08-11 20:47:22 +00003964 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003965 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
3966 getF32Constant(DAG, 0x4020d29c));
Craig Topper08ac4692012-11-16 20:01:39 +00003967 } else { // LimitFloatPrecision <= 18
Bill Wendling3eb59402008-09-09 00:28:24 +00003968 // For floating-point precision of 18:
3969 //
3970 // Log2ofMantissa =
3971 // -3.0400495f +
3972 // (6.1129976f +
3973 // (-5.3420409f +
3974 // (3.2865683f +
3975 // (-1.2669343f +
3976 // (0.27515199f -
3977 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
3978 //
3979 // error 0.0000018516, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003980 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003981 getF32Constant(DAG, 0xbcd2769e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003982 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003983 getF32Constant(DAG, 0x3e8ce0b9));
Owen Anderson825b72b2009-08-11 20:47:22 +00003984 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3985 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003986 getF32Constant(DAG, 0x3fa22ae7));
Owen Anderson825b72b2009-08-11 20:47:22 +00003987 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3988 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003989 getF32Constant(DAG, 0x40525723));
Owen Anderson825b72b2009-08-11 20:47:22 +00003990 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3991 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003992 getF32Constant(DAG, 0x40aaf200));
Owen Anderson825b72b2009-08-11 20:47:22 +00003993 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3994 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003995 getF32Constant(DAG, 0x40c39dad));
Owen Anderson825b72b2009-08-11 20:47:22 +00003996 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003997 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
3998 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003999 }
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004000
Craig Topper5d1e0892012-11-23 18:38:31 +00004001 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log2ofMantissa);
Dale Johannesen853244f2008-09-05 23:49:37 +00004002 }
Bill Wendling3eb59402008-09-09 00:28:24 +00004003
Craig Topper5d1e0892012-11-23 18:38:31 +00004004 // No special expansion.
4005 return DAG.getNode(ISD::FLOG2, dl, Op.getValueType(), Op);
Dale Johannesen59e577f2008-09-05 18:38:42 +00004006}
4007
Craig Topper5d1e0892012-11-23 18:38:31 +00004008/// expandLog10 - Lower a log10 intrinsic. Handles the special sequences for
Bill Wendling3eb59402008-09-09 00:28:24 +00004009/// limited-precision mode.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004010static SDValue expandLog10(SDLoc dl, SDValue Op, SelectionDAG &DAG,
Craig Topper5d1e0892012-11-23 18:38:31 +00004011 const TargetLowering &TLI) {
4012 if (Op.getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00004013 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004014 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00004015
Bill Wendling39150252008-09-09 20:39:27 +00004016 // Scale the exponent by log10(2) [0.30102999f].
Bill Wendling46ada192010-03-02 01:55:18 +00004017 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00004018 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004019 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00004020
4021 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00004022 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00004023 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling3eb59402008-09-09 00:28:24 +00004024
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004025 SDValue Log10ofMantissa;
Bill Wendling3eb59402008-09-09 00:28:24 +00004026 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00004027 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004028 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00004029 // Log10ofMantissa =
4030 // -0.50419619f +
4031 // (0.60948995f - 0.10380950f * x) * x;
4032 //
4033 // error 0.0014886165, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004034 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004035 getF32Constant(DAG, 0xbdd49a13));
Owen Anderson825b72b2009-08-11 20:47:22 +00004036 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004037 getF32Constant(DAG, 0x3f1c0789));
Owen Anderson825b72b2009-08-11 20:47:22 +00004038 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004039 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
4040 getF32Constant(DAG, 0x3f011300));
Craig Topper08ac4692012-11-16 20:01:39 +00004041 } else if (LimitFloatPrecision <= 12) {
Bill Wendling3eb59402008-09-09 00:28:24 +00004042 // For floating-point precision of 12:
4043 //
4044 // Log10ofMantissa =
4045 // -0.64831180f +
4046 // (0.91751397f +
4047 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
4048 //
4049 // error 0.00019228036, which is better than 12 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004050 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004051 getF32Constant(DAG, 0x3d431f31));
Owen Anderson825b72b2009-08-11 20:47:22 +00004052 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004053 getF32Constant(DAG, 0x3ea21fb2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004054 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
4055 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004056 getF32Constant(DAG, 0x3f6ae232));
Owen Anderson825b72b2009-08-11 20:47:22 +00004057 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004058 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
4059 getF32Constant(DAG, 0x3f25f7c3));
Craig Topper08ac4692012-11-16 20:01:39 +00004060 } else { // LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00004061 // For floating-point precision of 18:
4062 //
4063 // Log10ofMantissa =
4064 // -0.84299375f +
4065 // (1.5327582f +
4066 // (-1.0688956f +
4067 // (0.49102474f +
4068 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
4069 //
4070 // error 0.0000037995730, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004071 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004072 getF32Constant(DAG, 0x3c5d51ce));
Owen Anderson825b72b2009-08-11 20:47:22 +00004073 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004074 getF32Constant(DAG, 0x3e00685a));
Owen Anderson825b72b2009-08-11 20:47:22 +00004075 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
4076 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004077 getF32Constant(DAG, 0x3efb6798));
Owen Anderson825b72b2009-08-11 20:47:22 +00004078 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4079 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004080 getF32Constant(DAG, 0x3f88d192));
Owen Anderson825b72b2009-08-11 20:47:22 +00004081 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4082 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004083 getF32Constant(DAG, 0x3fc4316c));
Owen Anderson825b72b2009-08-11 20:47:22 +00004084 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004085 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
4086 getF32Constant(DAG, 0x3f57ce70));
Bill Wendling3eb59402008-09-09 00:28:24 +00004087 }
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004088
Craig Topper5d1e0892012-11-23 18:38:31 +00004089 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log10ofMantissa);
Dale Johannesen852680a2008-09-05 21:27:19 +00004090 }
Bill Wendling3eb59402008-09-09 00:28:24 +00004091
Craig Topper5d1e0892012-11-23 18:38:31 +00004092 // No special expansion.
4093 return DAG.getNode(ISD::FLOG10, dl, Op.getValueType(), Op);
Dale Johannesen59e577f2008-09-05 18:38:42 +00004094}
4095
Craig Topper538cd482012-11-24 18:52:06 +00004096/// expandExp2 - Lower an exp2 intrinsic. Handles the special sequences for
Bill Wendlinge10c8142008-09-09 22:39:21 +00004097/// limited-precision mode.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004098static SDValue expandExp2(SDLoc dl, SDValue Op, SelectionDAG &DAG,
Craig Topper538cd482012-11-24 18:52:06 +00004099 const TargetLowering &TLI) {
4100 if (Op.getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00004101 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004102 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00004103
4104 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00004105 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
4106 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00004107
4108 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00004109 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00004110 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00004111
Craig Topperb3157722012-11-24 08:22:37 +00004112 SDValue TwoToFractionalPartOfX;
Bill Wendlinge10c8142008-09-09 22:39:21 +00004113 if (LimitFloatPrecision <= 6) {
4114 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004115 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00004116 // TwoToFractionalPartOfX =
4117 // 0.997535578f +
4118 // (0.735607626f + 0.252464424f * x) * x;
4119 //
4120 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004121 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004122 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00004123 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004124 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004125 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
Craig Topperb3157722012-11-24 08:22:37 +00004126 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
4127 getF32Constant(DAG, 0x3f7f5e7e));
Craig Topper08ac4692012-11-16 20:01:39 +00004128 } else if (LimitFloatPrecision <= 12) {
Bill Wendlinge10c8142008-09-09 22:39:21 +00004129 // For floating-point precision of 12:
4130 //
4131 // TwoToFractionalPartOfX =
4132 // 0.999892986f +
4133 // (0.696457318f +
4134 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
4135 //
4136 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004137 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004138 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004139 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004140 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004141 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4142 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004143 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00004144 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
Craig Topperb3157722012-11-24 08:22:37 +00004145 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
4146 getF32Constant(DAG, 0x3f7ff8fd));
Craig Topper08ac4692012-11-16 20:01:39 +00004147 } else { // LimitFloatPrecision <= 18
Bill Wendlinge10c8142008-09-09 22:39:21 +00004148 // For floating-point precision of 18:
4149 //
4150 // TwoToFractionalPartOfX =
4151 // 0.999999982f +
4152 // (0.693148872f +
4153 // (0.240227044f +
4154 // (0.554906021e-1f +
4155 // (0.961591928e-2f +
4156 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
4157 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004158 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004159 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004160 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004161 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00004162 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4163 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004164 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00004165 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4166 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004167 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00004168 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
4169 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004170 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00004171 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
4172 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004173 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00004174 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
Craig Topperb3157722012-11-24 08:22:37 +00004175 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
4176 getF32Constant(DAG, 0x3f800000));
Bill Wendlinge10c8142008-09-09 22:39:21 +00004177 }
Craig Topperb3157722012-11-24 08:22:37 +00004178
4179 // Add the exponent into the result in integer domain.
4180 SDValue t13 = DAG.getNode(ISD::BITCAST, dl, MVT::i32,
4181 TwoToFractionalPartOfX);
Craig Topper538cd482012-11-24 18:52:06 +00004182 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
4183 DAG.getNode(ISD::ADD, dl, MVT::i32,
4184 t13, IntegerPartOfX));
Dale Johannesen601d3c02008-09-05 01:48:15 +00004185 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00004186
Craig Topper538cd482012-11-24 18:52:06 +00004187 // No special expansion.
4188 return DAG.getNode(ISD::FEXP2, dl, Op.getValueType(), Op);
Dale Johannesen601d3c02008-09-05 01:48:15 +00004189}
4190
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004191/// visitPow - Lower a pow intrinsic. Handles the special sequences for
4192/// limited-precision mode with x == 10.0f.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004193static SDValue expandPow(SDLoc dl, SDValue LHS, SDValue RHS,
Craig Topper327e4cb2012-11-25 08:08:58 +00004194 SelectionDAG &DAG, const TargetLowering &TLI) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004195 bool IsExp10 = false;
Craig Topper327e4cb2012-11-25 08:08:58 +00004196 if (LHS.getValueType() == MVT::f32 && LHS.getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004197 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Craig Topper327e4cb2012-11-25 08:08:58 +00004198 if (ConstantFPSDNode *LHSC = dyn_cast<ConstantFPSDNode>(LHS)) {
4199 APFloat Ten(10.0f);
4200 IsExp10 = LHSC->isExactlyValue(Ten);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004201 }
4202 }
4203
Craig Topperc1aa6382012-11-25 00:48:58 +00004204 if (IsExp10) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004205 // Put the exponent in the right bit position for later addition to the
4206 // final result:
4207 //
4208 // #define LOG2OF10 3.3219281f
4209 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Craig Topper327e4cb2012-11-25 08:08:58 +00004210 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, RHS,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004211 getF32Constant(DAG, 0x40549a78));
Owen Anderson825b72b2009-08-11 20:47:22 +00004212 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004213
4214 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00004215 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
4216 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004217
4218 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00004219 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00004220 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004221
Craig Topper915562e2012-11-25 00:15:07 +00004222 SDValue TwoToFractionalPartOfX;
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004223 if (LimitFloatPrecision <= 6) {
4224 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004225 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004226 // twoToFractionalPartOfX =
4227 // 0.997535578f +
4228 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004229 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004230 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004231 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004232 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00004233 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004234 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004235 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
Craig Topper915562e2012-11-25 00:15:07 +00004236 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
4237 getF32Constant(DAG, 0x3f7f5e7e));
Craig Topper08ac4692012-11-16 20:01:39 +00004238 } else if (LimitFloatPrecision <= 12) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004239 // For floating-point precision of 12:
4240 //
4241 // TwoToFractionalPartOfX =
4242 // 0.999892986f +
4243 // (0.696457318f +
4244 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
4245 //
4246 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004247 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004248 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004249 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004250 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004251 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4252 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004253 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00004254 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
Craig Topper915562e2012-11-25 00:15:07 +00004255 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
4256 getF32Constant(DAG, 0x3f7ff8fd));
Craig Topper08ac4692012-11-16 20:01:39 +00004257 } else { // LimitFloatPrecision <= 18
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004258 // For floating-point precision of 18:
4259 //
4260 // TwoToFractionalPartOfX =
4261 // 0.999999982f +
4262 // (0.693148872f +
4263 // (0.240227044f +
4264 // (0.554906021e-1f +
4265 // (0.961591928e-2f +
4266 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
4267 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004268 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004269 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004270 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004271 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00004272 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4273 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004274 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00004275 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4276 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004277 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00004278 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
4279 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004280 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00004281 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
4282 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004283 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00004284 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
Craig Topper915562e2012-11-25 00:15:07 +00004285 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
4286 getF32Constant(DAG, 0x3f800000));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004287 }
Craig Topper915562e2012-11-25 00:15:07 +00004288
4289 SDValue t13 = DAG.getNode(ISD::BITCAST, dl,MVT::i32,TwoToFractionalPartOfX);
Craig Topper327e4cb2012-11-25 08:08:58 +00004290 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
4291 DAG.getNode(ISD::ADD, dl, MVT::i32,
4292 t13, IntegerPartOfX));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004293 }
4294
Craig Topper327e4cb2012-11-25 08:08:58 +00004295 // No special expansion.
4296 return DAG.getNode(ISD::FPOW, dl, LHS.getValueType(), LHS, RHS);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004297}
4298
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004299
4300/// ExpandPowI - Expand a llvm.powi intrinsic.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004301static SDValue ExpandPowI(SDLoc DL, SDValue LHS, SDValue RHS,
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004302 SelectionDAG &DAG) {
4303 // If RHS is a constant, we can expand this out to a multiplication tree,
4304 // otherwise we end up lowering to a call to __powidf2 (for example). When
4305 // optimizing for size, we only want to do this if the expansion would produce
4306 // a small number of multiplies, otherwise we do the full expansion.
4307 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
4308 // Get the exponent as a positive value.
4309 unsigned Val = RHSC->getSExtValue();
4310 if ((int)Val < 0) Val = -Val;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004311
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004312 // powi(x, 0) -> 1.0
4313 if (Val == 0)
4314 return DAG.getConstantFP(1.0, LHS.getValueType());
4315
Dan Gohmanae541aa2010-04-15 04:33:49 +00004316 const Function *F = DAG.getMachineFunction().getFunction();
Bill Wendling831737d2012-12-30 10:32:01 +00004317 if (!F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
4318 Attribute::OptimizeForSize) ||
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004319 // If optimizing for size, don't insert too many multiplies. This
4320 // inserts up to 5 multiplies.
4321 CountPopulation_32(Val)+Log2_32(Val) < 7) {
4322 // We use the simple binary decomposition method to generate the multiply
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004323 // sequence. There are more optimal ways to do this (for example,
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004324 // powi(x,15) generates one more multiply than it should), but this has
4325 // the benefit of being both really simple and much better than a libcall.
4326 SDValue Res; // Logically starts equal to 1.0
4327 SDValue CurSquare = LHS;
4328 while (Val) {
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00004329 if (Val & 1) {
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004330 if (Res.getNode())
4331 Res = DAG.getNode(ISD::FMUL, DL,Res.getValueType(), Res, CurSquare);
4332 else
4333 Res = CurSquare; // 1.0*CurSquare.
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00004334 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004335
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004336 CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
4337 CurSquare, CurSquare);
4338 Val >>= 1;
4339 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004340
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004341 // If the original was negative, invert the result, producing 1/(x*x*x).
4342 if (RHSC->getSExtValue() < 0)
4343 Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(),
4344 DAG.getConstantFP(1.0, LHS.getValueType()), Res);
4345 return Res;
4346 }
4347 }
4348
4349 // Otherwise, expand to a libcall.
4350 return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS);
4351}
4352
Devang Patel227dfdb2011-05-16 21:24:05 +00004353// getTruncatedArgReg - Find underlying register used for an truncated
4354// argument.
4355static unsigned getTruncatedArgReg(const SDValue &N) {
4356 if (N.getOpcode() != ISD::TRUNCATE)
4357 return 0;
4358
4359 const SDValue &Ext = N.getOperand(0);
Stephen Lin09f8ca32013-07-06 21:44:25 +00004360 if (Ext.getOpcode() == ISD::AssertZext ||
4361 Ext.getOpcode() == ISD::AssertSext) {
Devang Patel227dfdb2011-05-16 21:24:05 +00004362 const SDValue &CFR = Ext.getOperand(0);
4363 if (CFR.getOpcode() == ISD::CopyFromReg)
4364 return cast<RegisterSDNode>(CFR.getOperand(1))->getReg();
Craig Topper7eb46d82012-04-11 04:55:51 +00004365 if (CFR.getOpcode() == ISD::TRUNCATE)
4366 return getTruncatedArgReg(CFR);
Devang Patel227dfdb2011-05-16 21:24:05 +00004367 }
4368 return 0;
4369}
4370
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004371/// EmitFuncArgumentDbgValue - If the DbgValueInst is a dbg_value of a function
4372/// argument, create the corresponding DBG_VALUE machine instruction for it now.
4373/// At the end of instruction selection, they will be inserted to the entry BB.
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004374bool
Devang Patel78a06e52010-08-25 20:39:26 +00004375SelectionDAGBuilder::EmitFuncArgumentDbgValue(const Value *V, MDNode *Variable,
Michael J. Spencere70c5262010-10-16 08:25:21 +00004376 int64_t Offset,
Dan Gohman5d11ea32010-05-01 00:33:16 +00004377 const SDValue &N) {
Devang Patel0b48ead2010-08-31 22:22:42 +00004378 const Argument *Arg = dyn_cast<Argument>(V);
4379 if (!Arg)
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004380 return false;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004381
Devang Patel719f6a92010-04-29 20:40:36 +00004382 MachineFunction &MF = DAG.getMachineFunction();
Devang Patela90b3052010-11-02 17:01:30 +00004383 const TargetInstrInfo *TII = DAG.getTarget().getInstrInfo();
Devang Patela90b3052010-11-02 17:01:30 +00004384
Devang Patela83ce982010-04-29 18:50:36 +00004385 // Ignore inlined function arguments here.
4386 DIVariable DV(Variable);
Devang Patel719f6a92010-04-29 20:40:36 +00004387 if (DV.isInlinedFnArgument(MF.getFunction()))
Devang Patela83ce982010-04-29 18:50:36 +00004388 return false;
4389
David Blaikie6d9dbd52013-06-16 20:34:15 +00004390 Optional<MachineOperand> Op;
Devang Patel9aee3352011-09-08 22:59:09 +00004391 // Some arguments' frame index is recorded during argument lowering.
David Blaikie6d9dbd52013-06-16 20:34:15 +00004392 if (int FI = FuncInfo.getArgumentFrameIndex(Arg))
4393 Op = MachineOperand::CreateFI(FI);
Devang Patel0b48ead2010-08-31 22:22:42 +00004394
David Blaikie6d9dbd52013-06-16 20:34:15 +00004395 if (!Op && N.getNode()) {
4396 unsigned Reg;
Devang Patel227dfdb2011-05-16 21:24:05 +00004397 if (N.getOpcode() == ISD::CopyFromReg)
4398 Reg = cast<RegisterSDNode>(N.getOperand(1))->getReg();
4399 else
4400 Reg = getTruncatedArgReg(N);
4401 if (Reg && TargetRegisterInfo::isVirtualRegister(Reg)) {
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004402 MachineRegisterInfo &RegInfo = MF.getRegInfo();
4403 unsigned PR = RegInfo.getLiveInPhysReg(Reg);
4404 if (PR)
4405 Reg = PR;
4406 }
David Blaikie6d9dbd52013-06-16 20:34:15 +00004407 if (Reg)
4408 Op = MachineOperand::CreateReg(Reg, false);
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004409 }
4410
David Blaikie6d9dbd52013-06-16 20:34:15 +00004411 if (!Op) {
Devang Patela90b3052010-11-02 17:01:30 +00004412 // Check if ValueMap has reg number.
Evan Chenga36acad2010-04-29 06:33:38 +00004413 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
Devang Patel8bc9ef72010-11-02 17:19:03 +00004414 if (VMI != FuncInfo.ValueMap.end())
David Blaikie6d9dbd52013-06-16 20:34:15 +00004415 Op = MachineOperand::CreateReg(VMI->second, false);
Evan Chenga36acad2010-04-29 06:33:38 +00004416 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004417
David Blaikie6d9dbd52013-06-16 20:34:15 +00004418 if (!Op && N.getNode())
Devang Patela90b3052010-11-02 17:01:30 +00004419 // Check if frame index is available.
4420 if (LoadSDNode *LNode = dyn_cast<LoadSDNode>(N.getNode()))
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004421 if (FrameIndexSDNode *FINode =
David Blaikie6d9dbd52013-06-16 20:34:15 +00004422 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
4423 Op = MachineOperand::CreateFI(FINode->getIndex());
Devang Patel8bc9ef72010-11-02 17:19:03 +00004424
David Blaikie6d9dbd52013-06-16 20:34:15 +00004425 if (!Op)
Devang Patel8bc9ef72010-11-02 17:19:03 +00004426 return false;
Devang Patela90b3052010-11-02 17:01:30 +00004427
Adrian Prantl893ae832013-07-10 01:53:30 +00004428 // FIXME: This does not handle register-indirect values at offset 0.
4429 bool IsIndirect = Offset != 0;
David Blaikie6d9dbd52013-06-16 20:34:15 +00004430 if (Op->isReg())
Adrian Prantl35176402013-07-09 20:28:37 +00004431 FuncInfo.ArgDbgValues.push_back(BuildMI(MF, getCurDebugLoc(),
4432 TII->get(TargetOpcode::DBG_VALUE),
Adrian Prantl893ae832013-07-10 01:53:30 +00004433 IsIndirect,
Adrian Prantl35176402013-07-09 20:28:37 +00004434 Op->getReg(), Offset, Variable));
4435 else
4436 FuncInfo.ArgDbgValues.push_back(
David Blaikie6d9dbd52013-06-16 20:34:15 +00004437 BuildMI(MF, getCurDebugLoc(), TII->get(TargetOpcode::DBG_VALUE))
4438 .addOperand(*Op).addImm(Offset).addMetadata(Variable));
Adrian Prantl35176402013-07-09 20:28:37 +00004439
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004440 return true;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004441}
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004442
Douglas Gregor7d9663c2010-05-11 06:17:44 +00004443// VisualStudio defines setjmp as _setjmp
Michael J. Spencer1f409602010-09-24 19:48:47 +00004444#if defined(_MSC_VER) && defined(setjmp) && \
4445 !defined(setjmp_undefined_for_msvc)
4446# pragma push_macro("setjmp")
4447# undef setjmp
4448# define setjmp_undefined_for_msvc
Douglas Gregor7d9663c2010-05-11 06:17:44 +00004449#endif
4450
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004451/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
4452/// we want to emit this as a call to a named external function, return the name
4453/// otherwise lower it and return null.
4454const char *
Dan Gohman46510a72010-04-15 01:51:59 +00004455SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, unsigned Intrinsic) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00004456 const TargetLowering *TLI = TM.getTargetLowering();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004457 SDLoc sdl = getCurSDLoc();
Dale Johannesen66978ee2009-01-31 02:22:37 +00004458 DebugLoc dl = getCurDebugLoc();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004459 SDValue Res;
4460
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004461 switch (Intrinsic) {
4462 default:
4463 // By default, turn this into a target intrinsic node.
4464 visitTargetIntrinsic(I, Intrinsic);
4465 return 0;
4466 case Intrinsic::vastart: visitVAStart(I); return 0;
4467 case Intrinsic::vaend: visitVAEnd(I); return 0;
4468 case Intrinsic::vacopy: visitVACopy(I); return 0;
4469 case Intrinsic::returnaddress:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004470 setValue(&I, DAG.getNode(ISD::RETURNADDR, sdl, TLI->getPointerTy(),
Gabor Greif0635f352010-06-25 09:38:13 +00004471 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004472 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00004473 case Intrinsic::frameaddress:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004474 setValue(&I, DAG.getNode(ISD::FRAMEADDR, sdl, TLI->getPointerTy(),
Gabor Greif0635f352010-06-25 09:38:13 +00004475 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004476 return 0;
4477 case Intrinsic::setjmp:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004478 return &"_setjmp"[!TLI->usesUnderscoreSetJmp()];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004479 case Intrinsic::longjmp:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004480 return &"_longjmp"[!TLI->usesUnderscoreLongJmp()];
Chris Lattner824b9582008-11-21 16:42:48 +00004481 case Intrinsic::memcpy: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00004482 // Assert for address < 256 since we support only user defined address
4483 // spaces.
Gabor Greif0635f352010-06-25 09:38:13 +00004484 assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004485 < 256 &&
Gabor Greif0635f352010-06-25 09:38:13 +00004486 cast<PointerType>(I.getArgOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004487 < 256 &&
4488 "Unknown address space");
Gabor Greif0635f352010-06-25 09:38:13 +00004489 SDValue Op1 = getValue(I.getArgOperand(0));
4490 SDValue Op2 = getValue(I.getArgOperand(1));
4491 SDValue Op3 = getValue(I.getArgOperand(2));
4492 unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
Chandler Carruthaf23f8e2013-02-25 14:20:21 +00004493 if (!Align)
4494 Align = 1; // @llvm.memcpy defines 0 and 1 to both mean no alignment.
Gabor Greif0635f352010-06-25 09:38:13 +00004495 bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004496 DAG.setRoot(DAG.getMemcpy(getRoot(), sdl, Op1, Op2, Op3, Align, isVol, false,
Chris Lattnere72f2022010-09-21 05:40:29 +00004497 MachinePointerInfo(I.getArgOperand(0)),
4498 MachinePointerInfo(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004499 return 0;
4500 }
Chris Lattner824b9582008-11-21 16:42:48 +00004501 case Intrinsic::memset: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00004502 // Assert for address < 256 since we support only user defined address
4503 // spaces.
Gabor Greif0635f352010-06-25 09:38:13 +00004504 assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004505 < 256 &&
4506 "Unknown address space");
Gabor Greif0635f352010-06-25 09:38:13 +00004507 SDValue Op1 = getValue(I.getArgOperand(0));
4508 SDValue Op2 = getValue(I.getArgOperand(1));
4509 SDValue Op3 = getValue(I.getArgOperand(2));
4510 unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
Chandler Carruthaf23f8e2013-02-25 14:20:21 +00004511 if (!Align)
4512 Align = 1; // @llvm.memset defines 0 and 1 to both mean no alignment.
Gabor Greif0635f352010-06-25 09:38:13 +00004513 bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004514 DAG.setRoot(DAG.getMemset(getRoot(), sdl, Op1, Op2, Op3, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004515 MachinePointerInfo(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004516 return 0;
4517 }
Chris Lattner824b9582008-11-21 16:42:48 +00004518 case Intrinsic::memmove: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00004519 // Assert for address < 256 since we support only user defined address
4520 // spaces.
Gabor Greif0635f352010-06-25 09:38:13 +00004521 assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004522 < 256 &&
Gabor Greif0635f352010-06-25 09:38:13 +00004523 cast<PointerType>(I.getArgOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004524 < 256 &&
4525 "Unknown address space");
Gabor Greif0635f352010-06-25 09:38:13 +00004526 SDValue Op1 = getValue(I.getArgOperand(0));
4527 SDValue Op2 = getValue(I.getArgOperand(1));
4528 SDValue Op3 = getValue(I.getArgOperand(2));
4529 unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
Chandler Carruthaf23f8e2013-02-25 14:20:21 +00004530 if (!Align)
4531 Align = 1; // @llvm.memmove defines 0 and 1 to both mean no alignment.
Gabor Greif0635f352010-06-25 09:38:13 +00004532 bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004533 DAG.setRoot(DAG.getMemmove(getRoot(), sdl, Op1, Op2, Op3, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004534 MachinePointerInfo(I.getArgOperand(0)),
4535 MachinePointerInfo(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004536 return 0;
4537 }
Bill Wendling92c1e122009-02-13 02:16:35 +00004538 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00004539 const DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
Devang Patelac1ceb32009-10-09 22:42:28 +00004540 MDNode *Variable = DI.getVariable();
Dan Gohman46510a72010-04-15 01:51:59 +00004541 const Value *Address = DI.getAddress();
Manman Rencbafae62013-06-28 05:43:10 +00004542 DIVariable DIVar(Variable);
4543 assert((!DIVar || DIVar.isVariable()) &&
4544 "Variable in DbgDeclareInst should be either null or a DIVariable.");
4545 if (!Address || !DIVar) {
Eric Christopher8f2a88d2012-03-15 21:33:41 +00004546 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
Dale Johannesen8ac38f22010-02-08 21:53:27 +00004547 return 0;
Eric Christopher8f2a88d2012-03-15 21:33:41 +00004548 }
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004549
Devang Patel3f74a112010-09-02 21:29:42 +00004550 // Check if address has undef value.
4551 if (isa<UndefValue>(Address) ||
4552 (Address->use_empty() && !isa<Argument>(Address))) {
Eric Christopher24413672012-02-23 03:39:39 +00004553 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
Devang Patel3f74a112010-09-02 21:29:42 +00004554 return 0;
4555 }
4556
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004557 SDValue &N = NodeMap[Address];
Devang Patel0b48ead2010-08-31 22:22:42 +00004558 if (!N.getNode() && isa<Argument>(Address))
4559 // Check unused arguments map.
4560 N = UnusedArgNodeMap[Address];
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004561 SDDbgValue *SDV;
4562 if (N.getNode()) {
Devang Patel8e741ed2010-09-02 21:02:27 +00004563 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
4564 Address = BCI->getOperand(0);
Eric Christopher178606d2012-02-24 01:59:08 +00004565 // Parameters are handled specially.
4566 bool isParameter =
4567 (DIVariable(Variable).getTag() == dwarf::DW_TAG_arg_variable ||
4568 isa<Argument>(Address));
4569
Devang Patel8e741ed2010-09-02 21:02:27 +00004570 const AllocaInst *AI = dyn_cast<AllocaInst>(Address);
4571
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004572 if (isParameter && !AI) {
4573 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N.getNode());
4574 if (FINode)
4575 // Byval parameter. We have a frame index at this point.
4576 SDV = DAG.getDbgValue(Variable, FINode->getIndex(),
4577 0, dl, SDNodeOrder);
Devang Patelafeaae72010-12-06 22:39:26 +00004578 else {
Devang Patel227dfdb2011-05-16 21:24:05 +00004579 // Address is an argument, so try to emit its dbg value using
4580 // virtual register info from the FuncInfo.ValueMap.
4581 EmitFuncArgumentDbgValue(Address, Variable, 0, N);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004582 return 0;
Devang Patelafeaae72010-12-06 22:39:26 +00004583 }
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004584 } else if (AI)
4585 SDV = DAG.getDbgValue(Variable, N.getNode(), N.getResNo(),
4586 0, dl, SDNodeOrder);
Devang Patelafeaae72010-12-06 22:39:26 +00004587 else {
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004588 // Can't do anything with other non-AI cases yet.
Eric Christopher24413672012-02-23 03:39:39 +00004589 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
Eric Christopher178606d2012-02-24 01:59:08 +00004590 DEBUG(dbgs() << "non-AllocaInst issue for Address: \n\t");
4591 DEBUG(Address->dump());
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004592 return 0;
Devang Patelafeaae72010-12-06 22:39:26 +00004593 }
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004594 DAG.AddDbgValue(SDV, N.getNode(), isParameter);
4595 } else {
Gabor Greiffb4032f2010-10-01 10:32:19 +00004596 // If Address is an argument then try to emit its dbg value using
Michael J. Spencere70c5262010-10-16 08:25:21 +00004597 // virtual register info from the FuncInfo.ValueMap.
Devang Patel6cd467b2010-08-26 22:53:27 +00004598 if (!EmitFuncArgumentDbgValue(Address, Variable, 0, N)) {
Devang Patel1397fdc2010-09-15 14:48:53 +00004599 // If variable is pinned by a alloca in dominating bb then
4600 // use StaticAllocaMap.
4601 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Address)) {
Devang Patel27ede1b2010-09-15 18:13:55 +00004602 if (AI->getParent() != DI.getParent()) {
4603 DenseMap<const AllocaInst*, int>::iterator SI =
4604 FuncInfo.StaticAllocaMap.find(AI);
4605 if (SI != FuncInfo.StaticAllocaMap.end()) {
4606 SDV = DAG.getDbgValue(Variable, SI->second,
4607 0, dl, SDNodeOrder);
4608 DAG.AddDbgValue(SDV, 0, false);
4609 return 0;
4610 }
Devang Patel1397fdc2010-09-15 14:48:53 +00004611 }
4612 }
Eric Christopher0822e012012-02-23 03:39:43 +00004613 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
Devang Patel6cd467b2010-08-26 22:53:27 +00004614 }
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004615 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004616 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00004617 }
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004618 case Intrinsic::dbg_value: {
Dan Gohman46510a72010-04-15 01:51:59 +00004619 const DbgValueInst &DI = cast<DbgValueInst>(I);
Manman Rencbafae62013-06-28 05:43:10 +00004620 DIVariable DIVar(DI.getVariable());
4621 assert((!DIVar || DIVar.isVariable()) &&
4622 "Variable in DbgValueInst should be either null or a DIVariable.");
4623 if (!DIVar)
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004624 return 0;
4625
4626 MDNode *Variable = DI.getVariable();
Devang Patel00190342010-03-15 19:15:44 +00004627 uint64_t Offset = DI.getOffset();
Dan Gohman46510a72010-04-15 01:51:59 +00004628 const Value *V = DI.getValue();
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004629 if (!V)
4630 return 0;
Devang Patel00190342010-03-15 19:15:44 +00004631
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004632 SDDbgValue *SDV;
Devang Patel57871242011-08-03 23:13:55 +00004633 if (isa<ConstantInt>(V) || isa<ConstantFP>(V) || isa<UndefValue>(V)) {
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004634 SDV = DAG.getDbgValue(Variable, V, Offset, dl, SDNodeOrder);
4635 DAG.AddDbgValue(SDV, 0, false);
Devang Patel00190342010-03-15 19:15:44 +00004636 } else {
Dale Johannesenbdc09d92010-07-16 00:02:08 +00004637 // Do not use getValue() in here; we don't want to generate code at
4638 // this point if it hasn't been done yet.
Devang Patel9126c0d2010-06-01 19:59:01 +00004639 SDValue N = NodeMap[V];
4640 if (!N.getNode() && isa<Argument>(V))
4641 // Check unused arguments map.
4642 N = UnusedArgNodeMap[V];
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004643 if (N.getNode()) {
Devang Patel78a06e52010-08-25 20:39:26 +00004644 if (!EmitFuncArgumentDbgValue(V, Variable, Offset, N)) {
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004645 SDV = DAG.getDbgValue(Variable, N.getNode(),
4646 N.getResNo(), Offset, dl, SDNodeOrder);
4647 DAG.AddDbgValue(SDV, N.getNode(), false);
4648 }
Devang Patela778f5c2011-02-18 22:43:42 +00004649 } else if (!V->use_empty() ) {
Dale Johannesenbdc09d92010-07-16 00:02:08 +00004650 // Do not call getValue(V) yet, as we don't want to generate code.
4651 // Remember it for later.
4652 DanglingDebugInfo DDI(&DI, dl, SDNodeOrder);
4653 DanglingDebugInfoMap[V] = DDI;
Devang Patel0991dfb2010-08-27 22:25:51 +00004654 } else {
Devang Patel00190342010-03-15 19:15:44 +00004655 // We may expand this to cover more cases. One case where we have no
Devang Patelafeaae72010-12-06 22:39:26 +00004656 // data available is an unreferenced parameter.
Eric Christopher0822e012012-02-23 03:39:43 +00004657 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004658 }
Devang Patel00190342010-03-15 19:15:44 +00004659 }
4660
4661 // Build a debug info table entry.
Dan Gohman46510a72010-04-15 01:51:59 +00004662 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(V))
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004663 V = BCI->getOperand(0);
Dan Gohman46510a72010-04-15 01:51:59 +00004664 const AllocaInst *AI = dyn_cast<AllocaInst>(V);
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004665 // Don't handle byval struct arguments or VLAs, for example.
Eric Christopher7e1e18f2012-03-26 06:10:32 +00004666 if (!AI) {
Eric Christopher9fc5c832012-03-28 07:34:36 +00004667 DEBUG(dbgs() << "Dropping debug location info for:\n " << DI << "\n");
4668 DEBUG(dbgs() << " Last seen at:\n " << *V << "\n");
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004669 return 0;
Eric Christopher7e1e18f2012-03-26 06:10:32 +00004670 }
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004671 DenseMap<const AllocaInst*, int>::iterator SI =
4672 FuncInfo.StaticAllocaMap.find(AI);
4673 if (SI == FuncInfo.StaticAllocaMap.end())
4674 return 0; // VLAs.
4675 int FI = SI->second;
Michael J. Spencere70c5262010-10-16 08:25:21 +00004676
Chris Lattner512063d2010-04-05 06:19:28 +00004677 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
4678 if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo())
4679 MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc());
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004680 return 0;
4681 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004682
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004683 case Intrinsic::eh_typeid_for: {
Chris Lattner512063d2010-04-05 06:19:28 +00004684 // Find the type id for the given typeinfo.
Gabor Greif0635f352010-06-25 09:38:13 +00004685 GlobalVariable *GV = ExtractTypeInfo(I.getArgOperand(0));
Chris Lattner512063d2010-04-05 06:19:28 +00004686 unsigned TypeID = DAG.getMachineFunction().getMMI().getTypeIDFor(GV);
4687 Res = DAG.getConstant(TypeID, MVT::i32);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004688 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004689 return 0;
4690 }
4691
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004692 case Intrinsic::eh_return_i32:
4693 case Intrinsic::eh_return_i64:
Chris Lattner512063d2010-04-05 06:19:28 +00004694 DAG.getMachineFunction().getMMI().setCallsEHReturn(true);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004695 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, sdl,
Chris Lattner512063d2010-04-05 06:19:28 +00004696 MVT::Other,
4697 getControlRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00004698 getValue(I.getArgOperand(0)),
4699 getValue(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004700 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004701 case Intrinsic::eh_unwind_init:
Chris Lattner512063d2010-04-05 06:19:28 +00004702 DAG.getMachineFunction().getMMI().setCallsUnwindInit(true);
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004703 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004704 case Intrinsic::eh_dwarf_cfa: {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004705 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getArgOperand(0)), sdl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00004706 TLI->getPointerTy());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004707 SDValue Offset = DAG.getNode(ISD::ADD, sdl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00004708 TLI->getPointerTy(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004709 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, sdl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00004710 TLI->getPointerTy()),
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004711 CfaArg);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004712 SDValue FA = DAG.getNode(ISD::FRAMEADDR, sdl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00004713 TLI->getPointerTy(),
4714 DAG.getConstant(0, TLI->getPointerTy()));
4715 setValue(&I, DAG.getNode(ISD::ADD, sdl, TLI->getPointerTy(),
Bill Wendling4533cac2010-01-28 21:51:40 +00004716 FA, Offset));
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004717 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004718 }
Jim Grosbachca752c92010-01-28 01:45:32 +00004719 case Intrinsic::eh_sjlj_callsite: {
Chris Lattner512063d2010-04-05 06:19:28 +00004720 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Gabor Greif0635f352010-06-25 09:38:13 +00004721 ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(0));
Jim Grosbachca752c92010-01-28 01:45:32 +00004722 assert(CI && "Non-constant call site value in eh.sjlj.callsite!");
Chris Lattner512063d2010-04-05 06:19:28 +00004723 assert(MMI.getCurrentCallSite() == 0 && "Overlapping call sites!");
Jim Grosbachca752c92010-01-28 01:45:32 +00004724
Chris Lattner512063d2010-04-05 06:19:28 +00004725 MMI.setCurrentCallSite(CI->getZExtValue());
Jim Grosbachca752c92010-01-28 01:45:32 +00004726 return 0;
4727 }
Bill Wendling6ef94172011-09-28 03:36:43 +00004728 case Intrinsic::eh_sjlj_functioncontext: {
4729 // Get and store the index of the function context.
4730 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bill Wendlingadbf7b22011-09-28 03:52:41 +00004731 AllocaInst *FnCtx =
4732 cast<AllocaInst>(I.getArgOperand(0)->stripPointerCasts());
Bill Wendling6ef94172011-09-28 03:36:43 +00004733 int FI = FuncInfo.StaticAllocaMap[FnCtx];
4734 MFI->setFunctionContextIndex(FI);
4735 return 0;
4736 }
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004737 case Intrinsic::eh_sjlj_setjmp: {
Bill Wendlingce370cf2011-10-07 21:25:38 +00004738 SDValue Ops[2];
4739 Ops[0] = getRoot();
4740 Ops[1] = getValue(I.getArgOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004741 SDValue Op = DAG.getNode(ISD::EH_SJLJ_SETJMP, sdl,
Bill Wendlingce370cf2011-10-07 21:25:38 +00004742 DAG.getVTList(MVT::i32, MVT::Other),
4743 Ops, 2);
4744 setValue(&I, Op.getValue(0));
4745 DAG.setRoot(Op.getValue(1));
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004746 return 0;
4747 }
Jim Grosbach5eb19512010-05-22 01:06:18 +00004748 case Intrinsic::eh_sjlj_longjmp: {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004749 DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_LONGJMP, sdl, MVT::Other,
Jim Grosbache4ad3872010-10-19 23:27:08 +00004750 getRoot(), getValue(I.getArgOperand(0))));
4751 return 0;
4752 }
Jim Grosbachca752c92010-01-28 01:45:32 +00004753
Dale Johannesen0488fb62010-09-30 23:57:10 +00004754 case Intrinsic::x86_mmx_pslli_w:
4755 case Intrinsic::x86_mmx_pslli_d:
4756 case Intrinsic::x86_mmx_pslli_q:
4757 case Intrinsic::x86_mmx_psrli_w:
4758 case Intrinsic::x86_mmx_psrli_d:
4759 case Intrinsic::x86_mmx_psrli_q:
4760 case Intrinsic::x86_mmx_psrai_w:
4761 case Intrinsic::x86_mmx_psrai_d: {
4762 SDValue ShAmt = getValue(I.getArgOperand(1));
4763 if (isa<ConstantSDNode>(ShAmt)) {
4764 visitTargetIntrinsic(I, Intrinsic);
4765 return 0;
4766 }
4767 unsigned NewIntrinsic = 0;
4768 EVT ShAmtVT = MVT::v2i32;
4769 switch (Intrinsic) {
4770 case Intrinsic::x86_mmx_pslli_w:
4771 NewIntrinsic = Intrinsic::x86_mmx_psll_w;
4772 break;
4773 case Intrinsic::x86_mmx_pslli_d:
4774 NewIntrinsic = Intrinsic::x86_mmx_psll_d;
4775 break;
4776 case Intrinsic::x86_mmx_pslli_q:
4777 NewIntrinsic = Intrinsic::x86_mmx_psll_q;
4778 break;
4779 case Intrinsic::x86_mmx_psrli_w:
4780 NewIntrinsic = Intrinsic::x86_mmx_psrl_w;
4781 break;
4782 case Intrinsic::x86_mmx_psrli_d:
4783 NewIntrinsic = Intrinsic::x86_mmx_psrl_d;
4784 break;
4785 case Intrinsic::x86_mmx_psrli_q:
4786 NewIntrinsic = Intrinsic::x86_mmx_psrl_q;
4787 break;
4788 case Intrinsic::x86_mmx_psrai_w:
4789 NewIntrinsic = Intrinsic::x86_mmx_psra_w;
4790 break;
4791 case Intrinsic::x86_mmx_psrai_d:
4792 NewIntrinsic = Intrinsic::x86_mmx_psra_d;
4793 break;
4794 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
4795 }
4796
4797 // The vector shift intrinsics with scalars uses 32b shift amounts but
4798 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
4799 // to be zero.
4800 // We must do this early because v2i32 is not a legal type.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004801 SDValue ShOps[2];
4802 ShOps[0] = ShAmt;
4803 ShOps[1] = DAG.getConstant(0, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004804 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, sdl, ShAmtVT, &ShOps[0], 2);
Bill Wendlingba54bca2013-06-19 21:36:55 +00004805 EVT DestVT = TLI->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004806 ShAmt = DAG.getNode(ISD::BITCAST, sdl, DestVT, ShAmt);
4807 Res = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, sdl, DestVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00004808 DAG.getConstant(NewIntrinsic, MVT::i32),
4809 getValue(I.getArgOperand(0)), ShAmt);
4810 setValue(&I, Res);
4811 return 0;
4812 }
Pete Cooperd18134f2012-02-24 03:51:49 +00004813 case Intrinsic::x86_avx_vinsertf128_pd_256:
4814 case Intrinsic::x86_avx_vinsertf128_ps_256:
Craig Topperb45c9692012-04-07 22:32:29 +00004815 case Intrinsic::x86_avx_vinsertf128_si_256:
4816 case Intrinsic::x86_avx2_vinserti128: {
Bill Wendlingba54bca2013-06-19 21:36:55 +00004817 EVT DestVT = TLI->getValueType(I.getType());
4818 EVT ElVT = TLI->getValueType(I.getArgOperand(1)->getType());
Pete Cooperd18134f2012-02-24 03:51:49 +00004819 uint64_t Idx = (cast<ConstantInt>(I.getArgOperand(2))->getZExtValue() & 1) *
4820 ElVT.getVectorNumElements();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004821 Res = DAG.getNode(ISD::INSERT_SUBVECTOR, sdl, DestVT,
Pete Cooperd18134f2012-02-24 03:51:49 +00004822 getValue(I.getArgOperand(0)),
4823 getValue(I.getArgOperand(1)),
Tom Stellard425b76c2013-08-05 22:22:01 +00004824 DAG.getConstant(Idx, TLI->getVectorIdxTy()));
Craig Topperf6dc7922012-09-05 05:48:09 +00004825 setValue(&I, Res);
4826 return 0;
4827 }
4828 case Intrinsic::x86_avx_vextractf128_pd_256:
4829 case Intrinsic::x86_avx_vextractf128_ps_256:
4830 case Intrinsic::x86_avx_vextractf128_si_256:
4831 case Intrinsic::x86_avx2_vextracti128: {
Bill Wendlingba54bca2013-06-19 21:36:55 +00004832 EVT DestVT = TLI->getValueType(I.getType());
Craig Topperf6dc7922012-09-05 05:48:09 +00004833 uint64_t Idx = (cast<ConstantInt>(I.getArgOperand(1))->getZExtValue() & 1) *
4834 DestVT.getVectorNumElements();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004835 Res = DAG.getNode(ISD::EXTRACT_SUBVECTOR, sdl, DestVT,
Craig Topperf6dc7922012-09-05 05:48:09 +00004836 getValue(I.getArgOperand(0)),
Tom Stellard425b76c2013-08-05 22:22:01 +00004837 DAG.getConstant(Idx, TLI->getVectorIdxTy()));
Pete Cooperd18134f2012-02-24 03:51:49 +00004838 setValue(&I, Res);
4839 return 0;
4840 }
Mon P Wang77cdf302008-11-10 20:54:11 +00004841 case Intrinsic::convertff:
4842 case Intrinsic::convertfsi:
4843 case Intrinsic::convertfui:
4844 case Intrinsic::convertsif:
4845 case Intrinsic::convertuif:
4846 case Intrinsic::convertss:
4847 case Intrinsic::convertsu:
4848 case Intrinsic::convertus:
4849 case Intrinsic::convertuu: {
4850 ISD::CvtCode Code = ISD::CVT_INVALID;
4851 switch (Intrinsic) {
Craig Topperc42e6402012-04-11 04:34:11 +00004852 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Mon P Wang77cdf302008-11-10 20:54:11 +00004853 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
4854 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
4855 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
4856 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
4857 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
4858 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
4859 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
4860 case Intrinsic::convertus: Code = ISD::CVT_US; break;
4861 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
4862 }
Bill Wendlingba54bca2013-06-19 21:36:55 +00004863 EVT DestVT = TLI->getValueType(I.getType());
Gabor Greif0635f352010-06-25 09:38:13 +00004864 const Value *Op1 = I.getArgOperand(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004865 Res = DAG.getConvertRndSat(DestVT, sdl, getValue(Op1),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004866 DAG.getValueType(DestVT),
4867 DAG.getValueType(getValue(Op1).getValueType()),
Gabor Greif0635f352010-06-25 09:38:13 +00004868 getValue(I.getArgOperand(1)),
4869 getValue(I.getArgOperand(2)),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004870 Code);
4871 setValue(&I, Res);
Mon P Wang77cdf302008-11-10 20:54:11 +00004872 return 0;
4873 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004874 case Intrinsic::powi:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004875 setValue(&I, ExpandPowI(sdl, getValue(I.getArgOperand(0)),
Gabor Greif0635f352010-06-25 09:38:13 +00004876 getValue(I.getArgOperand(1)), DAG));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004877 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004878 case Intrinsic::log:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004879 setValue(&I, expandLog(sdl, getValue(I.getArgOperand(0)), DAG, *TLI));
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004880 return 0;
4881 case Intrinsic::log2:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004882 setValue(&I, expandLog2(sdl, getValue(I.getArgOperand(0)), DAG, *TLI));
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004883 return 0;
4884 case Intrinsic::log10:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004885 setValue(&I, expandLog10(sdl, getValue(I.getArgOperand(0)), DAG, *TLI));
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004886 return 0;
4887 case Intrinsic::exp:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004888 setValue(&I, expandExp(sdl, getValue(I.getArgOperand(0)), DAG, *TLI));
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004889 return 0;
4890 case Intrinsic::exp2:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004891 setValue(&I, expandExp2(sdl, getValue(I.getArgOperand(0)), DAG, *TLI));
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004892 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004893 case Intrinsic::pow:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004894 setValue(&I, expandPow(sdl, getValue(I.getArgOperand(0)),
Bill Wendlingba54bca2013-06-19 21:36:55 +00004895 getValue(I.getArgOperand(1)), DAG, *TLI));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004896 return 0;
Craig Topper9bd4dd72012-11-16 07:48:23 +00004897 case Intrinsic::sqrt:
Peter Collingbourneb34d3aa2012-05-28 21:48:37 +00004898 case Intrinsic::fabs:
Craig Topper9bd4dd72012-11-16 07:48:23 +00004899 case Intrinsic::sin:
4900 case Intrinsic::cos:
Dan Gohman27db99f2012-07-26 17:43:27 +00004901 case Intrinsic::floor:
Craig Topper49010472012-11-15 06:51:10 +00004902 case Intrinsic::ceil:
Craig Topper49010472012-11-15 06:51:10 +00004903 case Intrinsic::trunc:
Craig Topper49010472012-11-15 06:51:10 +00004904 case Intrinsic::rint:
Hal Finkel41418d12013-08-07 22:49:12 +00004905 case Intrinsic::nearbyint:
4906 case Intrinsic::round: {
Craig Topper9bd4dd72012-11-16 07:48:23 +00004907 unsigned Opcode;
4908 switch (Intrinsic) {
4909 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
4910 case Intrinsic::sqrt: Opcode = ISD::FSQRT; break;
4911 case Intrinsic::fabs: Opcode = ISD::FABS; break;
4912 case Intrinsic::sin: Opcode = ISD::FSIN; break;
4913 case Intrinsic::cos: Opcode = ISD::FCOS; break;
4914 case Intrinsic::floor: Opcode = ISD::FFLOOR; break;
4915 case Intrinsic::ceil: Opcode = ISD::FCEIL; break;
4916 case Intrinsic::trunc: Opcode = ISD::FTRUNC; break;
4917 case Intrinsic::rint: Opcode = ISD::FRINT; break;
4918 case Intrinsic::nearbyint: Opcode = ISD::FNEARBYINT; break;
Hal Finkel41418d12013-08-07 22:49:12 +00004919 case Intrinsic::round: Opcode = ISD::FROUND; break;
Craig Topper9bd4dd72012-11-16 07:48:23 +00004920 }
4921
Andrew Trickac6d9be2013-05-25 02:42:55 +00004922 setValue(&I, DAG.getNode(Opcode, sdl,
Craig Topper49010472012-11-15 06:51:10 +00004923 getValue(I.getArgOperand(0)).getValueType(),
4924 getValue(I.getArgOperand(0))));
4925 return 0;
Craig Topper9bd4dd72012-11-16 07:48:23 +00004926 }
Cameron Zwarich33390842011-07-08 21:39:21 +00004927 case Intrinsic::fma:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004928 setValue(&I, DAG.getNode(ISD::FMA, sdl,
Cameron Zwarich33390842011-07-08 21:39:21 +00004929 getValue(I.getArgOperand(0)).getValueType(),
4930 getValue(I.getArgOperand(0)),
4931 getValue(I.getArgOperand(1)),
4932 getValue(I.getArgOperand(2))));
4933 return 0;
Lang Hames5afba6f2012-06-05 19:07:46 +00004934 case Intrinsic::fmuladd: {
Bill Wendlingba54bca2013-06-19 21:36:55 +00004935 EVT VT = TLI->getValueType(I.getType());
Lang Hamese0231412012-06-22 01:09:09 +00004936 if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict &&
Stephen Line54885a2013-07-09 18:16:56 +00004937 TLI->isFMAFasterThanFMulAndFAdd(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004938 setValue(&I, DAG.getNode(ISD::FMA, sdl,
Lang Hames5afba6f2012-06-05 19:07:46 +00004939 getValue(I.getArgOperand(0)).getValueType(),
4940 getValue(I.getArgOperand(0)),
4941 getValue(I.getArgOperand(1)),
4942 getValue(I.getArgOperand(2))));
4943 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004944 SDValue Mul = DAG.getNode(ISD::FMUL, sdl,
Lang Hames5afba6f2012-06-05 19:07:46 +00004945 getValue(I.getArgOperand(0)).getValueType(),
4946 getValue(I.getArgOperand(0)),
4947 getValue(I.getArgOperand(1)));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004948 SDValue Add = DAG.getNode(ISD::FADD, sdl,
Lang Hames5afba6f2012-06-05 19:07:46 +00004949 getValue(I.getArgOperand(0)).getValueType(),
4950 Mul,
4951 getValue(I.getArgOperand(2)));
4952 setValue(&I, Add);
4953 }
4954 return 0;
4955 }
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004956 case Intrinsic::convert_to_fp16:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004957 setValue(&I, DAG.getNode(ISD::FP32_TO_FP16, sdl,
Gabor Greif0635f352010-06-25 09:38:13 +00004958 MVT::i16, getValue(I.getArgOperand(0))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004959 return 0;
4960 case Intrinsic::convert_from_fp16:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004961 setValue(&I, DAG.getNode(ISD::FP16_TO_FP32, sdl,
Gabor Greif0635f352010-06-25 09:38:13 +00004962 MVT::f32, getValue(I.getArgOperand(0))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004963 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004964 case Intrinsic::pcmarker: {
Gabor Greif0635f352010-06-25 09:38:13 +00004965 SDValue Tmp = getValue(I.getArgOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004966 DAG.setRoot(DAG.getNode(ISD::PCMARKER, sdl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004967 return 0;
4968 }
4969 case Intrinsic::readcyclecounter: {
4970 SDValue Op = getRoot();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004971 Res = DAG.getNode(ISD::READCYCLECOUNTER, sdl,
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004972 DAG.getVTList(MVT::i64, MVT::Other),
4973 &Op, 1);
4974 setValue(&I, Res);
4975 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004976 return 0;
4977 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004978 case Intrinsic::bswap:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004979 setValue(&I, DAG.getNode(ISD::BSWAP, sdl,
Gabor Greif0635f352010-06-25 09:38:13 +00004980 getValue(I.getArgOperand(0)).getValueType(),
4981 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004982 return 0;
4983 case Intrinsic::cttz: {
Gabor Greif0635f352010-06-25 09:38:13 +00004984 SDValue Arg = getValue(I.getArgOperand(0));
Chandler Carruth63974b22011-12-13 01:56:10 +00004985 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004986 EVT Ty = Arg.getValueType();
Chandler Carruth63974b22011-12-13 01:56:10 +00004987 setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTTZ : ISD::CTTZ_ZERO_UNDEF,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004988 sdl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004989 return 0;
4990 }
4991 case Intrinsic::ctlz: {
Gabor Greif0635f352010-06-25 09:38:13 +00004992 SDValue Arg = getValue(I.getArgOperand(0));
Chandler Carruth63974b22011-12-13 01:56:10 +00004993 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004994 EVT Ty = Arg.getValueType();
Chandler Carruth63974b22011-12-13 01:56:10 +00004995 setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTLZ : ISD::CTLZ_ZERO_UNDEF,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004996 sdl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004997 return 0;
4998 }
4999 case Intrinsic::ctpop: {
Gabor Greif0635f352010-06-25 09:38:13 +00005000 SDValue Arg = getValue(I.getArgOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00005001 EVT Ty = Arg.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005002 setValue(&I, DAG.getNode(ISD::CTPOP, sdl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005003 return 0;
5004 }
5005 case Intrinsic::stacksave: {
5006 SDValue Op = getRoot();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005007 Res = DAG.getNode(ISD::STACKSAVE, sdl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00005008 DAG.getVTList(TLI->getPointerTy(), MVT::Other), &Op, 1);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00005009 setValue(&I, Res);
5010 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005011 return 0;
5012 }
5013 case Intrinsic::stackrestore: {
Gabor Greif0635f352010-06-25 09:38:13 +00005014 Res = getValue(I.getArgOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005015 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, sdl, MVT::Other, getRoot(), Res));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005016 return 0;
5017 }
Bill Wendling57344502008-11-18 11:01:33 +00005018 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00005019 // Emit code into the DAG to store the stack guard onto the stack.
5020 MachineFunction &MF = DAG.getMachineFunction();
5021 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendlingba54bca2013-06-19 21:36:55 +00005022 EVT PtrTy = TLI->getPointerTy();
Bill Wendlingb2a42982008-11-06 02:29:10 +00005023
Gabor Greif0635f352010-06-25 09:38:13 +00005024 SDValue Src = getValue(I.getArgOperand(0)); // The guard's value.
5025 AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Bill Wendlingb2a42982008-11-06 02:29:10 +00005026
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00005027 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00005028 MFI->setStackProtectorIndex(FI);
5029
5030 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
5031
5032 // Store the stack protector onto the stack.
Andrew Trickac6d9be2013-05-25 02:42:55 +00005033 Res = DAG.getStore(getRoot(), sdl, Src, FIN,
Chris Lattner84bd98a2010-09-21 18:58:22 +00005034 MachinePointerInfo::getFixedStack(FI),
5035 true, false, 0);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00005036 setValue(&I, Res);
5037 DAG.setRoot(Res);
Bill Wendlingb2a42982008-11-06 02:29:10 +00005038 return 0;
5039 }
Eric Christopher7b5e6172009-10-27 00:52:25 +00005040 case Intrinsic::objectsize: {
5041 // If we don't know by now, we're never going to know.
Gabor Greif0635f352010-06-25 09:38:13 +00005042 ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(1));
Eric Christopher7b5e6172009-10-27 00:52:25 +00005043
5044 assert(CI && "Non-constant type in __builtin_object_size?");
5045
Gabor Greif0635f352010-06-25 09:38:13 +00005046 SDValue Arg = getValue(I.getCalledValue());
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00005047 EVT Ty = Arg.getValueType();
5048
Dan Gohmane368b462010-06-18 14:22:04 +00005049 if (CI->isZero())
Bill Wendlingd0283fa2009-12-22 00:40:51 +00005050 Res = DAG.getConstant(-1ULL, Ty);
Eric Christopher7b5e6172009-10-27 00:52:25 +00005051 else
Bill Wendlingd0283fa2009-12-22 00:40:51 +00005052 Res = DAG.getConstant(0, Ty);
5053
5054 setValue(&I, Res);
Eric Christopher7b5e6172009-10-27 00:52:25 +00005055 return 0;
5056 }
Justin Holewinskic2b7f5f2013-05-21 14:37:16 +00005057 case Intrinsic::annotation:
5058 case Intrinsic::ptr_annotation:
5059 // Drop the intrinsic, but forward the value
5060 setValue(&I, getValue(I.getOperand(0)));
5061 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005062 case Intrinsic::var_annotation:
5063 // Discard annotate attributes
5064 return 0;
5065
5066 case Intrinsic::init_trampoline: {
Gabor Greif0635f352010-06-25 09:38:13 +00005067 const Function *F = cast<Function>(I.getArgOperand(1)->stripPointerCasts());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005068
5069 SDValue Ops[6];
5070 Ops[0] = getRoot();
Gabor Greif0635f352010-06-25 09:38:13 +00005071 Ops[1] = getValue(I.getArgOperand(0));
5072 Ops[2] = getValue(I.getArgOperand(1));
5073 Ops[3] = getValue(I.getArgOperand(2));
5074 Ops[4] = DAG.getSrcValue(I.getArgOperand(0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005075 Ops[5] = DAG.getSrcValue(F);
5076
Andrew Trickac6d9be2013-05-25 02:42:55 +00005077 Res = DAG.getNode(ISD::INIT_TRAMPOLINE, sdl, MVT::Other, Ops, 6);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005078
Duncan Sands4a544a72011-09-06 13:37:06 +00005079 DAG.setRoot(Res);
5080 return 0;
5081 }
5082 case Intrinsic::adjust_trampoline: {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005083 setValue(&I, DAG.getNode(ISD::ADJUST_TRAMPOLINE, sdl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00005084 TLI->getPointerTy(),
Duncan Sands4a544a72011-09-06 13:37:06 +00005085 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005086 return 0;
5087 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005088 case Intrinsic::gcroot:
5089 if (GFI) {
Bill Wendling95dd4422012-05-01 22:50:45 +00005090 const Value *Alloca = I.getArgOperand(0)->stripPointerCasts();
Gabor Greif0635f352010-06-25 09:38:13 +00005091 const Constant *TypeMap = cast<Constant>(I.getArgOperand(1));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005092
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005093 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
5094 GFI->addStackRoot(FI->getIndex(), TypeMap);
5095 }
5096 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005097 case Intrinsic::gcread:
5098 case Intrinsic::gcwrite:
Torok Edwinc23197a2009-07-14 16:55:14 +00005099 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
Bill Wendlingd0283fa2009-12-22 00:40:51 +00005100 case Intrinsic::flt_rounds:
Andrew Trickac6d9be2013-05-25 02:42:55 +00005101 setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, sdl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005102 return 0;
Jakub Staszak9da99342011-07-06 18:22:43 +00005103
5104 case Intrinsic::expect: {
5105 // Just replace __builtin_expect(exp, c) with EXP.
5106 setValue(&I, getValue(I.getArgOperand(0)));
5107 return 0;
5108 }
5109
Shuxin Yang970755e2012-10-19 20:11:16 +00005110 case Intrinsic::debugtrap:
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005111 case Intrinsic::trap: {
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005112 StringRef TrapFuncName = TM.Options.getTrapFunctionName();
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005113 if (TrapFuncName.empty()) {
Stephen Lin155615d2013-07-08 00:37:03 +00005114 ISD::NodeType Op = (Intrinsic == Intrinsic::trap) ?
Shuxin Yang970755e2012-10-19 20:11:16 +00005115 ISD::TRAP : ISD::DEBUGTRAP;
Andrew Trickac6d9be2013-05-25 02:42:55 +00005116 DAG.setRoot(DAG.getNode(Op, sdl,MVT::Other, getRoot()));
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005117 return 0;
5118 }
5119 TargetLowering::ArgListTy Args;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00005120 TargetLowering::
5121 CallLoweringInfo CLI(getRoot(), I.getType(),
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005122 false, false, false, false, 0, CallingConv::C,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00005123 /*isTailCall=*/false,
5124 /*doesNotRet=*/false, /*isReturnValueUsed=*/true,
Bill Wendlingba54bca2013-06-19 21:36:55 +00005125 DAG.getExternalSymbol(TrapFuncName.data(),
5126 TLI->getPointerTy()),
Andrew Trickac6d9be2013-05-25 02:42:55 +00005127 Args, DAG, sdl);
Bill Wendlingba54bca2013-06-19 21:36:55 +00005128 std::pair<SDValue, SDValue> Result = TLI->LowerCallTo(CLI);
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005129 DAG.setRoot(Result.second);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005130 return 0;
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005131 }
Shuxin Yang970755e2012-10-19 20:11:16 +00005132
Bill Wendlingef375462008-11-21 02:38:44 +00005133 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00005134 case Intrinsic::sadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00005135 case Intrinsic::usub_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00005136 case Intrinsic::ssub_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00005137 case Intrinsic::umul_with_overflow:
Craig Topperc42e6402012-04-11 04:34:11 +00005138 case Intrinsic::smul_with_overflow: {
5139 ISD::NodeType Op;
5140 switch (Intrinsic) {
5141 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
5142 case Intrinsic::uadd_with_overflow: Op = ISD::UADDO; break;
5143 case Intrinsic::sadd_with_overflow: Op = ISD::SADDO; break;
5144 case Intrinsic::usub_with_overflow: Op = ISD::USUBO; break;
5145 case Intrinsic::ssub_with_overflow: Op = ISD::SSUBO; break;
5146 case Intrinsic::umul_with_overflow: Op = ISD::UMULO; break;
5147 case Intrinsic::smul_with_overflow: Op = ISD::SMULO; break;
5148 }
5149 SDValue Op1 = getValue(I.getArgOperand(0));
5150 SDValue Op2 = getValue(I.getArgOperand(1));
Bill Wendling7cdc3c82008-11-21 02:03:52 +00005151
Craig Topperc42e6402012-04-11 04:34:11 +00005152 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005153 setValue(&I, DAG.getNode(Op, sdl, VTs, Op1, Op2));
Craig Topperc42e6402012-04-11 04:34:11 +00005154 return 0;
5155 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005156 case Intrinsic::prefetch: {
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00005157 SDValue Ops[5];
Dale Johannesen1de4aa92010-10-26 23:11:10 +00005158 unsigned rw = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005159 Ops[0] = getRoot();
Gabor Greif0635f352010-06-25 09:38:13 +00005160 Ops[1] = getValue(I.getArgOperand(0));
5161 Ops[2] = getValue(I.getArgOperand(1));
5162 Ops[3] = getValue(I.getArgOperand(2));
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00005163 Ops[4] = getValue(I.getArgOperand(3));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005164 DAG.setRoot(DAG.getMemIntrinsicNode(ISD::PREFETCH, sdl,
Dale Johannesen1de4aa92010-10-26 23:11:10 +00005165 DAG.getVTList(MVT::Other),
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00005166 &Ops[0], 5,
Dale Johannesen1de4aa92010-10-26 23:11:10 +00005167 EVT::getIntegerVT(*Context, 8),
5168 MachinePointerInfo(I.getArgOperand(0)),
5169 0, /* align */
5170 false, /* volatile */
5171 rw==0, /* read */
5172 rw==1)); /* write */
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005173 return 0;
5174 }
Duncan Sandsf07c9492009-11-10 09:08:09 +00005175 case Intrinsic::lifetime_start:
Nadav Rotemc05d3062012-09-06 09:17:37 +00005176 case Intrinsic::lifetime_end: {
Nadav Rotemc05d3062012-09-06 09:17:37 +00005177 bool IsStart = (Intrinsic == Intrinsic::lifetime_start);
Nadav Rotem9a2ae002012-09-10 08:43:23 +00005178 // Stack coloring is not enabled in O0, discard region information.
5179 if (TM.getOptLevel() == CodeGenOpt::None)
5180 return 0;
Nadav Rotemc05d3062012-09-06 09:17:37 +00005181
Nadav Rotem9a2ae002012-09-10 08:43:23 +00005182 SmallVector<Value *, 4> Allocas;
5183 GetUnderlyingObjects(I.getArgOperand(1), Allocas, TD);
5184
Craig Topperf22fd3f2013-07-03 05:11:49 +00005185 for (SmallVectorImpl<Value*>::iterator Object = Allocas.begin(),
5186 E = Allocas.end(); Object != E; ++Object) {
Nadav Rotem9a2ae002012-09-10 08:43:23 +00005187 AllocaInst *LifetimeObject = dyn_cast_or_null<AllocaInst>(*Object);
5188
5189 // Could not find an Alloca.
5190 if (!LifetimeObject)
5191 continue;
5192
5193 int FI = FuncInfo.StaticAllocaMap[LifetimeObject];
5194
5195 SDValue Ops[2];
5196 Ops[0] = getRoot();
Bill Wendlingba54bca2013-06-19 21:36:55 +00005197 Ops[1] = DAG.getFrameIndex(FI, TLI->getPointerTy(), true);
Nadav Rotem9a2ae002012-09-10 08:43:23 +00005198 unsigned Opcode = (IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END);
5199
Andrew Trickac6d9be2013-05-25 02:42:55 +00005200 Res = DAG.getNode(Opcode, sdl, MVT::Other, Ops, 2);
Nadav Rotem9a2ae002012-09-10 08:43:23 +00005201 DAG.setRoot(Res);
5202 }
Nadav Rotem5882e562013-02-01 19:25:23 +00005203 return 0;
Nadav Rotemc05d3062012-09-06 09:17:37 +00005204 }
5205 case Intrinsic::invariant_start:
Duncan Sandsf07c9492009-11-10 09:08:09 +00005206 // Discard region information.
Bill Wendlingba54bca2013-06-19 21:36:55 +00005207 setValue(&I, DAG.getUNDEF(TLI->getPointerTy()));
Duncan Sandsf07c9492009-11-10 09:08:09 +00005208 return 0;
5209 case Intrinsic::invariant_end:
Duncan Sandsf07c9492009-11-10 09:08:09 +00005210 // Discard region information.
5211 return 0;
Nuno Lopes85b40892012-06-28 22:30:12 +00005212 case Intrinsic::donothing:
5213 // ignore
5214 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005215 }
5216}
5217
Dan Gohman46510a72010-04-15 01:51:59 +00005218void SelectionDAGBuilder::LowerCallTo(ImmutableCallSite CS, SDValue Callee,
Dan Gohman2048b852009-11-23 18:04:58 +00005219 bool isTailCall,
5220 MachineBasicBlock *LandingPad) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005221 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
5222 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
5223 Type *RetTy = FTy->getReturnType();
Chris Lattner512063d2010-04-05 06:19:28 +00005224 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Chris Lattner16112732010-03-14 01:41:15 +00005225 MCSymbol *BeginLabel = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005226
5227 TargetLowering::ArgListTy Args;
5228 TargetLowering::ArgListEntry Entry;
5229 Args.reserve(CS.arg_size());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005230
5231 // Check whether the function can return without sret-demotion.
Dan Gohman84023e02010-07-10 09:00:22 +00005232 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00005233 const TargetLowering *TLI = TM.getTargetLowering();
5234 GetReturnInfo(RetTy, CS.getAttributes(), Outs, *TLI);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005235
Bill Wendlingba54bca2013-06-19 21:36:55 +00005236 bool CanLowerReturn = TLI->CanLowerReturn(CS.getCallingConv(),
5237 DAG.getMachineFunction(),
5238 FTy->isVarArg(), Outs,
5239 FTy->getContext());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005240
5241 SDValue DemoteStackSlot;
Chris Lattnerecf42c42010-09-21 16:36:31 +00005242 int DemoteStackIdx = -100;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005243
5244 if (!CanLowerReturn) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00005245 uint64_t TySize = TLI->getDataLayout()->getTypeAllocSize(
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005246 FTy->getReturnType());
Bill Wendlingba54bca2013-06-19 21:36:55 +00005247 unsigned Align = TLI->getDataLayout()->getPrefTypeAlignment(
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005248 FTy->getReturnType());
5249 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattnerecf42c42010-09-21 16:36:31 +00005250 DemoteStackIdx = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005251 Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005252
Bill Wendlingba54bca2013-06-19 21:36:55 +00005253 DemoteStackSlot = DAG.getFrameIndex(DemoteStackIdx, TLI->getPointerTy());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005254 Entry.Node = DemoteStackSlot;
5255 Entry.Ty = StackSlotPtrType;
5256 Entry.isSExt = false;
5257 Entry.isZExt = false;
5258 Entry.isInReg = false;
5259 Entry.isSRet = true;
5260 Entry.isNest = false;
5261 Entry.isByVal = false;
Stephen Lin456ca042013-04-20 05:14:40 +00005262 Entry.isReturned = false;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005263 Entry.Alignment = Align;
5264 Args.push_back(Entry);
5265 RetTy = Type::getVoidTy(FTy->getContext());
5266 }
5267
Dan Gohman46510a72010-04-15 01:51:59 +00005268 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005269 i != e; ++i) {
Rafael Espindola3fa82832011-05-13 15:18:06 +00005270 const Value *V = *i;
5271
5272 // Skip empty types
5273 if (V->getType()->isEmptyTy())
5274 continue;
5275
5276 SDValue ArgNode = getValue(V);
5277 Entry.Node = ArgNode; Entry.Ty = V->getType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005278
5279 unsigned attrInd = i - CS.arg_begin() + 1;
Stephen Lin456ca042013-04-20 05:14:40 +00005280 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
5281 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
5282 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
5283 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
5284 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
5285 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
5286 Entry.isReturned = CS.paramHasAttr(attrInd, Attribute::Returned);
5287 Entry.Alignment = CS.getParamAlignment(attrInd);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005288 Args.push_back(Entry);
5289 }
5290
Chris Lattner512063d2010-04-05 06:19:28 +00005291 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005292 // Insert a label before the invoke call to mark the try range. This can be
5293 // used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00005294 BeginLabel = MMI.getContext().CreateTempSymbol();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00005295
Jim Grosbachca752c92010-01-28 01:45:32 +00005296 // For SjLj, keep track of which landing pads go with which invokes
5297 // so as to maintain the ordering of pads in the LSDA.
Chris Lattner512063d2010-04-05 06:19:28 +00005298 unsigned CallSiteIndex = MMI.getCurrentCallSite();
Jim Grosbachca752c92010-01-28 01:45:32 +00005299 if (CallSiteIndex) {
Chris Lattner512063d2010-04-05 06:19:28 +00005300 MMI.setCallSiteBeginLabel(BeginLabel, CallSiteIndex);
Bill Wendling30e67402011-10-05 22:24:35 +00005301 LPadToCallSiteMap[LandingPad].push_back(CallSiteIndex);
Bill Wendlinga8512ed2011-10-04 22:00:35 +00005302
Jim Grosbachca752c92010-01-28 01:45:32 +00005303 // Now that the call site is handled, stop tracking it.
Chris Lattner512063d2010-04-05 06:19:28 +00005304 MMI.setCurrentCallSite(0);
Jim Grosbachca752c92010-01-28 01:45:32 +00005305 }
5306
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005307 // Both PendingLoads and PendingExports must be flushed here;
5308 // this call might not return.
5309 (void)getRoot();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005310 DAG.setRoot(DAG.getEHLabel(getCurSDLoc(), getControlRoot(), BeginLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005311 }
5312
Dan Gohman98ca4f22009-08-05 01:29:28 +00005313 // Check if target-independent constraints permit a tail call here.
Bill Wendlingba54bca2013-06-19 21:36:55 +00005314 // Target-dependent constraints are checked within TLI->LowerCallTo.
5315 if (isTailCall && !isInTailCallPosition(CS, *TLI))
Dan Gohman98ca4f22009-08-05 01:29:28 +00005316 isTailCall = false;
5317
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00005318 TargetLowering::
5319 CallLoweringInfo CLI(getRoot(), RetTy, FTy, isTailCall, Callee, Args, DAG,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005320 getCurSDLoc(), CS);
Bill Wendlingba54bca2013-06-19 21:36:55 +00005321 std::pair<SDValue,SDValue> Result = TLI->LowerCallTo(CLI);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005322 assert((isTailCall || Result.second.getNode()) &&
5323 "Non-null chain expected with non-tail call!");
5324 assert((Result.second.getNode() || !Result.first.getNode()) &&
5325 "Null value expected with tail call!");
Bill Wendlinge80ae832009-12-22 00:50:32 +00005326 if (Result.first.getNode()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005327 setValue(CS.getInstruction(), Result.first);
Bill Wendlinge80ae832009-12-22 00:50:32 +00005328 } else if (!CanLowerReturn && Result.second.getNode()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005329 // The instruction result is the result of loading from the
5330 // hidden sret parameter.
5331 SmallVector<EVT, 1> PVTs;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005332 Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005333
Bill Wendlingba54bca2013-06-19 21:36:55 +00005334 ComputeValueVTs(*TLI, PtrRetTy, PVTs);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005335 assert(PVTs.size() == 1 && "Pointers should fit in one register");
5336 EVT PtrVT = PVTs[0];
Eli Friedman2db0e9e2012-05-25 00:09:29 +00005337
5338 SmallVector<EVT, 4> RetTys;
5339 SmallVector<uint64_t, 4> Offsets;
5340 RetTy = FTy->getReturnType();
Bill Wendlingba54bca2013-06-19 21:36:55 +00005341 ComputeValueVTs(*TLI, RetTy, RetTys, &Offsets);
Eli Friedman2db0e9e2012-05-25 00:09:29 +00005342
5343 unsigned NumValues = RetTys.size();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005344 SmallVector<SDValue, 4> Values(NumValues);
5345 SmallVector<SDValue, 4> Chains(NumValues);
5346
5347 for (unsigned i = 0; i < NumValues; ++i) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005348 SDValue Add = DAG.getNode(ISD::ADD, getCurSDLoc(), PtrVT,
Bill Wendlinge80ae832009-12-22 00:50:32 +00005349 DemoteStackSlot,
5350 DAG.getConstant(Offsets[i], PtrVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005351 SDValue L = DAG.getLoad(RetTys[i], getCurSDLoc(), Result.second, Add,
Chris Lattnerecf42c42010-09-21 16:36:31 +00005352 MachinePointerInfo::getFixedStack(DemoteStackIdx, Offsets[i]),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005353 false, false, false, 1);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005354 Values[i] = L;
5355 Chains[i] = L.getValue(1);
5356 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00005357
Andrew Trickac6d9be2013-05-25 02:42:55 +00005358 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005359 MVT::Other, &Chains[0], NumValues);
5360 PendingLoads.push_back(Chain);
Michael J. Spencere70c5262010-10-16 08:25:21 +00005361
Bill Wendling4533cac2010-01-28 21:51:40 +00005362 setValue(CS.getInstruction(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00005363 DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00005364 DAG.getVTList(&RetTys[0], RetTys.size()),
Eli Friedman2db0e9e2012-05-25 00:09:29 +00005365 &Values[0], Values.size()));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005366 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00005367
Evan Cheng8380c032011-04-01 19:42:22 +00005368 if (!Result.second.getNode()) {
Evan Chengc249e482011-04-01 19:57:01 +00005369 // As a special case, a null chain means that a tail call has been emitted and
5370 // the DAG root is already updated.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005371 HasTailCall = true;
Tim Northovere5a81a12013-07-06 12:58:45 +00005372
5373 // Since there's no actual continuation from this block, nothing can be
5374 // relying on us setting vregs for them.
5375 PendingExports.clear();
Evan Cheng8380c032011-04-01 19:42:22 +00005376 } else {
5377 DAG.setRoot(Result.second);
Evan Cheng8380c032011-04-01 19:42:22 +00005378 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005379
Chris Lattner512063d2010-04-05 06:19:28 +00005380 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005381 // Insert a label at the end of the invoke call to mark the try range. This
5382 // can be used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00005383 MCSymbol *EndLabel = MMI.getContext().CreateTempSymbol();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005384 DAG.setRoot(DAG.getEHLabel(getCurSDLoc(), getRoot(), EndLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005385
5386 // Inform MachineModuleInfo of range.
Chris Lattner512063d2010-04-05 06:19:28 +00005387 MMI.addInvoke(LandingPad, BeginLabel, EndLabel);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005388 }
5389}
5390
Chris Lattner8047d9a2009-12-24 00:37:38 +00005391/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
5392/// value is equal or not-equal to zero.
Dan Gohman46510a72010-04-15 01:51:59 +00005393static bool IsOnlyUsedInZeroEqualityComparison(const Value *V) {
5394 for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end();
Chris Lattner8047d9a2009-12-24 00:37:38 +00005395 UI != E; ++UI) {
Dan Gohman46510a72010-04-15 01:51:59 +00005396 if (const ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
Chris Lattner8047d9a2009-12-24 00:37:38 +00005397 if (IC->isEquality())
Dan Gohman46510a72010-04-15 01:51:59 +00005398 if (const Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
Chris Lattner8047d9a2009-12-24 00:37:38 +00005399 if (C->isNullValue())
5400 continue;
5401 // Unknown instruction.
5402 return false;
5403 }
5404 return true;
5405}
5406
Dan Gohman46510a72010-04-15 01:51:59 +00005407static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005408 Type *LoadTy,
Chris Lattner8047d9a2009-12-24 00:37:38 +00005409 SelectionDAGBuilder &Builder) {
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005410
Chris Lattner8047d9a2009-12-24 00:37:38 +00005411 // Check to see if this load can be trivially constant folded, e.g. if the
5412 // input is from a string literal.
Dan Gohman46510a72010-04-15 01:51:59 +00005413 if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00005414 // Cast pointer to the type we really want to load.
Dan Gohman46510a72010-04-15 01:51:59 +00005415 LoadInput = ConstantExpr::getBitCast(const_cast<Constant *>(LoadInput),
Chris Lattner8047d9a2009-12-24 00:37:38 +00005416 PointerType::getUnqual(LoadTy));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005417
Dan Gohman46510a72010-04-15 01:51:59 +00005418 if (const Constant *LoadCst =
5419 ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput),
5420 Builder.TD))
Chris Lattner8047d9a2009-12-24 00:37:38 +00005421 return Builder.getValue(LoadCst);
5422 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005423
Chris Lattner8047d9a2009-12-24 00:37:38 +00005424 // Otherwise, we have to emit the load. If the pointer is to unfoldable but
5425 // still constant memory, the input chain can be the entry node.
5426 SDValue Root;
5427 bool ConstantMemory = false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005428
Chris Lattner8047d9a2009-12-24 00:37:38 +00005429 // Do not serialize (non-volatile) loads of constant memory with anything.
5430 if (Builder.AA->pointsToConstantMemory(PtrVal)) {
5431 Root = Builder.DAG.getEntryNode();
5432 ConstantMemory = true;
5433 } else {
5434 // Do not serialize non-volatile loads against each other.
5435 Root = Builder.DAG.getRoot();
5436 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005437
Chris Lattner8047d9a2009-12-24 00:37:38 +00005438 SDValue Ptr = Builder.getValue(PtrVal);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005439 SDValue LoadVal = Builder.DAG.getLoad(LoadVT, Builder.getCurSDLoc(), Root,
Chris Lattnerecf42c42010-09-21 16:36:31 +00005440 Ptr, MachinePointerInfo(PtrVal),
David Greene1e559442010-02-15 17:00:31 +00005441 false /*volatile*/,
Stephen Lin155615d2013-07-08 00:37:03 +00005442 false /*nontemporal*/,
Pete Cooperd752e0f2011-11-08 18:42:53 +00005443 false /*isinvariant*/, 1 /* align=1 */);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005444
Chris Lattner8047d9a2009-12-24 00:37:38 +00005445 if (!ConstantMemory)
5446 Builder.PendingLoads.push_back(LoadVal.getValue(1));
5447 return LoadVal;
5448}
5449
5450
5451/// visitMemCmpCall - See if we can lower a call to memcmp in an optimized form.
5452/// If so, return true and lower it, otherwise return false and it will be
5453/// lowered like a normal call.
Dan Gohman46510a72010-04-15 01:51:59 +00005454bool SelectionDAGBuilder::visitMemCmpCall(const CallInst &I) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00005455 // Verify that the prototype makes sense. int memcmp(void*,void*,size_t)
Gabor Greif37387d52010-06-30 12:55:46 +00005456 if (I.getNumArgOperands() != 3)
Chris Lattner8047d9a2009-12-24 00:37:38 +00005457 return false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005458
Gabor Greif0635f352010-06-25 09:38:13 +00005459 const Value *LHS = I.getArgOperand(0), *RHS = I.getArgOperand(1);
Duncan Sands1df98592010-02-16 11:11:14 +00005460 if (!LHS->getType()->isPointerTy() || !RHS->getType()->isPointerTy() ||
Gabor Greif0635f352010-06-25 09:38:13 +00005461 !I.getArgOperand(2)->getType()->isIntegerTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +00005462 !I.getType()->isIntegerTy())
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005463 return false;
5464
Gabor Greif0635f352010-06-25 09:38:13 +00005465 const ConstantInt *Size = dyn_cast<ConstantInt>(I.getArgOperand(2));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005466
Chris Lattner8047d9a2009-12-24 00:37:38 +00005467 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
5468 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
Chris Lattner04b091a2009-12-24 01:07:17 +00005469 if (Size && IsOnlyUsedInZeroEqualityComparison(&I)) {
5470 bool ActuallyDoIt = true;
5471 MVT LoadVT;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005472 Type *LoadTy;
Chris Lattner04b091a2009-12-24 01:07:17 +00005473 switch (Size->getZExtValue()) {
5474 default:
5475 LoadVT = MVT::Other;
5476 LoadTy = 0;
5477 ActuallyDoIt = false;
5478 break;
5479 case 2:
5480 LoadVT = MVT::i16;
5481 LoadTy = Type::getInt16Ty(Size->getContext());
5482 break;
5483 case 4:
5484 LoadVT = MVT::i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005485 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005486 break;
5487 case 8:
5488 LoadVT = MVT::i64;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005489 LoadTy = Type::getInt64Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005490 break;
5491 /*
5492 case 16:
5493 LoadVT = MVT::v4i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005494 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005495 LoadTy = VectorType::get(LoadTy, 4);
5496 break;
5497 */
5498 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005499
Chris Lattner04b091a2009-12-24 01:07:17 +00005500 // This turns into unaligned loads. We only do this if the target natively
5501 // supports the MVT we'll be loading or if it is small enough (<= 4) that
5502 // we'll only produce a small number of byte loads.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005503
Chris Lattner04b091a2009-12-24 01:07:17 +00005504 // Require that we can find a legal MVT, and only do this if the target
5505 // supports unaligned loads of that type. Expanding into byte loads would
5506 // bloat the code.
Bill Wendlingba54bca2013-06-19 21:36:55 +00005507 const TargetLowering *TLI = TM.getTargetLowering();
Chris Lattner04b091a2009-12-24 01:07:17 +00005508 if (ActuallyDoIt && Size->getZExtValue() > 4) {
5509 // TODO: Handle 5 byte compare as 4-byte + 1 byte.
5510 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
Bill Wendlingba54bca2013-06-19 21:36:55 +00005511 if (!TLI->isTypeLegal(LoadVT) ||!TLI->allowsUnalignedMemoryAccesses(LoadVT))
Chris Lattner04b091a2009-12-24 01:07:17 +00005512 ActuallyDoIt = false;
5513 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005514
Chris Lattner04b091a2009-12-24 01:07:17 +00005515 if (ActuallyDoIt) {
5516 SDValue LHSVal = getMemCmpLoad(LHS, LoadVT, LoadTy, *this);
5517 SDValue RHSVal = getMemCmpLoad(RHS, LoadVT, LoadTy, *this);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005518
Andrew Trickac6d9be2013-05-25 02:42:55 +00005519 SDValue Res = DAG.getSetCC(getCurSDLoc(), MVT::i1, LHSVal, RHSVal,
Chris Lattner04b091a2009-12-24 01:07:17 +00005520 ISD::SETNE);
Bill Wendlingba54bca2013-06-19 21:36:55 +00005521 EVT CallVT = TLI->getValueType(I.getType(), true);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005522 setValue(&I, DAG.getZExtOrTrunc(Res, getCurSDLoc(), CallVT));
Chris Lattner04b091a2009-12-24 01:07:17 +00005523 return true;
5524 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00005525 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005526
5527
Chris Lattner8047d9a2009-12-24 00:37:38 +00005528 return false;
5529}
5530
Bob Wilson53624a22012-08-03 23:29:17 +00005531/// visitUnaryFloatCall - If a call instruction is a unary floating-point
5532/// operation (as expected), translate it to an SDNode with the specified opcode
5533/// and return true.
5534bool SelectionDAGBuilder::visitUnaryFloatCall(const CallInst &I,
5535 unsigned Opcode) {
5536 // Sanity check that it really is a unary floating-point call.
5537 if (I.getNumArgOperands() != 1 ||
5538 !I.getArgOperand(0)->getType()->isFloatingPointTy() ||
5539 I.getType() != I.getArgOperand(0)->getType() ||
5540 !I.onlyReadsMemory())
5541 return false;
5542
5543 SDValue Tmp = getValue(I.getArgOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005544 setValue(&I, DAG.getNode(Opcode, getCurSDLoc(), Tmp.getValueType(), Tmp));
Bob Wilson53624a22012-08-03 23:29:17 +00005545 return true;
5546}
Chris Lattner8047d9a2009-12-24 00:37:38 +00005547
Dan Gohman46510a72010-04-15 01:51:59 +00005548void SelectionDAGBuilder::visitCall(const CallInst &I) {
Chris Lattner598751e2010-07-05 05:36:21 +00005549 // Handle inline assembly differently.
5550 if (isa<InlineAsm>(I.getCalledValue())) {
5551 visitInlineAsm(&I);
5552 return;
5553 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00005554
Michael J. Spencer391b43b2010-10-21 20:49:23 +00005555 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Michael J. Spencerc9c137b2012-02-22 19:06:13 +00005556 ComputeUsesVAFloatArgument(I, &MMI);
Michael J. Spencer391b43b2010-10-21 20:49:23 +00005557
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005558 const char *RenameFn = 0;
5559 if (Function *F = I.getCalledFunction()) {
5560 if (F->isDeclaration()) {
Chris Lattner598751e2010-07-05 05:36:21 +00005561 if (const TargetIntrinsicInfo *II = TM.getIntrinsicInfo()) {
Dale Johannesen49de9822009-02-05 01:49:45 +00005562 if (unsigned IID = II->getIntrinsicID(F)) {
5563 RenameFn = visitIntrinsicCall(I, IID);
5564 if (!RenameFn)
5565 return;
5566 }
5567 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005568 if (unsigned IID = F->getIntrinsicID()) {
5569 RenameFn = visitIntrinsicCall(I, IID);
5570 if (!RenameFn)
5571 return;
5572 }
5573 }
5574
5575 // Check for well-known libc/libm calls. If the function is internal, it
5576 // can't be a library call.
Bob Wilson982dc842012-08-03 21:26:24 +00005577 LibFunc::Func Func;
5578 if (!F->hasLocalLinkage() && F->hasName() &&
5579 LibInfo->getLibFunc(F->getName(), Func) &&
5580 LibInfo->hasOptimizedCodeGen(Func)) {
5581 switch (Func) {
5582 default: break;
5583 case LibFunc::copysign:
5584 case LibFunc::copysignf:
5585 case LibFunc::copysignl:
Gabor Greif37387d52010-06-30 12:55:46 +00005586 if (I.getNumArgOperands() == 2 && // Basic sanity checks.
Gabor Greif0635f352010-06-25 09:38:13 +00005587 I.getArgOperand(0)->getType()->isFloatingPointTy() &&
5588 I.getType() == I.getArgOperand(0)->getType() &&
Bob Wilson53624a22012-08-03 23:29:17 +00005589 I.getType() == I.getArgOperand(1)->getType() &&
5590 I.onlyReadsMemory()) {
Gabor Greif0635f352010-06-25 09:38:13 +00005591 SDValue LHS = getValue(I.getArgOperand(0));
5592 SDValue RHS = getValue(I.getArgOperand(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005593 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurSDLoc(),
Bill Wendling0d580132009-12-23 01:28:19 +00005594 LHS.getValueType(), LHS, RHS));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005595 return;
5596 }
Bob Wilson982dc842012-08-03 21:26:24 +00005597 break;
5598 case LibFunc::fabs:
5599 case LibFunc::fabsf:
5600 case LibFunc::fabsl:
Bob Wilson53624a22012-08-03 23:29:17 +00005601 if (visitUnaryFloatCall(I, ISD::FABS))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005602 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005603 break;
5604 case LibFunc::sin:
5605 case LibFunc::sinf:
5606 case LibFunc::sinl:
Bob Wilson53624a22012-08-03 23:29:17 +00005607 if (visitUnaryFloatCall(I, ISD::FSIN))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005608 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005609 break;
5610 case LibFunc::cos:
5611 case LibFunc::cosf:
5612 case LibFunc::cosl:
Bob Wilson53624a22012-08-03 23:29:17 +00005613 if (visitUnaryFloatCall(I, ISD::FCOS))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005614 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005615 break;
5616 case LibFunc::sqrt:
5617 case LibFunc::sqrtf:
5618 case LibFunc::sqrtl:
Preston Gurdb704d232013-05-27 15:44:35 +00005619 case LibFunc::sqrt_finite:
5620 case LibFunc::sqrtf_finite:
5621 case LibFunc::sqrtl_finite:
Bob Wilson53624a22012-08-03 23:29:17 +00005622 if (visitUnaryFloatCall(I, ISD::FSQRT))
Dale Johannesen52fb79b2009-09-25 17:23:22 +00005623 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005624 break;
5625 case LibFunc::floor:
5626 case LibFunc::floorf:
5627 case LibFunc::floorl:
Bob Wilson53624a22012-08-03 23:29:17 +00005628 if (visitUnaryFloatCall(I, ISD::FFLOOR))
Owen Anderson4a4fdf32011-12-08 19:32:14 +00005629 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005630 break;
5631 case LibFunc::nearbyint:
5632 case LibFunc::nearbyintf:
5633 case LibFunc::nearbyintl:
Bob Wilson53624a22012-08-03 23:29:17 +00005634 if (visitUnaryFloatCall(I, ISD::FNEARBYINT))
Owen Anderson4a4fdf32011-12-08 19:32:14 +00005635 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005636 break;
5637 case LibFunc::ceil:
5638 case LibFunc::ceilf:
5639 case LibFunc::ceill:
Bob Wilson53624a22012-08-03 23:29:17 +00005640 if (visitUnaryFloatCall(I, ISD::FCEIL))
Owen Anderson4a4fdf32011-12-08 19:32:14 +00005641 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005642 break;
5643 case LibFunc::rint:
5644 case LibFunc::rintf:
5645 case LibFunc::rintl:
Bob Wilson53624a22012-08-03 23:29:17 +00005646 if (visitUnaryFloatCall(I, ISD::FRINT))
Owen Anderson4a4fdf32011-12-08 19:32:14 +00005647 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005648 break;
Hal Finkel41418d12013-08-07 22:49:12 +00005649 case LibFunc::round:
5650 case LibFunc::roundf:
5651 case LibFunc::roundl:
5652 if (visitUnaryFloatCall(I, ISD::FROUND))
5653 return;
5654 break;
Bob Wilson982dc842012-08-03 21:26:24 +00005655 case LibFunc::trunc:
5656 case LibFunc::truncf:
5657 case LibFunc::truncl:
Bob Wilson53624a22012-08-03 23:29:17 +00005658 if (visitUnaryFloatCall(I, ISD::FTRUNC))
Owen Anderson4a4fdf32011-12-08 19:32:14 +00005659 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005660 break;
5661 case LibFunc::log2:
5662 case LibFunc::log2f:
5663 case LibFunc::log2l:
Bob Wilson53624a22012-08-03 23:29:17 +00005664 if (visitUnaryFloatCall(I, ISD::FLOG2))
Owen Anderson4e0adfa2011-12-15 00:54:12 +00005665 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005666 break;
5667 case LibFunc::exp2:
5668 case LibFunc::exp2f:
5669 case LibFunc::exp2l:
Bob Wilson53624a22012-08-03 23:29:17 +00005670 if (visitUnaryFloatCall(I, ISD::FEXP2))
Owen Anderson4e0adfa2011-12-15 00:54:12 +00005671 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005672 break;
5673 case LibFunc::memcmp:
Chris Lattner8047d9a2009-12-24 00:37:38 +00005674 if (visitMemCmpCall(I))
5675 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005676 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005677 }
5678 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005679 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00005680
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005681 SDValue Callee;
5682 if (!RenameFn)
Gabor Greif0635f352010-06-25 09:38:13 +00005683 Callee = getValue(I.getCalledValue());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005684 else
Bill Wendlingba54bca2013-06-19 21:36:55 +00005685 Callee = DAG.getExternalSymbol(RenameFn,
5686 TM.getTargetLowering()->getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005687
Bill Wendling0d580132009-12-23 01:28:19 +00005688 // Check if we can potentially perform a tail call. More detailed checking is
5689 // be done within LowerCallTo, after more information about the call is known.
Evan Cheng11e67932010-01-26 23:13:04 +00005690 LowerCallTo(&I, Callee, I.isTailCall());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005691}
5692
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005693namespace {
Dan Gohman462f6b52010-05-29 17:53:24 +00005694
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005695/// AsmOperandInfo - This contains information for each constraint that we are
5696/// lowering.
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005697class SDISelAsmOperandInfo : public TargetLowering::AsmOperandInfo {
Cedric Venetaff9c272009-02-14 16:06:42 +00005698public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005699 /// CallOperand - If this is the result output operand or a clobber
5700 /// this is null, otherwise it is the incoming operand to the CallInst.
5701 /// This gets modified as the asm is processed.
5702 SDValue CallOperand;
5703
5704 /// AssignedRegs - If this is a register or register class operand, this
5705 /// contains the set of register corresponding to the operand.
5706 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005707
John Thompsoneac6e1d2010-09-13 18:15:37 +00005708 explicit SDISelAsmOperandInfo(const TargetLowering::AsmOperandInfo &info)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005709 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
5710 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005711
Owen Andersone50ed302009-08-10 22:56:29 +00005712 /// getCallOperandValEVT - Return the EVT of the Value* that this operand
Chris Lattner81249c92008-10-17 17:05:25 +00005713 /// corresponds to. If there is no Value* for this operand, it returns
Owen Anderson825b72b2009-08-11 20:47:22 +00005714 /// MVT::Other.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005715 EVT getCallOperandValEVT(LLVMContext &Context,
Owen Anderson1d0be152009-08-13 21:58:54 +00005716 const TargetLowering &TLI,
Micah Villmow3574eca2012-10-08 16:38:25 +00005717 const DataLayout *TD) const {
Owen Anderson825b72b2009-08-11 20:47:22 +00005718 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005719
Chris Lattner81249c92008-10-17 17:05:25 +00005720 if (isa<BasicBlock>(CallOperandVal))
5721 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005722
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005723 llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005724
Eric Christophercef81b72011-05-09 20:04:43 +00005725 // FIXME: code duplicated from TargetLowering::ParseConstraints().
Chris Lattner81249c92008-10-17 17:05:25 +00005726 // If this is an indirect operand, the operand is a pointer to the
5727 // accessed type.
Bob Wilsone261b0c2009-12-22 18:34:19 +00005728 if (isIndirect) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005729 llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
Bob Wilsone261b0c2009-12-22 18:34:19 +00005730 if (!PtrTy)
Chris Lattner75361b62010-04-07 22:58:41 +00005731 report_fatal_error("Indirect operand for inline asm not a pointer!");
Bob Wilsone261b0c2009-12-22 18:34:19 +00005732 OpTy = PtrTy->getElementType();
5733 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005734
Eric Christophercef81b72011-05-09 20:04:43 +00005735 // Look for vector wrapped in a struct. e.g. { <16 x i8> }.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005736 if (StructType *STy = dyn_cast<StructType>(OpTy))
Eric Christophercef81b72011-05-09 20:04:43 +00005737 if (STy->getNumElements() == 1)
5738 OpTy = STy->getElementType(0);
5739
Chris Lattner81249c92008-10-17 17:05:25 +00005740 // If OpTy is not a single value, it may be a struct/union that we
5741 // can tile with integers.
5742 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
5743 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
5744 switch (BitSize) {
5745 default: break;
5746 case 1:
5747 case 8:
5748 case 16:
5749 case 32:
5750 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00005751 case 128:
Owen Anderson1d0be152009-08-13 21:58:54 +00005752 OpTy = IntegerType::get(Context, BitSize);
Chris Lattner81249c92008-10-17 17:05:25 +00005753 break;
5754 }
5755 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005756
Chris Lattner81249c92008-10-17 17:05:25 +00005757 return TLI.getValueType(OpTy, true);
5758 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005759};
Dan Gohman462f6b52010-05-29 17:53:24 +00005760
John Thompson44ab89e2010-10-29 17:29:13 +00005761typedef SmallVector<SDISelAsmOperandInfo,16> SDISelAsmOperandInfoVector;
5762
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005763} // end anonymous namespace
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005764
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005765/// GetRegistersForValue - Assign registers (virtual or physical) for the
5766/// specified operand. We prefer to assign virtual registers, to allow the
Bob Wilson266d9452009-12-17 05:07:36 +00005767/// register allocator to handle the assignment process. However, if the asm
5768/// uses features that we can't model on machineinstrs, we have SDISel do the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005769/// allocation. This produces generally horrible, but correct, code.
5770///
5771/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005772///
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005773static void GetRegistersForValue(SelectionDAG &DAG,
5774 const TargetLowering &TLI,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005775 SDLoc DL,
Benjamin Kramer8b93ff22012-02-24 14:01:17 +00005776 SDISelAsmOperandInfo &OpInfo) {
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005777 LLVMContext &Context = *DAG.getContext();
Owen Anderson23b9b192009-08-12 00:36:31 +00005778
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005779 MachineFunction &MF = DAG.getMachineFunction();
5780 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005781
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005782 // If this is a constraint for a single physreg, or a constraint for a
5783 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005784 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005785 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
5786 OpInfo.ConstraintVT);
5787
5788 unsigned NumRegs = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00005789 if (OpInfo.ConstraintVT != MVT::Other) {
Chris Lattner01426e12008-10-21 00:45:36 +00005790 // If this is a FP input in an integer register (or visa versa) insert a bit
5791 // cast of the input value. More generally, handle any case where the input
5792 // value disagrees with the register class we plan to stick this in.
5793 if (OpInfo.Type == InlineAsm::isInput &&
5794 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00005795 // Try to convert to the first EVT that the reg class contains. If the
Chris Lattner01426e12008-10-21 00:45:36 +00005796 // types are identical size, use a bitcast to convert (e.g. two differing
5797 // vector types).
Patrik Hagglund8963fec2012-12-19 12:23:01 +00005798 MVT RegVT = *PhysReg.second->vt_begin();
Chris Lattner01426e12008-10-21 00:45:36 +00005799 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005800 OpInfo.CallOperand = DAG.getNode(ISD::BITCAST, DL,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005801 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005802 OpInfo.ConstraintVT = RegVT;
5803 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
5804 // If the input is a FP value and we want it in FP registers, do a
5805 // bitcast to the corresponding integer type. This turns an f64 value
5806 // into i64, which can be passed with two i32 values on a 32-bit
5807 // machine.
Patrik Hagglund8963fec2012-12-19 12:23:01 +00005808 RegVT = MVT::getIntegerVT(OpInfo.ConstraintVT.getSizeInBits());
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005809 OpInfo.CallOperand = DAG.getNode(ISD::BITCAST, DL,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005810 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005811 OpInfo.ConstraintVT = RegVT;
5812 }
5813 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005814
Owen Anderson23b9b192009-08-12 00:36:31 +00005815 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00005816 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005817
Patrik Hagglund8963fec2012-12-19 12:23:01 +00005818 MVT RegVT;
Owen Andersone50ed302009-08-10 22:56:29 +00005819 EVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005820
5821 // If this is a constraint for a specific physical register, like {r17},
5822 // assign it now.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005823 if (unsigned AssignedReg = PhysReg.first) {
5824 const TargetRegisterClass *RC = PhysReg.second;
Owen Anderson825b72b2009-08-11 20:47:22 +00005825 if (OpInfo.ConstraintVT == MVT::Other)
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005826 ValueVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005827
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005828 // Get the actual register value type. This is important, because the user
5829 // may have asked for (e.g.) the AX register in i32 type. We need to
5830 // remember that AX is actually i16 to get the right extension.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005831 RegVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005832
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005833 // This is a explicit reference to a physical register.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005834 Regs.push_back(AssignedReg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005835
5836 // If this is an expanded reference, add the rest of the regs to Regs.
5837 if (NumRegs != 1) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005838 TargetRegisterClass::iterator I = RC->begin();
5839 for (; *I != AssignedReg; ++I)
5840 assert(I != RC->end() && "Didn't find reg!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005841
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005842 // Already added the first reg.
5843 --NumRegs; ++I;
5844 for (; NumRegs; --NumRegs, ++I) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005845 assert(I != RC->end() && "Ran out of registers to allocate!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005846 Regs.push_back(*I);
5847 }
5848 }
Bill Wendling651ad132009-12-22 01:25:10 +00005849
Dan Gohman7451d3e2010-05-29 17:03:36 +00005850 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005851 return;
5852 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005853
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005854 // Otherwise, if this was a reference to an LLVM register class, create vregs
5855 // for this reference.
Chris Lattnerb3b44842009-03-24 15:25:07 +00005856 if (const TargetRegisterClass *RC = PhysReg.second) {
5857 RegVT = *RC->vt_begin();
Owen Anderson825b72b2009-08-11 20:47:22 +00005858 if (OpInfo.ConstraintVT == MVT::Other)
Evan Chengfb112882009-03-23 08:01:15 +00005859 ValueVT = RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005860
Evan Chengfb112882009-03-23 08:01:15 +00005861 // Create the appropriate number of virtual registers.
5862 MachineRegisterInfo &RegInfo = MF.getRegInfo();
5863 for (; NumRegs; --NumRegs)
Chris Lattnerb3b44842009-03-24 15:25:07 +00005864 Regs.push_back(RegInfo.createVirtualRegister(RC));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005865
Dan Gohman7451d3e2010-05-29 17:03:36 +00005866 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
Evan Chengfb112882009-03-23 08:01:15 +00005867 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005868 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005869
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005870 // Otherwise, we couldn't allocate enough registers for this.
5871}
5872
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005873/// visitInlineAsm - Handle a call to an InlineAsm object.
5874///
Dan Gohman46510a72010-04-15 01:51:59 +00005875void SelectionDAGBuilder::visitInlineAsm(ImmutableCallSite CS) {
5876 const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005877
5878 /// ConstraintOperands - Information about all of the constraints.
John Thompson44ab89e2010-10-29 17:29:13 +00005879 SDISelAsmOperandInfoVector ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005880
Bill Wendlingba54bca2013-06-19 21:36:55 +00005881 const TargetLowering *TLI = TM.getTargetLowering();
Evan Chengce1cdac2011-05-06 20:52:23 +00005882 TargetLowering::AsmOperandInfoVector
Bill Wendlingba54bca2013-06-19 21:36:55 +00005883 TargetConstraints = TLI->ParseConstraints(CS);
Evan Chengce1cdac2011-05-06 20:52:23 +00005884
John Thompsoneac6e1d2010-09-13 18:15:37 +00005885 bool hasMemory = false;
Michael J. Spencere70c5262010-10-16 08:25:21 +00005886
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005887 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
5888 unsigned ResNo = 0; // ResNo - The result number of the next output.
John Thompsoneac6e1d2010-09-13 18:15:37 +00005889 for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) {
5890 ConstraintOperands.push_back(SDISelAsmOperandInfo(TargetConstraints[i]));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005891 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Michael J. Spencere70c5262010-10-16 08:25:21 +00005892
Patrik Hagglundc698d3a2012-12-19 15:19:11 +00005893 MVT OpVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005894
5895 // Compute the value type for each operand.
5896 switch (OpInfo.Type) {
5897 case InlineAsm::isOutput:
5898 // Indirect outputs just consume an argument.
5899 if (OpInfo.isIndirect) {
Dan Gohman46510a72010-04-15 01:51:59 +00005900 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005901 break;
5902 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005903
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005904 // The return value of the call is this value. As such, there is no
5905 // corresponding argument.
Nick Lewycky8de34002011-09-30 22:19:53 +00005906 assert(!CS.getType()->isVoidTy() && "Bad inline asm!");
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005907 if (StructType *STy = dyn_cast<StructType>(CS.getType())) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00005908 OpVT = TLI->getSimpleValueType(STy->getElementType(ResNo));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005909 } else {
5910 assert(ResNo == 0 && "Asm only has one result!");
Bill Wendlingba54bca2013-06-19 21:36:55 +00005911 OpVT = TLI->getSimpleValueType(CS.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005912 }
5913 ++ResNo;
5914 break;
5915 case InlineAsm::isInput:
Dan Gohman46510a72010-04-15 01:51:59 +00005916 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005917 break;
5918 case InlineAsm::isClobber:
5919 // Nothing to do.
5920 break;
5921 }
5922
5923 // If this is an input or an indirect output, process the call argument.
5924 // BasicBlocks are labels, currently appearing only in asm's.
5925 if (OpInfo.CallOperandVal) {
Dan Gohman46510a72010-04-15 01:51:59 +00005926 if (const BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005927 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00005928 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005929 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005930 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005931
Bill Wendlingba54bca2013-06-19 21:36:55 +00005932 OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), *TLI, TD).
Patrik Hagglundc698d3a2012-12-19 15:19:11 +00005933 getSimpleVT();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005934 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005935
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005936 OpInfo.ConstraintVT = OpVT;
Michael J. Spencere70c5262010-10-16 08:25:21 +00005937
John Thompsoneac6e1d2010-09-13 18:15:37 +00005938 // Indirect operand accesses access memory.
5939 if (OpInfo.isIndirect)
5940 hasMemory = true;
5941 else {
5942 for (unsigned j = 0, ee = OpInfo.Codes.size(); j != ee; ++j) {
Evan Chengce1cdac2011-05-06 20:52:23 +00005943 TargetLowering::ConstraintType
Bill Wendlingba54bca2013-06-19 21:36:55 +00005944 CType = TLI->getConstraintType(OpInfo.Codes[j]);
John Thompsoneac6e1d2010-09-13 18:15:37 +00005945 if (CType == TargetLowering::C_Memory) {
5946 hasMemory = true;
5947 break;
5948 }
5949 }
5950 }
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005951 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005952
John Thompsoneac6e1d2010-09-13 18:15:37 +00005953 SDValue Chain, Flag;
5954
5955 // We won't need to flush pending loads if this asm doesn't touch
5956 // memory and is nonvolatile.
5957 if (hasMemory || IA->hasSideEffects())
5958 Chain = getRoot();
5959 else
5960 Chain = DAG.getRoot();
5961
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005962 // Second pass over the constraints: compute which constraint option to use
5963 // and assign registers to constraints that want a specific physreg.
John Thompsoneac6e1d2010-09-13 18:15:37 +00005964 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005965 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005966
John Thompson54584742010-09-24 22:24:05 +00005967 // If this is an output operand with a matching input operand, look up the
5968 // matching input. If their types mismatch, e.g. one is an integer, the
5969 // other is floating point, or their sizes are different, flag it as an
5970 // error.
5971 if (OpInfo.hasMatchingInput()) {
5972 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
Michael J. Spencere70c5262010-10-16 08:25:21 +00005973
John Thompson54584742010-09-24 22:24:05 +00005974 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Bill Wendling96cb1122012-07-19 00:04:14 +00005975 std::pair<unsigned, const TargetRegisterClass*> MatchRC =
Bill Wendlingba54bca2013-06-19 21:36:55 +00005976 TLI->getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
5977 OpInfo.ConstraintVT);
Bill Wendling96cb1122012-07-19 00:04:14 +00005978 std::pair<unsigned, const TargetRegisterClass*> InputRC =
Bill Wendlingba54bca2013-06-19 21:36:55 +00005979 TLI->getRegForInlineAsmConstraint(Input.ConstraintCode,
5980 Input.ConstraintVT);
John Thompson54584742010-09-24 22:24:05 +00005981 if ((OpInfo.ConstraintVT.isInteger() !=
5982 Input.ConstraintVT.isInteger()) ||
Eric Christopher5427ede2011-07-14 20:13:52 +00005983 (MatchRC.second != InputRC.second)) {
John Thompson54584742010-09-24 22:24:05 +00005984 report_fatal_error("Unsupported asm: input constraint"
5985 " with a matching output constraint of"
5986 " incompatible type!");
5987 }
5988 Input.ConstraintVT = OpInfo.ConstraintVT;
5989 }
5990 }
5991
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005992 // Compute the constraint code and ConstraintType to use.
Bill Wendlingba54bca2013-06-19 21:36:55 +00005993 TLI->ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005994
Eric Christopherfffe3632013-01-11 18:12:39 +00005995 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
5996 OpInfo.Type == InlineAsm::isClobber)
5997 continue;
5998
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005999 // If this is a memory input, and if the operand is not indirect, do what we
6000 // need to to provide an address for the memory input.
6001 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
6002 !OpInfo.isIndirect) {
Evan Chengce1cdac2011-05-06 20:52:23 +00006003 assert((OpInfo.isMultipleAlternative ||
6004 (OpInfo.Type == InlineAsm::isInput)) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006005 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006006
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006007 // Memory operands really want the address of the value. If we don't have
6008 // an indirect input, put it in the constpool if we can, otherwise spill
6009 // it to a stack slot.
Eric Christophere0b42c02011-06-03 17:21:23 +00006010 // TODO: This isn't quite right. We need to handle these according to
6011 // the addressing mode that the constraint wants. Also, this may take
6012 // an additional register for the computation and we don't want that
6013 // either.
Eric Christopher471e4222011-06-08 23:55:35 +00006014
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006015 // If the operand is a float, integer, or vector constant, spill to a
6016 // constant pool entry to get its address.
Dan Gohman46510a72010-04-15 01:51:59 +00006017 const Value *OpVal = OpInfo.CallOperandVal;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006018 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
Chris Lattnera78fa8c2012-01-27 03:08:05 +00006019 isa<ConstantVector>(OpVal) || isa<ConstantDataVector>(OpVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006020 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
Bill Wendlingba54bca2013-06-19 21:36:55 +00006021 TLI->getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006022 } else {
6023 // Otherwise, create a stack slot and emit a store to it before the
6024 // asm.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006025 Type *Ty = OpVal->getType();
Bill Wendlingba54bca2013-06-19 21:36:55 +00006026 uint64_t TySize = TLI->getDataLayout()->getTypeAllocSize(Ty);
6027 unsigned Align = TLI->getDataLayout()->getPrefTypeAlignment(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006028 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006029 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Bill Wendlingba54bca2013-06-19 21:36:55 +00006030 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI->getPointerTy());
Andrew Trickac6d9be2013-05-25 02:42:55 +00006031 Chain = DAG.getStore(Chain, getCurSDLoc(),
Chris Lattnerecf42c42010-09-21 16:36:31 +00006032 OpInfo.CallOperand, StackSlot,
6033 MachinePointerInfo::getFixedStack(SSFI),
David Greene1e559442010-02-15 17:00:31 +00006034 false, false, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006035 OpInfo.CallOperand = StackSlot;
6036 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006037
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006038 // There is no longer a Value* corresponding to this operand.
6039 OpInfo.CallOperandVal = 0;
Bill Wendling651ad132009-12-22 01:25:10 +00006040
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006041 // It is now an indirect operand.
6042 OpInfo.isIndirect = true;
6043 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006044
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006045 // If this constraint is for a specific register, allocate it before
6046 // anything else.
6047 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Bill Wendlingba54bca2013-06-19 21:36:55 +00006048 GetRegistersForValue(DAG, *TLI, getCurSDLoc(), OpInfo);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006049 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006050
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006051 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00006052 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006053 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
6054 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006055
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006056 // C_Register operands have already been allocated, Other/Memory don't need
6057 // to be.
6058 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Bill Wendlingba54bca2013-06-19 21:36:55 +00006059 GetRegistersForValue(DAG, *TLI, getCurSDLoc(), OpInfo);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006060 }
6061
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006062 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
6063 std::vector<SDValue> AsmNodeOperands;
6064 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
6065 AsmNodeOperands.push_back(
Dan Gohmanf2d7fb32010-01-04 21:00:54 +00006066 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00006067 TLI->getPointerTy()));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006068
Chris Lattnerdecc2672010-04-07 05:20:54 +00006069 // If we have a !srcloc metadata node associated with it, we want to attach
6070 // this to the ultimately generated inline asm machineinstr. To do this, we
6071 // pass in the third operand as this (potentially null) inline asm MDNode.
6072 const MDNode *SrcLoc = CS.getInstruction()->getMetadata("srcloc");
6073 AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006074
Chad Rosier3d716882012-10-30 19:11:54 +00006075 // Remember the HasSideEffect, AlignStack, AsmDialect, MayLoad and MayStore
6076 // bits as operand 3.
Evan Chengc36b7062011-01-07 23:50:32 +00006077 unsigned ExtraInfo = 0;
6078 if (IA->hasSideEffects())
6079 ExtraInfo |= InlineAsm::Extra_HasSideEffects;
6080 if (IA->isAlignStack())
6081 ExtraInfo |= InlineAsm::Extra_IsAlignStack;
Chad Rosier77fffa62012-09-05 22:17:43 +00006082 // Set the asm dialect.
Chad Rosier2f1d8152012-09-05 22:40:13 +00006083 ExtraInfo |= IA->getDialect() * InlineAsm::Extra_AsmDialect;
Chad Rosier3d716882012-10-30 19:11:54 +00006084
6085 // Determine if this InlineAsm MayLoad or MayStore based on the constraints.
6086 for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) {
6087 TargetLowering::AsmOperandInfo &OpInfo = TargetConstraints[i];
6088
6089 // Compute the constraint code and ConstraintType to use.
Bill Wendlingba54bca2013-06-19 21:36:55 +00006090 TLI->ComputeConstraintToUse(OpInfo, SDValue());
Chad Rosier3d716882012-10-30 19:11:54 +00006091
Chad Rosierdfa4cec2012-10-30 20:01:12 +00006092 // Ideally, we would only check against memory constraints. However, the
6093 // meaning of an other constraint can be target-specific and we can't easily
6094 // reason about it. Therefore, be conservative and set MayLoad/MayStore
6095 // for other constriants as well.
Chad Rosier3d716882012-10-30 19:11:54 +00006096 if (OpInfo.ConstraintType == TargetLowering::C_Memory ||
6097 OpInfo.ConstraintType == TargetLowering::C_Other) {
6098 if (OpInfo.Type == InlineAsm::isInput)
6099 ExtraInfo |= InlineAsm::Extra_MayLoad;
6100 else if (OpInfo.Type == InlineAsm::isOutput)
6101 ExtraInfo |= InlineAsm::Extra_MayStore;
Eric Christopherfffe3632013-01-11 18:12:39 +00006102 else if (OpInfo.Type == InlineAsm::isClobber)
6103 ExtraInfo |= (InlineAsm::Extra_MayLoad | InlineAsm::Extra_MayStore);
Chad Rosier3d716882012-10-30 19:11:54 +00006104 }
6105 }
6106
Evan Chengc36b7062011-01-07 23:50:32 +00006107 AsmNodeOperands.push_back(DAG.getTargetConstant(ExtraInfo,
Bill Wendlingba54bca2013-06-19 21:36:55 +00006108 TLI->getPointerTy()));
Dale Johannesenf1e309e2010-07-02 20:16:09 +00006109
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006110 // Loop over all of the inputs, copying the operand values into the
6111 // appropriate registers and processing the output regs.
6112 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006113
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006114 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
6115 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006116
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006117 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
6118 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
6119
6120 switch (OpInfo.Type) {
6121 case InlineAsm::isOutput: {
6122 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
6123 OpInfo.ConstraintType != TargetLowering::C_Register) {
6124 // Memory output, or 'other' output (e.g. 'X' constraint).
6125 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
6126
6127 // Add information to the INLINEASM node to know about this output.
Chris Lattnerdecc2672010-04-07 05:20:54 +00006128 unsigned OpFlags = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
6129 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags,
Bill Wendlingba54bca2013-06-19 21:36:55 +00006130 TLI->getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006131 AsmNodeOperands.push_back(OpInfo.CallOperand);
6132 break;
6133 }
6134
6135 // Otherwise, this is a register or register class output.
6136
6137 // Copy the output from the appropriate register. Find a register that
6138 // we can use.
Chris Lattnerfcd70902012-01-03 23:51:01 +00006139 if (OpInfo.AssignedRegs.Regs.empty()) {
6140 LLVMContext &Ctx = *DAG.getContext();
Stephen Lin155615d2013-07-08 00:37:03 +00006141 Ctx.emitError(CS.getInstruction(),
Chris Lattnerfcd70902012-01-03 23:51:01 +00006142 "couldn't allocate output register for constraint '" +
Eric Christopher1a54c572013-07-31 01:26:24 +00006143 Twine(OpInfo.ConstraintCode) + "'");
6144 return;
Chris Lattnerfcd70902012-01-03 23:51:01 +00006145 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006146
6147 // If this is an indirect operand, store through the pointer after the
6148 // asm.
6149 if (OpInfo.isIndirect) {
6150 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
6151 OpInfo.CallOperandVal));
6152 } else {
6153 // This is the result value of the call.
Benjamin Kramerf0127052010-01-05 13:12:22 +00006154 assert(!CS.getType()->isVoidTy() && "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006155 // Concatenate this output onto the outputs list.
6156 RetValRegs.append(OpInfo.AssignedRegs);
6157 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006158
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006159 // Add information to the INLINEASM node to know that this register is
6160 // set.
Eric Christopherb0bee812013-07-30 22:50:44 +00006161 OpInfo.AssignedRegs
6162 .AddInlineAsmOperands(OpInfo.isEarlyClobber
6163 ? InlineAsm::Kind_RegDefEarlyClobber
6164 : InlineAsm::Kind_RegDef,
6165 false, 0, DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006166 break;
6167 }
6168 case InlineAsm::isInput: {
6169 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006170
Chris Lattner6bdcda32008-10-17 16:47:46 +00006171 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006172 // If this is required to match an output register we have already set,
6173 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00006174 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006175
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006176 // Scan until we find the definition we already emitted of this operand.
6177 // When we find it, create a RegsForValue operand.
Chris Lattnerdecc2672010-04-07 05:20:54 +00006178 unsigned CurOp = InlineAsm::Op_FirstOperand;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006179 for (; OperandNo; --OperandNo) {
6180 // Advance to the next operand.
Evan Cheng697cbbf2009-03-20 18:03:34 +00006181 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006182 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00006183 assert((InlineAsm::isRegDefKind(OpFlag) ||
6184 InlineAsm::isRegDefEarlyClobberKind(OpFlag) ||
6185 InlineAsm::isMemKind(OpFlag)) && "Skipped past definitions?");
Evan Cheng697cbbf2009-03-20 18:03:34 +00006186 CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006187 }
6188
Evan Cheng697cbbf2009-03-20 18:03:34 +00006189 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006190 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00006191 if (InlineAsm::isRegDefKind(OpFlag) ||
6192 InlineAsm::isRegDefEarlyClobberKind(OpFlag)) {
Evan Cheng697cbbf2009-03-20 18:03:34 +00006193 // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
Chris Lattner6129c372010-04-08 00:09:16 +00006194 if (OpInfo.isIndirect) {
6195 // This happens on gcc/testsuite/gcc.dg/pr8788-1.c
Dan Gohman99be8ae2010-04-19 22:41:47 +00006196 LLVMContext &Ctx = *DAG.getContext();
Eric Christopher1a54c572013-07-31 01:26:24 +00006197 Ctx.emitError(CS.getInstruction(), "inline asm not supported yet:"
6198 " don't know how to handle tied "
6199 "indirect register inputs");
6200 return;
Chris Lattner6129c372010-04-08 00:09:16 +00006201 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00006202
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006203 RegsForValue MatchedRegs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006204 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00006205 MVT RegVT = AsmNodeOperands[CurOp+1].getSimpleValueType();
Evan Chengfb112882009-03-23 08:01:15 +00006206 MatchedRegs.RegVTs.push_back(RegVT);
6207 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
Evan Cheng697cbbf2009-03-20 18:03:34 +00006208 for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag);
Chad Rosier2871ba92013-04-24 22:53:10 +00006209 i != e; ++i) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00006210 if (const TargetRegisterClass *RC = TLI->getRegClassFor(RegVT))
Chad Rosier2871ba92013-04-24 22:53:10 +00006211 MatchedRegs.Regs.push_back(RegInfo.createVirtualRegister(RC));
6212 else {
6213 LLVMContext &Ctx = *DAG.getContext();
Eric Christopher1a54c572013-07-31 01:26:24 +00006214 Ctx.emitError(CS.getInstruction(),
6215 "inline asm error: This value"
Chad Rosier2871ba92013-04-24 22:53:10 +00006216 " type register class is not natively supported!");
Eric Christopher1a54c572013-07-31 01:26:24 +00006217 return;
Chad Rosier2871ba92013-04-24 22:53:10 +00006218 }
6219 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006220 // Use the produced MatchedRegs object to
Andrew Trickac6d9be2013-05-25 02:42:55 +00006221 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurSDLoc(),
Bill Wendlingf18eb582012-09-26 06:16:18 +00006222 Chain, &Flag, CS.getInstruction());
Chris Lattnerdecc2672010-04-07 05:20:54 +00006223 MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse,
Evan Chengfb112882009-03-23 08:01:15 +00006224 true, OpInfo.getMatchedOperand(),
Bill Wendling46ada192010-03-02 01:55:18 +00006225 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006226 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006227 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00006228
Chris Lattnerdecc2672010-04-07 05:20:54 +00006229 assert(InlineAsm::isMemKind(OpFlag) && "Unknown matching constraint!");
6230 assert(InlineAsm::getNumOperandRegisters(OpFlag) == 1 &&
6231 "Unexpected number of operands");
6232 // Add information to the INLINEASM node to know about this input.
6233 // See InlineAsm.h isUseOperandTiedToDef.
6234 OpFlag = InlineAsm::getFlagWordForMatchingOp(OpFlag,
6235 OpInfo.getMatchedOperand());
6236 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag,
Bill Wendlingba54bca2013-06-19 21:36:55 +00006237 TLI->getPointerTy()));
Chris Lattnerdecc2672010-04-07 05:20:54 +00006238 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
6239 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006240 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006241
Dale Johannesenb5611a62010-07-13 20:17:05 +00006242 // Treat indirect 'X' constraint as memory.
Michael J. Spencere70c5262010-10-16 08:25:21 +00006243 if (OpInfo.ConstraintType == TargetLowering::C_Other &&
6244 OpInfo.isIndirect)
Dale Johannesenb5611a62010-07-13 20:17:05 +00006245 OpInfo.ConstraintType = TargetLowering::C_Memory;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006246
Dale Johannesenb5611a62010-07-13 20:17:05 +00006247 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006248 std::vector<SDValue> Ops;
Bill Wendlingba54bca2013-06-19 21:36:55 +00006249 TLI->LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode,
6250 Ops, DAG);
Chris Lattnerfcd70902012-01-03 23:51:01 +00006251 if (Ops.empty()) {
6252 LLVMContext &Ctx = *DAG.getContext();
6253 Ctx.emitError(CS.getInstruction(),
6254 "invalid operand for inline asm constraint '" +
Eric Christopher1a54c572013-07-31 01:26:24 +00006255 Twine(OpInfo.ConstraintCode) + "'");
6256 return;
Chris Lattnerfcd70902012-01-03 23:51:01 +00006257 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006258
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006259 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00006260 unsigned ResOpType =
6261 InlineAsm::getFlagWord(InlineAsm::Kind_Imm, Ops.size());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006262 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Bill Wendlingba54bca2013-06-19 21:36:55 +00006263 TLI->getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006264 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
6265 break;
Chris Lattnerdecc2672010-04-07 05:20:54 +00006266 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00006267
Chris Lattnerdecc2672010-04-07 05:20:54 +00006268 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006269 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
Bill Wendlingba54bca2013-06-19 21:36:55 +00006270 assert(InOperandVal.getValueType() == TLI->getPointerTy() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006271 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006272
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006273 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00006274 unsigned ResOpType = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
Dale Johannesen86b49f82008-09-24 01:07:17 +00006275 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Bill Wendlingba54bca2013-06-19 21:36:55 +00006276 TLI->getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006277 AsmNodeOperands.push_back(InOperandVal);
6278 break;
6279 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006280
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006281 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
6282 OpInfo.ConstraintType == TargetLowering::C_Register) &&
6283 "Unknown constraint type!");
Eric Christopher9eb4f8a2012-07-02 21:16:43 +00006284
6285 // TODO: Support this.
6286 if (OpInfo.isIndirect) {
6287 LLVMContext &Ctx = *DAG.getContext();
6288 Ctx.emitError(CS.getInstruction(),
6289 "Don't know how to handle indirect register inputs yet "
Eric Christopher1a54c572013-07-31 01:26:24 +00006290 "for constraint '" +
6291 Twine(OpInfo.ConstraintCode) + "'");
6292 return;
Eric Christopher9eb4f8a2012-07-02 21:16:43 +00006293 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006294
6295 // Copy the input into the appropriate registers.
Chris Lattnerfcd70902012-01-03 23:51:01 +00006296 if (OpInfo.AssignedRegs.Regs.empty()) {
6297 LLVMContext &Ctx = *DAG.getContext();
Stephen Lin155615d2013-07-08 00:37:03 +00006298 Ctx.emitError(CS.getInstruction(),
Chris Lattnerfcd70902012-01-03 23:51:01 +00006299 "couldn't allocate input reg for constraint '" +
Eric Christopher1a54c572013-07-31 01:26:24 +00006300 Twine(OpInfo.ConstraintCode) + "'");
6301 return;
Chris Lattnerfcd70902012-01-03 23:51:01 +00006302 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006303
Andrew Trickac6d9be2013-05-25 02:42:55 +00006304 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurSDLoc(),
Bill Wendlingf18eb582012-09-26 06:16:18 +00006305 Chain, &Flag, CS.getInstruction());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006306
Chris Lattnerdecc2672010-04-07 05:20:54 +00006307 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse, false, 0,
Bill Wendling46ada192010-03-02 01:55:18 +00006308 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006309 break;
6310 }
6311 case InlineAsm::isClobber: {
6312 // Add the clobbered value to the operand list, so that the register
6313 // allocator is aware that the physreg got clobbered.
6314 if (!OpInfo.AssignedRegs.Regs.empty())
Jakob Stoklund Olesenf792fa92011-06-27 04:08:33 +00006315 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_Clobber,
Bill Wendling46ada192010-03-02 01:55:18 +00006316 false, 0, DAG,
Bill Wendling651ad132009-12-22 01:25:10 +00006317 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006318 break;
6319 }
6320 }
6321 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006322
Chris Lattnerdecc2672010-04-07 05:20:54 +00006323 // Finish up input operands. Set the input chain and add the flag last.
Dale Johannesenf1e309e2010-07-02 20:16:09 +00006324 AsmNodeOperands[InlineAsm::Op_InputChain] = Chain;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006325 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006326
Andrew Trickac6d9be2013-05-25 02:42:55 +00006327 Chain = DAG.getNode(ISD::INLINEASM, getCurSDLoc(),
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006328 DAG.getVTList(MVT::Other, MVT::Glue),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006329 &AsmNodeOperands[0], AsmNodeOperands.size());
6330 Flag = Chain.getValue(1);
6331
6332 // If this asm returns a register value, copy the result from that register
6333 // and set it as the value of the call.
6334 if (!RetValRegs.Regs.empty()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006335 SDValue Val = RetValRegs.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(),
Bill Wendling12931302012-09-26 04:04:19 +00006336 Chain, &Flag, CS.getInstruction());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006337
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006338 // FIXME: Why don't we do this for inline asms with MRVs?
6339 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00006340 EVT ResultType = TLI->getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006341
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006342 // If any of the results of the inline asm is a vector, it may have the
6343 // wrong width/num elts. This can happen for register classes that can
6344 // contain multiple different value types. The preg or vreg allocated may
6345 // not have the same VT as was expected. Convert it to the right type
6346 // with bit_convert.
6347 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006348 Val = DAG.getNode(ISD::BITCAST, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00006349 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00006350
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006351 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006352 ResultType.isInteger() && Val.getValueType().isInteger()) {
6353 // If a result value was tied to an input value, the computed result may
6354 // have a wider width than the expected result. Extract the relevant
6355 // portion.
Andrew Trickac6d9be2013-05-25 02:42:55 +00006356 Val = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00006357 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006358
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006359 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Chris Lattner0c526442008-10-17 17:52:49 +00006360 }
Dan Gohman95915732008-10-18 01:03:45 +00006361
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006362 setValue(CS.getInstruction(), Val);
Dale Johannesenec65a7d2009-04-14 00:56:56 +00006363 // Don't need to use this as a chain in this case.
6364 if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty())
6365 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006366 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006367
Dan Gohman46510a72010-04-15 01:51:59 +00006368 std::vector<std::pair<SDValue, const Value *> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006369
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006370 // Process indirect outputs, first output all of the flagged copies out of
6371 // physregs.
6372 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
6373 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
Dan Gohman46510a72010-04-15 01:51:59 +00006374 const Value *Ptr = IndirectStoresToEmit[i].second;
Andrew Trickac6d9be2013-05-25 02:42:55 +00006375 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(),
Bill Wendling12931302012-09-26 04:04:19 +00006376 Chain, &Flag, IA);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006377 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
6378 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006379
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006380 // Emit the non-flagged stores from the physregs.
6381 SmallVector<SDValue, 8> OutChains;
Bill Wendling651ad132009-12-22 01:25:10 +00006382 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006383 SDValue Val = DAG.getStore(Chain, getCurSDLoc(),
Bill Wendling651ad132009-12-22 01:25:10 +00006384 StoresToEmit[i].first,
6385 getValue(StoresToEmit[i].second),
Chris Lattner84bd98a2010-09-21 18:58:22 +00006386 MachinePointerInfo(StoresToEmit[i].second),
David Greene1e559442010-02-15 17:00:31 +00006387 false, false, 0);
Bill Wendling651ad132009-12-22 01:25:10 +00006388 OutChains.push_back(Val);
Bill Wendling651ad132009-12-22 01:25:10 +00006389 }
6390
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006391 if (!OutChains.empty())
Andrew Trickac6d9be2013-05-25 02:42:55 +00006392 Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006393 &OutChains[0], OutChains.size());
Bill Wendling651ad132009-12-22 01:25:10 +00006394
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006395 DAG.setRoot(Chain);
6396}
6397
Dan Gohman46510a72010-04-15 01:51:59 +00006398void SelectionDAGBuilder::visitVAStart(const CallInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006399 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurSDLoc(),
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006400 MVT::Other, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00006401 getValue(I.getArgOperand(0)),
6402 DAG.getSrcValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006403}
6404
Dan Gohman46510a72010-04-15 01:51:59 +00006405void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00006406 const TargetLowering *TLI = TM.getTargetLowering();
6407 const DataLayout &TD = *TLI->getDataLayout();
6408 SDValue V = DAG.getVAArg(TLI->getValueType(I.getType()), getCurSDLoc(),
Dale Johannesena04b7572009-02-03 23:04:43 +00006409 getRoot(), getValue(I.getOperand(0)),
Rafael Espindolacbeeae22010-07-11 04:01:49 +00006410 DAG.getSrcValue(I.getOperand(0)),
Rafael Espindola9d544d02010-07-12 18:11:17 +00006411 TD.getABITypeAlignment(I.getType()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006412 setValue(&I, V);
6413 DAG.setRoot(V.getValue(1));
6414}
6415
Dan Gohman46510a72010-04-15 01:51:59 +00006416void SelectionDAGBuilder::visitVAEnd(const CallInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006417 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurSDLoc(),
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006418 MVT::Other, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00006419 getValue(I.getArgOperand(0)),
6420 DAG.getSrcValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006421}
6422
Dan Gohman46510a72010-04-15 01:51:59 +00006423void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006424 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurSDLoc(),
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006425 MVT::Other, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00006426 getValue(I.getArgOperand(0)),
6427 getValue(I.getArgOperand(1)),
6428 DAG.getSrcValue(I.getArgOperand(0)),
6429 DAG.getSrcValue(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006430}
6431
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006432/// TargetLowering::LowerCallTo - This is the default LowerCallTo
Dan Gohman98ca4f22009-08-05 01:29:28 +00006433/// implementation, which just calls LowerCall.
6434/// FIXME: When all targets are
6435/// migrated to using LowerCall, this hook should be integrated into SDISel.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006436std::pair<SDValue, SDValue>
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006437TargetLowering::LowerCallTo(TargetLowering::CallLoweringInfo &CLI) const {
Stephen Lin3484da92013-04-30 22:49:28 +00006438 // Handle the incoming return values from the call.
6439 CLI.Ins.clear();
6440 SmallVector<EVT, 4> RetTys;
6441 ComputeValueVTs(*this, CLI.RetTy, RetTys);
6442 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
6443 EVT VT = RetTys[I];
6444 MVT RegisterVT = getRegisterType(CLI.RetTy->getContext(), VT);
6445 unsigned NumRegs = getNumRegisters(CLI.RetTy->getContext(), VT);
6446 for (unsigned i = 0; i != NumRegs; ++i) {
6447 ISD::InputArg MyFlags;
6448 MyFlags.VT = RegisterVT;
6449 MyFlags.Used = CLI.IsReturnValueUsed;
6450 if (CLI.RetSExt)
6451 MyFlags.Flags.setSExt();
6452 if (CLI.RetZExt)
6453 MyFlags.Flags.setZExt();
6454 if (CLI.IsInReg)
6455 MyFlags.Flags.setInReg();
6456 CLI.Ins.push_back(MyFlags);
6457 }
6458 }
6459
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006460 // Handle all of the outgoing arguments.
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006461 CLI.Outs.clear();
6462 CLI.OutVals.clear();
6463 ArgListTy &Args = CLI.Args;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006464 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00006465 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006466 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
6467 for (unsigned Value = 0, NumValues = ValueVTs.size();
6468 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006469 EVT VT = ValueVTs[Value];
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006470 Type *ArgTy = VT.getTypeForEVT(CLI.RetTy->getContext());
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006471 SDValue Op = SDValue(Args[i].Node.getNode(),
6472 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006473 ISD::ArgFlagsTy Flags;
6474 unsigned OriginalAlignment =
Micah Villmow3574eca2012-10-08 16:38:25 +00006475 getDataLayout()->getABITypeAlignment(ArgTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006476
6477 if (Args[i].isZExt)
6478 Flags.setZExt();
6479 if (Args[i].isSExt)
6480 Flags.setSExt();
6481 if (Args[i].isInReg)
6482 Flags.setInReg();
6483 if (Args[i].isSRet)
6484 Flags.setSRet();
6485 if (Args[i].isByVal) {
6486 Flags.setByVal();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006487 PointerType *Ty = cast<PointerType>(Args[i].Ty);
6488 Type *ElementTy = Ty->getElementType();
Micah Villmow3574eca2012-10-08 16:38:25 +00006489 Flags.setByValSize(getDataLayout()->getTypeAllocSize(ElementTy));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006490 // For ByVal, alignment should come from FE. BE will guess if this
6491 // info is not there but there are cases it cannot get right.
Chris Lattner9db20f32011-05-22 23:23:02 +00006492 unsigned FrameAlign;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006493 if (Args[i].Alignment)
6494 FrameAlign = Args[i].Alignment;
Chris Lattner9db20f32011-05-22 23:23:02 +00006495 else
6496 FrameAlign = getByValTypeAlignment(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006497 Flags.setByValAlign(FrameAlign);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006498 }
6499 if (Args[i].isNest)
6500 Flags.setNest();
6501 Flags.setOrigAlign(OriginalAlignment);
6502
Patrik Hagglunddfcf33a2012-12-19 11:48:16 +00006503 MVT PartVT = getRegisterType(CLI.RetTy->getContext(), VT);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006504 unsigned NumParts = getNumRegisters(CLI.RetTy->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006505 SmallVector<SDValue, 4> Parts(NumParts);
6506 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
6507
6508 if (Args[i].isSExt)
6509 ExtendKind = ISD::SIGN_EXTEND;
6510 else if (Args[i].isZExt)
6511 ExtendKind = ISD::ZERO_EXTEND;
6512
Stephen Lin3484da92013-04-30 22:49:28 +00006513 // Conservatively only handle 'returned' on non-vectors for now
6514 if (Args[i].isReturned && !Op.getValueType().isVector()) {
6515 assert(CLI.RetTy == Args[i].Ty && RetTys.size() == NumValues &&
6516 "unexpected use of 'returned'");
6517 // Before passing 'returned' to the target lowering code, ensure that
6518 // either the register MVT and the actual EVT are the same size or that
6519 // the return value and argument are extended in the same way; in these
6520 // cases it's safe to pass the argument register value unchanged as the
6521 // return register value (although it's at the target's option whether
6522 // to do so)
6523 // TODO: allow code generation to take advantage of partially preserved
6524 // registers rather than clobbering the entire register when the
6525 // parameter extension method is not compatible with the return
6526 // extension method
6527 if ((NumParts * PartVT.getSizeInBits() == VT.getSizeInBits()) ||
6528 (ExtendKind != ISD::ANY_EXTEND &&
6529 CLI.RetSExt == Args[i].isSExt && CLI.RetZExt == Args[i].isZExt))
6530 Flags.setReturned();
6531 }
6532
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006533 getCopyToParts(CLI.DAG, CLI.DL, Op, &Parts[0], NumParts,
Bill Wendlingf18eb582012-09-26 06:16:18 +00006534 PartVT, CLI.CS ? CLI.CS->getInstruction() : 0, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006535
Dan Gohman98ca4f22009-08-05 01:29:28 +00006536 for (unsigned j = 0; j != NumParts; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006537 // if it isn't first piece, alignment must be 1
Dan Gohmanc9403652010-07-07 15:54:55 +00006538 ISD::OutputArg MyFlags(Flags, Parts[j].getValueType(),
Manman Ren0a1544d2012-11-01 23:49:58 +00006539 i < CLI.NumFixedArgs,
6540 i, j*Parts[j].getValueType().getStoreSize());
Dan Gohman98ca4f22009-08-05 01:29:28 +00006541 if (NumParts > 1 && j == 0)
6542 MyFlags.Flags.setSplit();
6543 else if (j != 0)
6544 MyFlags.Flags.setOrigAlign(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006545
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006546 CLI.Outs.push_back(MyFlags);
6547 CLI.OutVals.push_back(Parts[j]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006548 }
6549 }
6550 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006551
Dan Gohman98ca4f22009-08-05 01:29:28 +00006552 SmallVector<SDValue, 4> InVals;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006553 CLI.Chain = LowerCall(CLI, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00006554
6555 // Verify that the target's LowerCall behaved as expected.
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006556 assert(CLI.Chain.getNode() && CLI.Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00006557 "LowerCall didn't return a valid chain!");
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006558 assert((!CLI.IsTailCall || InVals.empty()) &&
Dan Gohman5e866062009-08-06 15:37:27 +00006559 "LowerCall emitted a return value for a tail call!");
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006560 assert((CLI.IsTailCall || InVals.size() == CLI.Ins.size()) &&
Dan Gohman5e866062009-08-06 15:37:27 +00006561 "LowerCall didn't emit the correct number of values!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00006562
6563 // For a tail call, the return value is merely live-out and there aren't
6564 // any nodes in the DAG representing it. Return a special value to
6565 // indicate that a tail call has been emitted and no more Instructions
6566 // should be processed in the current block.
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006567 if (CLI.IsTailCall) {
6568 CLI.DAG.setRoot(CLI.Chain);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006569 return std::make_pair(SDValue(), SDValue());
6570 }
6571
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006572 DEBUG(for (unsigned i = 0, e = CLI.Ins.size(); i != e; ++i) {
Evan Chengaf1871f2010-03-11 19:38:18 +00006573 assert(InVals[i].getNode() &&
6574 "LowerCall emitted a null value!");
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006575 assert(EVT(CLI.Ins[i].VT) == InVals[i].getValueType() &&
Evan Chengaf1871f2010-03-11 19:38:18 +00006576 "LowerCall emitted a value with the wrong type!");
6577 });
6578
Dan Gohman98ca4f22009-08-05 01:29:28 +00006579 // Collect the legal value parts into potentially illegal values
6580 // that correspond to the original function's return values.
6581 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006582 if (CLI.RetSExt)
Dan Gohman98ca4f22009-08-05 01:29:28 +00006583 AssertOp = ISD::AssertSext;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006584 else if (CLI.RetZExt)
Dan Gohman98ca4f22009-08-05 01:29:28 +00006585 AssertOp = ISD::AssertZext;
6586 SmallVector<SDValue, 4> ReturnValues;
6587 unsigned CurReg = 0;
6588 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00006589 EVT VT = RetTys[I];
Patrik Hagglunddfcf33a2012-12-19 11:48:16 +00006590 MVT RegisterVT = getRegisterType(CLI.RetTy->getContext(), VT);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006591 unsigned NumRegs = getNumRegisters(CLI.RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006592
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006593 ReturnValues.push_back(getCopyFromParts(CLI.DAG, CLI.DL, &InVals[CurReg],
Bill Wendling12931302012-09-26 04:04:19 +00006594 NumRegs, RegisterVT, VT, NULL,
Bill Wendling4533cac2010-01-28 21:51:40 +00006595 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00006596 CurReg += NumRegs;
6597 }
6598
6599 // For a function returning void, there is no return value. We can't create
6600 // such a node, so we just return a null return value in that case. In
Chris Lattner7a2bdde2011-04-15 05:18:47 +00006601 // that case, nothing will actually look at the value.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006602 if (ReturnValues.empty())
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006603 return std::make_pair(SDValue(), CLI.Chain);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006604
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006605 SDValue Res = CLI.DAG.getNode(ISD::MERGE_VALUES, CLI.DL,
6606 CLI.DAG.getVTList(&RetTys[0], RetTys.size()),
Dan Gohman98ca4f22009-08-05 01:29:28 +00006607 &ReturnValues[0], ReturnValues.size());
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006608 return std::make_pair(Res, CLI.Chain);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006609}
6610
Duncan Sands9fbc7e22009-01-21 09:00:29 +00006611void TargetLowering::LowerOperationWrapper(SDNode *N,
6612 SmallVectorImpl<SDValue> &Results,
Dan Gohmand858e902010-04-17 15:26:15 +00006613 SelectionDAG &DAG) const {
Duncan Sands9fbc7e22009-01-21 09:00:29 +00006614 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00006615 if (Res.getNode())
6616 Results.push_back(Res);
6617}
6618
Dan Gohmand858e902010-04-17 15:26:15 +00006619SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Torok Edwinc23197a2009-07-14 16:55:14 +00006620 llvm_unreachable("LowerOperation not implemented for this target!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006621}
6622
Dan Gohman46510a72010-04-15 01:51:59 +00006623void
6624SelectionDAGBuilder::CopyValueToVirtualRegister(const Value *V, unsigned Reg) {
Dan Gohman28a17352010-07-01 01:59:43 +00006625 SDValue Op = getNonRegisterValue(V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006626 assert((Op.getOpcode() != ISD::CopyFromReg ||
6627 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
6628 "Copy from a reg to the same reg!");
6629 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
6630
Bill Wendlingba54bca2013-06-19 21:36:55 +00006631 const TargetLowering *TLI = TM.getTargetLowering();
6632 RegsForValue RFV(V->getContext(), *TLI, Reg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006633 SDValue Chain = DAG.getEntryNode();
Andrew Trickac6d9be2013-05-25 02:42:55 +00006634 RFV.getCopyToRegs(Op, DAG, getCurSDLoc(), Chain, 0, V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006635 PendingExports.push_back(Chain);
6636}
6637
6638#include "llvm/CodeGen/SelectionDAGISel.h"
6639
Eli Friedman23d32432011-05-05 16:53:34 +00006640/// isOnlyUsedInEntryBlock - If the specified argument is only used in the
6641/// entry block, return true. This includes arguments used by switches, since
6642/// the switch may expand into multiple basic blocks.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006643static bool isOnlyUsedInEntryBlock(const Argument *A, bool FastISel) {
Eli Friedman23d32432011-05-05 16:53:34 +00006644 // With FastISel active, we may be splitting blocks, so force creation
6645 // of virtual registers for all non-dead arguments.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006646 if (FastISel)
Eli Friedman23d32432011-05-05 16:53:34 +00006647 return A->use_empty();
6648
6649 const BasicBlock *Entry = A->getParent()->begin();
6650 for (Value::const_use_iterator UI = A->use_begin(), E = A->use_end();
6651 UI != E; ++UI) {
6652 const User *U = *UI;
6653 if (cast<Instruction>(U)->getParent() != Entry || isa<SwitchInst>(U))
6654 return false; // Use not in entry block.
6655 }
6656 return true;
6657}
6658
Eli Bendersky6437d382013-02-28 23:09:18 +00006659void SelectionDAGISel::LowerArguments(const Function &F) {
Dan Gohman2048b852009-11-23 18:04:58 +00006660 SelectionDAG &DAG = SDB->DAG;
Andrew Trickac6d9be2013-05-25 02:42:55 +00006661 SDLoc dl = SDB->getCurSDLoc();
Bill Wendlingba54bca2013-06-19 21:36:55 +00006662 const TargetLowering *TLI = getTargetLowering();
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006663 const DataLayout *TD = TLI->getDataLayout();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006664 SmallVector<ISD::InputArg, 16> Ins;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006665
Dan Gohman7451d3e2010-05-29 17:03:36 +00006666 if (!FuncInfo->CanLowerReturn) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006667 // Put in an sret pointer parameter before all the other parameters.
6668 SmallVector<EVT, 1> ValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00006669 ComputeValueVTs(*getTargetLowering(),
6670 PointerType::getUnqual(F.getReturnType()), ValueVTs);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006671
6672 // NOTE: Assuming that a pointer will never break down to more than one VT
6673 // or one register.
6674 ISD::ArgFlagsTy Flags;
6675 Flags.setSRet();
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006676 MVT RegisterVT = TLI->getRegisterType(*DAG.getContext(), ValueVTs[0]);
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00006677 ISD::InputArg RetArg(Flags, RegisterVT, true, 0, 0);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006678 Ins.push_back(RetArg);
6679 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00006680
Dan Gohman98ca4f22009-08-05 01:29:28 +00006681 // Set up the incoming argument description vector.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006682 unsigned Idx = 1;
Dan Gohman46510a72010-04-15 01:51:59 +00006683 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
Dan Gohman98ca4f22009-08-05 01:29:28 +00006684 I != E; ++I, ++Idx) {
Owen Andersone50ed302009-08-10 22:56:29 +00006685 SmallVector<EVT, 4> ValueVTs;
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006686 ComputeValueVTs(*TLI, I->getType(), ValueVTs);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006687 bool isArgValueUsed = !I->use_empty();
6688 for (unsigned Value = 0, NumValues = ValueVTs.size();
6689 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006690 EVT VT = ValueVTs[Value];
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006691 Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00006692 ISD::ArgFlagsTy Flags;
6693 unsigned OriginalAlignment =
6694 TD->getABITypeAlignment(ArgTy);
6695
Bill Wendling39cd0c82012-12-30 12:45:13 +00006696 if (F.getAttributes().hasAttribute(Idx, Attribute::ZExt))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006697 Flags.setZExt();
Bill Wendling39cd0c82012-12-30 12:45:13 +00006698 if (F.getAttributes().hasAttribute(Idx, Attribute::SExt))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006699 Flags.setSExt();
Bill Wendling39cd0c82012-12-30 12:45:13 +00006700 if (F.getAttributes().hasAttribute(Idx, Attribute::InReg))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006701 Flags.setInReg();
Bill Wendling39cd0c82012-12-30 12:45:13 +00006702 if (F.getAttributes().hasAttribute(Idx, Attribute::StructRet))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006703 Flags.setSRet();
Bill Wendling39cd0c82012-12-30 12:45:13 +00006704 if (F.getAttributes().hasAttribute(Idx, Attribute::ByVal)) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00006705 Flags.setByVal();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006706 PointerType *Ty = cast<PointerType>(I->getType());
6707 Type *ElementTy = Ty->getElementType();
Chris Lattner9db20f32011-05-22 23:23:02 +00006708 Flags.setByValSize(TD->getTypeAllocSize(ElementTy));
Dan Gohman98ca4f22009-08-05 01:29:28 +00006709 // For ByVal, alignment should be passed from FE. BE will guess if
6710 // this info is not there but there are cases it cannot get right.
Chris Lattner9db20f32011-05-22 23:23:02 +00006711 unsigned FrameAlign;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006712 if (F.getParamAlignment(Idx))
6713 FrameAlign = F.getParamAlignment(Idx);
Chris Lattner9db20f32011-05-22 23:23:02 +00006714 else
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006715 FrameAlign = TLI->getByValTypeAlignment(ElementTy);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006716 Flags.setByValAlign(FrameAlign);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006717 }
Bill Wendling39cd0c82012-12-30 12:45:13 +00006718 if (F.getAttributes().hasAttribute(Idx, Attribute::Nest))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006719 Flags.setNest();
6720 Flags.setOrigAlign(OriginalAlignment);
6721
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006722 MVT RegisterVT = TLI->getRegisterType(*CurDAG->getContext(), VT);
6723 unsigned NumRegs = TLI->getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006724 for (unsigned i = 0; i != NumRegs; ++i) {
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00006725 ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed,
6726 Idx-1, i*RegisterVT.getStoreSize());
Dan Gohman98ca4f22009-08-05 01:29:28 +00006727 if (NumRegs > 1 && i == 0)
6728 MyFlags.Flags.setSplit();
6729 // if it isn't first piece, alignment must be 1
6730 else if (i > 0)
6731 MyFlags.Flags.setOrigAlign(1);
6732 Ins.push_back(MyFlags);
6733 }
6734 }
6735 }
6736
6737 // Call the target to set up the argument values.
6738 SmallVector<SDValue, 8> InVals;
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006739 SDValue NewRoot = TLI->LowerFormalArguments(DAG.getRoot(), F.getCallingConv(),
6740 F.isVarArg(), Ins,
6741 dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00006742
6743 // Verify that the target's LowerFormalArguments behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00006744 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00006745 "LowerFormalArguments didn't return a valid chain!");
6746 assert(InVals.size() == Ins.size() &&
6747 "LowerFormalArguments didn't emit the correct number of values!");
Bill Wendling3ea58b62009-12-22 21:35:02 +00006748 DEBUG({
6749 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
6750 assert(InVals[i].getNode() &&
6751 "LowerFormalArguments emitted a null value!");
Duncan Sands1440e8b2010-11-03 11:35:31 +00006752 assert(EVT(Ins[i].VT) == InVals[i].getValueType() &&
Bill Wendling3ea58b62009-12-22 21:35:02 +00006753 "LowerFormalArguments emitted a value with the wrong type!");
6754 }
6755 });
Bill Wendling3ea3c242009-12-22 02:10:19 +00006756
Dan Gohman5e866062009-08-06 15:37:27 +00006757 // Update the DAG with the new chain value resulting from argument lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006758 DAG.setRoot(NewRoot);
6759
6760 // Set up the argument values.
6761 unsigned i = 0;
6762 Idx = 1;
Dan Gohman7451d3e2010-05-29 17:03:36 +00006763 if (!FuncInfo->CanLowerReturn) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006764 // Create a virtual register for the sret pointer, and put in a copy
6765 // from the sret argument into it.
6766 SmallVector<EVT, 1> ValueVTs;
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006767 ComputeValueVTs(*TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00006768 MVT VT = ValueVTs[0].getSimpleVT();
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006769 MVT RegVT = TLI->getRegisterType(*CurDAG->getContext(), VT);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006770 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Bill Wendling46ada192010-03-02 01:55:18 +00006771 SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1,
Bill Wendling12931302012-09-26 04:04:19 +00006772 RegVT, VT, NULL, AssertOp);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006773
Dan Gohman2048b852009-11-23 18:04:58 +00006774 MachineFunction& MF = SDB->DAG.getMachineFunction();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006775 MachineRegisterInfo& RegInfo = MF.getRegInfo();
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006776 unsigned SRetReg = RegInfo.createVirtualRegister(TLI->getRegClassFor(RegVT));
Dan Gohman7451d3e2010-05-29 17:03:36 +00006777 FuncInfo->DemoteRegister = SRetReg;
Andrew Trickac6d9be2013-05-25 02:42:55 +00006778 NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurSDLoc(),
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00006779 SRetReg, ArgValue);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006780 DAG.setRoot(NewRoot);
Bill Wendling3ea3c242009-12-22 02:10:19 +00006781
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006782 // i indexes lowered arguments. Bump it past the hidden sret argument.
6783 // Idx indexes LLVM arguments. Don't touch it.
6784 ++i;
6785 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006786
Dan Gohman46510a72010-04-15 01:51:59 +00006787 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006788 ++I, ++Idx) {
6789 SmallVector<SDValue, 4> ArgValues;
Owen Andersone50ed302009-08-10 22:56:29 +00006790 SmallVector<EVT, 4> ValueVTs;
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006791 ComputeValueVTs(*TLI, I->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006792 unsigned NumValues = ValueVTs.size();
Devang Patel9126c0d2010-06-01 19:59:01 +00006793
6794 // If this argument is unused then remember its value. It is used to generate
6795 // debugging information.
Adrian Prantldf688032013-05-16 23:44:12 +00006796 if (I->use_empty() && NumValues) {
Devang Patel9126c0d2010-06-01 19:59:01 +00006797 SDB->setUnusedArgValue(I, InVals[i]);
6798
Adrian Prantldf688032013-05-16 23:44:12 +00006799 // Also remember any frame index for use in FastISel.
6800 if (FrameIndexSDNode *FI =
6801 dyn_cast<FrameIndexSDNode>(InVals[i].getNode()))
6802 FuncInfo->setArgumentFrameIndex(I, FI->getIndex());
6803 }
6804
Eli Friedman23d32432011-05-05 16:53:34 +00006805 for (unsigned Val = 0; Val != NumValues; ++Val) {
6806 EVT VT = ValueVTs[Val];
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006807 MVT PartVT = TLI->getRegisterType(*CurDAG->getContext(), VT);
6808 unsigned NumParts = TLI->getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006809
6810 if (!I->use_empty()) {
6811 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Bill Wendling39cd0c82012-12-30 12:45:13 +00006812 if (F.getAttributes().hasAttribute(Idx, Attribute::SExt))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006813 AssertOp = ISD::AssertSext;
Bill Wendling39cd0c82012-12-30 12:45:13 +00006814 else if (F.getAttributes().hasAttribute(Idx, Attribute::ZExt))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006815 AssertOp = ISD::AssertZext;
6816
Bill Wendling46ada192010-03-02 01:55:18 +00006817 ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i],
Bill Wendling3ea3c242009-12-22 02:10:19 +00006818 NumParts, PartVT, VT,
Bill Wendling12931302012-09-26 04:04:19 +00006819 NULL, AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00006820 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006821
Dan Gohman98ca4f22009-08-05 01:29:28 +00006822 i += NumParts;
6823 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006824
Eli Friedman23d32432011-05-05 16:53:34 +00006825 // We don't need to do anything else for unused arguments.
6826 if (ArgValues.empty())
6827 continue;
6828
Devang Patel9aee3352011-09-08 22:59:09 +00006829 // Note down frame index.
6830 if (FrameIndexSDNode *FI =
Bill Wendling96cb1122012-07-19 00:04:14 +00006831 dyn_cast<FrameIndexSDNode>(ArgValues[0].getNode()))
Devang Patel9aee3352011-09-08 22:59:09 +00006832 FuncInfo->setArgumentFrameIndex(I, FI->getIndex());
Devang Patel0b48ead2010-08-31 22:22:42 +00006833
Eli Friedman23d32432011-05-05 16:53:34 +00006834 SDValue Res = DAG.getMergeValues(&ArgValues[0], NumValues,
Andrew Trickac6d9be2013-05-25 02:42:55 +00006835 SDB->getCurSDLoc());
Devang Patel9aee3352011-09-08 22:59:09 +00006836
Eli Friedman23d32432011-05-05 16:53:34 +00006837 SDB->setValue(I, Res);
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006838 if (!TM.Options.EnableFastISel && Res.getOpcode() == ISD::BUILD_PAIR) {
Stephen Lin155615d2013-07-08 00:37:03 +00006839 if (LoadSDNode *LNode =
Devang Patel9aee3352011-09-08 22:59:09 +00006840 dyn_cast<LoadSDNode>(Res.getOperand(0).getNode()))
6841 if (FrameIndexSDNode *FI =
6842 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
6843 FuncInfo->setArgumentFrameIndex(I, FI->getIndex());
6844 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006845
Eli Friedman23d32432011-05-05 16:53:34 +00006846 // If this argument is live outside of the entry block, insert a copy from
6847 // wherever we got it to the vreg that other BB's will reference it as.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006848 if (!TM.Options.EnableFastISel && Res.getOpcode() == ISD::CopyFromReg) {
Eli Friedman23d32432011-05-05 16:53:34 +00006849 // If we can, though, try to skip creating an unnecessary vreg.
6850 // FIXME: This isn't very clean... it would be nice to make this more
Eli Friedman7f33d672011-05-10 21:50:58 +00006851 // general. It's also subtly incompatible with the hacks FastISel
6852 // uses with vregs.
Eli Friedman23d32432011-05-05 16:53:34 +00006853 unsigned Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg();
6854 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
6855 FuncInfo->ValueMap[I] = Reg;
6856 continue;
6857 }
6858 }
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006859 if (!isOnlyUsedInEntryBlock(I, TM.Options.EnableFastISel)) {
Eli Friedman23d32432011-05-05 16:53:34 +00006860 FuncInfo->InitializeRegForValue(I);
Dan Gohman2048b852009-11-23 18:04:58 +00006861 SDB->CopyToExportRegsIfNeeded(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006862 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006863 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006864
Dan Gohman98ca4f22009-08-05 01:29:28 +00006865 assert(i == InVals.size() && "Argument register count mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006866
6867 // Finally, if the target has anything special to do, allow it to do so.
6868 // FIXME: this should insert code into the DAG!
Dan Gohman64652652010-04-14 20:17:22 +00006869 EmitFunctionEntryCode();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006870}
6871
6872/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
6873/// ensure constants are generated when needed. Remember the virtual registers
6874/// that need to be added to the Machine PHI nodes as input. We cannot just
6875/// directly add them, because expansion might result in multiple MBB's for one
6876/// BB. As such, the start of the BB might correspond to a different MBB than
6877/// the end.
6878///
6879void
Dan Gohmanf81eca02010-04-22 20:46:50 +00006880SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
Dan Gohman46510a72010-04-15 01:51:59 +00006881 const TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006882
6883 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
6884
6885 // Check successor nodes' PHI nodes that expect a constant to be available
6886 // from this block.
6887 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
Dan Gohman46510a72010-04-15 01:51:59 +00006888 const BasicBlock *SuccBB = TI->getSuccessor(succ);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006889 if (!isa<PHINode>(SuccBB->begin())) continue;
Dan Gohmanf81eca02010-04-22 20:46:50 +00006890 MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006891
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006892 // If this terminator has multiple identical successors (common for
6893 // switches), only handle each succ once.
6894 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006895
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006896 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006897
6898 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6899 // nodes and Machine PHI nodes, but the incoming operands have not been
6900 // emitted yet.
Dan Gohman46510a72010-04-15 01:51:59 +00006901 for (BasicBlock::const_iterator I = SuccBB->begin();
6902 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006903 // Ignore dead phi's.
6904 if (PN->use_empty()) continue;
6905
Rafael Espindola3fa82832011-05-13 15:18:06 +00006906 // Skip empty types
6907 if (PN->getType()->isEmptyTy())
6908 continue;
6909
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006910 unsigned Reg;
Dan Gohman46510a72010-04-15 01:51:59 +00006911 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006912
Dan Gohman46510a72010-04-15 01:51:59 +00006913 if (const Constant *C = dyn_cast<Constant>(PHIOp)) {
Dan Gohmanf81eca02010-04-22 20:46:50 +00006914 unsigned &RegOut = ConstantsOut[C];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006915 if (RegOut == 0) {
Dan Gohman89496d02010-07-02 00:10:16 +00006916 RegOut = FuncInfo.CreateRegs(C->getType());
Dan Gohmanf81eca02010-04-22 20:46:50 +00006917 CopyValueToVirtualRegister(C, RegOut);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006918 }
6919 Reg = RegOut;
6920 } else {
Dan Gohmanc25ad632010-07-01 01:33:21 +00006921 DenseMap<const Value *, unsigned>::iterator I =
6922 FuncInfo.ValueMap.find(PHIOp);
6923 if (I != FuncInfo.ValueMap.end())
6924 Reg = I->second;
6925 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006926 assert(isa<AllocaInst>(PHIOp) &&
Dan Gohmanf81eca02010-04-22 20:46:50 +00006927 FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006928 "Didn't codegen value into a register!??");
Dan Gohman89496d02010-07-02 00:10:16 +00006929 Reg = FuncInfo.CreateRegs(PHIOp->getType());
Dan Gohmanf81eca02010-04-22 20:46:50 +00006930 CopyValueToVirtualRegister(PHIOp, Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006931 }
6932 }
6933
6934 // Remember that this register needs to added to the machine PHI node as
6935 // the input for this MBB.
Owen Andersone50ed302009-08-10 22:56:29 +00006936 SmallVector<EVT, 4> ValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00006937 const TargetLowering *TLI = TM.getTargetLowering();
6938 ComputeValueVTs(*TLI, PN->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006939 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +00006940 EVT VT = ValueVTs[vti];
Bill Wendlingba54bca2013-06-19 21:36:55 +00006941 unsigned NumRegisters = TLI->getNumRegisters(*DAG.getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006942 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
Dan Gohmanf81eca02010-04-22 20:46:50 +00006943 FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006944 Reg += NumRegisters;
6945 }
6946 }
6947 }
Bill Wendlingba54bca2013-06-19 21:36:55 +00006948
Dan Gohmanf81eca02010-04-22 20:46:50 +00006949 ConstantsOut.clear();
Dan Gohman3df24e62008-09-03 23:12:08 +00006950}