blob: 21148ae1211ef917a9ca03c04d20d2209802fd0b [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"
Dan Gohman5b229802008-09-04 20:49:27 +000018#include "llvm/ADT/SmallSet.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000019#include "llvm/Analysis/AliasAnalysis.h"
Jakub Staszak81bfd712013-01-10 22:13:13 +000020#include "llvm/Analysis/BranchProbabilityInfo.h"
Chris Lattner8047d9a2009-12-24 00:37:38 +000021#include "llvm/Analysis/ConstantFolding.h"
Nadav Rotemc05d3062012-09-06 09:17:37 +000022#include "llvm/Analysis/ValueTracking.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000023#include "llvm/CodeGen/Analysis.h"
24#include "llvm/CodeGen/FastISel.h"
25#include "llvm/CodeGen/FunctionLoweringInfo.h"
26#include "llvm/CodeGen/GCMetadata.h"
27#include "llvm/CodeGen/GCStrategy.h"
28#include "llvm/CodeGen/MachineFrameInfo.h"
29#include "llvm/CodeGen/MachineFunction.h"
30#include "llvm/CodeGen/MachineInstrBuilder.h"
31#include "llvm/CodeGen/MachineJumpTableInfo.h"
32#include "llvm/CodeGen/MachineModuleInfo.h"
33#include "llvm/CodeGen/MachineRegisterInfo.h"
34#include "llvm/CodeGen/SelectionDAG.h"
Bill Wendling0bcbd1d2012-06-28 00:05:13 +000035#include "llvm/DebugInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000036#include "llvm/IR/CallingConv.h"
37#include "llvm/IR/Constants.h"
38#include "llvm/IR/DataLayout.h"
39#include "llvm/IR/DerivedTypes.h"
40#include "llvm/IR/Function.h"
41#include "llvm/IR/GlobalVariable.h"
42#include "llvm/IR/InlineAsm.h"
43#include "llvm/IR/Instructions.h"
44#include "llvm/IR/IntrinsicInst.h"
45#include "llvm/IR/Intrinsics.h"
46#include "llvm/IR/LLVMContext.h"
47#include "llvm/IR/Module.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000048#include "llvm/Support/CommandLine.h"
49#include "llvm/Support/Debug.h"
50#include "llvm/Support/ErrorHandling.h"
51#include "llvm/Support/IntegersSubsetMapping.h"
52#include "llvm/Support/MathExtras.h"
53#include "llvm/Support/raw_ostream.h"
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000054#include "llvm/Target/TargetFrameLowering.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000055#include "llvm/Target/TargetInstrInfo.h"
Dale Johannesen49de9822009-02-05 01:49:45 +000056#include "llvm/Target/TargetIntrinsicInfo.h"
Owen Anderson243eb9e2011-12-08 22:15:21 +000057#include "llvm/Target/TargetLibraryInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000058#include "llvm/Target/TargetLowering.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000059#include "llvm/Target/TargetOptions.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000060#include <algorithm>
61using namespace llvm;
62
Dale Johannesen601d3c02008-09-05 01:48:15 +000063/// LimitFloatPrecision - Generate low-precision inline sequences for
64/// some float libcalls (6, 8 or 12 bits).
65static unsigned LimitFloatPrecision;
66
67static cl::opt<unsigned, true>
68LimitFPPrecision("limit-float-precision",
69 cl::desc("Generate low-precision inline sequences "
70 "for some float libcalls"),
71 cl::location(LimitFloatPrecision),
72 cl::init(0));
73
Andrew Trickde91f3c2010-11-12 17:50:46 +000074// Limit the width of DAG chains. This is important in general to prevent
75// prevent DAG-based analysis from blowing up. For example, alias analysis and
76// load clustering may not complete in reasonable time. It is difficult to
77// recognize and avoid this situation within each individual analysis, and
78// future analyses are likely to have the same behavior. Limiting DAG width is
Andrew Trickb9e6fe12010-11-20 07:26:51 +000079// the safe approach, and will be especially important with global DAGs.
Andrew Trickde91f3c2010-11-12 17:50:46 +000080//
81// MaxParallelChains default is arbitrarily high to avoid affecting
82// optimization, but could be lowered to improve compile time. Any ld-ld-st-st
Andrew Trickb9e6fe12010-11-20 07:26:51 +000083// sequence over this should have been converted to llvm.memcpy by the
84// frontend. It easy to induce this behavior with .ll code such as:
85// %buffer = alloca [4096 x i8]
86// %data = load [4096 x i8]* %argPtr
87// store [4096 x i8] %data, [4096 x i8]* %buffer
Andrew Trick778583a2011-03-11 17:46:59 +000088static const unsigned MaxParallelChains = 64;
Andrew Trickde91f3c2010-11-12 17:50:46 +000089
Andrew Trickac6d9be2013-05-25 02:42:55 +000090static SDValue getCopyFromPartsVector(SelectionDAG &DAG, SDLoc DL,
Chris Lattner3ac18842010-08-24 23:20:40 +000091 const SDValue *Parts, unsigned NumParts,
Patrik Hagglundb9e12e52012-12-19 12:33:30 +000092 MVT PartVT, EVT ValueVT, const Value *V);
Michael J. Spencere70c5262010-10-16 08:25:21 +000093
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000094/// getCopyFromParts - Create a value that contains the specified legal parts
95/// combined into the value they represent. If the parts combine to a type
96/// larger then ValueVT then AssertOp can be used to specify whether the extra
97/// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT
98/// (ISD::AssertSext).
Andrew Trickac6d9be2013-05-25 02:42:55 +000099static SDValue getCopyFromParts(SelectionDAG &DAG, SDLoc DL,
Dale Johannesen66978ee2009-01-31 02:22:37 +0000100 const SDValue *Parts,
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000101 unsigned NumParts, MVT PartVT, EVT ValueVT,
Bill Wendling12931302012-09-26 04:04:19 +0000102 const Value *V,
Duncan Sands0b3aa262009-01-28 14:42:54 +0000103 ISD::NodeType AssertOp = ISD::DELETED_NODE) {
Chris Lattner3ac18842010-08-24 23:20:40 +0000104 if (ValueVT.isVector())
Bill Wendling12931302012-09-26 04:04:19 +0000105 return getCopyFromPartsVector(DAG, DL, Parts, NumParts,
106 PartVT, ValueVT, V);
Michael J. Spencere70c5262010-10-16 08:25:21 +0000107
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000108 assert(NumParts > 0 && "No parts to assemble!");
Dan Gohmane9530ec2009-01-15 16:58:17 +0000109 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000110 SDValue Val = Parts[0];
111
112 if (NumParts > 1) {
113 // Assemble the value from multiple parts.
Chris Lattner3ac18842010-08-24 23:20:40 +0000114 if (ValueVT.isInteger()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000115 unsigned PartBits = PartVT.getSizeInBits();
116 unsigned ValueBits = ValueVT.getSizeInBits();
117
118 // Assemble the power of 2 part.
119 unsigned RoundParts = NumParts & (NumParts - 1) ?
120 1 << Log2_32(NumParts) : NumParts;
121 unsigned RoundBits = PartBits * RoundParts;
Owen Andersone50ed302009-08-10 22:56:29 +0000122 EVT RoundVT = RoundBits == ValueBits ?
Owen Anderson23b9b192009-08-12 00:36:31 +0000123 ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000124 SDValue Lo, Hi;
125
Owen Anderson23b9b192009-08-12 00:36:31 +0000126 EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2);
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000127
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000128 if (RoundParts > 2) {
Chris Lattner3ac18842010-08-24 23:20:40 +0000129 Lo = getCopyFromParts(DAG, DL, Parts, RoundParts / 2,
Bill Wendling12931302012-09-26 04:04:19 +0000130 PartVT, HalfVT, V);
Chris Lattner3ac18842010-08-24 23:20:40 +0000131 Hi = getCopyFromParts(DAG, DL, Parts + RoundParts / 2,
Bill Wendling12931302012-09-26 04:04:19 +0000132 RoundParts / 2, PartVT, HalfVT, V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000133 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000134 Lo = DAG.getNode(ISD::BITCAST, DL, HalfVT, Parts[0]);
135 Hi = DAG.getNode(ISD::BITCAST, DL, HalfVT, Parts[1]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000136 }
Bill Wendling3ea3c242009-12-22 02:10:19 +0000137
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000138 if (TLI.isBigEndian())
139 std::swap(Lo, Hi);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000140
Chris Lattner3ac18842010-08-24 23:20:40 +0000141 Val = DAG.getNode(ISD::BUILD_PAIR, DL, RoundVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000142
143 if (RoundParts < NumParts) {
144 // Assemble the trailing non-power-of-2 part.
145 unsigned OddParts = NumParts - RoundParts;
Owen Anderson23b9b192009-08-12 00:36:31 +0000146 EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits);
Chris Lattner3ac18842010-08-24 23:20:40 +0000147 Hi = getCopyFromParts(DAG, DL,
Bill Wendling12931302012-09-26 04:04:19 +0000148 Parts + RoundParts, OddParts, PartVT, OddVT, V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000149
150 // Combine the round and odd parts.
151 Lo = Val;
152 if (TLI.isBigEndian())
153 std::swap(Lo, Hi);
Owen Anderson23b9b192009-08-12 00:36:31 +0000154 EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Chris Lattner3ac18842010-08-24 23:20:40 +0000155 Hi = DAG.getNode(ISD::ANY_EXTEND, DL, TotalVT, Hi);
156 Hi = DAG.getNode(ISD::SHL, DL, TotalVT, Hi,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000157 DAG.getConstant(Lo.getValueType().getSizeInBits(),
Duncan Sands92abc622009-01-31 15:50:11 +0000158 TLI.getPointerTy()));
Chris Lattner3ac18842010-08-24 23:20:40 +0000159 Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, TotalVT, Lo);
160 Val = DAG.getNode(ISD::OR, DL, TotalVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000161 }
Eli Friedman2ac8b322009-05-20 06:02:09 +0000162 } else if (PartVT.isFloatingPoint()) {
163 // FP split into multiple FP parts (for ppcf128)
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000164 assert(ValueVT == EVT(MVT::ppcf128) && PartVT == MVT::f64 &&
Eli Friedman2ac8b322009-05-20 06:02:09 +0000165 "Unexpected split");
166 SDValue Lo, Hi;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000167 Lo = DAG.getNode(ISD::BITCAST, DL, EVT(MVT::f64), Parts[0]);
168 Hi = DAG.getNode(ISD::BITCAST, DL, EVT(MVT::f64), Parts[1]);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000169 if (TLI.isBigEndian())
170 std::swap(Lo, Hi);
Chris Lattner3ac18842010-08-24 23:20:40 +0000171 Val = DAG.getNode(ISD::BUILD_PAIR, DL, ValueVT, Lo, Hi);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000172 } else {
173 // FP split into integer parts (soft fp)
174 assert(ValueVT.isFloatingPoint() && PartVT.isInteger() &&
175 !PartVT.isVector() && "Unexpected split");
Owen Anderson23b9b192009-08-12 00:36:31 +0000176 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
Bill Wendling12931302012-09-26 04:04:19 +0000177 Val = getCopyFromParts(DAG, DL, Parts, NumParts, PartVT, IntVT, V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000178 }
179 }
180
181 // There is now one part, held in Val. Correct it to match ValueVT.
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000182 EVT PartEVT = Val.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000183
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000184 if (PartEVT == ValueVT)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000185 return Val;
186
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000187 if (PartEVT.isInteger() && ValueVT.isInteger()) {
188 if (ValueVT.bitsLT(PartEVT)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000189 // For a truncate, see if we have any information to
190 // indicate whether the truncated bits will always be
191 // zero or sign-extension.
192 if (AssertOp != ISD::DELETED_NODE)
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000193 Val = DAG.getNode(AssertOp, DL, PartEVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000194 DAG.getValueType(ValueVT));
Chris Lattner3ac18842010-08-24 23:20:40 +0000195 return DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000196 }
Chris Lattner3ac18842010-08-24 23:20:40 +0000197 return DAG.getNode(ISD::ANY_EXTEND, DL, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000198 }
199
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000200 if (PartEVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
Chris Lattner3ac18842010-08-24 23:20:40 +0000201 // FP_ROUND's are always exact here.
202 if (ValueVT.bitsLT(Val.getValueType()))
203 return DAG.getNode(ISD::FP_ROUND, DL, ValueVT, Val,
Pete Cooperf57e1c22012-01-17 01:54:07 +0000204 DAG.getTargetConstant(1, TLI.getPointerTy()));
Bill Wendling3ea3c242009-12-22 02:10:19 +0000205
Chris Lattner3ac18842010-08-24 23:20:40 +0000206 return DAG.getNode(ISD::FP_EXTEND, DL, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000207 }
208
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000209 if (PartEVT.getSizeInBits() == ValueVT.getSizeInBits())
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000210 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000211
Torok Edwinc23197a2009-07-14 16:55:14 +0000212 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000213}
214
Bill Wendling12931302012-09-26 04:04:19 +0000215/// getCopyFromPartsVector - Create a value that contains the specified legal
216/// parts combined into the value they represent. If the parts combine to a
217/// type larger then ValueVT then AssertOp can be used to specify whether the
218/// extra bits are known to be zero (ISD::AssertZext) or sign extended from
219/// ValueVT (ISD::AssertSext).
Andrew Trickac6d9be2013-05-25 02:42:55 +0000220static SDValue getCopyFromPartsVector(SelectionDAG &DAG, SDLoc DL,
Chris Lattner3ac18842010-08-24 23:20:40 +0000221 const SDValue *Parts, unsigned NumParts,
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000222 MVT PartVT, EVT ValueVT, const Value *V) {
Chris Lattner3ac18842010-08-24 23:20:40 +0000223 assert(ValueVT.isVector() && "Not a vector value");
224 assert(NumParts > 0 && "No parts to assemble!");
225 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
226 SDValue Val = Parts[0];
Michael J. Spencere70c5262010-10-16 08:25:21 +0000227
Chris Lattner3ac18842010-08-24 23:20:40 +0000228 // Handle a multi-element vector.
229 if (NumParts > 1) {
Patrik Hagglundee211d22012-12-19 11:53:21 +0000230 EVT IntermediateVT;
231 MVT RegisterVT;
Chris Lattner3ac18842010-08-24 23:20:40 +0000232 unsigned NumIntermediates;
233 unsigned NumRegs =
234 TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT,
235 NumIntermediates, RegisterVT);
236 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
237 NumParts = NumRegs; // Silence a compiler warning.
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000238 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
Patrik Hagglundee211d22012-12-19 11:53:21 +0000239 assert(RegisterVT == Parts[0].getSimpleValueType() &&
Chris Lattner3ac18842010-08-24 23:20:40 +0000240 "Part type doesn't match part!");
Michael J. Spencere70c5262010-10-16 08:25:21 +0000241
Chris Lattner3ac18842010-08-24 23:20:40 +0000242 // Assemble the parts into intermediate operands.
243 SmallVector<SDValue, 8> Ops(NumIntermediates);
244 if (NumIntermediates == NumParts) {
245 // If the register was not expanded, truncate or copy the value,
246 // as appropriate.
247 for (unsigned i = 0; i != NumParts; ++i)
248 Ops[i] = getCopyFromParts(DAG, DL, &Parts[i], 1,
Bill Wendling12931302012-09-26 04:04:19 +0000249 PartVT, IntermediateVT, V);
Chris Lattner3ac18842010-08-24 23:20:40 +0000250 } else if (NumParts > 0) {
251 // If the intermediate type was expanded, build the intermediate
252 // operands from the parts.
253 assert(NumParts % NumIntermediates == 0 &&
254 "Must expand into a divisible number of parts!");
255 unsigned Factor = NumParts / NumIntermediates;
256 for (unsigned i = 0; i != NumIntermediates; ++i)
257 Ops[i] = getCopyFromParts(DAG, DL, &Parts[i * Factor], Factor,
Bill Wendling12931302012-09-26 04:04:19 +0000258 PartVT, IntermediateVT, V);
Chris Lattner3ac18842010-08-24 23:20:40 +0000259 }
Michael J. Spencere70c5262010-10-16 08:25:21 +0000260
Chris Lattner3ac18842010-08-24 23:20:40 +0000261 // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the
262 // intermediate operands.
263 Val = DAG.getNode(IntermediateVT.isVector() ?
264 ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, DL,
265 ValueVT, &Ops[0], NumIntermediates);
266 }
Michael J. Spencere70c5262010-10-16 08:25:21 +0000267
Chris Lattner3ac18842010-08-24 23:20:40 +0000268 // There is now one part, held in Val. Correct it to match ValueVT.
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000269 EVT PartEVT = Val.getValueType();
Michael J. Spencere70c5262010-10-16 08:25:21 +0000270
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000271 if (PartEVT == ValueVT)
Chris Lattner3ac18842010-08-24 23:20:40 +0000272 return Val;
Michael J. Spencere70c5262010-10-16 08:25:21 +0000273
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000274 if (PartEVT.isVector()) {
Chris Lattnere6f7c262010-08-25 22:49:25 +0000275 // If the element type of the source/dest vectors are the same, but the
276 // parts vector has more elements than the value vector, then we have a
277 // vector widening case (e.g. <2 x float> -> <4 x float>). Extract the
278 // elements we want.
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000279 if (PartEVT.getVectorElementType() == ValueVT.getVectorElementType()) {
280 assert(PartEVT.getVectorNumElements() > ValueVT.getVectorNumElements() &&
Chris Lattnere6f7c262010-08-25 22:49:25 +0000281 "Cannot narrow, it would be a lossy transformation");
282 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, ValueVT, Val,
283 DAG.getIntPtrConstant(0));
Michael J. Spencere70c5262010-10-16 08:25:21 +0000284 }
285
Chris Lattnere6f7c262010-08-25 22:49:25 +0000286 // Vector/Vector bitcast.
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000287 if (ValueVT.getSizeInBits() == PartEVT.getSizeInBits())
Nadav Rotem0b666362011-06-04 20:58:08 +0000288 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
289
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000290 assert(PartEVT.getVectorNumElements() == ValueVT.getVectorNumElements() &&
Nadav Rotem0b666362011-06-04 20:58:08 +0000291 "Cannot handle this kind of promotion");
292 // Promoted vector extract
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000293 bool Smaller = ValueVT.bitsLE(PartEVT);
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000294 return DAG.getNode((Smaller ? ISD::TRUNCATE : ISD::ANY_EXTEND),
295 DL, ValueVT, Val);
Nadav Rotem0b666362011-06-04 20:58:08 +0000296
Chris Lattnere6f7c262010-08-25 22:49:25 +0000297 }
Eric Christopher471e4222011-06-08 23:55:35 +0000298
Eric Christopher9aaa02a2011-06-01 19:55:10 +0000299 // Trivial bitcast if the types are the same size and the destination
300 // vector type is legal.
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000301 if (PartEVT.getSizeInBits() == ValueVT.getSizeInBits() &&
Eric Christopher9aaa02a2011-06-01 19:55:10 +0000302 TLI.isTypeLegal(ValueVT))
303 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
Michael J. Spencere70c5262010-10-16 08:25:21 +0000304
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000305 // Handle cases such as i8 -> <1 x i1>
Bill Wendling12931302012-09-26 04:04:19 +0000306 if (ValueVT.getVectorNumElements() != 1) {
307 LLVMContext &Ctx = *DAG.getContext();
308 Twine ErrMsg("non-trivial scalar-to-vector conversion");
309 if (const Instruction *I = dyn_cast_or_null<Instruction>(V)) {
310 if (const CallInst *CI = dyn_cast<CallInst>(I))
311 if (isa<InlineAsm>(CI->getCalledValue()))
312 ErrMsg = ErrMsg + ", possible invalid constraint for vector type";
313 Ctx.emitError(I, ErrMsg);
314 } else {
315 Ctx.emitError(ErrMsg);
316 }
Chad Rosierf0b07552013-05-01 19:49:26 +0000317 return DAG.getUNDEF(ValueVT);
Bill Wendling12931302012-09-26 04:04:19 +0000318 }
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000319
320 if (ValueVT.getVectorNumElements() == 1 &&
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000321 ValueVT.getVectorElementType() != PartEVT) {
322 bool Smaller = ValueVT.bitsLE(PartEVT);
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000323 Val = DAG.getNode((Smaller ? ISD::TRUNCATE : ISD::ANY_EXTEND),
324 DL, ValueVT.getScalarType(), Val);
325 }
326
Chris Lattner3ac18842010-08-24 23:20:40 +0000327 return DAG.getNode(ISD::BUILD_VECTOR, DL, ValueVT, Val);
328}
329
Andrew Trickac6d9be2013-05-25 02:42:55 +0000330static void getCopyToPartsVector(SelectionDAG &DAG, SDLoc dl,
Chris Lattnera13b8602010-08-24 23:10:06 +0000331 SDValue Val, SDValue *Parts, unsigned NumParts,
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000332 MVT PartVT, const Value *V);
Michael J. Spencere70c5262010-10-16 08:25:21 +0000333
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000334/// getCopyToParts - Create a series of nodes that contain the specified value
335/// split into legal parts. If the parts contain more bits than Val, then, for
336/// integers, ExtendKind can be used to specify how to generate the extra bits.
Andrew Trickac6d9be2013-05-25 02:42:55 +0000337static void getCopyToParts(SelectionDAG &DAG, SDLoc DL,
Bill Wendling3ea3c242009-12-22 02:10:19 +0000338 SDValue Val, SDValue *Parts, unsigned NumParts,
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000339 MVT PartVT, const Value *V,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000340 ISD::NodeType ExtendKind = ISD::ANY_EXTEND) {
Owen Andersone50ed302009-08-10 22:56:29 +0000341 EVT ValueVT = Val.getValueType();
Michael J. Spencere70c5262010-10-16 08:25:21 +0000342
Chris Lattnera13b8602010-08-24 23:10:06 +0000343 // Handle the vector case separately.
344 if (ValueVT.isVector())
Bill Wendlingf18eb582012-09-26 06:16:18 +0000345 return getCopyToPartsVector(DAG, DL, Val, Parts, NumParts, PartVT, V);
Michael J. Spencere70c5262010-10-16 08:25:21 +0000346
Chris Lattnera13b8602010-08-24 23:10:06 +0000347 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000348 unsigned PartBits = PartVT.getSizeInBits();
Dale Johannesen8a36f502009-02-25 22:39:13 +0000349 unsigned OrigNumParts = NumParts;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000350 assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!");
351
Chris Lattnera13b8602010-08-24 23:10:06 +0000352 if (NumParts == 0)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000353 return;
354
Chris Lattnera13b8602010-08-24 23:10:06 +0000355 assert(!ValueVT.isVector() && "Vector case handled elsewhere");
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000356 EVT PartEVT = PartVT;
357 if (PartEVT == ValueVT) {
Chris Lattnera13b8602010-08-24 23:10:06 +0000358 assert(NumParts == 1 && "No-op copy with multiple parts!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000359 Parts[0] = Val;
360 return;
361 }
362
Chris Lattnera13b8602010-08-24 23:10:06 +0000363 if (NumParts * PartBits > ValueVT.getSizeInBits()) {
364 // If the parts cover more bits than the value has, promote the value.
365 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
366 assert(NumParts == 1 && "Do not know what to promote to!");
367 Val = DAG.getNode(ISD::FP_EXTEND, DL, PartVT, Val);
368 } else {
Bill Wendling9e8ceb02012-02-23 23:25:25 +0000369 assert((PartVT.isInteger() || PartVT == MVT::x86mmx) &&
370 ValueVT.isInteger() &&
Michael J. Spencere70c5262010-10-16 08:25:21 +0000371 "Unknown mismatch!");
Chris Lattnera13b8602010-08-24 23:10:06 +0000372 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
373 Val = DAG.getNode(ExtendKind, DL, ValueVT, Val);
Bill Wendling9e8ceb02012-02-23 23:25:25 +0000374 if (PartVT == MVT::x86mmx)
375 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
Chris Lattnera13b8602010-08-24 23:10:06 +0000376 }
377 } else if (PartBits == ValueVT.getSizeInBits()) {
378 // Different types of the same size.
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000379 assert(NumParts == 1 && PartEVT != ValueVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000380 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
Chris Lattnera13b8602010-08-24 23:10:06 +0000381 } else if (NumParts * PartBits < ValueVT.getSizeInBits()) {
382 // If the parts cover less bits than value has, truncate the value.
Bill Wendling9e8ceb02012-02-23 23:25:25 +0000383 assert((PartVT.isInteger() || PartVT == MVT::x86mmx) &&
384 ValueVT.isInteger() &&
Chris Lattnera13b8602010-08-24 23:10:06 +0000385 "Unknown mismatch!");
386 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
387 Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
Bill Wendling9e8ceb02012-02-23 23:25:25 +0000388 if (PartVT == MVT::x86mmx)
389 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
Chris Lattnera13b8602010-08-24 23:10:06 +0000390 }
391
392 // The value may have changed - recompute ValueVT.
393 ValueVT = Val.getValueType();
394 assert(NumParts * PartBits == ValueVT.getSizeInBits() &&
395 "Failed to tile the value with PartVT!");
396
397 if (NumParts == 1) {
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000398 if (PartEVT != ValueVT) {
Bill Wendlingf18eb582012-09-26 06:16:18 +0000399 LLVMContext &Ctx = *DAG.getContext();
400 Twine ErrMsg("scalar-to-vector conversion failed");
401 if (const Instruction *I = dyn_cast_or_null<Instruction>(V)) {
402 if (const CallInst *CI = dyn_cast<CallInst>(I))
403 if (isa<InlineAsm>(CI->getCalledValue()))
404 ErrMsg = ErrMsg + ", possible invalid constraint for vector type";
405 Ctx.emitError(I, ErrMsg);
406 } else {
407 Ctx.emitError(ErrMsg);
408 }
409 }
410
Chris Lattnera13b8602010-08-24 23:10:06 +0000411 Parts[0] = Val;
412 return;
413 }
414
415 // Expand the value into multiple parts.
416 if (NumParts & (NumParts - 1)) {
417 // The number of parts is not a power of 2. Split off and copy the tail.
418 assert(PartVT.isInteger() && ValueVT.isInteger() &&
419 "Do not know what to expand to!");
420 unsigned RoundParts = 1 << Log2_32(NumParts);
421 unsigned RoundBits = RoundParts * PartBits;
422 unsigned OddParts = NumParts - RoundParts;
423 SDValue OddVal = DAG.getNode(ISD::SRL, DL, ValueVT, Val,
424 DAG.getIntPtrConstant(RoundBits));
Bill Wendlingf18eb582012-09-26 06:16:18 +0000425 getCopyToParts(DAG, DL, OddVal, Parts + RoundParts, OddParts, PartVT, V);
Chris Lattnera13b8602010-08-24 23:10:06 +0000426
427 if (TLI.isBigEndian())
428 // The odd parts were reversed by getCopyToParts - unreverse them.
429 std::reverse(Parts + RoundParts, Parts + NumParts);
430
431 NumParts = RoundParts;
432 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
433 Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
434 }
435
436 // The number of parts is a power of 2. Repeatedly bisect the value using
437 // EXTRACT_ELEMENT.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000438 Parts[0] = DAG.getNode(ISD::BITCAST, DL,
Chris Lattnera13b8602010-08-24 23:10:06 +0000439 EVT::getIntegerVT(*DAG.getContext(),
440 ValueVT.getSizeInBits()),
441 Val);
442
443 for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) {
444 for (unsigned i = 0; i < NumParts; i += StepSize) {
445 unsigned ThisBits = StepSize * PartBits / 2;
446 EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits);
447 SDValue &Part0 = Parts[i];
448 SDValue &Part1 = Parts[i+StepSize/2];
449
450 Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL,
451 ThisVT, Part0, DAG.getIntPtrConstant(1));
452 Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL,
453 ThisVT, Part0, DAG.getIntPtrConstant(0));
454
455 if (ThisBits == PartBits && ThisVT != PartVT) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000456 Part0 = DAG.getNode(ISD::BITCAST, DL, PartVT, Part0);
457 Part1 = DAG.getNode(ISD::BITCAST, DL, PartVT, Part1);
Chris Lattnera13b8602010-08-24 23:10:06 +0000458 }
459 }
460 }
461
462 if (TLI.isBigEndian())
463 std::reverse(Parts, Parts + OrigNumParts);
464}
465
466
467/// getCopyToPartsVector - Create a series of nodes that contain the specified
468/// value split into legal parts.
Andrew Trickac6d9be2013-05-25 02:42:55 +0000469static void getCopyToPartsVector(SelectionDAG &DAG, SDLoc DL,
Chris Lattnera13b8602010-08-24 23:10:06 +0000470 SDValue Val, SDValue *Parts, unsigned NumParts,
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000471 MVT PartVT, const Value *V) {
Chris Lattnera13b8602010-08-24 23:10:06 +0000472 EVT ValueVT = Val.getValueType();
473 assert(ValueVT.isVector() && "Not a vector");
474 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Michael J. Spencere70c5262010-10-16 08:25:21 +0000475
Chris Lattnera13b8602010-08-24 23:10:06 +0000476 if (NumParts == 1) {
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000477 EVT PartEVT = PartVT;
478 if (PartEVT == ValueVT) {
Chris Lattnere6f7c262010-08-25 22:49:25 +0000479 // Nothing to do.
480 } else if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) {
481 // Bitconvert vector->vector case.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000482 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
Chris Lattnere6f7c262010-08-25 22:49:25 +0000483 } else if (PartVT.isVector() &&
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000484 PartEVT.getVectorElementType() == ValueVT.getVectorElementType() &&
485 PartEVT.getVectorNumElements() > ValueVT.getVectorNumElements()) {
Chris Lattnere6f7c262010-08-25 22:49:25 +0000486 EVT ElementVT = PartVT.getVectorElementType();
487 // Vector widening case, e.g. <2 x float> -> <4 x float>. Shuffle in
488 // undef elements.
489 SmallVector<SDValue, 16> Ops;
490 for (unsigned i = 0, e = ValueVT.getVectorNumElements(); i != e; ++i)
491 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
492 ElementVT, Val, DAG.getIntPtrConstant(i)));
Michael J. Spencere70c5262010-10-16 08:25:21 +0000493
Chris Lattnere6f7c262010-08-25 22:49:25 +0000494 for (unsigned i = ValueVT.getVectorNumElements(),
495 e = PartVT.getVectorNumElements(); i != e; ++i)
496 Ops.push_back(DAG.getUNDEF(ElementVT));
497
498 Val = DAG.getNode(ISD::BUILD_VECTOR, DL, PartVT, &Ops[0], Ops.size());
499
500 // FIXME: Use CONCAT for 2x -> 4x.
Michael J. Spencere70c5262010-10-16 08:25:21 +0000501
Chris Lattnere6f7c262010-08-25 22:49:25 +0000502 //SDValue UndefElts = DAG.getUNDEF(VectorTy);
503 //Val = DAG.getNode(ISD::CONCAT_VECTORS, DL, PartVT, Val, UndefElts);
Nadav Rotem0b666362011-06-04 20:58:08 +0000504 } else if (PartVT.isVector() &&
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000505 PartEVT.getVectorElementType().bitsGE(
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000506 ValueVT.getVectorElementType()) &&
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000507 PartEVT.getVectorNumElements() == ValueVT.getVectorNumElements()) {
Nadav Rotem0b666362011-06-04 20:58:08 +0000508
509 // Promoted vector extract
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000510 bool Smaller = PartEVT.bitsLE(ValueVT);
Nadav Rotemc6341e62011-06-19 08:49:38 +0000511 Val = DAG.getNode((Smaller ? ISD::TRUNCATE : ISD::ANY_EXTEND),
512 DL, PartVT, Val);
Nadav Rotem0b666362011-06-04 20:58:08 +0000513 } else{
Chris Lattnere6f7c262010-08-25 22:49:25 +0000514 // Vector -> scalar conversion.
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000515 assert(ValueVT.getVectorNumElements() == 1 &&
Chris Lattnere6f7c262010-08-25 22:49:25 +0000516 "Only trivial vector-to-scalar conversions should get here!");
517 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
518 PartVT, Val, DAG.getIntPtrConstant(0));
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000519
520 bool Smaller = ValueVT.bitsLE(PartVT);
521 Val = DAG.getNode((Smaller ? ISD::TRUNCATE : ISD::ANY_EXTEND),
522 DL, PartVT, Val);
Chris Lattnera13b8602010-08-24 23:10:06 +0000523 }
Michael J. Spencere70c5262010-10-16 08:25:21 +0000524
Chris Lattnera13b8602010-08-24 23:10:06 +0000525 Parts[0] = Val;
526 return;
527 }
Michael J. Spencere70c5262010-10-16 08:25:21 +0000528
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000529 // Handle a multi-element vector.
Patrik Hagglundee211d22012-12-19 11:53:21 +0000530 EVT IntermediateVT;
531 MVT RegisterVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000532 unsigned NumIntermediates;
Owen Anderson23b9b192009-08-12 00:36:31 +0000533 unsigned NumRegs = TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT,
Devang Patel8f09bea2010-08-26 20:32:32 +0000534 IntermediateVT,
535 NumIntermediates, RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000536 unsigned NumElements = ValueVT.getVectorNumElements();
Michael J. Spencere70c5262010-10-16 08:25:21 +0000537
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000538 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
539 NumParts = NumRegs; // Silence a compiler warning.
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000540 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
Michael J. Spencere70c5262010-10-16 08:25:21 +0000541
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000542 // Split the vector into intermediate operands.
543 SmallVector<SDValue, 8> Ops(NumIntermediates);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000544 for (unsigned i = 0; i != NumIntermediates; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000545 if (IntermediateVT.isVector())
Chris Lattnera13b8602010-08-24 23:10:06 +0000546 Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000547 IntermediateVT, Val,
Chris Lattnera13b8602010-08-24 23:10:06 +0000548 DAG.getIntPtrConstant(i * (NumElements / NumIntermediates)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000549 else
Chris Lattnera13b8602010-08-24 23:10:06 +0000550 Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
Chris Lattnere6f7c262010-08-25 22:49:25 +0000551 IntermediateVT, Val, DAG.getIntPtrConstant(i));
Bill Wendling3ea3c242009-12-22 02:10:19 +0000552 }
Michael J. Spencere70c5262010-10-16 08:25:21 +0000553
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000554 // Split the intermediate operands into legal parts.
555 if (NumParts == NumIntermediates) {
556 // If the register was not expanded, promote or copy the value,
557 // as appropriate.
558 for (unsigned i = 0; i != NumParts; ++i)
Bill Wendlingf18eb582012-09-26 06:16:18 +0000559 getCopyToParts(DAG, DL, Ops[i], &Parts[i], 1, PartVT, V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000560 } else if (NumParts > 0) {
561 // If the intermediate type was expanded, split each the value into
562 // legal parts.
563 assert(NumParts % NumIntermediates == 0 &&
564 "Must expand into a divisible number of parts!");
565 unsigned Factor = NumParts / NumIntermediates;
566 for (unsigned i = 0; i != NumIntermediates; ++i)
Bill Wendlingf18eb582012-09-26 06:16:18 +0000567 getCopyToParts(DAG, DL, Ops[i], &Parts[i*Factor], Factor, PartVT, V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000568 }
569}
570
Dan Gohman462f6b52010-05-29 17:53:24 +0000571namespace {
572 /// RegsForValue - This struct represents the registers (physical or virtual)
573 /// that a particular set of values is assigned, and the type information
574 /// about the value. The most common situation is to represent one value at a
575 /// time, but struct or array values are handled element-wise as multiple
576 /// values. The splitting of aggregates is performed recursively, so that we
577 /// never have aggregate-typed registers. The values at this point do not
578 /// necessarily have legal types, so each value may require one or more
579 /// registers of some legal type.
580 ///
581 struct RegsForValue {
582 /// ValueVTs - The value types of the values, which may not be legal, and
583 /// may need be promoted or synthesized from one or more registers.
584 ///
585 SmallVector<EVT, 4> ValueVTs;
586
587 /// RegVTs - The value types of the registers. This is the same size as
588 /// ValueVTs and it records, for each value, what the type of the assigned
589 /// register or registers are. (Individual values are never synthesized
590 /// from more than one type of register.)
591 ///
592 /// With virtual registers, the contents of RegVTs is redundant with TLI's
593 /// getRegisterType member function, however when with physical registers
594 /// it is necessary to have a separate record of the types.
595 ///
Patrik Hagglund8963fec2012-12-19 12:23:01 +0000596 SmallVector<MVT, 4> RegVTs;
Dan Gohman462f6b52010-05-29 17:53:24 +0000597
598 /// Regs - This list holds the registers assigned to the values.
599 /// Each legal or promoted value requires one register, and each
600 /// expanded value requires multiple registers.
601 ///
602 SmallVector<unsigned, 4> Regs;
603
604 RegsForValue() {}
605
606 RegsForValue(const SmallVector<unsigned, 4> &regs,
Patrik Hagglund8963fec2012-12-19 12:23:01 +0000607 MVT regvt, EVT valuevt)
Dan Gohman462f6b52010-05-29 17:53:24 +0000608 : ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs) {}
609
Dan Gohman462f6b52010-05-29 17:53:24 +0000610 RegsForValue(LLVMContext &Context, const TargetLowering &tli,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000611 unsigned Reg, Type *Ty) {
Dan Gohman462f6b52010-05-29 17:53:24 +0000612 ComputeValueVTs(tli, Ty, ValueVTs);
613
614 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
615 EVT ValueVT = ValueVTs[Value];
616 unsigned NumRegs = tli.getNumRegisters(Context, ValueVT);
Patrik Hagglunddfcf33a2012-12-19 11:48:16 +0000617 MVT RegisterVT = tli.getRegisterType(Context, ValueVT);
Dan Gohman462f6b52010-05-29 17:53:24 +0000618 for (unsigned i = 0; i != NumRegs; ++i)
619 Regs.push_back(Reg + i);
620 RegVTs.push_back(RegisterVT);
621 Reg += NumRegs;
622 }
623 }
624
625 /// areValueTypesLegal - Return true if types of all the values are legal.
626 bool areValueTypesLegal(const TargetLowering &TLI) {
627 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
Patrik Hagglund8963fec2012-12-19 12:23:01 +0000628 MVT RegisterVT = RegVTs[Value];
Dan Gohman462f6b52010-05-29 17:53:24 +0000629 if (!TLI.isTypeLegal(RegisterVT))
630 return false;
631 }
632 return true;
633 }
634
635 /// append - Add the specified values to this one.
636 void append(const RegsForValue &RHS) {
637 ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end());
638 RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end());
639 Regs.append(RHS.Regs.begin(), RHS.Regs.end());
640 }
641
642 /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
643 /// this value and returns the result as a ValueVTs value. This uses
644 /// Chain/Flag as the input and updates them for the output Chain/Flag.
645 /// If the Flag pointer is NULL, no flag is used.
646 SDValue getCopyFromRegs(SelectionDAG &DAG, FunctionLoweringInfo &FuncInfo,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000647 SDLoc dl,
Bill Wendling12931302012-09-26 04:04:19 +0000648 SDValue &Chain, SDValue *Flag,
649 const Value *V = 0) const;
Dan Gohman462f6b52010-05-29 17:53:24 +0000650
651 /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
652 /// specified value into the registers specified by this object. This uses
653 /// Chain/Flag as the input and updates them for the output Chain/Flag.
654 /// If the Flag pointer is NULL, no flag is used.
Andrew Trickac6d9be2013-05-25 02:42:55 +0000655 void getCopyToRegs(SDValue Val, SelectionDAG &DAG, SDLoc dl,
Bill Wendlingf18eb582012-09-26 06:16:18 +0000656 SDValue &Chain, SDValue *Flag, const Value *V) const;
Dan Gohman462f6b52010-05-29 17:53:24 +0000657
658 /// AddInlineAsmOperands - Add this value to the specified inlineasm node
659 /// operand list. This adds the code marker, matching input operand index
660 /// (if applicable), and includes the number of values added into it.
661 void AddInlineAsmOperands(unsigned Kind,
662 bool HasMatching, unsigned MatchingIdx,
663 SelectionDAG &DAG,
664 std::vector<SDValue> &Ops) const;
665 };
666}
667
668/// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
669/// this value and returns the result as a ValueVT value. This uses
670/// Chain/Flag as the input and updates them for the output Chain/Flag.
671/// If the Flag pointer is NULL, no flag is used.
672SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG,
673 FunctionLoweringInfo &FuncInfo,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000674 SDLoc dl,
Bill Wendling12931302012-09-26 04:04:19 +0000675 SDValue &Chain, SDValue *Flag,
676 const Value *V) const {
Dan Gohman7da5d3f2010-07-26 18:15:41 +0000677 // A Value with type {} or [0 x %t] needs no registers.
678 if (ValueVTs.empty())
679 return SDValue();
680
Dan Gohman462f6b52010-05-29 17:53:24 +0000681 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
682
683 // Assemble the legal parts into the final values.
684 SmallVector<SDValue, 4> Values(ValueVTs.size());
685 SmallVector<SDValue, 8> Parts;
686 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
687 // Copy the legal parts from the registers.
688 EVT ValueVT = ValueVTs[Value];
689 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVT);
Patrik Hagglund8963fec2012-12-19 12:23:01 +0000690 MVT RegisterVT = RegVTs[Value];
Dan Gohman462f6b52010-05-29 17:53:24 +0000691
692 Parts.resize(NumRegs);
693 for (unsigned i = 0; i != NumRegs; ++i) {
694 SDValue P;
695 if (Flag == 0) {
696 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
697 } else {
698 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag);
699 *Flag = P.getValue(2);
700 }
701
702 Chain = P.getValue(1);
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000703 Parts[i] = P;
Dan Gohman462f6b52010-05-29 17:53:24 +0000704
705 // If the source register was virtual and if we know something about it,
706 // add an assert node.
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000707 if (!TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) ||
Cameron Zwariche1497b92011-02-24 10:00:08 +0000708 !RegisterVT.isInteger() || RegisterVT.isVector())
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000709 continue;
Cameron Zwariche1497b92011-02-24 10:00:08 +0000710
711 const FunctionLoweringInfo::LiveOutInfo *LOI =
712 FuncInfo.GetLiveOutRegInfo(Regs[Part+i]);
713 if (!LOI)
714 continue;
Dan Gohman462f6b52010-05-29 17:53:24 +0000715
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000716 unsigned RegSize = RegisterVT.getSizeInBits();
Cameron Zwariche1497b92011-02-24 10:00:08 +0000717 unsigned NumSignBits = LOI->NumSignBits;
718 unsigned NumZeroBits = LOI->KnownZero.countLeadingOnes();
Dan Gohman462f6b52010-05-29 17:53:24 +0000719
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000720 // FIXME: We capture more information than the dag can represent. For
721 // now, just use the tightest assertzext/assertsext possible.
722 bool isSExt = true;
723 EVT FromVT(MVT::Other);
724 if (NumSignBits == RegSize)
725 isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1
726 else if (NumZeroBits >= RegSize-1)
727 isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1
728 else if (NumSignBits > RegSize-8)
729 isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8
730 else if (NumZeroBits >= RegSize-8)
731 isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8
732 else if (NumSignBits > RegSize-16)
733 isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16
734 else if (NumZeroBits >= RegSize-16)
735 isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16
736 else if (NumSignBits > RegSize-32)
737 isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32
738 else if (NumZeroBits >= RegSize-32)
739 isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32
740 else
741 continue;
Dan Gohman462f6b52010-05-29 17:53:24 +0000742
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000743 // Add an assertion node.
744 assert(FromVT != MVT::Other);
745 Parts[i] = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
746 RegisterVT, P, DAG.getValueType(FromVT));
Dan Gohman462f6b52010-05-29 17:53:24 +0000747 }
748
749 Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(),
Bill Wendling12931302012-09-26 04:04:19 +0000750 NumRegs, RegisterVT, ValueVT, V);
Dan Gohman462f6b52010-05-29 17:53:24 +0000751 Part += NumRegs;
752 Parts.clear();
753 }
754
755 return DAG.getNode(ISD::MERGE_VALUES, dl,
756 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
757 &Values[0], ValueVTs.size());
758}
759
760/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
761/// specified value into the registers specified by this object. This uses
762/// Chain/Flag as the input and updates them for the output Chain/Flag.
763/// If the Flag pointer is NULL, no flag is used.
Andrew Trickac6d9be2013-05-25 02:42:55 +0000764void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, SDLoc dl,
Bill Wendlingf18eb582012-09-26 06:16:18 +0000765 SDValue &Chain, SDValue *Flag,
766 const Value *V) const {
Dan Gohman462f6b52010-05-29 17:53:24 +0000767 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
768
769 // Get the list of the values's legal parts.
770 unsigned NumRegs = Regs.size();
771 SmallVector<SDValue, 8> Parts(NumRegs);
772 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
773 EVT ValueVT = ValueVTs[Value];
774 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), ValueVT);
Patrik Hagglund8963fec2012-12-19 12:23:01 +0000775 MVT RegisterVT = RegVTs[Value];
Evan Cheng2766a472012-12-06 19:13:27 +0000776 ISD::NodeType ExtendKind =
777 TLI.isZExtFree(Val, RegisterVT)? ISD::ZERO_EXTEND: ISD::ANY_EXTEND;
Dan Gohman462f6b52010-05-29 17:53:24 +0000778
Chris Lattner3ac18842010-08-24 23:20:40 +0000779 getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value),
Evan Cheng2766a472012-12-06 19:13:27 +0000780 &Parts[Part], NumParts, RegisterVT, V, ExtendKind);
Dan Gohman462f6b52010-05-29 17:53:24 +0000781 Part += NumParts;
782 }
783
784 // Copy the parts into the registers.
785 SmallVector<SDValue, 8> Chains(NumRegs);
786 for (unsigned i = 0; i != NumRegs; ++i) {
787 SDValue Part;
788 if (Flag == 0) {
789 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
790 } else {
791 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag);
792 *Flag = Part.getValue(1);
793 }
794
795 Chains[i] = Part.getValue(0);
796 }
797
798 if (NumRegs == 1 || Flag)
799 // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
800 // flagged to it. That is the CopyToReg nodes and the user are considered
801 // a single scheduling unit. If we create a TokenFactor and return it as
802 // chain, then the TokenFactor is both a predecessor (operand) of the
803 // user as well as a successor (the TF operands are flagged to the user).
804 // c1, f1 = CopyToReg
805 // c2, f2 = CopyToReg
806 // c3 = TokenFactor c1, c2
807 // ...
808 // = op c3, ..., f2
809 Chain = Chains[NumRegs-1];
810 else
811 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs);
812}
813
814/// AddInlineAsmOperands - Add this value to the specified inlineasm node
815/// operand list. This adds the code marker and includes the number of
816/// values added into it.
817void RegsForValue::AddInlineAsmOperands(unsigned Code, bool HasMatching,
818 unsigned MatchingIdx,
819 SelectionDAG &DAG,
820 std::vector<SDValue> &Ops) const {
821 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
822
823 unsigned Flag = InlineAsm::getFlagWord(Code, Regs.size());
824 if (HasMatching)
825 Flag = InlineAsm::getFlagWordForMatchingOp(Flag, MatchingIdx);
Jakob Stoklund Olesen459b74b2011-10-12 23:37:29 +0000826 else if (!Regs.empty() &&
827 TargetRegisterInfo::isVirtualRegister(Regs.front())) {
828 // Put the register class of the virtual registers in the flag word. That
829 // way, later passes can recompute register class constraints for inline
830 // assembly as well as normal instructions.
831 // Don't do this for tied operands that can use the regclass information
832 // from the def.
833 const MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
834 const TargetRegisterClass *RC = MRI.getRegClass(Regs.front());
835 Flag = InlineAsm::getFlagWordForRegClass(Flag, RC->getID());
836 }
837
Dan Gohman462f6b52010-05-29 17:53:24 +0000838 SDValue Res = DAG.getTargetConstant(Flag, MVT::i32);
839 Ops.push_back(Res);
840
841 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
842 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVTs[Value]);
Patrik Hagglund8963fec2012-12-19 12:23:01 +0000843 MVT RegisterVT = RegVTs[Value];
Dan Gohman462f6b52010-05-29 17:53:24 +0000844 for (unsigned i = 0; i != NumRegs; ++i) {
845 assert(Reg < Regs.size() && "Mismatch in # registers expected");
846 Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT));
847 }
848 }
849}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000850
Owen Anderson243eb9e2011-12-08 22:15:21 +0000851void SelectionDAGBuilder::init(GCFunctionInfo *gfi, AliasAnalysis &aa,
852 const TargetLibraryInfo *li) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000853 AA = &aa;
854 GFI = gfi;
Owen Anderson243eb9e2011-12-08 22:15:21 +0000855 LibInfo = li;
Micah Villmow3574eca2012-10-08 16:38:25 +0000856 TD = DAG.getTarget().getDataLayout();
Richard Smithcb1f68d2012-08-22 00:42:39 +0000857 Context = DAG.getContext();
Bill Wendling4ed1fb02011-10-15 01:00:26 +0000858 LPadToCallSiteMap.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000859}
860
Dan Gohmanb02b62a2010-04-14 18:24:06 +0000861/// clear - Clear out the current SelectionDAG and the associated
Dan Gohman2048b852009-11-23 18:04:58 +0000862/// state and prepare this SelectionDAGBuilder object to be used
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000863/// for a new block. This doesn't clear out information about
864/// additional blocks that are needed to complete switch lowering
865/// or PHI node updating; that information is cleared out as it is
866/// consumed.
Dan Gohman2048b852009-11-23 18:04:58 +0000867void SelectionDAGBuilder::clear() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000868 NodeMap.clear();
Devang Patel9126c0d2010-06-01 19:59:01 +0000869 UnusedArgNodeMap.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000870 PendingLoads.clear();
871 PendingExports.clear();
Andrew Trickea5db0c2013-05-25 02:20:36 +0000872 CurInst = NULL;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000873 HasTailCall = false;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000874}
875
Devang Patel23385752011-05-23 17:44:13 +0000876/// clearDanglingDebugInfo - Clear the dangling debug information
Benjamin Kramerd9b0b022012-06-02 10:20:22 +0000877/// map. This function is separated from the clear so that debug
Devang Patel23385752011-05-23 17:44:13 +0000878/// information that is dangling in a basic block can be properly
879/// resolved in a different basic block. This allows the
880/// SelectionDAG to resolve dangling debug information attached
881/// to PHI nodes.
882void SelectionDAGBuilder::clearDanglingDebugInfo() {
883 DanglingDebugInfoMap.clear();
884}
885
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000886/// getRoot - Return the current virtual root of the Selection DAG,
887/// flushing any PendingLoad items. This must be done before emitting
888/// a store or any other node that may need to be ordered after any
889/// prior load instructions.
890///
Dan Gohman2048b852009-11-23 18:04:58 +0000891SDValue SelectionDAGBuilder::getRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000892 if (PendingLoads.empty())
893 return DAG.getRoot();
894
895 if (PendingLoads.size() == 1) {
896 SDValue Root = PendingLoads[0];
897 DAG.setRoot(Root);
898 PendingLoads.clear();
899 return Root;
900 }
901
902 // Otherwise, we have to make a token factor node.
Andrew Trickac6d9be2013-05-25 02:42:55 +0000903 SDValue Root = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000904 &PendingLoads[0], PendingLoads.size());
905 PendingLoads.clear();
906 DAG.setRoot(Root);
907 return Root;
908}
909
910/// getControlRoot - Similar to getRoot, but instead of flushing all the
911/// PendingLoad items, flush all the PendingExports items. It is necessary
912/// to do this before emitting a terminator instruction.
913///
Dan Gohman2048b852009-11-23 18:04:58 +0000914SDValue SelectionDAGBuilder::getControlRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000915 SDValue Root = DAG.getRoot();
916
917 if (PendingExports.empty())
918 return Root;
919
920 // Turn all of the CopyToReg chains into one factored node.
921 if (Root.getOpcode() != ISD::EntryToken) {
922 unsigned i = 0, e = PendingExports.size();
923 for (; i != e; ++i) {
924 assert(PendingExports[i].getNode()->getNumOperands() > 1);
925 if (PendingExports[i].getNode()->getOperand(0) == Root)
926 break; // Don't add the root if we already indirectly depend on it.
927 }
928
929 if (i == e)
930 PendingExports.push_back(Root);
931 }
932
Andrew Trickac6d9be2013-05-25 02:42:55 +0000933 Root = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000934 &PendingExports[0],
935 PendingExports.size());
936 PendingExports.clear();
937 DAG.setRoot(Root);
938 return Root;
939}
940
Dan Gohman46510a72010-04-15 01:51:59 +0000941void SelectionDAGBuilder::visit(const Instruction &I) {
Dan Gohmanc105a2b2010-04-22 20:55:53 +0000942 // Set up outgoing PHI node register values before emitting the terminator.
943 if (isa<TerminatorInst>(&I))
944 HandlePHINodesInSuccessorBlocks(I.getParent());
945
Andrew Trickdd0fb012013-05-25 03:08:10 +0000946 ++SDNodeOrder;
947
Andrew Trickea5db0c2013-05-25 02:20:36 +0000948 CurInst = &I;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000949
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000950 visit(I.getOpcode(), I);
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000951
Dan Gohman92884f72010-04-20 15:03:56 +0000952 if (!isa<TerminatorInst>(&I) && !HasTailCall)
953 CopyToExportRegsIfNeeded(&I);
954
Andrew Trickea5db0c2013-05-25 02:20:36 +0000955 CurInst = NULL;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000956}
957
Dan Gohmanba5be5c2010-04-20 15:00:41 +0000958void SelectionDAGBuilder::visitPHI(const PHINode &) {
959 llvm_unreachable("SelectionDAGBuilder shouldn't visit PHI nodes!");
960}
961
Dan Gohman46510a72010-04-15 01:51:59 +0000962void SelectionDAGBuilder::visit(unsigned Opcode, const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000963 // Note: this doesn't use InstVisitor, because it has to work with
964 // ConstantExpr's in addition to instructions.
965 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000966 default: llvm_unreachable("Unknown instruction type encountered!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000967 // Build the switch statement using the Instruction.def file.
968#define HANDLE_INST(NUM, OPCODE, CLASS) \
Galina Kistanova72ea0c92012-07-19 04:50:12 +0000969 case Instruction::OPCODE: visit##OPCODE((const CLASS&)I); break;
Chandler Carruth0b8c9a82013-01-02 11:36:10 +0000970#include "llvm/IR/Instruction.def"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000971 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000972}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000973
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000974// resolveDanglingDebugInfo - if we saw an earlier dbg_value referring to V,
975// generate the debug data structures now that we've seen its definition.
976void SelectionDAGBuilder::resolveDanglingDebugInfo(const Value *V,
977 SDValue Val) {
978 DanglingDebugInfo &DDI = DanglingDebugInfoMap[V];
Devang Patel4cf81c42010-08-26 23:35:15 +0000979 if (DDI.getDI()) {
980 const DbgValueInst *DI = DDI.getDI();
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000981 DebugLoc dl = DDI.getdl();
982 unsigned DbgSDNodeOrder = DDI.getSDNodeOrder();
Devang Patel4cf81c42010-08-26 23:35:15 +0000983 MDNode *Variable = DI->getVariable();
984 uint64_t Offset = DI->getOffset();
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000985 SDDbgValue *SDV;
986 if (Val.getNode()) {
Devang Patel78a06e52010-08-25 20:39:26 +0000987 if (!EmitFuncArgumentDbgValue(V, Variable, Offset, Val)) {
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000988 SDV = DAG.getDbgValue(Variable, Val.getNode(),
989 Val.getResNo(), Offset, dl, DbgSDNodeOrder);
990 DAG.AddDbgValue(SDV, Val.getNode(), false);
991 }
Owen Anderson95771af2011-02-25 21:41:48 +0000992 } else
Adrian Prantl5da4e4f2013-05-22 18:02:19 +0000993 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000994 DanglingDebugInfoMap[V] = DanglingDebugInfo();
995 }
996}
997
Nick Lewycky8de34002011-09-30 22:19:53 +0000998/// getValue - Return an SDValue for the given Value.
Dan Gohman2048b852009-11-23 18:04:58 +0000999SDValue SelectionDAGBuilder::getValue(const Value *V) {
Dan Gohman28a17352010-07-01 01:59:43 +00001000 // If we already have an SDValue for this value, use it. It's important
1001 // to do this first, so that we don't create a CopyFromReg if we already
1002 // have a regular SDValue.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001003 SDValue &N = NodeMap[V];
1004 if (N.getNode()) return N;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001005
Dan Gohman28a17352010-07-01 01:59:43 +00001006 // If there's a virtual register allocated and initialized for this
1007 // value, use it.
1008 DenseMap<const Value *, unsigned>::iterator It = FuncInfo.ValueMap.find(V);
1009 if (It != FuncInfo.ValueMap.end()) {
1010 unsigned InReg = It->second;
1011 RegsForValue RFV(*DAG.getContext(), TLI, InReg, V->getType());
1012 SDValue Chain = DAG.getEntryNode();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001013 N = RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, NULL, V);
Devang Patel8f314282011-01-25 18:09:58 +00001014 resolveDanglingDebugInfo(V, N);
1015 return N;
Dan Gohman28a17352010-07-01 01:59:43 +00001016 }
1017
1018 // Otherwise create a new SDValue and remember it.
1019 SDValue Val = getValueImpl(V);
1020 NodeMap[V] = Val;
Dale Johannesenbdc09d92010-07-16 00:02:08 +00001021 resolveDanglingDebugInfo(V, Val);
Dan Gohman28a17352010-07-01 01:59:43 +00001022 return Val;
1023}
1024
1025/// getNonRegisterValue - Return an SDValue for the given Value, but
1026/// don't look in FuncInfo.ValueMap for a virtual register.
1027SDValue SelectionDAGBuilder::getNonRegisterValue(const Value *V) {
1028 // If we already have an SDValue for this value, use it.
1029 SDValue &N = NodeMap[V];
1030 if (N.getNode()) return N;
1031
1032 // Otherwise create a new SDValue and remember it.
1033 SDValue Val = getValueImpl(V);
1034 NodeMap[V] = Val;
Dale Johannesenbdc09d92010-07-16 00:02:08 +00001035 resolveDanglingDebugInfo(V, Val);
Dan Gohman28a17352010-07-01 01:59:43 +00001036 return Val;
1037}
1038
Dale Johannesenbdc09d92010-07-16 00:02:08 +00001039/// getValueImpl - Helper function for getValue and getNonRegisterValue.
Dan Gohman28a17352010-07-01 01:59:43 +00001040/// Create an SDValue for the given value.
1041SDValue SelectionDAGBuilder::getValueImpl(const Value *V) {
Dan Gohman383b5f62010-04-17 15:32:28 +00001042 if (const Constant *C = dyn_cast<Constant>(V)) {
Owen Andersone50ed302009-08-10 22:56:29 +00001043 EVT VT = TLI.getValueType(V->getType(), true);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001044
Dan Gohman383b5f62010-04-17 15:32:28 +00001045 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C))
Dan Gohman28a17352010-07-01 01:59:43 +00001046 return DAG.getConstant(*CI, VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001047
Dan Gohman383b5f62010-04-17 15:32:28 +00001048 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001049 return DAG.getGlobalAddress(GV, getCurSDLoc(), VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001050
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001051 if (isa<ConstantPointerNull>(C))
Dan Gohman28a17352010-07-01 01:59:43 +00001052 return DAG.getConstant(0, TLI.getPointerTy());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001053
Dan Gohman383b5f62010-04-17 15:32:28 +00001054 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Dan Gohman28a17352010-07-01 01:59:43 +00001055 return DAG.getConstantFP(*CFP, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001056
Nate Begeman9008ca62009-04-27 18:41:29 +00001057 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
Dan Gohman28a17352010-07-01 01:59:43 +00001058 return DAG.getUNDEF(VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001059
Dan Gohman383b5f62010-04-17 15:32:28 +00001060 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001061 visit(CE->getOpcode(), *CE);
1062 SDValue N1 = NodeMap[V];
Dan Gohmanac7d05c2010-04-16 16:55:18 +00001063 assert(N1.getNode() && "visit didn't populate the NodeMap!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001064 return N1;
1065 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001066
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001067 if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
1068 SmallVector<SDValue, 4> Constants;
1069 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
1070 OI != OE; ++OI) {
1071 SDNode *Val = getValue(*OI).getNode();
Dan Gohmaned48caf2009-09-08 01:44:02 +00001072 // If the operand is an empty aggregate, there are no values.
1073 if (!Val) continue;
1074 // Add each leaf value from the operand to the Constants list
1075 // to form a flattened list of all the values.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001076 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
1077 Constants.push_back(SDValue(Val, i));
1078 }
Bill Wendling87710f02009-12-21 23:47:40 +00001079
Bill Wendling4533cac2010-01-28 21:51:40 +00001080 return DAG.getMergeValues(&Constants[0], Constants.size(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001081 getCurSDLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001082 }
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001083
1084 if (const ConstantDataSequential *CDS =
1085 dyn_cast<ConstantDataSequential>(C)) {
1086 SmallVector<SDValue, 4> Ops;
Chris Lattner0f193b82012-01-25 01:27:20 +00001087 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001088 SDNode *Val = getValue(CDS->getElementAsConstant(i)).getNode();
1089 // Add each leaf value from the operand to the Constants list
1090 // to form a flattened list of all the values.
1091 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
1092 Ops.push_back(SDValue(Val, i));
1093 }
1094
1095 if (isa<ArrayType>(CDS->getType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001096 return DAG.getMergeValues(&Ops[0], Ops.size(), getCurSDLoc());
1097 return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurSDLoc(),
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001098 VT, &Ops[0], Ops.size());
1099 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001100
Duncan Sands1df98592010-02-16 11:11:14 +00001101 if (C->getType()->isStructTy() || C->getType()->isArrayTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001102 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
1103 "Unknown struct or array constant!");
1104
Owen Andersone50ed302009-08-10 22:56:29 +00001105 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001106 ComputeValueVTs(TLI, C->getType(), ValueVTs);
1107 unsigned NumElts = ValueVTs.size();
1108 if (NumElts == 0)
1109 return SDValue(); // empty struct
1110 SmallVector<SDValue, 4> Constants(NumElts);
1111 for (unsigned i = 0; i != NumElts; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00001112 EVT EltVT = ValueVTs[i];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001113 if (isa<UndefValue>(C))
Dale Johannesene8d72302009-02-06 23:05:02 +00001114 Constants[i] = DAG.getUNDEF(EltVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001115 else if (EltVT.isFloatingPoint())
1116 Constants[i] = DAG.getConstantFP(0, EltVT);
1117 else
1118 Constants[i] = DAG.getConstant(0, EltVT);
1119 }
Bill Wendling87710f02009-12-21 23:47:40 +00001120
Bill Wendling4533cac2010-01-28 21:51:40 +00001121 return DAG.getMergeValues(&Constants[0], NumElts,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001122 getCurSDLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001123 }
1124
Dan Gohman383b5f62010-04-17 15:32:28 +00001125 if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
Dan Gohman29cbade2009-11-20 23:18:13 +00001126 return DAG.getBlockAddress(BA, VT);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001127
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001128 VectorType *VecTy = cast<VectorType>(V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001129 unsigned NumElements = VecTy->getNumElements();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001130
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001131 // Now that we know the number and type of the elements, get that number of
1132 // elements into the Ops array based on what kind of constant it is.
1133 SmallVector<SDValue, 16> Ops;
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001134 if (const ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001135 for (unsigned i = 0; i != NumElements; ++i)
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001136 Ops.push_back(getValue(CV->getOperand(i)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001137 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00001138 assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!");
Owen Andersone50ed302009-08-10 22:56:29 +00001139 EVT EltVT = TLI.getValueType(VecTy->getElementType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001140
1141 SDValue Op;
Nate Begeman9008ca62009-04-27 18:41:29 +00001142 if (EltVT.isFloatingPoint())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001143 Op = DAG.getConstantFP(0, EltVT);
1144 else
1145 Op = DAG.getConstant(0, EltVT);
1146 Ops.assign(NumElements, Op);
1147 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001148
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001149 // Create a BUILD_VECTOR node.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001150 return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00001151 VT, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001152 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001153
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001154 // If this is a static alloca, generate it as the frameindex instead of
1155 // computation.
1156 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
1157 DenseMap<const AllocaInst*, int>::iterator SI =
1158 FuncInfo.StaticAllocaMap.find(AI);
1159 if (SI != FuncInfo.StaticAllocaMap.end())
1160 return DAG.getFrameIndex(SI->second, TLI.getPointerTy());
1161 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001162
Dan Gohman28a17352010-07-01 01:59:43 +00001163 // If this is an instruction which fast-isel has deferred, select it now.
1164 if (const Instruction *Inst = dyn_cast<Instruction>(V)) {
Dan Gohman84023e02010-07-10 09:00:22 +00001165 unsigned InReg = FuncInfo.InitializeRegForValue(Inst);
1166 RegsForValue RFV(*DAG.getContext(), TLI, InReg, Inst->getType());
1167 SDValue Chain = DAG.getEntryNode();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001168 return RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, NULL, V);
Dan Gohman28a17352010-07-01 01:59:43 +00001169 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001170
Dan Gohman28a17352010-07-01 01:59:43 +00001171 llvm_unreachable("Can't get register for value!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001172}
1173
Dan Gohman46510a72010-04-15 01:51:59 +00001174void SelectionDAGBuilder::visitRet(const ReturnInst &I) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001175 SDValue Chain = getControlRoot();
1176 SmallVector<ISD::OutputArg, 8> Outs;
Dan Gohmanc9403652010-07-07 15:54:55 +00001177 SmallVector<SDValue, 8> OutVals;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001178
Dan Gohman7451d3e2010-05-29 17:03:36 +00001179 if (!FuncInfo.CanLowerReturn) {
1180 unsigned DemoteReg = FuncInfo.DemoteRegister;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001181 const Function *F = I.getParent()->getParent();
1182
1183 // Emit a store of the return value through the virtual register.
1184 // Leave Outs empty so that LowerReturn won't try to load return
1185 // registers the usual way.
1186 SmallVector<EVT, 1> PtrValueVTs;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001187 ComputeValueVTs(TLI, PointerType::getUnqual(F->getReturnType()),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001188 PtrValueVTs);
1189
1190 SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]);
1191 SDValue RetOp = getValue(I.getOperand(0));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001192
Owen Andersone50ed302009-08-10 22:56:29 +00001193 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001194 SmallVector<uint64_t, 4> Offsets;
1195 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets);
Dan Gohman7ea1ca62008-10-21 20:00:42 +00001196 unsigned NumValues = ValueVTs.size();
Dan Gohman7ea1ca62008-10-21 20:00:42 +00001197
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001198 SmallVector<SDValue, 4> Chains(NumValues);
Bill Wendling87710f02009-12-21 23:47:40 +00001199 for (unsigned i = 0; i != NumValues; ++i) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001200 SDValue Add = DAG.getNode(ISD::ADD, getCurSDLoc(),
Chris Lattnera13b8602010-08-24 23:10:06 +00001201 RetPtr.getValueType(), RetPtr,
1202 DAG.getIntPtrConstant(Offsets[i]));
Bill Wendling87710f02009-12-21 23:47:40 +00001203 Chains[i] =
Andrew Trickac6d9be2013-05-25 02:42:55 +00001204 DAG.getStore(Chain, getCurSDLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001205 SDValue(RetOp.getNode(), RetOp.getResNo() + i),
Chris Lattner84bd98a2010-09-21 18:58:22 +00001206 // FIXME: better loc info would be nice.
1207 Add, MachinePointerInfo(), false, false, 0);
Bill Wendling87710f02009-12-21 23:47:40 +00001208 }
1209
Andrew Trickac6d9be2013-05-25 02:42:55 +00001210 Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001211 MVT::Other, &Chains[0], NumValues);
Chris Lattner25d58372010-02-28 18:53:13 +00001212 } else if (I.getNumOperands() != 0) {
1213 SmallVector<EVT, 4> ValueVTs;
1214 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs);
1215 unsigned NumValues = ValueVTs.size();
1216 if (NumValues) {
1217 SDValue RetOp = getValue(I.getOperand(0));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001218 for (unsigned j = 0, f = NumValues; j != f; ++j) {
1219 EVT VT = ValueVTs[j];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001220
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001221 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001222
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001223 const Function *F = I.getParent()->getParent();
Bill Wendling8b62abd2012-12-30 13:01:51 +00001224 if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
1225 Attribute::SExt))
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001226 ExtendKind = ISD::SIGN_EXTEND;
Bill Wendling8b62abd2012-12-30 13:01:51 +00001227 else if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
1228 Attribute::ZExt))
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001229 ExtendKind = ISD::ZERO_EXTEND;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001230
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001231 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger())
Patrik Hagglunde5c65912012-12-19 12:02:25 +00001232 VT = TLI.getTypeForExtArgOrReturn(VT.getSimpleVT(), ExtendKind);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001233
1234 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), VT);
Patrik Hagglunddfcf33a2012-12-19 11:48:16 +00001235 MVT PartVT = TLI.getRegisterType(*DAG.getContext(), VT);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001236 SmallVector<SDValue, 4> Parts(NumParts);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001237 getCopyToParts(DAG, getCurSDLoc(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001238 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
Bill Wendlingf18eb582012-09-26 06:16:18 +00001239 &Parts[0], NumParts, PartVT, &I, ExtendKind);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001240
1241 // 'inreg' on function refers to return value
1242 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
Bill Wendling8b62abd2012-12-30 13:01:51 +00001243 if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
1244 Attribute::InReg))
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001245 Flags.setInReg();
1246
1247 // Propagate extension type if any
Cameron Zwarich8df6bf52011-03-16 22:20:07 +00001248 if (ExtendKind == ISD::SIGN_EXTEND)
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001249 Flags.setSExt();
Cameron Zwarich8df6bf52011-03-16 22:20:07 +00001250 else if (ExtendKind == ISD::ZERO_EXTEND)
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001251 Flags.setZExt();
1252
Dan Gohmanc9403652010-07-07 15:54:55 +00001253 for (unsigned i = 0; i < NumParts; ++i) {
1254 Outs.push_back(ISD::OutputArg(Flags, Parts[i].getValueType(),
Manman Ren0a1544d2012-11-01 23:49:58 +00001255 /*isfixed=*/true, 0, 0));
Dan Gohmanc9403652010-07-07 15:54:55 +00001256 OutVals.push_back(Parts[i]);
1257 }
Evan Cheng3927f432009-03-25 20:20:11 +00001258 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001259 }
1260 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001261
1262 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001263 CallingConv::ID CallConv =
1264 DAG.getMachineFunction().getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001265 Chain = TLI.LowerReturn(Chain, CallConv, isVarArg,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001266 Outs, OutVals, getCurSDLoc(), DAG);
Dan Gohman5e866062009-08-06 15:37:27 +00001267
1268 // Verify that the target's LowerReturn behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00001269 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00001270 "LowerReturn didn't return a valid chain!");
1271
1272 // Update the DAG with the new chain value resulting from return lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001273 DAG.setRoot(Chain);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001274}
1275
Dan Gohmanad62f532009-04-23 23:13:24 +00001276/// CopyToExportRegsIfNeeded - If the given value has virtual registers
1277/// created for it, emit nodes to copy the value into the virtual
1278/// registers.
Dan Gohman46510a72010-04-15 01:51:59 +00001279void SelectionDAGBuilder::CopyToExportRegsIfNeeded(const Value *V) {
Rafael Espindola3fa82832011-05-13 15:18:06 +00001280 // Skip empty types
1281 if (V->getType()->isEmptyTy())
1282 return;
1283
Dan Gohman33b7a292010-04-16 17:15:02 +00001284 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
1285 if (VMI != FuncInfo.ValueMap.end()) {
1286 assert(!V->use_empty() && "Unused value assigned virtual registers!");
1287 CopyValueToVirtualRegister(V, VMI->second);
Dan Gohmanad62f532009-04-23 23:13:24 +00001288 }
1289}
1290
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001291/// ExportFromCurrentBlock - If this condition isn't known to be exported from
1292/// the current basic block, add it to ValueMap now so that we'll get a
1293/// CopyTo/FromReg.
Dan Gohman46510a72010-04-15 01:51:59 +00001294void SelectionDAGBuilder::ExportFromCurrentBlock(const Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001295 // No need to export constants.
1296 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001297
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001298 // Already exported?
1299 if (FuncInfo.isExportedInst(V)) return;
1300
1301 unsigned Reg = FuncInfo.InitializeRegForValue(V);
1302 CopyValueToVirtualRegister(V, Reg);
1303}
1304
Dan Gohman46510a72010-04-15 01:51:59 +00001305bool SelectionDAGBuilder::isExportableFromCurrentBlock(const Value *V,
Dan Gohman2048b852009-11-23 18:04:58 +00001306 const BasicBlock *FromBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001307 // The operands of the setcc have to be in this block. We don't know
1308 // how to export them from some other block.
Dan Gohman46510a72010-04-15 01:51:59 +00001309 if (const Instruction *VI = dyn_cast<Instruction>(V)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001310 // Can export from current BB.
1311 if (VI->getParent() == FromBB)
1312 return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001313
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001314 // Is already exported, noop.
1315 return FuncInfo.isExportedInst(V);
1316 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001317
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001318 // If this is an argument, we can export it if the BB is the entry block or
1319 // if it is already exported.
1320 if (isa<Argument>(V)) {
1321 if (FromBB == &FromBB->getParent()->getEntryBlock())
1322 return true;
1323
1324 // Otherwise, can only export this if it is already exported.
1325 return FuncInfo.isExportedInst(V);
1326 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001327
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001328 // Otherwise, constants can always be exported.
1329 return true;
1330}
1331
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001332/// Return branch probability calculated by BranchProbabilityInfo for IR blocks.
Jakub Staszak25101bb2011-12-20 20:03:10 +00001333uint32_t SelectionDAGBuilder::getEdgeWeight(const MachineBasicBlock *Src,
1334 const MachineBasicBlock *Dst) const {
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001335 BranchProbabilityInfo *BPI = FuncInfo.BPI;
1336 if (!BPI)
1337 return 0;
Jakub Staszak95ece8e2011-07-29 20:05:36 +00001338 const BasicBlock *SrcBB = Src->getBasicBlock();
1339 const BasicBlock *DstBB = Dst->getBasicBlock();
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001340 return BPI->getEdgeWeight(SrcBB, DstBB);
1341}
1342
Jakub Staszakc8f34de2011-07-29 22:25:21 +00001343void SelectionDAGBuilder::
1344addSuccessorWithWeight(MachineBasicBlock *Src, MachineBasicBlock *Dst,
1345 uint32_t Weight /* = 0 */) {
1346 if (!Weight)
1347 Weight = getEdgeWeight(Src, Dst);
1348 Src->addSuccessor(Dst, Weight);
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001349}
1350
1351
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001352static bool InBlock(const Value *V, const BasicBlock *BB) {
1353 if (const Instruction *I = dyn_cast<Instruction>(V))
1354 return I->getParent() == BB;
1355 return true;
1356}
1357
Dan Gohmanc2277342008-10-17 21:16:08 +00001358/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
1359/// This function emits a branch and is used at the leaves of an OR or an
1360/// AND operator tree.
1361///
1362void
Dan Gohman46510a72010-04-15 01:51:59 +00001363SelectionDAGBuilder::EmitBranchForMergedCondition(const Value *Cond,
Dan Gohman2048b852009-11-23 18:04:58 +00001364 MachineBasicBlock *TBB,
1365 MachineBasicBlock *FBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001366 MachineBasicBlock *CurBB,
1367 MachineBasicBlock *SwitchBB) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001368 const BasicBlock *BB = CurBB->getBasicBlock();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001369
Dan Gohmanc2277342008-10-17 21:16:08 +00001370 // If the leaf of the tree is a comparison, merge the condition into
1371 // the caseblock.
Dan Gohman46510a72010-04-15 01:51:59 +00001372 if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001373 // The operands of the cmp have to be in this block. We don't know
1374 // how to export them from some other block. If this is the first block
1375 // of the sequence, no exporting is needed.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001376 if (CurBB == SwitchBB ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001377 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
1378 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001379 ISD::CondCode Condition;
Dan Gohman46510a72010-04-15 01:51:59 +00001380 if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001381 Condition = getICmpCondCode(IC->getPredicate());
Dan Gohman46510a72010-04-15 01:51:59 +00001382 } else if (const FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001383 Condition = getFCmpCondCode(FC->getPredicate());
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001384 if (TM.Options.NoNaNsFPMath)
1385 Condition = getFCmpCodeWithoutNaN(Condition);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001386 } else {
1387 Condition = ISD::SETEQ; // silence warning.
Torok Edwinc23197a2009-07-14 16:55:14 +00001388 llvm_unreachable("Unknown compare instruction");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001389 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001390
1391 CaseBlock CB(Condition, BOp->getOperand(0),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001392 BOp->getOperand(1), NULL, TBB, FBB, CurBB);
1393 SwitchCases.push_back(CB);
1394 return;
1395 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001396 }
1397
1398 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001399 CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanc2277342008-10-17 21:16:08 +00001400 NULL, TBB, FBB, CurBB);
1401 SwitchCases.push_back(CB);
1402}
1403
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001404/// FindMergedConditions - If Cond is an expression like
Dan Gohman46510a72010-04-15 01:51:59 +00001405void SelectionDAGBuilder::FindMergedConditions(const Value *Cond,
Dan Gohman2048b852009-11-23 18:04:58 +00001406 MachineBasicBlock *TBB,
1407 MachineBasicBlock *FBB,
1408 MachineBasicBlock *CurBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001409 MachineBasicBlock *SwitchBB,
Dan Gohman2048b852009-11-23 18:04:58 +00001410 unsigned Opc) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001411 // If this node is not part of the or/and tree, emit it as a branch.
Dan Gohman46510a72010-04-15 01:51:59 +00001412 const Instruction *BOp = dyn_cast<Instruction>(Cond);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001413 if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001414 (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
1415 BOp->getParent() != CurBB->getBasicBlock() ||
1416 !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1417 !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001418 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001419 return;
1420 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001421
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001422 // Create TmpBB after CurBB.
1423 MachineFunction::iterator BBI = CurBB;
1424 MachineFunction &MF = DAG.getMachineFunction();
1425 MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
1426 CurBB->getParent()->insert(++BBI, TmpBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001427
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001428 if (Opc == Instruction::Or) {
1429 // Codegen X | Y as:
1430 // jmp_if_X TBB
1431 // jmp TmpBB
1432 // TmpBB:
1433 // jmp_if_Y TBB
1434 // jmp FBB
1435 //
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001436
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001437 // Emit the LHS condition.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001438 FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, SwitchBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001439
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001440 // Emit the RHS condition into TmpBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001441 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001442 } else {
1443 assert(Opc == Instruction::And && "Unknown merge op!");
1444 // Codegen X & Y as:
1445 // jmp_if_X TmpBB
1446 // jmp FBB
1447 // TmpBB:
1448 // jmp_if_Y TBB
1449 // jmp FBB
1450 //
1451 // This requires creation of TmpBB after CurBB.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001452
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001453 // Emit the LHS condition.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001454 FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, SwitchBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001455
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001456 // Emit the RHS condition into TmpBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001457 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001458 }
1459}
1460
1461/// If the set of cases should be emitted as a series of branches, return true.
1462/// If we should emit this as a bunch of and/or'd together conditions, return
1463/// false.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001464bool
Dan Gohman2048b852009-11-23 18:04:58 +00001465SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001466 if (Cases.size() != 2) return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001467
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001468 // If this is two comparisons of the same values or'd or and'd together, they
1469 // will get folded into a single comparison, so don't emit two blocks.
1470 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1471 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1472 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1473 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1474 return false;
1475 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001476
Chris Lattner133ce872010-01-02 00:00:03 +00001477 // Handle: (X != null) | (Y != null) --> (X|Y) != 0
1478 // Handle: (X == null) & (Y == null) --> (X|Y) == 0
1479 if (Cases[0].CmpRHS == Cases[1].CmpRHS &&
1480 Cases[0].CC == Cases[1].CC &&
1481 isa<Constant>(Cases[0].CmpRHS) &&
1482 cast<Constant>(Cases[0].CmpRHS)->isNullValue()) {
1483 if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB)
1484 return false;
1485 if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB)
1486 return false;
1487 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00001488
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001489 return true;
1490}
1491
Dan Gohman46510a72010-04-15 01:51:59 +00001492void SelectionDAGBuilder::visitBr(const BranchInst &I) {
Dan Gohman84023e02010-07-10 09:00:22 +00001493 MachineBasicBlock *BrMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001494
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001495 // Update machine-CFG edges.
1496 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1497
1498 // Figure out which block is immediately after the current one.
1499 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001500 MachineFunction::iterator BBI = BrMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001501 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001502 NextBlock = BBI;
1503
1504 if (I.isUnconditional()) {
1505 // Update machine-CFG edges.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001506 BrMBB->addSuccessor(Succ0MBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001507
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001508 // If this is not a fall-through branch, emit the branch.
Bill Wendling4533cac2010-01-28 21:51:40 +00001509 if (Succ0MBB != NextBlock)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001510 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001511 MVT::Other, getControlRoot(),
Bill Wendling4533cac2010-01-28 21:51:40 +00001512 DAG.getBasicBlock(Succ0MBB)));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001513
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001514 return;
1515 }
1516
1517 // If this condition is one of the special cases we handle, do special stuff
1518 // now.
Dan Gohman46510a72010-04-15 01:51:59 +00001519 const Value *CondVal = I.getCondition();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001520 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1521
1522 // If this is a series of conditions that are or'd or and'd together, emit
1523 // this as a sequence of branches instead of setcc's with and/or operations.
Chris Lattnerde189be2010-11-30 18:12:52 +00001524 // As long as jumps are not expensive, this should improve performance.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001525 // For example, instead of something like:
1526 // cmp A, B
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001527 // C = seteq
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001528 // cmp D, E
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001529 // F = setle
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001530 // or C, F
1531 // jnz foo
1532 // Emit:
1533 // cmp A, B
1534 // je foo
1535 // cmp D, E
1536 // jle foo
1537 //
Dan Gohman46510a72010-04-15 01:51:59 +00001538 if (const BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
Owen Anderson95771af2011-02-25 21:41:48 +00001539 if (!TLI.isJumpExpensive() &&
Chris Lattnerde189be2010-11-30 18:12:52 +00001540 BOp->hasOneUse() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001541 (BOp->getOpcode() == Instruction::And ||
1542 BOp->getOpcode() == Instruction::Or)) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001543 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, BrMBB, BrMBB,
1544 BOp->getOpcode());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001545 // If the compares in later blocks need to use values not currently
1546 // exported from this block, export them now. This block should always
1547 // be the first entry.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001548 assert(SwitchCases[0].ThisBB == BrMBB && "Unexpected lowering!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001549
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001550 // Allow some cases to be rejected.
1551 if (ShouldEmitAsBranches(SwitchCases)) {
1552 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1553 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1554 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1555 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001556
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001557 // Emit the branch for this block.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001558 visitSwitchCase(SwitchCases[0], BrMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001559 SwitchCases.erase(SwitchCases.begin());
1560 return;
1561 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001562
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001563 // Okay, we decided not to do this, remove any inserted MBB's and clear
1564 // SwitchCases.
1565 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001566 FuncInfo.MF->erase(SwitchCases[i].ThisBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001567
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001568 SwitchCases.clear();
1569 }
1570 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001571
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001572 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001573 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohman99be8ae2010-04-19 22:41:47 +00001574 NULL, Succ0MBB, Succ1MBB, BrMBB);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001575
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001576 // Use visitSwitchCase to actually insert the fast branch sequence for this
1577 // cond branch.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001578 visitSwitchCase(CB, BrMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001579}
1580
1581/// visitSwitchCase - Emits the necessary code to represent a single node in
1582/// the binary search tree resulting from lowering a switch instruction.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001583void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB,
1584 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001585 SDValue Cond;
1586 SDValue CondLHS = getValue(CB.CmpLHS);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001587 SDLoc dl = getCurSDLoc();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001588
1589 // Build the setcc now.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001590 if (CB.CmpMHS == NULL) {
1591 // Fold "(X == true)" to X and "(X == false)" to !X to
1592 // handle common cases produced by branch lowering.
Owen Anderson5defacc2009-07-31 17:39:07 +00001593 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001594 CB.CC == ISD::SETEQ)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001595 Cond = CondLHS;
Owen Anderson5defacc2009-07-31 17:39:07 +00001596 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001597 CB.CC == ISD::SETEQ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001598 SDValue True = DAG.getConstant(1, CondLHS.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001599 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001600 } else
Owen Anderson825b72b2009-08-11 20:47:22 +00001601 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001602 } else {
Stepan Dyatkovskiyc187df22012-05-17 08:56:30 +00001603 assert(CB.CC == ISD::SETCC_INVALID &&
1604 "Condition is undefined for to-the-range belonging check.");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001605
Anton Korobeynikov23218582008-12-23 22:25:27 +00001606 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
1607 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001608
1609 SDValue CmpOp = getValue(CB.CmpMHS);
Owen Andersone50ed302009-08-10 22:56:29 +00001610 EVT VT = CmpOp.getValueType();
Stepan Dyatkovskiyc187df22012-05-17 08:56:30 +00001611
1612 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(false)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001613 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT),
Stepan Dyatkovskiyc187df22012-05-17 08:56:30 +00001614 ISD::SETULE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001615 } else {
Dale Johannesenf5d97892009-02-04 01:48:28 +00001616 SDValue SUB = DAG.getNode(ISD::SUB, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001617 VT, CmpOp, DAG.getConstant(Low, VT));
Owen Anderson825b72b2009-08-11 20:47:22 +00001618 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001619 DAG.getConstant(High-Low, VT), ISD::SETULE);
1620 }
1621 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001622
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001623 // Update successor info
Jakub Staszakc8f34de2011-07-29 22:25:21 +00001624 addSuccessorWithWeight(SwitchBB, CB.TrueBB, CB.TrueWeight);
Jakob Stoklund Olesene7fdef42012-08-20 21:39:52 +00001625 // TrueBB and FalseBB are always different unless the incoming IR is
1626 // degenerate. This only happens when running llc on weird IR.
1627 if (CB.TrueBB != CB.FalseBB)
1628 addSuccessorWithWeight(SwitchBB, CB.FalseBB, CB.FalseWeight);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001629
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001630 // Set NextBlock to be the MBB immediately after the current one, if any.
1631 // This is used to avoid emitting unnecessary branches to the next block.
1632 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001633 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001634 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001635 NextBlock = BBI;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001636
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001637 // If the lhs block is the next block, invert the condition so that we can
1638 // fall through to the lhs instead of the rhs block.
1639 if (CB.TrueBB == NextBlock) {
1640 std::swap(CB.TrueBB, CB.FalseBB);
1641 SDValue True = DAG.getConstant(1, Cond.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001642 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001643 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001644
Dale Johannesenf5d97892009-02-04 01:48:28 +00001645 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001646 MVT::Other, getControlRoot(), Cond,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001647 DAG.getBasicBlock(CB.TrueBB));
Bill Wendling87710f02009-12-21 23:47:40 +00001648
Evan Cheng266a99d2010-09-23 06:51:55 +00001649 // Insert the false branch. Do this even if it's a fall through branch,
1650 // this makes it easier to do DAG optimizations which require inverting
1651 // the branch condition.
1652 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
1653 DAG.getBasicBlock(CB.FalseBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001654
1655 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001656}
1657
1658/// visitJumpTable - Emit JumpTable node in the current MBB
Dan Gohman2048b852009-11-23 18:04:58 +00001659void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001660 // Emit the code for the jump table
1661 assert(JT.Reg != -1U && "Should lower JT Header first!");
Owen Andersone50ed302009-08-10 22:56:29 +00001662 EVT PTy = TLI.getPointerTy();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001663 SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurSDLoc(),
Dale Johannesena04b7572009-02-03 23:04:43 +00001664 JT.Reg, PTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001665 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001666 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurSDLoc(),
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001667 MVT::Other, Index.getValue(1),
1668 Table, Index);
1669 DAG.setRoot(BrJumpTable);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001670}
1671
1672/// visitJumpTableHeader - This function emits necessary code to produce index
1673/// in the JumpTable from switch case.
Dan Gohman2048b852009-11-23 18:04:58 +00001674void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001675 JumpTableHeader &JTH,
1676 MachineBasicBlock *SwitchBB) {
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001677 // Subtract the lowest switch case value from the value being switched on and
1678 // conditional branch to default mbb if the result is greater than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001679 // difference between smallest and largest cases.
1680 SDValue SwitchOp = getValue(JTH.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001681 EVT VT = SwitchOp.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001682 SDValue Sub = DAG.getNode(ISD::SUB, getCurSDLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001683 DAG.getConstant(JTH.First, VT));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001684
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001685 // The SDNode we just created, which holds the value being switched on minus
Dan Gohmanf451cb82010-02-10 16:03:48 +00001686 // the smallest case value, needs to be copied to a virtual register so it
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001687 // can be used as an index into the jump table in a subsequent basic block.
1688 // This value may be smaller or larger than the target's pointer type, and
1689 // therefore require extension or truncating.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001690 SwitchOp = DAG.getZExtOrTrunc(Sub, getCurSDLoc(), TLI.getPointerTy());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001691
Dan Gohman89496d02010-07-02 00:10:16 +00001692 unsigned JumpTableReg = FuncInfo.CreateReg(TLI.getPointerTy());
Andrew Trickac6d9be2013-05-25 02:42:55 +00001693 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurSDLoc(),
Dale Johannesena04b7572009-02-03 23:04:43 +00001694 JumpTableReg, SwitchOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001695 JT.Reg = JumpTableReg;
1696
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001697 // Emit the range check for the jump table, and branch to the default block
1698 // for the switch statement if the value being switched on exceeds the largest
1699 // case in the switch.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001700 SDValue CMP = DAG.getSetCC(getCurSDLoc(),
Matt Arsenault225ed702013-05-18 00:21:46 +00001701 TLI.getSetCCResultType(*DAG.getContext(),
1702 Sub.getValueType()),
1703 Sub,
1704 DAG.getConstant(JTH.Last - JTH.First,VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001705 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001706
1707 // Set NextBlock to be the MBB immediately after the current one, if any.
1708 // This is used to avoid emitting unnecessary branches to the next block.
1709 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001710 MachineFunction::iterator BBI = SwitchBB;
Bill Wendling87710f02009-12-21 23:47:40 +00001711
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001712 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001713 NextBlock = BBI;
1714
Andrew Trickac6d9be2013-05-25 02:42:55 +00001715 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurSDLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001716 MVT::Other, CopyTo, CMP,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001717 DAG.getBasicBlock(JT.Default));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001718
Bill Wendling4533cac2010-01-28 21:51:40 +00001719 if (JT.MBB != NextBlock)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001720 BrCond = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, BrCond,
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001721 DAG.getBasicBlock(JT.MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001722
Bill Wendling87710f02009-12-21 23:47:40 +00001723 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001724}
1725
1726/// visitBitTestHeader - This function emits necessary code to produce value
1727/// suitable for "bit tests"
Dan Gohman99be8ae2010-04-19 22:41:47 +00001728void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B,
1729 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001730 // Subtract the minimum value
1731 SDValue SwitchOp = getValue(B.SValue);
Patrik Hagglund34525f92012-12-11 11:14:33 +00001732 EVT VT = SwitchOp.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001733 SDValue Sub = DAG.getNode(ISD::SUB, getCurSDLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001734 DAG.getConstant(B.First, VT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001735
1736 // Check range
Andrew Trickac6d9be2013-05-25 02:42:55 +00001737 SDValue RangeCmp = DAG.getSetCC(getCurSDLoc(),
Matt Arsenault225ed702013-05-18 00:21:46 +00001738 TLI.getSetCCResultType(*DAG.getContext(),
1739 Sub.getValueType()),
Bill Wendling87710f02009-12-21 23:47:40 +00001740 Sub, DAG.getConstant(B.Range, VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001741 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001742
Evan Chengd08e5b42011-01-06 01:02:44 +00001743 // Determine the type of the test operands.
1744 bool UsePtrType = false;
1745 if (!TLI.isTypeLegal(VT))
1746 UsePtrType = true;
1747 else {
1748 for (unsigned i = 0, e = B.Cases.size(); i != e; ++i)
Eli Friedman5c75af62011-10-12 22:46:45 +00001749 if (!isUIntN(VT.getSizeInBits(), B.Cases[i].Mask)) {
Evan Chengd08e5b42011-01-06 01:02:44 +00001750 // Switch table case range are encoded into series of masks.
1751 // Just use pointer type, it's guaranteed to fit.
1752 UsePtrType = true;
1753 break;
1754 }
1755 }
1756 if (UsePtrType) {
1757 VT = TLI.getPointerTy();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001758 Sub = DAG.getZExtOrTrunc(Sub, getCurSDLoc(), VT);
Evan Chengd08e5b42011-01-06 01:02:44 +00001759 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001760
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00001761 B.RegVT = VT.getSimpleVT();
Patrik Hagglund8963fec2012-12-19 12:23:01 +00001762 B.Reg = FuncInfo.CreateReg(B.RegVT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001763 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurSDLoc(),
Evan Chengd08e5b42011-01-06 01:02:44 +00001764 B.Reg, Sub);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001765
1766 // Set NextBlock to be the MBB immediately after the current one, if any.
1767 // This is used to avoid emitting unnecessary branches to the next block.
1768 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001769 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001770 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001771 NextBlock = BBI;
1772
1773 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1774
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001775 addSuccessorWithWeight(SwitchBB, B.Default);
1776 addSuccessorWithWeight(SwitchBB, MBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001777
Andrew Trickac6d9be2013-05-25 02:42:55 +00001778 SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurSDLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001779 MVT::Other, CopyTo, RangeCmp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001780 DAG.getBasicBlock(B.Default));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001781
Evan Cheng8c1f4322010-09-23 18:32:19 +00001782 if (MBB != NextBlock)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001783 BrRange = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, CopyTo,
Evan Cheng8c1f4322010-09-23 18:32:19 +00001784 DAG.getBasicBlock(MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001785
Bill Wendling87710f02009-12-21 23:47:40 +00001786 DAG.setRoot(BrRange);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001787}
1788
1789/// visitBitTestCase - this function produces one "bit test"
Evan Chengd08e5b42011-01-06 01:02:44 +00001790void SelectionDAGBuilder::visitBitTestCase(BitTestBlock &BB,
1791 MachineBasicBlock* NextMBB,
Manman Ren1a710fd2012-08-24 18:14:27 +00001792 uint32_t BranchWeightToNext,
Dan Gohman2048b852009-11-23 18:04:58 +00001793 unsigned Reg,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001794 BitTestCase &B,
1795 MachineBasicBlock *SwitchBB) {
Patrik Hagglund8963fec2012-12-19 12:23:01 +00001796 MVT VT = BB.RegVT;
Andrew Trickac6d9be2013-05-25 02:42:55 +00001797 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurSDLoc(),
Evan Chengd08e5b42011-01-06 01:02:44 +00001798 Reg, VT);
Dan Gohman8e0163a2010-06-24 02:06:24 +00001799 SDValue Cmp;
Benjamin Kramer3ff25512011-07-14 01:38:42 +00001800 unsigned PopCount = CountPopulation_64(B.Mask);
1801 if (PopCount == 1) {
Dan Gohman8e0163a2010-06-24 02:06:24 +00001802 // Testing for a single bit; just compare the shift count with what it
1803 // would need to be to shift a 1 bit in that position.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001804 Cmp = DAG.getSetCC(getCurSDLoc(),
Matt Arsenault225ed702013-05-18 00:21:46 +00001805 TLI.getSetCCResultType(*DAG.getContext(), VT),
Dan Gohman8e0163a2010-06-24 02:06:24 +00001806 ShiftOp,
Michael J. Spencerc6af2432013-05-24 22:23:49 +00001807 DAG.getConstant(countTrailingZeros(B.Mask), VT),
Dan Gohman8e0163a2010-06-24 02:06:24 +00001808 ISD::SETEQ);
Benjamin Kramer3ff25512011-07-14 01:38:42 +00001809 } else if (PopCount == BB.Range) {
1810 // There is only one zero bit in the range, test for it directly.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001811 Cmp = DAG.getSetCC(getCurSDLoc(),
Matt Arsenault225ed702013-05-18 00:21:46 +00001812 TLI.getSetCCResultType(*DAG.getContext(), VT),
Benjamin Kramer3ff25512011-07-14 01:38:42 +00001813 ShiftOp,
1814 DAG.getConstant(CountTrailingOnes_64(B.Mask), VT),
1815 ISD::SETNE);
Dan Gohman8e0163a2010-06-24 02:06:24 +00001816 } else {
1817 // Make desired shift
Andrew Trickac6d9be2013-05-25 02:42:55 +00001818 SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurSDLoc(), VT,
Evan Chengd08e5b42011-01-06 01:02:44 +00001819 DAG.getConstant(1, VT), ShiftOp);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001820
Dan Gohman8e0163a2010-06-24 02:06:24 +00001821 // Emit bit tests and jumps
Andrew Trickac6d9be2013-05-25 02:42:55 +00001822 SDValue AndOp = DAG.getNode(ISD::AND, getCurSDLoc(),
Evan Chengd08e5b42011-01-06 01:02:44 +00001823 VT, SwitchVal, DAG.getConstant(B.Mask, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00001824 Cmp = DAG.getSetCC(getCurSDLoc(),
Matt Arsenault225ed702013-05-18 00:21:46 +00001825 TLI.getSetCCResultType(*DAG.getContext(), VT),
Evan Chengd08e5b42011-01-06 01:02:44 +00001826 AndOp, DAG.getConstant(0, VT),
Dan Gohman8e0163a2010-06-24 02:06:24 +00001827 ISD::SETNE);
1828 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001829
Manman Ren1a710fd2012-08-24 18:14:27 +00001830 // The branch weight from SwitchBB to B.TargetBB is B.ExtraWeight.
1831 addSuccessorWithWeight(SwitchBB, B.TargetBB, B.ExtraWeight);
1832 // The branch weight from SwitchBB to NextMBB is BranchWeightToNext.
1833 addSuccessorWithWeight(SwitchBB, NextMBB, BranchWeightToNext);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001834
Andrew Trickac6d9be2013-05-25 02:42:55 +00001835 SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurSDLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001836 MVT::Other, getControlRoot(),
Dan Gohman8e0163a2010-06-24 02:06:24 +00001837 Cmp, DAG.getBasicBlock(B.TargetBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001838
1839 // Set NextBlock to be the MBB immediately after the current one, if any.
1840 // This is used to avoid emitting unnecessary branches to the next block.
1841 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001842 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001843 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001844 NextBlock = BBI;
1845
Evan Cheng8c1f4322010-09-23 18:32:19 +00001846 if (NextMBB != NextBlock)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001847 BrAnd = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, BrAnd,
Evan Cheng8c1f4322010-09-23 18:32:19 +00001848 DAG.getBasicBlock(NextMBB));
Bill Wendling0777e922009-12-21 21:59:52 +00001849
Bill Wendling87710f02009-12-21 23:47:40 +00001850 DAG.setRoot(BrAnd);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001851}
1852
Dan Gohman46510a72010-04-15 01:51:59 +00001853void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) {
Dan Gohman84023e02010-07-10 09:00:22 +00001854 MachineBasicBlock *InvokeMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001855
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001856 // Retrieve successors.
1857 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1858 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1859
Gabor Greifb67e6b32009-01-15 11:10:44 +00001860 const Value *Callee(I.getCalledValue());
Nuno Lopes85b40892012-06-28 22:30:12 +00001861 const Function *Fn = dyn_cast<Function>(Callee);
Gabor Greifb67e6b32009-01-15 11:10:44 +00001862 if (isa<InlineAsm>(Callee))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001863 visitInlineAsm(&I);
Nuno Lopes85b40892012-06-28 22:30:12 +00001864 else if (Fn && Fn->isIntrinsic()) {
1865 assert(Fn->getIntrinsicID() == Intrinsic::donothing);
Nuno Lopes4532bf62012-07-18 00:07:17 +00001866 // Ignore invokes to @llvm.donothing: jump directly to the next BB.
Nuno Lopes85b40892012-06-28 22:30:12 +00001867 } else
Gabor Greifb67e6b32009-01-15 11:10:44 +00001868 LowerCallTo(&I, getValue(Callee), false, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001869
1870 // If the value of the invoke is used outside of its defining block, make it
1871 // available as a virtual register.
Dan Gohmanad62f532009-04-23 23:13:24 +00001872 CopyToExportRegsIfNeeded(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001873
1874 // Update successor info
Chandler Carruthf2645682011-11-22 11:37:46 +00001875 addSuccessorWithWeight(InvokeMBB, Return);
1876 addSuccessorWithWeight(InvokeMBB, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001877
1878 // Drop into normal successor.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001879 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00001880 MVT::Other, getControlRoot(),
1881 DAG.getBasicBlock(Return)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001882}
1883
Bill Wendlingdccc03b2011-07-31 06:30:59 +00001884void SelectionDAGBuilder::visitResume(const ResumeInst &RI) {
1885 llvm_unreachable("SelectionDAGBuilder shouldn't visit resume instructions!");
1886}
1887
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00001888void SelectionDAGBuilder::visitLandingPad(const LandingPadInst &LP) {
1889 assert(FuncInfo.MBB->isLandingPad() &&
1890 "Call to landingpad not in landing pad!");
1891
1892 MachineBasicBlock *MBB = FuncInfo.MBB;
1893 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
1894 AddLandingPadInfo(LP, MMI, MBB);
1895
Bill Wendlingbdf9db62012-02-13 23:47:16 +00001896 // If there aren't registers to copy the values into (e.g., during SjLj
1897 // exceptions), then don't bother to create these DAG nodes.
Lang Hames07961342012-02-14 04:45:49 +00001898 if (TLI.getExceptionPointerRegister() == 0 &&
Bill Wendlingbdf9db62012-02-13 23:47:16 +00001899 TLI.getExceptionSelectorRegister() == 0)
1900 return;
1901
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00001902 SmallVector<EVT, 2> ValueVTs;
1903 ComputeValueVTs(TLI, LP.getType(), ValueVTs);
1904
1905 // Insert the EXCEPTIONADDR instruction.
1906 assert(FuncInfo.MBB->isLandingPad() &&
1907 "Call to eh.exception not in landing pad!");
1908 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
1909 SDValue Ops[2];
1910 Ops[0] = DAG.getRoot();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001911 SDValue Op1 = DAG.getNode(ISD::EXCEPTIONADDR, getCurSDLoc(), VTs, Ops, 1);
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00001912 SDValue Chain = Op1.getValue(1);
1913
1914 // Insert the EHSELECTION instruction.
1915 VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
1916 Ops[0] = Op1;
1917 Ops[1] = Chain;
Andrew Trickac6d9be2013-05-25 02:42:55 +00001918 SDValue Op2 = DAG.getNode(ISD::EHSELECTION, getCurSDLoc(), VTs, Ops, 2);
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00001919 Chain = Op2.getValue(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001920 Op2 = DAG.getSExtOrTrunc(Op2, getCurSDLoc(), MVT::i32);
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00001921
1922 Ops[0] = Op1;
1923 Ops[1] = Op2;
Andrew Trickac6d9be2013-05-25 02:42:55 +00001924 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00001925 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
1926 &Ops[0], 2);
1927
1928 std::pair<SDValue, SDValue> RetPair = std::make_pair(Res, Chain);
1929 setValue(&LP, RetPair.first);
1930 DAG.setRoot(RetPair.second);
1931}
1932
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001933/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
1934/// small case ranges).
Dan Gohman2048b852009-11-23 18:04:58 +00001935bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR,
1936 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001937 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001938 MachineBasicBlock *Default,
1939 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001940 // Size is the number of Cases represented by this range.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001941 size_t Size = CR.Range.second - CR.Range.first;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001942 if (Size > 3)
Anton Korobeynikov23218582008-12-23 22:25:27 +00001943 return false;
1944
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001945 // Get the MachineFunction which holds the current MBB. This is used when
1946 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001947 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001948
1949 // Figure out which block is immediately after the current one.
1950 MachineBasicBlock *NextBlock = 0;
1951 MachineFunction::iterator BBI = CR.CaseBB;
1952
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001953 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001954 NextBlock = BBI;
1955
Manman Ren1a710fd2012-08-24 18:14:27 +00001956 BranchProbabilityInfo *BPI = FuncInfo.BPI;
Benjamin Kramerce750f02010-11-22 09:45:38 +00001957 // If any two of the cases has the same destination, and if one value
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001958 // is the same as the other, but has one bit unset that the other has set,
1959 // use bit manipulation to do two compares at once. For example:
1960 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
Benjamin Kramerce750f02010-11-22 09:45:38 +00001961 // TODO: This could be extended to merge any 2 cases in switches with 3 cases.
1962 // TODO: Handle cases where CR.CaseBB != SwitchBB.
1963 if (Size == 2 && CR.CaseBB == SwitchBB) {
1964 Case &Small = *CR.Range.first;
1965 Case &Big = *(CR.Range.second-1);
1966
1967 if (Small.Low == Small.High && Big.Low == Big.High && Small.BB == Big.BB) {
1968 const APInt& SmallValue = cast<ConstantInt>(Small.Low)->getValue();
1969 const APInt& BigValue = cast<ConstantInt>(Big.Low)->getValue();
1970
1971 // Check that there is only one bit different.
1972 if (BigValue.countPopulation() == SmallValue.countPopulation() + 1 &&
1973 (SmallValue | BigValue) == BigValue) {
1974 // Isolate the common bit.
1975 APInt CommonBit = BigValue & ~SmallValue;
1976 assert((SmallValue | CommonBit) == BigValue &&
1977 CommonBit.countPopulation() == 1 && "Not a common bit?");
1978
1979 SDValue CondLHS = getValue(SV);
1980 EVT VT = CondLHS.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001981 SDLoc DL = getCurSDLoc();
Benjamin Kramerce750f02010-11-22 09:45:38 +00001982
1983 SDValue Or = DAG.getNode(ISD::OR, DL, VT, CondLHS,
1984 DAG.getConstant(CommonBit, VT));
1985 SDValue Cond = DAG.getSetCC(DL, MVT::i1,
1986 Or, DAG.getConstant(BigValue, VT),
1987 ISD::SETEQ);
1988
1989 // Update successor info.
Manman Ren1a710fd2012-08-24 18:14:27 +00001990 // Both Small and Big will jump to Small.BB, so we sum up the weights.
1991 addSuccessorWithWeight(SwitchBB, Small.BB,
1992 Small.ExtraWeight + Big.ExtraWeight);
1993 addSuccessorWithWeight(SwitchBB, Default,
1994 // The default destination is the first successor in IR.
1995 BPI ? BPI->getEdgeWeight(SwitchBB->getBasicBlock(), (unsigned)0) : 0);
Benjamin Kramerce750f02010-11-22 09:45:38 +00001996
1997 // Insert the true branch.
1998 SDValue BrCond = DAG.getNode(ISD::BRCOND, DL, MVT::Other,
1999 getControlRoot(), Cond,
2000 DAG.getBasicBlock(Small.BB));
2001
2002 // Insert the false branch.
2003 BrCond = DAG.getNode(ISD::BR, DL, MVT::Other, BrCond,
2004 DAG.getBasicBlock(Default));
2005
2006 DAG.setRoot(BrCond);
2007 return true;
2008 }
2009 }
2010 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002011
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002012 // Order cases by weight so the most likely case will be checked first.
Manman Ren1a710fd2012-08-24 18:14:27 +00002013 uint32_t UnhandledWeights = 0;
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002014 if (BPI) {
2015 for (CaseItr I = CR.Range.first, IE = CR.Range.second; I != IE; ++I) {
Manman Ren1a710fd2012-08-24 18:14:27 +00002016 uint32_t IWeight = I->ExtraWeight;
2017 UnhandledWeights += IWeight;
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002018 for (CaseItr J = CR.Range.first; J < I; ++J) {
Manman Ren1a710fd2012-08-24 18:14:27 +00002019 uint32_t JWeight = J->ExtraWeight;
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002020 if (IWeight > JWeight)
2021 std::swap(*I, *J);
2022 }
2023 }
2024 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002025 // Rearrange the case blocks so that the last one falls through if possible.
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002026 Case &BackCase = *(CR.Range.second-1);
Benjamin Kramer5db954d2012-05-26 21:19:12 +00002027 if (Size > 1 &&
2028 NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002029 // The last case block won't fall through into 'NextBlock' if we emit the
2030 // branches in this order. See if rearranging a case value would help.
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002031 // We start at the bottom as it's the case with the least weight.
Benjamin Kramercf1d69d2012-05-27 10:56:55 +00002032 for (Case *I = &*(CR.Range.second-2), *E = &*CR.Range.first-1; I != E; --I){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002033 if (I->BB == NextBlock) {
2034 std::swap(*I, BackCase);
2035 break;
2036 }
2037 }
2038 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002039
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002040 // Create a CaseBlock record representing a conditional branch to
2041 // the Case's target mbb if the value being switched on SV is equal
2042 // to C.
2043 MachineBasicBlock *CurBlock = CR.CaseBB;
2044 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
2045 MachineBasicBlock *FallThrough;
2046 if (I != E-1) {
2047 FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
2048 CurMF->insert(BBI, FallThrough);
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002049
2050 // Put SV in a virtual register to make it available from the new blocks.
2051 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002052 } else {
2053 // If the last case doesn't match, go to the default block.
2054 FallThrough = Default;
2055 }
2056
Dan Gohman46510a72010-04-15 01:51:59 +00002057 const Value *RHS, *LHS, *MHS;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002058 ISD::CondCode CC;
2059 if (I->High == I->Low) {
2060 // This is just small small case range :) containing exactly 1 case
2061 CC = ISD::SETEQ;
2062 LHS = SV; RHS = I->High; MHS = NULL;
2063 } else {
Stepan Dyatkovskiyc187df22012-05-17 08:56:30 +00002064 CC = ISD::SETCC_INVALID;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002065 LHS = I->Low; MHS = SV; RHS = I->High;
2066 }
Jakub Staszakc8f34de2011-07-29 22:25:21 +00002067
Manman Ren1a710fd2012-08-24 18:14:27 +00002068 // The false weight should be sum of all un-handled cases.
2069 UnhandledWeights -= I->ExtraWeight;
Jakub Staszakc8f34de2011-07-29 22:25:21 +00002070 CaseBlock CB(CC, LHS, RHS, MHS, /* truebb */ I->BB, /* falsebb */ FallThrough,
2071 /* me */ CurBlock,
Manman Ren1a710fd2012-08-24 18:14:27 +00002072 /* trueweight */ I->ExtraWeight,
2073 /* falseweight */ UnhandledWeights);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002074
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002075 // If emitting the first comparison, just call visitSwitchCase to emit the
2076 // code into the current block. Otherwise, push the CaseBlock onto the
2077 // vector to be later processed by SDISel, and insert the node's MBB
2078 // before the next MBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002079 if (CurBlock == SwitchBB)
2080 visitSwitchCase(CB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002081 else
2082 SwitchCases.push_back(CB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002083
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002084 CurBlock = FallThrough;
2085 }
2086
2087 return true;
2088}
2089
2090static inline bool areJTsAllowed(const TargetLowering &TLI) {
Evan Cheng769951f2012-07-02 22:39:56 +00002091 return TLI.supportJumpTables() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00002092 (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
2093 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002094}
Anton Korobeynikov23218582008-12-23 22:25:27 +00002095
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002096static APInt ComputeRange(const APInt &First, const APInt &Last) {
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002097 uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1;
Stepan Dyatkovskiyc187df22012-05-17 08:56:30 +00002098 APInt LastExt = Last.zext(BitWidth), FirstExt = First.zext(BitWidth);
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002099 return (LastExt - FirstExt + 1ULL);
2100}
2101
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002102/// handleJTSwitchCase - Emit jumptable for current switch case range
Chris Lattnerc3ab3882011-09-09 22:06:59 +00002103bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec &CR,
2104 CaseRecVector &WorkList,
2105 const Value *SV,
2106 MachineBasicBlock *Default,
Dan Gohman99be8ae2010-04-19 22:41:47 +00002107 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002108 Case& FrontCase = *CR.Range.first;
2109 Case& BackCase = *(CR.Range.second-1);
2110
Chris Lattnere880efe2009-11-07 07:50:34 +00002111 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
2112 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002113
Chris Lattnere880efe2009-11-07 07:50:34 +00002114 APInt TSize(First.getBitWidth(), 0);
Chris Lattnerc3ab3882011-09-09 22:06:59 +00002115 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002116 TSize += I->size();
2117
Sebastian Pop1a37d7e2012-09-25 20:35:36 +00002118 if (!areJTsAllowed(TLI) || TSize.ult(TLI.getMinimumJumpTableEntries()))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002119 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002120
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002121 APInt Range = ComputeRange(First, Last);
Jakob Stoklund Olesen79443912011-10-26 01:47:48 +00002122 // The density is TSize / Range. Require at least 40%.
2123 // It should not be possible for IntTSize to saturate for sane code, but make
2124 // sure we handle Range saturation correctly.
2125 uint64_t IntRange = Range.getLimitedValue(UINT64_MAX/10);
2126 uint64_t IntTSize = TSize.getLimitedValue(UINT64_MAX/10);
2127 if (IntTSize * 10 < IntRange * 4)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002128 return false;
2129
David Greene4b69d992010-01-05 01:24:57 +00002130 DEBUG(dbgs() << "Lowering jump table\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002131 << "First entry: " << First << ". Last entry: " << Last << '\n'
Jakob Stoklund Olesen79443912011-10-26 01:47:48 +00002132 << "Range: " << Range << ". Size: " << TSize << ".\n\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002133
2134 // Get the MachineFunction which holds the current MBB. This is used when
2135 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002136 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002137
2138 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002139 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00002140 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002141
2142 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
2143
2144 // Create a new basic block to hold the code for loading the address
2145 // of the jump table, and jumping to it. Update successor information;
2146 // we will either branch to the default case for the switch, or the jump
2147 // table.
2148 MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2149 CurMF->insert(BBI, JumpTableBB);
Jakub Staszak7cc2b072011-06-16 20:22:37 +00002150
2151 addSuccessorWithWeight(CR.CaseBB, Default);
2152 addSuccessorWithWeight(CR.CaseBB, JumpTableBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002153
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002154 // Build a vector of destination BBs, corresponding to each target
2155 // of the jump table. If the value of the jump table slot corresponds to
2156 // a case statement, push the case's BB onto the vector, otherwise, push
2157 // the default BB.
2158 std::vector<MachineBasicBlock*> DestBBs;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002159 APInt TEI = First;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002160 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
Chris Lattner071c62f2010-01-25 23:26:13 +00002161 const APInt &Low = cast<ConstantInt>(I->Low)->getValue();
2162 const APInt &High = cast<ConstantInt>(I->High)->getValue();
Anton Korobeynikov23218582008-12-23 22:25:27 +00002163
Stepan Dyatkovskiyc187df22012-05-17 08:56:30 +00002164 if (Low.ule(TEI) && TEI.ule(High)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002165 DestBBs.push_back(I->BB);
2166 if (TEI==High)
2167 ++I;
2168 } else {
2169 DestBBs.push_back(Default);
2170 }
2171 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002172
Manman Ren1a710fd2012-08-24 18:14:27 +00002173 // Calculate weight for each unique destination in CR.
2174 DenseMap<MachineBasicBlock*, uint32_t> DestWeights;
2175 if (FuncInfo.BPI)
2176 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
2177 DenseMap<MachineBasicBlock*, uint32_t>::iterator Itr =
2178 DestWeights.find(I->BB);
2179 if (Itr != DestWeights.end())
2180 Itr->second += I->ExtraWeight;
2181 else
2182 DestWeights[I->BB] = I->ExtraWeight;
2183 }
2184
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002185 // Update successor info. Add one edge to each unique successor.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002186 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
2187 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002188 E = DestBBs.end(); I != E; ++I) {
2189 if (!SuccsHandled[(*I)->getNumber()]) {
2190 SuccsHandled[(*I)->getNumber()] = true;
Manman Ren1a710fd2012-08-24 18:14:27 +00002191 DenseMap<MachineBasicBlock*, uint32_t>::iterator Itr =
2192 DestWeights.find(*I);
2193 addSuccessorWithWeight(JumpTableBB, *I,
2194 Itr != DestWeights.end() ? Itr->second : 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002195 }
2196 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002197
Bob Wilsond1ec31d2010-03-18 18:42:41 +00002198 // Create a jump table index for this jump table.
Chris Lattner071c62f2010-01-25 23:26:13 +00002199 unsigned JTEncoding = TLI.getJumpTableEncoding();
2200 unsigned JTI = CurMF->getOrCreateJumpTableInfo(JTEncoding)
Bob Wilsond1ec31d2010-03-18 18:42:41 +00002201 ->createJumpTableIndex(DestBBs);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002202
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002203 // Set the jump table information so that we can codegen it as a second
2204 // MachineBasicBlock
2205 JumpTable JT(-1U, JTI, JumpTableBB, Default);
Dan Gohman99be8ae2010-04-19 22:41:47 +00002206 JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == SwitchBB));
2207 if (CR.CaseBB == SwitchBB)
2208 visitJumpTableHeader(JT, JTH, SwitchBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002209
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002210 JTCases.push_back(JumpTableBlock(JTH, JT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002211 return true;
2212}
2213
2214/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
2215/// 2 subtrees.
Dan Gohman2048b852009-11-23 18:04:58 +00002216bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR,
2217 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00002218 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00002219 MachineBasicBlock *Default,
2220 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002221 // Get the MachineFunction which holds the current MBB. This is used when
2222 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002223 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002224
2225 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002226 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00002227 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002228
2229 Case& FrontCase = *CR.Range.first;
2230 Case& BackCase = *(CR.Range.second-1);
2231 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
2232
2233 // Size is the number of Cases represented by this range.
2234 unsigned Size = CR.Range.second - CR.Range.first;
2235
Chris Lattnere880efe2009-11-07 07:50:34 +00002236 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
2237 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002238 double FMetric = 0;
2239 CaseItr Pivot = CR.Range.first + Size/2;
2240
2241 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
2242 // (heuristically) allow us to emit JumpTable's later.
Chris Lattnere880efe2009-11-07 07:50:34 +00002243 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002244 for (CaseItr I = CR.Range.first, E = CR.Range.second;
2245 I!=E; ++I)
2246 TSize += I->size();
2247
Chris Lattnere880efe2009-11-07 07:50:34 +00002248 APInt LSize = FrontCase.size();
2249 APInt RSize = TSize-LSize;
David Greene4b69d992010-01-05 01:24:57 +00002250 DEBUG(dbgs() << "Selecting best pivot: \n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002251 << "First: " << First << ", Last: " << Last <<'\n'
2252 << "LSize: " << LSize << ", RSize: " << RSize << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002253 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
2254 J!=E; ++I, ++J) {
Chris Lattnere880efe2009-11-07 07:50:34 +00002255 const APInt &LEnd = cast<ConstantInt>(I->High)->getValue();
2256 const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002257 APInt Range = ComputeRange(LEnd, RBegin);
Stepan Dyatkovskiyc2c52a62012-05-15 06:50:18 +00002258 assert((Range - 2ULL).isNonNegative() &&
2259 "Invalid case distance");
Chris Lattnerc3e4e592011-04-09 06:57:13 +00002260 // Use volatile double here to avoid excess precision issues on some hosts,
2261 // e.g. that use 80-bit X87 registers.
2262 volatile double LDensity =
2263 (double)LSize.roundToDouble() /
Chris Lattnere880efe2009-11-07 07:50:34 +00002264 (LEnd - First + 1ULL).roundToDouble();
Chris Lattnerc3e4e592011-04-09 06:57:13 +00002265 volatile double RDensity =
2266 (double)RSize.roundToDouble() /
Chris Lattnere880efe2009-11-07 07:50:34 +00002267 (Last - RBegin + 1ULL).roundToDouble();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002268 double Metric = Range.logBase2()*(LDensity+RDensity);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002269 // Should always split in some non-trivial place
David Greene4b69d992010-01-05 01:24:57 +00002270 DEBUG(dbgs() <<"=>Step\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002271 << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n'
2272 << "LDensity: " << LDensity
2273 << ", RDensity: " << RDensity << '\n'
2274 << "Metric: " << Metric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002275 if (FMetric < Metric) {
2276 Pivot = J;
2277 FMetric = Metric;
David Greene4b69d992010-01-05 01:24:57 +00002278 DEBUG(dbgs() << "Current metric set to: " << FMetric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002279 }
2280
2281 LSize += J->size();
2282 RSize -= J->size();
2283 }
2284 if (areJTsAllowed(TLI)) {
2285 // If our case is dense we *really* should handle it earlier!
2286 assert((FMetric > 0) && "Should handle dense range earlier!");
2287 } else {
2288 Pivot = CR.Range.first + Size/2;
2289 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002290
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002291 CaseRange LHSR(CR.Range.first, Pivot);
2292 CaseRange RHSR(Pivot, CR.Range.second);
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00002293 const Constant *C = Pivot->Low;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002294 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002295
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002296 // We know that we branch to the LHS if the Value being switched on is
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002297 // less than the Pivot value, C. We use this to optimize our binary
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002298 // tree a bit, by recognizing that if SV is greater than or equal to the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002299 // LHS's Case Value, and that Case Value is exactly one less than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002300 // Pivot's Value, then we can branch directly to the LHS's Target,
2301 // rather than creating a leaf node for it.
2302 if ((LHSR.second - LHSR.first) == 1 &&
2303 LHSR.first->High == CR.GE &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00002304 cast<ConstantInt>(C)->getValue() ==
2305 (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002306 TrueBB = LHSR.first->BB;
2307 } else {
2308 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2309 CurMF->insert(BBI, TrueBB);
2310 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002311
2312 // Put SV in a virtual register to make it available from the new blocks.
2313 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002314 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002315
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002316 // Similar to the optimization above, if the Value being switched on is
2317 // known to be less than the Constant CR.LT, and the current Case Value
2318 // is CR.LT - 1, then we can branch directly to the target block for
2319 // the current Case Value, rather than emitting a RHS leaf node for it.
2320 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00002321 cast<ConstantInt>(RHSR.first->Low)->getValue() ==
2322 (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002323 FalseBB = RHSR.first->BB;
2324 } else {
2325 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2326 CurMF->insert(BBI, FalseBB);
2327 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002328
2329 // Put SV in a virtual register to make it available from the new blocks.
2330 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002331 }
2332
2333 // Create a CaseBlock record representing a conditional branch to
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002334 // the LHS node if the value being switched on SV is less than C.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002335 // Otherwise, branch to LHS.
Stepan Dyatkovskiyc187df22012-05-17 08:56:30 +00002336 CaseBlock CB(ISD::SETULT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002337
Dan Gohman99be8ae2010-04-19 22:41:47 +00002338 if (CR.CaseBB == SwitchBB)
2339 visitSwitchCase(CB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002340 else
2341 SwitchCases.push_back(CB);
2342
2343 return true;
2344}
2345
2346/// handleBitTestsSwitchCase - if current case range has few destination and
2347/// range span less, than machine word bitwidth, encode case range into series
2348/// of masks and emit bit tests with these masks.
Dan Gohman2048b852009-11-23 18:04:58 +00002349bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR,
2350 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00002351 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00002352 MachineBasicBlock* Default,
2353 MachineBasicBlock *SwitchBB){
Owen Andersone50ed302009-08-10 22:56:29 +00002354 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002355 unsigned IntPtrBits = PTy.getSizeInBits();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002356
2357 Case& FrontCase = *CR.Range.first;
2358 Case& BackCase = *(CR.Range.second-1);
2359
2360 // Get the MachineFunction which holds the current MBB. This is used when
2361 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002362 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002363
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00002364 // If target does not have legal shift left, do not emit bit tests at all.
2365 if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy()))
2366 return false;
2367
Anton Korobeynikov23218582008-12-23 22:25:27 +00002368 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002369 for (CaseItr I = CR.Range.first, E = CR.Range.second;
2370 I!=E; ++I) {
2371 // Single case counts one, case range - two.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002372 numCmps += (I->Low == I->High ? 1 : 2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002373 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002374
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002375 // Count unique destinations
2376 SmallSet<MachineBasicBlock*, 4> Dests;
2377 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
2378 Dests.insert(I->BB);
2379 if (Dests.size() > 3)
2380 // Don't bother the code below, if there are too much unique destinations
2381 return false;
2382 }
David Greene4b69d992010-01-05 01:24:57 +00002383 DEBUG(dbgs() << "Total number of unique destinations: "
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002384 << Dests.size() << '\n'
2385 << "Total number of comparisons: " << numCmps << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00002386
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002387 // Compute span of values.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002388 const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue();
2389 const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002390 APInt cmpRange = maxValue - minValue;
2391
David Greene4b69d992010-01-05 01:24:57 +00002392 DEBUG(dbgs() << "Compare range: " << cmpRange << '\n'
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002393 << "Low bound: " << minValue << '\n'
2394 << "High bound: " << maxValue << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00002395
Dan Gohmane0567812010-04-08 23:03:40 +00002396 if (cmpRange.uge(IntPtrBits) ||
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002397 (!(Dests.size() == 1 && numCmps >= 3) &&
2398 !(Dests.size() == 2 && numCmps >= 5) &&
2399 !(Dests.size() >= 3 && numCmps >= 6)))
2400 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002401
David Greene4b69d992010-01-05 01:24:57 +00002402 DEBUG(dbgs() << "Emitting bit tests\n");
Anton Korobeynikov23218582008-12-23 22:25:27 +00002403 APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth());
2404
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002405 // Optimize the case where all the case values fit in a
2406 // word without having to subtract minValue. In this case,
2407 // we can optimize away the subtraction.
Stepan Dyatkovskiyc187df22012-05-17 08:56:30 +00002408 if (maxValue.ult(IntPtrBits)) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002409 cmpRange = maxValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002410 } else {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002411 lowBound = minValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002412 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002413
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002414 CaseBitsVector CasesBits;
2415 unsigned i, count = 0;
2416
2417 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
2418 MachineBasicBlock* Dest = I->BB;
2419 for (i = 0; i < count; ++i)
2420 if (Dest == CasesBits[i].BB)
2421 break;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002422
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002423 if (i == count) {
2424 assert((count < 3) && "Too much destinations to test!");
Manman Ren1a710fd2012-08-24 18:14:27 +00002425 CasesBits.push_back(CaseBits(0, Dest, 0, 0/*Weight*/));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002426 count++;
2427 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002428
2429 const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue();
2430 const APInt& highValue = cast<ConstantInt>(I->High)->getValue();
2431
2432 uint64_t lo = (lowValue - lowBound).getZExtValue();
2433 uint64_t hi = (highValue - lowBound).getZExtValue();
Manman Ren1a710fd2012-08-24 18:14:27 +00002434 CasesBits[i].ExtraWeight += I->ExtraWeight;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002435
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002436 for (uint64_t j = lo; j <= hi; j++) {
2437 CasesBits[i].Mask |= 1ULL << j;
2438 CasesBits[i].Bits++;
2439 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002440
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002441 }
2442 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
Anton Korobeynikov23218582008-12-23 22:25:27 +00002443
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002444 BitTestInfo BTC;
2445
2446 // Figure out which block is immediately after the current one.
2447 MachineFunction::iterator BBI = CR.CaseBB;
2448 ++BBI;
2449
2450 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
2451
David Greene4b69d992010-01-05 01:24:57 +00002452 DEBUG(dbgs() << "Cases:\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002453 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
David Greene4b69d992010-01-05 01:24:57 +00002454 DEBUG(dbgs() << "Mask: " << CasesBits[i].Mask
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002455 << ", Bits: " << CasesBits[i].Bits
2456 << ", BB: " << CasesBits[i].BB << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002457
2458 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2459 CurMF->insert(BBI, CaseBB);
2460 BTC.push_back(BitTestCase(CasesBits[i].Mask,
2461 CaseBB,
Manman Ren1a710fd2012-08-24 18:14:27 +00002462 CasesBits[i].BB, CasesBits[i].ExtraWeight));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002463
2464 // Put SV in a virtual register to make it available from the new blocks.
2465 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002466 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002467
2468 BitTestBlock BTB(lowBound, cmpRange, SV,
Evan Chengd08e5b42011-01-06 01:02:44 +00002469 -1U, MVT::Other, (CR.CaseBB == SwitchBB),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002470 CR.CaseBB, Default, BTC);
2471
Dan Gohman99be8ae2010-04-19 22:41:47 +00002472 if (CR.CaseBB == SwitchBB)
2473 visitBitTestHeader(BTB, SwitchBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002474
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002475 BitTestCases.push_back(BTB);
2476
2477 return true;
2478}
2479
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002480/// Clusterify - Transform simple list of Cases into list of CaseRange's
Dan Gohman2048b852009-11-23 18:04:58 +00002481size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases,
2482 const SwitchInst& SI) {
Stepan Dyatkovskiy05cfe2e2012-05-18 08:32:28 +00002483
2484 /// Use a shorter form of declaration, and also
2485 /// show the we want to use CRSBuilder as Clusterifier.
Stepan Dyatkovskiy4319a552012-06-02 07:26:00 +00002486 typedef IntegersSubsetMapping<MachineBasicBlock> Clusterifier;
Stepan Dyatkovskiy05cfe2e2012-05-18 08:32:28 +00002487
2488 Clusterifier TheClusterifier;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002489
Manman Ren1a710fd2012-08-24 18:14:27 +00002490 BranchProbabilityInfo *BPI = FuncInfo.BPI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002491 // Start with "simple" cases
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002492 for (SwitchInst::ConstCaseIt i = SI.case_begin(), e = SI.case_end();
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002493 i != e; ++i) {
2494 const BasicBlock *SuccBB = i.getCaseSuccessor();
Jakub Staszakc8f34de2011-07-29 22:25:21 +00002495 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SuccBB];
2496
Manman Ren1a710fd2012-08-24 18:14:27 +00002497 TheClusterifier.add(i.getCaseValueEx(), SMBB,
2498 BPI ? BPI->getEdgeWeight(SI.getParent(), i.getSuccessorIndex()) : 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002499 }
Stepan Dyatkovskiy05cfe2e2012-05-18 08:32:28 +00002500
2501 TheClusterifier.optimize();
2502
Stepan Dyatkovskiy05cfe2e2012-05-18 08:32:28 +00002503 size_t numCmps = 0;
2504 for (Clusterifier::RangeIterator i = TheClusterifier.begin(),
2505 e = TheClusterifier.end(); i != e; ++i, ++numCmps) {
Stepan Dyatkovskiy66d79ce2012-07-04 05:53:05 +00002506 Clusterifier::Cluster &C = *i;
Manman Ren1a710fd2012-08-24 18:14:27 +00002507 // Update edge weight for the cluster.
2508 unsigned W = C.first.Weight;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002509
Stepan Dyatkovskiy484fc932012-05-28 12:39:09 +00002510 // FIXME: Currently work with ConstantInt based numbers.
2511 // Changing it to APInt based is a pretty heavy for this commit.
Stepan Dyatkovskiy66d79ce2012-07-04 05:53:05 +00002512 Cases.push_back(Case(C.first.getLow().toConstantInt(),
2513 C.first.getHigh().toConstantInt(), C.second, W));
Stepan Dyatkovskiy05cfe2e2012-05-18 08:32:28 +00002514
Stepan Dyatkovskiy66d79ce2012-07-04 05:53:05 +00002515 if (C.first.getLow() != C.first.getHigh())
Stepan Dyatkovskiy05cfe2e2012-05-18 08:32:28 +00002516 // A range counts double, since it requires two compares.
2517 ++numCmps;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002518 }
2519
2520 return numCmps;
2521}
2522
Jakob Stoklund Olesen2622f462010-09-30 19:44:31 +00002523void SelectionDAGBuilder::UpdateSplitBlock(MachineBasicBlock *First,
2524 MachineBasicBlock *Last) {
2525 // Update JTCases.
2526 for (unsigned i = 0, e = JTCases.size(); i != e; ++i)
2527 if (JTCases[i].first.HeaderBB == First)
2528 JTCases[i].first.HeaderBB = Last;
2529
2530 // Update BitTestCases.
2531 for (unsigned i = 0, e = BitTestCases.size(); i != e; ++i)
2532 if (BitTestCases[i].Parent == First)
2533 BitTestCases[i].Parent = Last;
2534}
2535
Dan Gohman46510a72010-04-15 01:51:59 +00002536void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) {
Dan Gohman84023e02010-07-10 09:00:22 +00002537 MachineBasicBlock *SwitchMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00002538
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002539 // Figure out which block is immediately after the current one.
2540 MachineBasicBlock *NextBlock = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002541 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
2542
2543 // If there is only the default destination, branch to it if it is not the
2544 // next basic block. Otherwise, just fall through.
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00002545 if (!SI.getNumCases()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002546 // Update machine-CFG edges.
2547
2548 // If this is not a fall-through branch, emit the branch.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002549 SwitchMBB->addSuccessor(Default);
Bill Wendling4533cac2010-01-28 21:51:40 +00002550 if (Default != NextBlock)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002551 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002552 MVT::Other, getControlRoot(),
2553 DAG.getBasicBlock(Default)));
Bill Wendling49fcff82009-12-21 22:30:11 +00002554
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002555 return;
2556 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002557
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002558 // If there are any non-default case statements, create a vector of Cases
2559 // representing each one, and sort the vector so that we can efficiently
2560 // create a binary search tree from them.
2561 CaseVector Cases;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002562 size_t numCmps = Clusterify(Cases, SI);
David Greene4b69d992010-01-05 01:24:57 +00002563 DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size()
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002564 << ". Total compares: " << numCmps << '\n');
Duncan Sands17001ce2011-10-18 12:44:00 +00002565 (void)numCmps;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002566
2567 // Get the Value to be switched on and default basic blocks, which will be
2568 // inserted into CaseBlock records, representing basic blocks in the binary
2569 // search tree.
Eli Friedmanbb5a7442011-09-29 20:21:17 +00002570 const Value *SV = SI.getCondition();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002571
2572 // Push the initial CaseRec onto the worklist
2573 CaseRecVector WorkList;
Dan Gohman99be8ae2010-04-19 22:41:47 +00002574 WorkList.push_back(CaseRec(SwitchMBB,0,0,
2575 CaseRange(Cases.begin(),Cases.end())));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002576
2577 while (!WorkList.empty()) {
2578 // Grab a record representing a case range to process off the worklist
2579 CaseRec CR = WorkList.back();
2580 WorkList.pop_back();
2581
Dan Gohman99be8ae2010-04-19 22:41:47 +00002582 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002583 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002584
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002585 // If the range has few cases (two or less) emit a series of specific
2586 // tests.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002587 if (handleSmallSwitchRange(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002588 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002589
Sebastian Pop1a37d7e2012-09-25 20:35:36 +00002590 // If the switch has more than N blocks, and is at least 40% dense, and the
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002591 // target supports indirect branches, then emit a jump table rather than
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002592 // lowering the switch to a binary tree of conditional branches.
Sebastian Pop1a37d7e2012-09-25 20:35:36 +00002593 // N defaults to 4 and is controlled via TLS.getMinimumJumpTableEntries().
Dan Gohman99be8ae2010-04-19 22:41:47 +00002594 if (handleJTSwitchCase(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002595 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002596
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002597 // Emit binary tree. We need to pick a pivot, and push left and right ranges
2598 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002599 handleBTSplitSwitchCase(CR, WorkList, SV, Default, SwitchMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002600 }
2601}
2602
Dan Gohman46510a72010-04-15 01:51:59 +00002603void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) {
Dan Gohman84023e02010-07-10 09:00:22 +00002604 MachineBasicBlock *IndirectBrMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00002605
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002606 // Update machine-CFG edges with unique successors.
Nadav Rotemee0ce152012-10-23 21:05:33 +00002607 SmallSet<BasicBlock*, 32> Done;
2608 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i) {
2609 BasicBlock *BB = I.getSuccessor(i);
2610 bool Inserted = Done.insert(BB);
2611 if (!Inserted)
2612 continue;
2613
2614 MachineBasicBlock *Succ = FuncInfo.MBBMap[BB];
Jakub Staszak7cc2b072011-06-16 20:22:37 +00002615 addSuccessorWithWeight(IndirectBrMBB, Succ);
2616 }
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002617
Andrew Trickac6d9be2013-05-25 02:42:55 +00002618 DAG.setRoot(DAG.getNode(ISD::BRIND, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002619 MVT::Other, getControlRoot(),
2620 getValue(I.getAddress())));
Bill Wendling49fcff82009-12-21 22:30:11 +00002621}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002622
Dan Gohman46510a72010-04-15 01:51:59 +00002623void SelectionDAGBuilder::visitFSub(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002624 // -0.0 - X --> fneg
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002625 Type *Ty = I.getType();
Chris Lattner2ca5c862011-02-15 00:14:00 +00002626 if (isa<Constant>(I.getOperand(0)) &&
2627 I.getOperand(0) == ConstantFP::getZeroValueForNegation(Ty)) {
2628 SDValue Op2 = getValue(I.getOperand(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00002629 setValue(&I, DAG.getNode(ISD::FNEG, getCurSDLoc(),
Chris Lattner2ca5c862011-02-15 00:14:00 +00002630 Op2.getValueType(), Op2));
2631 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002632 }
Bill Wendling49fcff82009-12-21 22:30:11 +00002633
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002634 visitBinary(I, ISD::FSUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002635}
2636
Dan Gohman46510a72010-04-15 01:51:59 +00002637void SelectionDAGBuilder::visitBinary(const User &I, unsigned OpCode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002638 SDValue Op1 = getValue(I.getOperand(0));
2639 SDValue Op2 = getValue(I.getOperand(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00002640 setValue(&I, DAG.getNode(OpCode, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002641 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002642}
2643
Dan Gohman46510a72010-04-15 01:51:59 +00002644void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002645 SDValue Op1 = getValue(I.getOperand(0));
2646 SDValue Op2 = getValue(I.getOperand(1));
Owen Anderson95771af2011-02-25 21:41:48 +00002647
Michael Liaoa6b20ce2013-03-01 18:40:30 +00002648 EVT ShiftTy = TLI.getShiftAmountTy(Op2.getValueType());
Owen Anderson95771af2011-02-25 21:41:48 +00002649
Chris Lattnerd3027732011-02-13 09:02:52 +00002650 // Coerce the shift amount to the right type if we can.
2651 if (!I.getType()->isVectorTy() && Op2.getValueType() != ShiftTy) {
Chris Lattner915eeb42011-02-13 09:10:56 +00002652 unsigned ShiftSize = ShiftTy.getSizeInBits();
2653 unsigned Op2Size = Op2.getValueType().getSizeInBits();
Andrew Trickac6d9be2013-05-25 02:42:55 +00002654 SDLoc DL = getCurSDLoc();
Owen Anderson95771af2011-02-25 21:41:48 +00002655
Dan Gohman57fc82d2009-04-09 03:51:29 +00002656 // If the operand is smaller than the shift count type, promote it.
Chris Lattnerd3027732011-02-13 09:02:52 +00002657 if (ShiftSize > Op2Size)
2658 Op2 = DAG.getNode(ISD::ZERO_EXTEND, DL, ShiftTy, Op2);
Owen Anderson95771af2011-02-25 21:41:48 +00002659
Dan Gohman57fc82d2009-04-09 03:51:29 +00002660 // If the operand is larger than the shift count type but the shift
2661 // count type has enough bits to represent any shift value, truncate
2662 // it now. This is a common case and it exposes the truncate to
2663 // optimization early.
Chris Lattnerd3027732011-02-13 09:02:52 +00002664 else if (ShiftSize >= Log2_32_Ceil(Op2.getValueType().getSizeInBits()))
2665 Op2 = DAG.getNode(ISD::TRUNCATE, DL, ShiftTy, Op2);
2666 // Otherwise we'll need to temporarily settle for some other convenient
Chris Lattnere0751182011-02-13 19:09:16 +00002667 // type. Type legalization will make adjustments once the shiftee is split.
Chris Lattnerd3027732011-02-13 09:02:52 +00002668 else
Chris Lattnere0751182011-02-13 19:09:16 +00002669 Op2 = DAG.getZExtOrTrunc(Op2, DL, MVT::i32);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002670 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002671
Andrew Trickac6d9be2013-05-25 02:42:55 +00002672 setValue(&I, DAG.getNode(Opcode, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002673 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002674}
2675
Benjamin Kramer9c640302011-07-08 10:31:30 +00002676void SelectionDAGBuilder::visitSDiv(const User &I) {
Benjamin Kramer9c640302011-07-08 10:31:30 +00002677 SDValue Op1 = getValue(I.getOperand(0));
2678 SDValue Op2 = getValue(I.getOperand(1));
2679
2680 // Turn exact SDivs into multiplications.
2681 // FIXME: This should be in DAGCombiner, but it doesn't have access to the
2682 // exact bit.
Benjamin Kramer3492a4a2011-07-08 12:08:24 +00002683 if (isa<BinaryOperator>(&I) && cast<BinaryOperator>(&I)->isExact() &&
2684 !isa<ConstantSDNode>(Op1) &&
Benjamin Kramer9c640302011-07-08 10:31:30 +00002685 isa<ConstantSDNode>(Op2) && !cast<ConstantSDNode>(Op2)->isNullValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00002686 setValue(&I, TLI.BuildExactSDIV(Op1, Op2, getCurSDLoc(), DAG));
Benjamin Kramer9c640302011-07-08 10:31:30 +00002687 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00002688 setValue(&I, DAG.getNode(ISD::SDIV, getCurSDLoc(), Op1.getValueType(),
Benjamin Kramer9c640302011-07-08 10:31:30 +00002689 Op1, Op2));
2690}
2691
Dan Gohman46510a72010-04-15 01:51:59 +00002692void SelectionDAGBuilder::visitICmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002693 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002694 if (const ICmpInst *IC = dyn_cast<ICmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002695 predicate = IC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002696 else if (const ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002697 predicate = ICmpInst::Predicate(IC->getPredicate());
2698 SDValue Op1 = getValue(I.getOperand(0));
2699 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002700 ISD::CondCode Opcode = getICmpCondCode(predicate);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002701
Owen Andersone50ed302009-08-10 22:56:29 +00002702 EVT DestVT = TLI.getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002703 setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Opcode));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002704}
2705
Dan Gohman46510a72010-04-15 01:51:59 +00002706void SelectionDAGBuilder::visitFCmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002707 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002708 if (const FCmpInst *FC = dyn_cast<FCmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002709 predicate = FC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002710 else if (const ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002711 predicate = FCmpInst::Predicate(FC->getPredicate());
2712 SDValue Op1 = getValue(I.getOperand(0));
2713 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002714 ISD::CondCode Condition = getFCmpCondCode(predicate);
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002715 if (TM.Options.NoNaNsFPMath)
2716 Condition = getFCmpCodeWithoutNaN(Condition);
Owen Andersone50ed302009-08-10 22:56:29 +00002717 EVT DestVT = TLI.getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002718 setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Condition));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002719}
2720
Dan Gohman46510a72010-04-15 01:51:59 +00002721void SelectionDAGBuilder::visitSelect(const User &I) {
Owen Andersone50ed302009-08-10 22:56:29 +00002722 SmallVector<EVT, 4> ValueVTs;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002723 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2724 unsigned NumValues = ValueVTs.size();
Bill Wendling49fcff82009-12-21 22:30:11 +00002725 if (NumValues == 0) return;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002726
Bill Wendling49fcff82009-12-21 22:30:11 +00002727 SmallVector<SDValue, 4> Values(NumValues);
2728 SDValue Cond = getValue(I.getOperand(0));
2729 SDValue TrueVal = getValue(I.getOperand(1));
2730 SDValue FalseVal = getValue(I.getOperand(2));
Duncan Sands28b77e92011-09-06 19:07:46 +00002731 ISD::NodeType OpCode = Cond.getValueType().isVector() ?
2732 ISD::VSELECT : ISD::SELECT;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002733
Bill Wendling4533cac2010-01-28 21:51:40 +00002734 for (unsigned i = 0; i != NumValues; ++i)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002735 Values[i] = DAG.getNode(OpCode, getCurSDLoc(),
Duncan Sands28b77e92011-09-06 19:07:46 +00002736 TrueVal.getNode()->getValueType(TrueVal.getResNo()+i),
Chris Lattnerb3e87b22010-03-12 07:15:36 +00002737 Cond,
Bill Wendling49fcff82009-12-21 22:30:11 +00002738 SDValue(TrueVal.getNode(),
2739 TrueVal.getResNo() + i),
2740 SDValue(FalseVal.getNode(),
2741 FalseVal.getResNo() + i));
2742
Andrew Trickac6d9be2013-05-25 02:42:55 +00002743 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002744 DAG.getVTList(&ValueVTs[0], NumValues),
2745 &Values[0], NumValues));
Bill Wendling49fcff82009-12-21 22:30:11 +00002746}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002747
Dan Gohman46510a72010-04-15 01:51:59 +00002748void SelectionDAGBuilder::visitTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002749 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2750 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002751 EVT DestVT = TLI.getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002752 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002753}
2754
Dan Gohman46510a72010-04-15 01:51:59 +00002755void SelectionDAGBuilder::visitZExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002756 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2757 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2758 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002759 EVT DestVT = TLI.getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002760 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002761}
2762
Dan Gohman46510a72010-04-15 01:51:59 +00002763void SelectionDAGBuilder::visitSExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002764 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2765 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2766 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002767 EVT DestVT = TLI.getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002768 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002769}
2770
Dan Gohman46510a72010-04-15 01:51:59 +00002771void SelectionDAGBuilder::visitFPTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002772 // FPTrunc is never a no-op cast, no need to check
2773 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002774 EVT DestVT = TLI.getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002775 setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurSDLoc(),
Pete Cooperf57e1c22012-01-17 01:54:07 +00002776 DestVT, N,
2777 DAG.getTargetConstant(0, TLI.getPointerTy())));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002778}
2779
Dan Gohman46510a72010-04-15 01:51:59 +00002780void SelectionDAGBuilder::visitFPExt(const User &I){
Hal Finkel46bb70c2011-10-18 03:51:57 +00002781 // FPExt is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002782 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002783 EVT DestVT = TLI.getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002784 setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002785}
2786
Dan Gohman46510a72010-04-15 01:51:59 +00002787void SelectionDAGBuilder::visitFPToUI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002788 // FPToUI is never a no-op cast, no need to check
2789 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002790 EVT DestVT = TLI.getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002791 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002792}
2793
Dan Gohman46510a72010-04-15 01:51:59 +00002794void SelectionDAGBuilder::visitFPToSI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002795 // FPToSI is never a no-op cast, no need to check
2796 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002797 EVT DestVT = TLI.getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002798 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002799}
2800
Dan Gohman46510a72010-04-15 01:51:59 +00002801void SelectionDAGBuilder::visitUIToFP(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002802 // UIToFP is never a no-op cast, no need to check
2803 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002804 EVT DestVT = TLI.getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002805 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002806}
2807
Dan Gohman46510a72010-04-15 01:51:59 +00002808void SelectionDAGBuilder::visitSIToFP(const User &I){
Bill Wendling181b6272008-10-19 20:34:04 +00002809 // SIToFP is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002810 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002811 EVT DestVT = TLI.getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002812 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002813}
2814
Dan Gohman46510a72010-04-15 01:51:59 +00002815void SelectionDAGBuilder::visitPtrToInt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002816 // What to do depends on the size of the integer and the size of the pointer.
2817 // We can either truncate, zero extend, or no-op, accordingly.
2818 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002819 EVT DestVT = TLI.getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002820 setValue(&I, DAG.getZExtOrTrunc(N, getCurSDLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002821}
2822
Dan Gohman46510a72010-04-15 01:51:59 +00002823void SelectionDAGBuilder::visitIntToPtr(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002824 // What to do depends on the size of the integer and the size of the pointer.
2825 // We can either truncate, zero extend, or no-op, accordingly.
2826 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002827 EVT DestVT = TLI.getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002828 setValue(&I, DAG.getZExtOrTrunc(N, getCurSDLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002829}
2830
Dan Gohman46510a72010-04-15 01:51:59 +00002831void SelectionDAGBuilder::visitBitCast(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002832 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002833 EVT DestVT = TLI.getValueType(I.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002834
Bill Wendling49fcff82009-12-21 22:30:11 +00002835 // BitCast assures us that source and destination are the same size so this is
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002836 // either a BITCAST or a no-op.
Bill Wendling4533cac2010-01-28 21:51:40 +00002837 if (DestVT != N.getValueType())
Andrew Trickac6d9be2013-05-25 02:42:55 +00002838 setValue(&I, DAG.getNode(ISD::BITCAST, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002839 DestVT, N)); // convert types.
2840 else
Bill Wendling49fcff82009-12-21 22:30:11 +00002841 setValue(&I, N); // noop cast.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002842}
2843
Dan Gohman46510a72010-04-15 01:51:59 +00002844void SelectionDAGBuilder::visitInsertElement(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002845 SDValue InVec = getValue(I.getOperand(0));
2846 SDValue InVal = getValue(I.getOperand(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00002847 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurSDLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002848 TLI.getPointerTy(),
2849 getValue(I.getOperand(2)));
Andrew Trickac6d9be2013-05-25 02:42:55 +00002850 setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002851 TLI.getValueType(I.getType()),
2852 InVec, InVal, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002853}
2854
Dan Gohman46510a72010-04-15 01:51:59 +00002855void SelectionDAGBuilder::visitExtractElement(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002856 SDValue InVec = getValue(I.getOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00002857 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurSDLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002858 TLI.getPointerTy(),
2859 getValue(I.getOperand(1)));
Andrew Trickac6d9be2013-05-25 02:42:55 +00002860 setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002861 TLI.getValueType(I.getType()), InVec, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002862}
2863
Craig Topper51578342012-01-04 09:23:09 +00002864// Utility for visitShuffleVector - Return true if every element in Mask,
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00002865// beginning from position Pos and ending in Pos+Size, falls within the
Craig Topper51578342012-01-04 09:23:09 +00002866// specified sequential range [L, L+Pos). or is undef.
2867static bool isSequentialInRange(const SmallVectorImpl<int> &Mask,
Craig Topper23de31b2012-04-11 03:06:35 +00002868 unsigned Pos, unsigned Size, int Low) {
2869 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Craig Topper51578342012-01-04 09:23:09 +00002870 if (Mask[i] >= 0 && Mask[i] != Low)
Nate Begeman9008ca62009-04-27 18:41:29 +00002871 return false;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002872 return true;
2873}
2874
Dan Gohman46510a72010-04-15 01:51:59 +00002875void SelectionDAGBuilder::visitShuffleVector(const User &I) {
Mon P Wang230e4fa2008-11-21 04:25:21 +00002876 SDValue Src1 = getValue(I.getOperand(0));
2877 SDValue Src2 = getValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002878
Chris Lattner56243b82012-01-26 02:51:13 +00002879 SmallVector<int, 8> Mask;
2880 ShuffleVectorInst::getShuffleMask(cast<Constant>(I.getOperand(2)), Mask);
2881 unsigned MaskNumElts = Mask.size();
2882
Owen Andersone50ed302009-08-10 22:56:29 +00002883 EVT VT = TLI.getValueType(I.getType());
2884 EVT SrcVT = Src1.getValueType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00002885 unsigned SrcNumElts = SrcVT.getVectorNumElements();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002886
Mon P Wangc7849c22008-11-16 05:06:27 +00002887 if (SrcNumElts == MaskNumElts) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002888 setValue(&I, DAG.getVectorShuffle(VT, getCurSDLoc(), Src1, Src2,
Bill Wendling4533cac2010-01-28 21:51:40 +00002889 &Mask[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002890 return;
2891 }
2892
2893 // Normalize the shuffle vector since mask and vector length don't match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002894 if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) {
2895 // Mask is longer than the source vectors and is a multiple of the source
2896 // vectors. We can use concatenate vector to make the mask and vectors
Mon P Wang230e4fa2008-11-21 04:25:21 +00002897 // lengths match.
Craig Topper51578342012-01-04 09:23:09 +00002898 if (SrcNumElts*2 == MaskNumElts) {
2899 // First check for Src1 in low and Src2 in high
2900 if (isSequentialInRange(Mask, 0, SrcNumElts, 0) &&
2901 isSequentialInRange(Mask, SrcNumElts, SrcNumElts, SrcNumElts)) {
2902 // The shuffle is concatenating two vectors together.
Andrew Trickac6d9be2013-05-25 02:42:55 +00002903 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurSDLoc(),
Craig Topper51578342012-01-04 09:23:09 +00002904 VT, Src1, Src2));
2905 return;
2906 }
2907 // Then check for Src2 in low and Src1 in high
2908 if (isSequentialInRange(Mask, 0, SrcNumElts, SrcNumElts) &&
2909 isSequentialInRange(Mask, SrcNumElts, SrcNumElts, 0)) {
2910 // The shuffle is concatenating two vectors together.
Andrew Trickac6d9be2013-05-25 02:42:55 +00002911 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurSDLoc(),
Craig Topper51578342012-01-04 09:23:09 +00002912 VT, Src2, Src1));
2913 return;
2914 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002915 }
2916
Mon P Wangc7849c22008-11-16 05:06:27 +00002917 // Pad both vectors with undefs to make them the same length as the mask.
2918 unsigned NumConcat = MaskNumElts / SrcNumElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00002919 bool Src1U = Src1.getOpcode() == ISD::UNDEF;
2920 bool Src2U = Src2.getOpcode() == ISD::UNDEF;
Dale Johannesene8d72302009-02-06 23:05:02 +00002921 SDValue UndefVal = DAG.getUNDEF(SrcVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002922
Nate Begeman9008ca62009-04-27 18:41:29 +00002923 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
2924 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002925 MOps1[0] = Src1;
2926 MOps2[0] = Src2;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002927
2928 Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002929 getCurSDLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002930 &MOps1[0], NumConcat);
2931 Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002932 getCurSDLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002933 &MOps2[0], NumConcat);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002934
Mon P Wangaeb06d22008-11-10 04:46:22 +00002935 // Readjust mask for new input vector length.
Nate Begeman9008ca62009-04-27 18:41:29 +00002936 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002937 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002938 int Idx = Mask[i];
Craig Topper23de31b2012-04-11 03:06:35 +00002939 if (Idx >= (int)SrcNumElts)
2940 Idx -= SrcNumElts - MaskNumElts;
2941 MappedOps.push_back(Idx);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002942 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002943
Andrew Trickac6d9be2013-05-25 02:42:55 +00002944 setValue(&I, DAG.getVectorShuffle(VT, getCurSDLoc(), Src1, Src2,
Bill Wendling4533cac2010-01-28 21:51:40 +00002945 &MappedOps[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002946 return;
2947 }
2948
Mon P Wangc7849c22008-11-16 05:06:27 +00002949 if (SrcNumElts > MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002950 // Analyze the access pattern of the vector to see if we can extract
2951 // two subvectors and do the shuffle. The analysis is done by calculating
2952 // the range of elements the mask access on both vectors.
Craig Topper10612dc2012-04-08 23:15:04 +00002953 int MinRange[2] = { static_cast<int>(SrcNumElts),
2954 static_cast<int>(SrcNumElts)};
Mon P Wangc7849c22008-11-16 05:06:27 +00002955 int MaxRange[2] = {-1, -1};
2956
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 Topper10612dc2012-04-08 23:15:04 +00002959 unsigned Input = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00002960 if (Idx < 0)
2961 continue;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002962
Nate Begeman5a5ca152009-04-29 05:20:52 +00002963 if (Idx >= (int)SrcNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002964 Input = 1;
2965 Idx -= SrcNumElts;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002966 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002967 if (Idx > MaxRange[Input])
2968 MaxRange[Input] = Idx;
2969 if (Idx < MinRange[Input])
2970 MinRange[Input] = Idx;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002971 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002972
Mon P Wangc7849c22008-11-16 05:06:27 +00002973 // Check if the access is smaller than the vector size and can we find
2974 // a reasonable extract index.
Craig Topper10612dc2012-04-08 23:15:04 +00002975 int RangeUse[2] = { -1, -1 }; // 0 = Unused, 1 = Extract, -1 = Can not
2976 // Extract.
Mon P Wangc7849c22008-11-16 05:06:27 +00002977 int StartIdx[2]; // StartIdx to extract from
Craig Topper10612dc2012-04-08 23:15:04 +00002978 for (unsigned Input = 0; Input < 2; ++Input) {
2979 if (MinRange[Input] >= (int)SrcNumElts && MaxRange[Input] < 0) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002980 RangeUse[Input] = 0; // Unused
2981 StartIdx[Input] = 0;
Craig Topperf873dde2012-04-08 17:53:33 +00002982 continue;
Mon P Wang230e4fa2008-11-21 04:25:21 +00002983 }
Craig Topperf873dde2012-04-08 17:53:33 +00002984
2985 // Find a good start index that is a multiple of the mask length. Then
2986 // see if the rest of the elements are in range.
2987 StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts;
2988 if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts &&
2989 StartIdx[Input] + MaskNumElts <= SrcNumElts)
2990 RangeUse[Input] = 1; // Extract from a multiple of the mask length.
Mon P Wangc7849c22008-11-16 05:06:27 +00002991 }
2992
Bill Wendling636e2582009-08-21 18:16:06 +00002993 if (RangeUse[0] == 0 && RangeUse[1] == 0) {
Bill Wendling4533cac2010-01-28 21:51:40 +00002994 setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
Mon P Wangc7849c22008-11-16 05:06:27 +00002995 return;
2996 }
Craig Topper10612dc2012-04-08 23:15:04 +00002997 if (RangeUse[0] >= 0 && RangeUse[1] >= 0) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002998 // Extract appropriate subvector and generate a vector shuffle
Craig Topper10612dc2012-04-08 23:15:04 +00002999 for (unsigned Input = 0; Input < 2; ++Input) {
Bill Wendling87710f02009-12-21 23:47:40 +00003000 SDValue &Src = Input == 0 ? Src1 : Src2;
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003001 if (RangeUse[Input] == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00003002 Src = DAG.getUNDEF(VT);
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003003 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00003004 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurSDLoc(), VT,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003005 Src, DAG.getIntPtrConstant(StartIdx[Input]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00003006 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003007
Mon P Wangc7849c22008-11-16 05:06:27 +00003008 // Calculate new mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00003009 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003010 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003011 int Idx = Mask[i];
Craig Topper23de31b2012-04-11 03:06:35 +00003012 if (Idx >= 0) {
3013 if (Idx < (int)SrcNumElts)
3014 Idx -= StartIdx[0];
3015 else
3016 Idx -= SrcNumElts + StartIdx[1] - MaskNumElts;
3017 }
3018 MappedOps.push_back(Idx);
Mon P Wangc7849c22008-11-16 05:06:27 +00003019 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003020
Andrew Trickac6d9be2013-05-25 02:42:55 +00003021 setValue(&I, DAG.getVectorShuffle(VT, getCurSDLoc(), Src1, Src2,
Bill Wendling4533cac2010-01-28 21:51:40 +00003022 &MappedOps[0]));
Mon P Wangc7849c22008-11-16 05:06:27 +00003023 return;
Mon P Wangaeb06d22008-11-10 04:46:22 +00003024 }
3025 }
3026
Mon P Wangc7849c22008-11-16 05:06:27 +00003027 // We can't use either concat vectors or extract subvectors so fall back to
3028 // replacing the shuffle with extract and build vector.
3029 // to insert and build vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003030 EVT EltVT = VT.getVectorElementType();
3031 EVT PtrVT = TLI.getPointerTy();
Mon P Wangaeb06d22008-11-10 04:46:22 +00003032 SmallVector<SDValue,8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003033 for (unsigned i = 0; i != MaskNumElts; ++i) {
Craig Topper23de31b2012-04-11 03:06:35 +00003034 int Idx = Mask[i];
3035 SDValue Res;
3036
3037 if (Idx < 0) {
3038 Res = DAG.getUNDEF(EltVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00003039 } else {
Craig Topper23de31b2012-04-11 03:06:35 +00003040 SDValue &Src = Idx < (int)SrcNumElts ? Src1 : Src2;
3041 if (Idx >= (int)SrcNumElts) Idx -= SrcNumElts;
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003042
Andrew Trickac6d9be2013-05-25 02:42:55 +00003043 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurSDLoc(),
Craig Topper23de31b2012-04-11 03:06:35 +00003044 EltVT, Src, DAG.getConstant(Idx, PtrVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00003045 }
Craig Topper23de31b2012-04-11 03:06:35 +00003046
3047 Ops.push_back(Res);
Mon P Wangaeb06d22008-11-10 04:46:22 +00003048 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003049
Andrew Trickac6d9be2013-05-25 02:42:55 +00003050 setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00003051 VT, &Ops[0], Ops.size()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003052}
3053
Dan Gohman46510a72010-04-15 01:51:59 +00003054void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003055 const Value *Op0 = I.getOperand(0);
3056 const Value *Op1 = I.getOperand(1);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003057 Type *AggTy = I.getType();
3058 Type *ValTy = Op1->getType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003059 bool IntoUndef = isa<UndefValue>(Op0);
3060 bool FromUndef = isa<UndefValue>(Op1);
3061
Jay Foadfc6d3a42011-07-13 10:26:04 +00003062 unsigned LinearIndex = ComputeLinearIndex(AggTy, I.getIndices());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003063
Owen Andersone50ed302009-08-10 22:56:29 +00003064 SmallVector<EVT, 4> AggValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003065 ComputeValueVTs(TLI, AggTy, AggValueVTs);
Owen Andersone50ed302009-08-10 22:56:29 +00003066 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003067 ComputeValueVTs(TLI, ValTy, ValValueVTs);
3068
3069 unsigned NumAggValues = AggValueVTs.size();
3070 unsigned NumValValues = ValValueVTs.size();
3071 SmallVector<SDValue, 4> Values(NumAggValues);
3072
3073 SDValue Agg = getValue(Op0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003074 unsigned i = 0;
3075 // Copy the beginning value(s) from the original aggregate.
3076 for (; i != LinearIndex; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00003077 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003078 SDValue(Agg.getNode(), Agg.getResNo() + i);
3079 // Copy values from the inserted value(s).
Rafael Espindola3fa82832011-05-13 15:18:06 +00003080 if (NumValValues) {
3081 SDValue Val = getValue(Op1);
3082 for (; i != LinearIndex + NumValValues; ++i)
3083 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
3084 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
3085 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003086 // Copy remaining value(s) from the original aggregate.
3087 for (; i != NumAggValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00003088 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003089 SDValue(Agg.getNode(), Agg.getResNo() + i);
3090
Andrew Trickac6d9be2013-05-25 02:42:55 +00003091 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00003092 DAG.getVTList(&AggValueVTs[0], NumAggValues),
3093 &Values[0], NumAggValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003094}
3095
Dan Gohman46510a72010-04-15 01:51:59 +00003096void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003097 const Value *Op0 = I.getOperand(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003098 Type *AggTy = Op0->getType();
3099 Type *ValTy = I.getType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003100 bool OutOfUndef = isa<UndefValue>(Op0);
3101
Jay Foadfc6d3a42011-07-13 10:26:04 +00003102 unsigned LinearIndex = ComputeLinearIndex(AggTy, I.getIndices());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003103
Owen Andersone50ed302009-08-10 22:56:29 +00003104 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003105 ComputeValueVTs(TLI, ValTy, ValValueVTs);
3106
3107 unsigned NumValValues = ValValueVTs.size();
Rafael Espindola3fa82832011-05-13 15:18:06 +00003108
3109 // Ignore a extractvalue that produces an empty object
3110 if (!NumValValues) {
3111 setValue(&I, DAG.getUNDEF(MVT(MVT::Other)));
3112 return;
3113 }
3114
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003115 SmallVector<SDValue, 4> Values(NumValValues);
3116
3117 SDValue Agg = getValue(Op0);
3118 // Copy out the selected value(s).
3119 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
3120 Values[i - LinearIndex] =
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00003121 OutOfUndef ?
Dale Johannesene8d72302009-02-06 23:05:02 +00003122 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00003123 SDValue(Agg.getNode(), Agg.getResNo() + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003124
Andrew Trickac6d9be2013-05-25 02:42:55 +00003125 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00003126 DAG.getVTList(&ValValueVTs[0], NumValValues),
3127 &Values[0], NumValValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003128}
3129
Dan Gohman46510a72010-04-15 01:51:59 +00003130void SelectionDAGBuilder::visitGetElementPtr(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003131 SDValue N = getValue(I.getOperand(0));
Nadav Rotem1c239202012-02-28 14:13:19 +00003132 // Note that the pointer operand may be a vector of pointers. Take the scalar
3133 // element which holds a pointer.
3134 Type *Ty = I.getOperand(0)->getType()->getScalarType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003135
Dan Gohman46510a72010-04-15 01:51:59 +00003136 for (GetElementPtrInst::const_op_iterator OI = I.op_begin()+1, E = I.op_end();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003137 OI != E; ++OI) {
Dan Gohman46510a72010-04-15 01:51:59 +00003138 const Value *Idx = *OI;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003139 if (StructType *StTy = dyn_cast<StructType>(Ty)) {
Duncan Sandsb2df01a2012-11-13 13:01:58 +00003140 unsigned Field = cast<Constant>(Idx)->getUniqueInteger().getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003141 if (Field) {
3142 // N = N + Offset
3143 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003144 N = DAG.getNode(ISD::ADD, getCurSDLoc(), N.getValueType(), N,
Duncan Sandsb2df01a2012-11-13 13:01:58 +00003145 DAG.getConstant(Offset, N.getValueType()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003146 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00003147
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003148 Ty = StTy->getElementType(Field);
3149 } else {
3150 Ty = cast<SequentialType>(Ty)->getElementType();
3151
3152 // If this is a constant subscript, handle it quickly.
Dan Gohman46510a72010-04-15 01:51:59 +00003153 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Dan Gohmane368b462010-06-18 14:22:04 +00003154 if (CI->isZero()) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003155 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +00003156 TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Evan Cheng65b52df2009-02-09 21:01:06 +00003157 SDValue OffsVal;
Owen Andersone50ed302009-08-10 22:56:29 +00003158 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00003159 unsigned PtrBits = PTy.getSizeInBits();
Bill Wendlinge1a90422009-12-21 23:10:19 +00003160 if (PtrBits < 64)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003161 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(),
Evan Cheng65b52df2009-02-09 21:01:06 +00003162 TLI.getPointerTy(),
Owen Anderson825b72b2009-08-11 20:47:22 +00003163 DAG.getConstant(Offs, MVT::i64));
Bill Wendlinge1a90422009-12-21 23:10:19 +00003164 else
Evan Chengb1032a82009-02-09 20:54:38 +00003165 OffsVal = DAG.getIntPtrConstant(Offs);
Bill Wendlinge1a90422009-12-21 23:10:19 +00003166
Andrew Trickac6d9be2013-05-25 02:42:55 +00003167 N = DAG.getNode(ISD::ADD, getCurSDLoc(), N.getValueType(), N,
Evan Chengb1032a82009-02-09 20:54:38 +00003168 OffsVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003169 continue;
3170 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003171
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003172 // N = N + Idx * ElementSize;
Dan Gohman7abbd042009-10-23 17:57:43 +00003173 APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(),
3174 TD->getTypeAllocSize(Ty));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003175 SDValue IdxN = getValue(Idx);
3176
3177 // If the index is smaller or larger than intptr_t, truncate or extend
3178 // it.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003179 IdxN = DAG.getSExtOrTrunc(IdxN, getCurSDLoc(), N.getValueType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003180
3181 // If this is a multiply by a power of two, turn it into a shl
3182 // immediately. This is a very common case.
3183 if (ElementSize != 1) {
Dan Gohman7abbd042009-10-23 17:57:43 +00003184 if (ElementSize.isPowerOf2()) {
3185 unsigned Amt = ElementSize.logBase2();
Andrew Trickac6d9be2013-05-25 02:42:55 +00003186 IdxN = DAG.getNode(ISD::SHL, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003187 N.getValueType(), IdxN,
Nadav Rotem16087692011-12-05 06:29:09 +00003188 DAG.getConstant(Amt, IdxN.getValueType()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003189 } else {
Duncan Sandsb2df01a2012-11-13 13:01:58 +00003190 SDValue Scale = DAG.getConstant(ElementSize, IdxN.getValueType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003191 IdxN = DAG.getNode(ISD::MUL, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003192 N.getValueType(), IdxN, Scale);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003193 }
3194 }
3195
Andrew Trickac6d9be2013-05-25 02:42:55 +00003196 N = DAG.getNode(ISD::ADD, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003197 N.getValueType(), N, IdxN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003198 }
3199 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00003200
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003201 setValue(&I, N);
3202}
3203
Dan Gohman46510a72010-04-15 01:51:59 +00003204void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003205 // If this is a fixed sized alloca in the entry block of the function,
3206 // allocate it statically on the stack.
3207 if (FuncInfo.StaticAllocaMap.count(&I))
3208 return; // getValue will auto-populate this.
3209
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003210 Type *Ty = I.getAllocatedType();
Micah Villmow3574eca2012-10-08 16:38:25 +00003211 uint64_t TySize = TLI.getDataLayout()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003212 unsigned Align =
Micah Villmow3574eca2012-10-08 16:38:25 +00003213 std::max((unsigned)TLI.getDataLayout()->getPrefTypeAlignment(Ty),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003214 I.getAlignment());
3215
3216 SDValue AllocSize = getValue(I.getArraySize());
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003217
Owen Andersone50ed302009-08-10 22:56:29 +00003218 EVT IntPtr = TLI.getPointerTy();
Dan Gohmanf75a7d32010-05-28 01:14:11 +00003219 if (AllocSize.getValueType() != IntPtr)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003220 AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurSDLoc(), IntPtr);
Dan Gohmanf75a7d32010-05-28 01:14:11 +00003221
Andrew Trickac6d9be2013-05-25 02:42:55 +00003222 AllocSize = DAG.getNode(ISD::MUL, getCurSDLoc(), IntPtr,
Dan Gohmanf75a7d32010-05-28 01:14:11 +00003223 AllocSize,
3224 DAG.getConstant(TySize, IntPtr));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003225
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003226 // Handle alignment. If the requested alignment is less than or equal to
3227 // the stack alignment, ignore it. If the size is greater than or equal to
3228 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00003229 unsigned StackAlign = TM.getFrameLowering()->getStackAlignment();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003230 if (Align <= StackAlign)
3231 Align = 0;
3232
3233 // Round the size of the allocation up to the stack alignment size
3234 // by add SA-1 to the size.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003235 AllocSize = DAG.getNode(ISD::ADD, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003236 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003237 DAG.getIntPtrConstant(StackAlign-1));
Bill Wendling856ff412009-12-22 00:12:37 +00003238
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003239 // Mask out the low bits for alignment purposes.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003240 AllocSize = DAG.getNode(ISD::AND, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003241 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003242 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
3243
3244 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Owen Anderson825b72b2009-08-11 20:47:22 +00003245 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003246 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurSDLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003247 VTs, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003248 setValue(&I, DSA);
3249 DAG.setRoot(DSA.getValue(1));
Bill Wendling856ff412009-12-22 00:12:37 +00003250
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003251 // Inform the Frame Information that we have just allocated a variable-sized
3252 // object.
Bob Wilson8f637ad2013-02-08 20:35:15 +00003253 FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject(Align ? Align : 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003254}
3255
Dan Gohman46510a72010-04-15 01:51:59 +00003256void SelectionDAGBuilder::visitLoad(const LoadInst &I) {
Eli Friedman327236c2011-08-24 20:50:09 +00003257 if (I.isAtomic())
3258 return visitAtomicLoad(I);
3259
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003260 const Value *SV = I.getOperand(0);
3261 SDValue Ptr = getValue(SV);
3262
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003263 Type *Ty = I.getType();
David Greene1e559442010-02-15 17:00:31 +00003264
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003265 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00003266 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Pete Cooperd752e0f2011-11-08 18:42:53 +00003267 bool isInvariant = I.getMetadata("invariant.load") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003268 unsigned Alignment = I.getAlignment();
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00003269 const MDNode *TBAAInfo = I.getMetadata(LLVMContext::MD_tbaa);
Rafael Espindola95d594c2012-03-31 18:14:00 +00003270 const MDNode *Ranges = I.getMetadata(LLVMContext::MD_range);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003271
Owen Andersone50ed302009-08-10 22:56:29 +00003272 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003273 SmallVector<uint64_t, 4> Offsets;
3274 ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets);
3275 unsigned NumValues = ValueVTs.size();
3276 if (NumValues == 0)
3277 return;
3278
3279 SDValue Root;
3280 bool ConstantMemory = false;
Andrew Trickde91f3c2010-11-12 17:50:46 +00003281 if (I.isVolatile() || NumValues > MaxParallelChains)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003282 // Serialize volatile loads with other side effects.
3283 Root = getRoot();
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00003284 else if (AA->pointsToConstantMemory(
3285 AliasAnalysis::Location(SV, AA->getTypeStoreSize(Ty), TBAAInfo))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003286 // Do not serialize (non-volatile) loads of constant memory with anything.
3287 Root = DAG.getEntryNode();
3288 ConstantMemory = true;
3289 } else {
3290 // Do not serialize non-volatile loads against each other.
3291 Root = DAG.getRoot();
3292 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003293
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003294 SmallVector<SDValue, 4> Values(NumValues);
Andrew Trickde91f3c2010-11-12 17:50:46 +00003295 SmallVector<SDValue, 4> Chains(std::min(unsigned(MaxParallelChains),
3296 NumValues));
Owen Andersone50ed302009-08-10 22:56:29 +00003297 EVT PtrVT = Ptr.getValueType();
Andrew Trickde91f3c2010-11-12 17:50:46 +00003298 unsigned ChainI = 0;
3299 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
3300 // Serializing loads here may result in excessive register pressure, and
3301 // TokenFactor places arbitrary choke points on the scheduler. SD scheduling
3302 // could recover a bit by hoisting nodes upward in the chain by recognizing
3303 // they are side-effect free or do not alias. The optimizer should really
3304 // avoid this case by converting large object/array copies to llvm.memcpy
3305 // (MaxParallelChains should always remain as failsafe).
3306 if (ChainI == MaxParallelChains) {
3307 assert(PendingLoads.empty() && "PendingLoads must be serialized first");
Andrew Trickac6d9be2013-05-25 02:42:55 +00003308 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003309 MVT::Other, &Chains[0], ChainI);
3310 Root = Chain;
3311 ChainI = 0;
3312 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00003313 SDValue A = DAG.getNode(ISD::ADD, getCurSDLoc(),
Bill Wendling856ff412009-12-22 00:12:37 +00003314 PtrVT, Ptr,
3315 DAG.getConstant(Offsets[i], PtrVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003316 SDValue L = DAG.getLoad(ValueVTs[i], getCurSDLoc(), Root,
Michael J. Spencere70c5262010-10-16 08:25:21 +00003317 A, MachinePointerInfo(SV, Offsets[i]), isVolatile,
Rafael Espindola95d594c2012-03-31 18:14:00 +00003318 isNonTemporal, isInvariant, Alignment, TBAAInfo,
3319 Ranges);
Bill Wendling856ff412009-12-22 00:12:37 +00003320
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003321 Values[i] = L;
Andrew Trickde91f3c2010-11-12 17:50:46 +00003322 Chains[ChainI] = L.getValue(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003323 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003324
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003325 if (!ConstantMemory) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003326 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003327 MVT::Other, &Chains[0], ChainI);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003328 if (isVolatile)
3329 DAG.setRoot(Chain);
3330 else
3331 PendingLoads.push_back(Chain);
3332 }
3333
Andrew Trickac6d9be2013-05-25 02:42:55 +00003334 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00003335 DAG.getVTList(&ValueVTs[0], NumValues),
3336 &Values[0], NumValues));
Bill Wendling856ff412009-12-22 00:12:37 +00003337}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003338
Dan Gohman46510a72010-04-15 01:51:59 +00003339void SelectionDAGBuilder::visitStore(const StoreInst &I) {
Eli Friedman327236c2011-08-24 20:50:09 +00003340 if (I.isAtomic())
3341 return visitAtomicStore(I);
3342
Dan Gohman46510a72010-04-15 01:51:59 +00003343 const Value *SrcV = I.getOperand(0);
3344 const Value *PtrV = I.getOperand(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003345
Owen Andersone50ed302009-08-10 22:56:29 +00003346 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003347 SmallVector<uint64_t, 4> Offsets;
3348 ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets);
3349 unsigned NumValues = ValueVTs.size();
3350 if (NumValues == 0)
3351 return;
3352
3353 // Get the lowered operands. Note that we do this after
3354 // checking if NumResults is zero, because with zero results
3355 // the operands won't have values in the map.
3356 SDValue Src = getValue(SrcV);
3357 SDValue Ptr = getValue(PtrV);
3358
3359 SDValue Root = getRoot();
Andrew Trickde91f3c2010-11-12 17:50:46 +00003360 SmallVector<SDValue, 4> Chains(std::min(unsigned(MaxParallelChains),
3361 NumValues));
Owen Andersone50ed302009-08-10 22:56:29 +00003362 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003363 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00003364 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003365 unsigned Alignment = I.getAlignment();
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00003366 const MDNode *TBAAInfo = I.getMetadata(LLVMContext::MD_tbaa);
Bill Wendling856ff412009-12-22 00:12:37 +00003367
Andrew Trickde91f3c2010-11-12 17:50:46 +00003368 unsigned ChainI = 0;
3369 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
3370 // See visitLoad comments.
3371 if (ChainI == MaxParallelChains) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003372 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003373 MVT::Other, &Chains[0], ChainI);
3374 Root = Chain;
3375 ChainI = 0;
3376 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00003377 SDValue Add = DAG.getNode(ISD::ADD, getCurSDLoc(), PtrVT, Ptr,
Bill Wendling856ff412009-12-22 00:12:37 +00003378 DAG.getConstant(Offsets[i], PtrVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003379 SDValue St = DAG.getStore(Root, getCurSDLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003380 SDValue(Src.getNode(), Src.getResNo() + i),
3381 Add, MachinePointerInfo(PtrV, Offsets[i]),
3382 isVolatile, isNonTemporal, Alignment, TBAAInfo);
3383 Chains[ChainI] = St;
Bill Wendling856ff412009-12-22 00:12:37 +00003384 }
3385
Andrew Trickac6d9be2013-05-25 02:42:55 +00003386 SDValue StoreNode = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003387 MVT::Other, &Chains[0], ChainI);
Devang Patel7e13efa2010-10-26 22:14:52 +00003388 DAG.setRoot(StoreNode);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003389}
3390
Eli Friedman26689ac2011-08-03 21:06:02 +00003391static SDValue InsertFenceForAtomic(SDValue Chain, AtomicOrdering Order,
Eli Friedman327236c2011-08-24 20:50:09 +00003392 SynchronizationScope Scope,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003393 bool Before, SDLoc dl,
Eli Friedman26689ac2011-08-03 21:06:02 +00003394 SelectionDAG &DAG,
3395 const TargetLowering &TLI) {
3396 // Fence, if necessary
3397 if (Before) {
Eli Friedman069e2ed2011-08-26 02:59:24 +00003398 if (Order == AcquireRelease || Order == SequentiallyConsistent)
Eli Friedman26689ac2011-08-03 21:06:02 +00003399 Order = Release;
3400 else if (Order == Acquire || Order == Monotonic)
3401 return Chain;
3402 } else {
3403 if (Order == AcquireRelease)
3404 Order = Acquire;
3405 else if (Order == Release || Order == Monotonic)
3406 return Chain;
3407 }
3408 SDValue Ops[3];
3409 Ops[0] = Chain;
Eli Friedman327236c2011-08-24 20:50:09 +00003410 Ops[1] = DAG.getConstant(Order, TLI.getPointerTy());
3411 Ops[2] = DAG.getConstant(Scope, TLI.getPointerTy());
Eli Friedman26689ac2011-08-03 21:06:02 +00003412 return DAG.getNode(ISD::ATOMIC_FENCE, dl, MVT::Other, Ops, 3);
3413}
3414
Eli Friedmanff030482011-07-28 21:48:00 +00003415void SelectionDAGBuilder::visitAtomicCmpXchg(const AtomicCmpXchgInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003416 SDLoc dl = getCurSDLoc();
Eli Friedman26689ac2011-08-03 21:06:02 +00003417 AtomicOrdering Order = I.getOrdering();
Eli Friedman327236c2011-08-24 20:50:09 +00003418 SynchronizationScope Scope = I.getSynchScope();
Eli Friedman26689ac2011-08-03 21:06:02 +00003419
3420 SDValue InChain = getRoot();
3421
3422 if (TLI.getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003423 InChain = InsertFenceForAtomic(InChain, Order, Scope, true, dl,
3424 DAG, TLI);
Eli Friedman26689ac2011-08-03 21:06:02 +00003425
Eli Friedman55ba8162011-07-29 03:05:32 +00003426 SDValue L =
Eli Friedman26689ac2011-08-03 21:06:02 +00003427 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl,
Eli Friedman55ba8162011-07-29 03:05:32 +00003428 getValue(I.getCompareOperand()).getValueType().getSimpleVT(),
Eli Friedman26689ac2011-08-03 21:06:02 +00003429 InChain,
Eli Friedman55ba8162011-07-29 03:05:32 +00003430 getValue(I.getPointerOperand()),
3431 getValue(I.getCompareOperand()),
3432 getValue(I.getNewValOperand()),
3433 MachinePointerInfo(I.getPointerOperand()), 0 /* Alignment */,
Eli Friedman327236c2011-08-24 20:50:09 +00003434 TLI.getInsertFencesForAtomic() ? Monotonic : Order,
3435 Scope);
Eli Friedman26689ac2011-08-03 21:06:02 +00003436
3437 SDValue OutChain = L.getValue(1);
3438
3439 if (TLI.getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003440 OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl,
3441 DAG, TLI);
Eli Friedman26689ac2011-08-03 21:06:02 +00003442
Eli Friedman55ba8162011-07-29 03:05:32 +00003443 setValue(&I, L);
Eli Friedman26689ac2011-08-03 21:06:02 +00003444 DAG.setRoot(OutChain);
Eli Friedmanff030482011-07-28 21:48:00 +00003445}
3446
3447void SelectionDAGBuilder::visitAtomicRMW(const AtomicRMWInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003448 SDLoc dl = getCurSDLoc();
Eli Friedman55ba8162011-07-29 03:05:32 +00003449 ISD::NodeType NT;
3450 switch (I.getOperation()) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003451 default: llvm_unreachable("Unknown atomicrmw operation");
Eli Friedman55ba8162011-07-29 03:05:32 +00003452 case AtomicRMWInst::Xchg: NT = ISD::ATOMIC_SWAP; break;
3453 case AtomicRMWInst::Add: NT = ISD::ATOMIC_LOAD_ADD; break;
3454 case AtomicRMWInst::Sub: NT = ISD::ATOMIC_LOAD_SUB; break;
3455 case AtomicRMWInst::And: NT = ISD::ATOMIC_LOAD_AND; break;
3456 case AtomicRMWInst::Nand: NT = ISD::ATOMIC_LOAD_NAND; break;
3457 case AtomicRMWInst::Or: NT = ISD::ATOMIC_LOAD_OR; break;
3458 case AtomicRMWInst::Xor: NT = ISD::ATOMIC_LOAD_XOR; break;
3459 case AtomicRMWInst::Max: NT = ISD::ATOMIC_LOAD_MAX; break;
3460 case AtomicRMWInst::Min: NT = ISD::ATOMIC_LOAD_MIN; break;
3461 case AtomicRMWInst::UMax: NT = ISD::ATOMIC_LOAD_UMAX; break;
3462 case AtomicRMWInst::UMin: NT = ISD::ATOMIC_LOAD_UMIN; break;
3463 }
Eli Friedman26689ac2011-08-03 21:06:02 +00003464 AtomicOrdering Order = I.getOrdering();
Eli Friedman327236c2011-08-24 20:50:09 +00003465 SynchronizationScope Scope = I.getSynchScope();
Eli Friedman26689ac2011-08-03 21:06:02 +00003466
3467 SDValue InChain = getRoot();
3468
3469 if (TLI.getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003470 InChain = InsertFenceForAtomic(InChain, Order, Scope, true, dl,
3471 DAG, TLI);
Eli Friedman26689ac2011-08-03 21:06:02 +00003472
Eli Friedman55ba8162011-07-29 03:05:32 +00003473 SDValue L =
Eli Friedman26689ac2011-08-03 21:06:02 +00003474 DAG.getAtomic(NT, dl,
Eli Friedman55ba8162011-07-29 03:05:32 +00003475 getValue(I.getValOperand()).getValueType().getSimpleVT(),
Eli Friedman26689ac2011-08-03 21:06:02 +00003476 InChain,
Eli Friedman55ba8162011-07-29 03:05:32 +00003477 getValue(I.getPointerOperand()),
3478 getValue(I.getValOperand()),
3479 I.getPointerOperand(), 0 /* Alignment */,
Eli Friedman26689ac2011-08-03 21:06:02 +00003480 TLI.getInsertFencesForAtomic() ? Monotonic : Order,
Eli Friedman327236c2011-08-24 20:50:09 +00003481 Scope);
Eli Friedman26689ac2011-08-03 21:06:02 +00003482
3483 SDValue OutChain = L.getValue(1);
3484
3485 if (TLI.getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003486 OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl,
3487 DAG, TLI);
Eli Friedman26689ac2011-08-03 21:06:02 +00003488
Eli Friedman55ba8162011-07-29 03:05:32 +00003489 setValue(&I, L);
Eli Friedman26689ac2011-08-03 21:06:02 +00003490 DAG.setRoot(OutChain);
Eli Friedmanff030482011-07-28 21:48:00 +00003491}
3492
Eli Friedman47f35132011-07-25 23:16:38 +00003493void SelectionDAGBuilder::visitFence(const FenceInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003494 SDLoc dl = getCurSDLoc();
Eli Friedman14648462011-07-27 22:21:52 +00003495 SDValue Ops[3];
3496 Ops[0] = getRoot();
3497 Ops[1] = DAG.getConstant(I.getOrdering(), TLI.getPointerTy());
3498 Ops[2] = DAG.getConstant(I.getSynchScope(), TLI.getPointerTy());
3499 DAG.setRoot(DAG.getNode(ISD::ATOMIC_FENCE, dl, MVT::Other, Ops, 3));
Eli Friedman47f35132011-07-25 23:16:38 +00003500}
3501
Eli Friedman327236c2011-08-24 20:50:09 +00003502void SelectionDAGBuilder::visitAtomicLoad(const LoadInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003503 SDLoc dl = getCurSDLoc();
Eli Friedman327236c2011-08-24 20:50:09 +00003504 AtomicOrdering Order = I.getOrdering();
3505 SynchronizationScope Scope = I.getSynchScope();
3506
3507 SDValue InChain = getRoot();
3508
Eli Friedmanfd45fa12012-08-17 23:24:29 +00003509 EVT VT = TLI.getValueType(I.getType());
Eli Friedman327236c2011-08-24 20:50:09 +00003510
Evan Cheng607acd62013-02-06 02:06:33 +00003511 if (I.getAlignment() < VT.getSizeInBits() / 8)
Eli Friedmanfe731212011-09-13 20:50:54 +00003512 report_fatal_error("Cannot generate unaligned atomic load");
3513
Eli Friedman327236c2011-08-24 20:50:09 +00003514 SDValue L =
3515 DAG.getAtomic(ISD::ATOMIC_LOAD, dl, VT, VT, InChain,
3516 getValue(I.getPointerOperand()),
3517 I.getPointerOperand(), I.getAlignment(),
3518 TLI.getInsertFencesForAtomic() ? Monotonic : Order,
3519 Scope);
3520
3521 SDValue OutChain = L.getValue(1);
3522
3523 if (TLI.getInsertFencesForAtomic())
3524 OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl,
3525 DAG, TLI);
3526
3527 setValue(&I, L);
3528 DAG.setRoot(OutChain);
3529}
3530
3531void SelectionDAGBuilder::visitAtomicStore(const StoreInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003532 SDLoc dl = getCurSDLoc();
Eli Friedman327236c2011-08-24 20:50:09 +00003533
3534 AtomicOrdering Order = I.getOrdering();
3535 SynchronizationScope Scope = I.getSynchScope();
3536
3537 SDValue InChain = getRoot();
3538
Eli Friedmanfd45fa12012-08-17 23:24:29 +00003539 EVT VT = TLI.getValueType(I.getValueOperand()->getType());
Eli Friedmanfe731212011-09-13 20:50:54 +00003540
Evan Cheng607acd62013-02-06 02:06:33 +00003541 if (I.getAlignment() < VT.getSizeInBits() / 8)
Eli Friedmanfe731212011-09-13 20:50:54 +00003542 report_fatal_error("Cannot generate unaligned atomic store");
3543
Eli Friedman327236c2011-08-24 20:50:09 +00003544 if (TLI.getInsertFencesForAtomic())
3545 InChain = InsertFenceForAtomic(InChain, Order, Scope, true, dl,
3546 DAG, TLI);
3547
3548 SDValue OutChain =
Eli Friedmanfe731212011-09-13 20:50:54 +00003549 DAG.getAtomic(ISD::ATOMIC_STORE, dl, VT,
Eli Friedman327236c2011-08-24 20:50:09 +00003550 InChain,
3551 getValue(I.getPointerOperand()),
3552 getValue(I.getValueOperand()),
3553 I.getPointerOperand(), I.getAlignment(),
3554 TLI.getInsertFencesForAtomic() ? Monotonic : Order,
3555 Scope);
3556
3557 if (TLI.getInsertFencesForAtomic())
3558 OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl,
3559 DAG, TLI);
3560
3561 DAG.setRoot(OutChain);
3562}
3563
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003564/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
3565/// node.
Dan Gohman46510a72010-04-15 01:51:59 +00003566void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I,
Dan Gohman2048b852009-11-23 18:04:58 +00003567 unsigned Intrinsic) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003568 bool HasChain = !I.doesNotAccessMemory();
3569 bool OnlyLoad = HasChain && I.onlyReadsMemory();
3570
3571 // Build the operand list.
3572 SmallVector<SDValue, 8> Ops;
3573 if (HasChain) { // If this intrinsic has side-effects, chainify it.
3574 if (OnlyLoad) {
3575 // We don't need to serialize loads against other loads.
3576 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003577 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003578 Ops.push_back(getRoot());
3579 }
3580 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003581
3582 // Info is set by getTgtMemInstrinsic
3583 TargetLowering::IntrinsicInfo Info;
3584 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic);
3585
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003586 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Bob Wilson65ffec42010-09-21 17:56:22 +00003587 if (!IsTgtIntrinsic || Info.opc == ISD::INTRINSIC_VOID ||
3588 Info.opc == ISD::INTRINSIC_W_CHAIN)
Pete Cooperbf421392012-01-16 04:08:12 +00003589 Ops.push_back(DAG.getTargetConstant(Intrinsic, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003590
3591 // Add all operands of the call to the operand list.
Gabor Greif0635f352010-06-25 09:38:13 +00003592 for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) {
3593 SDValue Op = getValue(I.getArgOperand(i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003594 Ops.push_back(Op);
3595 }
3596
Owen Andersone50ed302009-08-10 22:56:29 +00003597 SmallVector<EVT, 4> ValueVTs;
Bob Wilson8d919552009-07-31 22:41:21 +00003598 ComputeValueVTs(TLI, I.getType(), ValueVTs);
Bill Wendling856ff412009-12-22 00:12:37 +00003599
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003600 if (HasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00003601 ValueVTs.push_back(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003602
Bob Wilson8d919552009-07-31 22:41:21 +00003603 SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003604
3605 // Create the node.
3606 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003607 if (IsTgtIntrinsic) {
3608 // This is target intrinsic that touches memory
Andrew Trickac6d9be2013-05-25 02:42:55 +00003609 Result = DAG.getMemIntrinsicNode(Info.opc, getCurSDLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003610 VTs, &Ops[0], Ops.size(),
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00003611 Info.memVT,
3612 MachinePointerInfo(Info.ptrVal, Info.offset),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003613 Info.align, Info.vol,
3614 Info.readMem, Info.writeMem);
Bill Wendling856ff412009-12-22 00:12:37 +00003615 } else if (!HasChain) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003616 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurSDLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003617 VTs, &Ops[0], Ops.size());
Benjamin Kramerf0127052010-01-05 13:12:22 +00003618 } else if (!I.getType()->isVoidTy()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003619 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurSDLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003620 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003621 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003622 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003623 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003624 }
3625
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003626 if (HasChain) {
3627 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
3628 if (OnlyLoad)
3629 PendingLoads.push_back(Chain);
3630 else
3631 DAG.setRoot(Chain);
3632 }
Bill Wendling856ff412009-12-22 00:12:37 +00003633
Benjamin Kramerf0127052010-01-05 13:12:22 +00003634 if (!I.getType()->isVoidTy()) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003635 if (VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Owen Andersone50ed302009-08-10 22:56:29 +00003636 EVT VT = TLI.getValueType(PTy);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003637 Result = DAG.getNode(ISD::BITCAST, getCurSDLoc(), VT, Result);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003638 }
Bill Wendling856ff412009-12-22 00:12:37 +00003639
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003640 setValue(&I, Result);
3641 }
3642}
3643
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003644/// GetSignificand - Get the significand and build it into a floating-point
3645/// number with exponent of 1:
3646///
3647/// Op = (Op & 0x007fffff) | 0x3f800000;
3648///
Matt Beaumont-Gay50e75bf2013-02-25 18:11:18 +00003649/// where Op is the hexadecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003650static SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00003651GetSignificand(SelectionDAG &DAG, SDValue Op, SDLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003652 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3653 DAG.getConstant(0x007fffff, MVT::i32));
3654 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
3655 DAG.getConstant(0x3f800000, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003656 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00003657}
3658
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003659/// GetExponent - Get the exponent:
3660///
Bill Wendlinge9a72862009-01-20 21:17:57 +00003661/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003662///
Matt Beaumont-Gay50e75bf2013-02-25 18:11:18 +00003663/// where Op is the hexadecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003664static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00003665GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003666 SDLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003667 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3668 DAG.getConstant(0x7f800000, MVT::i32));
3669 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Duncan Sands92abc622009-01-31 15:50:11 +00003670 DAG.getConstant(23, TLI.getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00003671 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
3672 DAG.getConstant(127, MVT::i32));
Bill Wendling4533cac2010-01-28 21:51:40 +00003673 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00003674}
3675
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003676/// getF32Constant - Get 32-bit floating point constant.
3677static SDValue
3678getF32Constant(SelectionDAG &DAG, unsigned Flt) {
Tim Northover0a29cb02013-01-22 09:46:31 +00003679 return DAG.getConstantFP(APFloat(APFloat::IEEEsingle, APInt(32, Flt)),
3680 MVT::f32);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003681}
3682
Craig Topper538cd482012-11-24 18:52:06 +00003683/// expandExp - Lower an exp intrinsic. Handles the special sequences for
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003684/// limited-precision mode.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003685static SDValue expandExp(SDLoc dl, SDValue Op, SelectionDAG &DAG,
Craig Topper538cd482012-11-24 18:52:06 +00003686 const TargetLowering &TLI) {
3687 if (Op.getValueType() == MVT::f32 &&
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003688 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003689
3690 // Put the exponent in the right bit position for later addition to the
3691 // final result:
3692 //
3693 // #define LOG2OFe 1.4426950f
3694 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Owen Anderson825b72b2009-08-11 20:47:22 +00003695 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003696 getF32Constant(DAG, 0x3fb8aa3b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003697 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003698
3699 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003700 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3701 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003702
3703 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003704 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003705 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendling856ff412009-12-22 00:12:37 +00003706
Craig Topperb3157722012-11-24 08:22:37 +00003707 SDValue TwoToFracPartOfX;
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003708 if (LimitFloatPrecision <= 6) {
3709 // For floating-point precision of 6:
3710 //
3711 // TwoToFractionalPartOfX =
3712 // 0.997535578f +
3713 // (0.735607626f + 0.252464424f * x) * x;
3714 //
3715 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003716 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003717 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003718 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003719 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003720 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
Craig Topperb3157722012-11-24 08:22:37 +00003721 TwoToFracPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
3722 getF32Constant(DAG, 0x3f7f5e7e));
Craig Topper08ac4692012-11-16 20:01:39 +00003723 } else if (LimitFloatPrecision <= 12) {
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003724 // For floating-point precision of 12:
3725 //
3726 // TwoToFractionalPartOfX =
3727 // 0.999892986f +
3728 // (0.696457318f +
3729 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3730 //
3731 // 0.000107046256 error, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003732 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003733 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003734 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003735 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003736 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3737 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003738 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003739 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
Craig Topperb3157722012-11-24 08:22:37 +00003740 TwoToFracPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
3741 getF32Constant(DAG, 0x3f7ff8fd));
Craig Topper08ac4692012-11-16 20:01:39 +00003742 } else { // LimitFloatPrecision <= 18
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003743 // For floating-point precision of 18:
3744 //
3745 // TwoToFractionalPartOfX =
3746 // 0.999999982f +
3747 // (0.693148872f +
3748 // (0.240227044f +
3749 // (0.554906021e-1f +
3750 // (0.961591928e-2f +
3751 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3752 //
3753 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003754 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003755 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003756 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003757 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003758 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3759 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003760 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003761 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3762 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003763 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003764 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3765 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003766 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003767 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3768 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003769 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003770 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
Craig Topperb3157722012-11-24 08:22:37 +00003771 TwoToFracPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
3772 getF32Constant(DAG, 0x3f800000));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003773 }
Craig Topperb3157722012-11-24 08:22:37 +00003774
3775 // Add the exponent into the result in integer domain.
3776 SDValue t13 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, TwoToFracPartOfX);
Craig Topper538cd482012-11-24 18:52:06 +00003777 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3778 DAG.getNode(ISD::ADD, dl, MVT::i32,
3779 t13, IntegerPartOfX));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003780 }
3781
Craig Topper538cd482012-11-24 18:52:06 +00003782 // No special expansion.
3783 return DAG.getNode(ISD::FEXP, dl, Op.getValueType(), Op);
Dale Johannesen59e577f2008-09-05 18:38:42 +00003784}
3785
Craig Topper5d1e0892012-11-23 18:38:31 +00003786/// expandLog - Lower a log intrinsic. Handles the special sequences for
Bill Wendling39150252008-09-09 20:39:27 +00003787/// limited-precision mode.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003788static SDValue expandLog(SDLoc dl, SDValue Op, SelectionDAG &DAG,
Craig Topper5d1e0892012-11-23 18:38:31 +00003789 const TargetLowering &TLI) {
3790 if (Op.getValueType() == MVT::f32 &&
Bill Wendling39150252008-09-09 20:39:27 +00003791 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003792 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00003793
3794 // Scale the exponent by log(2) [0.69314718f].
Bill Wendling46ada192010-03-02 01:55:18 +00003795 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003796 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003797 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003798
3799 // Get the significand and build it into a floating-point number with
3800 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003801 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling39150252008-09-09 20:39:27 +00003802
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003803 SDValue LogOfMantissa;
Bill Wendling39150252008-09-09 20:39:27 +00003804 if (LimitFloatPrecision <= 6) {
3805 // For floating-point precision of 6:
3806 //
3807 // LogofMantissa =
3808 // -1.1609546f +
3809 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003810 //
Bill Wendling39150252008-09-09 20:39:27 +00003811 // error 0.0034276066, which is better than 8 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003812 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003813 getF32Constant(DAG, 0xbe74c456));
Owen Anderson825b72b2009-08-11 20:47:22 +00003814 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003815 getF32Constant(DAG, 0x3fb3a2b1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003816 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003817 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
3818 getF32Constant(DAG, 0x3f949a29));
Craig Topper08ac4692012-11-16 20:01:39 +00003819 } else if (LimitFloatPrecision <= 12) {
Bill Wendling39150252008-09-09 20:39:27 +00003820 // For floating-point precision of 12:
3821 //
3822 // LogOfMantissa =
3823 // -1.7417939f +
3824 // (2.8212026f +
3825 // (-1.4699568f +
3826 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3827 //
3828 // error 0.000061011436, which is 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003829 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003830 getF32Constant(DAG, 0xbd67b6d6));
Owen Anderson825b72b2009-08-11 20:47:22 +00003831 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003832 getF32Constant(DAG, 0x3ee4f4b8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003833 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3834 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003835 getF32Constant(DAG, 0x3fbc278b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003836 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3837 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003838 getF32Constant(DAG, 0x40348e95));
Owen Anderson825b72b2009-08-11 20:47:22 +00003839 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003840 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
3841 getF32Constant(DAG, 0x3fdef31a));
Craig Topper08ac4692012-11-16 20:01:39 +00003842 } else { // LimitFloatPrecision <= 18
Bill Wendling39150252008-09-09 20:39:27 +00003843 // For floating-point precision of 18:
3844 //
3845 // LogOfMantissa =
3846 // -2.1072184f +
3847 // (4.2372794f +
3848 // (-3.7029485f +
3849 // (2.2781945f +
3850 // (-0.87823314f +
3851 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3852 //
3853 // error 0.0000023660568, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003854 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003855 getF32Constant(DAG, 0xbc91e5ac));
Owen Anderson825b72b2009-08-11 20:47:22 +00003856 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003857 getF32Constant(DAG, 0x3e4350aa));
Owen Anderson825b72b2009-08-11 20:47:22 +00003858 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3859 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003860 getF32Constant(DAG, 0x3f60d3e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003861 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3862 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003863 getF32Constant(DAG, 0x4011cdf0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003864 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3865 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003866 getF32Constant(DAG, 0x406cfd1c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003867 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3868 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003869 getF32Constant(DAG, 0x408797cb));
Owen Anderson825b72b2009-08-11 20:47:22 +00003870 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003871 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
3872 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003873 }
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003874
Craig Topper5d1e0892012-11-23 18:38:31 +00003875 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003876 }
3877
Craig Topper5d1e0892012-11-23 18:38:31 +00003878 // No special expansion.
3879 return DAG.getNode(ISD::FLOG, dl, Op.getValueType(), Op);
Dale Johannesen59e577f2008-09-05 18:38:42 +00003880}
3881
Craig Topper5d1e0892012-11-23 18:38:31 +00003882/// expandLog2 - Lower a log2 intrinsic. Handles the special sequences for
Bill Wendling3eb59402008-09-09 00:28:24 +00003883/// limited-precision mode.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003884static SDValue expandLog2(SDLoc dl, SDValue Op, SelectionDAG &DAG,
Craig Topper5d1e0892012-11-23 18:38:31 +00003885 const TargetLowering &TLI) {
3886 if (Op.getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003887 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003888 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003889
Bill Wendling39150252008-09-09 20:39:27 +00003890 // Get the exponent.
Bill Wendling46ada192010-03-02 01:55:18 +00003891 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
Bill Wendling856ff412009-12-22 00:12:37 +00003892
Bill Wendling3eb59402008-09-09 00:28:24 +00003893 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003894 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003895 SDValue X = GetSignificand(DAG, Op1, dl);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003896
Bill Wendling3eb59402008-09-09 00:28:24 +00003897 // Different possible minimax approximations of significand in
3898 // floating-point for various degrees of accuracy over [1,2].
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003899 SDValue Log2ofMantissa;
Bill Wendling3eb59402008-09-09 00:28:24 +00003900 if (LimitFloatPrecision <= 6) {
3901 // For floating-point precision of 6:
3902 //
3903 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
3904 //
3905 // error 0.0049451742, which is more than 7 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003906 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003907 getF32Constant(DAG, 0xbeb08fe0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003908 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003909 getF32Constant(DAG, 0x40019463));
Owen Anderson825b72b2009-08-11 20:47:22 +00003910 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003911 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
3912 getF32Constant(DAG, 0x3fd6633d));
Craig Topper08ac4692012-11-16 20:01:39 +00003913 } else if (LimitFloatPrecision <= 12) {
Bill Wendling3eb59402008-09-09 00:28:24 +00003914 // For floating-point precision of 12:
3915 //
3916 // Log2ofMantissa =
3917 // -2.51285454f +
3918 // (4.07009056f +
3919 // (-2.12067489f +
3920 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003921 //
Bill Wendling3eb59402008-09-09 00:28:24 +00003922 // error 0.0000876136000, which is better than 13 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003923 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003924 getF32Constant(DAG, 0xbda7262e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003925 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003926 getF32Constant(DAG, 0x3f25280b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003927 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3928 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003929 getF32Constant(DAG, 0x4007b923));
Owen Anderson825b72b2009-08-11 20:47:22 +00003930 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3931 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003932 getF32Constant(DAG, 0x40823e2f));
Owen Anderson825b72b2009-08-11 20:47:22 +00003933 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003934 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
3935 getF32Constant(DAG, 0x4020d29c));
Craig Topper08ac4692012-11-16 20:01:39 +00003936 } else { // LimitFloatPrecision <= 18
Bill Wendling3eb59402008-09-09 00:28:24 +00003937 // For floating-point precision of 18:
3938 //
3939 // Log2ofMantissa =
3940 // -3.0400495f +
3941 // (6.1129976f +
3942 // (-5.3420409f +
3943 // (3.2865683f +
3944 // (-1.2669343f +
3945 // (0.27515199f -
3946 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
3947 //
3948 // error 0.0000018516, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003949 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003950 getF32Constant(DAG, 0xbcd2769e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003951 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003952 getF32Constant(DAG, 0x3e8ce0b9));
Owen Anderson825b72b2009-08-11 20:47:22 +00003953 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3954 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003955 getF32Constant(DAG, 0x3fa22ae7));
Owen Anderson825b72b2009-08-11 20:47:22 +00003956 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3957 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003958 getF32Constant(DAG, 0x40525723));
Owen Anderson825b72b2009-08-11 20:47:22 +00003959 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3960 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003961 getF32Constant(DAG, 0x40aaf200));
Owen Anderson825b72b2009-08-11 20:47:22 +00003962 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3963 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003964 getF32Constant(DAG, 0x40c39dad));
Owen Anderson825b72b2009-08-11 20:47:22 +00003965 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003966 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
3967 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003968 }
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003969
Craig Topper5d1e0892012-11-23 18:38:31 +00003970 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log2ofMantissa);
Dale Johannesen853244f2008-09-05 23:49:37 +00003971 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003972
Craig Topper5d1e0892012-11-23 18:38:31 +00003973 // No special expansion.
3974 return DAG.getNode(ISD::FLOG2, dl, Op.getValueType(), Op);
Dale Johannesen59e577f2008-09-05 18:38:42 +00003975}
3976
Craig Topper5d1e0892012-11-23 18:38:31 +00003977/// expandLog10 - Lower a log10 intrinsic. Handles the special sequences for
Bill Wendling3eb59402008-09-09 00:28:24 +00003978/// limited-precision mode.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003979static SDValue expandLog10(SDLoc dl, SDValue Op, SelectionDAG &DAG,
Craig Topper5d1e0892012-11-23 18:38:31 +00003980 const TargetLowering &TLI) {
3981 if (Op.getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003982 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003983 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003984
Bill Wendling39150252008-09-09 20:39:27 +00003985 // Scale the exponent by log10(2) [0.30102999f].
Bill Wendling46ada192010-03-02 01:55:18 +00003986 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003987 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003988 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00003989
3990 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003991 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003992 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling3eb59402008-09-09 00:28:24 +00003993
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003994 SDValue Log10ofMantissa;
Bill Wendling3eb59402008-09-09 00:28:24 +00003995 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003996 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003997 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003998 // Log10ofMantissa =
3999 // -0.50419619f +
4000 // (0.60948995f - 0.10380950f * x) * x;
4001 //
4002 // error 0.0014886165, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004003 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004004 getF32Constant(DAG, 0xbdd49a13));
Owen Anderson825b72b2009-08-11 20:47:22 +00004005 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004006 getF32Constant(DAG, 0x3f1c0789));
Owen Anderson825b72b2009-08-11 20:47:22 +00004007 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004008 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
4009 getF32Constant(DAG, 0x3f011300));
Craig Topper08ac4692012-11-16 20:01:39 +00004010 } else if (LimitFloatPrecision <= 12) {
Bill Wendling3eb59402008-09-09 00:28:24 +00004011 // For floating-point precision of 12:
4012 //
4013 // Log10ofMantissa =
4014 // -0.64831180f +
4015 // (0.91751397f +
4016 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
4017 //
4018 // error 0.00019228036, which is better than 12 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004019 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004020 getF32Constant(DAG, 0x3d431f31));
Owen Anderson825b72b2009-08-11 20:47:22 +00004021 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004022 getF32Constant(DAG, 0x3ea21fb2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004023 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
4024 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004025 getF32Constant(DAG, 0x3f6ae232));
Owen Anderson825b72b2009-08-11 20:47:22 +00004026 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004027 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
4028 getF32Constant(DAG, 0x3f25f7c3));
Craig Topper08ac4692012-11-16 20:01:39 +00004029 } else { // LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00004030 // For floating-point precision of 18:
4031 //
4032 // Log10ofMantissa =
4033 // -0.84299375f +
4034 // (1.5327582f +
4035 // (-1.0688956f +
4036 // (0.49102474f +
4037 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
4038 //
4039 // error 0.0000037995730, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004040 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004041 getF32Constant(DAG, 0x3c5d51ce));
Owen Anderson825b72b2009-08-11 20:47:22 +00004042 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004043 getF32Constant(DAG, 0x3e00685a));
Owen Anderson825b72b2009-08-11 20:47:22 +00004044 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
4045 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004046 getF32Constant(DAG, 0x3efb6798));
Owen Anderson825b72b2009-08-11 20:47:22 +00004047 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4048 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004049 getF32Constant(DAG, 0x3f88d192));
Owen Anderson825b72b2009-08-11 20:47:22 +00004050 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4051 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004052 getF32Constant(DAG, 0x3fc4316c));
Owen Anderson825b72b2009-08-11 20:47:22 +00004053 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004054 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
4055 getF32Constant(DAG, 0x3f57ce70));
Bill Wendling3eb59402008-09-09 00:28:24 +00004056 }
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004057
Craig Topper5d1e0892012-11-23 18:38:31 +00004058 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log10ofMantissa);
Dale Johannesen852680a2008-09-05 21:27:19 +00004059 }
Bill Wendling3eb59402008-09-09 00:28:24 +00004060
Craig Topper5d1e0892012-11-23 18:38:31 +00004061 // No special expansion.
4062 return DAG.getNode(ISD::FLOG10, dl, Op.getValueType(), Op);
Dale Johannesen59e577f2008-09-05 18:38:42 +00004063}
4064
Craig Topper538cd482012-11-24 18:52:06 +00004065/// expandExp2 - Lower an exp2 intrinsic. Handles the special sequences for
Bill Wendlinge10c8142008-09-09 22:39:21 +00004066/// limited-precision mode.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004067static SDValue expandExp2(SDLoc dl, SDValue Op, SelectionDAG &DAG,
Craig Topper538cd482012-11-24 18:52:06 +00004068 const TargetLowering &TLI) {
4069 if (Op.getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00004070 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004071 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00004072
4073 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00004074 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
4075 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00004076
4077 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00004078 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00004079 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00004080
Craig Topperb3157722012-11-24 08:22:37 +00004081 SDValue TwoToFractionalPartOfX;
Bill Wendlinge10c8142008-09-09 22:39:21 +00004082 if (LimitFloatPrecision <= 6) {
4083 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004084 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00004085 // TwoToFractionalPartOfX =
4086 // 0.997535578f +
4087 // (0.735607626f + 0.252464424f * x) * x;
4088 //
4089 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004090 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004091 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00004092 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004093 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004094 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
Craig Topperb3157722012-11-24 08:22:37 +00004095 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
4096 getF32Constant(DAG, 0x3f7f5e7e));
Craig Topper08ac4692012-11-16 20:01:39 +00004097 } else if (LimitFloatPrecision <= 12) {
Bill Wendlinge10c8142008-09-09 22:39:21 +00004098 // For floating-point precision of 12:
4099 //
4100 // TwoToFractionalPartOfX =
4101 // 0.999892986f +
4102 // (0.696457318f +
4103 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
4104 //
4105 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004106 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004107 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004108 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004109 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004110 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4111 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004112 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00004113 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
Craig Topperb3157722012-11-24 08:22:37 +00004114 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
4115 getF32Constant(DAG, 0x3f7ff8fd));
Craig Topper08ac4692012-11-16 20:01:39 +00004116 } else { // LimitFloatPrecision <= 18
Bill Wendlinge10c8142008-09-09 22:39:21 +00004117 // For floating-point precision of 18:
4118 //
4119 // TwoToFractionalPartOfX =
4120 // 0.999999982f +
4121 // (0.693148872f +
4122 // (0.240227044f +
4123 // (0.554906021e-1f +
4124 // (0.961591928e-2f +
4125 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
4126 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004127 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004128 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004129 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004130 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00004131 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4132 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004133 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00004134 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4135 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004136 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00004137 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
4138 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004139 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00004140 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
4141 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004142 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00004143 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
Craig Topperb3157722012-11-24 08:22:37 +00004144 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
4145 getF32Constant(DAG, 0x3f800000));
Bill Wendlinge10c8142008-09-09 22:39:21 +00004146 }
Craig Topperb3157722012-11-24 08:22:37 +00004147
4148 // Add the exponent into the result in integer domain.
4149 SDValue t13 = DAG.getNode(ISD::BITCAST, dl, MVT::i32,
4150 TwoToFractionalPartOfX);
Craig Topper538cd482012-11-24 18:52:06 +00004151 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
4152 DAG.getNode(ISD::ADD, dl, MVT::i32,
4153 t13, IntegerPartOfX));
Dale Johannesen601d3c02008-09-05 01:48:15 +00004154 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00004155
Craig Topper538cd482012-11-24 18:52:06 +00004156 // No special expansion.
4157 return DAG.getNode(ISD::FEXP2, dl, Op.getValueType(), Op);
Dale Johannesen601d3c02008-09-05 01:48:15 +00004158}
4159
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004160/// visitPow - Lower a pow intrinsic. Handles the special sequences for
4161/// limited-precision mode with x == 10.0f.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004162static SDValue expandPow(SDLoc dl, SDValue LHS, SDValue RHS,
Craig Topper327e4cb2012-11-25 08:08:58 +00004163 SelectionDAG &DAG, const TargetLowering &TLI) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004164 bool IsExp10 = false;
Craig Topper327e4cb2012-11-25 08:08:58 +00004165 if (LHS.getValueType() == MVT::f32 && LHS.getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004166 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Craig Topper327e4cb2012-11-25 08:08:58 +00004167 if (ConstantFPSDNode *LHSC = dyn_cast<ConstantFPSDNode>(LHS)) {
4168 APFloat Ten(10.0f);
4169 IsExp10 = LHSC->isExactlyValue(Ten);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004170 }
4171 }
4172
Craig Topperc1aa6382012-11-25 00:48:58 +00004173 if (IsExp10) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004174 // Put the exponent in the right bit position for later addition to the
4175 // final result:
4176 //
4177 // #define LOG2OF10 3.3219281f
4178 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Craig Topper327e4cb2012-11-25 08:08:58 +00004179 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, RHS,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004180 getF32Constant(DAG, 0x40549a78));
Owen Anderson825b72b2009-08-11 20:47:22 +00004181 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004182
4183 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00004184 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
4185 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004186
4187 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00004188 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00004189 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004190
Craig Topper915562e2012-11-25 00:15:07 +00004191 SDValue TwoToFractionalPartOfX;
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004192 if (LimitFloatPrecision <= 6) {
4193 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004194 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004195 // twoToFractionalPartOfX =
4196 // 0.997535578f +
4197 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004198 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004199 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004200 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004201 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00004202 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004203 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004204 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
Craig Topper915562e2012-11-25 00:15:07 +00004205 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
4206 getF32Constant(DAG, 0x3f7f5e7e));
Craig Topper08ac4692012-11-16 20:01:39 +00004207 } else if (LimitFloatPrecision <= 12) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004208 // For floating-point precision of 12:
4209 //
4210 // TwoToFractionalPartOfX =
4211 // 0.999892986f +
4212 // (0.696457318f +
4213 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
4214 //
4215 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004216 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004217 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004218 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004219 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004220 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4221 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004222 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00004223 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
Craig Topper915562e2012-11-25 00:15:07 +00004224 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
4225 getF32Constant(DAG, 0x3f7ff8fd));
Craig Topper08ac4692012-11-16 20:01:39 +00004226 } else { // LimitFloatPrecision <= 18
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004227 // For floating-point precision of 18:
4228 //
4229 // TwoToFractionalPartOfX =
4230 // 0.999999982f +
4231 // (0.693148872f +
4232 // (0.240227044f +
4233 // (0.554906021e-1f +
4234 // (0.961591928e-2f +
4235 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
4236 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004237 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004238 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004239 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004240 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00004241 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4242 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004243 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00004244 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4245 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004246 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00004247 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
4248 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004249 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00004250 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
4251 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004252 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00004253 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
Craig Topper915562e2012-11-25 00:15:07 +00004254 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
4255 getF32Constant(DAG, 0x3f800000));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004256 }
Craig Topper915562e2012-11-25 00:15:07 +00004257
4258 SDValue t13 = DAG.getNode(ISD::BITCAST, dl,MVT::i32,TwoToFractionalPartOfX);
Craig Topper327e4cb2012-11-25 08:08:58 +00004259 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
4260 DAG.getNode(ISD::ADD, dl, MVT::i32,
4261 t13, IntegerPartOfX));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004262 }
4263
Craig Topper327e4cb2012-11-25 08:08:58 +00004264 // No special expansion.
4265 return DAG.getNode(ISD::FPOW, dl, LHS.getValueType(), LHS, RHS);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004266}
4267
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004268
4269/// ExpandPowI - Expand a llvm.powi intrinsic.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004270static SDValue ExpandPowI(SDLoc DL, SDValue LHS, SDValue RHS,
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004271 SelectionDAG &DAG) {
4272 // If RHS is a constant, we can expand this out to a multiplication tree,
4273 // otherwise we end up lowering to a call to __powidf2 (for example). When
4274 // optimizing for size, we only want to do this if the expansion would produce
4275 // a small number of multiplies, otherwise we do the full expansion.
4276 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
4277 // Get the exponent as a positive value.
4278 unsigned Val = RHSC->getSExtValue();
4279 if ((int)Val < 0) Val = -Val;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004280
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004281 // powi(x, 0) -> 1.0
4282 if (Val == 0)
4283 return DAG.getConstantFP(1.0, LHS.getValueType());
4284
Dan Gohmanae541aa2010-04-15 04:33:49 +00004285 const Function *F = DAG.getMachineFunction().getFunction();
Bill Wendling831737d2012-12-30 10:32:01 +00004286 if (!F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
4287 Attribute::OptimizeForSize) ||
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004288 // If optimizing for size, don't insert too many multiplies. This
4289 // inserts up to 5 multiplies.
4290 CountPopulation_32(Val)+Log2_32(Val) < 7) {
4291 // We use the simple binary decomposition method to generate the multiply
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004292 // sequence. There are more optimal ways to do this (for example,
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004293 // powi(x,15) generates one more multiply than it should), but this has
4294 // the benefit of being both really simple and much better than a libcall.
4295 SDValue Res; // Logically starts equal to 1.0
4296 SDValue CurSquare = LHS;
4297 while (Val) {
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00004298 if (Val & 1) {
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004299 if (Res.getNode())
4300 Res = DAG.getNode(ISD::FMUL, DL,Res.getValueType(), Res, CurSquare);
4301 else
4302 Res = CurSquare; // 1.0*CurSquare.
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00004303 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004304
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004305 CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
4306 CurSquare, CurSquare);
4307 Val >>= 1;
4308 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004309
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004310 // If the original was negative, invert the result, producing 1/(x*x*x).
4311 if (RHSC->getSExtValue() < 0)
4312 Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(),
4313 DAG.getConstantFP(1.0, LHS.getValueType()), Res);
4314 return Res;
4315 }
4316 }
4317
4318 // Otherwise, expand to a libcall.
4319 return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS);
4320}
4321
Devang Patel227dfdb2011-05-16 21:24:05 +00004322// getTruncatedArgReg - Find underlying register used for an truncated
4323// argument.
4324static unsigned getTruncatedArgReg(const SDValue &N) {
4325 if (N.getOpcode() != ISD::TRUNCATE)
4326 return 0;
4327
4328 const SDValue &Ext = N.getOperand(0);
4329 if (Ext.getOpcode() == ISD::AssertZext || Ext.getOpcode() == ISD::AssertSext){
4330 const SDValue &CFR = Ext.getOperand(0);
4331 if (CFR.getOpcode() == ISD::CopyFromReg)
4332 return cast<RegisterSDNode>(CFR.getOperand(1))->getReg();
Craig Topper7eb46d82012-04-11 04:55:51 +00004333 if (CFR.getOpcode() == ISD::TRUNCATE)
4334 return getTruncatedArgReg(CFR);
Devang Patel227dfdb2011-05-16 21:24:05 +00004335 }
4336 return 0;
4337}
4338
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004339/// EmitFuncArgumentDbgValue - If the DbgValueInst is a dbg_value of a function
4340/// argument, create the corresponding DBG_VALUE machine instruction for it now.
4341/// At the end of instruction selection, they will be inserted to the entry BB.
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004342bool
Devang Patel78a06e52010-08-25 20:39:26 +00004343SelectionDAGBuilder::EmitFuncArgumentDbgValue(const Value *V, MDNode *Variable,
Michael J. Spencere70c5262010-10-16 08:25:21 +00004344 int64_t Offset,
Dan Gohman5d11ea32010-05-01 00:33:16 +00004345 const SDValue &N) {
Devang Patel0b48ead2010-08-31 22:22:42 +00004346 const Argument *Arg = dyn_cast<Argument>(V);
4347 if (!Arg)
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004348 return false;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004349
Devang Patel719f6a92010-04-29 20:40:36 +00004350 MachineFunction &MF = DAG.getMachineFunction();
Devang Patela90b3052010-11-02 17:01:30 +00004351 const TargetInstrInfo *TII = DAG.getTarget().getInstrInfo();
4352 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
4353
Devang Patela83ce982010-04-29 18:50:36 +00004354 // Ignore inlined function arguments here.
4355 DIVariable DV(Variable);
Devang Patel719f6a92010-04-29 20:40:36 +00004356 if (DV.isInlinedFnArgument(MF.getFunction()))
Devang Patela83ce982010-04-29 18:50:36 +00004357 return false;
4358
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004359 unsigned Reg = 0;
Devang Patel9aee3352011-09-08 22:59:09 +00004360 // Some arguments' frame index is recorded during argument lowering.
4361 Offset = FuncInfo.getArgumentFrameIndex(Arg);
4362 if (Offset)
Craig Topper7eb46d82012-04-11 04:55:51 +00004363 Reg = TRI->getFrameRegister(MF);
Devang Patel0b48ead2010-08-31 22:22:42 +00004364
Devang Patel9aee3352011-09-08 22:59:09 +00004365 if (!Reg && N.getNode()) {
Devang Patel227dfdb2011-05-16 21:24:05 +00004366 if (N.getOpcode() == ISD::CopyFromReg)
4367 Reg = cast<RegisterSDNode>(N.getOperand(1))->getReg();
4368 else
4369 Reg = getTruncatedArgReg(N);
4370 if (Reg && TargetRegisterInfo::isVirtualRegister(Reg)) {
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004371 MachineRegisterInfo &RegInfo = MF.getRegInfo();
4372 unsigned PR = RegInfo.getLiveInPhysReg(Reg);
4373 if (PR)
4374 Reg = PR;
4375 }
4376 }
4377
Evan Chenga36acad2010-04-29 06:33:38 +00004378 if (!Reg) {
Devang Patela90b3052010-11-02 17:01:30 +00004379 // Check if ValueMap has reg number.
Evan Chenga36acad2010-04-29 06:33:38 +00004380 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
Devang Patel8bc9ef72010-11-02 17:19:03 +00004381 if (VMI != FuncInfo.ValueMap.end())
4382 Reg = VMI->second;
Evan Chenga36acad2010-04-29 06:33:38 +00004383 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004384
Devang Patel8bc9ef72010-11-02 17:19:03 +00004385 if (!Reg && N.getNode()) {
Devang Patela90b3052010-11-02 17:01:30 +00004386 // Check if frame index is available.
4387 if (LoadSDNode *LNode = dyn_cast<LoadSDNode>(N.getNode()))
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004388 if (FrameIndexSDNode *FINode =
Devang Patela90b3052010-11-02 17:01:30 +00004389 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode())) {
4390 Reg = TRI->getFrameRegister(MF);
4391 Offset = FINode->getIndex();
4392 }
Devang Patel8bc9ef72010-11-02 17:19:03 +00004393 }
4394
4395 if (!Reg)
4396 return false;
Devang Patela90b3052010-11-02 17:01:30 +00004397
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004398 MachineInstrBuilder MIB = BuildMI(MF, getCurDebugLoc(),
Adrian Prantl86a87d92013-04-30 22:35:14 +00004399 TII->get(TargetOpcode::DBG_VALUE))
4400 .addReg(Reg, RegState::Debug).addImm(Offset).addMetadata(Variable);
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004401 FuncInfo.ArgDbgValues.push_back(&*MIB);
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004402 return true;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004403}
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004404
Douglas Gregor7d9663c2010-05-11 06:17:44 +00004405// VisualStudio defines setjmp as _setjmp
Michael J. Spencer1f409602010-09-24 19:48:47 +00004406#if defined(_MSC_VER) && defined(setjmp) && \
4407 !defined(setjmp_undefined_for_msvc)
4408# pragma push_macro("setjmp")
4409# undef setjmp
4410# define setjmp_undefined_for_msvc
Douglas Gregor7d9663c2010-05-11 06:17:44 +00004411#endif
4412
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004413/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
4414/// we want to emit this as a call to a named external function, return the name
4415/// otherwise lower it and return null.
4416const char *
Dan Gohman46510a72010-04-15 01:51:59 +00004417SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, unsigned Intrinsic) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004418 SDLoc sdl = getCurSDLoc();
Dale Johannesen66978ee2009-01-31 02:22:37 +00004419 DebugLoc dl = getCurDebugLoc();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004420 SDValue Res;
4421
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004422 switch (Intrinsic) {
4423 default:
4424 // By default, turn this into a target intrinsic node.
4425 visitTargetIntrinsic(I, Intrinsic);
4426 return 0;
4427 case Intrinsic::vastart: visitVAStart(I); return 0;
4428 case Intrinsic::vaend: visitVAEnd(I); return 0;
4429 case Intrinsic::vacopy: visitVACopy(I); return 0;
4430 case Intrinsic::returnaddress:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004431 setValue(&I, DAG.getNode(ISD::RETURNADDR, sdl, TLI.getPointerTy(),
Gabor Greif0635f352010-06-25 09:38:13 +00004432 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004433 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00004434 case Intrinsic::frameaddress:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004435 setValue(&I, DAG.getNode(ISD::FRAMEADDR, sdl, TLI.getPointerTy(),
Gabor Greif0635f352010-06-25 09:38:13 +00004436 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004437 return 0;
4438 case Intrinsic::setjmp:
Bill Wendlingc27facc2012-03-05 19:29:36 +00004439 return &"_setjmp"[!TLI.usesUnderscoreSetJmp()];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004440 case Intrinsic::longjmp:
Bill Wendlingc27facc2012-03-05 19:29:36 +00004441 return &"_longjmp"[!TLI.usesUnderscoreLongJmp()];
Chris Lattner824b9582008-11-21 16:42:48 +00004442 case Intrinsic::memcpy: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00004443 // Assert for address < 256 since we support only user defined address
4444 // spaces.
Gabor Greif0635f352010-06-25 09:38:13 +00004445 assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004446 < 256 &&
Gabor Greif0635f352010-06-25 09:38:13 +00004447 cast<PointerType>(I.getArgOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004448 < 256 &&
4449 "Unknown address space");
Gabor Greif0635f352010-06-25 09:38:13 +00004450 SDValue Op1 = getValue(I.getArgOperand(0));
4451 SDValue Op2 = getValue(I.getArgOperand(1));
4452 SDValue Op3 = getValue(I.getArgOperand(2));
4453 unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
Chandler Carruthaf23f8e2013-02-25 14:20:21 +00004454 if (!Align)
4455 Align = 1; // @llvm.memcpy defines 0 and 1 to both mean no alignment.
Gabor Greif0635f352010-06-25 09:38:13 +00004456 bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004457 DAG.setRoot(DAG.getMemcpy(getRoot(), sdl, Op1, Op2, Op3, Align, isVol, false,
Chris Lattnere72f2022010-09-21 05:40:29 +00004458 MachinePointerInfo(I.getArgOperand(0)),
4459 MachinePointerInfo(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004460 return 0;
4461 }
Chris Lattner824b9582008-11-21 16:42:48 +00004462 case Intrinsic::memset: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00004463 // Assert for address < 256 since we support only user defined address
4464 // spaces.
Gabor Greif0635f352010-06-25 09:38:13 +00004465 assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004466 < 256 &&
4467 "Unknown address space");
Gabor Greif0635f352010-06-25 09:38:13 +00004468 SDValue Op1 = getValue(I.getArgOperand(0));
4469 SDValue Op2 = getValue(I.getArgOperand(1));
4470 SDValue Op3 = getValue(I.getArgOperand(2));
4471 unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
Chandler Carruthaf23f8e2013-02-25 14:20:21 +00004472 if (!Align)
4473 Align = 1; // @llvm.memset defines 0 and 1 to both mean no alignment.
Gabor Greif0635f352010-06-25 09:38:13 +00004474 bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004475 DAG.setRoot(DAG.getMemset(getRoot(), sdl, Op1, Op2, Op3, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004476 MachinePointerInfo(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004477 return 0;
4478 }
Chris Lattner824b9582008-11-21 16:42:48 +00004479 case Intrinsic::memmove: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00004480 // Assert for address < 256 since we support only user defined address
4481 // spaces.
Gabor Greif0635f352010-06-25 09:38:13 +00004482 assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004483 < 256 &&
Gabor Greif0635f352010-06-25 09:38:13 +00004484 cast<PointerType>(I.getArgOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004485 < 256 &&
4486 "Unknown address space");
Gabor Greif0635f352010-06-25 09:38:13 +00004487 SDValue Op1 = getValue(I.getArgOperand(0));
4488 SDValue Op2 = getValue(I.getArgOperand(1));
4489 SDValue Op3 = getValue(I.getArgOperand(2));
4490 unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
Chandler Carruthaf23f8e2013-02-25 14:20:21 +00004491 if (!Align)
4492 Align = 1; // @llvm.memmove defines 0 and 1 to both mean no alignment.
Gabor Greif0635f352010-06-25 09:38:13 +00004493 bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004494 DAG.setRoot(DAG.getMemmove(getRoot(), sdl, Op1, Op2, Op3, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004495 MachinePointerInfo(I.getArgOperand(0)),
4496 MachinePointerInfo(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004497 return 0;
4498 }
Bill Wendling92c1e122009-02-13 02:16:35 +00004499 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00004500 const DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
Devang Patelac1ceb32009-10-09 22:42:28 +00004501 MDNode *Variable = DI.getVariable();
Dan Gohman46510a72010-04-15 01:51:59 +00004502 const Value *Address = DI.getAddress();
Eric Christopher8f2a88d2012-03-15 21:33:41 +00004503 if (!Address || !DIVariable(Variable).Verify()) {
4504 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
Dale Johannesen8ac38f22010-02-08 21:53:27 +00004505 return 0;
Eric Christopher8f2a88d2012-03-15 21:33:41 +00004506 }
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004507
Devang Patel3f74a112010-09-02 21:29:42 +00004508 // Check if address has undef value.
4509 if (isa<UndefValue>(Address) ||
4510 (Address->use_empty() && !isa<Argument>(Address))) {
Eric Christopher24413672012-02-23 03:39:39 +00004511 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
Devang Patel3f74a112010-09-02 21:29:42 +00004512 return 0;
4513 }
4514
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004515 SDValue &N = NodeMap[Address];
Devang Patel0b48ead2010-08-31 22:22:42 +00004516 if (!N.getNode() && isa<Argument>(Address))
4517 // Check unused arguments map.
4518 N = UnusedArgNodeMap[Address];
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004519 SDDbgValue *SDV;
4520 if (N.getNode()) {
Devang Patel8e741ed2010-09-02 21:02:27 +00004521 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
4522 Address = BCI->getOperand(0);
Eric Christopher178606d2012-02-24 01:59:08 +00004523 // Parameters are handled specially.
4524 bool isParameter =
4525 (DIVariable(Variable).getTag() == dwarf::DW_TAG_arg_variable ||
4526 isa<Argument>(Address));
4527
Devang Patel8e741ed2010-09-02 21:02:27 +00004528 const AllocaInst *AI = dyn_cast<AllocaInst>(Address);
4529
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004530 if (isParameter && !AI) {
4531 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N.getNode());
4532 if (FINode)
4533 // Byval parameter. We have a frame index at this point.
4534 SDV = DAG.getDbgValue(Variable, FINode->getIndex(),
4535 0, dl, SDNodeOrder);
Devang Patelafeaae72010-12-06 22:39:26 +00004536 else {
Devang Patel227dfdb2011-05-16 21:24:05 +00004537 // Address is an argument, so try to emit its dbg value using
4538 // virtual register info from the FuncInfo.ValueMap.
4539 EmitFuncArgumentDbgValue(Address, Variable, 0, N);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004540 return 0;
Devang Patelafeaae72010-12-06 22:39:26 +00004541 }
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004542 } else if (AI)
4543 SDV = DAG.getDbgValue(Variable, N.getNode(), N.getResNo(),
4544 0, dl, SDNodeOrder);
Devang Patelafeaae72010-12-06 22:39:26 +00004545 else {
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004546 // Can't do anything with other non-AI cases yet.
Eric Christopher24413672012-02-23 03:39:39 +00004547 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
Eric Christopher178606d2012-02-24 01:59:08 +00004548 DEBUG(dbgs() << "non-AllocaInst issue for Address: \n\t");
4549 DEBUG(Address->dump());
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004550 return 0;
Devang Patelafeaae72010-12-06 22:39:26 +00004551 }
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004552 DAG.AddDbgValue(SDV, N.getNode(), isParameter);
4553 } else {
Gabor Greiffb4032f2010-10-01 10:32:19 +00004554 // If Address is an argument then try to emit its dbg value using
Michael J. Spencere70c5262010-10-16 08:25:21 +00004555 // virtual register info from the FuncInfo.ValueMap.
Devang Patel6cd467b2010-08-26 22:53:27 +00004556 if (!EmitFuncArgumentDbgValue(Address, Variable, 0, N)) {
Devang Patel1397fdc2010-09-15 14:48:53 +00004557 // If variable is pinned by a alloca in dominating bb then
4558 // use StaticAllocaMap.
4559 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Address)) {
Devang Patel27ede1b2010-09-15 18:13:55 +00004560 if (AI->getParent() != DI.getParent()) {
4561 DenseMap<const AllocaInst*, int>::iterator SI =
4562 FuncInfo.StaticAllocaMap.find(AI);
4563 if (SI != FuncInfo.StaticAllocaMap.end()) {
4564 SDV = DAG.getDbgValue(Variable, SI->second,
4565 0, dl, SDNodeOrder);
4566 DAG.AddDbgValue(SDV, 0, false);
4567 return 0;
4568 }
Devang Patel1397fdc2010-09-15 14:48:53 +00004569 }
4570 }
Eric Christopher0822e012012-02-23 03:39:43 +00004571 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
Devang Patel6cd467b2010-08-26 22:53:27 +00004572 }
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004573 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004574 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00004575 }
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004576 case Intrinsic::dbg_value: {
Dan Gohman46510a72010-04-15 01:51:59 +00004577 const DbgValueInst &DI = cast<DbgValueInst>(I);
Devang Patel02f0dbd2010-05-07 22:04:20 +00004578 if (!DIVariable(DI.getVariable()).Verify())
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004579 return 0;
4580
4581 MDNode *Variable = DI.getVariable();
Devang Patel00190342010-03-15 19:15:44 +00004582 uint64_t Offset = DI.getOffset();
Dan Gohman46510a72010-04-15 01:51:59 +00004583 const Value *V = DI.getValue();
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004584 if (!V)
4585 return 0;
Devang Patel00190342010-03-15 19:15:44 +00004586
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004587 SDDbgValue *SDV;
Devang Patel57871242011-08-03 23:13:55 +00004588 if (isa<ConstantInt>(V) || isa<ConstantFP>(V) || isa<UndefValue>(V)) {
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004589 SDV = DAG.getDbgValue(Variable, V, Offset, dl, SDNodeOrder);
4590 DAG.AddDbgValue(SDV, 0, false);
Devang Patel00190342010-03-15 19:15:44 +00004591 } else {
Dale Johannesenbdc09d92010-07-16 00:02:08 +00004592 // Do not use getValue() in here; we don't want to generate code at
4593 // this point if it hasn't been done yet.
Devang Patel9126c0d2010-06-01 19:59:01 +00004594 SDValue N = NodeMap[V];
4595 if (!N.getNode() && isa<Argument>(V))
4596 // Check unused arguments map.
4597 N = UnusedArgNodeMap[V];
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004598 if (N.getNode()) {
Devang Patel78a06e52010-08-25 20:39:26 +00004599 if (!EmitFuncArgumentDbgValue(V, Variable, Offset, N)) {
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004600 SDV = DAG.getDbgValue(Variable, N.getNode(),
4601 N.getResNo(), Offset, dl, SDNodeOrder);
4602 DAG.AddDbgValue(SDV, N.getNode(), false);
4603 }
Devang Patela778f5c2011-02-18 22:43:42 +00004604 } else if (!V->use_empty() ) {
Dale Johannesenbdc09d92010-07-16 00:02:08 +00004605 // Do not call getValue(V) yet, as we don't want to generate code.
4606 // Remember it for later.
4607 DanglingDebugInfo DDI(&DI, dl, SDNodeOrder);
4608 DanglingDebugInfoMap[V] = DDI;
Devang Patel0991dfb2010-08-27 22:25:51 +00004609 } else {
Devang Patel00190342010-03-15 19:15:44 +00004610 // We may expand this to cover more cases. One case where we have no
Devang Patelafeaae72010-12-06 22:39:26 +00004611 // data available is an unreferenced parameter.
Eric Christopher0822e012012-02-23 03:39:43 +00004612 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004613 }
Devang Patel00190342010-03-15 19:15:44 +00004614 }
4615
4616 // Build a debug info table entry.
Dan Gohman46510a72010-04-15 01:51:59 +00004617 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(V))
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004618 V = BCI->getOperand(0);
Dan Gohman46510a72010-04-15 01:51:59 +00004619 const AllocaInst *AI = dyn_cast<AllocaInst>(V);
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004620 // Don't handle byval struct arguments or VLAs, for example.
Eric Christopher7e1e18f2012-03-26 06:10:32 +00004621 if (!AI) {
Eric Christopher9fc5c832012-03-28 07:34:36 +00004622 DEBUG(dbgs() << "Dropping debug location info for:\n " << DI << "\n");
4623 DEBUG(dbgs() << " Last seen at:\n " << *V << "\n");
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004624 return 0;
Eric Christopher7e1e18f2012-03-26 06:10:32 +00004625 }
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004626 DenseMap<const AllocaInst*, int>::iterator SI =
4627 FuncInfo.StaticAllocaMap.find(AI);
4628 if (SI == FuncInfo.StaticAllocaMap.end())
4629 return 0; // VLAs.
4630 int FI = SI->second;
Michael J. Spencere70c5262010-10-16 08:25:21 +00004631
Chris Lattner512063d2010-04-05 06:19:28 +00004632 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
4633 if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo())
4634 MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc());
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004635 return 0;
4636 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004637
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004638 case Intrinsic::eh_typeid_for: {
Chris Lattner512063d2010-04-05 06:19:28 +00004639 // Find the type id for the given typeinfo.
Gabor Greif0635f352010-06-25 09:38:13 +00004640 GlobalVariable *GV = ExtractTypeInfo(I.getArgOperand(0));
Chris Lattner512063d2010-04-05 06:19:28 +00004641 unsigned TypeID = DAG.getMachineFunction().getMMI().getTypeIDFor(GV);
4642 Res = DAG.getConstant(TypeID, MVT::i32);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004643 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004644 return 0;
4645 }
4646
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004647 case Intrinsic::eh_return_i32:
4648 case Intrinsic::eh_return_i64:
Chris Lattner512063d2010-04-05 06:19:28 +00004649 DAG.getMachineFunction().getMMI().setCallsEHReturn(true);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004650 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, sdl,
Chris Lattner512063d2010-04-05 06:19:28 +00004651 MVT::Other,
4652 getControlRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00004653 getValue(I.getArgOperand(0)),
4654 getValue(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004655 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004656 case Intrinsic::eh_unwind_init:
Chris Lattner512063d2010-04-05 06:19:28 +00004657 DAG.getMachineFunction().getMMI().setCallsUnwindInit(true);
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004658 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004659 case Intrinsic::eh_dwarf_cfa: {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004660 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getArgOperand(0)), sdl,
Duncan Sands3a66a682009-10-13 21:04:12 +00004661 TLI.getPointerTy());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004662 SDValue Offset = DAG.getNode(ISD::ADD, sdl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004663 TLI.getPointerTy(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004664 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, sdl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004665 TLI.getPointerTy()),
4666 CfaArg);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004667 SDValue FA = DAG.getNode(ISD::FRAMEADDR, sdl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004668 TLI.getPointerTy(),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004669 DAG.getConstant(0, TLI.getPointerTy()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004670 setValue(&I, DAG.getNode(ISD::ADD, sdl, TLI.getPointerTy(),
Bill Wendling4533cac2010-01-28 21:51:40 +00004671 FA, Offset));
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004672 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004673 }
Jim Grosbachca752c92010-01-28 01:45:32 +00004674 case Intrinsic::eh_sjlj_callsite: {
Chris Lattner512063d2010-04-05 06:19:28 +00004675 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Gabor Greif0635f352010-06-25 09:38:13 +00004676 ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(0));
Jim Grosbachca752c92010-01-28 01:45:32 +00004677 assert(CI && "Non-constant call site value in eh.sjlj.callsite!");
Chris Lattner512063d2010-04-05 06:19:28 +00004678 assert(MMI.getCurrentCallSite() == 0 && "Overlapping call sites!");
Jim Grosbachca752c92010-01-28 01:45:32 +00004679
Chris Lattner512063d2010-04-05 06:19:28 +00004680 MMI.setCurrentCallSite(CI->getZExtValue());
Jim Grosbachca752c92010-01-28 01:45:32 +00004681 return 0;
4682 }
Bill Wendling6ef94172011-09-28 03:36:43 +00004683 case Intrinsic::eh_sjlj_functioncontext: {
4684 // Get and store the index of the function context.
4685 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bill Wendlingadbf7b22011-09-28 03:52:41 +00004686 AllocaInst *FnCtx =
4687 cast<AllocaInst>(I.getArgOperand(0)->stripPointerCasts());
Bill Wendling6ef94172011-09-28 03:36:43 +00004688 int FI = FuncInfo.StaticAllocaMap[FnCtx];
4689 MFI->setFunctionContextIndex(FI);
4690 return 0;
4691 }
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004692 case Intrinsic::eh_sjlj_setjmp: {
Bill Wendlingce370cf2011-10-07 21:25:38 +00004693 SDValue Ops[2];
4694 Ops[0] = getRoot();
4695 Ops[1] = getValue(I.getArgOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004696 SDValue Op = DAG.getNode(ISD::EH_SJLJ_SETJMP, sdl,
Bill Wendlingce370cf2011-10-07 21:25:38 +00004697 DAG.getVTList(MVT::i32, MVT::Other),
4698 Ops, 2);
4699 setValue(&I, Op.getValue(0));
4700 DAG.setRoot(Op.getValue(1));
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004701 return 0;
4702 }
Jim Grosbach5eb19512010-05-22 01:06:18 +00004703 case Intrinsic::eh_sjlj_longjmp: {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004704 DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_LONGJMP, sdl, MVT::Other,
Jim Grosbache4ad3872010-10-19 23:27:08 +00004705 getRoot(), getValue(I.getArgOperand(0))));
4706 return 0;
4707 }
Jim Grosbachca752c92010-01-28 01:45:32 +00004708
Dale Johannesen0488fb62010-09-30 23:57:10 +00004709 case Intrinsic::x86_mmx_pslli_w:
4710 case Intrinsic::x86_mmx_pslli_d:
4711 case Intrinsic::x86_mmx_pslli_q:
4712 case Intrinsic::x86_mmx_psrli_w:
4713 case Intrinsic::x86_mmx_psrli_d:
4714 case Intrinsic::x86_mmx_psrli_q:
4715 case Intrinsic::x86_mmx_psrai_w:
4716 case Intrinsic::x86_mmx_psrai_d: {
4717 SDValue ShAmt = getValue(I.getArgOperand(1));
4718 if (isa<ConstantSDNode>(ShAmt)) {
4719 visitTargetIntrinsic(I, Intrinsic);
4720 return 0;
4721 }
4722 unsigned NewIntrinsic = 0;
4723 EVT ShAmtVT = MVT::v2i32;
4724 switch (Intrinsic) {
4725 case Intrinsic::x86_mmx_pslli_w:
4726 NewIntrinsic = Intrinsic::x86_mmx_psll_w;
4727 break;
4728 case Intrinsic::x86_mmx_pslli_d:
4729 NewIntrinsic = Intrinsic::x86_mmx_psll_d;
4730 break;
4731 case Intrinsic::x86_mmx_pslli_q:
4732 NewIntrinsic = Intrinsic::x86_mmx_psll_q;
4733 break;
4734 case Intrinsic::x86_mmx_psrli_w:
4735 NewIntrinsic = Intrinsic::x86_mmx_psrl_w;
4736 break;
4737 case Intrinsic::x86_mmx_psrli_d:
4738 NewIntrinsic = Intrinsic::x86_mmx_psrl_d;
4739 break;
4740 case Intrinsic::x86_mmx_psrli_q:
4741 NewIntrinsic = Intrinsic::x86_mmx_psrl_q;
4742 break;
4743 case Intrinsic::x86_mmx_psrai_w:
4744 NewIntrinsic = Intrinsic::x86_mmx_psra_w;
4745 break;
4746 case Intrinsic::x86_mmx_psrai_d:
4747 NewIntrinsic = Intrinsic::x86_mmx_psra_d;
4748 break;
4749 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
4750 }
4751
4752 // The vector shift intrinsics with scalars uses 32b shift amounts but
4753 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
4754 // to be zero.
4755 // We must do this early because v2i32 is not a legal type.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004756 SDValue ShOps[2];
4757 ShOps[0] = ShAmt;
4758 ShOps[1] = DAG.getConstant(0, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004759 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, sdl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004760 EVT DestVT = TLI.getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004761 ShAmt = DAG.getNode(ISD::BITCAST, sdl, DestVT, ShAmt);
4762 Res = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, sdl, DestVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00004763 DAG.getConstant(NewIntrinsic, MVT::i32),
4764 getValue(I.getArgOperand(0)), ShAmt);
4765 setValue(&I, Res);
4766 return 0;
4767 }
Pete Cooperd18134f2012-02-24 03:51:49 +00004768 case Intrinsic::x86_avx_vinsertf128_pd_256:
4769 case Intrinsic::x86_avx_vinsertf128_ps_256:
Craig Topperb45c9692012-04-07 22:32:29 +00004770 case Intrinsic::x86_avx_vinsertf128_si_256:
4771 case Intrinsic::x86_avx2_vinserti128: {
Pete Cooperd18134f2012-02-24 03:51:49 +00004772 EVT DestVT = TLI.getValueType(I.getType());
4773 EVT ElVT = TLI.getValueType(I.getArgOperand(1)->getType());
4774 uint64_t Idx = (cast<ConstantInt>(I.getArgOperand(2))->getZExtValue() & 1) *
4775 ElVT.getVectorNumElements();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004776 Res = DAG.getNode(ISD::INSERT_SUBVECTOR, sdl, DestVT,
Pete Cooperd18134f2012-02-24 03:51:49 +00004777 getValue(I.getArgOperand(0)),
4778 getValue(I.getArgOperand(1)),
Craig Topperf6dc7922012-09-05 05:48:09 +00004779 DAG.getIntPtrConstant(Idx));
4780 setValue(&I, Res);
4781 return 0;
4782 }
4783 case Intrinsic::x86_avx_vextractf128_pd_256:
4784 case Intrinsic::x86_avx_vextractf128_ps_256:
4785 case Intrinsic::x86_avx_vextractf128_si_256:
4786 case Intrinsic::x86_avx2_vextracti128: {
Craig Topperf6dc7922012-09-05 05:48:09 +00004787 EVT DestVT = TLI.getValueType(I.getType());
4788 uint64_t Idx = (cast<ConstantInt>(I.getArgOperand(1))->getZExtValue() & 1) *
4789 DestVT.getVectorNumElements();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004790 Res = DAG.getNode(ISD::EXTRACT_SUBVECTOR, sdl, DestVT,
Craig Topperf6dc7922012-09-05 05:48:09 +00004791 getValue(I.getArgOperand(0)),
4792 DAG.getIntPtrConstant(Idx));
Pete Cooperd18134f2012-02-24 03:51:49 +00004793 setValue(&I, Res);
4794 return 0;
4795 }
Mon P Wang77cdf302008-11-10 20:54:11 +00004796 case Intrinsic::convertff:
4797 case Intrinsic::convertfsi:
4798 case Intrinsic::convertfui:
4799 case Intrinsic::convertsif:
4800 case Intrinsic::convertuif:
4801 case Intrinsic::convertss:
4802 case Intrinsic::convertsu:
4803 case Intrinsic::convertus:
4804 case Intrinsic::convertuu: {
4805 ISD::CvtCode Code = ISD::CVT_INVALID;
4806 switch (Intrinsic) {
Craig Topperc42e6402012-04-11 04:34:11 +00004807 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Mon P Wang77cdf302008-11-10 20:54:11 +00004808 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
4809 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
4810 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
4811 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
4812 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
4813 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
4814 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
4815 case Intrinsic::convertus: Code = ISD::CVT_US; break;
4816 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
4817 }
Owen Andersone50ed302009-08-10 22:56:29 +00004818 EVT DestVT = TLI.getValueType(I.getType());
Gabor Greif0635f352010-06-25 09:38:13 +00004819 const Value *Op1 = I.getArgOperand(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004820 Res = DAG.getConvertRndSat(DestVT, sdl, getValue(Op1),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004821 DAG.getValueType(DestVT),
4822 DAG.getValueType(getValue(Op1).getValueType()),
Gabor Greif0635f352010-06-25 09:38:13 +00004823 getValue(I.getArgOperand(1)),
4824 getValue(I.getArgOperand(2)),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004825 Code);
4826 setValue(&I, Res);
Mon P Wang77cdf302008-11-10 20:54:11 +00004827 return 0;
4828 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004829 case Intrinsic::powi:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004830 setValue(&I, ExpandPowI(sdl, getValue(I.getArgOperand(0)),
Gabor Greif0635f352010-06-25 09:38:13 +00004831 getValue(I.getArgOperand(1)), DAG));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004832 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004833 case Intrinsic::log:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004834 setValue(&I, expandLog(sdl, getValue(I.getArgOperand(0)), DAG, TLI));
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004835 return 0;
4836 case Intrinsic::log2:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004837 setValue(&I, expandLog2(sdl, getValue(I.getArgOperand(0)), DAG, TLI));
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004838 return 0;
4839 case Intrinsic::log10:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004840 setValue(&I, expandLog10(sdl, getValue(I.getArgOperand(0)), DAG, TLI));
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004841 return 0;
4842 case Intrinsic::exp:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004843 setValue(&I, expandExp(sdl, getValue(I.getArgOperand(0)), DAG, TLI));
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004844 return 0;
4845 case Intrinsic::exp2:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004846 setValue(&I, expandExp2(sdl, getValue(I.getArgOperand(0)), DAG, TLI));
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004847 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004848 case Intrinsic::pow:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004849 setValue(&I, expandPow(sdl, getValue(I.getArgOperand(0)),
Craig Topper327e4cb2012-11-25 08:08:58 +00004850 getValue(I.getArgOperand(1)), DAG, TLI));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004851 return 0;
Craig Topper9bd4dd72012-11-16 07:48:23 +00004852 case Intrinsic::sqrt:
Peter Collingbourneb34d3aa2012-05-28 21:48:37 +00004853 case Intrinsic::fabs:
Craig Topper9bd4dd72012-11-16 07:48:23 +00004854 case Intrinsic::sin:
4855 case Intrinsic::cos:
Dan Gohman27db99f2012-07-26 17:43:27 +00004856 case Intrinsic::floor:
Craig Topper49010472012-11-15 06:51:10 +00004857 case Intrinsic::ceil:
Craig Topper49010472012-11-15 06:51:10 +00004858 case Intrinsic::trunc:
Craig Topper49010472012-11-15 06:51:10 +00004859 case Intrinsic::rint:
Craig Topper9bd4dd72012-11-16 07:48:23 +00004860 case Intrinsic::nearbyint: {
4861 unsigned Opcode;
4862 switch (Intrinsic) {
4863 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
4864 case Intrinsic::sqrt: Opcode = ISD::FSQRT; break;
4865 case Intrinsic::fabs: Opcode = ISD::FABS; break;
4866 case Intrinsic::sin: Opcode = ISD::FSIN; break;
4867 case Intrinsic::cos: Opcode = ISD::FCOS; break;
4868 case Intrinsic::floor: Opcode = ISD::FFLOOR; break;
4869 case Intrinsic::ceil: Opcode = ISD::FCEIL; break;
4870 case Intrinsic::trunc: Opcode = ISD::FTRUNC; break;
4871 case Intrinsic::rint: Opcode = ISD::FRINT; break;
4872 case Intrinsic::nearbyint: Opcode = ISD::FNEARBYINT; break;
4873 }
4874
Andrew Trickac6d9be2013-05-25 02:42:55 +00004875 setValue(&I, DAG.getNode(Opcode, sdl,
Craig Topper49010472012-11-15 06:51:10 +00004876 getValue(I.getArgOperand(0)).getValueType(),
4877 getValue(I.getArgOperand(0))));
4878 return 0;
Craig Topper9bd4dd72012-11-16 07:48:23 +00004879 }
Cameron Zwarich33390842011-07-08 21:39:21 +00004880 case Intrinsic::fma:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004881 setValue(&I, DAG.getNode(ISD::FMA, sdl,
Cameron Zwarich33390842011-07-08 21:39:21 +00004882 getValue(I.getArgOperand(0)).getValueType(),
4883 getValue(I.getArgOperand(0)),
4884 getValue(I.getArgOperand(1)),
4885 getValue(I.getArgOperand(2))));
4886 return 0;
Lang Hames5afba6f2012-06-05 19:07:46 +00004887 case Intrinsic::fmuladd: {
4888 EVT VT = TLI.getValueType(I.getType());
Lang Hamese0231412012-06-22 01:09:09 +00004889 if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict &&
Lang Hamese0231412012-06-22 01:09:09 +00004890 TLI.isFMAFasterThanMulAndAdd(VT)){
Andrew Trickac6d9be2013-05-25 02:42:55 +00004891 setValue(&I, DAG.getNode(ISD::FMA, sdl,
Lang Hames5afba6f2012-06-05 19:07:46 +00004892 getValue(I.getArgOperand(0)).getValueType(),
4893 getValue(I.getArgOperand(0)),
4894 getValue(I.getArgOperand(1)),
4895 getValue(I.getArgOperand(2))));
4896 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004897 SDValue Mul = DAG.getNode(ISD::FMUL, sdl,
Lang Hames5afba6f2012-06-05 19:07:46 +00004898 getValue(I.getArgOperand(0)).getValueType(),
4899 getValue(I.getArgOperand(0)),
4900 getValue(I.getArgOperand(1)));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004901 SDValue Add = DAG.getNode(ISD::FADD, sdl,
Lang Hames5afba6f2012-06-05 19:07:46 +00004902 getValue(I.getArgOperand(0)).getValueType(),
4903 Mul,
4904 getValue(I.getArgOperand(2)));
4905 setValue(&I, Add);
4906 }
4907 return 0;
4908 }
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004909 case Intrinsic::convert_to_fp16:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004910 setValue(&I, DAG.getNode(ISD::FP32_TO_FP16, sdl,
Gabor Greif0635f352010-06-25 09:38:13 +00004911 MVT::i16, getValue(I.getArgOperand(0))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004912 return 0;
4913 case Intrinsic::convert_from_fp16:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004914 setValue(&I, DAG.getNode(ISD::FP16_TO_FP32, sdl,
Gabor Greif0635f352010-06-25 09:38:13 +00004915 MVT::f32, getValue(I.getArgOperand(0))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004916 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004917 case Intrinsic::pcmarker: {
Gabor Greif0635f352010-06-25 09:38:13 +00004918 SDValue Tmp = getValue(I.getArgOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004919 DAG.setRoot(DAG.getNode(ISD::PCMARKER, sdl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004920 return 0;
4921 }
4922 case Intrinsic::readcyclecounter: {
4923 SDValue Op = getRoot();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004924 Res = DAG.getNode(ISD::READCYCLECOUNTER, sdl,
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004925 DAG.getVTList(MVT::i64, MVT::Other),
4926 &Op, 1);
4927 setValue(&I, Res);
4928 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004929 return 0;
4930 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004931 case Intrinsic::bswap:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004932 setValue(&I, DAG.getNode(ISD::BSWAP, sdl,
Gabor Greif0635f352010-06-25 09:38:13 +00004933 getValue(I.getArgOperand(0)).getValueType(),
4934 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004935 return 0;
4936 case Intrinsic::cttz: {
Gabor Greif0635f352010-06-25 09:38:13 +00004937 SDValue Arg = getValue(I.getArgOperand(0));
Chandler Carruth63974b22011-12-13 01:56:10 +00004938 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004939 EVT Ty = Arg.getValueType();
Chandler Carruth63974b22011-12-13 01:56:10 +00004940 setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTTZ : ISD::CTTZ_ZERO_UNDEF,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004941 sdl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004942 return 0;
4943 }
4944 case Intrinsic::ctlz: {
Gabor Greif0635f352010-06-25 09:38:13 +00004945 SDValue Arg = getValue(I.getArgOperand(0));
Chandler Carruth63974b22011-12-13 01:56:10 +00004946 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004947 EVT Ty = Arg.getValueType();
Chandler Carruth63974b22011-12-13 01:56:10 +00004948 setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTLZ : ISD::CTLZ_ZERO_UNDEF,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004949 sdl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004950 return 0;
4951 }
4952 case Intrinsic::ctpop: {
Gabor Greif0635f352010-06-25 09:38:13 +00004953 SDValue Arg = getValue(I.getArgOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00004954 EVT Ty = Arg.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004955 setValue(&I, DAG.getNode(ISD::CTPOP, sdl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004956 return 0;
4957 }
4958 case Intrinsic::stacksave: {
4959 SDValue Op = getRoot();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004960 Res = DAG.getNode(ISD::STACKSAVE, sdl,
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004961 DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1);
4962 setValue(&I, Res);
4963 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004964 return 0;
4965 }
4966 case Intrinsic::stackrestore: {
Gabor Greif0635f352010-06-25 09:38:13 +00004967 Res = getValue(I.getArgOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004968 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, sdl, MVT::Other, getRoot(), Res));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004969 return 0;
4970 }
Bill Wendling57344502008-11-18 11:01:33 +00004971 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00004972 // Emit code into the DAG to store the stack guard onto the stack.
4973 MachineFunction &MF = DAG.getMachineFunction();
4974 MachineFrameInfo *MFI = MF.getFrameInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004975 EVT PtrTy = TLI.getPointerTy();
Bill Wendlingb2a42982008-11-06 02:29:10 +00004976
Gabor Greif0635f352010-06-25 09:38:13 +00004977 SDValue Src = getValue(I.getArgOperand(0)); // The guard's value.
4978 AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Bill Wendlingb2a42982008-11-06 02:29:10 +00004979
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004980 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00004981 MFI->setStackProtectorIndex(FI);
4982
4983 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4984
4985 // Store the stack protector onto the stack.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004986 Res = DAG.getStore(getRoot(), sdl, Src, FIN,
Chris Lattner84bd98a2010-09-21 18:58:22 +00004987 MachinePointerInfo::getFixedStack(FI),
4988 true, false, 0);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004989 setValue(&I, Res);
4990 DAG.setRoot(Res);
Bill Wendlingb2a42982008-11-06 02:29:10 +00004991 return 0;
4992 }
Eric Christopher7b5e6172009-10-27 00:52:25 +00004993 case Intrinsic::objectsize: {
4994 // If we don't know by now, we're never going to know.
Gabor Greif0635f352010-06-25 09:38:13 +00004995 ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(1));
Eric Christopher7b5e6172009-10-27 00:52:25 +00004996
4997 assert(CI && "Non-constant type in __builtin_object_size?");
4998
Gabor Greif0635f352010-06-25 09:38:13 +00004999 SDValue Arg = getValue(I.getCalledValue());
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00005000 EVT Ty = Arg.getValueType();
5001
Dan Gohmane368b462010-06-18 14:22:04 +00005002 if (CI->isZero())
Bill Wendlingd0283fa2009-12-22 00:40:51 +00005003 Res = DAG.getConstant(-1ULL, Ty);
Eric Christopher7b5e6172009-10-27 00:52:25 +00005004 else
Bill Wendlingd0283fa2009-12-22 00:40:51 +00005005 Res = DAG.getConstant(0, Ty);
5006
5007 setValue(&I, Res);
Eric Christopher7b5e6172009-10-27 00:52:25 +00005008 return 0;
5009 }
Justin Holewinskic2b7f5f2013-05-21 14:37:16 +00005010 case Intrinsic::annotation:
5011 case Intrinsic::ptr_annotation:
5012 // Drop the intrinsic, but forward the value
5013 setValue(&I, getValue(I.getOperand(0)));
5014 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005015 case Intrinsic::var_annotation:
5016 // Discard annotate attributes
5017 return 0;
5018
5019 case Intrinsic::init_trampoline: {
Gabor Greif0635f352010-06-25 09:38:13 +00005020 const Function *F = cast<Function>(I.getArgOperand(1)->stripPointerCasts());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005021
5022 SDValue Ops[6];
5023 Ops[0] = getRoot();
Gabor Greif0635f352010-06-25 09:38:13 +00005024 Ops[1] = getValue(I.getArgOperand(0));
5025 Ops[2] = getValue(I.getArgOperand(1));
5026 Ops[3] = getValue(I.getArgOperand(2));
5027 Ops[4] = DAG.getSrcValue(I.getArgOperand(0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005028 Ops[5] = DAG.getSrcValue(F);
5029
Andrew Trickac6d9be2013-05-25 02:42:55 +00005030 Res = DAG.getNode(ISD::INIT_TRAMPOLINE, sdl, MVT::Other, Ops, 6);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005031
Duncan Sands4a544a72011-09-06 13:37:06 +00005032 DAG.setRoot(Res);
5033 return 0;
5034 }
5035 case Intrinsic::adjust_trampoline: {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005036 setValue(&I, DAG.getNode(ISD::ADJUST_TRAMPOLINE, sdl,
Duncan Sands4a544a72011-09-06 13:37:06 +00005037 TLI.getPointerTy(),
5038 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005039 return 0;
5040 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005041 case Intrinsic::gcroot:
5042 if (GFI) {
Bill Wendling95dd4422012-05-01 22:50:45 +00005043 const Value *Alloca = I.getArgOperand(0)->stripPointerCasts();
Gabor Greif0635f352010-06-25 09:38:13 +00005044 const Constant *TypeMap = cast<Constant>(I.getArgOperand(1));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005045
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005046 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
5047 GFI->addStackRoot(FI->getIndex(), TypeMap);
5048 }
5049 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005050 case Intrinsic::gcread:
5051 case Intrinsic::gcwrite:
Torok Edwinc23197a2009-07-14 16:55:14 +00005052 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
Bill Wendlingd0283fa2009-12-22 00:40:51 +00005053 case Intrinsic::flt_rounds:
Andrew Trickac6d9be2013-05-25 02:42:55 +00005054 setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, sdl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005055 return 0;
Jakub Staszak9da99342011-07-06 18:22:43 +00005056
5057 case Intrinsic::expect: {
5058 // Just replace __builtin_expect(exp, c) with EXP.
5059 setValue(&I, getValue(I.getArgOperand(0)));
5060 return 0;
5061 }
5062
Shuxin Yang970755e2012-10-19 20:11:16 +00005063 case Intrinsic::debugtrap:
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005064 case Intrinsic::trap: {
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005065 StringRef TrapFuncName = TM.Options.getTrapFunctionName();
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005066 if (TrapFuncName.empty()) {
Shuxin Yang970755e2012-10-19 20:11:16 +00005067 ISD::NodeType Op = (Intrinsic == Intrinsic::trap) ?
5068 ISD::TRAP : ISD::DEBUGTRAP;
Andrew Trickac6d9be2013-05-25 02:42:55 +00005069 DAG.setRoot(DAG.getNode(Op, sdl,MVT::Other, getRoot()));
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005070 return 0;
5071 }
5072 TargetLowering::ArgListTy Args;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00005073 TargetLowering::
5074 CallLoweringInfo CLI(getRoot(), I.getType(),
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005075 false, false, false, false, 0, CallingConv::C,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00005076 /*isTailCall=*/false,
5077 /*doesNotRet=*/false, /*isReturnValueUsed=*/true,
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005078 DAG.getExternalSymbol(TrapFuncName.data(), TLI.getPointerTy()),
Andrew Trickac6d9be2013-05-25 02:42:55 +00005079 Args, DAG, sdl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00005080 std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005081 DAG.setRoot(Result.second);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005082 return 0;
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005083 }
Shuxin Yang970755e2012-10-19 20:11:16 +00005084
Bill Wendlingef375462008-11-21 02:38:44 +00005085 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00005086 case Intrinsic::sadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00005087 case Intrinsic::usub_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00005088 case Intrinsic::ssub_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00005089 case Intrinsic::umul_with_overflow:
Craig Topperc42e6402012-04-11 04:34:11 +00005090 case Intrinsic::smul_with_overflow: {
5091 ISD::NodeType Op;
5092 switch (Intrinsic) {
5093 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
5094 case Intrinsic::uadd_with_overflow: Op = ISD::UADDO; break;
5095 case Intrinsic::sadd_with_overflow: Op = ISD::SADDO; break;
5096 case Intrinsic::usub_with_overflow: Op = ISD::USUBO; break;
5097 case Intrinsic::ssub_with_overflow: Op = ISD::SSUBO; break;
5098 case Intrinsic::umul_with_overflow: Op = ISD::UMULO; break;
5099 case Intrinsic::smul_with_overflow: Op = ISD::SMULO; break;
5100 }
5101 SDValue Op1 = getValue(I.getArgOperand(0));
5102 SDValue Op2 = getValue(I.getArgOperand(1));
Bill Wendling7cdc3c82008-11-21 02:03:52 +00005103
Craig Topperc42e6402012-04-11 04:34:11 +00005104 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005105 setValue(&I, DAG.getNode(Op, sdl, VTs, Op1, Op2));
Craig Topperc42e6402012-04-11 04:34:11 +00005106 return 0;
5107 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005108 case Intrinsic::prefetch: {
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00005109 SDValue Ops[5];
Dale Johannesen1de4aa92010-10-26 23:11:10 +00005110 unsigned rw = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005111 Ops[0] = getRoot();
Gabor Greif0635f352010-06-25 09:38:13 +00005112 Ops[1] = getValue(I.getArgOperand(0));
5113 Ops[2] = getValue(I.getArgOperand(1));
5114 Ops[3] = getValue(I.getArgOperand(2));
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00005115 Ops[4] = getValue(I.getArgOperand(3));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005116 DAG.setRoot(DAG.getMemIntrinsicNode(ISD::PREFETCH, sdl,
Dale Johannesen1de4aa92010-10-26 23:11:10 +00005117 DAG.getVTList(MVT::Other),
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00005118 &Ops[0], 5,
Dale Johannesen1de4aa92010-10-26 23:11:10 +00005119 EVT::getIntegerVT(*Context, 8),
5120 MachinePointerInfo(I.getArgOperand(0)),
5121 0, /* align */
5122 false, /* volatile */
5123 rw==0, /* read */
5124 rw==1)); /* write */
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005125 return 0;
5126 }
Duncan Sandsf07c9492009-11-10 09:08:09 +00005127 case Intrinsic::lifetime_start:
Nadav Rotemc05d3062012-09-06 09:17:37 +00005128 case Intrinsic::lifetime_end: {
Nadav Rotemc05d3062012-09-06 09:17:37 +00005129 bool IsStart = (Intrinsic == Intrinsic::lifetime_start);
Nadav Rotem9a2ae002012-09-10 08:43:23 +00005130 // Stack coloring is not enabled in O0, discard region information.
5131 if (TM.getOptLevel() == CodeGenOpt::None)
5132 return 0;
Nadav Rotemc05d3062012-09-06 09:17:37 +00005133
Nadav Rotem9a2ae002012-09-10 08:43:23 +00005134 SmallVector<Value *, 4> Allocas;
5135 GetUnderlyingObjects(I.getArgOperand(1), Allocas, TD);
5136
5137 for (SmallVector<Value*, 4>::iterator Object = Allocas.begin(),
5138 E = Allocas.end(); Object != E; ++Object) {
5139 AllocaInst *LifetimeObject = dyn_cast_or_null<AllocaInst>(*Object);
5140
5141 // Could not find an Alloca.
5142 if (!LifetimeObject)
5143 continue;
5144
5145 int FI = FuncInfo.StaticAllocaMap[LifetimeObject];
5146
5147 SDValue Ops[2];
5148 Ops[0] = getRoot();
5149 Ops[1] = DAG.getFrameIndex(FI, TLI.getPointerTy(), true);
5150 unsigned Opcode = (IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END);
5151
Andrew Trickac6d9be2013-05-25 02:42:55 +00005152 Res = DAG.getNode(Opcode, sdl, MVT::Other, Ops, 2);
Nadav Rotem9a2ae002012-09-10 08:43:23 +00005153 DAG.setRoot(Res);
5154 }
Nadav Rotem5882e562013-02-01 19:25:23 +00005155 return 0;
Nadav Rotemc05d3062012-09-06 09:17:37 +00005156 }
5157 case Intrinsic::invariant_start:
Duncan Sandsf07c9492009-11-10 09:08:09 +00005158 // Discard region information.
Bill Wendling4533cac2010-01-28 21:51:40 +00005159 setValue(&I, DAG.getUNDEF(TLI.getPointerTy()));
Duncan Sandsf07c9492009-11-10 09:08:09 +00005160 return 0;
5161 case Intrinsic::invariant_end:
Duncan Sandsf07c9492009-11-10 09:08:09 +00005162 // Discard region information.
5163 return 0;
Nuno Lopes85b40892012-06-28 22:30:12 +00005164 case Intrinsic::donothing:
5165 // ignore
5166 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005167 }
5168}
5169
Dan Gohman46510a72010-04-15 01:51:59 +00005170void SelectionDAGBuilder::LowerCallTo(ImmutableCallSite CS, SDValue Callee,
Dan Gohman2048b852009-11-23 18:04:58 +00005171 bool isTailCall,
5172 MachineBasicBlock *LandingPad) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005173 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
5174 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
5175 Type *RetTy = FTy->getReturnType();
Chris Lattner512063d2010-04-05 06:19:28 +00005176 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Chris Lattner16112732010-03-14 01:41:15 +00005177 MCSymbol *BeginLabel = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005178
5179 TargetLowering::ArgListTy Args;
5180 TargetLowering::ArgListEntry Entry;
5181 Args.reserve(CS.arg_size());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005182
5183 // Check whether the function can return without sret-demotion.
Dan Gohman84023e02010-07-10 09:00:22 +00005184 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling8b62abd2012-12-30 13:01:51 +00005185 GetReturnInfo(RetTy, CS.getAttributes(), Outs, TLI);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005186
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005187 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Bill Wendling96cb1122012-07-19 00:04:14 +00005188 DAG.getMachineFunction(),
5189 FTy->isVarArg(), Outs,
5190 FTy->getContext());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005191
5192 SDValue DemoteStackSlot;
Chris Lattnerecf42c42010-09-21 16:36:31 +00005193 int DemoteStackIdx = -100;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005194
5195 if (!CanLowerReturn) {
Micah Villmow3574eca2012-10-08 16:38:25 +00005196 uint64_t TySize = TLI.getDataLayout()->getTypeAllocSize(
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005197 FTy->getReturnType());
Micah Villmow3574eca2012-10-08 16:38:25 +00005198 unsigned Align = TLI.getDataLayout()->getPrefTypeAlignment(
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005199 FTy->getReturnType());
5200 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattnerecf42c42010-09-21 16:36:31 +00005201 DemoteStackIdx = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005202 Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005203
Chris Lattnerecf42c42010-09-21 16:36:31 +00005204 DemoteStackSlot = DAG.getFrameIndex(DemoteStackIdx, TLI.getPointerTy());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005205 Entry.Node = DemoteStackSlot;
5206 Entry.Ty = StackSlotPtrType;
5207 Entry.isSExt = false;
5208 Entry.isZExt = false;
5209 Entry.isInReg = false;
5210 Entry.isSRet = true;
5211 Entry.isNest = false;
5212 Entry.isByVal = false;
Stephen Lin456ca042013-04-20 05:14:40 +00005213 Entry.isReturned = false;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005214 Entry.Alignment = Align;
5215 Args.push_back(Entry);
5216 RetTy = Type::getVoidTy(FTy->getContext());
5217 }
5218
Dan Gohman46510a72010-04-15 01:51:59 +00005219 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005220 i != e; ++i) {
Rafael Espindola3fa82832011-05-13 15:18:06 +00005221 const Value *V = *i;
5222
5223 // Skip empty types
5224 if (V->getType()->isEmptyTy())
5225 continue;
5226
5227 SDValue ArgNode = getValue(V);
5228 Entry.Node = ArgNode; Entry.Ty = V->getType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005229
5230 unsigned attrInd = i - CS.arg_begin() + 1;
Stephen Lin456ca042013-04-20 05:14:40 +00005231 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
5232 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
5233 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
5234 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
5235 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
5236 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
5237 Entry.isReturned = CS.paramHasAttr(attrInd, Attribute::Returned);
5238 Entry.Alignment = CS.getParamAlignment(attrInd);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005239 Args.push_back(Entry);
5240 }
5241
Chris Lattner512063d2010-04-05 06:19:28 +00005242 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005243 // Insert a label before the invoke call to mark the try range. This can be
5244 // used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00005245 BeginLabel = MMI.getContext().CreateTempSymbol();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00005246
Jim Grosbachca752c92010-01-28 01:45:32 +00005247 // For SjLj, keep track of which landing pads go with which invokes
5248 // so as to maintain the ordering of pads in the LSDA.
Chris Lattner512063d2010-04-05 06:19:28 +00005249 unsigned CallSiteIndex = MMI.getCurrentCallSite();
Jim Grosbachca752c92010-01-28 01:45:32 +00005250 if (CallSiteIndex) {
Chris Lattner512063d2010-04-05 06:19:28 +00005251 MMI.setCallSiteBeginLabel(BeginLabel, CallSiteIndex);
Bill Wendling30e67402011-10-05 22:24:35 +00005252 LPadToCallSiteMap[LandingPad].push_back(CallSiteIndex);
Bill Wendlinga8512ed2011-10-04 22:00:35 +00005253
Jim Grosbachca752c92010-01-28 01:45:32 +00005254 // Now that the call site is handled, stop tracking it.
Chris Lattner512063d2010-04-05 06:19:28 +00005255 MMI.setCurrentCallSite(0);
Jim Grosbachca752c92010-01-28 01:45:32 +00005256 }
5257
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005258 // Both PendingLoads and PendingExports must be flushed here;
5259 // this call might not return.
5260 (void)getRoot();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005261 DAG.setRoot(DAG.getEHLabel(getCurSDLoc(), getControlRoot(), BeginLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005262 }
5263
Dan Gohman98ca4f22009-08-05 01:29:28 +00005264 // Check if target-independent constraints permit a tail call here.
5265 // Target-dependent constraints are checked within TLI.LowerCallTo.
Bill Wendling1a17bd22013-01-18 21:50:24 +00005266 if (isTailCall && !isInTailCallPosition(CS, TLI))
Dan Gohman98ca4f22009-08-05 01:29:28 +00005267 isTailCall = false;
5268
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00005269 TargetLowering::
5270 CallLoweringInfo CLI(getRoot(), RetTy, FTy, isTailCall, Callee, Args, DAG,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005271 getCurSDLoc(), CS);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00005272 std::pair<SDValue,SDValue> Result = TLI.LowerCallTo(CLI);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005273 assert((isTailCall || Result.second.getNode()) &&
5274 "Non-null chain expected with non-tail call!");
5275 assert((Result.second.getNode() || !Result.first.getNode()) &&
5276 "Null value expected with tail call!");
Bill Wendlinge80ae832009-12-22 00:50:32 +00005277 if (Result.first.getNode()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005278 setValue(CS.getInstruction(), Result.first);
Bill Wendlinge80ae832009-12-22 00:50:32 +00005279 } else if (!CanLowerReturn && Result.second.getNode()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005280 // The instruction result is the result of loading from the
5281 // hidden sret parameter.
5282 SmallVector<EVT, 1> PVTs;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005283 Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005284
5285 ComputeValueVTs(TLI, PtrRetTy, PVTs);
5286 assert(PVTs.size() == 1 && "Pointers should fit in one register");
5287 EVT PtrVT = PVTs[0];
Eli Friedman2db0e9e2012-05-25 00:09:29 +00005288
5289 SmallVector<EVT, 4> RetTys;
5290 SmallVector<uint64_t, 4> Offsets;
5291 RetTy = FTy->getReturnType();
5292 ComputeValueVTs(TLI, RetTy, RetTys, &Offsets);
5293
5294 unsigned NumValues = RetTys.size();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005295 SmallVector<SDValue, 4> Values(NumValues);
5296 SmallVector<SDValue, 4> Chains(NumValues);
5297
5298 for (unsigned i = 0; i < NumValues; ++i) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005299 SDValue Add = DAG.getNode(ISD::ADD, getCurSDLoc(), PtrVT,
Bill Wendlinge80ae832009-12-22 00:50:32 +00005300 DemoteStackSlot,
5301 DAG.getConstant(Offsets[i], PtrVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005302 SDValue L = DAG.getLoad(RetTys[i], getCurSDLoc(), Result.second, Add,
Chris Lattnerecf42c42010-09-21 16:36:31 +00005303 MachinePointerInfo::getFixedStack(DemoteStackIdx, Offsets[i]),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005304 false, false, false, 1);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005305 Values[i] = L;
5306 Chains[i] = L.getValue(1);
5307 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00005308
Andrew Trickac6d9be2013-05-25 02:42:55 +00005309 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005310 MVT::Other, &Chains[0], NumValues);
5311 PendingLoads.push_back(Chain);
Michael J. Spencere70c5262010-10-16 08:25:21 +00005312
Bill Wendling4533cac2010-01-28 21:51:40 +00005313 setValue(CS.getInstruction(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00005314 DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00005315 DAG.getVTList(&RetTys[0], RetTys.size()),
Eli Friedman2db0e9e2012-05-25 00:09:29 +00005316 &Values[0], Values.size()));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005317 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00005318
Evan Cheng8380c032011-04-01 19:42:22 +00005319 if (!Result.second.getNode()) {
Evan Chengc249e482011-04-01 19:57:01 +00005320 // As a special case, a null chain means that a tail call has been emitted and
5321 // the DAG root is already updated.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005322 HasTailCall = true;
Evan Cheng8380c032011-04-01 19:42:22 +00005323 } else {
5324 DAG.setRoot(Result.second);
Evan Cheng8380c032011-04-01 19:42:22 +00005325 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005326
Chris Lattner512063d2010-04-05 06:19:28 +00005327 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005328 // Insert a label at the end of the invoke call to mark the try range. This
5329 // can be used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00005330 MCSymbol *EndLabel = MMI.getContext().CreateTempSymbol();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005331 DAG.setRoot(DAG.getEHLabel(getCurSDLoc(), getRoot(), EndLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005332
5333 // Inform MachineModuleInfo of range.
Chris Lattner512063d2010-04-05 06:19:28 +00005334 MMI.addInvoke(LandingPad, BeginLabel, EndLabel);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005335 }
5336}
5337
Chris Lattner8047d9a2009-12-24 00:37:38 +00005338/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
5339/// value is equal or not-equal to zero.
Dan Gohman46510a72010-04-15 01:51:59 +00005340static bool IsOnlyUsedInZeroEqualityComparison(const Value *V) {
5341 for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end();
Chris Lattner8047d9a2009-12-24 00:37:38 +00005342 UI != E; ++UI) {
Dan Gohman46510a72010-04-15 01:51:59 +00005343 if (const ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
Chris Lattner8047d9a2009-12-24 00:37:38 +00005344 if (IC->isEquality())
Dan Gohman46510a72010-04-15 01:51:59 +00005345 if (const Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
Chris Lattner8047d9a2009-12-24 00:37:38 +00005346 if (C->isNullValue())
5347 continue;
5348 // Unknown instruction.
5349 return false;
5350 }
5351 return true;
5352}
5353
Dan Gohman46510a72010-04-15 01:51:59 +00005354static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005355 Type *LoadTy,
Chris Lattner8047d9a2009-12-24 00:37:38 +00005356 SelectionDAGBuilder &Builder) {
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005357
Chris Lattner8047d9a2009-12-24 00:37:38 +00005358 // Check to see if this load can be trivially constant folded, e.g. if the
5359 // input is from a string literal.
Dan Gohman46510a72010-04-15 01:51:59 +00005360 if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00005361 // Cast pointer to the type we really want to load.
Dan Gohman46510a72010-04-15 01:51:59 +00005362 LoadInput = ConstantExpr::getBitCast(const_cast<Constant *>(LoadInput),
Chris Lattner8047d9a2009-12-24 00:37:38 +00005363 PointerType::getUnqual(LoadTy));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005364
Dan Gohman46510a72010-04-15 01:51:59 +00005365 if (const Constant *LoadCst =
5366 ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput),
5367 Builder.TD))
Chris Lattner8047d9a2009-12-24 00:37:38 +00005368 return Builder.getValue(LoadCst);
5369 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005370
Chris Lattner8047d9a2009-12-24 00:37:38 +00005371 // Otherwise, we have to emit the load. If the pointer is to unfoldable but
5372 // still constant memory, the input chain can be the entry node.
5373 SDValue Root;
5374 bool ConstantMemory = false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005375
Chris Lattner8047d9a2009-12-24 00:37:38 +00005376 // Do not serialize (non-volatile) loads of constant memory with anything.
5377 if (Builder.AA->pointsToConstantMemory(PtrVal)) {
5378 Root = Builder.DAG.getEntryNode();
5379 ConstantMemory = true;
5380 } else {
5381 // Do not serialize non-volatile loads against each other.
5382 Root = Builder.DAG.getRoot();
5383 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005384
Chris Lattner8047d9a2009-12-24 00:37:38 +00005385 SDValue Ptr = Builder.getValue(PtrVal);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005386 SDValue LoadVal = Builder.DAG.getLoad(LoadVT, Builder.getCurSDLoc(), Root,
Chris Lattnerecf42c42010-09-21 16:36:31 +00005387 Ptr, MachinePointerInfo(PtrVal),
David Greene1e559442010-02-15 17:00:31 +00005388 false /*volatile*/,
Pete Cooperd752e0f2011-11-08 18:42:53 +00005389 false /*nontemporal*/,
5390 false /*isinvariant*/, 1 /* align=1 */);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005391
Chris Lattner8047d9a2009-12-24 00:37:38 +00005392 if (!ConstantMemory)
5393 Builder.PendingLoads.push_back(LoadVal.getValue(1));
5394 return LoadVal;
5395}
5396
5397
5398/// visitMemCmpCall - See if we can lower a call to memcmp in an optimized form.
5399/// If so, return true and lower it, otherwise return false and it will be
5400/// lowered like a normal call.
Dan Gohman46510a72010-04-15 01:51:59 +00005401bool SelectionDAGBuilder::visitMemCmpCall(const CallInst &I) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00005402 // Verify that the prototype makes sense. int memcmp(void*,void*,size_t)
Gabor Greif37387d52010-06-30 12:55:46 +00005403 if (I.getNumArgOperands() != 3)
Chris Lattner8047d9a2009-12-24 00:37:38 +00005404 return false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005405
Gabor Greif0635f352010-06-25 09:38:13 +00005406 const Value *LHS = I.getArgOperand(0), *RHS = I.getArgOperand(1);
Duncan Sands1df98592010-02-16 11:11:14 +00005407 if (!LHS->getType()->isPointerTy() || !RHS->getType()->isPointerTy() ||
Gabor Greif0635f352010-06-25 09:38:13 +00005408 !I.getArgOperand(2)->getType()->isIntegerTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +00005409 !I.getType()->isIntegerTy())
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005410 return false;
5411
Gabor Greif0635f352010-06-25 09:38:13 +00005412 const ConstantInt *Size = dyn_cast<ConstantInt>(I.getArgOperand(2));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005413
Chris Lattner8047d9a2009-12-24 00:37:38 +00005414 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
5415 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
Chris Lattner04b091a2009-12-24 01:07:17 +00005416 if (Size && IsOnlyUsedInZeroEqualityComparison(&I)) {
5417 bool ActuallyDoIt = true;
5418 MVT LoadVT;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005419 Type *LoadTy;
Chris Lattner04b091a2009-12-24 01:07:17 +00005420 switch (Size->getZExtValue()) {
5421 default:
5422 LoadVT = MVT::Other;
5423 LoadTy = 0;
5424 ActuallyDoIt = false;
5425 break;
5426 case 2:
5427 LoadVT = MVT::i16;
5428 LoadTy = Type::getInt16Ty(Size->getContext());
5429 break;
5430 case 4:
5431 LoadVT = MVT::i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005432 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005433 break;
5434 case 8:
5435 LoadVT = MVT::i64;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005436 LoadTy = Type::getInt64Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005437 break;
5438 /*
5439 case 16:
5440 LoadVT = MVT::v4i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005441 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005442 LoadTy = VectorType::get(LoadTy, 4);
5443 break;
5444 */
5445 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005446
Chris Lattner04b091a2009-12-24 01:07:17 +00005447 // This turns into unaligned loads. We only do this if the target natively
5448 // supports the MVT we'll be loading or if it is small enough (<= 4) that
5449 // we'll only produce a small number of byte loads.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005450
Chris Lattner04b091a2009-12-24 01:07:17 +00005451 // Require that we can find a legal MVT, and only do this if the target
5452 // supports unaligned loads of that type. Expanding into byte loads would
5453 // bloat the code.
5454 if (ActuallyDoIt && Size->getZExtValue() > 4) {
5455 // TODO: Handle 5 byte compare as 4-byte + 1 byte.
5456 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
5457 if (!TLI.isTypeLegal(LoadVT) ||!TLI.allowsUnalignedMemoryAccesses(LoadVT))
5458 ActuallyDoIt = false;
5459 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005460
Chris Lattner04b091a2009-12-24 01:07:17 +00005461 if (ActuallyDoIt) {
5462 SDValue LHSVal = getMemCmpLoad(LHS, LoadVT, LoadTy, *this);
5463 SDValue RHSVal = getMemCmpLoad(RHS, LoadVT, LoadTy, *this);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005464
Andrew Trickac6d9be2013-05-25 02:42:55 +00005465 SDValue Res = DAG.getSetCC(getCurSDLoc(), MVT::i1, LHSVal, RHSVal,
Chris Lattner04b091a2009-12-24 01:07:17 +00005466 ISD::SETNE);
5467 EVT CallVT = TLI.getValueType(I.getType(), true);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005468 setValue(&I, DAG.getZExtOrTrunc(Res, getCurSDLoc(), CallVT));
Chris Lattner04b091a2009-12-24 01:07:17 +00005469 return true;
5470 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00005471 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005472
5473
Chris Lattner8047d9a2009-12-24 00:37:38 +00005474 return false;
5475}
5476
Bob Wilson53624a22012-08-03 23:29:17 +00005477/// visitUnaryFloatCall - If a call instruction is a unary floating-point
5478/// operation (as expected), translate it to an SDNode with the specified opcode
5479/// and return true.
5480bool SelectionDAGBuilder::visitUnaryFloatCall(const CallInst &I,
5481 unsigned Opcode) {
5482 // Sanity check that it really is a unary floating-point call.
5483 if (I.getNumArgOperands() != 1 ||
5484 !I.getArgOperand(0)->getType()->isFloatingPointTy() ||
5485 I.getType() != I.getArgOperand(0)->getType() ||
5486 !I.onlyReadsMemory())
5487 return false;
5488
5489 SDValue Tmp = getValue(I.getArgOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005490 setValue(&I, DAG.getNode(Opcode, getCurSDLoc(), Tmp.getValueType(), Tmp));
Bob Wilson53624a22012-08-03 23:29:17 +00005491 return true;
5492}
Chris Lattner8047d9a2009-12-24 00:37:38 +00005493
Dan Gohman46510a72010-04-15 01:51:59 +00005494void SelectionDAGBuilder::visitCall(const CallInst &I) {
Chris Lattner598751e2010-07-05 05:36:21 +00005495 // Handle inline assembly differently.
5496 if (isa<InlineAsm>(I.getCalledValue())) {
5497 visitInlineAsm(&I);
5498 return;
5499 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00005500
Michael J. Spencer391b43b2010-10-21 20:49:23 +00005501 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Michael J. Spencerc9c137b2012-02-22 19:06:13 +00005502 ComputeUsesVAFloatArgument(I, &MMI);
Michael J. Spencer391b43b2010-10-21 20:49:23 +00005503
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005504 const char *RenameFn = 0;
5505 if (Function *F = I.getCalledFunction()) {
5506 if (F->isDeclaration()) {
Chris Lattner598751e2010-07-05 05:36:21 +00005507 if (const TargetIntrinsicInfo *II = TM.getIntrinsicInfo()) {
Dale Johannesen49de9822009-02-05 01:49:45 +00005508 if (unsigned IID = II->getIntrinsicID(F)) {
5509 RenameFn = visitIntrinsicCall(I, IID);
5510 if (!RenameFn)
5511 return;
5512 }
5513 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005514 if (unsigned IID = F->getIntrinsicID()) {
5515 RenameFn = visitIntrinsicCall(I, IID);
5516 if (!RenameFn)
5517 return;
5518 }
5519 }
5520
5521 // Check for well-known libc/libm calls. If the function is internal, it
5522 // can't be a library call.
Bob Wilson982dc842012-08-03 21:26:24 +00005523 LibFunc::Func Func;
5524 if (!F->hasLocalLinkage() && F->hasName() &&
5525 LibInfo->getLibFunc(F->getName(), Func) &&
5526 LibInfo->hasOptimizedCodeGen(Func)) {
5527 switch (Func) {
5528 default: break;
5529 case LibFunc::copysign:
5530 case LibFunc::copysignf:
5531 case LibFunc::copysignl:
Gabor Greif37387d52010-06-30 12:55:46 +00005532 if (I.getNumArgOperands() == 2 && // Basic sanity checks.
Gabor Greif0635f352010-06-25 09:38:13 +00005533 I.getArgOperand(0)->getType()->isFloatingPointTy() &&
5534 I.getType() == I.getArgOperand(0)->getType() &&
Bob Wilson53624a22012-08-03 23:29:17 +00005535 I.getType() == I.getArgOperand(1)->getType() &&
5536 I.onlyReadsMemory()) {
Gabor Greif0635f352010-06-25 09:38:13 +00005537 SDValue LHS = getValue(I.getArgOperand(0));
5538 SDValue RHS = getValue(I.getArgOperand(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005539 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurSDLoc(),
Bill Wendling0d580132009-12-23 01:28:19 +00005540 LHS.getValueType(), LHS, RHS));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005541 return;
5542 }
Bob Wilson982dc842012-08-03 21:26:24 +00005543 break;
5544 case LibFunc::fabs:
5545 case LibFunc::fabsf:
5546 case LibFunc::fabsl:
Bob Wilson53624a22012-08-03 23:29:17 +00005547 if (visitUnaryFloatCall(I, ISD::FABS))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005548 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005549 break;
5550 case LibFunc::sin:
5551 case LibFunc::sinf:
5552 case LibFunc::sinl:
Bob Wilson53624a22012-08-03 23:29:17 +00005553 if (visitUnaryFloatCall(I, ISD::FSIN))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005554 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005555 break;
5556 case LibFunc::cos:
5557 case LibFunc::cosf:
5558 case LibFunc::cosl:
Bob Wilson53624a22012-08-03 23:29:17 +00005559 if (visitUnaryFloatCall(I, ISD::FCOS))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005560 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005561 break;
5562 case LibFunc::sqrt:
5563 case LibFunc::sqrtf:
5564 case LibFunc::sqrtl:
Preston Gurdb704d232013-05-27 15:44:35 +00005565 case LibFunc::sqrt_finite:
5566 case LibFunc::sqrtf_finite:
5567 case LibFunc::sqrtl_finite:
Bob Wilson53624a22012-08-03 23:29:17 +00005568 if (visitUnaryFloatCall(I, ISD::FSQRT))
Dale Johannesen52fb79b2009-09-25 17:23:22 +00005569 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005570 break;
5571 case LibFunc::floor:
5572 case LibFunc::floorf:
5573 case LibFunc::floorl:
Bob Wilson53624a22012-08-03 23:29:17 +00005574 if (visitUnaryFloatCall(I, ISD::FFLOOR))
Owen Anderson4a4fdf32011-12-08 19:32:14 +00005575 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005576 break;
5577 case LibFunc::nearbyint:
5578 case LibFunc::nearbyintf:
5579 case LibFunc::nearbyintl:
Bob Wilson53624a22012-08-03 23:29:17 +00005580 if (visitUnaryFloatCall(I, ISD::FNEARBYINT))
Owen Anderson4a4fdf32011-12-08 19:32:14 +00005581 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005582 break;
5583 case LibFunc::ceil:
5584 case LibFunc::ceilf:
5585 case LibFunc::ceill:
Bob Wilson53624a22012-08-03 23:29:17 +00005586 if (visitUnaryFloatCall(I, ISD::FCEIL))
Owen Anderson4a4fdf32011-12-08 19:32:14 +00005587 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005588 break;
5589 case LibFunc::rint:
5590 case LibFunc::rintf:
5591 case LibFunc::rintl:
Bob Wilson53624a22012-08-03 23:29:17 +00005592 if (visitUnaryFloatCall(I, ISD::FRINT))
Owen Anderson4a4fdf32011-12-08 19:32:14 +00005593 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005594 break;
5595 case LibFunc::trunc:
5596 case LibFunc::truncf:
5597 case LibFunc::truncl:
Bob Wilson53624a22012-08-03 23:29:17 +00005598 if (visitUnaryFloatCall(I, ISD::FTRUNC))
Owen Anderson4a4fdf32011-12-08 19:32:14 +00005599 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005600 break;
5601 case LibFunc::log2:
5602 case LibFunc::log2f:
5603 case LibFunc::log2l:
Bob Wilson53624a22012-08-03 23:29:17 +00005604 if (visitUnaryFloatCall(I, ISD::FLOG2))
Owen Anderson4e0adfa2011-12-15 00:54:12 +00005605 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005606 break;
5607 case LibFunc::exp2:
5608 case LibFunc::exp2f:
5609 case LibFunc::exp2l:
Bob Wilson53624a22012-08-03 23:29:17 +00005610 if (visitUnaryFloatCall(I, ISD::FEXP2))
Owen Anderson4e0adfa2011-12-15 00:54:12 +00005611 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005612 break;
5613 case LibFunc::memcmp:
Chris Lattner8047d9a2009-12-24 00:37:38 +00005614 if (visitMemCmpCall(I))
5615 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005616 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005617 }
5618 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005619 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00005620
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005621 SDValue Callee;
5622 if (!RenameFn)
Gabor Greif0635f352010-06-25 09:38:13 +00005623 Callee = getValue(I.getCalledValue());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005624 else
Bill Wendling056292f2008-09-16 21:48:12 +00005625 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005626
Bill Wendling0d580132009-12-23 01:28:19 +00005627 // Check if we can potentially perform a tail call. More detailed checking is
5628 // be done within LowerCallTo, after more information about the call is known.
Evan Cheng11e67932010-01-26 23:13:04 +00005629 LowerCallTo(&I, Callee, I.isTailCall());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005630}
5631
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005632namespace {
Dan Gohman462f6b52010-05-29 17:53:24 +00005633
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005634/// AsmOperandInfo - This contains information for each constraint that we are
5635/// lowering.
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005636class SDISelAsmOperandInfo : public TargetLowering::AsmOperandInfo {
Cedric Venetaff9c272009-02-14 16:06:42 +00005637public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005638 /// CallOperand - If this is the result output operand or a clobber
5639 /// this is null, otherwise it is the incoming operand to the CallInst.
5640 /// This gets modified as the asm is processed.
5641 SDValue CallOperand;
5642
5643 /// AssignedRegs - If this is a register or register class operand, this
5644 /// contains the set of register corresponding to the operand.
5645 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005646
John Thompsoneac6e1d2010-09-13 18:15:37 +00005647 explicit SDISelAsmOperandInfo(const TargetLowering::AsmOperandInfo &info)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005648 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
5649 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005650
Owen Andersone50ed302009-08-10 22:56:29 +00005651 /// getCallOperandValEVT - Return the EVT of the Value* that this operand
Chris Lattner81249c92008-10-17 17:05:25 +00005652 /// corresponds to. If there is no Value* for this operand, it returns
Owen Anderson825b72b2009-08-11 20:47:22 +00005653 /// MVT::Other.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005654 EVT getCallOperandValEVT(LLVMContext &Context,
Owen Anderson1d0be152009-08-13 21:58:54 +00005655 const TargetLowering &TLI,
Micah Villmow3574eca2012-10-08 16:38:25 +00005656 const DataLayout *TD) const {
Owen Anderson825b72b2009-08-11 20:47:22 +00005657 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005658
Chris Lattner81249c92008-10-17 17:05:25 +00005659 if (isa<BasicBlock>(CallOperandVal))
5660 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005661
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005662 llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005663
Eric Christophercef81b72011-05-09 20:04:43 +00005664 // FIXME: code duplicated from TargetLowering::ParseConstraints().
Chris Lattner81249c92008-10-17 17:05:25 +00005665 // If this is an indirect operand, the operand is a pointer to the
5666 // accessed type.
Bob Wilsone261b0c2009-12-22 18:34:19 +00005667 if (isIndirect) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005668 llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
Bob Wilsone261b0c2009-12-22 18:34:19 +00005669 if (!PtrTy)
Chris Lattner75361b62010-04-07 22:58:41 +00005670 report_fatal_error("Indirect operand for inline asm not a pointer!");
Bob Wilsone261b0c2009-12-22 18:34:19 +00005671 OpTy = PtrTy->getElementType();
5672 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005673
Eric Christophercef81b72011-05-09 20:04:43 +00005674 // Look for vector wrapped in a struct. e.g. { <16 x i8> }.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005675 if (StructType *STy = dyn_cast<StructType>(OpTy))
Eric Christophercef81b72011-05-09 20:04:43 +00005676 if (STy->getNumElements() == 1)
5677 OpTy = STy->getElementType(0);
5678
Chris Lattner81249c92008-10-17 17:05:25 +00005679 // If OpTy is not a single value, it may be a struct/union that we
5680 // can tile with integers.
5681 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
5682 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
5683 switch (BitSize) {
5684 default: break;
5685 case 1:
5686 case 8:
5687 case 16:
5688 case 32:
5689 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00005690 case 128:
Owen Anderson1d0be152009-08-13 21:58:54 +00005691 OpTy = IntegerType::get(Context, BitSize);
Chris Lattner81249c92008-10-17 17:05:25 +00005692 break;
5693 }
5694 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005695
Chris Lattner81249c92008-10-17 17:05:25 +00005696 return TLI.getValueType(OpTy, true);
5697 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005698};
Dan Gohman462f6b52010-05-29 17:53:24 +00005699
John Thompson44ab89e2010-10-29 17:29:13 +00005700typedef SmallVector<SDISelAsmOperandInfo,16> SDISelAsmOperandInfoVector;
5701
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005702} // end anonymous namespace
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005703
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005704/// GetRegistersForValue - Assign registers (virtual or physical) for the
5705/// specified operand. We prefer to assign virtual registers, to allow the
Bob Wilson266d9452009-12-17 05:07:36 +00005706/// register allocator to handle the assignment process. However, if the asm
5707/// uses features that we can't model on machineinstrs, we have SDISel do the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005708/// allocation. This produces generally horrible, but correct, code.
5709///
5710/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005711///
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005712static void GetRegistersForValue(SelectionDAG &DAG,
5713 const TargetLowering &TLI,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005714 SDLoc DL,
Benjamin Kramer8b93ff22012-02-24 14:01:17 +00005715 SDISelAsmOperandInfo &OpInfo) {
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005716 LLVMContext &Context = *DAG.getContext();
Owen Anderson23b9b192009-08-12 00:36:31 +00005717
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005718 MachineFunction &MF = DAG.getMachineFunction();
5719 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005720
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005721 // If this is a constraint for a single physreg, or a constraint for a
5722 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005723 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005724 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
5725 OpInfo.ConstraintVT);
5726
5727 unsigned NumRegs = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00005728 if (OpInfo.ConstraintVT != MVT::Other) {
Chris Lattner01426e12008-10-21 00:45:36 +00005729 // If this is a FP input in an integer register (or visa versa) insert a bit
5730 // cast of the input value. More generally, handle any case where the input
5731 // value disagrees with the register class we plan to stick this in.
5732 if (OpInfo.Type == InlineAsm::isInput &&
5733 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00005734 // Try to convert to the first EVT that the reg class contains. If the
Chris Lattner01426e12008-10-21 00:45:36 +00005735 // types are identical size, use a bitcast to convert (e.g. two differing
5736 // vector types).
Patrik Hagglund8963fec2012-12-19 12:23:01 +00005737 MVT RegVT = *PhysReg.second->vt_begin();
Chris Lattner01426e12008-10-21 00:45:36 +00005738 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005739 OpInfo.CallOperand = DAG.getNode(ISD::BITCAST, DL,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005740 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005741 OpInfo.ConstraintVT = RegVT;
5742 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
5743 // If the input is a FP value and we want it in FP registers, do a
5744 // bitcast to the corresponding integer type. This turns an f64 value
5745 // into i64, which can be passed with two i32 values on a 32-bit
5746 // machine.
Patrik Hagglund8963fec2012-12-19 12:23:01 +00005747 RegVT = MVT::getIntegerVT(OpInfo.ConstraintVT.getSizeInBits());
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005748 OpInfo.CallOperand = DAG.getNode(ISD::BITCAST, DL,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005749 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005750 OpInfo.ConstraintVT = RegVT;
5751 }
5752 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005753
Owen Anderson23b9b192009-08-12 00:36:31 +00005754 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00005755 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005756
Patrik Hagglund8963fec2012-12-19 12:23:01 +00005757 MVT RegVT;
Owen Andersone50ed302009-08-10 22:56:29 +00005758 EVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005759
5760 // If this is a constraint for a specific physical register, like {r17},
5761 // assign it now.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005762 if (unsigned AssignedReg = PhysReg.first) {
5763 const TargetRegisterClass *RC = PhysReg.second;
Owen Anderson825b72b2009-08-11 20:47:22 +00005764 if (OpInfo.ConstraintVT == MVT::Other)
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005765 ValueVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005766
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005767 // Get the actual register value type. This is important, because the user
5768 // may have asked for (e.g.) the AX register in i32 type. We need to
5769 // remember that AX is actually i16 to get the right extension.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005770 RegVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005771
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005772 // This is a explicit reference to a physical register.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005773 Regs.push_back(AssignedReg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005774
5775 // If this is an expanded reference, add the rest of the regs to Regs.
5776 if (NumRegs != 1) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005777 TargetRegisterClass::iterator I = RC->begin();
5778 for (; *I != AssignedReg; ++I)
5779 assert(I != RC->end() && "Didn't find reg!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005780
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005781 // Already added the first reg.
5782 --NumRegs; ++I;
5783 for (; NumRegs; --NumRegs, ++I) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005784 assert(I != RC->end() && "Ran out of registers to allocate!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005785 Regs.push_back(*I);
5786 }
5787 }
Bill Wendling651ad132009-12-22 01:25:10 +00005788
Dan Gohman7451d3e2010-05-29 17:03:36 +00005789 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005790 return;
5791 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005792
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005793 // Otherwise, if this was a reference to an LLVM register class, create vregs
5794 // for this reference.
Chris Lattnerb3b44842009-03-24 15:25:07 +00005795 if (const TargetRegisterClass *RC = PhysReg.second) {
5796 RegVT = *RC->vt_begin();
Owen Anderson825b72b2009-08-11 20:47:22 +00005797 if (OpInfo.ConstraintVT == MVT::Other)
Evan Chengfb112882009-03-23 08:01:15 +00005798 ValueVT = RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005799
Evan Chengfb112882009-03-23 08:01:15 +00005800 // Create the appropriate number of virtual registers.
5801 MachineRegisterInfo &RegInfo = MF.getRegInfo();
5802 for (; NumRegs; --NumRegs)
Chris Lattnerb3b44842009-03-24 15:25:07 +00005803 Regs.push_back(RegInfo.createVirtualRegister(RC));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005804
Dan Gohman7451d3e2010-05-29 17:03:36 +00005805 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
Evan Chengfb112882009-03-23 08:01:15 +00005806 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005807 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005808
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005809 // Otherwise, we couldn't allocate enough registers for this.
5810}
5811
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005812/// visitInlineAsm - Handle a call to an InlineAsm object.
5813///
Dan Gohman46510a72010-04-15 01:51:59 +00005814void SelectionDAGBuilder::visitInlineAsm(ImmutableCallSite CS) {
5815 const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005816
5817 /// ConstraintOperands - Information about all of the constraints.
John Thompson44ab89e2010-10-29 17:29:13 +00005818 SDISelAsmOperandInfoVector ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005819
Evan Chengce1cdac2011-05-06 20:52:23 +00005820 TargetLowering::AsmOperandInfoVector
5821 TargetConstraints = TLI.ParseConstraints(CS);
5822
John Thompsoneac6e1d2010-09-13 18:15:37 +00005823 bool hasMemory = false;
Michael J. Spencere70c5262010-10-16 08:25:21 +00005824
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005825 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
5826 unsigned ResNo = 0; // ResNo - The result number of the next output.
John Thompsoneac6e1d2010-09-13 18:15:37 +00005827 for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) {
5828 ConstraintOperands.push_back(SDISelAsmOperandInfo(TargetConstraints[i]));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005829 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Michael J. Spencere70c5262010-10-16 08:25:21 +00005830
Patrik Hagglundc698d3a2012-12-19 15:19:11 +00005831 MVT OpVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005832
5833 // Compute the value type for each operand.
5834 switch (OpInfo.Type) {
5835 case InlineAsm::isOutput:
5836 // Indirect outputs just consume an argument.
5837 if (OpInfo.isIndirect) {
Dan Gohman46510a72010-04-15 01:51:59 +00005838 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005839 break;
5840 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005841
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005842 // The return value of the call is this value. As such, there is no
5843 // corresponding argument.
Nick Lewycky8de34002011-09-30 22:19:53 +00005844 assert(!CS.getType()->isVoidTy() && "Bad inline asm!");
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005845 if (StructType *STy = dyn_cast<StructType>(CS.getType())) {
Patrik Hagglundc698d3a2012-12-19 15:19:11 +00005846 OpVT = TLI.getSimpleValueType(STy->getElementType(ResNo));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005847 } else {
5848 assert(ResNo == 0 && "Asm only has one result!");
Patrik Hagglundc698d3a2012-12-19 15:19:11 +00005849 OpVT = TLI.getSimpleValueType(CS.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005850 }
5851 ++ResNo;
5852 break;
5853 case InlineAsm::isInput:
Dan Gohman46510a72010-04-15 01:51:59 +00005854 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005855 break;
5856 case InlineAsm::isClobber:
5857 // Nothing to do.
5858 break;
5859 }
5860
5861 // If this is an input or an indirect output, process the call argument.
5862 // BasicBlocks are labels, currently appearing only in asm's.
5863 if (OpInfo.CallOperandVal) {
Dan Gohman46510a72010-04-15 01:51:59 +00005864 if (const BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005865 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00005866 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005867 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005868 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005869
Patrik Hagglundc698d3a2012-12-19 15:19:11 +00005870 OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD).
5871 getSimpleVT();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005872 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005873
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005874 OpInfo.ConstraintVT = OpVT;
Michael J. Spencere70c5262010-10-16 08:25:21 +00005875
John Thompsoneac6e1d2010-09-13 18:15:37 +00005876 // Indirect operand accesses access memory.
5877 if (OpInfo.isIndirect)
5878 hasMemory = true;
5879 else {
5880 for (unsigned j = 0, ee = OpInfo.Codes.size(); j != ee; ++j) {
Evan Chengce1cdac2011-05-06 20:52:23 +00005881 TargetLowering::ConstraintType
5882 CType = TLI.getConstraintType(OpInfo.Codes[j]);
John Thompsoneac6e1d2010-09-13 18:15:37 +00005883 if (CType == TargetLowering::C_Memory) {
5884 hasMemory = true;
5885 break;
5886 }
5887 }
5888 }
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005889 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005890
John Thompsoneac6e1d2010-09-13 18:15:37 +00005891 SDValue Chain, Flag;
5892
5893 // We won't need to flush pending loads if this asm doesn't touch
5894 // memory and is nonvolatile.
5895 if (hasMemory || IA->hasSideEffects())
5896 Chain = getRoot();
5897 else
5898 Chain = DAG.getRoot();
5899
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005900 // Second pass over the constraints: compute which constraint option to use
5901 // and assign registers to constraints that want a specific physreg.
John Thompsoneac6e1d2010-09-13 18:15:37 +00005902 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005903 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005904
John Thompson54584742010-09-24 22:24:05 +00005905 // If this is an output operand with a matching input operand, look up the
5906 // matching input. If their types mismatch, e.g. one is an integer, the
5907 // other is floating point, or their sizes are different, flag it as an
5908 // error.
5909 if (OpInfo.hasMatchingInput()) {
5910 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
Michael J. Spencere70c5262010-10-16 08:25:21 +00005911
John Thompson54584742010-09-24 22:24:05 +00005912 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Bill Wendling96cb1122012-07-19 00:04:14 +00005913 std::pair<unsigned, const TargetRegisterClass*> MatchRC =
5914 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
Evan Cheng1dafa702011-08-23 19:17:21 +00005915 OpInfo.ConstraintVT);
Bill Wendling96cb1122012-07-19 00:04:14 +00005916 std::pair<unsigned, const TargetRegisterClass*> InputRC =
5917 TLI.getRegForInlineAsmConstraint(Input.ConstraintCode,
Evan Cheng1dafa702011-08-23 19:17:21 +00005918 Input.ConstraintVT);
John Thompson54584742010-09-24 22:24:05 +00005919 if ((OpInfo.ConstraintVT.isInteger() !=
5920 Input.ConstraintVT.isInteger()) ||
Eric Christopher5427ede2011-07-14 20:13:52 +00005921 (MatchRC.second != InputRC.second)) {
John Thompson54584742010-09-24 22:24:05 +00005922 report_fatal_error("Unsupported asm: input constraint"
5923 " with a matching output constraint of"
5924 " incompatible type!");
5925 }
5926 Input.ConstraintVT = OpInfo.ConstraintVT;
5927 }
5928 }
5929
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005930 // Compute the constraint code and ConstraintType to use.
Dale Johannesen1784d162010-06-25 21:55:36 +00005931 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005932
Eric Christopherfffe3632013-01-11 18:12:39 +00005933 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
5934 OpInfo.Type == InlineAsm::isClobber)
5935 continue;
5936
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005937 // If this is a memory input, and if the operand is not indirect, do what we
5938 // need to to provide an address for the memory input.
5939 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
5940 !OpInfo.isIndirect) {
Evan Chengce1cdac2011-05-06 20:52:23 +00005941 assert((OpInfo.isMultipleAlternative ||
5942 (OpInfo.Type == InlineAsm::isInput)) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005943 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005944
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005945 // Memory operands really want the address of the value. If we don't have
5946 // an indirect input, put it in the constpool if we can, otherwise spill
5947 // it to a stack slot.
Eric Christophere0b42c02011-06-03 17:21:23 +00005948 // TODO: This isn't quite right. We need to handle these according to
5949 // the addressing mode that the constraint wants. Also, this may take
5950 // an additional register for the computation and we don't want that
5951 // either.
Eric Christopher471e4222011-06-08 23:55:35 +00005952
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005953 // If the operand is a float, integer, or vector constant, spill to a
5954 // constant pool entry to get its address.
Dan Gohman46510a72010-04-15 01:51:59 +00005955 const Value *OpVal = OpInfo.CallOperandVal;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005956 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
Chris Lattnera78fa8c2012-01-27 03:08:05 +00005957 isa<ConstantVector>(OpVal) || isa<ConstantDataVector>(OpVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005958 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
5959 TLI.getPointerTy());
5960 } else {
5961 // Otherwise, create a stack slot and emit a store to it before the
5962 // asm.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005963 Type *Ty = OpVal->getType();
Micah Villmow3574eca2012-10-08 16:38:25 +00005964 uint64_t TySize = TLI.getDataLayout()->getTypeAllocSize(Ty);
5965 unsigned Align = TLI.getDataLayout()->getPrefTypeAlignment(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005966 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005967 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005968 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005969 Chain = DAG.getStore(Chain, getCurSDLoc(),
Chris Lattnerecf42c42010-09-21 16:36:31 +00005970 OpInfo.CallOperand, StackSlot,
5971 MachinePointerInfo::getFixedStack(SSFI),
David Greene1e559442010-02-15 17:00:31 +00005972 false, false, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005973 OpInfo.CallOperand = StackSlot;
5974 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005975
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005976 // There is no longer a Value* corresponding to this operand.
5977 OpInfo.CallOperandVal = 0;
Bill Wendling651ad132009-12-22 01:25:10 +00005978
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005979 // It is now an indirect operand.
5980 OpInfo.isIndirect = true;
5981 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005982
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005983 // If this constraint is for a specific register, allocate it before
5984 // anything else.
5985 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005986 GetRegistersForValue(DAG, TLI, getCurSDLoc(), OpInfo);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005987 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005988
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005989 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00005990 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005991 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5992 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005993
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005994 // C_Register operands have already been allocated, Other/Memory don't need
5995 // to be.
5996 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005997 GetRegistersForValue(DAG, TLI, getCurSDLoc(), OpInfo);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005998 }
5999
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006000 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
6001 std::vector<SDValue> AsmNodeOperands;
6002 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
6003 AsmNodeOperands.push_back(
Dan Gohmanf2d7fb32010-01-04 21:00:54 +00006004 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(),
6005 TLI.getPointerTy()));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006006
Chris Lattnerdecc2672010-04-07 05:20:54 +00006007 // If we have a !srcloc metadata node associated with it, we want to attach
6008 // this to the ultimately generated inline asm machineinstr. To do this, we
6009 // pass in the third operand as this (potentially null) inline asm MDNode.
6010 const MDNode *SrcLoc = CS.getInstruction()->getMetadata("srcloc");
6011 AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006012
Chad Rosier3d716882012-10-30 19:11:54 +00006013 // Remember the HasSideEffect, AlignStack, AsmDialect, MayLoad and MayStore
6014 // bits as operand 3.
Evan Chengc36b7062011-01-07 23:50:32 +00006015 unsigned ExtraInfo = 0;
6016 if (IA->hasSideEffects())
6017 ExtraInfo |= InlineAsm::Extra_HasSideEffects;
6018 if (IA->isAlignStack())
6019 ExtraInfo |= InlineAsm::Extra_IsAlignStack;
Chad Rosier77fffa62012-09-05 22:17:43 +00006020 // Set the asm dialect.
Chad Rosier2f1d8152012-09-05 22:40:13 +00006021 ExtraInfo |= IA->getDialect() * InlineAsm::Extra_AsmDialect;
Chad Rosier3d716882012-10-30 19:11:54 +00006022
6023 // Determine if this InlineAsm MayLoad or MayStore based on the constraints.
6024 for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) {
6025 TargetLowering::AsmOperandInfo &OpInfo = TargetConstraints[i];
6026
6027 // Compute the constraint code and ConstraintType to use.
6028 TLI.ComputeConstraintToUse(OpInfo, SDValue());
6029
Chad Rosierdfa4cec2012-10-30 20:01:12 +00006030 // Ideally, we would only check against memory constraints. However, the
6031 // meaning of an other constraint can be target-specific and we can't easily
6032 // reason about it. Therefore, be conservative and set MayLoad/MayStore
6033 // for other constriants as well.
Chad Rosier3d716882012-10-30 19:11:54 +00006034 if (OpInfo.ConstraintType == TargetLowering::C_Memory ||
6035 OpInfo.ConstraintType == TargetLowering::C_Other) {
6036 if (OpInfo.Type == InlineAsm::isInput)
6037 ExtraInfo |= InlineAsm::Extra_MayLoad;
6038 else if (OpInfo.Type == InlineAsm::isOutput)
6039 ExtraInfo |= InlineAsm::Extra_MayStore;
Eric Christopherfffe3632013-01-11 18:12:39 +00006040 else if (OpInfo.Type == InlineAsm::isClobber)
6041 ExtraInfo |= (InlineAsm::Extra_MayLoad | InlineAsm::Extra_MayStore);
Chad Rosier3d716882012-10-30 19:11:54 +00006042 }
6043 }
6044
Evan Chengc36b7062011-01-07 23:50:32 +00006045 AsmNodeOperands.push_back(DAG.getTargetConstant(ExtraInfo,
6046 TLI.getPointerTy()));
Dale Johannesenf1e309e2010-07-02 20:16:09 +00006047
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006048 // Loop over all of the inputs, copying the operand values into the
6049 // appropriate registers and processing the output regs.
6050 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006051
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006052 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
6053 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006054
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006055 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
6056 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
6057
6058 switch (OpInfo.Type) {
6059 case InlineAsm::isOutput: {
6060 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
6061 OpInfo.ConstraintType != TargetLowering::C_Register) {
6062 // Memory output, or 'other' output (e.g. 'X' constraint).
6063 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
6064
6065 // Add information to the INLINEASM node to know about this output.
Chris Lattnerdecc2672010-04-07 05:20:54 +00006066 unsigned OpFlags = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
6067 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006068 TLI.getPointerTy()));
6069 AsmNodeOperands.push_back(OpInfo.CallOperand);
6070 break;
6071 }
6072
6073 // Otherwise, this is a register or register class output.
6074
6075 // Copy the output from the appropriate register. Find a register that
6076 // we can use.
Chris Lattnerfcd70902012-01-03 23:51:01 +00006077 if (OpInfo.AssignedRegs.Regs.empty()) {
6078 LLVMContext &Ctx = *DAG.getContext();
6079 Ctx.emitError(CS.getInstruction(),
6080 "couldn't allocate output register for constraint '" +
6081 Twine(OpInfo.ConstraintCode) + "'");
6082 break;
6083 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006084
6085 // If this is an indirect operand, store through the pointer after the
6086 // asm.
6087 if (OpInfo.isIndirect) {
6088 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
6089 OpInfo.CallOperandVal));
6090 } else {
6091 // This is the result value of the call.
Benjamin Kramerf0127052010-01-05 13:12:22 +00006092 assert(!CS.getType()->isVoidTy() && "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006093 // Concatenate this output onto the outputs list.
6094 RetValRegs.append(OpInfo.AssignedRegs);
6095 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006096
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006097 // Add information to the INLINEASM node to know that this register is
6098 // set.
Dale Johannesen913d3df2008-09-12 17:49:03 +00006099 OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ?
Chris Lattnerdecc2672010-04-07 05:20:54 +00006100 InlineAsm::Kind_RegDefEarlyClobber :
6101 InlineAsm::Kind_RegDef,
Evan Chengfb112882009-03-23 08:01:15 +00006102 false,
6103 0,
Bill Wendling46ada192010-03-02 01:55:18 +00006104 DAG,
Bill Wendling651ad132009-12-22 01:25:10 +00006105 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006106 break;
6107 }
6108 case InlineAsm::isInput: {
6109 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006110
Chris Lattner6bdcda32008-10-17 16:47:46 +00006111 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006112 // If this is required to match an output register we have already set,
6113 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00006114 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006115
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006116 // Scan until we find the definition we already emitted of this operand.
6117 // When we find it, create a RegsForValue operand.
Chris Lattnerdecc2672010-04-07 05:20:54 +00006118 unsigned CurOp = InlineAsm::Op_FirstOperand;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006119 for (; OperandNo; --OperandNo) {
6120 // Advance to the next operand.
Evan Cheng697cbbf2009-03-20 18:03:34 +00006121 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006122 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00006123 assert((InlineAsm::isRegDefKind(OpFlag) ||
6124 InlineAsm::isRegDefEarlyClobberKind(OpFlag) ||
6125 InlineAsm::isMemKind(OpFlag)) && "Skipped past definitions?");
Evan Cheng697cbbf2009-03-20 18:03:34 +00006126 CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006127 }
6128
Evan Cheng697cbbf2009-03-20 18:03:34 +00006129 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006130 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00006131 if (InlineAsm::isRegDefKind(OpFlag) ||
6132 InlineAsm::isRegDefEarlyClobberKind(OpFlag)) {
Evan Cheng697cbbf2009-03-20 18:03:34 +00006133 // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
Chris Lattner6129c372010-04-08 00:09:16 +00006134 if (OpInfo.isIndirect) {
6135 // This happens on gcc/testsuite/gcc.dg/pr8788-1.c
Dan Gohman99be8ae2010-04-19 22:41:47 +00006136 LLVMContext &Ctx = *DAG.getContext();
Chris Lattner6129c372010-04-08 00:09:16 +00006137 Ctx.emitError(CS.getInstruction(), "inline asm not supported yet:"
6138 " don't know how to handle tied "
6139 "indirect register inputs");
Chad Rosier75900222013-03-01 19:12:05 +00006140 report_fatal_error("Cannot handle indirect register inputs!");
Chris Lattner6129c372010-04-08 00:09:16 +00006141 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00006142
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006143 RegsForValue MatchedRegs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006144 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00006145 MVT RegVT = AsmNodeOperands[CurOp+1].getSimpleValueType();
Evan Chengfb112882009-03-23 08:01:15 +00006146 MatchedRegs.RegVTs.push_back(RegVT);
6147 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
Evan Cheng697cbbf2009-03-20 18:03:34 +00006148 for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag);
Chad Rosier2871ba92013-04-24 22:53:10 +00006149 i != e; ++i) {
6150 if (const TargetRegisterClass *RC = TLI.getRegClassFor(RegVT))
6151 MatchedRegs.Regs.push_back(RegInfo.createVirtualRegister(RC));
6152 else {
6153 LLVMContext &Ctx = *DAG.getContext();
6154 Ctx.emitError(CS.getInstruction(), "inline asm error: This value"
6155 " type register class is not natively supported!");
6156 report_fatal_error("inline asm error: This value type register "
6157 "class is not natively supported!");
6158 }
6159 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006160 // Use the produced MatchedRegs object to
Andrew Trickac6d9be2013-05-25 02:42:55 +00006161 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurSDLoc(),
Bill Wendlingf18eb582012-09-26 06:16:18 +00006162 Chain, &Flag, CS.getInstruction());
Chris Lattnerdecc2672010-04-07 05:20:54 +00006163 MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse,
Evan Chengfb112882009-03-23 08:01:15 +00006164 true, OpInfo.getMatchedOperand(),
Bill Wendling46ada192010-03-02 01:55:18 +00006165 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006166 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006167 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00006168
Chris Lattnerdecc2672010-04-07 05:20:54 +00006169 assert(InlineAsm::isMemKind(OpFlag) && "Unknown matching constraint!");
6170 assert(InlineAsm::getNumOperandRegisters(OpFlag) == 1 &&
6171 "Unexpected number of operands");
6172 // Add information to the INLINEASM node to know about this input.
6173 // See InlineAsm.h isUseOperandTiedToDef.
6174 OpFlag = InlineAsm::getFlagWordForMatchingOp(OpFlag,
6175 OpInfo.getMatchedOperand());
6176 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag,
6177 TLI.getPointerTy()));
6178 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
6179 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006180 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006181
Dale Johannesenb5611a62010-07-13 20:17:05 +00006182 // Treat indirect 'X' constraint as memory.
Michael J. Spencere70c5262010-10-16 08:25:21 +00006183 if (OpInfo.ConstraintType == TargetLowering::C_Other &&
6184 OpInfo.isIndirect)
Dale Johannesenb5611a62010-07-13 20:17:05 +00006185 OpInfo.ConstraintType = TargetLowering::C_Memory;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006186
Dale Johannesenb5611a62010-07-13 20:17:05 +00006187 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006188 std::vector<SDValue> Ops;
Eric Christopher100c8332011-06-02 23:16:42 +00006189 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode,
Dale Johannesen1784d162010-06-25 21:55:36 +00006190 Ops, DAG);
Chris Lattnerfcd70902012-01-03 23:51:01 +00006191 if (Ops.empty()) {
6192 LLVMContext &Ctx = *DAG.getContext();
6193 Ctx.emitError(CS.getInstruction(),
6194 "invalid operand for inline asm constraint '" +
6195 Twine(OpInfo.ConstraintCode) + "'");
6196 break;
6197 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006198
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006199 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00006200 unsigned ResOpType =
6201 InlineAsm::getFlagWord(InlineAsm::Kind_Imm, Ops.size());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006202 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006203 TLI.getPointerTy()));
6204 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
6205 break;
Chris Lattnerdecc2672010-04-07 05:20:54 +00006206 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00006207
Chris Lattnerdecc2672010-04-07 05:20:54 +00006208 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006209 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
6210 assert(InOperandVal.getValueType() == TLI.getPointerTy() &&
6211 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006212
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006213 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00006214 unsigned ResOpType = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
Dale Johannesen86b49f82008-09-24 01:07:17 +00006215 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006216 TLI.getPointerTy()));
6217 AsmNodeOperands.push_back(InOperandVal);
6218 break;
6219 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006220
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006221 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
6222 OpInfo.ConstraintType == TargetLowering::C_Register) &&
6223 "Unknown constraint type!");
Eric Christopher9eb4f8a2012-07-02 21:16:43 +00006224
6225 // TODO: Support this.
6226 if (OpInfo.isIndirect) {
6227 LLVMContext &Ctx = *DAG.getContext();
6228 Ctx.emitError(CS.getInstruction(),
6229 "Don't know how to handle indirect register inputs yet "
6230 "for constraint '" + Twine(OpInfo.ConstraintCode) + "'");
6231 break;
6232 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006233
6234 // Copy the input into the appropriate registers.
Chris Lattnerfcd70902012-01-03 23:51:01 +00006235 if (OpInfo.AssignedRegs.Regs.empty()) {
6236 LLVMContext &Ctx = *DAG.getContext();
6237 Ctx.emitError(CS.getInstruction(),
6238 "couldn't allocate input reg for constraint '" +
6239 Twine(OpInfo.ConstraintCode) + "'");
6240 break;
6241 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006242
Andrew Trickac6d9be2013-05-25 02:42:55 +00006243 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurSDLoc(),
Bill Wendlingf18eb582012-09-26 06:16:18 +00006244 Chain, &Flag, CS.getInstruction());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006245
Chris Lattnerdecc2672010-04-07 05:20:54 +00006246 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse, false, 0,
Bill Wendling46ada192010-03-02 01:55:18 +00006247 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006248 break;
6249 }
6250 case InlineAsm::isClobber: {
6251 // Add the clobbered value to the operand list, so that the register
6252 // allocator is aware that the physreg got clobbered.
6253 if (!OpInfo.AssignedRegs.Regs.empty())
Jakob Stoklund Olesenf792fa92011-06-27 04:08:33 +00006254 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_Clobber,
Bill Wendling46ada192010-03-02 01:55:18 +00006255 false, 0, DAG,
Bill Wendling651ad132009-12-22 01:25:10 +00006256 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006257 break;
6258 }
6259 }
6260 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006261
Chris Lattnerdecc2672010-04-07 05:20:54 +00006262 // Finish up input operands. Set the input chain and add the flag last.
Dale Johannesenf1e309e2010-07-02 20:16:09 +00006263 AsmNodeOperands[InlineAsm::Op_InputChain] = Chain;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006264 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006265
Andrew Trickac6d9be2013-05-25 02:42:55 +00006266 Chain = DAG.getNode(ISD::INLINEASM, getCurSDLoc(),
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006267 DAG.getVTList(MVT::Other, MVT::Glue),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006268 &AsmNodeOperands[0], AsmNodeOperands.size());
6269 Flag = Chain.getValue(1);
6270
6271 // If this asm returns a register value, copy the result from that register
6272 // and set it as the value of the call.
6273 if (!RetValRegs.Regs.empty()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006274 SDValue Val = RetValRegs.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(),
Bill Wendling12931302012-09-26 04:04:19 +00006275 Chain, &Flag, CS.getInstruction());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006276
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006277 // FIXME: Why don't we do this for inline asms with MRVs?
6278 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
Owen Andersone50ed302009-08-10 22:56:29 +00006279 EVT ResultType = TLI.getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006280
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006281 // If any of the results of the inline asm is a vector, it may have the
6282 // wrong width/num elts. This can happen for register classes that can
6283 // contain multiple different value types. The preg or vreg allocated may
6284 // not have the same VT as was expected. Convert it to the right type
6285 // with bit_convert.
6286 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006287 Val = DAG.getNode(ISD::BITCAST, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00006288 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00006289
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006290 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006291 ResultType.isInteger() && Val.getValueType().isInteger()) {
6292 // If a result value was tied to an input value, the computed result may
6293 // have a wider width than the expected result. Extract the relevant
6294 // portion.
Andrew Trickac6d9be2013-05-25 02:42:55 +00006295 Val = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00006296 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006297
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006298 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Chris Lattner0c526442008-10-17 17:52:49 +00006299 }
Dan Gohman95915732008-10-18 01:03:45 +00006300
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006301 setValue(CS.getInstruction(), Val);
Dale Johannesenec65a7d2009-04-14 00:56:56 +00006302 // Don't need to use this as a chain in this case.
6303 if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty())
6304 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006305 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006306
Dan Gohman46510a72010-04-15 01:51:59 +00006307 std::vector<std::pair<SDValue, const Value *> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006308
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006309 // Process indirect outputs, first output all of the flagged copies out of
6310 // physregs.
6311 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
6312 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
Dan Gohman46510a72010-04-15 01:51:59 +00006313 const Value *Ptr = IndirectStoresToEmit[i].second;
Andrew Trickac6d9be2013-05-25 02:42:55 +00006314 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(),
Bill Wendling12931302012-09-26 04:04:19 +00006315 Chain, &Flag, IA);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006316 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
6317 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006318
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006319 // Emit the non-flagged stores from the physregs.
6320 SmallVector<SDValue, 8> OutChains;
Bill Wendling651ad132009-12-22 01:25:10 +00006321 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006322 SDValue Val = DAG.getStore(Chain, getCurSDLoc(),
Bill Wendling651ad132009-12-22 01:25:10 +00006323 StoresToEmit[i].first,
6324 getValue(StoresToEmit[i].second),
Chris Lattner84bd98a2010-09-21 18:58:22 +00006325 MachinePointerInfo(StoresToEmit[i].second),
David Greene1e559442010-02-15 17:00:31 +00006326 false, false, 0);
Bill Wendling651ad132009-12-22 01:25:10 +00006327 OutChains.push_back(Val);
Bill Wendling651ad132009-12-22 01:25:10 +00006328 }
6329
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006330 if (!OutChains.empty())
Andrew Trickac6d9be2013-05-25 02:42:55 +00006331 Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006332 &OutChains[0], OutChains.size());
Bill Wendling651ad132009-12-22 01:25:10 +00006333
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006334 DAG.setRoot(Chain);
6335}
6336
Dan Gohman46510a72010-04-15 01:51:59 +00006337void SelectionDAGBuilder::visitVAStart(const CallInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006338 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurSDLoc(),
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006339 MVT::Other, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00006340 getValue(I.getArgOperand(0)),
6341 DAG.getSrcValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006342}
6343
Dan Gohman46510a72010-04-15 01:51:59 +00006344void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) {
Micah Villmow3574eca2012-10-08 16:38:25 +00006345 const DataLayout &TD = *TLI.getDataLayout();
Andrew Trickac6d9be2013-05-25 02:42:55 +00006346 SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurSDLoc(),
Dale Johannesena04b7572009-02-03 23:04:43 +00006347 getRoot(), getValue(I.getOperand(0)),
Rafael Espindolacbeeae22010-07-11 04:01:49 +00006348 DAG.getSrcValue(I.getOperand(0)),
Rafael Espindola9d544d02010-07-12 18:11:17 +00006349 TD.getABITypeAlignment(I.getType()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006350 setValue(&I, V);
6351 DAG.setRoot(V.getValue(1));
6352}
6353
Dan Gohman46510a72010-04-15 01:51:59 +00006354void SelectionDAGBuilder::visitVAEnd(const CallInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006355 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurSDLoc(),
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006356 MVT::Other, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00006357 getValue(I.getArgOperand(0)),
6358 DAG.getSrcValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006359}
6360
Dan Gohman46510a72010-04-15 01:51:59 +00006361void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006362 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurSDLoc(),
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006363 MVT::Other, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00006364 getValue(I.getArgOperand(0)),
6365 getValue(I.getArgOperand(1)),
6366 DAG.getSrcValue(I.getArgOperand(0)),
6367 DAG.getSrcValue(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006368}
6369
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006370/// TargetLowering::LowerCallTo - This is the default LowerCallTo
Dan Gohman98ca4f22009-08-05 01:29:28 +00006371/// implementation, which just calls LowerCall.
6372/// FIXME: When all targets are
6373/// migrated to using LowerCall, this hook should be integrated into SDISel.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006374std::pair<SDValue, SDValue>
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006375TargetLowering::LowerCallTo(TargetLowering::CallLoweringInfo &CLI) const {
Stephen Lin3484da92013-04-30 22:49:28 +00006376 // Handle the incoming return values from the call.
6377 CLI.Ins.clear();
6378 SmallVector<EVT, 4> RetTys;
6379 ComputeValueVTs(*this, CLI.RetTy, RetTys);
6380 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
6381 EVT VT = RetTys[I];
6382 MVT RegisterVT = getRegisterType(CLI.RetTy->getContext(), VT);
6383 unsigned NumRegs = getNumRegisters(CLI.RetTy->getContext(), VT);
6384 for (unsigned i = 0; i != NumRegs; ++i) {
6385 ISD::InputArg MyFlags;
6386 MyFlags.VT = RegisterVT;
6387 MyFlags.Used = CLI.IsReturnValueUsed;
6388 if (CLI.RetSExt)
6389 MyFlags.Flags.setSExt();
6390 if (CLI.RetZExt)
6391 MyFlags.Flags.setZExt();
6392 if (CLI.IsInReg)
6393 MyFlags.Flags.setInReg();
6394 CLI.Ins.push_back(MyFlags);
6395 }
6396 }
6397
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006398 // Handle all of the outgoing arguments.
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006399 CLI.Outs.clear();
6400 CLI.OutVals.clear();
6401 ArgListTy &Args = CLI.Args;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006402 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00006403 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006404 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
6405 for (unsigned Value = 0, NumValues = ValueVTs.size();
6406 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006407 EVT VT = ValueVTs[Value];
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006408 Type *ArgTy = VT.getTypeForEVT(CLI.RetTy->getContext());
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006409 SDValue Op = SDValue(Args[i].Node.getNode(),
6410 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006411 ISD::ArgFlagsTy Flags;
6412 unsigned OriginalAlignment =
Micah Villmow3574eca2012-10-08 16:38:25 +00006413 getDataLayout()->getABITypeAlignment(ArgTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006414
6415 if (Args[i].isZExt)
6416 Flags.setZExt();
6417 if (Args[i].isSExt)
6418 Flags.setSExt();
6419 if (Args[i].isInReg)
6420 Flags.setInReg();
6421 if (Args[i].isSRet)
6422 Flags.setSRet();
6423 if (Args[i].isByVal) {
6424 Flags.setByVal();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006425 PointerType *Ty = cast<PointerType>(Args[i].Ty);
6426 Type *ElementTy = Ty->getElementType();
Micah Villmow3574eca2012-10-08 16:38:25 +00006427 Flags.setByValSize(getDataLayout()->getTypeAllocSize(ElementTy));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006428 // For ByVal, alignment should come from FE. BE will guess if this
6429 // info is not there but there are cases it cannot get right.
Chris Lattner9db20f32011-05-22 23:23:02 +00006430 unsigned FrameAlign;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006431 if (Args[i].Alignment)
6432 FrameAlign = Args[i].Alignment;
Chris Lattner9db20f32011-05-22 23:23:02 +00006433 else
6434 FrameAlign = getByValTypeAlignment(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006435 Flags.setByValAlign(FrameAlign);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006436 }
6437 if (Args[i].isNest)
6438 Flags.setNest();
6439 Flags.setOrigAlign(OriginalAlignment);
6440
Patrik Hagglunddfcf33a2012-12-19 11:48:16 +00006441 MVT PartVT = getRegisterType(CLI.RetTy->getContext(), VT);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006442 unsigned NumParts = getNumRegisters(CLI.RetTy->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006443 SmallVector<SDValue, 4> Parts(NumParts);
6444 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
6445
6446 if (Args[i].isSExt)
6447 ExtendKind = ISD::SIGN_EXTEND;
6448 else if (Args[i].isZExt)
6449 ExtendKind = ISD::ZERO_EXTEND;
6450
Stephen Lin3484da92013-04-30 22:49:28 +00006451 // Conservatively only handle 'returned' on non-vectors for now
6452 if (Args[i].isReturned && !Op.getValueType().isVector()) {
6453 assert(CLI.RetTy == Args[i].Ty && RetTys.size() == NumValues &&
6454 "unexpected use of 'returned'");
6455 // Before passing 'returned' to the target lowering code, ensure that
6456 // either the register MVT and the actual EVT are the same size or that
6457 // the return value and argument are extended in the same way; in these
6458 // cases it's safe to pass the argument register value unchanged as the
6459 // return register value (although it's at the target's option whether
6460 // to do so)
6461 // TODO: allow code generation to take advantage of partially preserved
6462 // registers rather than clobbering the entire register when the
6463 // parameter extension method is not compatible with the return
6464 // extension method
6465 if ((NumParts * PartVT.getSizeInBits() == VT.getSizeInBits()) ||
6466 (ExtendKind != ISD::ANY_EXTEND &&
6467 CLI.RetSExt == Args[i].isSExt && CLI.RetZExt == Args[i].isZExt))
6468 Flags.setReturned();
6469 }
6470
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006471 getCopyToParts(CLI.DAG, CLI.DL, Op, &Parts[0], NumParts,
Bill Wendlingf18eb582012-09-26 06:16:18 +00006472 PartVT, CLI.CS ? CLI.CS->getInstruction() : 0, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006473
Dan Gohman98ca4f22009-08-05 01:29:28 +00006474 for (unsigned j = 0; j != NumParts; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006475 // if it isn't first piece, alignment must be 1
Dan Gohmanc9403652010-07-07 15:54:55 +00006476 ISD::OutputArg MyFlags(Flags, Parts[j].getValueType(),
Manman Ren0a1544d2012-11-01 23:49:58 +00006477 i < CLI.NumFixedArgs,
6478 i, j*Parts[j].getValueType().getStoreSize());
Dan Gohman98ca4f22009-08-05 01:29:28 +00006479 if (NumParts > 1 && j == 0)
6480 MyFlags.Flags.setSplit();
6481 else if (j != 0)
6482 MyFlags.Flags.setOrigAlign(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006483
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006484 CLI.Outs.push_back(MyFlags);
6485 CLI.OutVals.push_back(Parts[j]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006486 }
6487 }
6488 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006489
Dan Gohman98ca4f22009-08-05 01:29:28 +00006490 SmallVector<SDValue, 4> InVals;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006491 CLI.Chain = LowerCall(CLI, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00006492
6493 // Verify that the target's LowerCall behaved as expected.
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006494 assert(CLI.Chain.getNode() && CLI.Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00006495 "LowerCall didn't return a valid chain!");
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006496 assert((!CLI.IsTailCall || InVals.empty()) &&
Dan Gohman5e866062009-08-06 15:37:27 +00006497 "LowerCall emitted a return value for a tail call!");
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006498 assert((CLI.IsTailCall || InVals.size() == CLI.Ins.size()) &&
Dan Gohman5e866062009-08-06 15:37:27 +00006499 "LowerCall didn't emit the correct number of values!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00006500
6501 // For a tail call, the return value is merely live-out and there aren't
6502 // any nodes in the DAG representing it. Return a special value to
6503 // indicate that a tail call has been emitted and no more Instructions
6504 // should be processed in the current block.
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006505 if (CLI.IsTailCall) {
6506 CLI.DAG.setRoot(CLI.Chain);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006507 return std::make_pair(SDValue(), SDValue());
6508 }
6509
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006510 DEBUG(for (unsigned i = 0, e = CLI.Ins.size(); i != e; ++i) {
Evan Chengaf1871f2010-03-11 19:38:18 +00006511 assert(InVals[i].getNode() &&
6512 "LowerCall emitted a null value!");
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006513 assert(EVT(CLI.Ins[i].VT) == InVals[i].getValueType() &&
Evan Chengaf1871f2010-03-11 19:38:18 +00006514 "LowerCall emitted a value with the wrong type!");
6515 });
6516
Dan Gohman98ca4f22009-08-05 01:29:28 +00006517 // Collect the legal value parts into potentially illegal values
6518 // that correspond to the original function's return values.
6519 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006520 if (CLI.RetSExt)
Dan Gohman98ca4f22009-08-05 01:29:28 +00006521 AssertOp = ISD::AssertSext;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006522 else if (CLI.RetZExt)
Dan Gohman98ca4f22009-08-05 01:29:28 +00006523 AssertOp = ISD::AssertZext;
6524 SmallVector<SDValue, 4> ReturnValues;
6525 unsigned CurReg = 0;
6526 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00006527 EVT VT = RetTys[I];
Patrik Hagglunddfcf33a2012-12-19 11:48:16 +00006528 MVT RegisterVT = getRegisterType(CLI.RetTy->getContext(), VT);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006529 unsigned NumRegs = getNumRegisters(CLI.RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006530
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006531 ReturnValues.push_back(getCopyFromParts(CLI.DAG, CLI.DL, &InVals[CurReg],
Bill Wendling12931302012-09-26 04:04:19 +00006532 NumRegs, RegisterVT, VT, NULL,
Bill Wendling4533cac2010-01-28 21:51:40 +00006533 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00006534 CurReg += NumRegs;
6535 }
6536
6537 // For a function returning void, there is no return value. We can't create
6538 // such a node, so we just return a null return value in that case. In
Chris Lattner7a2bdde2011-04-15 05:18:47 +00006539 // that case, nothing will actually look at the value.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006540 if (ReturnValues.empty())
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006541 return std::make_pair(SDValue(), CLI.Chain);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006542
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006543 SDValue Res = CLI.DAG.getNode(ISD::MERGE_VALUES, CLI.DL,
6544 CLI.DAG.getVTList(&RetTys[0], RetTys.size()),
Dan Gohman98ca4f22009-08-05 01:29:28 +00006545 &ReturnValues[0], ReturnValues.size());
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006546 return std::make_pair(Res, CLI.Chain);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006547}
6548
Duncan Sands9fbc7e22009-01-21 09:00:29 +00006549void TargetLowering::LowerOperationWrapper(SDNode *N,
6550 SmallVectorImpl<SDValue> &Results,
Dan Gohmand858e902010-04-17 15:26:15 +00006551 SelectionDAG &DAG) const {
Duncan Sands9fbc7e22009-01-21 09:00:29 +00006552 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00006553 if (Res.getNode())
6554 Results.push_back(Res);
6555}
6556
Dan Gohmand858e902010-04-17 15:26:15 +00006557SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Torok Edwinc23197a2009-07-14 16:55:14 +00006558 llvm_unreachable("LowerOperation not implemented for this target!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006559}
6560
Dan Gohman46510a72010-04-15 01:51:59 +00006561void
6562SelectionDAGBuilder::CopyValueToVirtualRegister(const Value *V, unsigned Reg) {
Dan Gohman28a17352010-07-01 01:59:43 +00006563 SDValue Op = getNonRegisterValue(V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006564 assert((Op.getOpcode() != ISD::CopyFromReg ||
6565 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
6566 "Copy from a reg to the same reg!");
6567 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
6568
Owen Anderson23b9b192009-08-12 00:36:31 +00006569 RegsForValue RFV(V->getContext(), TLI, Reg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006570 SDValue Chain = DAG.getEntryNode();
Andrew Trickac6d9be2013-05-25 02:42:55 +00006571 RFV.getCopyToRegs(Op, DAG, getCurSDLoc(), Chain, 0, V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006572 PendingExports.push_back(Chain);
6573}
6574
6575#include "llvm/CodeGen/SelectionDAGISel.h"
6576
Eli Friedman23d32432011-05-05 16:53:34 +00006577/// isOnlyUsedInEntryBlock - If the specified argument is only used in the
6578/// entry block, return true. This includes arguments used by switches, since
6579/// the switch may expand into multiple basic blocks.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006580static bool isOnlyUsedInEntryBlock(const Argument *A, bool FastISel) {
Eli Friedman23d32432011-05-05 16:53:34 +00006581 // With FastISel active, we may be splitting blocks, so force creation
6582 // of virtual registers for all non-dead arguments.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006583 if (FastISel)
Eli Friedman23d32432011-05-05 16:53:34 +00006584 return A->use_empty();
6585
6586 const BasicBlock *Entry = A->getParent()->begin();
6587 for (Value::const_use_iterator UI = A->use_begin(), E = A->use_end();
6588 UI != E; ++UI) {
6589 const User *U = *UI;
6590 if (cast<Instruction>(U)->getParent() != Entry || isa<SwitchInst>(U))
6591 return false; // Use not in entry block.
6592 }
6593 return true;
6594}
6595
Eli Bendersky6437d382013-02-28 23:09:18 +00006596void SelectionDAGISel::LowerArguments(const Function &F) {
Dan Gohman2048b852009-11-23 18:04:58 +00006597 SelectionDAG &DAG = SDB->DAG;
Andrew Trickac6d9be2013-05-25 02:42:55 +00006598 SDLoc dl = SDB->getCurSDLoc();
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006599 const DataLayout *TD = TLI->getDataLayout();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006600 SmallVector<ISD::InputArg, 16> Ins;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006601
Dan Gohman7451d3e2010-05-29 17:03:36 +00006602 if (!FuncInfo->CanLowerReturn) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006603 // Put in an sret pointer parameter before all the other parameters.
6604 SmallVector<EVT, 1> ValueVTs;
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006605 ComputeValueVTs(*TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006606
6607 // NOTE: Assuming that a pointer will never break down to more than one VT
6608 // or one register.
6609 ISD::ArgFlagsTy Flags;
6610 Flags.setSRet();
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006611 MVT RegisterVT = TLI->getRegisterType(*DAG.getContext(), ValueVTs[0]);
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00006612 ISD::InputArg RetArg(Flags, RegisterVT, true, 0, 0);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006613 Ins.push_back(RetArg);
6614 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00006615
Dan Gohman98ca4f22009-08-05 01:29:28 +00006616 // Set up the incoming argument description vector.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006617 unsigned Idx = 1;
Dan Gohman46510a72010-04-15 01:51:59 +00006618 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
Dan Gohman98ca4f22009-08-05 01:29:28 +00006619 I != E; ++I, ++Idx) {
Owen Andersone50ed302009-08-10 22:56:29 +00006620 SmallVector<EVT, 4> ValueVTs;
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006621 ComputeValueVTs(*TLI, I->getType(), ValueVTs);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006622 bool isArgValueUsed = !I->use_empty();
6623 for (unsigned Value = 0, NumValues = ValueVTs.size();
6624 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006625 EVT VT = ValueVTs[Value];
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006626 Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00006627 ISD::ArgFlagsTy Flags;
6628 unsigned OriginalAlignment =
6629 TD->getABITypeAlignment(ArgTy);
6630
Bill Wendling39cd0c82012-12-30 12:45:13 +00006631 if (F.getAttributes().hasAttribute(Idx, Attribute::ZExt))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006632 Flags.setZExt();
Bill Wendling39cd0c82012-12-30 12:45:13 +00006633 if (F.getAttributes().hasAttribute(Idx, Attribute::SExt))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006634 Flags.setSExt();
Bill Wendling39cd0c82012-12-30 12:45:13 +00006635 if (F.getAttributes().hasAttribute(Idx, Attribute::InReg))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006636 Flags.setInReg();
Bill Wendling39cd0c82012-12-30 12:45:13 +00006637 if (F.getAttributes().hasAttribute(Idx, Attribute::StructRet))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006638 Flags.setSRet();
Bill Wendling39cd0c82012-12-30 12:45:13 +00006639 if (F.getAttributes().hasAttribute(Idx, Attribute::ByVal)) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00006640 Flags.setByVal();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006641 PointerType *Ty = cast<PointerType>(I->getType());
6642 Type *ElementTy = Ty->getElementType();
Chris Lattner9db20f32011-05-22 23:23:02 +00006643 Flags.setByValSize(TD->getTypeAllocSize(ElementTy));
Dan Gohman98ca4f22009-08-05 01:29:28 +00006644 // For ByVal, alignment should be passed from FE. BE will guess if
6645 // this info is not there but there are cases it cannot get right.
Chris Lattner9db20f32011-05-22 23:23:02 +00006646 unsigned FrameAlign;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006647 if (F.getParamAlignment(Idx))
6648 FrameAlign = F.getParamAlignment(Idx);
Chris Lattner9db20f32011-05-22 23:23:02 +00006649 else
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006650 FrameAlign = TLI->getByValTypeAlignment(ElementTy);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006651 Flags.setByValAlign(FrameAlign);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006652 }
Bill Wendling39cd0c82012-12-30 12:45:13 +00006653 if (F.getAttributes().hasAttribute(Idx, Attribute::Nest))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006654 Flags.setNest();
6655 Flags.setOrigAlign(OriginalAlignment);
6656
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006657 MVT RegisterVT = TLI->getRegisterType(*CurDAG->getContext(), VT);
6658 unsigned NumRegs = TLI->getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006659 for (unsigned i = 0; i != NumRegs; ++i) {
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00006660 ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed,
6661 Idx-1, i*RegisterVT.getStoreSize());
Dan Gohman98ca4f22009-08-05 01:29:28 +00006662 if (NumRegs > 1 && i == 0)
6663 MyFlags.Flags.setSplit();
6664 // if it isn't first piece, alignment must be 1
6665 else if (i > 0)
6666 MyFlags.Flags.setOrigAlign(1);
6667 Ins.push_back(MyFlags);
6668 }
6669 }
6670 }
6671
6672 // Call the target to set up the argument values.
6673 SmallVector<SDValue, 8> InVals;
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006674 SDValue NewRoot = TLI->LowerFormalArguments(DAG.getRoot(), F.getCallingConv(),
6675 F.isVarArg(), Ins,
6676 dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00006677
6678 // Verify that the target's LowerFormalArguments behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00006679 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00006680 "LowerFormalArguments didn't return a valid chain!");
6681 assert(InVals.size() == Ins.size() &&
6682 "LowerFormalArguments didn't emit the correct number of values!");
Bill Wendling3ea58b62009-12-22 21:35:02 +00006683 DEBUG({
6684 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
6685 assert(InVals[i].getNode() &&
6686 "LowerFormalArguments emitted a null value!");
Duncan Sands1440e8b2010-11-03 11:35:31 +00006687 assert(EVT(Ins[i].VT) == InVals[i].getValueType() &&
Bill Wendling3ea58b62009-12-22 21:35:02 +00006688 "LowerFormalArguments emitted a value with the wrong type!");
6689 }
6690 });
Bill Wendling3ea3c242009-12-22 02:10:19 +00006691
Dan Gohman5e866062009-08-06 15:37:27 +00006692 // Update the DAG with the new chain value resulting from argument lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006693 DAG.setRoot(NewRoot);
6694
6695 // Set up the argument values.
6696 unsigned i = 0;
6697 Idx = 1;
Dan Gohman7451d3e2010-05-29 17:03:36 +00006698 if (!FuncInfo->CanLowerReturn) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006699 // Create a virtual register for the sret pointer, and put in a copy
6700 // from the sret argument into it.
6701 SmallVector<EVT, 1> ValueVTs;
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006702 ComputeValueVTs(*TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00006703 MVT VT = ValueVTs[0].getSimpleVT();
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006704 MVT RegVT = TLI->getRegisterType(*CurDAG->getContext(), VT);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006705 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Bill Wendling46ada192010-03-02 01:55:18 +00006706 SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1,
Bill Wendling12931302012-09-26 04:04:19 +00006707 RegVT, VT, NULL, AssertOp);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006708
Dan Gohman2048b852009-11-23 18:04:58 +00006709 MachineFunction& MF = SDB->DAG.getMachineFunction();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006710 MachineRegisterInfo& RegInfo = MF.getRegInfo();
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006711 unsigned SRetReg = RegInfo.createVirtualRegister(TLI->getRegClassFor(RegVT));
Dan Gohman7451d3e2010-05-29 17:03:36 +00006712 FuncInfo->DemoteRegister = SRetReg;
Andrew Trickac6d9be2013-05-25 02:42:55 +00006713 NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurSDLoc(),
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00006714 SRetReg, ArgValue);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006715 DAG.setRoot(NewRoot);
Bill Wendling3ea3c242009-12-22 02:10:19 +00006716
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006717 // i indexes lowered arguments. Bump it past the hidden sret argument.
6718 // Idx indexes LLVM arguments. Don't touch it.
6719 ++i;
6720 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006721
Dan Gohman46510a72010-04-15 01:51:59 +00006722 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006723 ++I, ++Idx) {
6724 SmallVector<SDValue, 4> ArgValues;
Owen Andersone50ed302009-08-10 22:56:29 +00006725 SmallVector<EVT, 4> ValueVTs;
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006726 ComputeValueVTs(*TLI, I->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006727 unsigned NumValues = ValueVTs.size();
Devang Patel9126c0d2010-06-01 19:59:01 +00006728
6729 // If this argument is unused then remember its value. It is used to generate
6730 // debugging information.
Adrian Prantldf688032013-05-16 23:44:12 +00006731 if (I->use_empty() && NumValues) {
Devang Patel9126c0d2010-06-01 19:59:01 +00006732 SDB->setUnusedArgValue(I, InVals[i]);
6733
Adrian Prantldf688032013-05-16 23:44:12 +00006734 // Also remember any frame index for use in FastISel.
6735 if (FrameIndexSDNode *FI =
6736 dyn_cast<FrameIndexSDNode>(InVals[i].getNode()))
6737 FuncInfo->setArgumentFrameIndex(I, FI->getIndex());
6738 }
6739
Eli Friedman23d32432011-05-05 16:53:34 +00006740 for (unsigned Val = 0; Val != NumValues; ++Val) {
6741 EVT VT = ValueVTs[Val];
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006742 MVT PartVT = TLI->getRegisterType(*CurDAG->getContext(), VT);
6743 unsigned NumParts = TLI->getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006744
6745 if (!I->use_empty()) {
6746 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Bill Wendling39cd0c82012-12-30 12:45:13 +00006747 if (F.getAttributes().hasAttribute(Idx, Attribute::SExt))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006748 AssertOp = ISD::AssertSext;
Bill Wendling39cd0c82012-12-30 12:45:13 +00006749 else if (F.getAttributes().hasAttribute(Idx, Attribute::ZExt))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006750 AssertOp = ISD::AssertZext;
6751
Bill Wendling46ada192010-03-02 01:55:18 +00006752 ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i],
Bill Wendling3ea3c242009-12-22 02:10:19 +00006753 NumParts, PartVT, VT,
Bill Wendling12931302012-09-26 04:04:19 +00006754 NULL, AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00006755 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006756
Dan Gohman98ca4f22009-08-05 01:29:28 +00006757 i += NumParts;
6758 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006759
Eli Friedman23d32432011-05-05 16:53:34 +00006760 // We don't need to do anything else for unused arguments.
6761 if (ArgValues.empty())
6762 continue;
6763
Devang Patel9aee3352011-09-08 22:59:09 +00006764 // Note down frame index.
6765 if (FrameIndexSDNode *FI =
Bill Wendling96cb1122012-07-19 00:04:14 +00006766 dyn_cast<FrameIndexSDNode>(ArgValues[0].getNode()))
Devang Patel9aee3352011-09-08 22:59:09 +00006767 FuncInfo->setArgumentFrameIndex(I, FI->getIndex());
Devang Patel0b48ead2010-08-31 22:22:42 +00006768
Eli Friedman23d32432011-05-05 16:53:34 +00006769 SDValue Res = DAG.getMergeValues(&ArgValues[0], NumValues,
Andrew Trickac6d9be2013-05-25 02:42:55 +00006770 SDB->getCurSDLoc());
Devang Patel9aee3352011-09-08 22:59:09 +00006771
Eli Friedman23d32432011-05-05 16:53:34 +00006772 SDB->setValue(I, Res);
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006773 if (!TM.Options.EnableFastISel && Res.getOpcode() == ISD::BUILD_PAIR) {
Devang Patel9aee3352011-09-08 22:59:09 +00006774 if (LoadSDNode *LNode =
6775 dyn_cast<LoadSDNode>(Res.getOperand(0).getNode()))
6776 if (FrameIndexSDNode *FI =
6777 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
6778 FuncInfo->setArgumentFrameIndex(I, FI->getIndex());
6779 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006780
Eli Friedman23d32432011-05-05 16:53:34 +00006781 // If this argument is live outside of the entry block, insert a copy from
6782 // wherever we got it to the vreg that other BB's will reference it as.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006783 if (!TM.Options.EnableFastISel && Res.getOpcode() == ISD::CopyFromReg) {
Eli Friedman23d32432011-05-05 16:53:34 +00006784 // If we can, though, try to skip creating an unnecessary vreg.
6785 // FIXME: This isn't very clean... it would be nice to make this more
Eli Friedman7f33d672011-05-10 21:50:58 +00006786 // general. It's also subtly incompatible with the hacks FastISel
6787 // uses with vregs.
Eli Friedman23d32432011-05-05 16:53:34 +00006788 unsigned Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg();
6789 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
6790 FuncInfo->ValueMap[I] = Reg;
6791 continue;
6792 }
6793 }
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006794 if (!isOnlyUsedInEntryBlock(I, TM.Options.EnableFastISel)) {
Eli Friedman23d32432011-05-05 16:53:34 +00006795 FuncInfo->InitializeRegForValue(I);
Dan Gohman2048b852009-11-23 18:04:58 +00006796 SDB->CopyToExportRegsIfNeeded(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006797 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006798 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006799
Dan Gohman98ca4f22009-08-05 01:29:28 +00006800 assert(i == InVals.size() && "Argument register count mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006801
6802 // Finally, if the target has anything special to do, allow it to do so.
6803 // FIXME: this should insert code into the DAG!
Dan Gohman64652652010-04-14 20:17:22 +00006804 EmitFunctionEntryCode();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006805}
6806
6807/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
6808/// ensure constants are generated when needed. Remember the virtual registers
6809/// that need to be added to the Machine PHI nodes as input. We cannot just
6810/// directly add them, because expansion might result in multiple MBB's for one
6811/// BB. As such, the start of the BB might correspond to a different MBB than
6812/// the end.
6813///
6814void
Dan Gohmanf81eca02010-04-22 20:46:50 +00006815SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
Dan Gohman46510a72010-04-15 01:51:59 +00006816 const TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006817
6818 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
6819
6820 // Check successor nodes' PHI nodes that expect a constant to be available
6821 // from this block.
6822 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
Dan Gohman46510a72010-04-15 01:51:59 +00006823 const BasicBlock *SuccBB = TI->getSuccessor(succ);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006824 if (!isa<PHINode>(SuccBB->begin())) continue;
Dan Gohmanf81eca02010-04-22 20:46:50 +00006825 MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006826
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006827 // If this terminator has multiple identical successors (common for
6828 // switches), only handle each succ once.
6829 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006830
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006831 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006832
6833 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6834 // nodes and Machine PHI nodes, but the incoming operands have not been
6835 // emitted yet.
Dan Gohman46510a72010-04-15 01:51:59 +00006836 for (BasicBlock::const_iterator I = SuccBB->begin();
6837 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006838 // Ignore dead phi's.
6839 if (PN->use_empty()) continue;
6840
Rafael Espindola3fa82832011-05-13 15:18:06 +00006841 // Skip empty types
6842 if (PN->getType()->isEmptyTy())
6843 continue;
6844
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006845 unsigned Reg;
Dan Gohman46510a72010-04-15 01:51:59 +00006846 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006847
Dan Gohman46510a72010-04-15 01:51:59 +00006848 if (const Constant *C = dyn_cast<Constant>(PHIOp)) {
Dan Gohmanf81eca02010-04-22 20:46:50 +00006849 unsigned &RegOut = ConstantsOut[C];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006850 if (RegOut == 0) {
Dan Gohman89496d02010-07-02 00:10:16 +00006851 RegOut = FuncInfo.CreateRegs(C->getType());
Dan Gohmanf81eca02010-04-22 20:46:50 +00006852 CopyValueToVirtualRegister(C, RegOut);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006853 }
6854 Reg = RegOut;
6855 } else {
Dan Gohmanc25ad632010-07-01 01:33:21 +00006856 DenseMap<const Value *, unsigned>::iterator I =
6857 FuncInfo.ValueMap.find(PHIOp);
6858 if (I != FuncInfo.ValueMap.end())
6859 Reg = I->second;
6860 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006861 assert(isa<AllocaInst>(PHIOp) &&
Dan Gohmanf81eca02010-04-22 20:46:50 +00006862 FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006863 "Didn't codegen value into a register!??");
Dan Gohman89496d02010-07-02 00:10:16 +00006864 Reg = FuncInfo.CreateRegs(PHIOp->getType());
Dan Gohmanf81eca02010-04-22 20:46:50 +00006865 CopyValueToVirtualRegister(PHIOp, Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006866 }
6867 }
6868
6869 // Remember that this register needs to added to the machine PHI node as
6870 // the input for this MBB.
Owen Andersone50ed302009-08-10 22:56:29 +00006871 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006872 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
6873 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +00006874 EVT VT = ValueVTs[vti];
Dan Gohmanf81eca02010-04-22 20:46:50 +00006875 unsigned NumRegisters = TLI.getNumRegisters(*DAG.getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006876 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
Dan Gohmanf81eca02010-04-22 20:46:50 +00006877 FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006878 Reg += NumRegisters;
6879 }
6880 }
6881 }
Dan Gohmanf81eca02010-04-22 20:46:50 +00006882 ConstantsOut.clear();
Dan Gohman3df24e62008-09-03 23:12:08 +00006883}