blob: 608fc275b5a9d65912454291b5c52764960d25f2 [file] [log] [blame]
Dan Gohman2048b852009-11-23 18:04:58 +00001//===-- SelectionDAGBuilder.cpp - Selection-DAG building ------------------===//
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This implements routines for translating from LLVM IR into SelectionDAG IR.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "isel"
Dan Gohman2048b852009-11-23 18:04:58 +000015#include "SelectionDAGBuilder.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "SDNodeDbgValue.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000017#include "llvm/ADT/BitVector.h"
David Blaikie6d9dbd52013-06-16 20:34:15 +000018#include "llvm/ADT/Optional.h"
Dan Gohman5b229802008-09-04 20:49:27 +000019#include "llvm/ADT/SmallSet.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000020#include "llvm/Analysis/AliasAnalysis.h"
Jakub Staszak81bfd712013-01-10 22:13:13 +000021#include "llvm/Analysis/BranchProbabilityInfo.h"
Chris Lattner8047d9a2009-12-24 00:37:38 +000022#include "llvm/Analysis/ConstantFolding.h"
Nadav Rotemc05d3062012-09-06 09:17:37 +000023#include "llvm/Analysis/ValueTracking.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000024#include "llvm/CodeGen/Analysis.h"
25#include "llvm/CodeGen/FastISel.h"
26#include "llvm/CodeGen/FunctionLoweringInfo.h"
27#include "llvm/CodeGen/GCMetadata.h"
28#include "llvm/CodeGen/GCStrategy.h"
29#include "llvm/CodeGen/MachineFrameInfo.h"
30#include "llvm/CodeGen/MachineFunction.h"
31#include "llvm/CodeGen/MachineInstrBuilder.h"
32#include "llvm/CodeGen/MachineJumpTableInfo.h"
33#include "llvm/CodeGen/MachineModuleInfo.h"
34#include "llvm/CodeGen/MachineRegisterInfo.h"
35#include "llvm/CodeGen/SelectionDAG.h"
Bill Wendling0bcbd1d2012-06-28 00:05:13 +000036#include "llvm/DebugInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000037#include "llvm/IR/CallingConv.h"
38#include "llvm/IR/Constants.h"
39#include "llvm/IR/DataLayout.h"
40#include "llvm/IR/DerivedTypes.h"
41#include "llvm/IR/Function.h"
42#include "llvm/IR/GlobalVariable.h"
43#include "llvm/IR/InlineAsm.h"
44#include "llvm/IR/Instructions.h"
45#include "llvm/IR/IntrinsicInst.h"
46#include "llvm/IR/Intrinsics.h"
47#include "llvm/IR/LLVMContext.h"
48#include "llvm/IR/Module.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000049#include "llvm/Support/CommandLine.h"
50#include "llvm/Support/Debug.h"
51#include "llvm/Support/ErrorHandling.h"
52#include "llvm/Support/IntegersSubsetMapping.h"
53#include "llvm/Support/MathExtras.h"
54#include "llvm/Support/raw_ostream.h"
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000055#include "llvm/Target/TargetFrameLowering.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000056#include "llvm/Target/TargetInstrInfo.h"
Dale Johannesen49de9822009-02-05 01:49:45 +000057#include "llvm/Target/TargetIntrinsicInfo.h"
Owen Anderson243eb9e2011-12-08 22:15:21 +000058#include "llvm/Target/TargetLibraryInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000059#include "llvm/Target/TargetLowering.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000060#include "llvm/Target/TargetOptions.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000061#include <algorithm>
62using namespace llvm;
63
Dale Johannesen601d3c02008-09-05 01:48:15 +000064/// LimitFloatPrecision - Generate low-precision inline sequences for
65/// some float libcalls (6, 8 or 12 bits).
66static unsigned LimitFloatPrecision;
67
68static cl::opt<unsigned, true>
69LimitFPPrecision("limit-float-precision",
70 cl::desc("Generate low-precision inline sequences "
71 "for some float libcalls"),
72 cl::location(LimitFloatPrecision),
73 cl::init(0));
74
Andrew Trickde91f3c2010-11-12 17:50:46 +000075// Limit the width of DAG chains. This is important in general to prevent
76// prevent DAG-based analysis from blowing up. For example, alias analysis and
77// load clustering may not complete in reasonable time. It is difficult to
78// recognize and avoid this situation within each individual analysis, and
79// future analyses are likely to have the same behavior. Limiting DAG width is
Andrew Trickb9e6fe12010-11-20 07:26:51 +000080// the safe approach, and will be especially important with global DAGs.
Andrew Trickde91f3c2010-11-12 17:50:46 +000081//
82// MaxParallelChains default is arbitrarily high to avoid affecting
83// optimization, but could be lowered to improve compile time. Any ld-ld-st-st
Andrew Trickb9e6fe12010-11-20 07:26:51 +000084// sequence over this should have been converted to llvm.memcpy by the
85// frontend. It easy to induce this behavior with .ll code such as:
86// %buffer = alloca [4096 x i8]
87// %data = load [4096 x i8]* %argPtr
88// store [4096 x i8] %data, [4096 x i8]* %buffer
Andrew Trick778583a2011-03-11 17:46:59 +000089static const unsigned MaxParallelChains = 64;
Andrew Trickde91f3c2010-11-12 17:50:46 +000090
Andrew Trickac6d9be2013-05-25 02:42:55 +000091static SDValue getCopyFromPartsVector(SelectionDAG &DAG, SDLoc DL,
Chris Lattner3ac18842010-08-24 23:20:40 +000092 const SDValue *Parts, unsigned NumParts,
Patrik Hagglundb9e12e52012-12-19 12:33:30 +000093 MVT PartVT, EVT ValueVT, const Value *V);
Michael J. Spencere70c5262010-10-16 08:25:21 +000094
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000095/// getCopyFromParts - Create a value that contains the specified legal parts
96/// combined into the value they represent. If the parts combine to a type
97/// larger then ValueVT then AssertOp can be used to specify whether the extra
98/// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT
99/// (ISD::AssertSext).
Andrew Trickac6d9be2013-05-25 02:42:55 +0000100static SDValue getCopyFromParts(SelectionDAG &DAG, SDLoc DL,
Dale Johannesen66978ee2009-01-31 02:22:37 +0000101 const SDValue *Parts,
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000102 unsigned NumParts, MVT PartVT, EVT ValueVT,
Bill Wendling12931302012-09-26 04:04:19 +0000103 const Value *V,
Duncan Sands0b3aa262009-01-28 14:42:54 +0000104 ISD::NodeType AssertOp = ISD::DELETED_NODE) {
Chris Lattner3ac18842010-08-24 23:20:40 +0000105 if (ValueVT.isVector())
Bill Wendling12931302012-09-26 04:04:19 +0000106 return getCopyFromPartsVector(DAG, DL, Parts, NumParts,
107 PartVT, ValueVT, V);
Michael J. Spencere70c5262010-10-16 08:25:21 +0000108
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000109 assert(NumParts > 0 && "No parts to assemble!");
Dan Gohmane9530ec2009-01-15 16:58:17 +0000110 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000111 SDValue Val = Parts[0];
112
113 if (NumParts > 1) {
114 // Assemble the value from multiple parts.
Chris Lattner3ac18842010-08-24 23:20:40 +0000115 if (ValueVT.isInteger()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000116 unsigned PartBits = PartVT.getSizeInBits();
117 unsigned ValueBits = ValueVT.getSizeInBits();
118
119 // Assemble the power of 2 part.
120 unsigned RoundParts = NumParts & (NumParts - 1) ?
121 1 << Log2_32(NumParts) : NumParts;
122 unsigned RoundBits = PartBits * RoundParts;
Owen Andersone50ed302009-08-10 22:56:29 +0000123 EVT RoundVT = RoundBits == ValueBits ?
Owen Anderson23b9b192009-08-12 00:36:31 +0000124 ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000125 SDValue Lo, Hi;
126
Owen Anderson23b9b192009-08-12 00:36:31 +0000127 EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2);
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000128
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000129 if (RoundParts > 2) {
Chris Lattner3ac18842010-08-24 23:20:40 +0000130 Lo = getCopyFromParts(DAG, DL, Parts, RoundParts / 2,
Bill Wendling12931302012-09-26 04:04:19 +0000131 PartVT, HalfVT, V);
Chris Lattner3ac18842010-08-24 23:20:40 +0000132 Hi = getCopyFromParts(DAG, DL, Parts + RoundParts / 2,
Bill Wendling12931302012-09-26 04:04:19 +0000133 RoundParts / 2, PartVT, HalfVT, V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000134 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000135 Lo = DAG.getNode(ISD::BITCAST, DL, HalfVT, Parts[0]);
136 Hi = DAG.getNode(ISD::BITCAST, DL, HalfVT, Parts[1]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000137 }
Bill Wendling3ea3c242009-12-22 02:10:19 +0000138
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000139 if (TLI.isBigEndian())
140 std::swap(Lo, Hi);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000141
Chris Lattner3ac18842010-08-24 23:20:40 +0000142 Val = DAG.getNode(ISD::BUILD_PAIR, DL, RoundVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000143
144 if (RoundParts < NumParts) {
145 // Assemble the trailing non-power-of-2 part.
146 unsigned OddParts = NumParts - RoundParts;
Owen Anderson23b9b192009-08-12 00:36:31 +0000147 EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits);
Chris Lattner3ac18842010-08-24 23:20:40 +0000148 Hi = getCopyFromParts(DAG, DL,
Bill Wendling12931302012-09-26 04:04:19 +0000149 Parts + RoundParts, OddParts, PartVT, OddVT, V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000150
151 // Combine the round and odd parts.
152 Lo = Val;
153 if (TLI.isBigEndian())
154 std::swap(Lo, Hi);
Owen Anderson23b9b192009-08-12 00:36:31 +0000155 EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Chris Lattner3ac18842010-08-24 23:20:40 +0000156 Hi = DAG.getNode(ISD::ANY_EXTEND, DL, TotalVT, Hi);
157 Hi = DAG.getNode(ISD::SHL, DL, TotalVT, Hi,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000158 DAG.getConstant(Lo.getValueType().getSizeInBits(),
Duncan Sands92abc622009-01-31 15:50:11 +0000159 TLI.getPointerTy()));
Chris Lattner3ac18842010-08-24 23:20:40 +0000160 Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, TotalVT, Lo);
161 Val = DAG.getNode(ISD::OR, DL, TotalVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000162 }
Eli Friedman2ac8b322009-05-20 06:02:09 +0000163 } else if (PartVT.isFloatingPoint()) {
164 // FP split into multiple FP parts (for ppcf128)
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000165 assert(ValueVT == EVT(MVT::ppcf128) && PartVT == MVT::f64 &&
Eli Friedman2ac8b322009-05-20 06:02:09 +0000166 "Unexpected split");
167 SDValue Lo, Hi;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000168 Lo = DAG.getNode(ISD::BITCAST, DL, EVT(MVT::f64), Parts[0]);
169 Hi = DAG.getNode(ISD::BITCAST, DL, EVT(MVT::f64), Parts[1]);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000170 if (TLI.isBigEndian())
171 std::swap(Lo, Hi);
Chris Lattner3ac18842010-08-24 23:20:40 +0000172 Val = DAG.getNode(ISD::BUILD_PAIR, DL, ValueVT, Lo, Hi);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000173 } else {
174 // FP split into integer parts (soft fp)
175 assert(ValueVT.isFloatingPoint() && PartVT.isInteger() &&
176 !PartVT.isVector() && "Unexpected split");
Owen Anderson23b9b192009-08-12 00:36:31 +0000177 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
Bill Wendling12931302012-09-26 04:04:19 +0000178 Val = getCopyFromParts(DAG, DL, Parts, NumParts, PartVT, IntVT, V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000179 }
180 }
181
182 // There is now one part, held in Val. Correct it to match ValueVT.
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000183 EVT PartEVT = Val.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000184
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000185 if (PartEVT == ValueVT)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000186 return Val;
187
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000188 if (PartEVT.isInteger() && ValueVT.isInteger()) {
189 if (ValueVT.bitsLT(PartEVT)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000190 // For a truncate, see if we have any information to
191 // indicate whether the truncated bits will always be
192 // zero or sign-extension.
193 if (AssertOp != ISD::DELETED_NODE)
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000194 Val = DAG.getNode(AssertOp, DL, PartEVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000195 DAG.getValueType(ValueVT));
Chris Lattner3ac18842010-08-24 23:20:40 +0000196 return DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000197 }
Chris Lattner3ac18842010-08-24 23:20:40 +0000198 return DAG.getNode(ISD::ANY_EXTEND, DL, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000199 }
200
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000201 if (PartEVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
Chris Lattner3ac18842010-08-24 23:20:40 +0000202 // FP_ROUND's are always exact here.
203 if (ValueVT.bitsLT(Val.getValueType()))
204 return DAG.getNode(ISD::FP_ROUND, DL, ValueVT, Val,
Pete Cooperf57e1c22012-01-17 01:54:07 +0000205 DAG.getTargetConstant(1, TLI.getPointerTy()));
Bill Wendling3ea3c242009-12-22 02:10:19 +0000206
Chris Lattner3ac18842010-08-24 23:20:40 +0000207 return DAG.getNode(ISD::FP_EXTEND, DL, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000208 }
209
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000210 if (PartEVT.getSizeInBits() == ValueVT.getSizeInBits())
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000211 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000212
Torok Edwinc23197a2009-07-14 16:55:14 +0000213 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000214}
215
Bill Wendling12931302012-09-26 04:04:19 +0000216/// getCopyFromPartsVector - Create a value that contains the specified legal
217/// parts combined into the value they represent. If the parts combine to a
218/// type larger then ValueVT then AssertOp can be used to specify whether the
219/// extra bits are known to be zero (ISD::AssertZext) or sign extended from
220/// ValueVT (ISD::AssertSext).
Andrew Trickac6d9be2013-05-25 02:42:55 +0000221static SDValue getCopyFromPartsVector(SelectionDAG &DAG, SDLoc DL,
Chris Lattner3ac18842010-08-24 23:20:40 +0000222 const SDValue *Parts, unsigned NumParts,
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000223 MVT PartVT, EVT ValueVT, const Value *V) {
Chris Lattner3ac18842010-08-24 23:20:40 +0000224 assert(ValueVT.isVector() && "Not a vector value");
225 assert(NumParts > 0 && "No parts to assemble!");
226 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
227 SDValue Val = Parts[0];
Michael J. Spencere70c5262010-10-16 08:25:21 +0000228
Chris Lattner3ac18842010-08-24 23:20:40 +0000229 // Handle a multi-element vector.
230 if (NumParts > 1) {
Patrik Hagglundee211d22012-12-19 11:53:21 +0000231 EVT IntermediateVT;
232 MVT RegisterVT;
Chris Lattner3ac18842010-08-24 23:20:40 +0000233 unsigned NumIntermediates;
234 unsigned NumRegs =
235 TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT,
236 NumIntermediates, RegisterVT);
237 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
238 NumParts = NumRegs; // Silence a compiler warning.
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000239 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
Patrik Hagglundee211d22012-12-19 11:53:21 +0000240 assert(RegisterVT == Parts[0].getSimpleValueType() &&
Chris Lattner3ac18842010-08-24 23:20:40 +0000241 "Part type doesn't match part!");
Michael J. Spencere70c5262010-10-16 08:25:21 +0000242
Chris Lattner3ac18842010-08-24 23:20:40 +0000243 // Assemble the parts into intermediate operands.
244 SmallVector<SDValue, 8> Ops(NumIntermediates);
245 if (NumIntermediates == NumParts) {
246 // If the register was not expanded, truncate or copy the value,
247 // as appropriate.
248 for (unsigned i = 0; i != NumParts; ++i)
249 Ops[i] = getCopyFromParts(DAG, DL, &Parts[i], 1,
Bill Wendling12931302012-09-26 04:04:19 +0000250 PartVT, IntermediateVT, V);
Chris Lattner3ac18842010-08-24 23:20:40 +0000251 } else if (NumParts > 0) {
252 // If the intermediate type was expanded, build the intermediate
253 // operands from the parts.
254 assert(NumParts % NumIntermediates == 0 &&
255 "Must expand into a divisible number of parts!");
256 unsigned Factor = NumParts / NumIntermediates;
257 for (unsigned i = 0; i != NumIntermediates; ++i)
258 Ops[i] = getCopyFromParts(DAG, DL, &Parts[i * Factor], Factor,
Bill Wendling12931302012-09-26 04:04:19 +0000259 PartVT, IntermediateVT, V);
Chris Lattner3ac18842010-08-24 23:20:40 +0000260 }
Michael J. Spencere70c5262010-10-16 08:25:21 +0000261
Chris Lattner3ac18842010-08-24 23:20:40 +0000262 // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the
263 // intermediate operands.
264 Val = DAG.getNode(IntermediateVT.isVector() ?
265 ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, DL,
266 ValueVT, &Ops[0], NumIntermediates);
267 }
Michael J. Spencere70c5262010-10-16 08:25:21 +0000268
Chris Lattner3ac18842010-08-24 23:20:40 +0000269 // There is now one part, held in Val. Correct it to match ValueVT.
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000270 EVT PartEVT = Val.getValueType();
Michael J. Spencere70c5262010-10-16 08:25:21 +0000271
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000272 if (PartEVT == ValueVT)
Chris Lattner3ac18842010-08-24 23:20:40 +0000273 return Val;
Michael J. Spencere70c5262010-10-16 08:25:21 +0000274
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000275 if (PartEVT.isVector()) {
Chris Lattnere6f7c262010-08-25 22:49:25 +0000276 // If the element type of the source/dest vectors are the same, but the
277 // parts vector has more elements than the value vector, then we have a
278 // vector widening case (e.g. <2 x float> -> <4 x float>). Extract the
279 // elements we want.
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000280 if (PartEVT.getVectorElementType() == ValueVT.getVectorElementType()) {
281 assert(PartEVT.getVectorNumElements() > ValueVT.getVectorNumElements() &&
Chris Lattnere6f7c262010-08-25 22:49:25 +0000282 "Cannot narrow, it would be a lossy transformation");
283 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, ValueVT, Val,
284 DAG.getIntPtrConstant(0));
Michael J. Spencere70c5262010-10-16 08:25:21 +0000285 }
286
Chris Lattnere6f7c262010-08-25 22:49:25 +0000287 // Vector/Vector bitcast.
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000288 if (ValueVT.getSizeInBits() == PartEVT.getSizeInBits())
Nadav Rotem0b666362011-06-04 20:58:08 +0000289 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
290
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000291 assert(PartEVT.getVectorNumElements() == ValueVT.getVectorNumElements() &&
Nadav Rotem0b666362011-06-04 20:58:08 +0000292 "Cannot handle this kind of promotion");
293 // Promoted vector extract
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000294 bool Smaller = ValueVT.bitsLE(PartEVT);
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000295 return DAG.getNode((Smaller ? ISD::TRUNCATE : ISD::ANY_EXTEND),
296 DL, ValueVT, Val);
Nadav Rotem0b666362011-06-04 20:58:08 +0000297
Chris Lattnere6f7c262010-08-25 22:49:25 +0000298 }
Eric Christopher471e4222011-06-08 23:55:35 +0000299
Eric Christopher9aaa02a2011-06-01 19:55:10 +0000300 // Trivial bitcast if the types are the same size and the destination
301 // vector type is legal.
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000302 if (PartEVT.getSizeInBits() == ValueVT.getSizeInBits() &&
Eric Christopher9aaa02a2011-06-01 19:55:10 +0000303 TLI.isTypeLegal(ValueVT))
304 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
Michael J. Spencere70c5262010-10-16 08:25:21 +0000305
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000306 // Handle cases such as i8 -> <1 x i1>
Bill Wendling12931302012-09-26 04:04:19 +0000307 if (ValueVT.getVectorNumElements() != 1) {
308 LLVMContext &Ctx = *DAG.getContext();
309 Twine ErrMsg("non-trivial scalar-to-vector conversion");
310 if (const Instruction *I = dyn_cast_or_null<Instruction>(V)) {
311 if (const CallInst *CI = dyn_cast<CallInst>(I))
312 if (isa<InlineAsm>(CI->getCalledValue()))
313 ErrMsg = ErrMsg + ", possible invalid constraint for vector type";
314 Ctx.emitError(I, ErrMsg);
315 } else {
316 Ctx.emitError(ErrMsg);
317 }
Chad Rosierf0b07552013-05-01 19:49:26 +0000318 return DAG.getUNDEF(ValueVT);
Bill Wendling12931302012-09-26 04:04:19 +0000319 }
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000320
321 if (ValueVT.getVectorNumElements() == 1 &&
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000322 ValueVT.getVectorElementType() != PartEVT) {
323 bool Smaller = ValueVT.bitsLE(PartEVT);
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000324 Val = DAG.getNode((Smaller ? ISD::TRUNCATE : ISD::ANY_EXTEND),
325 DL, ValueVT.getScalarType(), Val);
326 }
327
Chris Lattner3ac18842010-08-24 23:20:40 +0000328 return DAG.getNode(ISD::BUILD_VECTOR, DL, ValueVT, Val);
329}
330
Andrew Trickac6d9be2013-05-25 02:42:55 +0000331static void getCopyToPartsVector(SelectionDAG &DAG, SDLoc dl,
Chris Lattnera13b8602010-08-24 23:10:06 +0000332 SDValue Val, SDValue *Parts, unsigned NumParts,
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000333 MVT PartVT, const Value *V);
Michael J. Spencere70c5262010-10-16 08:25:21 +0000334
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000335/// getCopyToParts - Create a series of nodes that contain the specified value
336/// split into legal parts. If the parts contain more bits than Val, then, for
337/// integers, ExtendKind can be used to specify how to generate the extra bits.
Andrew Trickac6d9be2013-05-25 02:42:55 +0000338static void getCopyToParts(SelectionDAG &DAG, SDLoc DL,
Bill Wendling3ea3c242009-12-22 02:10:19 +0000339 SDValue Val, SDValue *Parts, unsigned NumParts,
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000340 MVT PartVT, const Value *V,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000341 ISD::NodeType ExtendKind = ISD::ANY_EXTEND) {
Owen Andersone50ed302009-08-10 22:56:29 +0000342 EVT ValueVT = Val.getValueType();
Michael J. Spencere70c5262010-10-16 08:25:21 +0000343
Chris Lattnera13b8602010-08-24 23:10:06 +0000344 // Handle the vector case separately.
345 if (ValueVT.isVector())
Bill Wendlingf18eb582012-09-26 06:16:18 +0000346 return getCopyToPartsVector(DAG, DL, Val, Parts, NumParts, PartVT, V);
Michael J. Spencere70c5262010-10-16 08:25:21 +0000347
Chris Lattnera13b8602010-08-24 23:10:06 +0000348 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000349 unsigned PartBits = PartVT.getSizeInBits();
Dale Johannesen8a36f502009-02-25 22:39:13 +0000350 unsigned OrigNumParts = NumParts;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000351 assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!");
352
Chris Lattnera13b8602010-08-24 23:10:06 +0000353 if (NumParts == 0)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000354 return;
355
Chris Lattnera13b8602010-08-24 23:10:06 +0000356 assert(!ValueVT.isVector() && "Vector case handled elsewhere");
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000357 EVT PartEVT = PartVT;
358 if (PartEVT == ValueVT) {
Chris Lattnera13b8602010-08-24 23:10:06 +0000359 assert(NumParts == 1 && "No-op copy with multiple parts!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000360 Parts[0] = Val;
361 return;
362 }
363
Chris Lattnera13b8602010-08-24 23:10:06 +0000364 if (NumParts * PartBits > ValueVT.getSizeInBits()) {
365 // If the parts cover more bits than the value has, promote the value.
366 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
367 assert(NumParts == 1 && "Do not know what to promote to!");
368 Val = DAG.getNode(ISD::FP_EXTEND, DL, PartVT, Val);
369 } else {
Bill Wendling9e8ceb02012-02-23 23:25:25 +0000370 assert((PartVT.isInteger() || PartVT == MVT::x86mmx) &&
371 ValueVT.isInteger() &&
Michael J. Spencere70c5262010-10-16 08:25:21 +0000372 "Unknown mismatch!");
Chris Lattnera13b8602010-08-24 23:10:06 +0000373 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
374 Val = DAG.getNode(ExtendKind, DL, ValueVT, Val);
Bill Wendling9e8ceb02012-02-23 23:25:25 +0000375 if (PartVT == MVT::x86mmx)
376 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
Chris Lattnera13b8602010-08-24 23:10:06 +0000377 }
378 } else if (PartBits == ValueVT.getSizeInBits()) {
379 // Different types of the same size.
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000380 assert(NumParts == 1 && PartEVT != ValueVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000381 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
Chris Lattnera13b8602010-08-24 23:10:06 +0000382 } else if (NumParts * PartBits < ValueVT.getSizeInBits()) {
383 // If the parts cover less bits than value has, truncate the value.
Bill Wendling9e8ceb02012-02-23 23:25:25 +0000384 assert((PartVT.isInteger() || PartVT == MVT::x86mmx) &&
385 ValueVT.isInteger() &&
Chris Lattnera13b8602010-08-24 23:10:06 +0000386 "Unknown mismatch!");
387 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
388 Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
Bill Wendling9e8ceb02012-02-23 23:25:25 +0000389 if (PartVT == MVT::x86mmx)
390 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
Chris Lattnera13b8602010-08-24 23:10:06 +0000391 }
392
393 // The value may have changed - recompute ValueVT.
394 ValueVT = Val.getValueType();
395 assert(NumParts * PartBits == ValueVT.getSizeInBits() &&
396 "Failed to tile the value with PartVT!");
397
398 if (NumParts == 1) {
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000399 if (PartEVT != ValueVT) {
Bill Wendlingf18eb582012-09-26 06:16:18 +0000400 LLVMContext &Ctx = *DAG.getContext();
401 Twine ErrMsg("scalar-to-vector conversion failed");
402 if (const Instruction *I = dyn_cast_or_null<Instruction>(V)) {
403 if (const CallInst *CI = dyn_cast<CallInst>(I))
404 if (isa<InlineAsm>(CI->getCalledValue()))
405 ErrMsg = ErrMsg + ", possible invalid constraint for vector type";
406 Ctx.emitError(I, ErrMsg);
407 } else {
408 Ctx.emitError(ErrMsg);
409 }
410 }
411
Chris Lattnera13b8602010-08-24 23:10:06 +0000412 Parts[0] = Val;
413 return;
414 }
415
416 // Expand the value into multiple parts.
417 if (NumParts & (NumParts - 1)) {
418 // The number of parts is not a power of 2. Split off and copy the tail.
419 assert(PartVT.isInteger() && ValueVT.isInteger() &&
420 "Do not know what to expand to!");
421 unsigned RoundParts = 1 << Log2_32(NumParts);
422 unsigned RoundBits = RoundParts * PartBits;
423 unsigned OddParts = NumParts - RoundParts;
424 SDValue OddVal = DAG.getNode(ISD::SRL, DL, ValueVT, Val,
425 DAG.getIntPtrConstant(RoundBits));
Bill Wendlingf18eb582012-09-26 06:16:18 +0000426 getCopyToParts(DAG, DL, OddVal, Parts + RoundParts, OddParts, PartVT, V);
Chris Lattnera13b8602010-08-24 23:10:06 +0000427
428 if (TLI.isBigEndian())
429 // The odd parts were reversed by getCopyToParts - unreverse them.
430 std::reverse(Parts + RoundParts, Parts + NumParts);
431
432 NumParts = RoundParts;
433 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
434 Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
435 }
436
437 // The number of parts is a power of 2. Repeatedly bisect the value using
438 // EXTRACT_ELEMENT.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000439 Parts[0] = DAG.getNode(ISD::BITCAST, DL,
Chris Lattnera13b8602010-08-24 23:10:06 +0000440 EVT::getIntegerVT(*DAG.getContext(),
441 ValueVT.getSizeInBits()),
442 Val);
443
444 for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) {
445 for (unsigned i = 0; i < NumParts; i += StepSize) {
446 unsigned ThisBits = StepSize * PartBits / 2;
447 EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits);
448 SDValue &Part0 = Parts[i];
449 SDValue &Part1 = Parts[i+StepSize/2];
450
451 Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL,
452 ThisVT, Part0, DAG.getIntPtrConstant(1));
453 Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL,
454 ThisVT, Part0, DAG.getIntPtrConstant(0));
455
456 if (ThisBits == PartBits && ThisVT != PartVT) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000457 Part0 = DAG.getNode(ISD::BITCAST, DL, PartVT, Part0);
458 Part1 = DAG.getNode(ISD::BITCAST, DL, PartVT, Part1);
Chris Lattnera13b8602010-08-24 23:10:06 +0000459 }
460 }
461 }
462
463 if (TLI.isBigEndian())
464 std::reverse(Parts, Parts + OrigNumParts);
465}
466
467
468/// getCopyToPartsVector - Create a series of nodes that contain the specified
469/// value split into legal parts.
Andrew Trickac6d9be2013-05-25 02:42:55 +0000470static void getCopyToPartsVector(SelectionDAG &DAG, SDLoc DL,
Chris Lattnera13b8602010-08-24 23:10:06 +0000471 SDValue Val, SDValue *Parts, unsigned NumParts,
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000472 MVT PartVT, const Value *V) {
Chris Lattnera13b8602010-08-24 23:10:06 +0000473 EVT ValueVT = Val.getValueType();
474 assert(ValueVT.isVector() && "Not a vector");
475 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Michael J. Spencere70c5262010-10-16 08:25:21 +0000476
Chris Lattnera13b8602010-08-24 23:10:06 +0000477 if (NumParts == 1) {
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000478 EVT PartEVT = PartVT;
479 if (PartEVT == ValueVT) {
Chris Lattnere6f7c262010-08-25 22:49:25 +0000480 // Nothing to do.
481 } else if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) {
482 // Bitconvert vector->vector case.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000483 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
Chris Lattnere6f7c262010-08-25 22:49:25 +0000484 } else if (PartVT.isVector() &&
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000485 PartEVT.getVectorElementType() == ValueVT.getVectorElementType() &&
486 PartEVT.getVectorNumElements() > ValueVT.getVectorNumElements()) {
Chris Lattnere6f7c262010-08-25 22:49:25 +0000487 EVT ElementVT = PartVT.getVectorElementType();
488 // Vector widening case, e.g. <2 x float> -> <4 x float>. Shuffle in
489 // undef elements.
490 SmallVector<SDValue, 16> Ops;
491 for (unsigned i = 0, e = ValueVT.getVectorNumElements(); i != e; ++i)
492 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
493 ElementVT, Val, DAG.getIntPtrConstant(i)));
Michael J. Spencere70c5262010-10-16 08:25:21 +0000494
Chris Lattnere6f7c262010-08-25 22:49:25 +0000495 for (unsigned i = ValueVT.getVectorNumElements(),
496 e = PartVT.getVectorNumElements(); i != e; ++i)
497 Ops.push_back(DAG.getUNDEF(ElementVT));
498
499 Val = DAG.getNode(ISD::BUILD_VECTOR, DL, PartVT, &Ops[0], Ops.size());
500
501 // FIXME: Use CONCAT for 2x -> 4x.
Michael J. Spencere70c5262010-10-16 08:25:21 +0000502
Chris Lattnere6f7c262010-08-25 22:49:25 +0000503 //SDValue UndefElts = DAG.getUNDEF(VectorTy);
504 //Val = DAG.getNode(ISD::CONCAT_VECTORS, DL, PartVT, Val, UndefElts);
Nadav Rotem0b666362011-06-04 20:58:08 +0000505 } else if (PartVT.isVector() &&
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000506 PartEVT.getVectorElementType().bitsGE(
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000507 ValueVT.getVectorElementType()) &&
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000508 PartEVT.getVectorNumElements() == ValueVT.getVectorNumElements()) {
Nadav Rotem0b666362011-06-04 20:58:08 +0000509
510 // Promoted vector extract
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000511 bool Smaller = PartEVT.bitsLE(ValueVT);
Nadav Rotemc6341e62011-06-19 08:49:38 +0000512 Val = DAG.getNode((Smaller ? ISD::TRUNCATE : ISD::ANY_EXTEND),
513 DL, PartVT, Val);
Nadav Rotem0b666362011-06-04 20:58:08 +0000514 } else{
Chris Lattnere6f7c262010-08-25 22:49:25 +0000515 // Vector -> scalar conversion.
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000516 assert(ValueVT.getVectorNumElements() == 1 &&
Chris Lattnere6f7c262010-08-25 22:49:25 +0000517 "Only trivial vector-to-scalar conversions should get here!");
518 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
519 PartVT, Val, DAG.getIntPtrConstant(0));
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000520
521 bool Smaller = ValueVT.bitsLE(PartVT);
522 Val = DAG.getNode((Smaller ? ISD::TRUNCATE : ISD::ANY_EXTEND),
523 DL, PartVT, Val);
Chris Lattnera13b8602010-08-24 23:10:06 +0000524 }
Michael J. Spencere70c5262010-10-16 08:25:21 +0000525
Chris Lattnera13b8602010-08-24 23:10:06 +0000526 Parts[0] = Val;
527 return;
528 }
Michael J. Spencere70c5262010-10-16 08:25:21 +0000529
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000530 // Handle a multi-element vector.
Patrik Hagglundee211d22012-12-19 11:53:21 +0000531 EVT IntermediateVT;
532 MVT RegisterVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000533 unsigned NumIntermediates;
Owen Anderson23b9b192009-08-12 00:36:31 +0000534 unsigned NumRegs = TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT,
Devang Patel8f09bea2010-08-26 20:32:32 +0000535 IntermediateVT,
536 NumIntermediates, RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000537 unsigned NumElements = ValueVT.getVectorNumElements();
Michael J. Spencere70c5262010-10-16 08:25:21 +0000538
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000539 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
540 NumParts = NumRegs; // Silence a compiler warning.
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000541 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
Michael J. Spencere70c5262010-10-16 08:25:21 +0000542
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000543 // Split the vector into intermediate operands.
544 SmallVector<SDValue, 8> Ops(NumIntermediates);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000545 for (unsigned i = 0; i != NumIntermediates; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000546 if (IntermediateVT.isVector())
Chris Lattnera13b8602010-08-24 23:10:06 +0000547 Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000548 IntermediateVT, Val,
Chris Lattnera13b8602010-08-24 23:10:06 +0000549 DAG.getIntPtrConstant(i * (NumElements / NumIntermediates)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000550 else
Chris Lattnera13b8602010-08-24 23:10:06 +0000551 Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
Chris Lattnere6f7c262010-08-25 22:49:25 +0000552 IntermediateVT, Val, DAG.getIntPtrConstant(i));
Bill Wendling3ea3c242009-12-22 02:10:19 +0000553 }
Michael J. Spencere70c5262010-10-16 08:25:21 +0000554
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000555 // Split the intermediate operands into legal parts.
556 if (NumParts == NumIntermediates) {
557 // If the register was not expanded, promote or copy the value,
558 // as appropriate.
559 for (unsigned i = 0; i != NumParts; ++i)
Bill Wendlingf18eb582012-09-26 06:16:18 +0000560 getCopyToParts(DAG, DL, Ops[i], &Parts[i], 1, PartVT, V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000561 } else if (NumParts > 0) {
562 // If the intermediate type was expanded, split each the value into
563 // legal parts.
564 assert(NumParts % NumIntermediates == 0 &&
565 "Must expand into a divisible number of parts!");
566 unsigned Factor = NumParts / NumIntermediates;
567 for (unsigned i = 0; i != NumIntermediates; ++i)
Bill Wendlingf18eb582012-09-26 06:16:18 +0000568 getCopyToParts(DAG, DL, Ops[i], &Parts[i*Factor], Factor, PartVT, V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000569 }
570}
571
Dan Gohman462f6b52010-05-29 17:53:24 +0000572namespace {
573 /// RegsForValue - This struct represents the registers (physical or virtual)
574 /// that a particular set of values is assigned, and the type information
575 /// about the value. The most common situation is to represent one value at a
576 /// time, but struct or array values are handled element-wise as multiple
577 /// values. The splitting of aggregates is performed recursively, so that we
578 /// never have aggregate-typed registers. The values at this point do not
579 /// necessarily have legal types, so each value may require one or more
580 /// registers of some legal type.
581 ///
582 struct RegsForValue {
583 /// ValueVTs - The value types of the values, which may not be legal, and
584 /// may need be promoted or synthesized from one or more registers.
585 ///
586 SmallVector<EVT, 4> ValueVTs;
587
588 /// RegVTs - The value types of the registers. This is the same size as
589 /// ValueVTs and it records, for each value, what the type of the assigned
590 /// register or registers are. (Individual values are never synthesized
591 /// from more than one type of register.)
592 ///
593 /// With virtual registers, the contents of RegVTs is redundant with TLI's
594 /// getRegisterType member function, however when with physical registers
595 /// it is necessary to have a separate record of the types.
596 ///
Patrik Hagglund8963fec2012-12-19 12:23:01 +0000597 SmallVector<MVT, 4> RegVTs;
Dan Gohman462f6b52010-05-29 17:53:24 +0000598
599 /// Regs - This list holds the registers assigned to the values.
600 /// Each legal or promoted value requires one register, and each
601 /// expanded value requires multiple registers.
602 ///
603 SmallVector<unsigned, 4> Regs;
604
605 RegsForValue() {}
606
607 RegsForValue(const SmallVector<unsigned, 4> &regs,
Patrik Hagglund8963fec2012-12-19 12:23:01 +0000608 MVT regvt, EVT valuevt)
Dan Gohman462f6b52010-05-29 17:53:24 +0000609 : ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs) {}
610
Dan Gohman462f6b52010-05-29 17:53:24 +0000611 RegsForValue(LLVMContext &Context, const TargetLowering &tli,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000612 unsigned Reg, Type *Ty) {
Dan Gohman462f6b52010-05-29 17:53:24 +0000613 ComputeValueVTs(tli, Ty, ValueVTs);
614
615 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
616 EVT ValueVT = ValueVTs[Value];
617 unsigned NumRegs = tli.getNumRegisters(Context, ValueVT);
Patrik Hagglunddfcf33a2012-12-19 11:48:16 +0000618 MVT RegisterVT = tli.getRegisterType(Context, ValueVT);
Dan Gohman462f6b52010-05-29 17:53:24 +0000619 for (unsigned i = 0; i != NumRegs; ++i)
620 Regs.push_back(Reg + i);
621 RegVTs.push_back(RegisterVT);
622 Reg += NumRegs;
623 }
624 }
625
626 /// areValueTypesLegal - Return true if types of all the values are legal.
627 bool areValueTypesLegal(const TargetLowering &TLI) {
628 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
Patrik Hagglund8963fec2012-12-19 12:23:01 +0000629 MVT RegisterVT = RegVTs[Value];
Dan Gohman462f6b52010-05-29 17:53:24 +0000630 if (!TLI.isTypeLegal(RegisterVT))
631 return false;
632 }
633 return true;
634 }
635
636 /// append - Add the specified values to this one.
637 void append(const RegsForValue &RHS) {
638 ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end());
639 RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end());
640 Regs.append(RHS.Regs.begin(), RHS.Regs.end());
641 }
642
643 /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
644 /// this value and returns the result as a ValueVTs value. This uses
645 /// Chain/Flag as the input and updates them for the output Chain/Flag.
646 /// If the Flag pointer is NULL, no flag is used.
647 SDValue getCopyFromRegs(SelectionDAG &DAG, FunctionLoweringInfo &FuncInfo,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000648 SDLoc dl,
Bill Wendling12931302012-09-26 04:04:19 +0000649 SDValue &Chain, SDValue *Flag,
650 const Value *V = 0) const;
Dan Gohman462f6b52010-05-29 17:53:24 +0000651
652 /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
653 /// specified value into the registers specified by this object. This uses
654 /// Chain/Flag as the input and updates them for the output Chain/Flag.
655 /// If the Flag pointer is NULL, no flag is used.
Andrew Trickac6d9be2013-05-25 02:42:55 +0000656 void getCopyToRegs(SDValue Val, SelectionDAG &DAG, SDLoc dl,
Bill Wendlingf18eb582012-09-26 06:16:18 +0000657 SDValue &Chain, SDValue *Flag, const Value *V) const;
Dan Gohman462f6b52010-05-29 17:53:24 +0000658
659 /// AddInlineAsmOperands - Add this value to the specified inlineasm node
660 /// operand list. This adds the code marker, matching input operand index
661 /// (if applicable), and includes the number of values added into it.
662 void AddInlineAsmOperands(unsigned Kind,
663 bool HasMatching, unsigned MatchingIdx,
664 SelectionDAG &DAG,
665 std::vector<SDValue> &Ops) const;
666 };
667}
668
669/// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
670/// this value and returns the result as a ValueVT value. This uses
671/// Chain/Flag as the input and updates them for the output Chain/Flag.
672/// If the Flag pointer is NULL, no flag is used.
673SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG,
674 FunctionLoweringInfo &FuncInfo,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000675 SDLoc dl,
Bill Wendling12931302012-09-26 04:04:19 +0000676 SDValue &Chain, SDValue *Flag,
677 const Value *V) const {
Dan Gohman7da5d3f2010-07-26 18:15:41 +0000678 // A Value with type {} or [0 x %t] needs no registers.
679 if (ValueVTs.empty())
680 return SDValue();
681
Dan Gohman462f6b52010-05-29 17:53:24 +0000682 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
683
684 // Assemble the legal parts into the final values.
685 SmallVector<SDValue, 4> Values(ValueVTs.size());
686 SmallVector<SDValue, 8> Parts;
687 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
688 // Copy the legal parts from the registers.
689 EVT ValueVT = ValueVTs[Value];
690 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVT);
Patrik Hagglund8963fec2012-12-19 12:23:01 +0000691 MVT RegisterVT = RegVTs[Value];
Dan Gohman462f6b52010-05-29 17:53:24 +0000692
693 Parts.resize(NumRegs);
694 for (unsigned i = 0; i != NumRegs; ++i) {
695 SDValue P;
696 if (Flag == 0) {
697 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
698 } else {
699 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag);
700 *Flag = P.getValue(2);
701 }
702
703 Chain = P.getValue(1);
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000704 Parts[i] = P;
Dan Gohman462f6b52010-05-29 17:53:24 +0000705
706 // If the source register was virtual and if we know something about it,
707 // add an assert node.
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000708 if (!TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) ||
Cameron Zwariche1497b92011-02-24 10:00:08 +0000709 !RegisterVT.isInteger() || RegisterVT.isVector())
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000710 continue;
Cameron Zwariche1497b92011-02-24 10:00:08 +0000711
712 const FunctionLoweringInfo::LiveOutInfo *LOI =
713 FuncInfo.GetLiveOutRegInfo(Regs[Part+i]);
714 if (!LOI)
715 continue;
Dan Gohman462f6b52010-05-29 17:53:24 +0000716
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000717 unsigned RegSize = RegisterVT.getSizeInBits();
Cameron Zwariche1497b92011-02-24 10:00:08 +0000718 unsigned NumSignBits = LOI->NumSignBits;
719 unsigned NumZeroBits = LOI->KnownZero.countLeadingOnes();
Dan Gohman462f6b52010-05-29 17:53:24 +0000720
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000721 // FIXME: We capture more information than the dag can represent. For
722 // now, just use the tightest assertzext/assertsext possible.
723 bool isSExt = true;
724 EVT FromVT(MVT::Other);
725 if (NumSignBits == RegSize)
726 isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1
727 else if (NumZeroBits >= RegSize-1)
728 isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1
729 else if (NumSignBits > RegSize-8)
730 isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8
731 else if (NumZeroBits >= RegSize-8)
732 isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8
733 else if (NumSignBits > RegSize-16)
734 isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16
735 else if (NumZeroBits >= RegSize-16)
736 isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16
737 else if (NumSignBits > RegSize-32)
738 isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32
739 else if (NumZeroBits >= RegSize-32)
740 isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32
741 else
742 continue;
Dan Gohman462f6b52010-05-29 17:53:24 +0000743
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000744 // Add an assertion node.
745 assert(FromVT != MVT::Other);
746 Parts[i] = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
747 RegisterVT, P, DAG.getValueType(FromVT));
Dan Gohman462f6b52010-05-29 17:53:24 +0000748 }
749
750 Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(),
Bill Wendling12931302012-09-26 04:04:19 +0000751 NumRegs, RegisterVT, ValueVT, V);
Dan Gohman462f6b52010-05-29 17:53:24 +0000752 Part += NumRegs;
753 Parts.clear();
754 }
755
756 return DAG.getNode(ISD::MERGE_VALUES, dl,
757 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
758 &Values[0], ValueVTs.size());
759}
760
761/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
762/// specified value into the registers specified by this object. This uses
763/// Chain/Flag as the input and updates them for the output Chain/Flag.
764/// If the Flag pointer is NULL, no flag is used.
Andrew Trickac6d9be2013-05-25 02:42:55 +0000765void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, SDLoc dl,
Bill Wendlingf18eb582012-09-26 06:16:18 +0000766 SDValue &Chain, SDValue *Flag,
767 const Value *V) const {
Dan Gohman462f6b52010-05-29 17:53:24 +0000768 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
769
770 // Get the list of the values's legal parts.
771 unsigned NumRegs = Regs.size();
772 SmallVector<SDValue, 8> Parts(NumRegs);
773 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
774 EVT ValueVT = ValueVTs[Value];
775 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), ValueVT);
Patrik Hagglund8963fec2012-12-19 12:23:01 +0000776 MVT RegisterVT = RegVTs[Value];
Evan Cheng2766a472012-12-06 19:13:27 +0000777 ISD::NodeType ExtendKind =
778 TLI.isZExtFree(Val, RegisterVT)? ISD::ZERO_EXTEND: ISD::ANY_EXTEND;
Dan Gohman462f6b52010-05-29 17:53:24 +0000779
Chris Lattner3ac18842010-08-24 23:20:40 +0000780 getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value),
Evan Cheng2766a472012-12-06 19:13:27 +0000781 &Parts[Part], NumParts, RegisterVT, V, ExtendKind);
Dan Gohman462f6b52010-05-29 17:53:24 +0000782 Part += NumParts;
783 }
784
785 // Copy the parts into the registers.
786 SmallVector<SDValue, 8> Chains(NumRegs);
787 for (unsigned i = 0; i != NumRegs; ++i) {
788 SDValue Part;
789 if (Flag == 0) {
790 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
791 } else {
792 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag);
793 *Flag = Part.getValue(1);
794 }
795
796 Chains[i] = Part.getValue(0);
797 }
798
799 if (NumRegs == 1 || Flag)
800 // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
801 // flagged to it. That is the CopyToReg nodes and the user are considered
802 // a single scheduling unit. If we create a TokenFactor and return it as
803 // chain, then the TokenFactor is both a predecessor (operand) of the
804 // user as well as a successor (the TF operands are flagged to the user).
805 // c1, f1 = CopyToReg
806 // c2, f2 = CopyToReg
807 // c3 = TokenFactor c1, c2
808 // ...
809 // = op c3, ..., f2
810 Chain = Chains[NumRegs-1];
811 else
812 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs);
813}
814
815/// AddInlineAsmOperands - Add this value to the specified inlineasm node
816/// operand list. This adds the code marker and includes the number of
817/// values added into it.
818void RegsForValue::AddInlineAsmOperands(unsigned Code, bool HasMatching,
819 unsigned MatchingIdx,
820 SelectionDAG &DAG,
821 std::vector<SDValue> &Ops) const {
822 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
823
824 unsigned Flag = InlineAsm::getFlagWord(Code, Regs.size());
825 if (HasMatching)
826 Flag = InlineAsm::getFlagWordForMatchingOp(Flag, MatchingIdx);
Jakob Stoklund Olesen459b74b2011-10-12 23:37:29 +0000827 else if (!Regs.empty() &&
828 TargetRegisterInfo::isVirtualRegister(Regs.front())) {
829 // Put the register class of the virtual registers in the flag word. That
830 // way, later passes can recompute register class constraints for inline
831 // assembly as well as normal instructions.
832 // Don't do this for tied operands that can use the regclass information
833 // from the def.
834 const MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
835 const TargetRegisterClass *RC = MRI.getRegClass(Regs.front());
836 Flag = InlineAsm::getFlagWordForRegClass(Flag, RC->getID());
837 }
838
Dan Gohman462f6b52010-05-29 17:53:24 +0000839 SDValue Res = DAG.getTargetConstant(Flag, MVT::i32);
840 Ops.push_back(Res);
841
842 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
843 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVTs[Value]);
Patrik Hagglund8963fec2012-12-19 12:23:01 +0000844 MVT RegisterVT = RegVTs[Value];
Dan Gohman462f6b52010-05-29 17:53:24 +0000845 for (unsigned i = 0; i != NumRegs; ++i) {
846 assert(Reg < Regs.size() && "Mismatch in # registers expected");
847 Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT));
848 }
849 }
850}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000851
Owen Anderson243eb9e2011-12-08 22:15:21 +0000852void SelectionDAGBuilder::init(GCFunctionInfo *gfi, AliasAnalysis &aa,
853 const TargetLibraryInfo *li) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000854 AA = &aa;
855 GFI = gfi;
Owen Anderson243eb9e2011-12-08 22:15:21 +0000856 LibInfo = li;
Micah Villmow3574eca2012-10-08 16:38:25 +0000857 TD = DAG.getTarget().getDataLayout();
Richard Smithcb1f68d2012-08-22 00:42:39 +0000858 Context = DAG.getContext();
Bill Wendling4ed1fb02011-10-15 01:00:26 +0000859 LPadToCallSiteMap.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000860}
861
Dan Gohmanb02b62a2010-04-14 18:24:06 +0000862/// clear - Clear out the current SelectionDAG and the associated
Dan Gohman2048b852009-11-23 18:04:58 +0000863/// state and prepare this SelectionDAGBuilder object to be used
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000864/// for a new block. This doesn't clear out information about
865/// additional blocks that are needed to complete switch lowering
866/// or PHI node updating; that information is cleared out as it is
867/// consumed.
Dan Gohman2048b852009-11-23 18:04:58 +0000868void SelectionDAGBuilder::clear() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000869 NodeMap.clear();
Devang Patel9126c0d2010-06-01 19:59:01 +0000870 UnusedArgNodeMap.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000871 PendingLoads.clear();
872 PendingExports.clear();
Andrew Trickea5db0c2013-05-25 02:20:36 +0000873 CurInst = NULL;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000874 HasTailCall = false;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000875}
876
Devang Patel23385752011-05-23 17:44:13 +0000877/// clearDanglingDebugInfo - Clear the dangling debug information
Benjamin Kramerd9b0b022012-06-02 10:20:22 +0000878/// map. This function is separated from the clear so that debug
Devang Patel23385752011-05-23 17:44:13 +0000879/// information that is dangling in a basic block can be properly
880/// resolved in a different basic block. This allows the
881/// SelectionDAG to resolve dangling debug information attached
882/// to PHI nodes.
883void SelectionDAGBuilder::clearDanglingDebugInfo() {
884 DanglingDebugInfoMap.clear();
885}
886
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000887/// getRoot - Return the current virtual root of the Selection DAG,
888/// flushing any PendingLoad items. This must be done before emitting
889/// a store or any other node that may need to be ordered after any
890/// prior load instructions.
891///
Dan Gohman2048b852009-11-23 18:04:58 +0000892SDValue SelectionDAGBuilder::getRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000893 if (PendingLoads.empty())
894 return DAG.getRoot();
895
896 if (PendingLoads.size() == 1) {
897 SDValue Root = PendingLoads[0];
898 DAG.setRoot(Root);
899 PendingLoads.clear();
900 return Root;
901 }
902
903 // Otherwise, we have to make a token factor node.
Andrew Trickac6d9be2013-05-25 02:42:55 +0000904 SDValue Root = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000905 &PendingLoads[0], PendingLoads.size());
906 PendingLoads.clear();
907 DAG.setRoot(Root);
908 return Root;
909}
910
911/// getControlRoot - Similar to getRoot, but instead of flushing all the
912/// PendingLoad items, flush all the PendingExports items. It is necessary
913/// to do this before emitting a terminator instruction.
914///
Dan Gohman2048b852009-11-23 18:04:58 +0000915SDValue SelectionDAGBuilder::getControlRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000916 SDValue Root = DAG.getRoot();
917
918 if (PendingExports.empty())
919 return Root;
920
921 // Turn all of the CopyToReg chains into one factored node.
922 if (Root.getOpcode() != ISD::EntryToken) {
923 unsigned i = 0, e = PendingExports.size();
924 for (; i != e; ++i) {
925 assert(PendingExports[i].getNode()->getNumOperands() > 1);
926 if (PendingExports[i].getNode()->getOperand(0) == Root)
927 break; // Don't add the root if we already indirectly depend on it.
928 }
929
930 if (i == e)
931 PendingExports.push_back(Root);
932 }
933
Andrew Trickac6d9be2013-05-25 02:42:55 +0000934 Root = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000935 &PendingExports[0],
936 PendingExports.size());
937 PendingExports.clear();
938 DAG.setRoot(Root);
939 return Root;
940}
941
Dan Gohman46510a72010-04-15 01:51:59 +0000942void SelectionDAGBuilder::visit(const Instruction &I) {
Dan Gohmanc105a2b2010-04-22 20:55:53 +0000943 // Set up outgoing PHI node register values before emitting the terminator.
944 if (isa<TerminatorInst>(&I))
945 HandlePHINodesInSuccessorBlocks(I.getParent());
946
Andrew Trickdd0fb012013-05-25 03:08:10 +0000947 ++SDNodeOrder;
948
Andrew Trickea5db0c2013-05-25 02:20:36 +0000949 CurInst = &I;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000950
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000951 visit(I.getOpcode(), I);
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000952
Dan Gohman92884f72010-04-20 15:03:56 +0000953 if (!isa<TerminatorInst>(&I) && !HasTailCall)
954 CopyToExportRegsIfNeeded(&I);
955
Andrew Trickea5db0c2013-05-25 02:20:36 +0000956 CurInst = NULL;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000957}
958
Dan Gohmanba5be5c2010-04-20 15:00:41 +0000959void SelectionDAGBuilder::visitPHI(const PHINode &) {
960 llvm_unreachable("SelectionDAGBuilder shouldn't visit PHI nodes!");
961}
962
Dan Gohman46510a72010-04-15 01:51:59 +0000963void SelectionDAGBuilder::visit(unsigned Opcode, const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000964 // Note: this doesn't use InstVisitor, because it has to work with
965 // ConstantExpr's in addition to instructions.
966 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000967 default: llvm_unreachable("Unknown instruction type encountered!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000968 // Build the switch statement using the Instruction.def file.
969#define HANDLE_INST(NUM, OPCODE, CLASS) \
Galina Kistanova72ea0c92012-07-19 04:50:12 +0000970 case Instruction::OPCODE: visit##OPCODE((const CLASS&)I); break;
Chandler Carruth0b8c9a82013-01-02 11:36:10 +0000971#include "llvm/IR/Instruction.def"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000972 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000973}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000974
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000975// resolveDanglingDebugInfo - if we saw an earlier dbg_value referring to V,
976// generate the debug data structures now that we've seen its definition.
977void SelectionDAGBuilder::resolveDanglingDebugInfo(const Value *V,
978 SDValue Val) {
979 DanglingDebugInfo &DDI = DanglingDebugInfoMap[V];
Devang Patel4cf81c42010-08-26 23:35:15 +0000980 if (DDI.getDI()) {
981 const DbgValueInst *DI = DDI.getDI();
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000982 DebugLoc dl = DDI.getdl();
983 unsigned DbgSDNodeOrder = DDI.getSDNodeOrder();
Devang Patel4cf81c42010-08-26 23:35:15 +0000984 MDNode *Variable = DI->getVariable();
985 uint64_t Offset = DI->getOffset();
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000986 SDDbgValue *SDV;
987 if (Val.getNode()) {
Devang Patel78a06e52010-08-25 20:39:26 +0000988 if (!EmitFuncArgumentDbgValue(V, Variable, Offset, Val)) {
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000989 SDV = DAG.getDbgValue(Variable, Val.getNode(),
990 Val.getResNo(), Offset, dl, DbgSDNodeOrder);
991 DAG.AddDbgValue(SDV, Val.getNode(), false);
992 }
Owen Anderson95771af2011-02-25 21:41:48 +0000993 } else
Adrian Prantl5da4e4f2013-05-22 18:02:19 +0000994 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000995 DanglingDebugInfoMap[V] = DanglingDebugInfo();
996 }
997}
998
Nick Lewycky8de34002011-09-30 22:19:53 +0000999/// getValue - Return an SDValue for the given Value.
Dan Gohman2048b852009-11-23 18:04:58 +00001000SDValue SelectionDAGBuilder::getValue(const Value *V) {
Dan Gohman28a17352010-07-01 01:59:43 +00001001 // If we already have an SDValue for this value, use it. It's important
1002 // to do this first, so that we don't create a CopyFromReg if we already
1003 // have a regular SDValue.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001004 SDValue &N = NodeMap[V];
1005 if (N.getNode()) return N;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001006
Dan Gohman28a17352010-07-01 01:59:43 +00001007 // If there's a virtual register allocated and initialized for this
1008 // value, use it.
1009 DenseMap<const Value *, unsigned>::iterator It = FuncInfo.ValueMap.find(V);
1010 if (It != FuncInfo.ValueMap.end()) {
1011 unsigned InReg = It->second;
1012 RegsForValue RFV(*DAG.getContext(), TLI, InReg, V->getType());
1013 SDValue Chain = DAG.getEntryNode();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001014 N = RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, NULL, V);
Devang Patel8f314282011-01-25 18:09:58 +00001015 resolveDanglingDebugInfo(V, N);
1016 return N;
Dan Gohman28a17352010-07-01 01:59:43 +00001017 }
1018
1019 // Otherwise create a new SDValue and remember it.
1020 SDValue Val = getValueImpl(V);
1021 NodeMap[V] = Val;
Dale Johannesenbdc09d92010-07-16 00:02:08 +00001022 resolveDanglingDebugInfo(V, Val);
Dan Gohman28a17352010-07-01 01:59:43 +00001023 return Val;
1024}
1025
1026/// getNonRegisterValue - Return an SDValue for the given Value, but
1027/// don't look in FuncInfo.ValueMap for a virtual register.
1028SDValue SelectionDAGBuilder::getNonRegisterValue(const Value *V) {
1029 // If we already have an SDValue for this value, use it.
1030 SDValue &N = NodeMap[V];
1031 if (N.getNode()) return N;
1032
1033 // Otherwise create a new SDValue and remember it.
1034 SDValue Val = getValueImpl(V);
1035 NodeMap[V] = Val;
Dale Johannesenbdc09d92010-07-16 00:02:08 +00001036 resolveDanglingDebugInfo(V, Val);
Dan Gohman28a17352010-07-01 01:59:43 +00001037 return Val;
1038}
1039
Dale Johannesenbdc09d92010-07-16 00:02:08 +00001040/// getValueImpl - Helper function for getValue and getNonRegisterValue.
Dan Gohman28a17352010-07-01 01:59:43 +00001041/// Create an SDValue for the given value.
1042SDValue SelectionDAGBuilder::getValueImpl(const Value *V) {
Dan Gohman383b5f62010-04-17 15:32:28 +00001043 if (const Constant *C = dyn_cast<Constant>(V)) {
Owen Andersone50ed302009-08-10 22:56:29 +00001044 EVT VT = TLI.getValueType(V->getType(), true);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001045
Dan Gohman383b5f62010-04-17 15:32:28 +00001046 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C))
Dan Gohman28a17352010-07-01 01:59:43 +00001047 return DAG.getConstant(*CI, VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001048
Dan Gohman383b5f62010-04-17 15:32:28 +00001049 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001050 return DAG.getGlobalAddress(GV, getCurSDLoc(), VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001051
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001052 if (isa<ConstantPointerNull>(C))
Dan Gohman28a17352010-07-01 01:59:43 +00001053 return DAG.getConstant(0, TLI.getPointerTy());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001054
Dan Gohman383b5f62010-04-17 15:32:28 +00001055 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Dan Gohman28a17352010-07-01 01:59:43 +00001056 return DAG.getConstantFP(*CFP, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001057
Nate Begeman9008ca62009-04-27 18:41:29 +00001058 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
Dan Gohman28a17352010-07-01 01:59:43 +00001059 return DAG.getUNDEF(VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001060
Dan Gohman383b5f62010-04-17 15:32:28 +00001061 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001062 visit(CE->getOpcode(), *CE);
1063 SDValue N1 = NodeMap[V];
Dan Gohmanac7d05c2010-04-16 16:55:18 +00001064 assert(N1.getNode() && "visit didn't populate the NodeMap!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001065 return N1;
1066 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001067
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001068 if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
1069 SmallVector<SDValue, 4> Constants;
1070 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
1071 OI != OE; ++OI) {
1072 SDNode *Val = getValue(*OI).getNode();
Dan Gohmaned48caf2009-09-08 01:44:02 +00001073 // If the operand is an empty aggregate, there are no values.
1074 if (!Val) continue;
1075 // Add each leaf value from the operand to the Constants list
1076 // to form a flattened list of all the values.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001077 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
1078 Constants.push_back(SDValue(Val, i));
1079 }
Bill Wendling87710f02009-12-21 23:47:40 +00001080
Bill Wendling4533cac2010-01-28 21:51:40 +00001081 return DAG.getMergeValues(&Constants[0], Constants.size(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001082 getCurSDLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001083 }
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001084
1085 if (const ConstantDataSequential *CDS =
1086 dyn_cast<ConstantDataSequential>(C)) {
1087 SmallVector<SDValue, 4> Ops;
Chris Lattner0f193b82012-01-25 01:27:20 +00001088 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001089 SDNode *Val = getValue(CDS->getElementAsConstant(i)).getNode();
1090 // Add each leaf value from the operand to the Constants list
1091 // to form a flattened list of all the values.
1092 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
1093 Ops.push_back(SDValue(Val, i));
1094 }
1095
1096 if (isa<ArrayType>(CDS->getType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001097 return DAG.getMergeValues(&Ops[0], Ops.size(), getCurSDLoc());
1098 return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurSDLoc(),
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001099 VT, &Ops[0], Ops.size());
1100 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001101
Duncan Sands1df98592010-02-16 11:11:14 +00001102 if (C->getType()->isStructTy() || C->getType()->isArrayTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001103 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
1104 "Unknown struct or array constant!");
1105
Owen Andersone50ed302009-08-10 22:56:29 +00001106 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001107 ComputeValueVTs(TLI, C->getType(), ValueVTs);
1108 unsigned NumElts = ValueVTs.size();
1109 if (NumElts == 0)
1110 return SDValue(); // empty struct
1111 SmallVector<SDValue, 4> Constants(NumElts);
1112 for (unsigned i = 0; i != NumElts; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00001113 EVT EltVT = ValueVTs[i];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001114 if (isa<UndefValue>(C))
Dale Johannesene8d72302009-02-06 23:05:02 +00001115 Constants[i] = DAG.getUNDEF(EltVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001116 else if (EltVT.isFloatingPoint())
1117 Constants[i] = DAG.getConstantFP(0, EltVT);
1118 else
1119 Constants[i] = DAG.getConstant(0, EltVT);
1120 }
Bill Wendling87710f02009-12-21 23:47:40 +00001121
Bill Wendling4533cac2010-01-28 21:51:40 +00001122 return DAG.getMergeValues(&Constants[0], NumElts,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001123 getCurSDLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001124 }
1125
Dan Gohman383b5f62010-04-17 15:32:28 +00001126 if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
Dan Gohman29cbade2009-11-20 23:18:13 +00001127 return DAG.getBlockAddress(BA, VT);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001128
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001129 VectorType *VecTy = cast<VectorType>(V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001130 unsigned NumElements = VecTy->getNumElements();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001131
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001132 // Now that we know the number and type of the elements, get that number of
1133 // elements into the Ops array based on what kind of constant it is.
1134 SmallVector<SDValue, 16> Ops;
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001135 if (const ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001136 for (unsigned i = 0; i != NumElements; ++i)
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001137 Ops.push_back(getValue(CV->getOperand(i)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001138 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00001139 assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!");
Owen Andersone50ed302009-08-10 22:56:29 +00001140 EVT EltVT = TLI.getValueType(VecTy->getElementType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001141
1142 SDValue Op;
Nate Begeman9008ca62009-04-27 18:41:29 +00001143 if (EltVT.isFloatingPoint())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001144 Op = DAG.getConstantFP(0, EltVT);
1145 else
1146 Op = DAG.getConstant(0, EltVT);
1147 Ops.assign(NumElements, Op);
1148 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001149
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001150 // Create a BUILD_VECTOR node.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001151 return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00001152 VT, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001153 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001154
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001155 // If this is a static alloca, generate it as the frameindex instead of
1156 // computation.
1157 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
1158 DenseMap<const AllocaInst*, int>::iterator SI =
1159 FuncInfo.StaticAllocaMap.find(AI);
1160 if (SI != FuncInfo.StaticAllocaMap.end())
1161 return DAG.getFrameIndex(SI->second, TLI.getPointerTy());
1162 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001163
Dan Gohman28a17352010-07-01 01:59:43 +00001164 // If this is an instruction which fast-isel has deferred, select it now.
1165 if (const Instruction *Inst = dyn_cast<Instruction>(V)) {
Dan Gohman84023e02010-07-10 09:00:22 +00001166 unsigned InReg = FuncInfo.InitializeRegForValue(Inst);
1167 RegsForValue RFV(*DAG.getContext(), TLI, InReg, Inst->getType());
1168 SDValue Chain = DAG.getEntryNode();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001169 return RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, NULL, V);
Dan Gohman28a17352010-07-01 01:59:43 +00001170 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001171
Dan Gohman28a17352010-07-01 01:59:43 +00001172 llvm_unreachable("Can't get register for value!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001173}
1174
Dan Gohman46510a72010-04-15 01:51:59 +00001175void SelectionDAGBuilder::visitRet(const ReturnInst &I) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001176 SDValue Chain = getControlRoot();
1177 SmallVector<ISD::OutputArg, 8> Outs;
Dan Gohmanc9403652010-07-07 15:54:55 +00001178 SmallVector<SDValue, 8> OutVals;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001179
Dan Gohman7451d3e2010-05-29 17:03:36 +00001180 if (!FuncInfo.CanLowerReturn) {
1181 unsigned DemoteReg = FuncInfo.DemoteRegister;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001182 const Function *F = I.getParent()->getParent();
1183
1184 // Emit a store of the return value through the virtual register.
1185 // Leave Outs empty so that LowerReturn won't try to load return
1186 // registers the usual way.
1187 SmallVector<EVT, 1> PtrValueVTs;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001188 ComputeValueVTs(TLI, PointerType::getUnqual(F->getReturnType()),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001189 PtrValueVTs);
1190
1191 SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]);
1192 SDValue RetOp = getValue(I.getOperand(0));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001193
Owen Andersone50ed302009-08-10 22:56:29 +00001194 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001195 SmallVector<uint64_t, 4> Offsets;
1196 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets);
Dan Gohman7ea1ca62008-10-21 20:00:42 +00001197 unsigned NumValues = ValueVTs.size();
Dan Gohman7ea1ca62008-10-21 20:00:42 +00001198
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001199 SmallVector<SDValue, 4> Chains(NumValues);
Bill Wendling87710f02009-12-21 23:47:40 +00001200 for (unsigned i = 0; i != NumValues; ++i) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001201 SDValue Add = DAG.getNode(ISD::ADD, getCurSDLoc(),
Chris Lattnera13b8602010-08-24 23:10:06 +00001202 RetPtr.getValueType(), RetPtr,
1203 DAG.getIntPtrConstant(Offsets[i]));
Bill Wendling87710f02009-12-21 23:47:40 +00001204 Chains[i] =
Andrew Trickac6d9be2013-05-25 02:42:55 +00001205 DAG.getStore(Chain, getCurSDLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001206 SDValue(RetOp.getNode(), RetOp.getResNo() + i),
Chris Lattner84bd98a2010-09-21 18:58:22 +00001207 // FIXME: better loc info would be nice.
1208 Add, MachinePointerInfo(), false, false, 0);
Bill Wendling87710f02009-12-21 23:47:40 +00001209 }
1210
Andrew Trickac6d9be2013-05-25 02:42:55 +00001211 Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001212 MVT::Other, &Chains[0], NumValues);
Chris Lattner25d58372010-02-28 18:53:13 +00001213 } else if (I.getNumOperands() != 0) {
1214 SmallVector<EVT, 4> ValueVTs;
1215 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs);
1216 unsigned NumValues = ValueVTs.size();
1217 if (NumValues) {
1218 SDValue RetOp = getValue(I.getOperand(0));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001219 for (unsigned j = 0, f = NumValues; j != f; ++j) {
1220 EVT VT = ValueVTs[j];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001221
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001222 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001223
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001224 const Function *F = I.getParent()->getParent();
Bill Wendling8b62abd2012-12-30 13:01:51 +00001225 if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
1226 Attribute::SExt))
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001227 ExtendKind = ISD::SIGN_EXTEND;
Bill Wendling8b62abd2012-12-30 13:01:51 +00001228 else if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
1229 Attribute::ZExt))
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001230 ExtendKind = ISD::ZERO_EXTEND;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001231
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001232 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger())
Patrik Hagglunde5c65912012-12-19 12:02:25 +00001233 VT = TLI.getTypeForExtArgOrReturn(VT.getSimpleVT(), ExtendKind);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001234
1235 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), VT);
Patrik Hagglunddfcf33a2012-12-19 11:48:16 +00001236 MVT PartVT = TLI.getRegisterType(*DAG.getContext(), VT);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001237 SmallVector<SDValue, 4> Parts(NumParts);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001238 getCopyToParts(DAG, getCurSDLoc(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001239 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
Bill Wendlingf18eb582012-09-26 06:16:18 +00001240 &Parts[0], NumParts, PartVT, &I, ExtendKind);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001241
1242 // 'inreg' on function refers to return value
1243 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
Bill Wendling8b62abd2012-12-30 13:01:51 +00001244 if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
1245 Attribute::InReg))
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001246 Flags.setInReg();
1247
1248 // Propagate extension type if any
Cameron Zwarich8df6bf52011-03-16 22:20:07 +00001249 if (ExtendKind == ISD::SIGN_EXTEND)
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001250 Flags.setSExt();
Cameron Zwarich8df6bf52011-03-16 22:20:07 +00001251 else if (ExtendKind == ISD::ZERO_EXTEND)
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001252 Flags.setZExt();
1253
Dan Gohmanc9403652010-07-07 15:54:55 +00001254 for (unsigned i = 0; i < NumParts; ++i) {
1255 Outs.push_back(ISD::OutputArg(Flags, Parts[i].getValueType(),
Manman Ren0a1544d2012-11-01 23:49:58 +00001256 /*isfixed=*/true, 0, 0));
Dan Gohmanc9403652010-07-07 15:54:55 +00001257 OutVals.push_back(Parts[i]);
1258 }
Evan Cheng3927f432009-03-25 20:20:11 +00001259 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001260 }
1261 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001262
1263 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001264 CallingConv::ID CallConv =
1265 DAG.getMachineFunction().getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001266 Chain = TLI.LowerReturn(Chain, CallConv, isVarArg,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001267 Outs, OutVals, getCurSDLoc(), DAG);
Dan Gohman5e866062009-08-06 15:37:27 +00001268
1269 // Verify that the target's LowerReturn behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00001270 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00001271 "LowerReturn didn't return a valid chain!");
1272
1273 // Update the DAG with the new chain value resulting from return lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001274 DAG.setRoot(Chain);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001275}
1276
Dan Gohmanad62f532009-04-23 23:13:24 +00001277/// CopyToExportRegsIfNeeded - If the given value has virtual registers
1278/// created for it, emit nodes to copy the value into the virtual
1279/// registers.
Dan Gohman46510a72010-04-15 01:51:59 +00001280void SelectionDAGBuilder::CopyToExportRegsIfNeeded(const Value *V) {
Rafael Espindola3fa82832011-05-13 15:18:06 +00001281 // Skip empty types
1282 if (V->getType()->isEmptyTy())
1283 return;
1284
Dan Gohman33b7a292010-04-16 17:15:02 +00001285 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
1286 if (VMI != FuncInfo.ValueMap.end()) {
1287 assert(!V->use_empty() && "Unused value assigned virtual registers!");
1288 CopyValueToVirtualRegister(V, VMI->second);
Dan Gohmanad62f532009-04-23 23:13:24 +00001289 }
1290}
1291
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001292/// ExportFromCurrentBlock - If this condition isn't known to be exported from
1293/// the current basic block, add it to ValueMap now so that we'll get a
1294/// CopyTo/FromReg.
Dan Gohman46510a72010-04-15 01:51:59 +00001295void SelectionDAGBuilder::ExportFromCurrentBlock(const Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001296 // No need to export constants.
1297 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001298
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001299 // Already exported?
1300 if (FuncInfo.isExportedInst(V)) return;
1301
1302 unsigned Reg = FuncInfo.InitializeRegForValue(V);
1303 CopyValueToVirtualRegister(V, Reg);
1304}
1305
Dan Gohman46510a72010-04-15 01:51:59 +00001306bool SelectionDAGBuilder::isExportableFromCurrentBlock(const Value *V,
Dan Gohman2048b852009-11-23 18:04:58 +00001307 const BasicBlock *FromBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001308 // The operands of the setcc have to be in this block. We don't know
1309 // how to export them from some other block.
Dan Gohman46510a72010-04-15 01:51:59 +00001310 if (const Instruction *VI = dyn_cast<Instruction>(V)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001311 // Can export from current BB.
1312 if (VI->getParent() == FromBB)
1313 return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001314
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001315 // Is already exported, noop.
1316 return FuncInfo.isExportedInst(V);
1317 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001318
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001319 // If this is an argument, we can export it if the BB is the entry block or
1320 // if it is already exported.
1321 if (isa<Argument>(V)) {
1322 if (FromBB == &FromBB->getParent()->getEntryBlock())
1323 return true;
1324
1325 // Otherwise, can only export this if it is already exported.
1326 return FuncInfo.isExportedInst(V);
1327 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001328
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001329 // Otherwise, constants can always be exported.
1330 return true;
1331}
1332
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001333/// Return branch probability calculated by BranchProbabilityInfo for IR blocks.
Jakub Staszak25101bb2011-12-20 20:03:10 +00001334uint32_t SelectionDAGBuilder::getEdgeWeight(const MachineBasicBlock *Src,
1335 const MachineBasicBlock *Dst) const {
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001336 BranchProbabilityInfo *BPI = FuncInfo.BPI;
1337 if (!BPI)
1338 return 0;
Jakub Staszak95ece8e2011-07-29 20:05:36 +00001339 const BasicBlock *SrcBB = Src->getBasicBlock();
1340 const BasicBlock *DstBB = Dst->getBasicBlock();
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001341 return BPI->getEdgeWeight(SrcBB, DstBB);
1342}
1343
Jakub Staszakc8f34de2011-07-29 22:25:21 +00001344void SelectionDAGBuilder::
1345addSuccessorWithWeight(MachineBasicBlock *Src, MachineBasicBlock *Dst,
1346 uint32_t Weight /* = 0 */) {
1347 if (!Weight)
1348 Weight = getEdgeWeight(Src, Dst);
1349 Src->addSuccessor(Dst, Weight);
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001350}
1351
1352
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001353static bool InBlock(const Value *V, const BasicBlock *BB) {
1354 if (const Instruction *I = dyn_cast<Instruction>(V))
1355 return I->getParent() == BB;
1356 return true;
1357}
1358
Dan Gohmanc2277342008-10-17 21:16:08 +00001359/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
1360/// This function emits a branch and is used at the leaves of an OR or an
1361/// AND operator tree.
1362///
1363void
Dan Gohman46510a72010-04-15 01:51:59 +00001364SelectionDAGBuilder::EmitBranchForMergedCondition(const Value *Cond,
Dan Gohman2048b852009-11-23 18:04:58 +00001365 MachineBasicBlock *TBB,
1366 MachineBasicBlock *FBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001367 MachineBasicBlock *CurBB,
1368 MachineBasicBlock *SwitchBB) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001369 const BasicBlock *BB = CurBB->getBasicBlock();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001370
Dan Gohmanc2277342008-10-17 21:16:08 +00001371 // If the leaf of the tree is a comparison, merge the condition into
1372 // the caseblock.
Dan Gohman46510a72010-04-15 01:51:59 +00001373 if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001374 // The operands of the cmp have to be in this block. We don't know
1375 // how to export them from some other block. If this is the first block
1376 // of the sequence, no exporting is needed.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001377 if (CurBB == SwitchBB ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001378 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
1379 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001380 ISD::CondCode Condition;
Dan Gohman46510a72010-04-15 01:51:59 +00001381 if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001382 Condition = getICmpCondCode(IC->getPredicate());
Dan Gohman46510a72010-04-15 01:51:59 +00001383 } else if (const FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001384 Condition = getFCmpCondCode(FC->getPredicate());
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001385 if (TM.Options.NoNaNsFPMath)
1386 Condition = getFCmpCodeWithoutNaN(Condition);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001387 } else {
1388 Condition = ISD::SETEQ; // silence warning.
Torok Edwinc23197a2009-07-14 16:55:14 +00001389 llvm_unreachable("Unknown compare instruction");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001390 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001391
1392 CaseBlock CB(Condition, BOp->getOperand(0),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001393 BOp->getOperand(1), NULL, TBB, FBB, CurBB);
1394 SwitchCases.push_back(CB);
1395 return;
1396 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001397 }
1398
1399 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001400 CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanc2277342008-10-17 21:16:08 +00001401 NULL, TBB, FBB, CurBB);
1402 SwitchCases.push_back(CB);
1403}
1404
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001405/// FindMergedConditions - If Cond is an expression like
Dan Gohman46510a72010-04-15 01:51:59 +00001406void SelectionDAGBuilder::FindMergedConditions(const Value *Cond,
Dan Gohman2048b852009-11-23 18:04:58 +00001407 MachineBasicBlock *TBB,
1408 MachineBasicBlock *FBB,
1409 MachineBasicBlock *CurBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001410 MachineBasicBlock *SwitchBB,
Dan Gohman2048b852009-11-23 18:04:58 +00001411 unsigned Opc) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001412 // If this node is not part of the or/and tree, emit it as a branch.
Dan Gohman46510a72010-04-15 01:51:59 +00001413 const Instruction *BOp = dyn_cast<Instruction>(Cond);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001414 if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001415 (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
1416 BOp->getParent() != CurBB->getBasicBlock() ||
1417 !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1418 !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001419 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001420 return;
1421 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001422
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001423 // Create TmpBB after CurBB.
1424 MachineFunction::iterator BBI = CurBB;
1425 MachineFunction &MF = DAG.getMachineFunction();
1426 MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
1427 CurBB->getParent()->insert(++BBI, TmpBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001428
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001429 if (Opc == Instruction::Or) {
1430 // Codegen X | Y as:
1431 // jmp_if_X TBB
1432 // jmp TmpBB
1433 // TmpBB:
1434 // jmp_if_Y TBB
1435 // jmp FBB
1436 //
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001437
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001438 // Emit the LHS condition.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001439 FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, SwitchBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001440
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001441 // Emit the RHS condition into TmpBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001442 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001443 } else {
1444 assert(Opc == Instruction::And && "Unknown merge op!");
1445 // Codegen X & Y as:
1446 // jmp_if_X TmpBB
1447 // jmp FBB
1448 // TmpBB:
1449 // jmp_if_Y TBB
1450 // jmp FBB
1451 //
1452 // This requires creation of TmpBB after CurBB.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001453
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001454 // Emit the LHS condition.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001455 FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, SwitchBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001456
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001457 // Emit the RHS condition into TmpBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001458 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001459 }
1460}
1461
1462/// If the set of cases should be emitted as a series of branches, return true.
1463/// If we should emit this as a bunch of and/or'd together conditions, return
1464/// false.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001465bool
Dan Gohman2048b852009-11-23 18:04:58 +00001466SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001467 if (Cases.size() != 2) return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001468
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001469 // If this is two comparisons of the same values or'd or and'd together, they
1470 // will get folded into a single comparison, so don't emit two blocks.
1471 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1472 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1473 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1474 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1475 return false;
1476 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001477
Chris Lattner133ce872010-01-02 00:00:03 +00001478 // Handle: (X != null) | (Y != null) --> (X|Y) != 0
1479 // Handle: (X == null) & (Y == null) --> (X|Y) == 0
1480 if (Cases[0].CmpRHS == Cases[1].CmpRHS &&
1481 Cases[0].CC == Cases[1].CC &&
1482 isa<Constant>(Cases[0].CmpRHS) &&
1483 cast<Constant>(Cases[0].CmpRHS)->isNullValue()) {
1484 if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB)
1485 return false;
1486 if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB)
1487 return false;
1488 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00001489
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001490 return true;
1491}
1492
Dan Gohman46510a72010-04-15 01:51:59 +00001493void SelectionDAGBuilder::visitBr(const BranchInst &I) {
Dan Gohman84023e02010-07-10 09:00:22 +00001494 MachineBasicBlock *BrMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001495
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001496 // Update machine-CFG edges.
1497 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1498
1499 // Figure out which block is immediately after the current one.
1500 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001501 MachineFunction::iterator BBI = BrMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001502 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001503 NextBlock = BBI;
1504
1505 if (I.isUnconditional()) {
1506 // Update machine-CFG edges.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001507 BrMBB->addSuccessor(Succ0MBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001508
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001509 // If this is not a fall-through branch, emit the branch.
Bill Wendling4533cac2010-01-28 21:51:40 +00001510 if (Succ0MBB != NextBlock)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001511 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001512 MVT::Other, getControlRoot(),
Bill Wendling4533cac2010-01-28 21:51:40 +00001513 DAG.getBasicBlock(Succ0MBB)));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001514
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001515 return;
1516 }
1517
1518 // If this condition is one of the special cases we handle, do special stuff
1519 // now.
Dan Gohman46510a72010-04-15 01:51:59 +00001520 const Value *CondVal = I.getCondition();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001521 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1522
1523 // If this is a series of conditions that are or'd or and'd together, emit
1524 // this as a sequence of branches instead of setcc's with and/or operations.
Chris Lattnerde189be2010-11-30 18:12:52 +00001525 // As long as jumps are not expensive, this should improve performance.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001526 // For example, instead of something like:
1527 // cmp A, B
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001528 // C = seteq
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001529 // cmp D, E
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001530 // F = setle
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001531 // or C, F
1532 // jnz foo
1533 // Emit:
1534 // cmp A, B
1535 // je foo
1536 // cmp D, E
1537 // jle foo
1538 //
Dan Gohman46510a72010-04-15 01:51:59 +00001539 if (const BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
Owen Anderson95771af2011-02-25 21:41:48 +00001540 if (!TLI.isJumpExpensive() &&
Chris Lattnerde189be2010-11-30 18:12:52 +00001541 BOp->hasOneUse() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001542 (BOp->getOpcode() == Instruction::And ||
1543 BOp->getOpcode() == Instruction::Or)) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001544 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, BrMBB, BrMBB,
1545 BOp->getOpcode());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001546 // If the compares in later blocks need to use values not currently
1547 // exported from this block, export them now. This block should always
1548 // be the first entry.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001549 assert(SwitchCases[0].ThisBB == BrMBB && "Unexpected lowering!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001550
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001551 // Allow some cases to be rejected.
1552 if (ShouldEmitAsBranches(SwitchCases)) {
1553 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1554 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1555 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1556 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001557
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001558 // Emit the branch for this block.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001559 visitSwitchCase(SwitchCases[0], BrMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001560 SwitchCases.erase(SwitchCases.begin());
1561 return;
1562 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001563
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001564 // Okay, we decided not to do this, remove any inserted MBB's and clear
1565 // SwitchCases.
1566 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001567 FuncInfo.MF->erase(SwitchCases[i].ThisBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001568
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001569 SwitchCases.clear();
1570 }
1571 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001572
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001573 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001574 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohman99be8ae2010-04-19 22:41:47 +00001575 NULL, Succ0MBB, Succ1MBB, BrMBB);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001576
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001577 // Use visitSwitchCase to actually insert the fast branch sequence for this
1578 // cond branch.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001579 visitSwitchCase(CB, BrMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001580}
1581
1582/// visitSwitchCase - Emits the necessary code to represent a single node in
1583/// the binary search tree resulting from lowering a switch instruction.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001584void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB,
1585 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001586 SDValue Cond;
1587 SDValue CondLHS = getValue(CB.CmpLHS);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001588 SDLoc dl = getCurSDLoc();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001589
1590 // Build the setcc now.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001591 if (CB.CmpMHS == NULL) {
1592 // Fold "(X == true)" to X and "(X == false)" to !X to
1593 // handle common cases produced by branch lowering.
Owen Anderson5defacc2009-07-31 17:39:07 +00001594 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001595 CB.CC == ISD::SETEQ)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001596 Cond = CondLHS;
Owen Anderson5defacc2009-07-31 17:39:07 +00001597 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001598 CB.CC == ISD::SETEQ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001599 SDValue True = DAG.getConstant(1, CondLHS.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001600 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001601 } else
Owen Anderson825b72b2009-08-11 20:47:22 +00001602 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001603 } else {
Stepan Dyatkovskiyc187df22012-05-17 08:56:30 +00001604 assert(CB.CC == ISD::SETCC_INVALID &&
1605 "Condition is undefined for to-the-range belonging check.");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001606
Anton Korobeynikov23218582008-12-23 22:25:27 +00001607 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
1608 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001609
1610 SDValue CmpOp = getValue(CB.CmpMHS);
Owen Andersone50ed302009-08-10 22:56:29 +00001611 EVT VT = CmpOp.getValueType();
Stepan Dyatkovskiyc187df22012-05-17 08:56:30 +00001612
1613 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(false)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001614 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT),
Stepan Dyatkovskiyc187df22012-05-17 08:56:30 +00001615 ISD::SETULE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001616 } else {
Dale Johannesenf5d97892009-02-04 01:48:28 +00001617 SDValue SUB = DAG.getNode(ISD::SUB, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001618 VT, CmpOp, DAG.getConstant(Low, VT));
Owen Anderson825b72b2009-08-11 20:47:22 +00001619 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001620 DAG.getConstant(High-Low, VT), ISD::SETULE);
1621 }
1622 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001623
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001624 // Update successor info
Jakub Staszakc8f34de2011-07-29 22:25:21 +00001625 addSuccessorWithWeight(SwitchBB, CB.TrueBB, CB.TrueWeight);
Jakob Stoklund Olesene7fdef42012-08-20 21:39:52 +00001626 // TrueBB and FalseBB are always different unless the incoming IR is
1627 // degenerate. This only happens when running llc on weird IR.
1628 if (CB.TrueBB != CB.FalseBB)
1629 addSuccessorWithWeight(SwitchBB, CB.FalseBB, CB.FalseWeight);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001630
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001631 // Set NextBlock to be the MBB immediately after the current one, if any.
1632 // This is used to avoid emitting unnecessary branches to the next block.
1633 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001634 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001635 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001636 NextBlock = BBI;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001637
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001638 // If the lhs block is the next block, invert the condition so that we can
1639 // fall through to the lhs instead of the rhs block.
1640 if (CB.TrueBB == NextBlock) {
1641 std::swap(CB.TrueBB, CB.FalseBB);
1642 SDValue True = DAG.getConstant(1, Cond.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001643 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001644 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001645
Dale Johannesenf5d97892009-02-04 01:48:28 +00001646 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001647 MVT::Other, getControlRoot(), Cond,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001648 DAG.getBasicBlock(CB.TrueBB));
Bill Wendling87710f02009-12-21 23:47:40 +00001649
Evan Cheng266a99d2010-09-23 06:51:55 +00001650 // Insert the false branch. Do this even if it's a fall through branch,
1651 // this makes it easier to do DAG optimizations which require inverting
1652 // the branch condition.
1653 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
1654 DAG.getBasicBlock(CB.FalseBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001655
1656 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001657}
1658
1659/// visitJumpTable - Emit JumpTable node in the current MBB
Dan Gohman2048b852009-11-23 18:04:58 +00001660void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001661 // Emit the code for the jump table
1662 assert(JT.Reg != -1U && "Should lower JT Header first!");
Owen Andersone50ed302009-08-10 22:56:29 +00001663 EVT PTy = TLI.getPointerTy();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001664 SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurSDLoc(),
Dale Johannesena04b7572009-02-03 23:04:43 +00001665 JT.Reg, PTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001666 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001667 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurSDLoc(),
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001668 MVT::Other, Index.getValue(1),
1669 Table, Index);
1670 DAG.setRoot(BrJumpTable);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001671}
1672
1673/// visitJumpTableHeader - This function emits necessary code to produce index
1674/// in the JumpTable from switch case.
Dan Gohman2048b852009-11-23 18:04:58 +00001675void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001676 JumpTableHeader &JTH,
1677 MachineBasicBlock *SwitchBB) {
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001678 // Subtract the lowest switch case value from the value being switched on and
1679 // conditional branch to default mbb if the result is greater than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001680 // difference between smallest and largest cases.
1681 SDValue SwitchOp = getValue(JTH.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001682 EVT VT = SwitchOp.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001683 SDValue Sub = DAG.getNode(ISD::SUB, getCurSDLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001684 DAG.getConstant(JTH.First, VT));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001685
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001686 // The SDNode we just created, which holds the value being switched on minus
Dan Gohmanf451cb82010-02-10 16:03:48 +00001687 // the smallest case value, needs to be copied to a virtual register so it
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001688 // can be used as an index into the jump table in a subsequent basic block.
1689 // This value may be smaller or larger than the target's pointer type, and
1690 // therefore require extension or truncating.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001691 SwitchOp = DAG.getZExtOrTrunc(Sub, getCurSDLoc(), TLI.getPointerTy());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001692
Dan Gohman89496d02010-07-02 00:10:16 +00001693 unsigned JumpTableReg = FuncInfo.CreateReg(TLI.getPointerTy());
Andrew Trickac6d9be2013-05-25 02:42:55 +00001694 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurSDLoc(),
Dale Johannesena04b7572009-02-03 23:04:43 +00001695 JumpTableReg, SwitchOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001696 JT.Reg = JumpTableReg;
1697
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001698 // Emit the range check for the jump table, and branch to the default block
1699 // for the switch statement if the value being switched on exceeds the largest
1700 // case in the switch.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001701 SDValue CMP = DAG.getSetCC(getCurSDLoc(),
Matt Arsenault225ed702013-05-18 00:21:46 +00001702 TLI.getSetCCResultType(*DAG.getContext(),
1703 Sub.getValueType()),
1704 Sub,
1705 DAG.getConstant(JTH.Last - JTH.First,VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001706 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001707
1708 // Set NextBlock to be the MBB immediately after the current one, if any.
1709 // This is used to avoid emitting unnecessary branches to the next block.
1710 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001711 MachineFunction::iterator BBI = SwitchBB;
Bill Wendling87710f02009-12-21 23:47:40 +00001712
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001713 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001714 NextBlock = BBI;
1715
Andrew Trickac6d9be2013-05-25 02:42:55 +00001716 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurSDLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001717 MVT::Other, CopyTo, CMP,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001718 DAG.getBasicBlock(JT.Default));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001719
Bill Wendling4533cac2010-01-28 21:51:40 +00001720 if (JT.MBB != NextBlock)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001721 BrCond = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, BrCond,
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001722 DAG.getBasicBlock(JT.MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001723
Bill Wendling87710f02009-12-21 23:47:40 +00001724 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001725}
1726
1727/// visitBitTestHeader - This function emits necessary code to produce value
1728/// suitable for "bit tests"
Dan Gohman99be8ae2010-04-19 22:41:47 +00001729void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B,
1730 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001731 // Subtract the minimum value
1732 SDValue SwitchOp = getValue(B.SValue);
Patrik Hagglund34525f92012-12-11 11:14:33 +00001733 EVT VT = SwitchOp.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001734 SDValue Sub = DAG.getNode(ISD::SUB, getCurSDLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001735 DAG.getConstant(B.First, VT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001736
1737 // Check range
Andrew Trickac6d9be2013-05-25 02:42:55 +00001738 SDValue RangeCmp = DAG.getSetCC(getCurSDLoc(),
Matt Arsenault225ed702013-05-18 00:21:46 +00001739 TLI.getSetCCResultType(*DAG.getContext(),
1740 Sub.getValueType()),
Bill Wendling87710f02009-12-21 23:47:40 +00001741 Sub, DAG.getConstant(B.Range, VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001742 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001743
Evan Chengd08e5b42011-01-06 01:02:44 +00001744 // Determine the type of the test operands.
1745 bool UsePtrType = false;
1746 if (!TLI.isTypeLegal(VT))
1747 UsePtrType = true;
1748 else {
1749 for (unsigned i = 0, e = B.Cases.size(); i != e; ++i)
Eli Friedman5c75af62011-10-12 22:46:45 +00001750 if (!isUIntN(VT.getSizeInBits(), B.Cases[i].Mask)) {
Evan Chengd08e5b42011-01-06 01:02:44 +00001751 // Switch table case range are encoded into series of masks.
1752 // Just use pointer type, it's guaranteed to fit.
1753 UsePtrType = true;
1754 break;
1755 }
1756 }
1757 if (UsePtrType) {
1758 VT = TLI.getPointerTy();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001759 Sub = DAG.getZExtOrTrunc(Sub, getCurSDLoc(), VT);
Evan Chengd08e5b42011-01-06 01:02:44 +00001760 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001761
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00001762 B.RegVT = VT.getSimpleVT();
Patrik Hagglund8963fec2012-12-19 12:23:01 +00001763 B.Reg = FuncInfo.CreateReg(B.RegVT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001764 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurSDLoc(),
Evan Chengd08e5b42011-01-06 01:02:44 +00001765 B.Reg, Sub);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001766
1767 // Set NextBlock to be the MBB immediately after the current one, if any.
1768 // This is used to avoid emitting unnecessary branches to the next block.
1769 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001770 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001771 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001772 NextBlock = BBI;
1773
1774 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1775
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001776 addSuccessorWithWeight(SwitchBB, B.Default);
1777 addSuccessorWithWeight(SwitchBB, MBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001778
Andrew Trickac6d9be2013-05-25 02:42:55 +00001779 SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurSDLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001780 MVT::Other, CopyTo, RangeCmp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001781 DAG.getBasicBlock(B.Default));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001782
Evan Cheng8c1f4322010-09-23 18:32:19 +00001783 if (MBB != NextBlock)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001784 BrRange = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, CopyTo,
Evan Cheng8c1f4322010-09-23 18:32:19 +00001785 DAG.getBasicBlock(MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001786
Bill Wendling87710f02009-12-21 23:47:40 +00001787 DAG.setRoot(BrRange);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001788}
1789
1790/// visitBitTestCase - this function produces one "bit test"
Evan Chengd08e5b42011-01-06 01:02:44 +00001791void SelectionDAGBuilder::visitBitTestCase(BitTestBlock &BB,
1792 MachineBasicBlock* NextMBB,
Manman Ren1a710fd2012-08-24 18:14:27 +00001793 uint32_t BranchWeightToNext,
Dan Gohman2048b852009-11-23 18:04:58 +00001794 unsigned Reg,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001795 BitTestCase &B,
1796 MachineBasicBlock *SwitchBB) {
Patrik Hagglund8963fec2012-12-19 12:23:01 +00001797 MVT VT = BB.RegVT;
Andrew Trickac6d9be2013-05-25 02:42:55 +00001798 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurSDLoc(),
Evan Chengd08e5b42011-01-06 01:02:44 +00001799 Reg, VT);
Dan Gohman8e0163a2010-06-24 02:06:24 +00001800 SDValue Cmp;
Benjamin Kramer3ff25512011-07-14 01:38:42 +00001801 unsigned PopCount = CountPopulation_64(B.Mask);
1802 if (PopCount == 1) {
Dan Gohman8e0163a2010-06-24 02:06:24 +00001803 // Testing for a single bit; just compare the shift count with what it
1804 // would need to be to shift a 1 bit in that position.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001805 Cmp = DAG.getSetCC(getCurSDLoc(),
Matt Arsenault225ed702013-05-18 00:21:46 +00001806 TLI.getSetCCResultType(*DAG.getContext(), VT),
Dan Gohman8e0163a2010-06-24 02:06:24 +00001807 ShiftOp,
Michael J. Spencerc6af2432013-05-24 22:23:49 +00001808 DAG.getConstant(countTrailingZeros(B.Mask), VT),
Dan Gohman8e0163a2010-06-24 02:06:24 +00001809 ISD::SETEQ);
Benjamin Kramer3ff25512011-07-14 01:38:42 +00001810 } else if (PopCount == BB.Range) {
1811 // There is only one zero bit in the range, test for it directly.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001812 Cmp = DAG.getSetCC(getCurSDLoc(),
Matt Arsenault225ed702013-05-18 00:21:46 +00001813 TLI.getSetCCResultType(*DAG.getContext(), VT),
Benjamin Kramer3ff25512011-07-14 01:38:42 +00001814 ShiftOp,
1815 DAG.getConstant(CountTrailingOnes_64(B.Mask), VT),
1816 ISD::SETNE);
Dan Gohman8e0163a2010-06-24 02:06:24 +00001817 } else {
1818 // Make desired shift
Andrew Trickac6d9be2013-05-25 02:42:55 +00001819 SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurSDLoc(), VT,
Evan Chengd08e5b42011-01-06 01:02:44 +00001820 DAG.getConstant(1, VT), ShiftOp);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001821
Dan Gohman8e0163a2010-06-24 02:06:24 +00001822 // Emit bit tests and jumps
Andrew Trickac6d9be2013-05-25 02:42:55 +00001823 SDValue AndOp = DAG.getNode(ISD::AND, getCurSDLoc(),
Evan Chengd08e5b42011-01-06 01:02:44 +00001824 VT, SwitchVal, DAG.getConstant(B.Mask, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00001825 Cmp = DAG.getSetCC(getCurSDLoc(),
Matt Arsenault225ed702013-05-18 00:21:46 +00001826 TLI.getSetCCResultType(*DAG.getContext(), VT),
Evan Chengd08e5b42011-01-06 01:02:44 +00001827 AndOp, DAG.getConstant(0, VT),
Dan Gohman8e0163a2010-06-24 02:06:24 +00001828 ISD::SETNE);
1829 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001830
Manman Ren1a710fd2012-08-24 18:14:27 +00001831 // The branch weight from SwitchBB to B.TargetBB is B.ExtraWeight.
1832 addSuccessorWithWeight(SwitchBB, B.TargetBB, B.ExtraWeight);
1833 // The branch weight from SwitchBB to NextMBB is BranchWeightToNext.
1834 addSuccessorWithWeight(SwitchBB, NextMBB, BranchWeightToNext);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001835
Andrew Trickac6d9be2013-05-25 02:42:55 +00001836 SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurSDLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001837 MVT::Other, getControlRoot(),
Dan Gohman8e0163a2010-06-24 02:06:24 +00001838 Cmp, DAG.getBasicBlock(B.TargetBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001839
1840 // Set NextBlock to be the MBB immediately after the current one, if any.
1841 // This is used to avoid emitting unnecessary branches to the next block.
1842 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001843 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001844 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001845 NextBlock = BBI;
1846
Evan Cheng8c1f4322010-09-23 18:32:19 +00001847 if (NextMBB != NextBlock)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001848 BrAnd = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, BrAnd,
Evan Cheng8c1f4322010-09-23 18:32:19 +00001849 DAG.getBasicBlock(NextMBB));
Bill Wendling0777e922009-12-21 21:59:52 +00001850
Bill Wendling87710f02009-12-21 23:47:40 +00001851 DAG.setRoot(BrAnd);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001852}
1853
Dan Gohman46510a72010-04-15 01:51:59 +00001854void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) {
Dan Gohman84023e02010-07-10 09:00:22 +00001855 MachineBasicBlock *InvokeMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001856
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001857 // Retrieve successors.
1858 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1859 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1860
Gabor Greifb67e6b32009-01-15 11:10:44 +00001861 const Value *Callee(I.getCalledValue());
Nuno Lopes85b40892012-06-28 22:30:12 +00001862 const Function *Fn = dyn_cast<Function>(Callee);
Gabor Greifb67e6b32009-01-15 11:10:44 +00001863 if (isa<InlineAsm>(Callee))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001864 visitInlineAsm(&I);
Nuno Lopes85b40892012-06-28 22:30:12 +00001865 else if (Fn && Fn->isIntrinsic()) {
1866 assert(Fn->getIntrinsicID() == Intrinsic::donothing);
Nuno Lopes4532bf62012-07-18 00:07:17 +00001867 // Ignore invokes to @llvm.donothing: jump directly to the next BB.
Nuno Lopes85b40892012-06-28 22:30:12 +00001868 } else
Gabor Greifb67e6b32009-01-15 11:10:44 +00001869 LowerCallTo(&I, getValue(Callee), false, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001870
1871 // If the value of the invoke is used outside of its defining block, make it
1872 // available as a virtual register.
Dan Gohmanad62f532009-04-23 23:13:24 +00001873 CopyToExportRegsIfNeeded(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001874
1875 // Update successor info
Chandler Carruthf2645682011-11-22 11:37:46 +00001876 addSuccessorWithWeight(InvokeMBB, Return);
1877 addSuccessorWithWeight(InvokeMBB, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001878
1879 // Drop into normal successor.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001880 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00001881 MVT::Other, getControlRoot(),
1882 DAG.getBasicBlock(Return)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001883}
1884
Bill Wendlingdccc03b2011-07-31 06:30:59 +00001885void SelectionDAGBuilder::visitResume(const ResumeInst &RI) {
1886 llvm_unreachable("SelectionDAGBuilder shouldn't visit resume instructions!");
1887}
1888
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00001889void SelectionDAGBuilder::visitLandingPad(const LandingPadInst &LP) {
1890 assert(FuncInfo.MBB->isLandingPad() &&
1891 "Call to landingpad not in landing pad!");
1892
1893 MachineBasicBlock *MBB = FuncInfo.MBB;
1894 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
1895 AddLandingPadInfo(LP, MMI, MBB);
1896
Bill Wendlingbdf9db62012-02-13 23:47:16 +00001897 // If there aren't registers to copy the values into (e.g., during SjLj
1898 // exceptions), then don't bother to create these DAG nodes.
Lang Hames07961342012-02-14 04:45:49 +00001899 if (TLI.getExceptionPointerRegister() == 0 &&
Bill Wendlingbdf9db62012-02-13 23:47:16 +00001900 TLI.getExceptionSelectorRegister() == 0)
1901 return;
1902
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00001903 SmallVector<EVT, 2> ValueVTs;
1904 ComputeValueVTs(TLI, LP.getType(), ValueVTs);
1905
1906 // Insert the EXCEPTIONADDR instruction.
1907 assert(FuncInfo.MBB->isLandingPad() &&
1908 "Call to eh.exception not in landing pad!");
1909 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
1910 SDValue Ops[2];
1911 Ops[0] = DAG.getRoot();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001912 SDValue Op1 = DAG.getNode(ISD::EXCEPTIONADDR, getCurSDLoc(), VTs, Ops, 1);
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00001913 SDValue Chain = Op1.getValue(1);
1914
1915 // Insert the EHSELECTION instruction.
1916 VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
1917 Ops[0] = Op1;
1918 Ops[1] = Chain;
Andrew Trickac6d9be2013-05-25 02:42:55 +00001919 SDValue Op2 = DAG.getNode(ISD::EHSELECTION, getCurSDLoc(), VTs, Ops, 2);
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00001920 Chain = Op2.getValue(1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001921 Op2 = DAG.getSExtOrTrunc(Op2, getCurSDLoc(), MVT::i32);
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00001922
1923 Ops[0] = Op1;
1924 Ops[1] = Op2;
Andrew Trickac6d9be2013-05-25 02:42:55 +00001925 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00001926 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
1927 &Ops[0], 2);
1928
1929 std::pair<SDValue, SDValue> RetPair = std::make_pair(Res, Chain);
1930 setValue(&LP, RetPair.first);
1931 DAG.setRoot(RetPair.second);
1932}
1933
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001934/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
1935/// small case ranges).
Dan Gohman2048b852009-11-23 18:04:58 +00001936bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR,
1937 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001938 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001939 MachineBasicBlock *Default,
1940 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001941 // Size is the number of Cases represented by this range.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001942 size_t Size = CR.Range.second - CR.Range.first;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001943 if (Size > 3)
Anton Korobeynikov23218582008-12-23 22:25:27 +00001944 return false;
1945
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001946 // Get the MachineFunction which holds the current MBB. This is used when
1947 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001948 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001949
1950 // Figure out which block is immediately after the current one.
1951 MachineBasicBlock *NextBlock = 0;
1952 MachineFunction::iterator BBI = CR.CaseBB;
1953
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001954 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001955 NextBlock = BBI;
1956
Manman Ren1a710fd2012-08-24 18:14:27 +00001957 BranchProbabilityInfo *BPI = FuncInfo.BPI;
Benjamin Kramerce750f02010-11-22 09:45:38 +00001958 // If any two of the cases has the same destination, and if one value
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001959 // is the same as the other, but has one bit unset that the other has set,
1960 // use bit manipulation to do two compares at once. For example:
1961 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
Benjamin Kramerce750f02010-11-22 09:45:38 +00001962 // TODO: This could be extended to merge any 2 cases in switches with 3 cases.
1963 // TODO: Handle cases where CR.CaseBB != SwitchBB.
1964 if (Size == 2 && CR.CaseBB == SwitchBB) {
1965 Case &Small = *CR.Range.first;
1966 Case &Big = *(CR.Range.second-1);
1967
1968 if (Small.Low == Small.High && Big.Low == Big.High && Small.BB == Big.BB) {
1969 const APInt& SmallValue = cast<ConstantInt>(Small.Low)->getValue();
1970 const APInt& BigValue = cast<ConstantInt>(Big.Low)->getValue();
1971
1972 // Check that there is only one bit different.
1973 if (BigValue.countPopulation() == SmallValue.countPopulation() + 1 &&
1974 (SmallValue | BigValue) == BigValue) {
1975 // Isolate the common bit.
1976 APInt CommonBit = BigValue & ~SmallValue;
1977 assert((SmallValue | CommonBit) == BigValue &&
1978 CommonBit.countPopulation() == 1 && "Not a common bit?");
1979
1980 SDValue CondLHS = getValue(SV);
1981 EVT VT = CondLHS.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001982 SDLoc DL = getCurSDLoc();
Benjamin Kramerce750f02010-11-22 09:45:38 +00001983
1984 SDValue Or = DAG.getNode(ISD::OR, DL, VT, CondLHS,
1985 DAG.getConstant(CommonBit, VT));
1986 SDValue Cond = DAG.getSetCC(DL, MVT::i1,
1987 Or, DAG.getConstant(BigValue, VT),
1988 ISD::SETEQ);
1989
1990 // Update successor info.
Manman Ren1a710fd2012-08-24 18:14:27 +00001991 // Both Small and Big will jump to Small.BB, so we sum up the weights.
1992 addSuccessorWithWeight(SwitchBB, Small.BB,
1993 Small.ExtraWeight + Big.ExtraWeight);
1994 addSuccessorWithWeight(SwitchBB, Default,
1995 // The default destination is the first successor in IR.
1996 BPI ? BPI->getEdgeWeight(SwitchBB->getBasicBlock(), (unsigned)0) : 0);
Benjamin Kramerce750f02010-11-22 09:45:38 +00001997
1998 // Insert the true branch.
1999 SDValue BrCond = DAG.getNode(ISD::BRCOND, DL, MVT::Other,
2000 getControlRoot(), Cond,
2001 DAG.getBasicBlock(Small.BB));
2002
2003 // Insert the false branch.
2004 BrCond = DAG.getNode(ISD::BR, DL, MVT::Other, BrCond,
2005 DAG.getBasicBlock(Default));
2006
2007 DAG.setRoot(BrCond);
2008 return true;
2009 }
2010 }
2011 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002012
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002013 // Order cases by weight so the most likely case will be checked first.
Manman Ren1a710fd2012-08-24 18:14:27 +00002014 uint32_t UnhandledWeights = 0;
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002015 if (BPI) {
2016 for (CaseItr I = CR.Range.first, IE = CR.Range.second; I != IE; ++I) {
Manman Ren1a710fd2012-08-24 18:14:27 +00002017 uint32_t IWeight = I->ExtraWeight;
2018 UnhandledWeights += IWeight;
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002019 for (CaseItr J = CR.Range.first; J < I; ++J) {
Manman Ren1a710fd2012-08-24 18:14:27 +00002020 uint32_t JWeight = J->ExtraWeight;
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002021 if (IWeight > JWeight)
2022 std::swap(*I, *J);
2023 }
2024 }
2025 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002026 // Rearrange the case blocks so that the last one falls through if possible.
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002027 Case &BackCase = *(CR.Range.second-1);
Benjamin Kramer5db954d2012-05-26 21:19:12 +00002028 if (Size > 1 &&
2029 NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002030 // The last case block won't fall through into 'NextBlock' if we emit the
2031 // branches in this order. See if rearranging a case value would help.
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002032 // We start at the bottom as it's the case with the least weight.
Benjamin Kramercf1d69d2012-05-27 10:56:55 +00002033 for (Case *I = &*(CR.Range.second-2), *E = &*CR.Range.first-1; I != E; --I){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002034 if (I->BB == NextBlock) {
2035 std::swap(*I, BackCase);
2036 break;
2037 }
2038 }
2039 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002040
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002041 // Create a CaseBlock record representing a conditional branch to
2042 // the Case's target mbb if the value being switched on SV is equal
2043 // to C.
2044 MachineBasicBlock *CurBlock = CR.CaseBB;
2045 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
2046 MachineBasicBlock *FallThrough;
2047 if (I != E-1) {
2048 FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
2049 CurMF->insert(BBI, FallThrough);
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002050
2051 // Put SV in a virtual register to make it available from the new blocks.
2052 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002053 } else {
2054 // If the last case doesn't match, go to the default block.
2055 FallThrough = Default;
2056 }
2057
Dan Gohman46510a72010-04-15 01:51:59 +00002058 const Value *RHS, *LHS, *MHS;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002059 ISD::CondCode CC;
2060 if (I->High == I->Low) {
2061 // This is just small small case range :) containing exactly 1 case
2062 CC = ISD::SETEQ;
2063 LHS = SV; RHS = I->High; MHS = NULL;
2064 } else {
Stepan Dyatkovskiyc187df22012-05-17 08:56:30 +00002065 CC = ISD::SETCC_INVALID;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002066 LHS = I->Low; MHS = SV; RHS = I->High;
2067 }
Jakub Staszakc8f34de2011-07-29 22:25:21 +00002068
Manman Ren1a710fd2012-08-24 18:14:27 +00002069 // The false weight should be sum of all un-handled cases.
2070 UnhandledWeights -= I->ExtraWeight;
Jakub Staszakc8f34de2011-07-29 22:25:21 +00002071 CaseBlock CB(CC, LHS, RHS, MHS, /* truebb */ I->BB, /* falsebb */ FallThrough,
2072 /* me */ CurBlock,
Manman Ren1a710fd2012-08-24 18:14:27 +00002073 /* trueweight */ I->ExtraWeight,
2074 /* falseweight */ UnhandledWeights);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002075
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002076 // If emitting the first comparison, just call visitSwitchCase to emit the
2077 // code into the current block. Otherwise, push the CaseBlock onto the
2078 // vector to be later processed by SDISel, and insert the node's MBB
2079 // before the next MBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002080 if (CurBlock == SwitchBB)
2081 visitSwitchCase(CB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002082 else
2083 SwitchCases.push_back(CB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002084
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002085 CurBlock = FallThrough;
2086 }
2087
2088 return true;
2089}
2090
2091static inline bool areJTsAllowed(const TargetLowering &TLI) {
Evan Cheng769951f2012-07-02 22:39:56 +00002092 return TLI.supportJumpTables() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00002093 (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
2094 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002095}
Anton Korobeynikov23218582008-12-23 22:25:27 +00002096
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002097static APInt ComputeRange(const APInt &First, const APInt &Last) {
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002098 uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1;
Stepan Dyatkovskiyc187df22012-05-17 08:56:30 +00002099 APInt LastExt = Last.zext(BitWidth), FirstExt = First.zext(BitWidth);
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002100 return (LastExt - FirstExt + 1ULL);
2101}
2102
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002103/// handleJTSwitchCase - Emit jumptable for current switch case range
Chris Lattnerc3ab3882011-09-09 22:06:59 +00002104bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec &CR,
2105 CaseRecVector &WorkList,
2106 const Value *SV,
2107 MachineBasicBlock *Default,
Dan Gohman99be8ae2010-04-19 22:41:47 +00002108 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002109 Case& FrontCase = *CR.Range.first;
2110 Case& BackCase = *(CR.Range.second-1);
2111
Chris Lattnere880efe2009-11-07 07:50:34 +00002112 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
2113 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002114
Chris Lattnere880efe2009-11-07 07:50:34 +00002115 APInt TSize(First.getBitWidth(), 0);
Chris Lattnerc3ab3882011-09-09 22:06:59 +00002116 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002117 TSize += I->size();
2118
Sebastian Pop1a37d7e2012-09-25 20:35:36 +00002119 if (!areJTsAllowed(TLI) || TSize.ult(TLI.getMinimumJumpTableEntries()))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002120 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002121
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002122 APInt Range = ComputeRange(First, Last);
Jakob Stoklund Olesen79443912011-10-26 01:47:48 +00002123 // The density is TSize / Range. Require at least 40%.
2124 // It should not be possible for IntTSize to saturate for sane code, but make
2125 // sure we handle Range saturation correctly.
2126 uint64_t IntRange = Range.getLimitedValue(UINT64_MAX/10);
2127 uint64_t IntTSize = TSize.getLimitedValue(UINT64_MAX/10);
2128 if (IntTSize * 10 < IntRange * 4)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002129 return false;
2130
David Greene4b69d992010-01-05 01:24:57 +00002131 DEBUG(dbgs() << "Lowering jump table\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002132 << "First entry: " << First << ". Last entry: " << Last << '\n'
Jakob Stoklund Olesen79443912011-10-26 01:47:48 +00002133 << "Range: " << Range << ". Size: " << TSize << ".\n\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002134
2135 // Get the MachineFunction which holds the current MBB. This is used when
2136 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002137 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002138
2139 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002140 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00002141 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002142
2143 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
2144
2145 // Create a new basic block to hold the code for loading the address
2146 // of the jump table, and jumping to it. Update successor information;
2147 // we will either branch to the default case for the switch, or the jump
2148 // table.
2149 MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2150 CurMF->insert(BBI, JumpTableBB);
Jakub Staszak7cc2b072011-06-16 20:22:37 +00002151
2152 addSuccessorWithWeight(CR.CaseBB, Default);
2153 addSuccessorWithWeight(CR.CaseBB, JumpTableBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002154
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002155 // Build a vector of destination BBs, corresponding to each target
2156 // of the jump table. If the value of the jump table slot corresponds to
2157 // a case statement, push the case's BB onto the vector, otherwise, push
2158 // the default BB.
2159 std::vector<MachineBasicBlock*> DestBBs;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002160 APInt TEI = First;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002161 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
Chris Lattner071c62f2010-01-25 23:26:13 +00002162 const APInt &Low = cast<ConstantInt>(I->Low)->getValue();
2163 const APInt &High = cast<ConstantInt>(I->High)->getValue();
Anton Korobeynikov23218582008-12-23 22:25:27 +00002164
Stepan Dyatkovskiyc187df22012-05-17 08:56:30 +00002165 if (Low.ule(TEI) && TEI.ule(High)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002166 DestBBs.push_back(I->BB);
2167 if (TEI==High)
2168 ++I;
2169 } else {
2170 DestBBs.push_back(Default);
2171 }
2172 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002173
Manman Ren1a710fd2012-08-24 18:14:27 +00002174 // Calculate weight for each unique destination in CR.
2175 DenseMap<MachineBasicBlock*, uint32_t> DestWeights;
2176 if (FuncInfo.BPI)
2177 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
2178 DenseMap<MachineBasicBlock*, uint32_t>::iterator Itr =
2179 DestWeights.find(I->BB);
2180 if (Itr != DestWeights.end())
2181 Itr->second += I->ExtraWeight;
2182 else
2183 DestWeights[I->BB] = I->ExtraWeight;
2184 }
2185
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002186 // Update successor info. Add one edge to each unique successor.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002187 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
2188 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002189 E = DestBBs.end(); I != E; ++I) {
2190 if (!SuccsHandled[(*I)->getNumber()]) {
2191 SuccsHandled[(*I)->getNumber()] = true;
Manman Ren1a710fd2012-08-24 18:14:27 +00002192 DenseMap<MachineBasicBlock*, uint32_t>::iterator Itr =
2193 DestWeights.find(*I);
2194 addSuccessorWithWeight(JumpTableBB, *I,
2195 Itr != DestWeights.end() ? Itr->second : 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002196 }
2197 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002198
Bob Wilsond1ec31d2010-03-18 18:42:41 +00002199 // Create a jump table index for this jump table.
Chris Lattner071c62f2010-01-25 23:26:13 +00002200 unsigned JTEncoding = TLI.getJumpTableEncoding();
2201 unsigned JTI = CurMF->getOrCreateJumpTableInfo(JTEncoding)
Bob Wilsond1ec31d2010-03-18 18:42:41 +00002202 ->createJumpTableIndex(DestBBs);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002203
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002204 // Set the jump table information so that we can codegen it as a second
2205 // MachineBasicBlock
2206 JumpTable JT(-1U, JTI, JumpTableBB, Default);
Dan Gohman99be8ae2010-04-19 22:41:47 +00002207 JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == SwitchBB));
2208 if (CR.CaseBB == SwitchBB)
2209 visitJumpTableHeader(JT, JTH, SwitchBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002210
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002211 JTCases.push_back(JumpTableBlock(JTH, JT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002212 return true;
2213}
2214
2215/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
2216/// 2 subtrees.
Dan Gohman2048b852009-11-23 18:04:58 +00002217bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR,
2218 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00002219 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00002220 MachineBasicBlock *Default,
2221 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002222 // Get the MachineFunction which holds the current MBB. This is used when
2223 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002224 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002225
2226 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002227 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00002228 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002229
2230 Case& FrontCase = *CR.Range.first;
2231 Case& BackCase = *(CR.Range.second-1);
2232 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
2233
2234 // Size is the number of Cases represented by this range.
2235 unsigned Size = CR.Range.second - CR.Range.first;
2236
Chris Lattnere880efe2009-11-07 07:50:34 +00002237 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
2238 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002239 double FMetric = 0;
2240 CaseItr Pivot = CR.Range.first + Size/2;
2241
2242 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
2243 // (heuristically) allow us to emit JumpTable's later.
Chris Lattnere880efe2009-11-07 07:50:34 +00002244 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002245 for (CaseItr I = CR.Range.first, E = CR.Range.second;
2246 I!=E; ++I)
2247 TSize += I->size();
2248
Chris Lattnere880efe2009-11-07 07:50:34 +00002249 APInt LSize = FrontCase.size();
2250 APInt RSize = TSize-LSize;
David Greene4b69d992010-01-05 01:24:57 +00002251 DEBUG(dbgs() << "Selecting best pivot: \n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002252 << "First: " << First << ", Last: " << Last <<'\n'
2253 << "LSize: " << LSize << ", RSize: " << RSize << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002254 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
2255 J!=E; ++I, ++J) {
Chris Lattnere880efe2009-11-07 07:50:34 +00002256 const APInt &LEnd = cast<ConstantInt>(I->High)->getValue();
2257 const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002258 APInt Range = ComputeRange(LEnd, RBegin);
Stepan Dyatkovskiyc2c52a62012-05-15 06:50:18 +00002259 assert((Range - 2ULL).isNonNegative() &&
2260 "Invalid case distance");
Chris Lattnerc3e4e592011-04-09 06:57:13 +00002261 // Use volatile double here to avoid excess precision issues on some hosts,
2262 // e.g. that use 80-bit X87 registers.
2263 volatile double LDensity =
2264 (double)LSize.roundToDouble() /
Chris Lattnere880efe2009-11-07 07:50:34 +00002265 (LEnd - First + 1ULL).roundToDouble();
Chris Lattnerc3e4e592011-04-09 06:57:13 +00002266 volatile double RDensity =
2267 (double)RSize.roundToDouble() /
Chris Lattnere880efe2009-11-07 07:50:34 +00002268 (Last - RBegin + 1ULL).roundToDouble();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002269 double Metric = Range.logBase2()*(LDensity+RDensity);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002270 // Should always split in some non-trivial place
David Greene4b69d992010-01-05 01:24:57 +00002271 DEBUG(dbgs() <<"=>Step\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002272 << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n'
2273 << "LDensity: " << LDensity
2274 << ", RDensity: " << RDensity << '\n'
2275 << "Metric: " << Metric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002276 if (FMetric < Metric) {
2277 Pivot = J;
2278 FMetric = Metric;
David Greene4b69d992010-01-05 01:24:57 +00002279 DEBUG(dbgs() << "Current metric set to: " << FMetric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002280 }
2281
2282 LSize += J->size();
2283 RSize -= J->size();
2284 }
2285 if (areJTsAllowed(TLI)) {
2286 // If our case is dense we *really* should handle it earlier!
2287 assert((FMetric > 0) && "Should handle dense range earlier!");
2288 } else {
2289 Pivot = CR.Range.first + Size/2;
2290 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002291
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002292 CaseRange LHSR(CR.Range.first, Pivot);
2293 CaseRange RHSR(Pivot, CR.Range.second);
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00002294 const Constant *C = Pivot->Low;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002295 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002296
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002297 // We know that we branch to the LHS if the Value being switched on is
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002298 // less than the Pivot value, C. We use this to optimize our binary
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002299 // tree a bit, by recognizing that if SV is greater than or equal to the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002300 // LHS's Case Value, and that Case Value is exactly one less than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002301 // Pivot's Value, then we can branch directly to the LHS's Target,
2302 // rather than creating a leaf node for it.
2303 if ((LHSR.second - LHSR.first) == 1 &&
2304 LHSR.first->High == CR.GE &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00002305 cast<ConstantInt>(C)->getValue() ==
2306 (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002307 TrueBB = LHSR.first->BB;
2308 } else {
2309 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2310 CurMF->insert(BBI, TrueBB);
2311 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002312
2313 // Put SV in a virtual register to make it available from the new blocks.
2314 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002315 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002316
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002317 // Similar to the optimization above, if the Value being switched on is
2318 // known to be less than the Constant CR.LT, and the current Case Value
2319 // is CR.LT - 1, then we can branch directly to the target block for
2320 // the current Case Value, rather than emitting a RHS leaf node for it.
2321 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00002322 cast<ConstantInt>(RHSR.first->Low)->getValue() ==
2323 (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002324 FalseBB = RHSR.first->BB;
2325 } else {
2326 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2327 CurMF->insert(BBI, FalseBB);
2328 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002329
2330 // Put SV in a virtual register to make it available from the new blocks.
2331 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002332 }
2333
2334 // Create a CaseBlock record representing a conditional branch to
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002335 // the LHS node if the value being switched on SV is less than C.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002336 // Otherwise, branch to LHS.
Stepan Dyatkovskiyc187df22012-05-17 08:56:30 +00002337 CaseBlock CB(ISD::SETULT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002338
Dan Gohman99be8ae2010-04-19 22:41:47 +00002339 if (CR.CaseBB == SwitchBB)
2340 visitSwitchCase(CB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002341 else
2342 SwitchCases.push_back(CB);
2343
2344 return true;
2345}
2346
2347/// handleBitTestsSwitchCase - if current case range has few destination and
2348/// range span less, than machine word bitwidth, encode case range into series
2349/// of masks and emit bit tests with these masks.
Dan Gohman2048b852009-11-23 18:04:58 +00002350bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR,
2351 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00002352 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00002353 MachineBasicBlock* Default,
2354 MachineBasicBlock *SwitchBB){
Owen Andersone50ed302009-08-10 22:56:29 +00002355 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002356 unsigned IntPtrBits = PTy.getSizeInBits();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002357
2358 Case& FrontCase = *CR.Range.first;
2359 Case& BackCase = *(CR.Range.second-1);
2360
2361 // Get the MachineFunction which holds the current MBB. This is used when
2362 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002363 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002364
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00002365 // If target does not have legal shift left, do not emit bit tests at all.
2366 if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy()))
2367 return false;
2368
Anton Korobeynikov23218582008-12-23 22:25:27 +00002369 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002370 for (CaseItr I = CR.Range.first, E = CR.Range.second;
2371 I!=E; ++I) {
2372 // Single case counts one, case range - two.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002373 numCmps += (I->Low == I->High ? 1 : 2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002374 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002375
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002376 // Count unique destinations
2377 SmallSet<MachineBasicBlock*, 4> Dests;
2378 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
2379 Dests.insert(I->BB);
2380 if (Dests.size() > 3)
2381 // Don't bother the code below, if there are too much unique destinations
2382 return false;
2383 }
David Greene4b69d992010-01-05 01:24:57 +00002384 DEBUG(dbgs() << "Total number of unique destinations: "
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002385 << Dests.size() << '\n'
2386 << "Total number of comparisons: " << numCmps << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00002387
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002388 // Compute span of values.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002389 const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue();
2390 const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002391 APInt cmpRange = maxValue - minValue;
2392
David Greene4b69d992010-01-05 01:24:57 +00002393 DEBUG(dbgs() << "Compare range: " << cmpRange << '\n'
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002394 << "Low bound: " << minValue << '\n'
2395 << "High bound: " << maxValue << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00002396
Dan Gohmane0567812010-04-08 23:03:40 +00002397 if (cmpRange.uge(IntPtrBits) ||
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002398 (!(Dests.size() == 1 && numCmps >= 3) &&
2399 !(Dests.size() == 2 && numCmps >= 5) &&
2400 !(Dests.size() >= 3 && numCmps >= 6)))
2401 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002402
David Greene4b69d992010-01-05 01:24:57 +00002403 DEBUG(dbgs() << "Emitting bit tests\n");
Anton Korobeynikov23218582008-12-23 22:25:27 +00002404 APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth());
2405
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002406 // Optimize the case where all the case values fit in a
2407 // word without having to subtract minValue. In this case,
2408 // we can optimize away the subtraction.
Stepan Dyatkovskiyc187df22012-05-17 08:56:30 +00002409 if (maxValue.ult(IntPtrBits)) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002410 cmpRange = maxValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002411 } else {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002412 lowBound = minValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002413 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002414
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002415 CaseBitsVector CasesBits;
2416 unsigned i, count = 0;
2417
2418 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
2419 MachineBasicBlock* Dest = I->BB;
2420 for (i = 0; i < count; ++i)
2421 if (Dest == CasesBits[i].BB)
2422 break;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002423
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002424 if (i == count) {
2425 assert((count < 3) && "Too much destinations to test!");
Manman Ren1a710fd2012-08-24 18:14:27 +00002426 CasesBits.push_back(CaseBits(0, Dest, 0, 0/*Weight*/));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002427 count++;
2428 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002429
2430 const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue();
2431 const APInt& highValue = cast<ConstantInt>(I->High)->getValue();
2432
2433 uint64_t lo = (lowValue - lowBound).getZExtValue();
2434 uint64_t hi = (highValue - lowBound).getZExtValue();
Manman Ren1a710fd2012-08-24 18:14:27 +00002435 CasesBits[i].ExtraWeight += I->ExtraWeight;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002436
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002437 for (uint64_t j = lo; j <= hi; j++) {
2438 CasesBits[i].Mask |= 1ULL << j;
2439 CasesBits[i].Bits++;
2440 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002441
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002442 }
2443 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
Anton Korobeynikov23218582008-12-23 22:25:27 +00002444
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002445 BitTestInfo BTC;
2446
2447 // Figure out which block is immediately after the current one.
2448 MachineFunction::iterator BBI = CR.CaseBB;
2449 ++BBI;
2450
2451 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
2452
David Greene4b69d992010-01-05 01:24:57 +00002453 DEBUG(dbgs() << "Cases:\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002454 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
David Greene4b69d992010-01-05 01:24:57 +00002455 DEBUG(dbgs() << "Mask: " << CasesBits[i].Mask
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002456 << ", Bits: " << CasesBits[i].Bits
2457 << ", BB: " << CasesBits[i].BB << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002458
2459 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2460 CurMF->insert(BBI, CaseBB);
2461 BTC.push_back(BitTestCase(CasesBits[i].Mask,
2462 CaseBB,
Manman Ren1a710fd2012-08-24 18:14:27 +00002463 CasesBits[i].BB, CasesBits[i].ExtraWeight));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002464
2465 // Put SV in a virtual register to make it available from the new blocks.
2466 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002467 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002468
2469 BitTestBlock BTB(lowBound, cmpRange, SV,
Evan Chengd08e5b42011-01-06 01:02:44 +00002470 -1U, MVT::Other, (CR.CaseBB == SwitchBB),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002471 CR.CaseBB, Default, BTC);
2472
Dan Gohman99be8ae2010-04-19 22:41:47 +00002473 if (CR.CaseBB == SwitchBB)
2474 visitBitTestHeader(BTB, SwitchBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002475
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002476 BitTestCases.push_back(BTB);
2477
2478 return true;
2479}
2480
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002481/// Clusterify - Transform simple list of Cases into list of CaseRange's
Dan Gohman2048b852009-11-23 18:04:58 +00002482size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases,
2483 const SwitchInst& SI) {
Stepan Dyatkovskiy05cfe2e2012-05-18 08:32:28 +00002484
2485 /// Use a shorter form of declaration, and also
2486 /// show the we want to use CRSBuilder as Clusterifier.
Stepan Dyatkovskiy4319a552012-06-02 07:26:00 +00002487 typedef IntegersSubsetMapping<MachineBasicBlock> Clusterifier;
Stepan Dyatkovskiy05cfe2e2012-05-18 08:32:28 +00002488
2489 Clusterifier TheClusterifier;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002490
Manman Ren1a710fd2012-08-24 18:14:27 +00002491 BranchProbabilityInfo *BPI = FuncInfo.BPI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002492 // Start with "simple" cases
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002493 for (SwitchInst::ConstCaseIt i = SI.case_begin(), e = SI.case_end();
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002494 i != e; ++i) {
2495 const BasicBlock *SuccBB = i.getCaseSuccessor();
Jakub Staszakc8f34de2011-07-29 22:25:21 +00002496 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SuccBB];
2497
Manman Ren1a710fd2012-08-24 18:14:27 +00002498 TheClusterifier.add(i.getCaseValueEx(), SMBB,
2499 BPI ? BPI->getEdgeWeight(SI.getParent(), i.getSuccessorIndex()) : 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002500 }
Stepan Dyatkovskiy05cfe2e2012-05-18 08:32:28 +00002501
2502 TheClusterifier.optimize();
2503
Stepan Dyatkovskiy05cfe2e2012-05-18 08:32:28 +00002504 size_t numCmps = 0;
2505 for (Clusterifier::RangeIterator i = TheClusterifier.begin(),
2506 e = TheClusterifier.end(); i != e; ++i, ++numCmps) {
Stepan Dyatkovskiy66d79ce2012-07-04 05:53:05 +00002507 Clusterifier::Cluster &C = *i;
Manman Ren1a710fd2012-08-24 18:14:27 +00002508 // Update edge weight for the cluster.
2509 unsigned W = C.first.Weight;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002510
Stepan Dyatkovskiy484fc932012-05-28 12:39:09 +00002511 // FIXME: Currently work with ConstantInt based numbers.
2512 // Changing it to APInt based is a pretty heavy for this commit.
Stepan Dyatkovskiy66d79ce2012-07-04 05:53:05 +00002513 Cases.push_back(Case(C.first.getLow().toConstantInt(),
2514 C.first.getHigh().toConstantInt(), C.second, W));
Stepan Dyatkovskiy05cfe2e2012-05-18 08:32:28 +00002515
Stepan Dyatkovskiy66d79ce2012-07-04 05:53:05 +00002516 if (C.first.getLow() != C.first.getHigh())
Stepan Dyatkovskiy05cfe2e2012-05-18 08:32:28 +00002517 // A range counts double, since it requires two compares.
2518 ++numCmps;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002519 }
2520
2521 return numCmps;
2522}
2523
Jakob Stoklund Olesen2622f462010-09-30 19:44:31 +00002524void SelectionDAGBuilder::UpdateSplitBlock(MachineBasicBlock *First,
2525 MachineBasicBlock *Last) {
2526 // Update JTCases.
2527 for (unsigned i = 0, e = JTCases.size(); i != e; ++i)
2528 if (JTCases[i].first.HeaderBB == First)
2529 JTCases[i].first.HeaderBB = Last;
2530
2531 // Update BitTestCases.
2532 for (unsigned i = 0, e = BitTestCases.size(); i != e; ++i)
2533 if (BitTestCases[i].Parent == First)
2534 BitTestCases[i].Parent = Last;
2535}
2536
Dan Gohman46510a72010-04-15 01:51:59 +00002537void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) {
Dan Gohman84023e02010-07-10 09:00:22 +00002538 MachineBasicBlock *SwitchMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00002539
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002540 // Figure out which block is immediately after the current one.
2541 MachineBasicBlock *NextBlock = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002542 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
2543
2544 // If there is only the default destination, branch to it if it is not the
2545 // next basic block. Otherwise, just fall through.
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00002546 if (!SI.getNumCases()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002547 // Update machine-CFG edges.
2548
2549 // If this is not a fall-through branch, emit the branch.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002550 SwitchMBB->addSuccessor(Default);
Bill Wendling4533cac2010-01-28 21:51:40 +00002551 if (Default != NextBlock)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002552 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002553 MVT::Other, getControlRoot(),
2554 DAG.getBasicBlock(Default)));
Bill Wendling49fcff82009-12-21 22:30:11 +00002555
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002556 return;
2557 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002558
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002559 // If there are any non-default case statements, create a vector of Cases
2560 // representing each one, and sort the vector so that we can efficiently
2561 // create a binary search tree from them.
2562 CaseVector Cases;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002563 size_t numCmps = Clusterify(Cases, SI);
David Greene4b69d992010-01-05 01:24:57 +00002564 DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size()
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002565 << ". Total compares: " << numCmps << '\n');
Duncan Sands17001ce2011-10-18 12:44:00 +00002566 (void)numCmps;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002567
2568 // Get the Value to be switched on and default basic blocks, which will be
2569 // inserted into CaseBlock records, representing basic blocks in the binary
2570 // search tree.
Eli Friedmanbb5a7442011-09-29 20:21:17 +00002571 const Value *SV = SI.getCondition();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002572
2573 // Push the initial CaseRec onto the worklist
2574 CaseRecVector WorkList;
Dan Gohman99be8ae2010-04-19 22:41:47 +00002575 WorkList.push_back(CaseRec(SwitchMBB,0,0,
2576 CaseRange(Cases.begin(),Cases.end())));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002577
2578 while (!WorkList.empty()) {
2579 // Grab a record representing a case range to process off the worklist
2580 CaseRec CR = WorkList.back();
2581 WorkList.pop_back();
2582
Dan Gohman99be8ae2010-04-19 22:41:47 +00002583 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002584 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002585
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002586 // If the range has few cases (two or less) emit a series of specific
2587 // tests.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002588 if (handleSmallSwitchRange(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002589 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002590
Sebastian Pop1a37d7e2012-09-25 20:35:36 +00002591 // If the switch has more than N blocks, and is at least 40% dense, and the
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002592 // target supports indirect branches, then emit a jump table rather than
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002593 // lowering the switch to a binary tree of conditional branches.
Sebastian Pop1a37d7e2012-09-25 20:35:36 +00002594 // N defaults to 4 and is controlled via TLS.getMinimumJumpTableEntries().
Dan Gohman99be8ae2010-04-19 22:41:47 +00002595 if (handleJTSwitchCase(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002596 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002597
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002598 // Emit binary tree. We need to pick a pivot, and push left and right ranges
2599 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002600 handleBTSplitSwitchCase(CR, WorkList, SV, Default, SwitchMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002601 }
2602}
2603
Dan Gohman46510a72010-04-15 01:51:59 +00002604void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) {
Dan Gohman84023e02010-07-10 09:00:22 +00002605 MachineBasicBlock *IndirectBrMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00002606
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002607 // Update machine-CFG edges with unique successors.
Nadav Rotemee0ce152012-10-23 21:05:33 +00002608 SmallSet<BasicBlock*, 32> Done;
2609 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i) {
2610 BasicBlock *BB = I.getSuccessor(i);
2611 bool Inserted = Done.insert(BB);
2612 if (!Inserted)
2613 continue;
2614
2615 MachineBasicBlock *Succ = FuncInfo.MBBMap[BB];
Jakub Staszak7cc2b072011-06-16 20:22:37 +00002616 addSuccessorWithWeight(IndirectBrMBB, Succ);
2617 }
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002618
Andrew Trickac6d9be2013-05-25 02:42:55 +00002619 DAG.setRoot(DAG.getNode(ISD::BRIND, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002620 MVT::Other, getControlRoot(),
2621 getValue(I.getAddress())));
Bill Wendling49fcff82009-12-21 22:30:11 +00002622}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002623
Dan Gohman46510a72010-04-15 01:51:59 +00002624void SelectionDAGBuilder::visitFSub(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002625 // -0.0 - X --> fneg
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002626 Type *Ty = I.getType();
Chris Lattner2ca5c862011-02-15 00:14:00 +00002627 if (isa<Constant>(I.getOperand(0)) &&
2628 I.getOperand(0) == ConstantFP::getZeroValueForNegation(Ty)) {
2629 SDValue Op2 = getValue(I.getOperand(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00002630 setValue(&I, DAG.getNode(ISD::FNEG, getCurSDLoc(),
Chris Lattner2ca5c862011-02-15 00:14:00 +00002631 Op2.getValueType(), Op2));
2632 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002633 }
Bill Wendling49fcff82009-12-21 22:30:11 +00002634
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002635 visitBinary(I, ISD::FSUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002636}
2637
Dan Gohman46510a72010-04-15 01:51:59 +00002638void SelectionDAGBuilder::visitBinary(const User &I, unsigned OpCode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002639 SDValue Op1 = getValue(I.getOperand(0));
2640 SDValue Op2 = getValue(I.getOperand(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00002641 setValue(&I, DAG.getNode(OpCode, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002642 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002643}
2644
Dan Gohman46510a72010-04-15 01:51:59 +00002645void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002646 SDValue Op1 = getValue(I.getOperand(0));
2647 SDValue Op2 = getValue(I.getOperand(1));
Owen Anderson95771af2011-02-25 21:41:48 +00002648
Michael Liaoa6b20ce2013-03-01 18:40:30 +00002649 EVT ShiftTy = TLI.getShiftAmountTy(Op2.getValueType());
Owen Anderson95771af2011-02-25 21:41:48 +00002650
Chris Lattnerd3027732011-02-13 09:02:52 +00002651 // Coerce the shift amount to the right type if we can.
2652 if (!I.getType()->isVectorTy() && Op2.getValueType() != ShiftTy) {
Chris Lattner915eeb42011-02-13 09:10:56 +00002653 unsigned ShiftSize = ShiftTy.getSizeInBits();
2654 unsigned Op2Size = Op2.getValueType().getSizeInBits();
Andrew Trickac6d9be2013-05-25 02:42:55 +00002655 SDLoc DL = getCurSDLoc();
Owen Anderson95771af2011-02-25 21:41:48 +00002656
Dan Gohman57fc82d2009-04-09 03:51:29 +00002657 // If the operand is smaller than the shift count type, promote it.
Chris Lattnerd3027732011-02-13 09:02:52 +00002658 if (ShiftSize > Op2Size)
2659 Op2 = DAG.getNode(ISD::ZERO_EXTEND, DL, ShiftTy, Op2);
Owen Anderson95771af2011-02-25 21:41:48 +00002660
Dan Gohman57fc82d2009-04-09 03:51:29 +00002661 // If the operand is larger than the shift count type but the shift
2662 // count type has enough bits to represent any shift value, truncate
2663 // it now. This is a common case and it exposes the truncate to
2664 // optimization early.
Chris Lattnerd3027732011-02-13 09:02:52 +00002665 else if (ShiftSize >= Log2_32_Ceil(Op2.getValueType().getSizeInBits()))
2666 Op2 = DAG.getNode(ISD::TRUNCATE, DL, ShiftTy, Op2);
2667 // Otherwise we'll need to temporarily settle for some other convenient
Chris Lattnere0751182011-02-13 19:09:16 +00002668 // type. Type legalization will make adjustments once the shiftee is split.
Chris Lattnerd3027732011-02-13 09:02:52 +00002669 else
Chris Lattnere0751182011-02-13 19:09:16 +00002670 Op2 = DAG.getZExtOrTrunc(Op2, DL, MVT::i32);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002671 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002672
Andrew Trickac6d9be2013-05-25 02:42:55 +00002673 setValue(&I, DAG.getNode(Opcode, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002674 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002675}
2676
Benjamin Kramer9c640302011-07-08 10:31:30 +00002677void SelectionDAGBuilder::visitSDiv(const User &I) {
Benjamin Kramer9c640302011-07-08 10:31:30 +00002678 SDValue Op1 = getValue(I.getOperand(0));
2679 SDValue Op2 = getValue(I.getOperand(1));
2680
2681 // Turn exact SDivs into multiplications.
2682 // FIXME: This should be in DAGCombiner, but it doesn't have access to the
2683 // exact bit.
Benjamin Kramer3492a4a2011-07-08 12:08:24 +00002684 if (isa<BinaryOperator>(&I) && cast<BinaryOperator>(&I)->isExact() &&
2685 !isa<ConstantSDNode>(Op1) &&
Benjamin Kramer9c640302011-07-08 10:31:30 +00002686 isa<ConstantSDNode>(Op2) && !cast<ConstantSDNode>(Op2)->isNullValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00002687 setValue(&I, TLI.BuildExactSDIV(Op1, Op2, getCurSDLoc(), DAG));
Benjamin Kramer9c640302011-07-08 10:31:30 +00002688 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00002689 setValue(&I, DAG.getNode(ISD::SDIV, getCurSDLoc(), Op1.getValueType(),
Benjamin Kramer9c640302011-07-08 10:31:30 +00002690 Op1, Op2));
2691}
2692
Dan Gohman46510a72010-04-15 01:51:59 +00002693void SelectionDAGBuilder::visitICmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002694 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002695 if (const ICmpInst *IC = dyn_cast<ICmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002696 predicate = IC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002697 else if (const ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002698 predicate = ICmpInst::Predicate(IC->getPredicate());
2699 SDValue Op1 = getValue(I.getOperand(0));
2700 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002701 ISD::CondCode Opcode = getICmpCondCode(predicate);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002702
Owen Andersone50ed302009-08-10 22:56:29 +00002703 EVT DestVT = TLI.getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002704 setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Opcode));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002705}
2706
Dan Gohman46510a72010-04-15 01:51:59 +00002707void SelectionDAGBuilder::visitFCmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002708 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002709 if (const FCmpInst *FC = dyn_cast<FCmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002710 predicate = FC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002711 else if (const ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002712 predicate = FCmpInst::Predicate(FC->getPredicate());
2713 SDValue Op1 = getValue(I.getOperand(0));
2714 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002715 ISD::CondCode Condition = getFCmpCondCode(predicate);
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002716 if (TM.Options.NoNaNsFPMath)
2717 Condition = getFCmpCodeWithoutNaN(Condition);
Owen Andersone50ed302009-08-10 22:56:29 +00002718 EVT DestVT = TLI.getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002719 setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Condition));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002720}
2721
Dan Gohman46510a72010-04-15 01:51:59 +00002722void SelectionDAGBuilder::visitSelect(const User &I) {
Owen Andersone50ed302009-08-10 22:56:29 +00002723 SmallVector<EVT, 4> ValueVTs;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002724 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2725 unsigned NumValues = ValueVTs.size();
Bill Wendling49fcff82009-12-21 22:30:11 +00002726 if (NumValues == 0) return;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002727
Bill Wendling49fcff82009-12-21 22:30:11 +00002728 SmallVector<SDValue, 4> Values(NumValues);
2729 SDValue Cond = getValue(I.getOperand(0));
2730 SDValue TrueVal = getValue(I.getOperand(1));
2731 SDValue FalseVal = getValue(I.getOperand(2));
Duncan Sands28b77e92011-09-06 19:07:46 +00002732 ISD::NodeType OpCode = Cond.getValueType().isVector() ?
2733 ISD::VSELECT : ISD::SELECT;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002734
Bill Wendling4533cac2010-01-28 21:51:40 +00002735 for (unsigned i = 0; i != NumValues; ++i)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002736 Values[i] = DAG.getNode(OpCode, getCurSDLoc(),
Duncan Sands28b77e92011-09-06 19:07:46 +00002737 TrueVal.getNode()->getValueType(TrueVal.getResNo()+i),
Chris Lattnerb3e87b22010-03-12 07:15:36 +00002738 Cond,
Bill Wendling49fcff82009-12-21 22:30:11 +00002739 SDValue(TrueVal.getNode(),
2740 TrueVal.getResNo() + i),
2741 SDValue(FalseVal.getNode(),
2742 FalseVal.getResNo() + i));
2743
Andrew Trickac6d9be2013-05-25 02:42:55 +00002744 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002745 DAG.getVTList(&ValueVTs[0], NumValues),
2746 &Values[0], NumValues));
Bill Wendling49fcff82009-12-21 22:30:11 +00002747}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002748
Dan Gohman46510a72010-04-15 01:51:59 +00002749void SelectionDAGBuilder::visitTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002750 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2751 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002752 EVT DestVT = TLI.getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002753 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002754}
2755
Dan Gohman46510a72010-04-15 01:51:59 +00002756void SelectionDAGBuilder::visitZExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002757 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2758 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2759 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002760 EVT DestVT = TLI.getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002761 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002762}
2763
Dan Gohman46510a72010-04-15 01:51:59 +00002764void SelectionDAGBuilder::visitSExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002765 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2766 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2767 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002768 EVT DestVT = TLI.getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002769 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002770}
2771
Dan Gohman46510a72010-04-15 01:51:59 +00002772void SelectionDAGBuilder::visitFPTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002773 // FPTrunc is never a no-op cast, no need to check
2774 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002775 EVT DestVT = TLI.getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002776 setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurSDLoc(),
Pete Cooperf57e1c22012-01-17 01:54:07 +00002777 DestVT, N,
2778 DAG.getTargetConstant(0, TLI.getPointerTy())));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002779}
2780
Dan Gohman46510a72010-04-15 01:51:59 +00002781void SelectionDAGBuilder::visitFPExt(const User &I){
Hal Finkel46bb70c2011-10-18 03:51:57 +00002782 // FPExt is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002783 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002784 EVT DestVT = TLI.getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002785 setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002786}
2787
Dan Gohman46510a72010-04-15 01:51:59 +00002788void SelectionDAGBuilder::visitFPToUI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002789 // FPToUI is never a no-op cast, no need to check
2790 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002791 EVT DestVT = TLI.getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002792 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002793}
2794
Dan Gohman46510a72010-04-15 01:51:59 +00002795void SelectionDAGBuilder::visitFPToSI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002796 // FPToSI is never a no-op cast, no need to check
2797 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002798 EVT DestVT = TLI.getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002799 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002800}
2801
Dan Gohman46510a72010-04-15 01:51:59 +00002802void SelectionDAGBuilder::visitUIToFP(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002803 // UIToFP is never a no-op cast, no need to check
2804 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002805 EVT DestVT = TLI.getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002806 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002807}
2808
Dan Gohman46510a72010-04-15 01:51:59 +00002809void SelectionDAGBuilder::visitSIToFP(const User &I){
Bill Wendling181b6272008-10-19 20:34:04 +00002810 // SIToFP is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002811 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002812 EVT DestVT = TLI.getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002813 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002814}
2815
Dan Gohman46510a72010-04-15 01:51:59 +00002816void SelectionDAGBuilder::visitPtrToInt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002817 // What to do depends on the size of the integer and the size of the pointer.
2818 // We can either truncate, zero extend, or no-op, accordingly.
2819 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002820 EVT DestVT = TLI.getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002821 setValue(&I, DAG.getZExtOrTrunc(N, getCurSDLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002822}
2823
Dan Gohman46510a72010-04-15 01:51:59 +00002824void SelectionDAGBuilder::visitIntToPtr(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002825 // What to do depends on the size of the integer and the size of the pointer.
2826 // We can either truncate, zero extend, or no-op, accordingly.
2827 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002828 EVT DestVT = TLI.getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002829 setValue(&I, DAG.getZExtOrTrunc(N, getCurSDLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002830}
2831
Dan Gohman46510a72010-04-15 01:51:59 +00002832void SelectionDAGBuilder::visitBitCast(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002833 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002834 EVT DestVT = TLI.getValueType(I.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002835
Bill Wendling49fcff82009-12-21 22:30:11 +00002836 // BitCast assures us that source and destination are the same size so this is
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002837 // either a BITCAST or a no-op.
Bill Wendling4533cac2010-01-28 21:51:40 +00002838 if (DestVT != N.getValueType())
Andrew Trickac6d9be2013-05-25 02:42:55 +00002839 setValue(&I, DAG.getNode(ISD::BITCAST, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002840 DestVT, N)); // convert types.
2841 else
Bill Wendling49fcff82009-12-21 22:30:11 +00002842 setValue(&I, N); // noop cast.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002843}
2844
Dan Gohman46510a72010-04-15 01:51:59 +00002845void SelectionDAGBuilder::visitInsertElement(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002846 SDValue InVec = getValue(I.getOperand(0));
2847 SDValue InVal = getValue(I.getOperand(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00002848 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurSDLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002849 TLI.getPointerTy(),
2850 getValue(I.getOperand(2)));
Andrew Trickac6d9be2013-05-25 02:42:55 +00002851 setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002852 TLI.getValueType(I.getType()),
2853 InVec, InVal, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002854}
2855
Dan Gohman46510a72010-04-15 01:51:59 +00002856void SelectionDAGBuilder::visitExtractElement(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002857 SDValue InVec = getValue(I.getOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00002858 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurSDLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002859 TLI.getPointerTy(),
2860 getValue(I.getOperand(1)));
Andrew Trickac6d9be2013-05-25 02:42:55 +00002861 setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002862 TLI.getValueType(I.getType()), InVec, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002863}
2864
Craig Topper51578342012-01-04 09:23:09 +00002865// Utility for visitShuffleVector - Return true if every element in Mask,
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00002866// beginning from position Pos and ending in Pos+Size, falls within the
Craig Topper51578342012-01-04 09:23:09 +00002867// specified sequential range [L, L+Pos). or is undef.
2868static bool isSequentialInRange(const SmallVectorImpl<int> &Mask,
Craig Topper23de31b2012-04-11 03:06:35 +00002869 unsigned Pos, unsigned Size, int Low) {
2870 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Craig Topper51578342012-01-04 09:23:09 +00002871 if (Mask[i] >= 0 && Mask[i] != Low)
Nate Begeman9008ca62009-04-27 18:41:29 +00002872 return false;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002873 return true;
2874}
2875
Dan Gohman46510a72010-04-15 01:51:59 +00002876void SelectionDAGBuilder::visitShuffleVector(const User &I) {
Mon P Wang230e4fa2008-11-21 04:25:21 +00002877 SDValue Src1 = getValue(I.getOperand(0));
2878 SDValue Src2 = getValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002879
Chris Lattner56243b82012-01-26 02:51:13 +00002880 SmallVector<int, 8> Mask;
2881 ShuffleVectorInst::getShuffleMask(cast<Constant>(I.getOperand(2)), Mask);
2882 unsigned MaskNumElts = Mask.size();
2883
Owen Andersone50ed302009-08-10 22:56:29 +00002884 EVT VT = TLI.getValueType(I.getType());
2885 EVT SrcVT = Src1.getValueType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00002886 unsigned SrcNumElts = SrcVT.getVectorNumElements();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002887
Mon P Wangc7849c22008-11-16 05:06:27 +00002888 if (SrcNumElts == MaskNumElts) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002889 setValue(&I, DAG.getVectorShuffle(VT, getCurSDLoc(), Src1, Src2,
Bill Wendling4533cac2010-01-28 21:51:40 +00002890 &Mask[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002891 return;
2892 }
2893
2894 // Normalize the shuffle vector since mask and vector length don't match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002895 if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) {
2896 // Mask is longer than the source vectors and is a multiple of the source
2897 // vectors. We can use concatenate vector to make the mask and vectors
Mon P Wang230e4fa2008-11-21 04:25:21 +00002898 // lengths match.
Craig Topper51578342012-01-04 09:23:09 +00002899 if (SrcNumElts*2 == MaskNumElts) {
2900 // First check for Src1 in low and Src2 in high
2901 if (isSequentialInRange(Mask, 0, SrcNumElts, 0) &&
2902 isSequentialInRange(Mask, SrcNumElts, SrcNumElts, SrcNumElts)) {
2903 // The shuffle is concatenating two vectors together.
Andrew Trickac6d9be2013-05-25 02:42:55 +00002904 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurSDLoc(),
Craig Topper51578342012-01-04 09:23:09 +00002905 VT, Src1, Src2));
2906 return;
2907 }
2908 // Then check for Src2 in low and Src1 in high
2909 if (isSequentialInRange(Mask, 0, SrcNumElts, SrcNumElts) &&
2910 isSequentialInRange(Mask, SrcNumElts, SrcNumElts, 0)) {
2911 // The shuffle is concatenating two vectors together.
Andrew Trickac6d9be2013-05-25 02:42:55 +00002912 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurSDLoc(),
Craig Topper51578342012-01-04 09:23:09 +00002913 VT, Src2, Src1));
2914 return;
2915 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002916 }
2917
Mon P Wangc7849c22008-11-16 05:06:27 +00002918 // Pad both vectors with undefs to make them the same length as the mask.
2919 unsigned NumConcat = MaskNumElts / SrcNumElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00002920 bool Src1U = Src1.getOpcode() == ISD::UNDEF;
2921 bool Src2U = Src2.getOpcode() == ISD::UNDEF;
Dale Johannesene8d72302009-02-06 23:05:02 +00002922 SDValue UndefVal = DAG.getUNDEF(SrcVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002923
Nate Begeman9008ca62009-04-27 18:41:29 +00002924 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
2925 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002926 MOps1[0] = Src1;
2927 MOps2[0] = Src2;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002928
2929 Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002930 getCurSDLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002931 &MOps1[0], NumConcat);
2932 Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002933 getCurSDLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002934 &MOps2[0], NumConcat);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002935
Mon P Wangaeb06d22008-11-10 04:46:22 +00002936 // Readjust mask for new input vector length.
Nate Begeman9008ca62009-04-27 18:41:29 +00002937 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002938 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002939 int Idx = Mask[i];
Craig Topper23de31b2012-04-11 03:06:35 +00002940 if (Idx >= (int)SrcNumElts)
2941 Idx -= SrcNumElts - MaskNumElts;
2942 MappedOps.push_back(Idx);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002943 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002944
Andrew Trickac6d9be2013-05-25 02:42:55 +00002945 setValue(&I, DAG.getVectorShuffle(VT, getCurSDLoc(), Src1, Src2,
Bill Wendling4533cac2010-01-28 21:51:40 +00002946 &MappedOps[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002947 return;
2948 }
2949
Mon P Wangc7849c22008-11-16 05:06:27 +00002950 if (SrcNumElts > MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002951 // Analyze the access pattern of the vector to see if we can extract
2952 // two subvectors and do the shuffle. The analysis is done by calculating
2953 // the range of elements the mask access on both vectors.
Craig Topper10612dc2012-04-08 23:15:04 +00002954 int MinRange[2] = { static_cast<int>(SrcNumElts),
2955 static_cast<int>(SrcNumElts)};
Mon P Wangc7849c22008-11-16 05:06:27 +00002956 int MaxRange[2] = {-1, -1};
2957
Nate Begeman5a5ca152009-04-29 05:20:52 +00002958 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002959 int Idx = Mask[i];
Craig Topper10612dc2012-04-08 23:15:04 +00002960 unsigned Input = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00002961 if (Idx < 0)
2962 continue;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002963
Nate Begeman5a5ca152009-04-29 05:20:52 +00002964 if (Idx >= (int)SrcNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002965 Input = 1;
2966 Idx -= SrcNumElts;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002967 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002968 if (Idx > MaxRange[Input])
2969 MaxRange[Input] = Idx;
2970 if (Idx < MinRange[Input])
2971 MinRange[Input] = Idx;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002972 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002973
Mon P Wangc7849c22008-11-16 05:06:27 +00002974 // Check if the access is smaller than the vector size and can we find
2975 // a reasonable extract index.
Craig Topper10612dc2012-04-08 23:15:04 +00002976 int RangeUse[2] = { -1, -1 }; // 0 = Unused, 1 = Extract, -1 = Can not
2977 // Extract.
Mon P Wangc7849c22008-11-16 05:06:27 +00002978 int StartIdx[2]; // StartIdx to extract from
Craig Topper10612dc2012-04-08 23:15:04 +00002979 for (unsigned Input = 0; Input < 2; ++Input) {
2980 if (MinRange[Input] >= (int)SrcNumElts && MaxRange[Input] < 0) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002981 RangeUse[Input] = 0; // Unused
2982 StartIdx[Input] = 0;
Craig Topperf873dde2012-04-08 17:53:33 +00002983 continue;
Mon P Wang230e4fa2008-11-21 04:25:21 +00002984 }
Craig Topperf873dde2012-04-08 17:53:33 +00002985
2986 // Find a good start index that is a multiple of the mask length. Then
2987 // see if the rest of the elements are in range.
2988 StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts;
2989 if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts &&
2990 StartIdx[Input] + MaskNumElts <= SrcNumElts)
2991 RangeUse[Input] = 1; // Extract from a multiple of the mask length.
Mon P Wangc7849c22008-11-16 05:06:27 +00002992 }
2993
Bill Wendling636e2582009-08-21 18:16:06 +00002994 if (RangeUse[0] == 0 && RangeUse[1] == 0) {
Bill Wendling4533cac2010-01-28 21:51:40 +00002995 setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
Mon P Wangc7849c22008-11-16 05:06:27 +00002996 return;
2997 }
Craig Topper10612dc2012-04-08 23:15:04 +00002998 if (RangeUse[0] >= 0 && RangeUse[1] >= 0) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002999 // Extract appropriate subvector and generate a vector shuffle
Craig Topper10612dc2012-04-08 23:15:04 +00003000 for (unsigned Input = 0; Input < 2; ++Input) {
Bill Wendling87710f02009-12-21 23:47:40 +00003001 SDValue &Src = Input == 0 ? Src1 : Src2;
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003002 if (RangeUse[Input] == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00003003 Src = DAG.getUNDEF(VT);
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003004 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00003005 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurSDLoc(), VT,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003006 Src, DAG.getIntPtrConstant(StartIdx[Input]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00003007 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003008
Mon P Wangc7849c22008-11-16 05:06:27 +00003009 // Calculate new mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00003010 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003011 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003012 int Idx = Mask[i];
Craig Topper23de31b2012-04-11 03:06:35 +00003013 if (Idx >= 0) {
3014 if (Idx < (int)SrcNumElts)
3015 Idx -= StartIdx[0];
3016 else
3017 Idx -= SrcNumElts + StartIdx[1] - MaskNumElts;
3018 }
3019 MappedOps.push_back(Idx);
Mon P Wangc7849c22008-11-16 05:06:27 +00003020 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003021
Andrew Trickac6d9be2013-05-25 02:42:55 +00003022 setValue(&I, DAG.getVectorShuffle(VT, getCurSDLoc(), Src1, Src2,
Bill Wendling4533cac2010-01-28 21:51:40 +00003023 &MappedOps[0]));
Mon P Wangc7849c22008-11-16 05:06:27 +00003024 return;
Mon P Wangaeb06d22008-11-10 04:46:22 +00003025 }
3026 }
3027
Mon P Wangc7849c22008-11-16 05:06:27 +00003028 // We can't use either concat vectors or extract subvectors so fall back to
3029 // replacing the shuffle with extract and build vector.
3030 // to insert and build vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003031 EVT EltVT = VT.getVectorElementType();
3032 EVT PtrVT = TLI.getPointerTy();
Mon P Wangaeb06d22008-11-10 04:46:22 +00003033 SmallVector<SDValue,8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003034 for (unsigned i = 0; i != MaskNumElts; ++i) {
Craig Topper23de31b2012-04-11 03:06:35 +00003035 int Idx = Mask[i];
3036 SDValue Res;
3037
3038 if (Idx < 0) {
3039 Res = DAG.getUNDEF(EltVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00003040 } else {
Craig Topper23de31b2012-04-11 03:06:35 +00003041 SDValue &Src = Idx < (int)SrcNumElts ? Src1 : Src2;
3042 if (Idx >= (int)SrcNumElts) Idx -= SrcNumElts;
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003043
Andrew Trickac6d9be2013-05-25 02:42:55 +00003044 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurSDLoc(),
Craig Topper23de31b2012-04-11 03:06:35 +00003045 EltVT, Src, DAG.getConstant(Idx, PtrVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00003046 }
Craig Topper23de31b2012-04-11 03:06:35 +00003047
3048 Ops.push_back(Res);
Mon P Wangaeb06d22008-11-10 04:46:22 +00003049 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003050
Andrew Trickac6d9be2013-05-25 02:42:55 +00003051 setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00003052 VT, &Ops[0], Ops.size()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003053}
3054
Dan Gohman46510a72010-04-15 01:51:59 +00003055void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003056 const Value *Op0 = I.getOperand(0);
3057 const Value *Op1 = I.getOperand(1);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003058 Type *AggTy = I.getType();
3059 Type *ValTy = Op1->getType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003060 bool IntoUndef = isa<UndefValue>(Op0);
3061 bool FromUndef = isa<UndefValue>(Op1);
3062
Jay Foadfc6d3a42011-07-13 10:26:04 +00003063 unsigned LinearIndex = ComputeLinearIndex(AggTy, I.getIndices());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003064
Owen Andersone50ed302009-08-10 22:56:29 +00003065 SmallVector<EVT, 4> AggValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003066 ComputeValueVTs(TLI, AggTy, AggValueVTs);
Owen Andersone50ed302009-08-10 22:56:29 +00003067 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003068 ComputeValueVTs(TLI, ValTy, ValValueVTs);
3069
3070 unsigned NumAggValues = AggValueVTs.size();
3071 unsigned NumValValues = ValValueVTs.size();
3072 SmallVector<SDValue, 4> Values(NumAggValues);
3073
3074 SDValue Agg = getValue(Op0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003075 unsigned i = 0;
3076 // Copy the beginning value(s) from the original aggregate.
3077 for (; i != LinearIndex; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00003078 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003079 SDValue(Agg.getNode(), Agg.getResNo() + i);
3080 // Copy values from the inserted value(s).
Rafael Espindola3fa82832011-05-13 15:18:06 +00003081 if (NumValValues) {
3082 SDValue Val = getValue(Op1);
3083 for (; i != LinearIndex + NumValValues; ++i)
3084 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
3085 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
3086 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003087 // Copy remaining value(s) from the original aggregate.
3088 for (; i != NumAggValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00003089 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003090 SDValue(Agg.getNode(), Agg.getResNo() + i);
3091
Andrew Trickac6d9be2013-05-25 02:42:55 +00003092 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00003093 DAG.getVTList(&AggValueVTs[0], NumAggValues),
3094 &Values[0], NumAggValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003095}
3096
Dan Gohman46510a72010-04-15 01:51:59 +00003097void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003098 const Value *Op0 = I.getOperand(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003099 Type *AggTy = Op0->getType();
3100 Type *ValTy = I.getType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003101 bool OutOfUndef = isa<UndefValue>(Op0);
3102
Jay Foadfc6d3a42011-07-13 10:26:04 +00003103 unsigned LinearIndex = ComputeLinearIndex(AggTy, I.getIndices());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003104
Owen Andersone50ed302009-08-10 22:56:29 +00003105 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003106 ComputeValueVTs(TLI, ValTy, ValValueVTs);
3107
3108 unsigned NumValValues = ValValueVTs.size();
Rafael Espindola3fa82832011-05-13 15:18:06 +00003109
3110 // Ignore a extractvalue that produces an empty object
3111 if (!NumValValues) {
3112 setValue(&I, DAG.getUNDEF(MVT(MVT::Other)));
3113 return;
3114 }
3115
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003116 SmallVector<SDValue, 4> Values(NumValValues);
3117
3118 SDValue Agg = getValue(Op0);
3119 // Copy out the selected value(s).
3120 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
3121 Values[i - LinearIndex] =
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00003122 OutOfUndef ?
Dale Johannesene8d72302009-02-06 23:05:02 +00003123 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00003124 SDValue(Agg.getNode(), Agg.getResNo() + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003125
Andrew Trickac6d9be2013-05-25 02:42:55 +00003126 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00003127 DAG.getVTList(&ValValueVTs[0], NumValValues),
3128 &Values[0], NumValValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003129}
3130
Dan Gohman46510a72010-04-15 01:51:59 +00003131void SelectionDAGBuilder::visitGetElementPtr(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003132 SDValue N = getValue(I.getOperand(0));
Nadav Rotem1c239202012-02-28 14:13:19 +00003133 // Note that the pointer operand may be a vector of pointers. Take the scalar
3134 // element which holds a pointer.
3135 Type *Ty = I.getOperand(0)->getType()->getScalarType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003136
Dan Gohman46510a72010-04-15 01:51:59 +00003137 for (GetElementPtrInst::const_op_iterator OI = I.op_begin()+1, E = I.op_end();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003138 OI != E; ++OI) {
Dan Gohman46510a72010-04-15 01:51:59 +00003139 const Value *Idx = *OI;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003140 if (StructType *StTy = dyn_cast<StructType>(Ty)) {
Duncan Sandsb2df01a2012-11-13 13:01:58 +00003141 unsigned Field = cast<Constant>(Idx)->getUniqueInteger().getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003142 if (Field) {
3143 // N = N + Offset
3144 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003145 N = DAG.getNode(ISD::ADD, getCurSDLoc(), N.getValueType(), N,
Duncan Sandsb2df01a2012-11-13 13:01:58 +00003146 DAG.getConstant(Offset, N.getValueType()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003147 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00003148
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003149 Ty = StTy->getElementType(Field);
3150 } else {
3151 Ty = cast<SequentialType>(Ty)->getElementType();
3152
3153 // If this is a constant subscript, handle it quickly.
Dan Gohman46510a72010-04-15 01:51:59 +00003154 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Dan Gohmane368b462010-06-18 14:22:04 +00003155 if (CI->isZero()) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003156 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +00003157 TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Evan Cheng65b52df2009-02-09 21:01:06 +00003158 SDValue OffsVal;
Owen Andersone50ed302009-08-10 22:56:29 +00003159 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00003160 unsigned PtrBits = PTy.getSizeInBits();
Bill Wendlinge1a90422009-12-21 23:10:19 +00003161 if (PtrBits < 64)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003162 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(),
Evan Cheng65b52df2009-02-09 21:01:06 +00003163 TLI.getPointerTy(),
Owen Anderson825b72b2009-08-11 20:47:22 +00003164 DAG.getConstant(Offs, MVT::i64));
Bill Wendlinge1a90422009-12-21 23:10:19 +00003165 else
Evan Chengb1032a82009-02-09 20:54:38 +00003166 OffsVal = DAG.getIntPtrConstant(Offs);
Bill Wendlinge1a90422009-12-21 23:10:19 +00003167
Andrew Trickac6d9be2013-05-25 02:42:55 +00003168 N = DAG.getNode(ISD::ADD, getCurSDLoc(), N.getValueType(), N,
Evan Chengb1032a82009-02-09 20:54:38 +00003169 OffsVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003170 continue;
3171 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003172
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003173 // N = N + Idx * ElementSize;
Dan Gohman7abbd042009-10-23 17:57:43 +00003174 APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(),
3175 TD->getTypeAllocSize(Ty));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003176 SDValue IdxN = getValue(Idx);
3177
3178 // If the index is smaller or larger than intptr_t, truncate or extend
3179 // it.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003180 IdxN = DAG.getSExtOrTrunc(IdxN, getCurSDLoc(), N.getValueType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003181
3182 // If this is a multiply by a power of two, turn it into a shl
3183 // immediately. This is a very common case.
3184 if (ElementSize != 1) {
Dan Gohman7abbd042009-10-23 17:57:43 +00003185 if (ElementSize.isPowerOf2()) {
3186 unsigned Amt = ElementSize.logBase2();
Andrew Trickac6d9be2013-05-25 02:42:55 +00003187 IdxN = DAG.getNode(ISD::SHL, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003188 N.getValueType(), IdxN,
Nadav Rotem16087692011-12-05 06:29:09 +00003189 DAG.getConstant(Amt, IdxN.getValueType()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003190 } else {
Duncan Sandsb2df01a2012-11-13 13:01:58 +00003191 SDValue Scale = DAG.getConstant(ElementSize, IdxN.getValueType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003192 IdxN = DAG.getNode(ISD::MUL, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003193 N.getValueType(), IdxN, Scale);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003194 }
3195 }
3196
Andrew Trickac6d9be2013-05-25 02:42:55 +00003197 N = DAG.getNode(ISD::ADD, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003198 N.getValueType(), N, IdxN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003199 }
3200 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00003201
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003202 setValue(&I, N);
3203}
3204
Dan Gohman46510a72010-04-15 01:51:59 +00003205void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003206 // If this is a fixed sized alloca in the entry block of the function,
3207 // allocate it statically on the stack.
3208 if (FuncInfo.StaticAllocaMap.count(&I))
3209 return; // getValue will auto-populate this.
3210
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003211 Type *Ty = I.getAllocatedType();
Micah Villmow3574eca2012-10-08 16:38:25 +00003212 uint64_t TySize = TLI.getDataLayout()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003213 unsigned Align =
Micah Villmow3574eca2012-10-08 16:38:25 +00003214 std::max((unsigned)TLI.getDataLayout()->getPrefTypeAlignment(Ty),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003215 I.getAlignment());
3216
3217 SDValue AllocSize = getValue(I.getArraySize());
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003218
Owen Andersone50ed302009-08-10 22:56:29 +00003219 EVT IntPtr = TLI.getPointerTy();
Dan Gohmanf75a7d32010-05-28 01:14:11 +00003220 if (AllocSize.getValueType() != IntPtr)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003221 AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurSDLoc(), IntPtr);
Dan Gohmanf75a7d32010-05-28 01:14:11 +00003222
Andrew Trickac6d9be2013-05-25 02:42:55 +00003223 AllocSize = DAG.getNode(ISD::MUL, getCurSDLoc(), IntPtr,
Dan Gohmanf75a7d32010-05-28 01:14:11 +00003224 AllocSize,
3225 DAG.getConstant(TySize, IntPtr));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003226
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003227 // Handle alignment. If the requested alignment is less than or equal to
3228 // the stack alignment, ignore it. If the size is greater than or equal to
3229 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00003230 unsigned StackAlign = TM.getFrameLowering()->getStackAlignment();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003231 if (Align <= StackAlign)
3232 Align = 0;
3233
3234 // Round the size of the allocation up to the stack alignment size
3235 // by add SA-1 to the size.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003236 AllocSize = DAG.getNode(ISD::ADD, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003237 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003238 DAG.getIntPtrConstant(StackAlign-1));
Bill Wendling856ff412009-12-22 00:12:37 +00003239
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003240 // Mask out the low bits for alignment purposes.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003241 AllocSize = DAG.getNode(ISD::AND, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003242 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003243 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
3244
3245 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Owen Anderson825b72b2009-08-11 20:47:22 +00003246 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003247 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurSDLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003248 VTs, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003249 setValue(&I, DSA);
3250 DAG.setRoot(DSA.getValue(1));
Bill Wendling856ff412009-12-22 00:12:37 +00003251
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003252 // Inform the Frame Information that we have just allocated a variable-sized
3253 // object.
Bob Wilson8f637ad2013-02-08 20:35:15 +00003254 FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject(Align ? Align : 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003255}
3256
Dan Gohman46510a72010-04-15 01:51:59 +00003257void SelectionDAGBuilder::visitLoad(const LoadInst &I) {
Eli Friedman327236c2011-08-24 20:50:09 +00003258 if (I.isAtomic())
3259 return visitAtomicLoad(I);
3260
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003261 const Value *SV = I.getOperand(0);
3262 SDValue Ptr = getValue(SV);
3263
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003264 Type *Ty = I.getType();
David Greene1e559442010-02-15 17:00:31 +00003265
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003266 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00003267 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Pete Cooperd752e0f2011-11-08 18:42:53 +00003268 bool isInvariant = I.getMetadata("invariant.load") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003269 unsigned Alignment = I.getAlignment();
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00003270 const MDNode *TBAAInfo = I.getMetadata(LLVMContext::MD_tbaa);
Rafael Espindola95d594c2012-03-31 18:14:00 +00003271 const MDNode *Ranges = I.getMetadata(LLVMContext::MD_range);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003272
Owen Andersone50ed302009-08-10 22:56:29 +00003273 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003274 SmallVector<uint64_t, 4> Offsets;
3275 ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets);
3276 unsigned NumValues = ValueVTs.size();
3277 if (NumValues == 0)
3278 return;
3279
3280 SDValue Root;
3281 bool ConstantMemory = false;
Andrew Trickde91f3c2010-11-12 17:50:46 +00003282 if (I.isVolatile() || NumValues > MaxParallelChains)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003283 // Serialize volatile loads with other side effects.
3284 Root = getRoot();
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00003285 else if (AA->pointsToConstantMemory(
3286 AliasAnalysis::Location(SV, AA->getTypeStoreSize(Ty), TBAAInfo))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003287 // Do not serialize (non-volatile) loads of constant memory with anything.
3288 Root = DAG.getEntryNode();
3289 ConstantMemory = true;
3290 } else {
3291 // Do not serialize non-volatile loads against each other.
3292 Root = DAG.getRoot();
3293 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003294
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003295 SmallVector<SDValue, 4> Values(NumValues);
Andrew Trickde91f3c2010-11-12 17:50:46 +00003296 SmallVector<SDValue, 4> Chains(std::min(unsigned(MaxParallelChains),
3297 NumValues));
Owen Andersone50ed302009-08-10 22:56:29 +00003298 EVT PtrVT = Ptr.getValueType();
Andrew Trickde91f3c2010-11-12 17:50:46 +00003299 unsigned ChainI = 0;
3300 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
3301 // Serializing loads here may result in excessive register pressure, and
3302 // TokenFactor places arbitrary choke points on the scheduler. SD scheduling
3303 // could recover a bit by hoisting nodes upward in the chain by recognizing
3304 // they are side-effect free or do not alias. The optimizer should really
3305 // avoid this case by converting large object/array copies to llvm.memcpy
3306 // (MaxParallelChains should always remain as failsafe).
3307 if (ChainI == MaxParallelChains) {
3308 assert(PendingLoads.empty() && "PendingLoads must be serialized first");
Andrew Trickac6d9be2013-05-25 02:42:55 +00003309 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003310 MVT::Other, &Chains[0], ChainI);
3311 Root = Chain;
3312 ChainI = 0;
3313 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00003314 SDValue A = DAG.getNode(ISD::ADD, getCurSDLoc(),
Bill Wendling856ff412009-12-22 00:12:37 +00003315 PtrVT, Ptr,
3316 DAG.getConstant(Offsets[i], PtrVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003317 SDValue L = DAG.getLoad(ValueVTs[i], getCurSDLoc(), Root,
Michael J. Spencere70c5262010-10-16 08:25:21 +00003318 A, MachinePointerInfo(SV, Offsets[i]), isVolatile,
Rafael Espindola95d594c2012-03-31 18:14:00 +00003319 isNonTemporal, isInvariant, Alignment, TBAAInfo,
3320 Ranges);
Bill Wendling856ff412009-12-22 00:12:37 +00003321
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003322 Values[i] = L;
Andrew Trickde91f3c2010-11-12 17:50:46 +00003323 Chains[ChainI] = L.getValue(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003324 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003325
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003326 if (!ConstantMemory) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003327 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003328 MVT::Other, &Chains[0], ChainI);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003329 if (isVolatile)
3330 DAG.setRoot(Chain);
3331 else
3332 PendingLoads.push_back(Chain);
3333 }
3334
Andrew Trickac6d9be2013-05-25 02:42:55 +00003335 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00003336 DAG.getVTList(&ValueVTs[0], NumValues),
3337 &Values[0], NumValues));
Bill Wendling856ff412009-12-22 00:12:37 +00003338}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003339
Dan Gohman46510a72010-04-15 01:51:59 +00003340void SelectionDAGBuilder::visitStore(const StoreInst &I) {
Eli Friedman327236c2011-08-24 20:50:09 +00003341 if (I.isAtomic())
3342 return visitAtomicStore(I);
3343
Dan Gohman46510a72010-04-15 01:51:59 +00003344 const Value *SrcV = I.getOperand(0);
3345 const Value *PtrV = I.getOperand(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003346
Owen Andersone50ed302009-08-10 22:56:29 +00003347 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003348 SmallVector<uint64_t, 4> Offsets;
3349 ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets);
3350 unsigned NumValues = ValueVTs.size();
3351 if (NumValues == 0)
3352 return;
3353
3354 // Get the lowered operands. Note that we do this after
3355 // checking if NumResults is zero, because with zero results
3356 // the operands won't have values in the map.
3357 SDValue Src = getValue(SrcV);
3358 SDValue Ptr = getValue(PtrV);
3359
3360 SDValue Root = getRoot();
Andrew Trickde91f3c2010-11-12 17:50:46 +00003361 SmallVector<SDValue, 4> Chains(std::min(unsigned(MaxParallelChains),
3362 NumValues));
Owen Andersone50ed302009-08-10 22:56:29 +00003363 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003364 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00003365 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003366 unsigned Alignment = I.getAlignment();
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00003367 const MDNode *TBAAInfo = I.getMetadata(LLVMContext::MD_tbaa);
Bill Wendling856ff412009-12-22 00:12:37 +00003368
Andrew Trickde91f3c2010-11-12 17:50:46 +00003369 unsigned ChainI = 0;
3370 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
3371 // See visitLoad comments.
3372 if (ChainI == MaxParallelChains) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003373 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003374 MVT::Other, &Chains[0], ChainI);
3375 Root = Chain;
3376 ChainI = 0;
3377 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00003378 SDValue Add = DAG.getNode(ISD::ADD, getCurSDLoc(), PtrVT, Ptr,
Bill Wendling856ff412009-12-22 00:12:37 +00003379 DAG.getConstant(Offsets[i], PtrVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003380 SDValue St = DAG.getStore(Root, getCurSDLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003381 SDValue(Src.getNode(), Src.getResNo() + i),
3382 Add, MachinePointerInfo(PtrV, Offsets[i]),
3383 isVolatile, isNonTemporal, Alignment, TBAAInfo);
3384 Chains[ChainI] = St;
Bill Wendling856ff412009-12-22 00:12:37 +00003385 }
3386
Andrew Trickac6d9be2013-05-25 02:42:55 +00003387 SDValue StoreNode = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003388 MVT::Other, &Chains[0], ChainI);
Devang Patel7e13efa2010-10-26 22:14:52 +00003389 DAG.setRoot(StoreNode);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003390}
3391
Eli Friedman26689ac2011-08-03 21:06:02 +00003392static SDValue InsertFenceForAtomic(SDValue Chain, AtomicOrdering Order,
Eli Friedman327236c2011-08-24 20:50:09 +00003393 SynchronizationScope Scope,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003394 bool Before, SDLoc dl,
Eli Friedman26689ac2011-08-03 21:06:02 +00003395 SelectionDAG &DAG,
3396 const TargetLowering &TLI) {
3397 // Fence, if necessary
3398 if (Before) {
Eli Friedman069e2ed2011-08-26 02:59:24 +00003399 if (Order == AcquireRelease || Order == SequentiallyConsistent)
Eli Friedman26689ac2011-08-03 21:06:02 +00003400 Order = Release;
3401 else if (Order == Acquire || Order == Monotonic)
3402 return Chain;
3403 } else {
3404 if (Order == AcquireRelease)
3405 Order = Acquire;
3406 else if (Order == Release || Order == Monotonic)
3407 return Chain;
3408 }
3409 SDValue Ops[3];
3410 Ops[0] = Chain;
Eli Friedman327236c2011-08-24 20:50:09 +00003411 Ops[1] = DAG.getConstant(Order, TLI.getPointerTy());
3412 Ops[2] = DAG.getConstant(Scope, TLI.getPointerTy());
Eli Friedman26689ac2011-08-03 21:06:02 +00003413 return DAG.getNode(ISD::ATOMIC_FENCE, dl, MVT::Other, Ops, 3);
3414}
3415
Eli Friedmanff030482011-07-28 21:48:00 +00003416void SelectionDAGBuilder::visitAtomicCmpXchg(const AtomicCmpXchgInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003417 SDLoc dl = getCurSDLoc();
Eli Friedman26689ac2011-08-03 21:06:02 +00003418 AtomicOrdering Order = I.getOrdering();
Eli Friedman327236c2011-08-24 20:50:09 +00003419 SynchronizationScope Scope = I.getSynchScope();
Eli Friedman26689ac2011-08-03 21:06:02 +00003420
3421 SDValue InChain = getRoot();
3422
3423 if (TLI.getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003424 InChain = InsertFenceForAtomic(InChain, Order, Scope, true, dl,
3425 DAG, TLI);
Eli Friedman26689ac2011-08-03 21:06:02 +00003426
Eli Friedman55ba8162011-07-29 03:05:32 +00003427 SDValue L =
Eli Friedman26689ac2011-08-03 21:06:02 +00003428 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl,
Eli Friedman55ba8162011-07-29 03:05:32 +00003429 getValue(I.getCompareOperand()).getValueType().getSimpleVT(),
Eli Friedman26689ac2011-08-03 21:06:02 +00003430 InChain,
Eli Friedman55ba8162011-07-29 03:05:32 +00003431 getValue(I.getPointerOperand()),
3432 getValue(I.getCompareOperand()),
3433 getValue(I.getNewValOperand()),
3434 MachinePointerInfo(I.getPointerOperand()), 0 /* Alignment */,
Eli Friedman327236c2011-08-24 20:50:09 +00003435 TLI.getInsertFencesForAtomic() ? Monotonic : Order,
3436 Scope);
Eli Friedman26689ac2011-08-03 21:06:02 +00003437
3438 SDValue OutChain = L.getValue(1);
3439
3440 if (TLI.getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003441 OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl,
3442 DAG, TLI);
Eli Friedman26689ac2011-08-03 21:06:02 +00003443
Eli Friedman55ba8162011-07-29 03:05:32 +00003444 setValue(&I, L);
Eli Friedman26689ac2011-08-03 21:06:02 +00003445 DAG.setRoot(OutChain);
Eli Friedmanff030482011-07-28 21:48:00 +00003446}
3447
3448void SelectionDAGBuilder::visitAtomicRMW(const AtomicRMWInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003449 SDLoc dl = getCurSDLoc();
Eli Friedman55ba8162011-07-29 03:05:32 +00003450 ISD::NodeType NT;
3451 switch (I.getOperation()) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003452 default: llvm_unreachable("Unknown atomicrmw operation");
Eli Friedman55ba8162011-07-29 03:05:32 +00003453 case AtomicRMWInst::Xchg: NT = ISD::ATOMIC_SWAP; break;
3454 case AtomicRMWInst::Add: NT = ISD::ATOMIC_LOAD_ADD; break;
3455 case AtomicRMWInst::Sub: NT = ISD::ATOMIC_LOAD_SUB; break;
3456 case AtomicRMWInst::And: NT = ISD::ATOMIC_LOAD_AND; break;
3457 case AtomicRMWInst::Nand: NT = ISD::ATOMIC_LOAD_NAND; break;
3458 case AtomicRMWInst::Or: NT = ISD::ATOMIC_LOAD_OR; break;
3459 case AtomicRMWInst::Xor: NT = ISD::ATOMIC_LOAD_XOR; break;
3460 case AtomicRMWInst::Max: NT = ISD::ATOMIC_LOAD_MAX; break;
3461 case AtomicRMWInst::Min: NT = ISD::ATOMIC_LOAD_MIN; break;
3462 case AtomicRMWInst::UMax: NT = ISD::ATOMIC_LOAD_UMAX; break;
3463 case AtomicRMWInst::UMin: NT = ISD::ATOMIC_LOAD_UMIN; break;
3464 }
Eli Friedman26689ac2011-08-03 21:06:02 +00003465 AtomicOrdering Order = I.getOrdering();
Eli Friedman327236c2011-08-24 20:50:09 +00003466 SynchronizationScope Scope = I.getSynchScope();
Eli Friedman26689ac2011-08-03 21:06:02 +00003467
3468 SDValue InChain = getRoot();
3469
3470 if (TLI.getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003471 InChain = InsertFenceForAtomic(InChain, Order, Scope, true, dl,
3472 DAG, TLI);
Eli Friedman26689ac2011-08-03 21:06:02 +00003473
Eli Friedman55ba8162011-07-29 03:05:32 +00003474 SDValue L =
Eli Friedman26689ac2011-08-03 21:06:02 +00003475 DAG.getAtomic(NT, dl,
Eli Friedman55ba8162011-07-29 03:05:32 +00003476 getValue(I.getValOperand()).getValueType().getSimpleVT(),
Eli Friedman26689ac2011-08-03 21:06:02 +00003477 InChain,
Eli Friedman55ba8162011-07-29 03:05:32 +00003478 getValue(I.getPointerOperand()),
3479 getValue(I.getValOperand()),
3480 I.getPointerOperand(), 0 /* Alignment */,
Eli Friedman26689ac2011-08-03 21:06:02 +00003481 TLI.getInsertFencesForAtomic() ? Monotonic : Order,
Eli Friedman327236c2011-08-24 20:50:09 +00003482 Scope);
Eli Friedman26689ac2011-08-03 21:06:02 +00003483
3484 SDValue OutChain = L.getValue(1);
3485
3486 if (TLI.getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003487 OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl,
3488 DAG, TLI);
Eli Friedman26689ac2011-08-03 21:06:02 +00003489
Eli Friedman55ba8162011-07-29 03:05:32 +00003490 setValue(&I, L);
Eli Friedman26689ac2011-08-03 21:06:02 +00003491 DAG.setRoot(OutChain);
Eli Friedmanff030482011-07-28 21:48:00 +00003492}
3493
Eli Friedman47f35132011-07-25 23:16:38 +00003494void SelectionDAGBuilder::visitFence(const FenceInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003495 SDLoc dl = getCurSDLoc();
Eli Friedman14648462011-07-27 22:21:52 +00003496 SDValue Ops[3];
3497 Ops[0] = getRoot();
3498 Ops[1] = DAG.getConstant(I.getOrdering(), TLI.getPointerTy());
3499 Ops[2] = DAG.getConstant(I.getSynchScope(), TLI.getPointerTy());
3500 DAG.setRoot(DAG.getNode(ISD::ATOMIC_FENCE, dl, MVT::Other, Ops, 3));
Eli Friedman47f35132011-07-25 23:16:38 +00003501}
3502
Eli Friedman327236c2011-08-24 20:50:09 +00003503void SelectionDAGBuilder::visitAtomicLoad(const LoadInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003504 SDLoc dl = getCurSDLoc();
Eli Friedman327236c2011-08-24 20:50:09 +00003505 AtomicOrdering Order = I.getOrdering();
3506 SynchronizationScope Scope = I.getSynchScope();
3507
3508 SDValue InChain = getRoot();
3509
Eli Friedmanfd45fa12012-08-17 23:24:29 +00003510 EVT VT = TLI.getValueType(I.getType());
Eli Friedman327236c2011-08-24 20:50:09 +00003511
Evan Cheng607acd62013-02-06 02:06:33 +00003512 if (I.getAlignment() < VT.getSizeInBits() / 8)
Eli Friedmanfe731212011-09-13 20:50:54 +00003513 report_fatal_error("Cannot generate unaligned atomic load");
3514
Eli Friedman327236c2011-08-24 20:50:09 +00003515 SDValue L =
3516 DAG.getAtomic(ISD::ATOMIC_LOAD, dl, VT, VT, InChain,
3517 getValue(I.getPointerOperand()),
3518 I.getPointerOperand(), I.getAlignment(),
3519 TLI.getInsertFencesForAtomic() ? Monotonic : Order,
3520 Scope);
3521
3522 SDValue OutChain = L.getValue(1);
3523
3524 if (TLI.getInsertFencesForAtomic())
3525 OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl,
3526 DAG, TLI);
3527
3528 setValue(&I, L);
3529 DAG.setRoot(OutChain);
3530}
3531
3532void SelectionDAGBuilder::visitAtomicStore(const StoreInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003533 SDLoc dl = getCurSDLoc();
Eli Friedman327236c2011-08-24 20:50:09 +00003534
3535 AtomicOrdering Order = I.getOrdering();
3536 SynchronizationScope Scope = I.getSynchScope();
3537
3538 SDValue InChain = getRoot();
3539
Eli Friedmanfd45fa12012-08-17 23:24:29 +00003540 EVT VT = TLI.getValueType(I.getValueOperand()->getType());
Eli Friedmanfe731212011-09-13 20:50:54 +00003541
Evan Cheng607acd62013-02-06 02:06:33 +00003542 if (I.getAlignment() < VT.getSizeInBits() / 8)
Eli Friedmanfe731212011-09-13 20:50:54 +00003543 report_fatal_error("Cannot generate unaligned atomic store");
3544
Eli Friedman327236c2011-08-24 20:50:09 +00003545 if (TLI.getInsertFencesForAtomic())
3546 InChain = InsertFenceForAtomic(InChain, Order, Scope, true, dl,
3547 DAG, TLI);
3548
3549 SDValue OutChain =
Eli Friedmanfe731212011-09-13 20:50:54 +00003550 DAG.getAtomic(ISD::ATOMIC_STORE, dl, VT,
Eli Friedman327236c2011-08-24 20:50:09 +00003551 InChain,
3552 getValue(I.getPointerOperand()),
3553 getValue(I.getValueOperand()),
3554 I.getPointerOperand(), I.getAlignment(),
3555 TLI.getInsertFencesForAtomic() ? Monotonic : Order,
3556 Scope);
3557
3558 if (TLI.getInsertFencesForAtomic())
3559 OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl,
3560 DAG, TLI);
3561
3562 DAG.setRoot(OutChain);
3563}
3564
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003565/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
3566/// node.
Dan Gohman46510a72010-04-15 01:51:59 +00003567void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I,
Dan Gohman2048b852009-11-23 18:04:58 +00003568 unsigned Intrinsic) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003569 bool HasChain = !I.doesNotAccessMemory();
3570 bool OnlyLoad = HasChain && I.onlyReadsMemory();
3571
3572 // Build the operand list.
3573 SmallVector<SDValue, 8> Ops;
3574 if (HasChain) { // If this intrinsic has side-effects, chainify it.
3575 if (OnlyLoad) {
3576 // We don't need to serialize loads against other loads.
3577 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003578 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003579 Ops.push_back(getRoot());
3580 }
3581 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003582
3583 // Info is set by getTgtMemInstrinsic
3584 TargetLowering::IntrinsicInfo Info;
3585 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic);
3586
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003587 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Bob Wilson65ffec42010-09-21 17:56:22 +00003588 if (!IsTgtIntrinsic || Info.opc == ISD::INTRINSIC_VOID ||
3589 Info.opc == ISD::INTRINSIC_W_CHAIN)
Pete Cooperbf421392012-01-16 04:08:12 +00003590 Ops.push_back(DAG.getTargetConstant(Intrinsic, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003591
3592 // Add all operands of the call to the operand list.
Gabor Greif0635f352010-06-25 09:38:13 +00003593 for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) {
3594 SDValue Op = getValue(I.getArgOperand(i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003595 Ops.push_back(Op);
3596 }
3597
Owen Andersone50ed302009-08-10 22:56:29 +00003598 SmallVector<EVT, 4> ValueVTs;
Bob Wilson8d919552009-07-31 22:41:21 +00003599 ComputeValueVTs(TLI, I.getType(), ValueVTs);
Bill Wendling856ff412009-12-22 00:12:37 +00003600
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003601 if (HasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00003602 ValueVTs.push_back(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003603
Bob Wilson8d919552009-07-31 22:41:21 +00003604 SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003605
3606 // Create the node.
3607 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003608 if (IsTgtIntrinsic) {
3609 // This is target intrinsic that touches memory
Andrew Trickac6d9be2013-05-25 02:42:55 +00003610 Result = DAG.getMemIntrinsicNode(Info.opc, getCurSDLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003611 VTs, &Ops[0], Ops.size(),
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00003612 Info.memVT,
3613 MachinePointerInfo(Info.ptrVal, Info.offset),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003614 Info.align, Info.vol,
3615 Info.readMem, Info.writeMem);
Bill Wendling856ff412009-12-22 00:12:37 +00003616 } else if (!HasChain) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003617 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurSDLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003618 VTs, &Ops[0], Ops.size());
Benjamin Kramerf0127052010-01-05 13:12:22 +00003619 } else if (!I.getType()->isVoidTy()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003620 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurSDLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003621 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003622 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003623 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003624 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003625 }
3626
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003627 if (HasChain) {
3628 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
3629 if (OnlyLoad)
3630 PendingLoads.push_back(Chain);
3631 else
3632 DAG.setRoot(Chain);
3633 }
Bill Wendling856ff412009-12-22 00:12:37 +00003634
Benjamin Kramerf0127052010-01-05 13:12:22 +00003635 if (!I.getType()->isVoidTy()) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003636 if (VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Owen Andersone50ed302009-08-10 22:56:29 +00003637 EVT VT = TLI.getValueType(PTy);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003638 Result = DAG.getNode(ISD::BITCAST, getCurSDLoc(), VT, Result);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003639 }
Bill Wendling856ff412009-12-22 00:12:37 +00003640
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003641 setValue(&I, Result);
3642 }
3643}
3644
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003645/// GetSignificand - Get the significand and build it into a floating-point
3646/// number with exponent of 1:
3647///
3648/// Op = (Op & 0x007fffff) | 0x3f800000;
3649///
Matt Beaumont-Gay50e75bf2013-02-25 18:11:18 +00003650/// where Op is the hexadecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003651static SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00003652GetSignificand(SelectionDAG &DAG, SDValue Op, SDLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003653 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3654 DAG.getConstant(0x007fffff, MVT::i32));
3655 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
3656 DAG.getConstant(0x3f800000, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003657 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00003658}
3659
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003660/// GetExponent - Get the exponent:
3661///
Bill Wendlinge9a72862009-01-20 21:17:57 +00003662/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003663///
Matt Beaumont-Gay50e75bf2013-02-25 18:11:18 +00003664/// where Op is the hexadecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003665static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00003666GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003667 SDLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003668 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3669 DAG.getConstant(0x7f800000, MVT::i32));
3670 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Duncan Sands92abc622009-01-31 15:50:11 +00003671 DAG.getConstant(23, TLI.getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00003672 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
3673 DAG.getConstant(127, MVT::i32));
Bill Wendling4533cac2010-01-28 21:51:40 +00003674 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00003675}
3676
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003677/// getF32Constant - Get 32-bit floating point constant.
3678static SDValue
3679getF32Constant(SelectionDAG &DAG, unsigned Flt) {
Tim Northover0a29cb02013-01-22 09:46:31 +00003680 return DAG.getConstantFP(APFloat(APFloat::IEEEsingle, APInt(32, Flt)),
3681 MVT::f32);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003682}
3683
Craig Topper538cd482012-11-24 18:52:06 +00003684/// expandExp - Lower an exp intrinsic. Handles the special sequences for
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003685/// limited-precision mode.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003686static SDValue expandExp(SDLoc dl, SDValue Op, SelectionDAG &DAG,
Craig Topper538cd482012-11-24 18:52:06 +00003687 const TargetLowering &TLI) {
3688 if (Op.getValueType() == MVT::f32 &&
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003689 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003690
3691 // Put the exponent in the right bit position for later addition to the
3692 // final result:
3693 //
3694 // #define LOG2OFe 1.4426950f
3695 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Owen Anderson825b72b2009-08-11 20:47:22 +00003696 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003697 getF32Constant(DAG, 0x3fb8aa3b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003698 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003699
3700 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003701 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3702 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003703
3704 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003705 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003706 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendling856ff412009-12-22 00:12:37 +00003707
Craig Topperb3157722012-11-24 08:22:37 +00003708 SDValue TwoToFracPartOfX;
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003709 if (LimitFloatPrecision <= 6) {
3710 // For floating-point precision of 6:
3711 //
3712 // TwoToFractionalPartOfX =
3713 // 0.997535578f +
3714 // (0.735607626f + 0.252464424f * x) * x;
3715 //
3716 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003717 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003718 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003719 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003720 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003721 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
Craig Topperb3157722012-11-24 08:22:37 +00003722 TwoToFracPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
3723 getF32Constant(DAG, 0x3f7f5e7e));
Craig Topper08ac4692012-11-16 20:01:39 +00003724 } else if (LimitFloatPrecision <= 12) {
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003725 // For floating-point precision of 12:
3726 //
3727 // TwoToFractionalPartOfX =
3728 // 0.999892986f +
3729 // (0.696457318f +
3730 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3731 //
3732 // 0.000107046256 error, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003733 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003734 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003735 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003736 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003737 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3738 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003739 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003740 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
Craig Topperb3157722012-11-24 08:22:37 +00003741 TwoToFracPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
3742 getF32Constant(DAG, 0x3f7ff8fd));
Craig Topper08ac4692012-11-16 20:01:39 +00003743 } else { // LimitFloatPrecision <= 18
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003744 // For floating-point precision of 18:
3745 //
3746 // TwoToFractionalPartOfX =
3747 // 0.999999982f +
3748 // (0.693148872f +
3749 // (0.240227044f +
3750 // (0.554906021e-1f +
3751 // (0.961591928e-2f +
3752 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3753 //
3754 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003755 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003756 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003757 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003758 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003759 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3760 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003761 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003762 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3763 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003764 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003765 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3766 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003767 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003768 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3769 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003770 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003771 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
Craig Topperb3157722012-11-24 08:22:37 +00003772 TwoToFracPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
3773 getF32Constant(DAG, 0x3f800000));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003774 }
Craig Topperb3157722012-11-24 08:22:37 +00003775
3776 // Add the exponent into the result in integer domain.
3777 SDValue t13 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, TwoToFracPartOfX);
Craig Topper538cd482012-11-24 18:52:06 +00003778 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3779 DAG.getNode(ISD::ADD, dl, MVT::i32,
3780 t13, IntegerPartOfX));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003781 }
3782
Craig Topper538cd482012-11-24 18:52:06 +00003783 // No special expansion.
3784 return DAG.getNode(ISD::FEXP, dl, Op.getValueType(), Op);
Dale Johannesen59e577f2008-09-05 18:38:42 +00003785}
3786
Craig Topper5d1e0892012-11-23 18:38:31 +00003787/// expandLog - Lower a log intrinsic. Handles the special sequences for
Bill Wendling39150252008-09-09 20:39:27 +00003788/// limited-precision mode.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003789static SDValue expandLog(SDLoc dl, SDValue Op, SelectionDAG &DAG,
Craig Topper5d1e0892012-11-23 18:38:31 +00003790 const TargetLowering &TLI) {
3791 if (Op.getValueType() == MVT::f32 &&
Bill Wendling39150252008-09-09 20:39:27 +00003792 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003793 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00003794
3795 // Scale the exponent by log(2) [0.69314718f].
Bill Wendling46ada192010-03-02 01:55:18 +00003796 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003797 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003798 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003799
3800 // Get the significand and build it into a floating-point number with
3801 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003802 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling39150252008-09-09 20:39:27 +00003803
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003804 SDValue LogOfMantissa;
Bill Wendling39150252008-09-09 20:39:27 +00003805 if (LimitFloatPrecision <= 6) {
3806 // For floating-point precision of 6:
3807 //
3808 // LogofMantissa =
3809 // -1.1609546f +
3810 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003811 //
Bill Wendling39150252008-09-09 20:39:27 +00003812 // error 0.0034276066, which is better than 8 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003813 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003814 getF32Constant(DAG, 0xbe74c456));
Owen Anderson825b72b2009-08-11 20:47:22 +00003815 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003816 getF32Constant(DAG, 0x3fb3a2b1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003817 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003818 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
3819 getF32Constant(DAG, 0x3f949a29));
Craig Topper08ac4692012-11-16 20:01:39 +00003820 } else if (LimitFloatPrecision <= 12) {
Bill Wendling39150252008-09-09 20:39:27 +00003821 // For floating-point precision of 12:
3822 //
3823 // LogOfMantissa =
3824 // -1.7417939f +
3825 // (2.8212026f +
3826 // (-1.4699568f +
3827 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3828 //
3829 // error 0.000061011436, which is 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003830 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003831 getF32Constant(DAG, 0xbd67b6d6));
Owen Anderson825b72b2009-08-11 20:47:22 +00003832 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003833 getF32Constant(DAG, 0x3ee4f4b8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003834 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3835 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003836 getF32Constant(DAG, 0x3fbc278b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003837 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3838 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003839 getF32Constant(DAG, 0x40348e95));
Owen Anderson825b72b2009-08-11 20:47:22 +00003840 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003841 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
3842 getF32Constant(DAG, 0x3fdef31a));
Craig Topper08ac4692012-11-16 20:01:39 +00003843 } else { // LimitFloatPrecision <= 18
Bill Wendling39150252008-09-09 20:39:27 +00003844 // For floating-point precision of 18:
3845 //
3846 // LogOfMantissa =
3847 // -2.1072184f +
3848 // (4.2372794f +
3849 // (-3.7029485f +
3850 // (2.2781945f +
3851 // (-0.87823314f +
3852 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3853 //
3854 // error 0.0000023660568, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003855 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003856 getF32Constant(DAG, 0xbc91e5ac));
Owen Anderson825b72b2009-08-11 20:47:22 +00003857 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003858 getF32Constant(DAG, 0x3e4350aa));
Owen Anderson825b72b2009-08-11 20:47:22 +00003859 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3860 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003861 getF32Constant(DAG, 0x3f60d3e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003862 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3863 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003864 getF32Constant(DAG, 0x4011cdf0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003865 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3866 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003867 getF32Constant(DAG, 0x406cfd1c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003868 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3869 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003870 getF32Constant(DAG, 0x408797cb));
Owen Anderson825b72b2009-08-11 20:47:22 +00003871 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003872 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
3873 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003874 }
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003875
Craig Topper5d1e0892012-11-23 18:38:31 +00003876 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003877 }
3878
Craig Topper5d1e0892012-11-23 18:38:31 +00003879 // No special expansion.
3880 return DAG.getNode(ISD::FLOG, dl, Op.getValueType(), Op);
Dale Johannesen59e577f2008-09-05 18:38:42 +00003881}
3882
Craig Topper5d1e0892012-11-23 18:38:31 +00003883/// expandLog2 - Lower a log2 intrinsic. Handles the special sequences for
Bill Wendling3eb59402008-09-09 00:28:24 +00003884/// limited-precision mode.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003885static SDValue expandLog2(SDLoc dl, SDValue Op, SelectionDAG &DAG,
Craig Topper5d1e0892012-11-23 18:38:31 +00003886 const TargetLowering &TLI) {
3887 if (Op.getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003888 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003889 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003890
Bill Wendling39150252008-09-09 20:39:27 +00003891 // Get the exponent.
Bill Wendling46ada192010-03-02 01:55:18 +00003892 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
Bill Wendling856ff412009-12-22 00:12:37 +00003893
Bill Wendling3eb59402008-09-09 00:28:24 +00003894 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003895 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003896 SDValue X = GetSignificand(DAG, Op1, dl);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003897
Bill Wendling3eb59402008-09-09 00:28:24 +00003898 // Different possible minimax approximations of significand in
3899 // floating-point for various degrees of accuracy over [1,2].
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003900 SDValue Log2ofMantissa;
Bill Wendling3eb59402008-09-09 00:28:24 +00003901 if (LimitFloatPrecision <= 6) {
3902 // For floating-point precision of 6:
3903 //
3904 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
3905 //
3906 // error 0.0049451742, which is more than 7 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003907 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003908 getF32Constant(DAG, 0xbeb08fe0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003909 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003910 getF32Constant(DAG, 0x40019463));
Owen Anderson825b72b2009-08-11 20:47:22 +00003911 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003912 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
3913 getF32Constant(DAG, 0x3fd6633d));
Craig Topper08ac4692012-11-16 20:01:39 +00003914 } else if (LimitFloatPrecision <= 12) {
Bill Wendling3eb59402008-09-09 00:28:24 +00003915 // For floating-point precision of 12:
3916 //
3917 // Log2ofMantissa =
3918 // -2.51285454f +
3919 // (4.07009056f +
3920 // (-2.12067489f +
3921 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003922 //
Bill Wendling3eb59402008-09-09 00:28:24 +00003923 // error 0.0000876136000, which is better than 13 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003924 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003925 getF32Constant(DAG, 0xbda7262e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003926 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003927 getF32Constant(DAG, 0x3f25280b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003928 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3929 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003930 getF32Constant(DAG, 0x4007b923));
Owen Anderson825b72b2009-08-11 20:47:22 +00003931 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3932 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003933 getF32Constant(DAG, 0x40823e2f));
Owen Anderson825b72b2009-08-11 20:47:22 +00003934 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003935 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
3936 getF32Constant(DAG, 0x4020d29c));
Craig Topper08ac4692012-11-16 20:01:39 +00003937 } else { // LimitFloatPrecision <= 18
Bill Wendling3eb59402008-09-09 00:28:24 +00003938 // For floating-point precision of 18:
3939 //
3940 // Log2ofMantissa =
3941 // -3.0400495f +
3942 // (6.1129976f +
3943 // (-5.3420409f +
3944 // (3.2865683f +
3945 // (-1.2669343f +
3946 // (0.27515199f -
3947 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
3948 //
3949 // error 0.0000018516, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003950 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003951 getF32Constant(DAG, 0xbcd2769e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003952 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003953 getF32Constant(DAG, 0x3e8ce0b9));
Owen Anderson825b72b2009-08-11 20:47:22 +00003954 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3955 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003956 getF32Constant(DAG, 0x3fa22ae7));
Owen Anderson825b72b2009-08-11 20:47:22 +00003957 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3958 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003959 getF32Constant(DAG, 0x40525723));
Owen Anderson825b72b2009-08-11 20:47:22 +00003960 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3961 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003962 getF32Constant(DAG, 0x40aaf200));
Owen Anderson825b72b2009-08-11 20:47:22 +00003963 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3964 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003965 getF32Constant(DAG, 0x40c39dad));
Owen Anderson825b72b2009-08-11 20:47:22 +00003966 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003967 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
3968 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003969 }
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003970
Craig Topper5d1e0892012-11-23 18:38:31 +00003971 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log2ofMantissa);
Dale Johannesen853244f2008-09-05 23:49:37 +00003972 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003973
Craig Topper5d1e0892012-11-23 18:38:31 +00003974 // No special expansion.
3975 return DAG.getNode(ISD::FLOG2, dl, Op.getValueType(), Op);
Dale Johannesen59e577f2008-09-05 18:38:42 +00003976}
3977
Craig Topper5d1e0892012-11-23 18:38:31 +00003978/// expandLog10 - Lower a log10 intrinsic. Handles the special sequences for
Bill Wendling3eb59402008-09-09 00:28:24 +00003979/// limited-precision mode.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003980static SDValue expandLog10(SDLoc dl, SDValue Op, SelectionDAG &DAG,
Craig Topper5d1e0892012-11-23 18:38:31 +00003981 const TargetLowering &TLI) {
3982 if (Op.getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003983 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003984 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003985
Bill Wendling39150252008-09-09 20:39:27 +00003986 // Scale the exponent by log10(2) [0.30102999f].
Bill Wendling46ada192010-03-02 01:55:18 +00003987 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003988 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003989 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00003990
3991 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003992 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003993 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling3eb59402008-09-09 00:28:24 +00003994
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003995 SDValue Log10ofMantissa;
Bill Wendling3eb59402008-09-09 00:28:24 +00003996 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003997 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003998 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003999 // Log10ofMantissa =
4000 // -0.50419619f +
4001 // (0.60948995f - 0.10380950f * x) * x;
4002 //
4003 // error 0.0014886165, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004004 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004005 getF32Constant(DAG, 0xbdd49a13));
Owen Anderson825b72b2009-08-11 20:47:22 +00004006 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004007 getF32Constant(DAG, 0x3f1c0789));
Owen Anderson825b72b2009-08-11 20:47:22 +00004008 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004009 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
4010 getF32Constant(DAG, 0x3f011300));
Craig Topper08ac4692012-11-16 20:01:39 +00004011 } else if (LimitFloatPrecision <= 12) {
Bill Wendling3eb59402008-09-09 00:28:24 +00004012 // For floating-point precision of 12:
4013 //
4014 // Log10ofMantissa =
4015 // -0.64831180f +
4016 // (0.91751397f +
4017 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
4018 //
4019 // error 0.00019228036, which is better than 12 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004020 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004021 getF32Constant(DAG, 0x3d431f31));
Owen Anderson825b72b2009-08-11 20:47:22 +00004022 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004023 getF32Constant(DAG, 0x3ea21fb2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004024 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
4025 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004026 getF32Constant(DAG, 0x3f6ae232));
Owen Anderson825b72b2009-08-11 20:47:22 +00004027 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004028 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
4029 getF32Constant(DAG, 0x3f25f7c3));
Craig Topper08ac4692012-11-16 20:01:39 +00004030 } else { // LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00004031 // For floating-point precision of 18:
4032 //
4033 // Log10ofMantissa =
4034 // -0.84299375f +
4035 // (1.5327582f +
4036 // (-1.0688956f +
4037 // (0.49102474f +
4038 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
4039 //
4040 // error 0.0000037995730, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004041 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004042 getF32Constant(DAG, 0x3c5d51ce));
Owen Anderson825b72b2009-08-11 20:47:22 +00004043 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004044 getF32Constant(DAG, 0x3e00685a));
Owen Anderson825b72b2009-08-11 20:47:22 +00004045 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
4046 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004047 getF32Constant(DAG, 0x3efb6798));
Owen Anderson825b72b2009-08-11 20:47:22 +00004048 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4049 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004050 getF32Constant(DAG, 0x3f88d192));
Owen Anderson825b72b2009-08-11 20:47:22 +00004051 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4052 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004053 getF32Constant(DAG, 0x3fc4316c));
Owen Anderson825b72b2009-08-11 20:47:22 +00004054 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004055 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
4056 getF32Constant(DAG, 0x3f57ce70));
Bill Wendling3eb59402008-09-09 00:28:24 +00004057 }
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004058
Craig Topper5d1e0892012-11-23 18:38:31 +00004059 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log10ofMantissa);
Dale Johannesen852680a2008-09-05 21:27:19 +00004060 }
Bill Wendling3eb59402008-09-09 00:28:24 +00004061
Craig Topper5d1e0892012-11-23 18:38:31 +00004062 // No special expansion.
4063 return DAG.getNode(ISD::FLOG10, dl, Op.getValueType(), Op);
Dale Johannesen59e577f2008-09-05 18:38:42 +00004064}
4065
Craig Topper538cd482012-11-24 18:52:06 +00004066/// expandExp2 - Lower an exp2 intrinsic. Handles the special sequences for
Bill Wendlinge10c8142008-09-09 22:39:21 +00004067/// limited-precision mode.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004068static SDValue expandExp2(SDLoc dl, SDValue Op, SelectionDAG &DAG,
Craig Topper538cd482012-11-24 18:52:06 +00004069 const TargetLowering &TLI) {
4070 if (Op.getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00004071 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004072 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00004073
4074 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00004075 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
4076 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00004077
4078 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00004079 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00004080 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00004081
Craig Topperb3157722012-11-24 08:22:37 +00004082 SDValue TwoToFractionalPartOfX;
Bill Wendlinge10c8142008-09-09 22:39:21 +00004083 if (LimitFloatPrecision <= 6) {
4084 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004085 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00004086 // TwoToFractionalPartOfX =
4087 // 0.997535578f +
4088 // (0.735607626f + 0.252464424f * x) * x;
4089 //
4090 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004091 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004092 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00004093 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004094 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004095 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
Craig Topperb3157722012-11-24 08:22:37 +00004096 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
4097 getF32Constant(DAG, 0x3f7f5e7e));
Craig Topper08ac4692012-11-16 20:01:39 +00004098 } else if (LimitFloatPrecision <= 12) {
Bill Wendlinge10c8142008-09-09 22:39:21 +00004099 // For floating-point precision of 12:
4100 //
4101 // TwoToFractionalPartOfX =
4102 // 0.999892986f +
4103 // (0.696457318f +
4104 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
4105 //
4106 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004107 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004108 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004109 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004110 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004111 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4112 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004113 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00004114 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
Craig Topperb3157722012-11-24 08:22:37 +00004115 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
4116 getF32Constant(DAG, 0x3f7ff8fd));
Craig Topper08ac4692012-11-16 20:01:39 +00004117 } else { // LimitFloatPrecision <= 18
Bill Wendlinge10c8142008-09-09 22:39:21 +00004118 // For floating-point precision of 18:
4119 //
4120 // TwoToFractionalPartOfX =
4121 // 0.999999982f +
4122 // (0.693148872f +
4123 // (0.240227044f +
4124 // (0.554906021e-1f +
4125 // (0.961591928e-2f +
4126 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
4127 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004128 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004129 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004130 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004131 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00004132 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4133 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004134 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00004135 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4136 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004137 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00004138 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
4139 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004140 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00004141 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
4142 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004143 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00004144 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
Craig Topperb3157722012-11-24 08:22:37 +00004145 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
4146 getF32Constant(DAG, 0x3f800000));
Bill Wendlinge10c8142008-09-09 22:39:21 +00004147 }
Craig Topperb3157722012-11-24 08:22:37 +00004148
4149 // Add the exponent into the result in integer domain.
4150 SDValue t13 = DAG.getNode(ISD::BITCAST, dl, MVT::i32,
4151 TwoToFractionalPartOfX);
Craig Topper538cd482012-11-24 18:52:06 +00004152 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
4153 DAG.getNode(ISD::ADD, dl, MVT::i32,
4154 t13, IntegerPartOfX));
Dale Johannesen601d3c02008-09-05 01:48:15 +00004155 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00004156
Craig Topper538cd482012-11-24 18:52:06 +00004157 // No special expansion.
4158 return DAG.getNode(ISD::FEXP2, dl, Op.getValueType(), Op);
Dale Johannesen601d3c02008-09-05 01:48:15 +00004159}
4160
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004161/// visitPow - Lower a pow intrinsic. Handles the special sequences for
4162/// limited-precision mode with x == 10.0f.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004163static SDValue expandPow(SDLoc dl, SDValue LHS, SDValue RHS,
Craig Topper327e4cb2012-11-25 08:08:58 +00004164 SelectionDAG &DAG, const TargetLowering &TLI) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004165 bool IsExp10 = false;
Craig Topper327e4cb2012-11-25 08:08:58 +00004166 if (LHS.getValueType() == MVT::f32 && LHS.getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004167 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Craig Topper327e4cb2012-11-25 08:08:58 +00004168 if (ConstantFPSDNode *LHSC = dyn_cast<ConstantFPSDNode>(LHS)) {
4169 APFloat Ten(10.0f);
4170 IsExp10 = LHSC->isExactlyValue(Ten);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004171 }
4172 }
4173
Craig Topperc1aa6382012-11-25 00:48:58 +00004174 if (IsExp10) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004175 // Put the exponent in the right bit position for later addition to the
4176 // final result:
4177 //
4178 // #define LOG2OF10 3.3219281f
4179 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Craig Topper327e4cb2012-11-25 08:08:58 +00004180 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, RHS,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004181 getF32Constant(DAG, 0x40549a78));
Owen Anderson825b72b2009-08-11 20:47:22 +00004182 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004183
4184 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00004185 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
4186 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004187
4188 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00004189 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00004190 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004191
Craig Topper915562e2012-11-25 00:15:07 +00004192 SDValue TwoToFractionalPartOfX;
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004193 if (LimitFloatPrecision <= 6) {
4194 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004195 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004196 // twoToFractionalPartOfX =
4197 // 0.997535578f +
4198 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004199 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004200 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004201 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004202 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00004203 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004204 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004205 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
Craig Topper915562e2012-11-25 00:15:07 +00004206 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
4207 getF32Constant(DAG, 0x3f7f5e7e));
Craig Topper08ac4692012-11-16 20:01:39 +00004208 } else if (LimitFloatPrecision <= 12) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004209 // For floating-point precision of 12:
4210 //
4211 // TwoToFractionalPartOfX =
4212 // 0.999892986f +
4213 // (0.696457318f +
4214 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
4215 //
4216 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004217 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004218 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004219 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004220 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004221 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4222 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004223 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00004224 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
Craig Topper915562e2012-11-25 00:15:07 +00004225 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
4226 getF32Constant(DAG, 0x3f7ff8fd));
Craig Topper08ac4692012-11-16 20:01:39 +00004227 } else { // LimitFloatPrecision <= 18
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004228 // For floating-point precision of 18:
4229 //
4230 // TwoToFractionalPartOfX =
4231 // 0.999999982f +
4232 // (0.693148872f +
4233 // (0.240227044f +
4234 // (0.554906021e-1f +
4235 // (0.961591928e-2f +
4236 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
4237 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004238 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004239 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004240 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004241 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00004242 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4243 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004244 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00004245 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4246 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004247 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00004248 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
4249 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004250 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00004251 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
4252 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004253 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00004254 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
Craig Topper915562e2012-11-25 00:15:07 +00004255 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
4256 getF32Constant(DAG, 0x3f800000));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004257 }
Craig Topper915562e2012-11-25 00:15:07 +00004258
4259 SDValue t13 = DAG.getNode(ISD::BITCAST, dl,MVT::i32,TwoToFractionalPartOfX);
Craig Topper327e4cb2012-11-25 08:08:58 +00004260 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
4261 DAG.getNode(ISD::ADD, dl, MVT::i32,
4262 t13, IntegerPartOfX));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004263 }
4264
Craig Topper327e4cb2012-11-25 08:08:58 +00004265 // No special expansion.
4266 return DAG.getNode(ISD::FPOW, dl, LHS.getValueType(), LHS, RHS);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004267}
4268
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004269
4270/// ExpandPowI - Expand a llvm.powi intrinsic.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004271static SDValue ExpandPowI(SDLoc DL, SDValue LHS, SDValue RHS,
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004272 SelectionDAG &DAG) {
4273 // If RHS is a constant, we can expand this out to a multiplication tree,
4274 // otherwise we end up lowering to a call to __powidf2 (for example). When
4275 // optimizing for size, we only want to do this if the expansion would produce
4276 // a small number of multiplies, otherwise we do the full expansion.
4277 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
4278 // Get the exponent as a positive value.
4279 unsigned Val = RHSC->getSExtValue();
4280 if ((int)Val < 0) Val = -Val;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004281
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004282 // powi(x, 0) -> 1.0
4283 if (Val == 0)
4284 return DAG.getConstantFP(1.0, LHS.getValueType());
4285
Dan Gohmanae541aa2010-04-15 04:33:49 +00004286 const Function *F = DAG.getMachineFunction().getFunction();
Bill Wendling831737d2012-12-30 10:32:01 +00004287 if (!F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
4288 Attribute::OptimizeForSize) ||
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004289 // If optimizing for size, don't insert too many multiplies. This
4290 // inserts up to 5 multiplies.
4291 CountPopulation_32(Val)+Log2_32(Val) < 7) {
4292 // We use the simple binary decomposition method to generate the multiply
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004293 // sequence. There are more optimal ways to do this (for example,
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004294 // powi(x,15) generates one more multiply than it should), but this has
4295 // the benefit of being both really simple and much better than a libcall.
4296 SDValue Res; // Logically starts equal to 1.0
4297 SDValue CurSquare = LHS;
4298 while (Val) {
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00004299 if (Val & 1) {
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004300 if (Res.getNode())
4301 Res = DAG.getNode(ISD::FMUL, DL,Res.getValueType(), Res, CurSquare);
4302 else
4303 Res = CurSquare; // 1.0*CurSquare.
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00004304 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004305
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004306 CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
4307 CurSquare, CurSquare);
4308 Val >>= 1;
4309 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004310
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004311 // If the original was negative, invert the result, producing 1/(x*x*x).
4312 if (RHSC->getSExtValue() < 0)
4313 Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(),
4314 DAG.getConstantFP(1.0, LHS.getValueType()), Res);
4315 return Res;
4316 }
4317 }
4318
4319 // Otherwise, expand to a libcall.
4320 return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS);
4321}
4322
Devang Patel227dfdb2011-05-16 21:24:05 +00004323// getTruncatedArgReg - Find underlying register used for an truncated
4324// argument.
4325static unsigned getTruncatedArgReg(const SDValue &N) {
4326 if (N.getOpcode() != ISD::TRUNCATE)
4327 return 0;
4328
4329 const SDValue &Ext = N.getOperand(0);
4330 if (Ext.getOpcode() == ISD::AssertZext || Ext.getOpcode() == ISD::AssertSext){
4331 const SDValue &CFR = Ext.getOperand(0);
4332 if (CFR.getOpcode() == ISD::CopyFromReg)
4333 return cast<RegisterSDNode>(CFR.getOperand(1))->getReg();
Craig Topper7eb46d82012-04-11 04:55:51 +00004334 if (CFR.getOpcode() == ISD::TRUNCATE)
4335 return getTruncatedArgReg(CFR);
Devang Patel227dfdb2011-05-16 21:24:05 +00004336 }
4337 return 0;
4338}
4339
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004340/// EmitFuncArgumentDbgValue - If the DbgValueInst is a dbg_value of a function
4341/// argument, create the corresponding DBG_VALUE machine instruction for it now.
4342/// At the end of instruction selection, they will be inserted to the entry BB.
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004343bool
Devang Patel78a06e52010-08-25 20:39:26 +00004344SelectionDAGBuilder::EmitFuncArgumentDbgValue(const Value *V, MDNode *Variable,
Michael J. Spencere70c5262010-10-16 08:25:21 +00004345 int64_t Offset,
Dan Gohman5d11ea32010-05-01 00:33:16 +00004346 const SDValue &N) {
Devang Patel0b48ead2010-08-31 22:22:42 +00004347 const Argument *Arg = dyn_cast<Argument>(V);
4348 if (!Arg)
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004349 return false;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004350
Devang Patel719f6a92010-04-29 20:40:36 +00004351 MachineFunction &MF = DAG.getMachineFunction();
Devang Patela90b3052010-11-02 17:01:30 +00004352 const TargetInstrInfo *TII = DAG.getTarget().getInstrInfo();
Devang Patela90b3052010-11-02 17:01:30 +00004353
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
David Blaikie6d9dbd52013-06-16 20:34:15 +00004359 Optional<MachineOperand> Op;
Devang Patel9aee3352011-09-08 22:59:09 +00004360 // Some arguments' frame index is recorded during argument lowering.
David Blaikie6d9dbd52013-06-16 20:34:15 +00004361 if (int FI = FuncInfo.getArgumentFrameIndex(Arg))
4362 Op = MachineOperand::CreateFI(FI);
Devang Patel0b48ead2010-08-31 22:22:42 +00004363
David Blaikie6d9dbd52013-06-16 20:34:15 +00004364 if (!Op && N.getNode()) {
4365 unsigned Reg;
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 }
David Blaikie6d9dbd52013-06-16 20:34:15 +00004376 if (Reg)
4377 Op = MachineOperand::CreateReg(Reg, false);
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004378 }
4379
David Blaikie6d9dbd52013-06-16 20:34:15 +00004380 if (!Op) {
Devang Patela90b3052010-11-02 17:01:30 +00004381 // Check if ValueMap has reg number.
Evan Chenga36acad2010-04-29 06:33:38 +00004382 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
Devang Patel8bc9ef72010-11-02 17:19:03 +00004383 if (VMI != FuncInfo.ValueMap.end())
David Blaikie6d9dbd52013-06-16 20:34:15 +00004384 Op = MachineOperand::CreateReg(VMI->second, false);
Evan Chenga36acad2010-04-29 06:33:38 +00004385 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004386
David Blaikie6d9dbd52013-06-16 20:34:15 +00004387 if (!Op && N.getNode())
Devang Patela90b3052010-11-02 17:01:30 +00004388 // Check if frame index is available.
4389 if (LoadSDNode *LNode = dyn_cast<LoadSDNode>(N.getNode()))
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004390 if (FrameIndexSDNode *FINode =
David Blaikie6d9dbd52013-06-16 20:34:15 +00004391 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
4392 Op = MachineOperand::CreateFI(FINode->getIndex());
Devang Patel8bc9ef72010-11-02 17:19:03 +00004393
David Blaikie6d9dbd52013-06-16 20:34:15 +00004394 if (!Op)
Devang Patel8bc9ef72010-11-02 17:19:03 +00004395 return false;
Devang Patela90b3052010-11-02 17:01:30 +00004396
David Blaikie6d9dbd52013-06-16 20:34:15 +00004397 if (Op->isReg())
4398 Op->setIsDebug();
4399
4400 FuncInfo.ArgDbgValues.push_back(
4401 BuildMI(MF, getCurDebugLoc(), TII->get(TargetOpcode::DBG_VALUE))
4402 .addOperand(*Op).addImm(Offset).addMetadata(Variable));
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004403 return true;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004404}
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004405
Douglas Gregor7d9663c2010-05-11 06:17:44 +00004406// VisualStudio defines setjmp as _setjmp
Michael J. Spencer1f409602010-09-24 19:48:47 +00004407#if defined(_MSC_VER) && defined(setjmp) && \
4408 !defined(setjmp_undefined_for_msvc)
4409# pragma push_macro("setjmp")
4410# undef setjmp
4411# define setjmp_undefined_for_msvc
Douglas Gregor7d9663c2010-05-11 06:17:44 +00004412#endif
4413
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004414/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
4415/// we want to emit this as a call to a named external function, return the name
4416/// otherwise lower it and return null.
4417const char *
Dan Gohman46510a72010-04-15 01:51:59 +00004418SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, unsigned Intrinsic) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004419 SDLoc sdl = getCurSDLoc();
Dale Johannesen66978ee2009-01-31 02:22:37 +00004420 DebugLoc dl = getCurDebugLoc();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004421 SDValue Res;
4422
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004423 switch (Intrinsic) {
4424 default:
4425 // By default, turn this into a target intrinsic node.
4426 visitTargetIntrinsic(I, Intrinsic);
4427 return 0;
4428 case Intrinsic::vastart: visitVAStart(I); return 0;
4429 case Intrinsic::vaend: visitVAEnd(I); return 0;
4430 case Intrinsic::vacopy: visitVACopy(I); return 0;
4431 case Intrinsic::returnaddress:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004432 setValue(&I, DAG.getNode(ISD::RETURNADDR, sdl, TLI.getPointerTy(),
Gabor Greif0635f352010-06-25 09:38:13 +00004433 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004434 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00004435 case Intrinsic::frameaddress:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004436 setValue(&I, DAG.getNode(ISD::FRAMEADDR, sdl, TLI.getPointerTy(),
Gabor Greif0635f352010-06-25 09:38:13 +00004437 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004438 return 0;
4439 case Intrinsic::setjmp:
Bill Wendlingc27facc2012-03-05 19:29:36 +00004440 return &"_setjmp"[!TLI.usesUnderscoreSetJmp()];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004441 case Intrinsic::longjmp:
Bill Wendlingc27facc2012-03-05 19:29:36 +00004442 return &"_longjmp"[!TLI.usesUnderscoreLongJmp()];
Chris Lattner824b9582008-11-21 16:42:48 +00004443 case Intrinsic::memcpy: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00004444 // Assert for address < 256 since we support only user defined address
4445 // spaces.
Gabor Greif0635f352010-06-25 09:38:13 +00004446 assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004447 < 256 &&
Gabor Greif0635f352010-06-25 09:38:13 +00004448 cast<PointerType>(I.getArgOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004449 < 256 &&
4450 "Unknown address space");
Gabor Greif0635f352010-06-25 09:38:13 +00004451 SDValue Op1 = getValue(I.getArgOperand(0));
4452 SDValue Op2 = getValue(I.getArgOperand(1));
4453 SDValue Op3 = getValue(I.getArgOperand(2));
4454 unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
Chandler Carruthaf23f8e2013-02-25 14:20:21 +00004455 if (!Align)
4456 Align = 1; // @llvm.memcpy defines 0 and 1 to both mean no alignment.
Gabor Greif0635f352010-06-25 09:38:13 +00004457 bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004458 DAG.setRoot(DAG.getMemcpy(getRoot(), sdl, Op1, Op2, Op3, Align, isVol, false,
Chris Lattnere72f2022010-09-21 05:40:29 +00004459 MachinePointerInfo(I.getArgOperand(0)),
4460 MachinePointerInfo(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004461 return 0;
4462 }
Chris Lattner824b9582008-11-21 16:42:48 +00004463 case Intrinsic::memset: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00004464 // Assert for address < 256 since we support only user defined address
4465 // spaces.
Gabor Greif0635f352010-06-25 09:38:13 +00004466 assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004467 < 256 &&
4468 "Unknown address space");
Gabor Greif0635f352010-06-25 09:38:13 +00004469 SDValue Op1 = getValue(I.getArgOperand(0));
4470 SDValue Op2 = getValue(I.getArgOperand(1));
4471 SDValue Op3 = getValue(I.getArgOperand(2));
4472 unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
Chandler Carruthaf23f8e2013-02-25 14:20:21 +00004473 if (!Align)
4474 Align = 1; // @llvm.memset defines 0 and 1 to both mean no alignment.
Gabor Greif0635f352010-06-25 09:38:13 +00004475 bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004476 DAG.setRoot(DAG.getMemset(getRoot(), sdl, Op1, Op2, Op3, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004477 MachinePointerInfo(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004478 return 0;
4479 }
Chris Lattner824b9582008-11-21 16:42:48 +00004480 case Intrinsic::memmove: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00004481 // Assert for address < 256 since we support only user defined address
4482 // spaces.
Gabor Greif0635f352010-06-25 09:38:13 +00004483 assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004484 < 256 &&
Gabor Greif0635f352010-06-25 09:38:13 +00004485 cast<PointerType>(I.getArgOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004486 < 256 &&
4487 "Unknown address space");
Gabor Greif0635f352010-06-25 09:38:13 +00004488 SDValue Op1 = getValue(I.getArgOperand(0));
4489 SDValue Op2 = getValue(I.getArgOperand(1));
4490 SDValue Op3 = getValue(I.getArgOperand(2));
4491 unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
Chandler Carruthaf23f8e2013-02-25 14:20:21 +00004492 if (!Align)
4493 Align = 1; // @llvm.memmove defines 0 and 1 to both mean no alignment.
Gabor Greif0635f352010-06-25 09:38:13 +00004494 bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004495 DAG.setRoot(DAG.getMemmove(getRoot(), sdl, Op1, Op2, Op3, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004496 MachinePointerInfo(I.getArgOperand(0)),
4497 MachinePointerInfo(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004498 return 0;
4499 }
Bill Wendling92c1e122009-02-13 02:16:35 +00004500 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00004501 const DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
Devang Patelac1ceb32009-10-09 22:42:28 +00004502 MDNode *Variable = DI.getVariable();
Dan Gohman46510a72010-04-15 01:51:59 +00004503 const Value *Address = DI.getAddress();
Eric Christopher8f2a88d2012-03-15 21:33:41 +00004504 if (!Address || !DIVariable(Variable).Verify()) {
4505 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
Dale Johannesen8ac38f22010-02-08 21:53:27 +00004506 return 0;
Eric Christopher8f2a88d2012-03-15 21:33:41 +00004507 }
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004508
Devang Patel3f74a112010-09-02 21:29:42 +00004509 // Check if address has undef value.
4510 if (isa<UndefValue>(Address) ||
4511 (Address->use_empty() && !isa<Argument>(Address))) {
Eric Christopher24413672012-02-23 03:39:39 +00004512 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
Devang Patel3f74a112010-09-02 21:29:42 +00004513 return 0;
4514 }
4515
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004516 SDValue &N = NodeMap[Address];
Devang Patel0b48ead2010-08-31 22:22:42 +00004517 if (!N.getNode() && isa<Argument>(Address))
4518 // Check unused arguments map.
4519 N = UnusedArgNodeMap[Address];
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004520 SDDbgValue *SDV;
4521 if (N.getNode()) {
Devang Patel8e741ed2010-09-02 21:02:27 +00004522 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
4523 Address = BCI->getOperand(0);
Eric Christopher178606d2012-02-24 01:59:08 +00004524 // Parameters are handled specially.
4525 bool isParameter =
4526 (DIVariable(Variable).getTag() == dwarf::DW_TAG_arg_variable ||
4527 isa<Argument>(Address));
4528
Devang Patel8e741ed2010-09-02 21:02:27 +00004529 const AllocaInst *AI = dyn_cast<AllocaInst>(Address);
4530
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004531 if (isParameter && !AI) {
4532 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N.getNode());
4533 if (FINode)
4534 // Byval parameter. We have a frame index at this point.
4535 SDV = DAG.getDbgValue(Variable, FINode->getIndex(),
4536 0, dl, SDNodeOrder);
Devang Patelafeaae72010-12-06 22:39:26 +00004537 else {
Devang Patel227dfdb2011-05-16 21:24:05 +00004538 // Address is an argument, so try to emit its dbg value using
4539 // virtual register info from the FuncInfo.ValueMap.
4540 EmitFuncArgumentDbgValue(Address, Variable, 0, N);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004541 return 0;
Devang Patelafeaae72010-12-06 22:39:26 +00004542 }
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004543 } else if (AI)
4544 SDV = DAG.getDbgValue(Variable, N.getNode(), N.getResNo(),
4545 0, dl, SDNodeOrder);
Devang Patelafeaae72010-12-06 22:39:26 +00004546 else {
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004547 // Can't do anything with other non-AI cases yet.
Eric Christopher24413672012-02-23 03:39:39 +00004548 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
Eric Christopher178606d2012-02-24 01:59:08 +00004549 DEBUG(dbgs() << "non-AllocaInst issue for Address: \n\t");
4550 DEBUG(Address->dump());
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004551 return 0;
Devang Patelafeaae72010-12-06 22:39:26 +00004552 }
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004553 DAG.AddDbgValue(SDV, N.getNode(), isParameter);
4554 } else {
Gabor Greiffb4032f2010-10-01 10:32:19 +00004555 // If Address is an argument then try to emit its dbg value using
Michael J. Spencere70c5262010-10-16 08:25:21 +00004556 // virtual register info from the FuncInfo.ValueMap.
Devang Patel6cd467b2010-08-26 22:53:27 +00004557 if (!EmitFuncArgumentDbgValue(Address, Variable, 0, N)) {
Devang Patel1397fdc2010-09-15 14:48:53 +00004558 // If variable is pinned by a alloca in dominating bb then
4559 // use StaticAllocaMap.
4560 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Address)) {
Devang Patel27ede1b2010-09-15 18:13:55 +00004561 if (AI->getParent() != DI.getParent()) {
4562 DenseMap<const AllocaInst*, int>::iterator SI =
4563 FuncInfo.StaticAllocaMap.find(AI);
4564 if (SI != FuncInfo.StaticAllocaMap.end()) {
4565 SDV = DAG.getDbgValue(Variable, SI->second,
4566 0, dl, SDNodeOrder);
4567 DAG.AddDbgValue(SDV, 0, false);
4568 return 0;
4569 }
Devang Patel1397fdc2010-09-15 14:48:53 +00004570 }
4571 }
Eric Christopher0822e012012-02-23 03:39:43 +00004572 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
Devang Patel6cd467b2010-08-26 22:53:27 +00004573 }
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004574 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004575 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00004576 }
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004577 case Intrinsic::dbg_value: {
Dan Gohman46510a72010-04-15 01:51:59 +00004578 const DbgValueInst &DI = cast<DbgValueInst>(I);
Devang Patel02f0dbd2010-05-07 22:04:20 +00004579 if (!DIVariable(DI.getVariable()).Verify())
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004580 return 0;
4581
4582 MDNode *Variable = DI.getVariable();
Devang Patel00190342010-03-15 19:15:44 +00004583 uint64_t Offset = DI.getOffset();
Dan Gohman46510a72010-04-15 01:51:59 +00004584 const Value *V = DI.getValue();
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004585 if (!V)
4586 return 0;
Devang Patel00190342010-03-15 19:15:44 +00004587
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004588 SDDbgValue *SDV;
Devang Patel57871242011-08-03 23:13:55 +00004589 if (isa<ConstantInt>(V) || isa<ConstantFP>(V) || isa<UndefValue>(V)) {
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004590 SDV = DAG.getDbgValue(Variable, V, Offset, dl, SDNodeOrder);
4591 DAG.AddDbgValue(SDV, 0, false);
Devang Patel00190342010-03-15 19:15:44 +00004592 } else {
Dale Johannesenbdc09d92010-07-16 00:02:08 +00004593 // Do not use getValue() in here; we don't want to generate code at
4594 // this point if it hasn't been done yet.
Devang Patel9126c0d2010-06-01 19:59:01 +00004595 SDValue N = NodeMap[V];
4596 if (!N.getNode() && isa<Argument>(V))
4597 // Check unused arguments map.
4598 N = UnusedArgNodeMap[V];
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004599 if (N.getNode()) {
Devang Patel78a06e52010-08-25 20:39:26 +00004600 if (!EmitFuncArgumentDbgValue(V, Variable, Offset, N)) {
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004601 SDV = DAG.getDbgValue(Variable, N.getNode(),
4602 N.getResNo(), Offset, dl, SDNodeOrder);
4603 DAG.AddDbgValue(SDV, N.getNode(), false);
4604 }
Devang Patela778f5c2011-02-18 22:43:42 +00004605 } else if (!V->use_empty() ) {
Dale Johannesenbdc09d92010-07-16 00:02:08 +00004606 // Do not call getValue(V) yet, as we don't want to generate code.
4607 // Remember it for later.
4608 DanglingDebugInfo DDI(&DI, dl, SDNodeOrder);
4609 DanglingDebugInfoMap[V] = DDI;
Devang Patel0991dfb2010-08-27 22:25:51 +00004610 } else {
Devang Patel00190342010-03-15 19:15:44 +00004611 // We may expand this to cover more cases. One case where we have no
Devang Patelafeaae72010-12-06 22:39:26 +00004612 // data available is an unreferenced parameter.
Eric Christopher0822e012012-02-23 03:39:43 +00004613 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004614 }
Devang Patel00190342010-03-15 19:15:44 +00004615 }
4616
4617 // Build a debug info table entry.
Dan Gohman46510a72010-04-15 01:51:59 +00004618 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(V))
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004619 V = BCI->getOperand(0);
Dan Gohman46510a72010-04-15 01:51:59 +00004620 const AllocaInst *AI = dyn_cast<AllocaInst>(V);
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004621 // Don't handle byval struct arguments or VLAs, for example.
Eric Christopher7e1e18f2012-03-26 06:10:32 +00004622 if (!AI) {
Eric Christopher9fc5c832012-03-28 07:34:36 +00004623 DEBUG(dbgs() << "Dropping debug location info for:\n " << DI << "\n");
4624 DEBUG(dbgs() << " Last seen at:\n " << *V << "\n");
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004625 return 0;
Eric Christopher7e1e18f2012-03-26 06:10:32 +00004626 }
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004627 DenseMap<const AllocaInst*, int>::iterator SI =
4628 FuncInfo.StaticAllocaMap.find(AI);
4629 if (SI == FuncInfo.StaticAllocaMap.end())
4630 return 0; // VLAs.
4631 int FI = SI->second;
Michael J. Spencere70c5262010-10-16 08:25:21 +00004632
Chris Lattner512063d2010-04-05 06:19:28 +00004633 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
4634 if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo())
4635 MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc());
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004636 return 0;
4637 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004638
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004639 case Intrinsic::eh_typeid_for: {
Chris Lattner512063d2010-04-05 06:19:28 +00004640 // Find the type id for the given typeinfo.
Gabor Greif0635f352010-06-25 09:38:13 +00004641 GlobalVariable *GV = ExtractTypeInfo(I.getArgOperand(0));
Chris Lattner512063d2010-04-05 06:19:28 +00004642 unsigned TypeID = DAG.getMachineFunction().getMMI().getTypeIDFor(GV);
4643 Res = DAG.getConstant(TypeID, MVT::i32);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004644 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004645 return 0;
4646 }
4647
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004648 case Intrinsic::eh_return_i32:
4649 case Intrinsic::eh_return_i64:
Chris Lattner512063d2010-04-05 06:19:28 +00004650 DAG.getMachineFunction().getMMI().setCallsEHReturn(true);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004651 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, sdl,
Chris Lattner512063d2010-04-05 06:19:28 +00004652 MVT::Other,
4653 getControlRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00004654 getValue(I.getArgOperand(0)),
4655 getValue(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004656 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004657 case Intrinsic::eh_unwind_init:
Chris Lattner512063d2010-04-05 06:19:28 +00004658 DAG.getMachineFunction().getMMI().setCallsUnwindInit(true);
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004659 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004660 case Intrinsic::eh_dwarf_cfa: {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004661 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getArgOperand(0)), sdl,
Duncan Sands3a66a682009-10-13 21:04:12 +00004662 TLI.getPointerTy());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004663 SDValue Offset = DAG.getNode(ISD::ADD, sdl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004664 TLI.getPointerTy(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004665 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, sdl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004666 TLI.getPointerTy()),
4667 CfaArg);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004668 SDValue FA = DAG.getNode(ISD::FRAMEADDR, sdl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004669 TLI.getPointerTy(),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004670 DAG.getConstant(0, TLI.getPointerTy()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004671 setValue(&I, DAG.getNode(ISD::ADD, sdl, TLI.getPointerTy(),
Bill Wendling4533cac2010-01-28 21:51:40 +00004672 FA, Offset));
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004673 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004674 }
Jim Grosbachca752c92010-01-28 01:45:32 +00004675 case Intrinsic::eh_sjlj_callsite: {
Chris Lattner512063d2010-04-05 06:19:28 +00004676 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Gabor Greif0635f352010-06-25 09:38:13 +00004677 ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(0));
Jim Grosbachca752c92010-01-28 01:45:32 +00004678 assert(CI && "Non-constant call site value in eh.sjlj.callsite!");
Chris Lattner512063d2010-04-05 06:19:28 +00004679 assert(MMI.getCurrentCallSite() == 0 && "Overlapping call sites!");
Jim Grosbachca752c92010-01-28 01:45:32 +00004680
Chris Lattner512063d2010-04-05 06:19:28 +00004681 MMI.setCurrentCallSite(CI->getZExtValue());
Jim Grosbachca752c92010-01-28 01:45:32 +00004682 return 0;
4683 }
Bill Wendling6ef94172011-09-28 03:36:43 +00004684 case Intrinsic::eh_sjlj_functioncontext: {
4685 // Get and store the index of the function context.
4686 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bill Wendlingadbf7b22011-09-28 03:52:41 +00004687 AllocaInst *FnCtx =
4688 cast<AllocaInst>(I.getArgOperand(0)->stripPointerCasts());
Bill Wendling6ef94172011-09-28 03:36:43 +00004689 int FI = FuncInfo.StaticAllocaMap[FnCtx];
4690 MFI->setFunctionContextIndex(FI);
4691 return 0;
4692 }
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004693 case Intrinsic::eh_sjlj_setjmp: {
Bill Wendlingce370cf2011-10-07 21:25:38 +00004694 SDValue Ops[2];
4695 Ops[0] = getRoot();
4696 Ops[1] = getValue(I.getArgOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004697 SDValue Op = DAG.getNode(ISD::EH_SJLJ_SETJMP, sdl,
Bill Wendlingce370cf2011-10-07 21:25:38 +00004698 DAG.getVTList(MVT::i32, MVT::Other),
4699 Ops, 2);
4700 setValue(&I, Op.getValue(0));
4701 DAG.setRoot(Op.getValue(1));
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004702 return 0;
4703 }
Jim Grosbach5eb19512010-05-22 01:06:18 +00004704 case Intrinsic::eh_sjlj_longjmp: {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004705 DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_LONGJMP, sdl, MVT::Other,
Jim Grosbache4ad3872010-10-19 23:27:08 +00004706 getRoot(), getValue(I.getArgOperand(0))));
4707 return 0;
4708 }
Jim Grosbachca752c92010-01-28 01:45:32 +00004709
Dale Johannesen0488fb62010-09-30 23:57:10 +00004710 case Intrinsic::x86_mmx_pslli_w:
4711 case Intrinsic::x86_mmx_pslli_d:
4712 case Intrinsic::x86_mmx_pslli_q:
4713 case Intrinsic::x86_mmx_psrli_w:
4714 case Intrinsic::x86_mmx_psrli_d:
4715 case Intrinsic::x86_mmx_psrli_q:
4716 case Intrinsic::x86_mmx_psrai_w:
4717 case Intrinsic::x86_mmx_psrai_d: {
4718 SDValue ShAmt = getValue(I.getArgOperand(1));
4719 if (isa<ConstantSDNode>(ShAmt)) {
4720 visitTargetIntrinsic(I, Intrinsic);
4721 return 0;
4722 }
4723 unsigned NewIntrinsic = 0;
4724 EVT ShAmtVT = MVT::v2i32;
4725 switch (Intrinsic) {
4726 case Intrinsic::x86_mmx_pslli_w:
4727 NewIntrinsic = Intrinsic::x86_mmx_psll_w;
4728 break;
4729 case Intrinsic::x86_mmx_pslli_d:
4730 NewIntrinsic = Intrinsic::x86_mmx_psll_d;
4731 break;
4732 case Intrinsic::x86_mmx_pslli_q:
4733 NewIntrinsic = Intrinsic::x86_mmx_psll_q;
4734 break;
4735 case Intrinsic::x86_mmx_psrli_w:
4736 NewIntrinsic = Intrinsic::x86_mmx_psrl_w;
4737 break;
4738 case Intrinsic::x86_mmx_psrli_d:
4739 NewIntrinsic = Intrinsic::x86_mmx_psrl_d;
4740 break;
4741 case Intrinsic::x86_mmx_psrli_q:
4742 NewIntrinsic = Intrinsic::x86_mmx_psrl_q;
4743 break;
4744 case Intrinsic::x86_mmx_psrai_w:
4745 NewIntrinsic = Intrinsic::x86_mmx_psra_w;
4746 break;
4747 case Intrinsic::x86_mmx_psrai_d:
4748 NewIntrinsic = Intrinsic::x86_mmx_psra_d;
4749 break;
4750 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
4751 }
4752
4753 // The vector shift intrinsics with scalars uses 32b shift amounts but
4754 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
4755 // to be zero.
4756 // We must do this early because v2i32 is not a legal type.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004757 SDValue ShOps[2];
4758 ShOps[0] = ShAmt;
4759 ShOps[1] = DAG.getConstant(0, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004760 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, sdl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004761 EVT DestVT = TLI.getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004762 ShAmt = DAG.getNode(ISD::BITCAST, sdl, DestVT, ShAmt);
4763 Res = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, sdl, DestVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00004764 DAG.getConstant(NewIntrinsic, MVT::i32),
4765 getValue(I.getArgOperand(0)), ShAmt);
4766 setValue(&I, Res);
4767 return 0;
4768 }
Pete Cooperd18134f2012-02-24 03:51:49 +00004769 case Intrinsic::x86_avx_vinsertf128_pd_256:
4770 case Intrinsic::x86_avx_vinsertf128_ps_256:
Craig Topperb45c9692012-04-07 22:32:29 +00004771 case Intrinsic::x86_avx_vinsertf128_si_256:
4772 case Intrinsic::x86_avx2_vinserti128: {
Pete Cooperd18134f2012-02-24 03:51:49 +00004773 EVT DestVT = TLI.getValueType(I.getType());
4774 EVT ElVT = TLI.getValueType(I.getArgOperand(1)->getType());
4775 uint64_t Idx = (cast<ConstantInt>(I.getArgOperand(2))->getZExtValue() & 1) *
4776 ElVT.getVectorNumElements();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004777 Res = DAG.getNode(ISD::INSERT_SUBVECTOR, sdl, DestVT,
Pete Cooperd18134f2012-02-24 03:51:49 +00004778 getValue(I.getArgOperand(0)),
4779 getValue(I.getArgOperand(1)),
Craig Topperf6dc7922012-09-05 05:48:09 +00004780 DAG.getIntPtrConstant(Idx));
4781 setValue(&I, Res);
4782 return 0;
4783 }
4784 case Intrinsic::x86_avx_vextractf128_pd_256:
4785 case Intrinsic::x86_avx_vextractf128_ps_256:
4786 case Intrinsic::x86_avx_vextractf128_si_256:
4787 case Intrinsic::x86_avx2_vextracti128: {
Craig Topperf6dc7922012-09-05 05:48:09 +00004788 EVT DestVT = TLI.getValueType(I.getType());
4789 uint64_t Idx = (cast<ConstantInt>(I.getArgOperand(1))->getZExtValue() & 1) *
4790 DestVT.getVectorNumElements();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004791 Res = DAG.getNode(ISD::EXTRACT_SUBVECTOR, sdl, DestVT,
Craig Topperf6dc7922012-09-05 05:48:09 +00004792 getValue(I.getArgOperand(0)),
4793 DAG.getIntPtrConstant(Idx));
Pete Cooperd18134f2012-02-24 03:51:49 +00004794 setValue(&I, Res);
4795 return 0;
4796 }
Mon P Wang77cdf302008-11-10 20:54:11 +00004797 case Intrinsic::convertff:
4798 case Intrinsic::convertfsi:
4799 case Intrinsic::convertfui:
4800 case Intrinsic::convertsif:
4801 case Intrinsic::convertuif:
4802 case Intrinsic::convertss:
4803 case Intrinsic::convertsu:
4804 case Intrinsic::convertus:
4805 case Intrinsic::convertuu: {
4806 ISD::CvtCode Code = ISD::CVT_INVALID;
4807 switch (Intrinsic) {
Craig Topperc42e6402012-04-11 04:34:11 +00004808 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Mon P Wang77cdf302008-11-10 20:54:11 +00004809 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
4810 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
4811 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
4812 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
4813 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
4814 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
4815 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
4816 case Intrinsic::convertus: Code = ISD::CVT_US; break;
4817 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
4818 }
Owen Andersone50ed302009-08-10 22:56:29 +00004819 EVT DestVT = TLI.getValueType(I.getType());
Gabor Greif0635f352010-06-25 09:38:13 +00004820 const Value *Op1 = I.getArgOperand(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004821 Res = DAG.getConvertRndSat(DestVT, sdl, getValue(Op1),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004822 DAG.getValueType(DestVT),
4823 DAG.getValueType(getValue(Op1).getValueType()),
Gabor Greif0635f352010-06-25 09:38:13 +00004824 getValue(I.getArgOperand(1)),
4825 getValue(I.getArgOperand(2)),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004826 Code);
4827 setValue(&I, Res);
Mon P Wang77cdf302008-11-10 20:54:11 +00004828 return 0;
4829 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004830 case Intrinsic::powi:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004831 setValue(&I, ExpandPowI(sdl, getValue(I.getArgOperand(0)),
Gabor Greif0635f352010-06-25 09:38:13 +00004832 getValue(I.getArgOperand(1)), DAG));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004833 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004834 case Intrinsic::log:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004835 setValue(&I, expandLog(sdl, getValue(I.getArgOperand(0)), DAG, TLI));
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004836 return 0;
4837 case Intrinsic::log2:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004838 setValue(&I, expandLog2(sdl, getValue(I.getArgOperand(0)), DAG, TLI));
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004839 return 0;
4840 case Intrinsic::log10:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004841 setValue(&I, expandLog10(sdl, getValue(I.getArgOperand(0)), DAG, TLI));
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004842 return 0;
4843 case Intrinsic::exp:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004844 setValue(&I, expandExp(sdl, getValue(I.getArgOperand(0)), DAG, TLI));
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004845 return 0;
4846 case Intrinsic::exp2:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004847 setValue(&I, expandExp2(sdl, getValue(I.getArgOperand(0)), DAG, TLI));
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004848 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004849 case Intrinsic::pow:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004850 setValue(&I, expandPow(sdl, getValue(I.getArgOperand(0)),
Craig Topper327e4cb2012-11-25 08:08:58 +00004851 getValue(I.getArgOperand(1)), DAG, TLI));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004852 return 0;
Craig Topper9bd4dd72012-11-16 07:48:23 +00004853 case Intrinsic::sqrt:
Peter Collingbourneb34d3aa2012-05-28 21:48:37 +00004854 case Intrinsic::fabs:
Craig Topper9bd4dd72012-11-16 07:48:23 +00004855 case Intrinsic::sin:
4856 case Intrinsic::cos:
Dan Gohman27db99f2012-07-26 17:43:27 +00004857 case Intrinsic::floor:
Craig Topper49010472012-11-15 06:51:10 +00004858 case Intrinsic::ceil:
Craig Topper49010472012-11-15 06:51:10 +00004859 case Intrinsic::trunc:
Craig Topper49010472012-11-15 06:51:10 +00004860 case Intrinsic::rint:
Craig Topper9bd4dd72012-11-16 07:48:23 +00004861 case Intrinsic::nearbyint: {
4862 unsigned Opcode;
4863 switch (Intrinsic) {
4864 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
4865 case Intrinsic::sqrt: Opcode = ISD::FSQRT; break;
4866 case Intrinsic::fabs: Opcode = ISD::FABS; break;
4867 case Intrinsic::sin: Opcode = ISD::FSIN; break;
4868 case Intrinsic::cos: Opcode = ISD::FCOS; break;
4869 case Intrinsic::floor: Opcode = ISD::FFLOOR; break;
4870 case Intrinsic::ceil: Opcode = ISD::FCEIL; break;
4871 case Intrinsic::trunc: Opcode = ISD::FTRUNC; break;
4872 case Intrinsic::rint: Opcode = ISD::FRINT; break;
4873 case Intrinsic::nearbyint: Opcode = ISD::FNEARBYINT; break;
4874 }
4875
Andrew Trickac6d9be2013-05-25 02:42:55 +00004876 setValue(&I, DAG.getNode(Opcode, sdl,
Craig Topper49010472012-11-15 06:51:10 +00004877 getValue(I.getArgOperand(0)).getValueType(),
4878 getValue(I.getArgOperand(0))));
4879 return 0;
Craig Topper9bd4dd72012-11-16 07:48:23 +00004880 }
Cameron Zwarich33390842011-07-08 21:39:21 +00004881 case Intrinsic::fma:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004882 setValue(&I, DAG.getNode(ISD::FMA, sdl,
Cameron Zwarich33390842011-07-08 21:39:21 +00004883 getValue(I.getArgOperand(0)).getValueType(),
4884 getValue(I.getArgOperand(0)),
4885 getValue(I.getArgOperand(1)),
4886 getValue(I.getArgOperand(2))));
4887 return 0;
Lang Hames5afba6f2012-06-05 19:07:46 +00004888 case Intrinsic::fmuladd: {
4889 EVT VT = TLI.getValueType(I.getType());
Lang Hamese0231412012-06-22 01:09:09 +00004890 if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict &&
Lang Hamese0231412012-06-22 01:09:09 +00004891 TLI.isFMAFasterThanMulAndAdd(VT)){
Andrew Trickac6d9be2013-05-25 02:42:55 +00004892 setValue(&I, DAG.getNode(ISD::FMA, sdl,
Lang Hames5afba6f2012-06-05 19:07:46 +00004893 getValue(I.getArgOperand(0)).getValueType(),
4894 getValue(I.getArgOperand(0)),
4895 getValue(I.getArgOperand(1)),
4896 getValue(I.getArgOperand(2))));
4897 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004898 SDValue Mul = DAG.getNode(ISD::FMUL, sdl,
Lang Hames5afba6f2012-06-05 19:07:46 +00004899 getValue(I.getArgOperand(0)).getValueType(),
4900 getValue(I.getArgOperand(0)),
4901 getValue(I.getArgOperand(1)));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004902 SDValue Add = DAG.getNode(ISD::FADD, sdl,
Lang Hames5afba6f2012-06-05 19:07:46 +00004903 getValue(I.getArgOperand(0)).getValueType(),
4904 Mul,
4905 getValue(I.getArgOperand(2)));
4906 setValue(&I, Add);
4907 }
4908 return 0;
4909 }
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004910 case Intrinsic::convert_to_fp16:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004911 setValue(&I, DAG.getNode(ISD::FP32_TO_FP16, sdl,
Gabor Greif0635f352010-06-25 09:38:13 +00004912 MVT::i16, getValue(I.getArgOperand(0))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004913 return 0;
4914 case Intrinsic::convert_from_fp16:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004915 setValue(&I, DAG.getNode(ISD::FP16_TO_FP32, sdl,
Gabor Greif0635f352010-06-25 09:38:13 +00004916 MVT::f32, getValue(I.getArgOperand(0))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004917 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004918 case Intrinsic::pcmarker: {
Gabor Greif0635f352010-06-25 09:38:13 +00004919 SDValue Tmp = getValue(I.getArgOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004920 DAG.setRoot(DAG.getNode(ISD::PCMARKER, sdl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004921 return 0;
4922 }
4923 case Intrinsic::readcyclecounter: {
4924 SDValue Op = getRoot();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004925 Res = DAG.getNode(ISD::READCYCLECOUNTER, sdl,
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004926 DAG.getVTList(MVT::i64, MVT::Other),
4927 &Op, 1);
4928 setValue(&I, Res);
4929 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004930 return 0;
4931 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004932 case Intrinsic::bswap:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004933 setValue(&I, DAG.getNode(ISD::BSWAP, sdl,
Gabor Greif0635f352010-06-25 09:38:13 +00004934 getValue(I.getArgOperand(0)).getValueType(),
4935 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004936 return 0;
4937 case Intrinsic::cttz: {
Gabor Greif0635f352010-06-25 09:38:13 +00004938 SDValue Arg = getValue(I.getArgOperand(0));
Chandler Carruth63974b22011-12-13 01:56:10 +00004939 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004940 EVT Ty = Arg.getValueType();
Chandler Carruth63974b22011-12-13 01:56:10 +00004941 setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTTZ : ISD::CTTZ_ZERO_UNDEF,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004942 sdl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004943 return 0;
4944 }
4945 case Intrinsic::ctlz: {
Gabor Greif0635f352010-06-25 09:38:13 +00004946 SDValue Arg = getValue(I.getArgOperand(0));
Chandler Carruth63974b22011-12-13 01:56:10 +00004947 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004948 EVT Ty = Arg.getValueType();
Chandler Carruth63974b22011-12-13 01:56:10 +00004949 setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTLZ : ISD::CTLZ_ZERO_UNDEF,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004950 sdl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004951 return 0;
4952 }
4953 case Intrinsic::ctpop: {
Gabor Greif0635f352010-06-25 09:38:13 +00004954 SDValue Arg = getValue(I.getArgOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00004955 EVT Ty = Arg.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004956 setValue(&I, DAG.getNode(ISD::CTPOP, sdl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004957 return 0;
4958 }
4959 case Intrinsic::stacksave: {
4960 SDValue Op = getRoot();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004961 Res = DAG.getNode(ISD::STACKSAVE, sdl,
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004962 DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1);
4963 setValue(&I, Res);
4964 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004965 return 0;
4966 }
4967 case Intrinsic::stackrestore: {
Gabor Greif0635f352010-06-25 09:38:13 +00004968 Res = getValue(I.getArgOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004969 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, sdl, MVT::Other, getRoot(), Res));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004970 return 0;
4971 }
Bill Wendling57344502008-11-18 11:01:33 +00004972 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00004973 // Emit code into the DAG to store the stack guard onto the stack.
4974 MachineFunction &MF = DAG.getMachineFunction();
4975 MachineFrameInfo *MFI = MF.getFrameInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004976 EVT PtrTy = TLI.getPointerTy();
Bill Wendlingb2a42982008-11-06 02:29:10 +00004977
Gabor Greif0635f352010-06-25 09:38:13 +00004978 SDValue Src = getValue(I.getArgOperand(0)); // The guard's value.
4979 AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Bill Wendlingb2a42982008-11-06 02:29:10 +00004980
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004981 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00004982 MFI->setStackProtectorIndex(FI);
4983
4984 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4985
4986 // Store the stack protector onto the stack.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004987 Res = DAG.getStore(getRoot(), sdl, Src, FIN,
Chris Lattner84bd98a2010-09-21 18:58:22 +00004988 MachinePointerInfo::getFixedStack(FI),
4989 true, false, 0);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004990 setValue(&I, Res);
4991 DAG.setRoot(Res);
Bill Wendlingb2a42982008-11-06 02:29:10 +00004992 return 0;
4993 }
Eric Christopher7b5e6172009-10-27 00:52:25 +00004994 case Intrinsic::objectsize: {
4995 // If we don't know by now, we're never going to know.
Gabor Greif0635f352010-06-25 09:38:13 +00004996 ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(1));
Eric Christopher7b5e6172009-10-27 00:52:25 +00004997
4998 assert(CI && "Non-constant type in __builtin_object_size?");
4999
Gabor Greif0635f352010-06-25 09:38:13 +00005000 SDValue Arg = getValue(I.getCalledValue());
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00005001 EVT Ty = Arg.getValueType();
5002
Dan Gohmane368b462010-06-18 14:22:04 +00005003 if (CI->isZero())
Bill Wendlingd0283fa2009-12-22 00:40:51 +00005004 Res = DAG.getConstant(-1ULL, Ty);
Eric Christopher7b5e6172009-10-27 00:52:25 +00005005 else
Bill Wendlingd0283fa2009-12-22 00:40:51 +00005006 Res = DAG.getConstant(0, Ty);
5007
5008 setValue(&I, Res);
Eric Christopher7b5e6172009-10-27 00:52:25 +00005009 return 0;
5010 }
Justin Holewinskic2b7f5f2013-05-21 14:37:16 +00005011 case Intrinsic::annotation:
5012 case Intrinsic::ptr_annotation:
5013 // Drop the intrinsic, but forward the value
5014 setValue(&I, getValue(I.getOperand(0)));
5015 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005016 case Intrinsic::var_annotation:
5017 // Discard annotate attributes
5018 return 0;
5019
5020 case Intrinsic::init_trampoline: {
Gabor Greif0635f352010-06-25 09:38:13 +00005021 const Function *F = cast<Function>(I.getArgOperand(1)->stripPointerCasts());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005022
5023 SDValue Ops[6];
5024 Ops[0] = getRoot();
Gabor Greif0635f352010-06-25 09:38:13 +00005025 Ops[1] = getValue(I.getArgOperand(0));
5026 Ops[2] = getValue(I.getArgOperand(1));
5027 Ops[3] = getValue(I.getArgOperand(2));
5028 Ops[4] = DAG.getSrcValue(I.getArgOperand(0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005029 Ops[5] = DAG.getSrcValue(F);
5030
Andrew Trickac6d9be2013-05-25 02:42:55 +00005031 Res = DAG.getNode(ISD::INIT_TRAMPOLINE, sdl, MVT::Other, Ops, 6);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005032
Duncan Sands4a544a72011-09-06 13:37:06 +00005033 DAG.setRoot(Res);
5034 return 0;
5035 }
5036 case Intrinsic::adjust_trampoline: {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005037 setValue(&I, DAG.getNode(ISD::ADJUST_TRAMPOLINE, sdl,
Duncan Sands4a544a72011-09-06 13:37:06 +00005038 TLI.getPointerTy(),
5039 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005040 return 0;
5041 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005042 case Intrinsic::gcroot:
5043 if (GFI) {
Bill Wendling95dd4422012-05-01 22:50:45 +00005044 const Value *Alloca = I.getArgOperand(0)->stripPointerCasts();
Gabor Greif0635f352010-06-25 09:38:13 +00005045 const Constant *TypeMap = cast<Constant>(I.getArgOperand(1));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005046
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005047 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
5048 GFI->addStackRoot(FI->getIndex(), TypeMap);
5049 }
5050 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005051 case Intrinsic::gcread:
5052 case Intrinsic::gcwrite:
Torok Edwinc23197a2009-07-14 16:55:14 +00005053 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
Bill Wendlingd0283fa2009-12-22 00:40:51 +00005054 case Intrinsic::flt_rounds:
Andrew Trickac6d9be2013-05-25 02:42:55 +00005055 setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, sdl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005056 return 0;
Jakub Staszak9da99342011-07-06 18:22:43 +00005057
5058 case Intrinsic::expect: {
5059 // Just replace __builtin_expect(exp, c) with EXP.
5060 setValue(&I, getValue(I.getArgOperand(0)));
5061 return 0;
5062 }
5063
Shuxin Yang970755e2012-10-19 20:11:16 +00005064 case Intrinsic::debugtrap:
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005065 case Intrinsic::trap: {
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005066 StringRef TrapFuncName = TM.Options.getTrapFunctionName();
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005067 if (TrapFuncName.empty()) {
Shuxin Yang970755e2012-10-19 20:11:16 +00005068 ISD::NodeType Op = (Intrinsic == Intrinsic::trap) ?
5069 ISD::TRAP : ISD::DEBUGTRAP;
Andrew Trickac6d9be2013-05-25 02:42:55 +00005070 DAG.setRoot(DAG.getNode(Op, sdl,MVT::Other, getRoot()));
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005071 return 0;
5072 }
5073 TargetLowering::ArgListTy Args;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00005074 TargetLowering::
5075 CallLoweringInfo CLI(getRoot(), I.getType(),
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005076 false, false, false, false, 0, CallingConv::C,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00005077 /*isTailCall=*/false,
5078 /*doesNotRet=*/false, /*isReturnValueUsed=*/true,
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005079 DAG.getExternalSymbol(TrapFuncName.data(), TLI.getPointerTy()),
Andrew Trickac6d9be2013-05-25 02:42:55 +00005080 Args, DAG, sdl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00005081 std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005082 DAG.setRoot(Result.second);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005083 return 0;
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005084 }
Shuxin Yang970755e2012-10-19 20:11:16 +00005085
Bill Wendlingef375462008-11-21 02:38:44 +00005086 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00005087 case Intrinsic::sadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00005088 case Intrinsic::usub_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00005089 case Intrinsic::ssub_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00005090 case Intrinsic::umul_with_overflow:
Craig Topperc42e6402012-04-11 04:34:11 +00005091 case Intrinsic::smul_with_overflow: {
5092 ISD::NodeType Op;
5093 switch (Intrinsic) {
5094 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
5095 case Intrinsic::uadd_with_overflow: Op = ISD::UADDO; break;
5096 case Intrinsic::sadd_with_overflow: Op = ISD::SADDO; break;
5097 case Intrinsic::usub_with_overflow: Op = ISD::USUBO; break;
5098 case Intrinsic::ssub_with_overflow: Op = ISD::SSUBO; break;
5099 case Intrinsic::umul_with_overflow: Op = ISD::UMULO; break;
5100 case Intrinsic::smul_with_overflow: Op = ISD::SMULO; break;
5101 }
5102 SDValue Op1 = getValue(I.getArgOperand(0));
5103 SDValue Op2 = getValue(I.getArgOperand(1));
Bill Wendling7cdc3c82008-11-21 02:03:52 +00005104
Craig Topperc42e6402012-04-11 04:34:11 +00005105 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005106 setValue(&I, DAG.getNode(Op, sdl, VTs, Op1, Op2));
Craig Topperc42e6402012-04-11 04:34:11 +00005107 return 0;
5108 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005109 case Intrinsic::prefetch: {
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00005110 SDValue Ops[5];
Dale Johannesen1de4aa92010-10-26 23:11:10 +00005111 unsigned rw = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005112 Ops[0] = getRoot();
Gabor Greif0635f352010-06-25 09:38:13 +00005113 Ops[1] = getValue(I.getArgOperand(0));
5114 Ops[2] = getValue(I.getArgOperand(1));
5115 Ops[3] = getValue(I.getArgOperand(2));
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00005116 Ops[4] = getValue(I.getArgOperand(3));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005117 DAG.setRoot(DAG.getMemIntrinsicNode(ISD::PREFETCH, sdl,
Dale Johannesen1de4aa92010-10-26 23:11:10 +00005118 DAG.getVTList(MVT::Other),
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00005119 &Ops[0], 5,
Dale Johannesen1de4aa92010-10-26 23:11:10 +00005120 EVT::getIntegerVT(*Context, 8),
5121 MachinePointerInfo(I.getArgOperand(0)),
5122 0, /* align */
5123 false, /* volatile */
5124 rw==0, /* read */
5125 rw==1)); /* write */
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005126 return 0;
5127 }
Duncan Sandsf07c9492009-11-10 09:08:09 +00005128 case Intrinsic::lifetime_start:
Nadav Rotemc05d3062012-09-06 09:17:37 +00005129 case Intrinsic::lifetime_end: {
Nadav Rotemc05d3062012-09-06 09:17:37 +00005130 bool IsStart = (Intrinsic == Intrinsic::lifetime_start);
Nadav Rotem9a2ae002012-09-10 08:43:23 +00005131 // Stack coloring is not enabled in O0, discard region information.
5132 if (TM.getOptLevel() == CodeGenOpt::None)
5133 return 0;
Nadav Rotemc05d3062012-09-06 09:17:37 +00005134
Nadav Rotem9a2ae002012-09-10 08:43:23 +00005135 SmallVector<Value *, 4> Allocas;
5136 GetUnderlyingObjects(I.getArgOperand(1), Allocas, TD);
5137
5138 for (SmallVector<Value*, 4>::iterator Object = Allocas.begin(),
5139 E = Allocas.end(); Object != E; ++Object) {
5140 AllocaInst *LifetimeObject = dyn_cast_or_null<AllocaInst>(*Object);
5141
5142 // Could not find an Alloca.
5143 if (!LifetimeObject)
5144 continue;
5145
5146 int FI = FuncInfo.StaticAllocaMap[LifetimeObject];
5147
5148 SDValue Ops[2];
5149 Ops[0] = getRoot();
5150 Ops[1] = DAG.getFrameIndex(FI, TLI.getPointerTy(), true);
5151 unsigned Opcode = (IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END);
5152
Andrew Trickac6d9be2013-05-25 02:42:55 +00005153 Res = DAG.getNode(Opcode, sdl, MVT::Other, Ops, 2);
Nadav Rotem9a2ae002012-09-10 08:43:23 +00005154 DAG.setRoot(Res);
5155 }
Nadav Rotem5882e562013-02-01 19:25:23 +00005156 return 0;
Nadav Rotemc05d3062012-09-06 09:17:37 +00005157 }
5158 case Intrinsic::invariant_start:
Duncan Sandsf07c9492009-11-10 09:08:09 +00005159 // Discard region information.
Bill Wendling4533cac2010-01-28 21:51:40 +00005160 setValue(&I, DAG.getUNDEF(TLI.getPointerTy()));
Duncan Sandsf07c9492009-11-10 09:08:09 +00005161 return 0;
5162 case Intrinsic::invariant_end:
Duncan Sandsf07c9492009-11-10 09:08:09 +00005163 // Discard region information.
5164 return 0;
Nuno Lopes85b40892012-06-28 22:30:12 +00005165 case Intrinsic::donothing:
5166 // ignore
5167 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005168 }
5169}
5170
Dan Gohman46510a72010-04-15 01:51:59 +00005171void SelectionDAGBuilder::LowerCallTo(ImmutableCallSite CS, SDValue Callee,
Dan Gohman2048b852009-11-23 18:04:58 +00005172 bool isTailCall,
5173 MachineBasicBlock *LandingPad) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005174 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
5175 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
5176 Type *RetTy = FTy->getReturnType();
Chris Lattner512063d2010-04-05 06:19:28 +00005177 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Chris Lattner16112732010-03-14 01:41:15 +00005178 MCSymbol *BeginLabel = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005179
5180 TargetLowering::ArgListTy Args;
5181 TargetLowering::ArgListEntry Entry;
5182 Args.reserve(CS.arg_size());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005183
5184 // Check whether the function can return without sret-demotion.
Dan Gohman84023e02010-07-10 09:00:22 +00005185 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling8b62abd2012-12-30 13:01:51 +00005186 GetReturnInfo(RetTy, CS.getAttributes(), Outs, TLI);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005187
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005188 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Bill Wendling96cb1122012-07-19 00:04:14 +00005189 DAG.getMachineFunction(),
5190 FTy->isVarArg(), Outs,
5191 FTy->getContext());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005192
5193 SDValue DemoteStackSlot;
Chris Lattnerecf42c42010-09-21 16:36:31 +00005194 int DemoteStackIdx = -100;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005195
5196 if (!CanLowerReturn) {
Micah Villmow3574eca2012-10-08 16:38:25 +00005197 uint64_t TySize = TLI.getDataLayout()->getTypeAllocSize(
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005198 FTy->getReturnType());
Micah Villmow3574eca2012-10-08 16:38:25 +00005199 unsigned Align = TLI.getDataLayout()->getPrefTypeAlignment(
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005200 FTy->getReturnType());
5201 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattnerecf42c42010-09-21 16:36:31 +00005202 DemoteStackIdx = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005203 Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005204
Chris Lattnerecf42c42010-09-21 16:36:31 +00005205 DemoteStackSlot = DAG.getFrameIndex(DemoteStackIdx, TLI.getPointerTy());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005206 Entry.Node = DemoteStackSlot;
5207 Entry.Ty = StackSlotPtrType;
5208 Entry.isSExt = false;
5209 Entry.isZExt = false;
5210 Entry.isInReg = false;
5211 Entry.isSRet = true;
5212 Entry.isNest = false;
5213 Entry.isByVal = false;
Stephen Lin456ca042013-04-20 05:14:40 +00005214 Entry.isReturned = false;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005215 Entry.Alignment = Align;
5216 Args.push_back(Entry);
5217 RetTy = Type::getVoidTy(FTy->getContext());
5218 }
5219
Dan Gohman46510a72010-04-15 01:51:59 +00005220 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005221 i != e; ++i) {
Rafael Espindola3fa82832011-05-13 15:18:06 +00005222 const Value *V = *i;
5223
5224 // Skip empty types
5225 if (V->getType()->isEmptyTy())
5226 continue;
5227
5228 SDValue ArgNode = getValue(V);
5229 Entry.Node = ArgNode; Entry.Ty = V->getType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005230
5231 unsigned attrInd = i - CS.arg_begin() + 1;
Stephen Lin456ca042013-04-20 05:14:40 +00005232 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
5233 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
5234 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
5235 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
5236 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
5237 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
5238 Entry.isReturned = CS.paramHasAttr(attrInd, Attribute::Returned);
5239 Entry.Alignment = CS.getParamAlignment(attrInd);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005240 Args.push_back(Entry);
5241 }
5242
Chris Lattner512063d2010-04-05 06:19:28 +00005243 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005244 // Insert a label before the invoke call to mark the try range. This can be
5245 // used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00005246 BeginLabel = MMI.getContext().CreateTempSymbol();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00005247
Jim Grosbachca752c92010-01-28 01:45:32 +00005248 // For SjLj, keep track of which landing pads go with which invokes
5249 // so as to maintain the ordering of pads in the LSDA.
Chris Lattner512063d2010-04-05 06:19:28 +00005250 unsigned CallSiteIndex = MMI.getCurrentCallSite();
Jim Grosbachca752c92010-01-28 01:45:32 +00005251 if (CallSiteIndex) {
Chris Lattner512063d2010-04-05 06:19:28 +00005252 MMI.setCallSiteBeginLabel(BeginLabel, CallSiteIndex);
Bill Wendling30e67402011-10-05 22:24:35 +00005253 LPadToCallSiteMap[LandingPad].push_back(CallSiteIndex);
Bill Wendlinga8512ed2011-10-04 22:00:35 +00005254
Jim Grosbachca752c92010-01-28 01:45:32 +00005255 // Now that the call site is handled, stop tracking it.
Chris Lattner512063d2010-04-05 06:19:28 +00005256 MMI.setCurrentCallSite(0);
Jim Grosbachca752c92010-01-28 01:45:32 +00005257 }
5258
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005259 // Both PendingLoads and PendingExports must be flushed here;
5260 // this call might not return.
5261 (void)getRoot();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005262 DAG.setRoot(DAG.getEHLabel(getCurSDLoc(), getControlRoot(), BeginLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005263 }
5264
Dan Gohman98ca4f22009-08-05 01:29:28 +00005265 // Check if target-independent constraints permit a tail call here.
5266 // Target-dependent constraints are checked within TLI.LowerCallTo.
Bill Wendling1a17bd22013-01-18 21:50:24 +00005267 if (isTailCall && !isInTailCallPosition(CS, TLI))
Dan Gohman98ca4f22009-08-05 01:29:28 +00005268 isTailCall = false;
5269
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00005270 TargetLowering::
5271 CallLoweringInfo CLI(getRoot(), RetTy, FTy, isTailCall, Callee, Args, DAG,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005272 getCurSDLoc(), CS);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00005273 std::pair<SDValue,SDValue> Result = TLI.LowerCallTo(CLI);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005274 assert((isTailCall || Result.second.getNode()) &&
5275 "Non-null chain expected with non-tail call!");
5276 assert((Result.second.getNode() || !Result.first.getNode()) &&
5277 "Null value expected with tail call!");
Bill Wendlinge80ae832009-12-22 00:50:32 +00005278 if (Result.first.getNode()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005279 setValue(CS.getInstruction(), Result.first);
Bill Wendlinge80ae832009-12-22 00:50:32 +00005280 } else if (!CanLowerReturn && Result.second.getNode()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005281 // The instruction result is the result of loading from the
5282 // hidden sret parameter.
5283 SmallVector<EVT, 1> PVTs;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005284 Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005285
5286 ComputeValueVTs(TLI, PtrRetTy, PVTs);
5287 assert(PVTs.size() == 1 && "Pointers should fit in one register");
5288 EVT PtrVT = PVTs[0];
Eli Friedman2db0e9e2012-05-25 00:09:29 +00005289
5290 SmallVector<EVT, 4> RetTys;
5291 SmallVector<uint64_t, 4> Offsets;
5292 RetTy = FTy->getReturnType();
5293 ComputeValueVTs(TLI, RetTy, RetTys, &Offsets);
5294
5295 unsigned NumValues = RetTys.size();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005296 SmallVector<SDValue, 4> Values(NumValues);
5297 SmallVector<SDValue, 4> Chains(NumValues);
5298
5299 for (unsigned i = 0; i < NumValues; ++i) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005300 SDValue Add = DAG.getNode(ISD::ADD, getCurSDLoc(), PtrVT,
Bill Wendlinge80ae832009-12-22 00:50:32 +00005301 DemoteStackSlot,
5302 DAG.getConstant(Offsets[i], PtrVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005303 SDValue L = DAG.getLoad(RetTys[i], getCurSDLoc(), Result.second, Add,
Chris Lattnerecf42c42010-09-21 16:36:31 +00005304 MachinePointerInfo::getFixedStack(DemoteStackIdx, Offsets[i]),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005305 false, false, false, 1);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005306 Values[i] = L;
5307 Chains[i] = L.getValue(1);
5308 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00005309
Andrew Trickac6d9be2013-05-25 02:42:55 +00005310 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005311 MVT::Other, &Chains[0], NumValues);
5312 PendingLoads.push_back(Chain);
Michael J. Spencere70c5262010-10-16 08:25:21 +00005313
Bill Wendling4533cac2010-01-28 21:51:40 +00005314 setValue(CS.getInstruction(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00005315 DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00005316 DAG.getVTList(&RetTys[0], RetTys.size()),
Eli Friedman2db0e9e2012-05-25 00:09:29 +00005317 &Values[0], Values.size()));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005318 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00005319
Evan Cheng8380c032011-04-01 19:42:22 +00005320 if (!Result.second.getNode()) {
Evan Chengc249e482011-04-01 19:57:01 +00005321 // As a special case, a null chain means that a tail call has been emitted and
5322 // the DAG root is already updated.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005323 HasTailCall = true;
Evan Cheng8380c032011-04-01 19:42:22 +00005324 } else {
5325 DAG.setRoot(Result.second);
Evan Cheng8380c032011-04-01 19:42:22 +00005326 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005327
Chris Lattner512063d2010-04-05 06:19:28 +00005328 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005329 // Insert a label at the end of the invoke call to mark the try range. This
5330 // can be used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00005331 MCSymbol *EndLabel = MMI.getContext().CreateTempSymbol();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005332 DAG.setRoot(DAG.getEHLabel(getCurSDLoc(), getRoot(), EndLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005333
5334 // Inform MachineModuleInfo of range.
Chris Lattner512063d2010-04-05 06:19:28 +00005335 MMI.addInvoke(LandingPad, BeginLabel, EndLabel);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005336 }
5337}
5338
Chris Lattner8047d9a2009-12-24 00:37:38 +00005339/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
5340/// value is equal or not-equal to zero.
Dan Gohman46510a72010-04-15 01:51:59 +00005341static bool IsOnlyUsedInZeroEqualityComparison(const Value *V) {
5342 for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end();
Chris Lattner8047d9a2009-12-24 00:37:38 +00005343 UI != E; ++UI) {
Dan Gohman46510a72010-04-15 01:51:59 +00005344 if (const ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
Chris Lattner8047d9a2009-12-24 00:37:38 +00005345 if (IC->isEquality())
Dan Gohman46510a72010-04-15 01:51:59 +00005346 if (const Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
Chris Lattner8047d9a2009-12-24 00:37:38 +00005347 if (C->isNullValue())
5348 continue;
5349 // Unknown instruction.
5350 return false;
5351 }
5352 return true;
5353}
5354
Dan Gohman46510a72010-04-15 01:51:59 +00005355static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005356 Type *LoadTy,
Chris Lattner8047d9a2009-12-24 00:37:38 +00005357 SelectionDAGBuilder &Builder) {
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005358
Chris Lattner8047d9a2009-12-24 00:37:38 +00005359 // Check to see if this load can be trivially constant folded, e.g. if the
5360 // input is from a string literal.
Dan Gohman46510a72010-04-15 01:51:59 +00005361 if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00005362 // Cast pointer to the type we really want to load.
Dan Gohman46510a72010-04-15 01:51:59 +00005363 LoadInput = ConstantExpr::getBitCast(const_cast<Constant *>(LoadInput),
Chris Lattner8047d9a2009-12-24 00:37:38 +00005364 PointerType::getUnqual(LoadTy));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005365
Dan Gohman46510a72010-04-15 01:51:59 +00005366 if (const Constant *LoadCst =
5367 ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput),
5368 Builder.TD))
Chris Lattner8047d9a2009-12-24 00:37:38 +00005369 return Builder.getValue(LoadCst);
5370 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005371
Chris Lattner8047d9a2009-12-24 00:37:38 +00005372 // Otherwise, we have to emit the load. If the pointer is to unfoldable but
5373 // still constant memory, the input chain can be the entry node.
5374 SDValue Root;
5375 bool ConstantMemory = false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005376
Chris Lattner8047d9a2009-12-24 00:37:38 +00005377 // Do not serialize (non-volatile) loads of constant memory with anything.
5378 if (Builder.AA->pointsToConstantMemory(PtrVal)) {
5379 Root = Builder.DAG.getEntryNode();
5380 ConstantMemory = true;
5381 } else {
5382 // Do not serialize non-volatile loads against each other.
5383 Root = Builder.DAG.getRoot();
5384 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005385
Chris Lattner8047d9a2009-12-24 00:37:38 +00005386 SDValue Ptr = Builder.getValue(PtrVal);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005387 SDValue LoadVal = Builder.DAG.getLoad(LoadVT, Builder.getCurSDLoc(), Root,
Chris Lattnerecf42c42010-09-21 16:36:31 +00005388 Ptr, MachinePointerInfo(PtrVal),
David Greene1e559442010-02-15 17:00:31 +00005389 false /*volatile*/,
Pete Cooperd752e0f2011-11-08 18:42:53 +00005390 false /*nontemporal*/,
5391 false /*isinvariant*/, 1 /* align=1 */);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005392
Chris Lattner8047d9a2009-12-24 00:37:38 +00005393 if (!ConstantMemory)
5394 Builder.PendingLoads.push_back(LoadVal.getValue(1));
5395 return LoadVal;
5396}
5397
5398
5399/// visitMemCmpCall - See if we can lower a call to memcmp in an optimized form.
5400/// If so, return true and lower it, otherwise return false and it will be
5401/// lowered like a normal call.
Dan Gohman46510a72010-04-15 01:51:59 +00005402bool SelectionDAGBuilder::visitMemCmpCall(const CallInst &I) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00005403 // Verify that the prototype makes sense. int memcmp(void*,void*,size_t)
Gabor Greif37387d52010-06-30 12:55:46 +00005404 if (I.getNumArgOperands() != 3)
Chris Lattner8047d9a2009-12-24 00:37:38 +00005405 return false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005406
Gabor Greif0635f352010-06-25 09:38:13 +00005407 const Value *LHS = I.getArgOperand(0), *RHS = I.getArgOperand(1);
Duncan Sands1df98592010-02-16 11:11:14 +00005408 if (!LHS->getType()->isPointerTy() || !RHS->getType()->isPointerTy() ||
Gabor Greif0635f352010-06-25 09:38:13 +00005409 !I.getArgOperand(2)->getType()->isIntegerTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +00005410 !I.getType()->isIntegerTy())
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005411 return false;
5412
Gabor Greif0635f352010-06-25 09:38:13 +00005413 const ConstantInt *Size = dyn_cast<ConstantInt>(I.getArgOperand(2));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005414
Chris Lattner8047d9a2009-12-24 00:37:38 +00005415 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
5416 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
Chris Lattner04b091a2009-12-24 01:07:17 +00005417 if (Size && IsOnlyUsedInZeroEqualityComparison(&I)) {
5418 bool ActuallyDoIt = true;
5419 MVT LoadVT;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005420 Type *LoadTy;
Chris Lattner04b091a2009-12-24 01:07:17 +00005421 switch (Size->getZExtValue()) {
5422 default:
5423 LoadVT = MVT::Other;
5424 LoadTy = 0;
5425 ActuallyDoIt = false;
5426 break;
5427 case 2:
5428 LoadVT = MVT::i16;
5429 LoadTy = Type::getInt16Ty(Size->getContext());
5430 break;
5431 case 4:
5432 LoadVT = MVT::i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005433 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005434 break;
5435 case 8:
5436 LoadVT = MVT::i64;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005437 LoadTy = Type::getInt64Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005438 break;
5439 /*
5440 case 16:
5441 LoadVT = MVT::v4i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005442 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005443 LoadTy = VectorType::get(LoadTy, 4);
5444 break;
5445 */
5446 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005447
Chris Lattner04b091a2009-12-24 01:07:17 +00005448 // This turns into unaligned loads. We only do this if the target natively
5449 // supports the MVT we'll be loading or if it is small enough (<= 4) that
5450 // we'll only produce a small number of byte loads.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005451
Chris Lattner04b091a2009-12-24 01:07:17 +00005452 // Require that we can find a legal MVT, and only do this if the target
5453 // supports unaligned loads of that type. Expanding into byte loads would
5454 // bloat the code.
5455 if (ActuallyDoIt && Size->getZExtValue() > 4) {
5456 // TODO: Handle 5 byte compare as 4-byte + 1 byte.
5457 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
5458 if (!TLI.isTypeLegal(LoadVT) ||!TLI.allowsUnalignedMemoryAccesses(LoadVT))
5459 ActuallyDoIt = false;
5460 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005461
Chris Lattner04b091a2009-12-24 01:07:17 +00005462 if (ActuallyDoIt) {
5463 SDValue LHSVal = getMemCmpLoad(LHS, LoadVT, LoadTy, *this);
5464 SDValue RHSVal = getMemCmpLoad(RHS, LoadVT, LoadTy, *this);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005465
Andrew Trickac6d9be2013-05-25 02:42:55 +00005466 SDValue Res = DAG.getSetCC(getCurSDLoc(), MVT::i1, LHSVal, RHSVal,
Chris Lattner04b091a2009-12-24 01:07:17 +00005467 ISD::SETNE);
5468 EVT CallVT = TLI.getValueType(I.getType(), true);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005469 setValue(&I, DAG.getZExtOrTrunc(Res, getCurSDLoc(), CallVT));
Chris Lattner04b091a2009-12-24 01:07:17 +00005470 return true;
5471 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00005472 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005473
5474
Chris Lattner8047d9a2009-12-24 00:37:38 +00005475 return false;
5476}
5477
Bob Wilson53624a22012-08-03 23:29:17 +00005478/// visitUnaryFloatCall - If a call instruction is a unary floating-point
5479/// operation (as expected), translate it to an SDNode with the specified opcode
5480/// and return true.
5481bool SelectionDAGBuilder::visitUnaryFloatCall(const CallInst &I,
5482 unsigned Opcode) {
5483 // Sanity check that it really is a unary floating-point call.
5484 if (I.getNumArgOperands() != 1 ||
5485 !I.getArgOperand(0)->getType()->isFloatingPointTy() ||
5486 I.getType() != I.getArgOperand(0)->getType() ||
5487 !I.onlyReadsMemory())
5488 return false;
5489
5490 SDValue Tmp = getValue(I.getArgOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005491 setValue(&I, DAG.getNode(Opcode, getCurSDLoc(), Tmp.getValueType(), Tmp));
Bob Wilson53624a22012-08-03 23:29:17 +00005492 return true;
5493}
Chris Lattner8047d9a2009-12-24 00:37:38 +00005494
Dan Gohman46510a72010-04-15 01:51:59 +00005495void SelectionDAGBuilder::visitCall(const CallInst &I) {
Chris Lattner598751e2010-07-05 05:36:21 +00005496 // Handle inline assembly differently.
5497 if (isa<InlineAsm>(I.getCalledValue())) {
5498 visitInlineAsm(&I);
5499 return;
5500 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00005501
Michael J. Spencer391b43b2010-10-21 20:49:23 +00005502 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Michael J. Spencerc9c137b2012-02-22 19:06:13 +00005503 ComputeUsesVAFloatArgument(I, &MMI);
Michael J. Spencer391b43b2010-10-21 20:49:23 +00005504
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005505 const char *RenameFn = 0;
5506 if (Function *F = I.getCalledFunction()) {
5507 if (F->isDeclaration()) {
Chris Lattner598751e2010-07-05 05:36:21 +00005508 if (const TargetIntrinsicInfo *II = TM.getIntrinsicInfo()) {
Dale Johannesen49de9822009-02-05 01:49:45 +00005509 if (unsigned IID = II->getIntrinsicID(F)) {
5510 RenameFn = visitIntrinsicCall(I, IID);
5511 if (!RenameFn)
5512 return;
5513 }
5514 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005515 if (unsigned IID = F->getIntrinsicID()) {
5516 RenameFn = visitIntrinsicCall(I, IID);
5517 if (!RenameFn)
5518 return;
5519 }
5520 }
5521
5522 // Check for well-known libc/libm calls. If the function is internal, it
5523 // can't be a library call.
Bob Wilson982dc842012-08-03 21:26:24 +00005524 LibFunc::Func Func;
5525 if (!F->hasLocalLinkage() && F->hasName() &&
5526 LibInfo->getLibFunc(F->getName(), Func) &&
5527 LibInfo->hasOptimizedCodeGen(Func)) {
5528 switch (Func) {
5529 default: break;
5530 case LibFunc::copysign:
5531 case LibFunc::copysignf:
5532 case LibFunc::copysignl:
Gabor Greif37387d52010-06-30 12:55:46 +00005533 if (I.getNumArgOperands() == 2 && // Basic sanity checks.
Gabor Greif0635f352010-06-25 09:38:13 +00005534 I.getArgOperand(0)->getType()->isFloatingPointTy() &&
5535 I.getType() == I.getArgOperand(0)->getType() &&
Bob Wilson53624a22012-08-03 23:29:17 +00005536 I.getType() == I.getArgOperand(1)->getType() &&
5537 I.onlyReadsMemory()) {
Gabor Greif0635f352010-06-25 09:38:13 +00005538 SDValue LHS = getValue(I.getArgOperand(0));
5539 SDValue RHS = getValue(I.getArgOperand(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005540 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurSDLoc(),
Bill Wendling0d580132009-12-23 01:28:19 +00005541 LHS.getValueType(), LHS, RHS));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005542 return;
5543 }
Bob Wilson982dc842012-08-03 21:26:24 +00005544 break;
5545 case LibFunc::fabs:
5546 case LibFunc::fabsf:
5547 case LibFunc::fabsl:
Bob Wilson53624a22012-08-03 23:29:17 +00005548 if (visitUnaryFloatCall(I, ISD::FABS))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005549 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005550 break;
5551 case LibFunc::sin:
5552 case LibFunc::sinf:
5553 case LibFunc::sinl:
Bob Wilson53624a22012-08-03 23:29:17 +00005554 if (visitUnaryFloatCall(I, ISD::FSIN))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005555 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005556 break;
5557 case LibFunc::cos:
5558 case LibFunc::cosf:
5559 case LibFunc::cosl:
Bob Wilson53624a22012-08-03 23:29:17 +00005560 if (visitUnaryFloatCall(I, ISD::FCOS))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005561 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005562 break;
5563 case LibFunc::sqrt:
5564 case LibFunc::sqrtf:
5565 case LibFunc::sqrtl:
Preston Gurdb704d232013-05-27 15:44:35 +00005566 case LibFunc::sqrt_finite:
5567 case LibFunc::sqrtf_finite:
5568 case LibFunc::sqrtl_finite:
Bob Wilson53624a22012-08-03 23:29:17 +00005569 if (visitUnaryFloatCall(I, ISD::FSQRT))
Dale Johannesen52fb79b2009-09-25 17:23:22 +00005570 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005571 break;
5572 case LibFunc::floor:
5573 case LibFunc::floorf:
5574 case LibFunc::floorl:
Bob Wilson53624a22012-08-03 23:29:17 +00005575 if (visitUnaryFloatCall(I, ISD::FFLOOR))
Owen Anderson4a4fdf32011-12-08 19:32:14 +00005576 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005577 break;
5578 case LibFunc::nearbyint:
5579 case LibFunc::nearbyintf:
5580 case LibFunc::nearbyintl:
Bob Wilson53624a22012-08-03 23:29:17 +00005581 if (visitUnaryFloatCall(I, ISD::FNEARBYINT))
Owen Anderson4a4fdf32011-12-08 19:32:14 +00005582 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005583 break;
5584 case LibFunc::ceil:
5585 case LibFunc::ceilf:
5586 case LibFunc::ceill:
Bob Wilson53624a22012-08-03 23:29:17 +00005587 if (visitUnaryFloatCall(I, ISD::FCEIL))
Owen Anderson4a4fdf32011-12-08 19:32:14 +00005588 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005589 break;
5590 case LibFunc::rint:
5591 case LibFunc::rintf:
5592 case LibFunc::rintl:
Bob Wilson53624a22012-08-03 23:29:17 +00005593 if (visitUnaryFloatCall(I, ISD::FRINT))
Owen Anderson4a4fdf32011-12-08 19:32:14 +00005594 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005595 break;
5596 case LibFunc::trunc:
5597 case LibFunc::truncf:
5598 case LibFunc::truncl:
Bob Wilson53624a22012-08-03 23:29:17 +00005599 if (visitUnaryFloatCall(I, ISD::FTRUNC))
Owen Anderson4a4fdf32011-12-08 19:32:14 +00005600 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005601 break;
5602 case LibFunc::log2:
5603 case LibFunc::log2f:
5604 case LibFunc::log2l:
Bob Wilson53624a22012-08-03 23:29:17 +00005605 if (visitUnaryFloatCall(I, ISD::FLOG2))
Owen Anderson4e0adfa2011-12-15 00:54:12 +00005606 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005607 break;
5608 case LibFunc::exp2:
5609 case LibFunc::exp2f:
5610 case LibFunc::exp2l:
Bob Wilson53624a22012-08-03 23:29:17 +00005611 if (visitUnaryFloatCall(I, ISD::FEXP2))
Owen Anderson4e0adfa2011-12-15 00:54:12 +00005612 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005613 break;
5614 case LibFunc::memcmp:
Chris Lattner8047d9a2009-12-24 00:37:38 +00005615 if (visitMemCmpCall(I))
5616 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005617 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005618 }
5619 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005620 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00005621
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005622 SDValue Callee;
5623 if (!RenameFn)
Gabor Greif0635f352010-06-25 09:38:13 +00005624 Callee = getValue(I.getCalledValue());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005625 else
Bill Wendling056292f2008-09-16 21:48:12 +00005626 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005627
Bill Wendling0d580132009-12-23 01:28:19 +00005628 // Check if we can potentially perform a tail call. More detailed checking is
5629 // be done within LowerCallTo, after more information about the call is known.
Evan Cheng11e67932010-01-26 23:13:04 +00005630 LowerCallTo(&I, Callee, I.isTailCall());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005631}
5632
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005633namespace {
Dan Gohman462f6b52010-05-29 17:53:24 +00005634
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005635/// AsmOperandInfo - This contains information for each constraint that we are
5636/// lowering.
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005637class SDISelAsmOperandInfo : public TargetLowering::AsmOperandInfo {
Cedric Venetaff9c272009-02-14 16:06:42 +00005638public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005639 /// CallOperand - If this is the result output operand or a clobber
5640 /// this is null, otherwise it is the incoming operand to the CallInst.
5641 /// This gets modified as the asm is processed.
5642 SDValue CallOperand;
5643
5644 /// AssignedRegs - If this is a register or register class operand, this
5645 /// contains the set of register corresponding to the operand.
5646 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005647
John Thompsoneac6e1d2010-09-13 18:15:37 +00005648 explicit SDISelAsmOperandInfo(const TargetLowering::AsmOperandInfo &info)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005649 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
5650 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005651
Owen Andersone50ed302009-08-10 22:56:29 +00005652 /// getCallOperandValEVT - Return the EVT of the Value* that this operand
Chris Lattner81249c92008-10-17 17:05:25 +00005653 /// corresponds to. If there is no Value* for this operand, it returns
Owen Anderson825b72b2009-08-11 20:47:22 +00005654 /// MVT::Other.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005655 EVT getCallOperandValEVT(LLVMContext &Context,
Owen Anderson1d0be152009-08-13 21:58:54 +00005656 const TargetLowering &TLI,
Micah Villmow3574eca2012-10-08 16:38:25 +00005657 const DataLayout *TD) const {
Owen Anderson825b72b2009-08-11 20:47:22 +00005658 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005659
Chris Lattner81249c92008-10-17 17:05:25 +00005660 if (isa<BasicBlock>(CallOperandVal))
5661 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005662
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005663 llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005664
Eric Christophercef81b72011-05-09 20:04:43 +00005665 // FIXME: code duplicated from TargetLowering::ParseConstraints().
Chris Lattner81249c92008-10-17 17:05:25 +00005666 // If this is an indirect operand, the operand is a pointer to the
5667 // accessed type.
Bob Wilsone261b0c2009-12-22 18:34:19 +00005668 if (isIndirect) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005669 llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
Bob Wilsone261b0c2009-12-22 18:34:19 +00005670 if (!PtrTy)
Chris Lattner75361b62010-04-07 22:58:41 +00005671 report_fatal_error("Indirect operand for inline asm not a pointer!");
Bob Wilsone261b0c2009-12-22 18:34:19 +00005672 OpTy = PtrTy->getElementType();
5673 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005674
Eric Christophercef81b72011-05-09 20:04:43 +00005675 // Look for vector wrapped in a struct. e.g. { <16 x i8> }.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005676 if (StructType *STy = dyn_cast<StructType>(OpTy))
Eric Christophercef81b72011-05-09 20:04:43 +00005677 if (STy->getNumElements() == 1)
5678 OpTy = STy->getElementType(0);
5679
Chris Lattner81249c92008-10-17 17:05:25 +00005680 // If OpTy is not a single value, it may be a struct/union that we
5681 // can tile with integers.
5682 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
5683 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
5684 switch (BitSize) {
5685 default: break;
5686 case 1:
5687 case 8:
5688 case 16:
5689 case 32:
5690 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00005691 case 128:
Owen Anderson1d0be152009-08-13 21:58:54 +00005692 OpTy = IntegerType::get(Context, BitSize);
Chris Lattner81249c92008-10-17 17:05:25 +00005693 break;
5694 }
5695 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005696
Chris Lattner81249c92008-10-17 17:05:25 +00005697 return TLI.getValueType(OpTy, true);
5698 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005699};
Dan Gohman462f6b52010-05-29 17:53:24 +00005700
John Thompson44ab89e2010-10-29 17:29:13 +00005701typedef SmallVector<SDISelAsmOperandInfo,16> SDISelAsmOperandInfoVector;
5702
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005703} // end anonymous namespace
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005704
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005705/// GetRegistersForValue - Assign registers (virtual or physical) for the
5706/// specified operand. We prefer to assign virtual registers, to allow the
Bob Wilson266d9452009-12-17 05:07:36 +00005707/// register allocator to handle the assignment process. However, if the asm
5708/// uses features that we can't model on machineinstrs, we have SDISel do the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005709/// allocation. This produces generally horrible, but correct, code.
5710///
5711/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005712///
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005713static void GetRegistersForValue(SelectionDAG &DAG,
5714 const TargetLowering &TLI,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005715 SDLoc DL,
Benjamin Kramer8b93ff22012-02-24 14:01:17 +00005716 SDISelAsmOperandInfo &OpInfo) {
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005717 LLVMContext &Context = *DAG.getContext();
Owen Anderson23b9b192009-08-12 00:36:31 +00005718
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005719 MachineFunction &MF = DAG.getMachineFunction();
5720 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005721
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005722 // If this is a constraint for a single physreg, or a constraint for a
5723 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005724 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005725 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
5726 OpInfo.ConstraintVT);
5727
5728 unsigned NumRegs = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00005729 if (OpInfo.ConstraintVT != MVT::Other) {
Chris Lattner01426e12008-10-21 00:45:36 +00005730 // If this is a FP input in an integer register (or visa versa) insert a bit
5731 // cast of the input value. More generally, handle any case where the input
5732 // value disagrees with the register class we plan to stick this in.
5733 if (OpInfo.Type == InlineAsm::isInput &&
5734 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00005735 // Try to convert to the first EVT that the reg class contains. If the
Chris Lattner01426e12008-10-21 00:45:36 +00005736 // types are identical size, use a bitcast to convert (e.g. two differing
5737 // vector types).
Patrik Hagglund8963fec2012-12-19 12:23:01 +00005738 MVT RegVT = *PhysReg.second->vt_begin();
Chris Lattner01426e12008-10-21 00:45:36 +00005739 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005740 OpInfo.CallOperand = DAG.getNode(ISD::BITCAST, DL,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005741 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005742 OpInfo.ConstraintVT = RegVT;
5743 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
5744 // If the input is a FP value and we want it in FP registers, do a
5745 // bitcast to the corresponding integer type. This turns an f64 value
5746 // into i64, which can be passed with two i32 values on a 32-bit
5747 // machine.
Patrik Hagglund8963fec2012-12-19 12:23:01 +00005748 RegVT = MVT::getIntegerVT(OpInfo.ConstraintVT.getSizeInBits());
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005749 OpInfo.CallOperand = DAG.getNode(ISD::BITCAST, DL,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005750 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005751 OpInfo.ConstraintVT = RegVT;
5752 }
5753 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005754
Owen Anderson23b9b192009-08-12 00:36:31 +00005755 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00005756 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005757
Patrik Hagglund8963fec2012-12-19 12:23:01 +00005758 MVT RegVT;
Owen Andersone50ed302009-08-10 22:56:29 +00005759 EVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005760
5761 // If this is a constraint for a specific physical register, like {r17},
5762 // assign it now.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005763 if (unsigned AssignedReg = PhysReg.first) {
5764 const TargetRegisterClass *RC = PhysReg.second;
Owen Anderson825b72b2009-08-11 20:47:22 +00005765 if (OpInfo.ConstraintVT == MVT::Other)
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005766 ValueVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005767
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005768 // Get the actual register value type. This is important, because the user
5769 // may have asked for (e.g.) the AX register in i32 type. We need to
5770 // remember that AX is actually i16 to get the right extension.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005771 RegVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005772
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005773 // This is a explicit reference to a physical register.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005774 Regs.push_back(AssignedReg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005775
5776 // If this is an expanded reference, add the rest of the regs to Regs.
5777 if (NumRegs != 1) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005778 TargetRegisterClass::iterator I = RC->begin();
5779 for (; *I != AssignedReg; ++I)
5780 assert(I != RC->end() && "Didn't find reg!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005781
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005782 // Already added the first reg.
5783 --NumRegs; ++I;
5784 for (; NumRegs; --NumRegs, ++I) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005785 assert(I != RC->end() && "Ran out of registers to allocate!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005786 Regs.push_back(*I);
5787 }
5788 }
Bill Wendling651ad132009-12-22 01:25:10 +00005789
Dan Gohman7451d3e2010-05-29 17:03:36 +00005790 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005791 return;
5792 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005793
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005794 // Otherwise, if this was a reference to an LLVM register class, create vregs
5795 // for this reference.
Chris Lattnerb3b44842009-03-24 15:25:07 +00005796 if (const TargetRegisterClass *RC = PhysReg.second) {
5797 RegVT = *RC->vt_begin();
Owen Anderson825b72b2009-08-11 20:47:22 +00005798 if (OpInfo.ConstraintVT == MVT::Other)
Evan Chengfb112882009-03-23 08:01:15 +00005799 ValueVT = RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005800
Evan Chengfb112882009-03-23 08:01:15 +00005801 // Create the appropriate number of virtual registers.
5802 MachineRegisterInfo &RegInfo = MF.getRegInfo();
5803 for (; NumRegs; --NumRegs)
Chris Lattnerb3b44842009-03-24 15:25:07 +00005804 Regs.push_back(RegInfo.createVirtualRegister(RC));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005805
Dan Gohman7451d3e2010-05-29 17:03:36 +00005806 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
Evan Chengfb112882009-03-23 08:01:15 +00005807 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005808 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005809
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005810 // Otherwise, we couldn't allocate enough registers for this.
5811}
5812
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005813/// visitInlineAsm - Handle a call to an InlineAsm object.
5814///
Dan Gohman46510a72010-04-15 01:51:59 +00005815void SelectionDAGBuilder::visitInlineAsm(ImmutableCallSite CS) {
5816 const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005817
5818 /// ConstraintOperands - Information about all of the constraints.
John Thompson44ab89e2010-10-29 17:29:13 +00005819 SDISelAsmOperandInfoVector ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005820
Evan Chengce1cdac2011-05-06 20:52:23 +00005821 TargetLowering::AsmOperandInfoVector
5822 TargetConstraints = TLI.ParseConstraints(CS);
5823
John Thompsoneac6e1d2010-09-13 18:15:37 +00005824 bool hasMemory = false;
Michael J. Spencere70c5262010-10-16 08:25:21 +00005825
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005826 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
5827 unsigned ResNo = 0; // ResNo - The result number of the next output.
John Thompsoneac6e1d2010-09-13 18:15:37 +00005828 for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) {
5829 ConstraintOperands.push_back(SDISelAsmOperandInfo(TargetConstraints[i]));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005830 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Michael J. Spencere70c5262010-10-16 08:25:21 +00005831
Patrik Hagglundc698d3a2012-12-19 15:19:11 +00005832 MVT OpVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005833
5834 // Compute the value type for each operand.
5835 switch (OpInfo.Type) {
5836 case InlineAsm::isOutput:
5837 // Indirect outputs just consume an argument.
5838 if (OpInfo.isIndirect) {
Dan Gohman46510a72010-04-15 01:51:59 +00005839 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005840 break;
5841 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005842
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005843 // The return value of the call is this value. As such, there is no
5844 // corresponding argument.
Nick Lewycky8de34002011-09-30 22:19:53 +00005845 assert(!CS.getType()->isVoidTy() && "Bad inline asm!");
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005846 if (StructType *STy = dyn_cast<StructType>(CS.getType())) {
Patrik Hagglundc698d3a2012-12-19 15:19:11 +00005847 OpVT = TLI.getSimpleValueType(STy->getElementType(ResNo));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005848 } else {
5849 assert(ResNo == 0 && "Asm only has one result!");
Patrik Hagglundc698d3a2012-12-19 15:19:11 +00005850 OpVT = TLI.getSimpleValueType(CS.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005851 }
5852 ++ResNo;
5853 break;
5854 case InlineAsm::isInput:
Dan Gohman46510a72010-04-15 01:51:59 +00005855 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005856 break;
5857 case InlineAsm::isClobber:
5858 // Nothing to do.
5859 break;
5860 }
5861
5862 // If this is an input or an indirect output, process the call argument.
5863 // BasicBlocks are labels, currently appearing only in asm's.
5864 if (OpInfo.CallOperandVal) {
Dan Gohman46510a72010-04-15 01:51:59 +00005865 if (const BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005866 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00005867 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005868 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005869 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005870
Patrik Hagglundc698d3a2012-12-19 15:19:11 +00005871 OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD).
5872 getSimpleVT();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005873 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005874
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005875 OpInfo.ConstraintVT = OpVT;
Michael J. Spencere70c5262010-10-16 08:25:21 +00005876
John Thompsoneac6e1d2010-09-13 18:15:37 +00005877 // Indirect operand accesses access memory.
5878 if (OpInfo.isIndirect)
5879 hasMemory = true;
5880 else {
5881 for (unsigned j = 0, ee = OpInfo.Codes.size(); j != ee; ++j) {
Evan Chengce1cdac2011-05-06 20:52:23 +00005882 TargetLowering::ConstraintType
5883 CType = TLI.getConstraintType(OpInfo.Codes[j]);
John Thompsoneac6e1d2010-09-13 18:15:37 +00005884 if (CType == TargetLowering::C_Memory) {
5885 hasMemory = true;
5886 break;
5887 }
5888 }
5889 }
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005890 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005891
John Thompsoneac6e1d2010-09-13 18:15:37 +00005892 SDValue Chain, Flag;
5893
5894 // We won't need to flush pending loads if this asm doesn't touch
5895 // memory and is nonvolatile.
5896 if (hasMemory || IA->hasSideEffects())
5897 Chain = getRoot();
5898 else
5899 Chain = DAG.getRoot();
5900
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005901 // Second pass over the constraints: compute which constraint option to use
5902 // and assign registers to constraints that want a specific physreg.
John Thompsoneac6e1d2010-09-13 18:15:37 +00005903 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005904 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005905
John Thompson54584742010-09-24 22:24:05 +00005906 // If this is an output operand with a matching input operand, look up the
5907 // matching input. If their types mismatch, e.g. one is an integer, the
5908 // other is floating point, or their sizes are different, flag it as an
5909 // error.
5910 if (OpInfo.hasMatchingInput()) {
5911 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
Michael J. Spencere70c5262010-10-16 08:25:21 +00005912
John Thompson54584742010-09-24 22:24:05 +00005913 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Bill Wendling96cb1122012-07-19 00:04:14 +00005914 std::pair<unsigned, const TargetRegisterClass*> MatchRC =
5915 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
Evan Cheng1dafa702011-08-23 19:17:21 +00005916 OpInfo.ConstraintVT);
Bill Wendling96cb1122012-07-19 00:04:14 +00005917 std::pair<unsigned, const TargetRegisterClass*> InputRC =
5918 TLI.getRegForInlineAsmConstraint(Input.ConstraintCode,
Evan Cheng1dafa702011-08-23 19:17:21 +00005919 Input.ConstraintVT);
John Thompson54584742010-09-24 22:24:05 +00005920 if ((OpInfo.ConstraintVT.isInteger() !=
5921 Input.ConstraintVT.isInteger()) ||
Eric Christopher5427ede2011-07-14 20:13:52 +00005922 (MatchRC.second != InputRC.second)) {
John Thompson54584742010-09-24 22:24:05 +00005923 report_fatal_error("Unsupported asm: input constraint"
5924 " with a matching output constraint of"
5925 " incompatible type!");
5926 }
5927 Input.ConstraintVT = OpInfo.ConstraintVT;
5928 }
5929 }
5930
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005931 // Compute the constraint code and ConstraintType to use.
Dale Johannesen1784d162010-06-25 21:55:36 +00005932 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005933
Eric Christopherfffe3632013-01-11 18:12:39 +00005934 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
5935 OpInfo.Type == InlineAsm::isClobber)
5936 continue;
5937
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005938 // If this is a memory input, and if the operand is not indirect, do what we
5939 // need to to provide an address for the memory input.
5940 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
5941 !OpInfo.isIndirect) {
Evan Chengce1cdac2011-05-06 20:52:23 +00005942 assert((OpInfo.isMultipleAlternative ||
5943 (OpInfo.Type == InlineAsm::isInput)) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005944 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005945
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005946 // Memory operands really want the address of the value. If we don't have
5947 // an indirect input, put it in the constpool if we can, otherwise spill
5948 // it to a stack slot.
Eric Christophere0b42c02011-06-03 17:21:23 +00005949 // TODO: This isn't quite right. We need to handle these according to
5950 // the addressing mode that the constraint wants. Also, this may take
5951 // an additional register for the computation and we don't want that
5952 // either.
Eric Christopher471e4222011-06-08 23:55:35 +00005953
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005954 // If the operand is a float, integer, or vector constant, spill to a
5955 // constant pool entry to get its address.
Dan Gohman46510a72010-04-15 01:51:59 +00005956 const Value *OpVal = OpInfo.CallOperandVal;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005957 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
Chris Lattnera78fa8c2012-01-27 03:08:05 +00005958 isa<ConstantVector>(OpVal) || isa<ConstantDataVector>(OpVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005959 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
5960 TLI.getPointerTy());
5961 } else {
5962 // Otherwise, create a stack slot and emit a store to it before the
5963 // asm.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005964 Type *Ty = OpVal->getType();
Micah Villmow3574eca2012-10-08 16:38:25 +00005965 uint64_t TySize = TLI.getDataLayout()->getTypeAllocSize(Ty);
5966 unsigned Align = TLI.getDataLayout()->getPrefTypeAlignment(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005967 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005968 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005969 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005970 Chain = DAG.getStore(Chain, getCurSDLoc(),
Chris Lattnerecf42c42010-09-21 16:36:31 +00005971 OpInfo.CallOperand, StackSlot,
5972 MachinePointerInfo::getFixedStack(SSFI),
David Greene1e559442010-02-15 17:00:31 +00005973 false, false, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005974 OpInfo.CallOperand = StackSlot;
5975 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005976
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005977 // There is no longer a Value* corresponding to this operand.
5978 OpInfo.CallOperandVal = 0;
Bill Wendling651ad132009-12-22 01:25:10 +00005979
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005980 // It is now an indirect operand.
5981 OpInfo.isIndirect = true;
5982 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005983
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005984 // If this constraint is for a specific register, allocate it before
5985 // anything else.
5986 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005987 GetRegistersForValue(DAG, TLI, getCurSDLoc(), OpInfo);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005988 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005989
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005990 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00005991 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005992 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5993 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005994
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005995 // C_Register operands have already been allocated, Other/Memory don't need
5996 // to be.
5997 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005998 GetRegistersForValue(DAG, TLI, getCurSDLoc(), OpInfo);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005999 }
6000
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006001 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
6002 std::vector<SDValue> AsmNodeOperands;
6003 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
6004 AsmNodeOperands.push_back(
Dan Gohmanf2d7fb32010-01-04 21:00:54 +00006005 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(),
6006 TLI.getPointerTy()));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006007
Chris Lattnerdecc2672010-04-07 05:20:54 +00006008 // If we have a !srcloc metadata node associated with it, we want to attach
6009 // this to the ultimately generated inline asm machineinstr. To do this, we
6010 // pass in the third operand as this (potentially null) inline asm MDNode.
6011 const MDNode *SrcLoc = CS.getInstruction()->getMetadata("srcloc");
6012 AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006013
Chad Rosier3d716882012-10-30 19:11:54 +00006014 // Remember the HasSideEffect, AlignStack, AsmDialect, MayLoad and MayStore
6015 // bits as operand 3.
Evan Chengc36b7062011-01-07 23:50:32 +00006016 unsigned ExtraInfo = 0;
6017 if (IA->hasSideEffects())
6018 ExtraInfo |= InlineAsm::Extra_HasSideEffects;
6019 if (IA->isAlignStack())
6020 ExtraInfo |= InlineAsm::Extra_IsAlignStack;
Chad Rosier77fffa62012-09-05 22:17:43 +00006021 // Set the asm dialect.
Chad Rosier2f1d8152012-09-05 22:40:13 +00006022 ExtraInfo |= IA->getDialect() * InlineAsm::Extra_AsmDialect;
Chad Rosier3d716882012-10-30 19:11:54 +00006023
6024 // Determine if this InlineAsm MayLoad or MayStore based on the constraints.
6025 for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) {
6026 TargetLowering::AsmOperandInfo &OpInfo = TargetConstraints[i];
6027
6028 // Compute the constraint code and ConstraintType to use.
6029 TLI.ComputeConstraintToUse(OpInfo, SDValue());
6030
Chad Rosierdfa4cec2012-10-30 20:01:12 +00006031 // Ideally, we would only check against memory constraints. However, the
6032 // meaning of an other constraint can be target-specific and we can't easily
6033 // reason about it. Therefore, be conservative and set MayLoad/MayStore
6034 // for other constriants as well.
Chad Rosier3d716882012-10-30 19:11:54 +00006035 if (OpInfo.ConstraintType == TargetLowering::C_Memory ||
6036 OpInfo.ConstraintType == TargetLowering::C_Other) {
6037 if (OpInfo.Type == InlineAsm::isInput)
6038 ExtraInfo |= InlineAsm::Extra_MayLoad;
6039 else if (OpInfo.Type == InlineAsm::isOutput)
6040 ExtraInfo |= InlineAsm::Extra_MayStore;
Eric Christopherfffe3632013-01-11 18:12:39 +00006041 else if (OpInfo.Type == InlineAsm::isClobber)
6042 ExtraInfo |= (InlineAsm::Extra_MayLoad | InlineAsm::Extra_MayStore);
Chad Rosier3d716882012-10-30 19:11:54 +00006043 }
6044 }
6045
Evan Chengc36b7062011-01-07 23:50:32 +00006046 AsmNodeOperands.push_back(DAG.getTargetConstant(ExtraInfo,
6047 TLI.getPointerTy()));
Dale Johannesenf1e309e2010-07-02 20:16:09 +00006048
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006049 // Loop over all of the inputs, copying the operand values into the
6050 // appropriate registers and processing the output regs.
6051 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006052
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006053 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
6054 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006055
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006056 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
6057 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
6058
6059 switch (OpInfo.Type) {
6060 case InlineAsm::isOutput: {
6061 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
6062 OpInfo.ConstraintType != TargetLowering::C_Register) {
6063 // Memory output, or 'other' output (e.g. 'X' constraint).
6064 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
6065
6066 // Add information to the INLINEASM node to know about this output.
Chris Lattnerdecc2672010-04-07 05:20:54 +00006067 unsigned OpFlags = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
6068 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006069 TLI.getPointerTy()));
6070 AsmNodeOperands.push_back(OpInfo.CallOperand);
6071 break;
6072 }
6073
6074 // Otherwise, this is a register or register class output.
6075
6076 // Copy the output from the appropriate register. Find a register that
6077 // we can use.
Chris Lattnerfcd70902012-01-03 23:51:01 +00006078 if (OpInfo.AssignedRegs.Regs.empty()) {
6079 LLVMContext &Ctx = *DAG.getContext();
6080 Ctx.emitError(CS.getInstruction(),
6081 "couldn't allocate output register for constraint '" +
6082 Twine(OpInfo.ConstraintCode) + "'");
6083 break;
6084 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006085
6086 // If this is an indirect operand, store through the pointer after the
6087 // asm.
6088 if (OpInfo.isIndirect) {
6089 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
6090 OpInfo.CallOperandVal));
6091 } else {
6092 // This is the result value of the call.
Benjamin Kramerf0127052010-01-05 13:12:22 +00006093 assert(!CS.getType()->isVoidTy() && "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006094 // Concatenate this output onto the outputs list.
6095 RetValRegs.append(OpInfo.AssignedRegs);
6096 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006097
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006098 // Add information to the INLINEASM node to know that this register is
6099 // set.
Dale Johannesen913d3df2008-09-12 17:49:03 +00006100 OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ?
Chris Lattnerdecc2672010-04-07 05:20:54 +00006101 InlineAsm::Kind_RegDefEarlyClobber :
6102 InlineAsm::Kind_RegDef,
Evan Chengfb112882009-03-23 08:01:15 +00006103 false,
6104 0,
Bill Wendling46ada192010-03-02 01:55:18 +00006105 DAG,
Bill Wendling651ad132009-12-22 01:25:10 +00006106 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006107 break;
6108 }
6109 case InlineAsm::isInput: {
6110 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006111
Chris Lattner6bdcda32008-10-17 16:47:46 +00006112 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006113 // If this is required to match an output register we have already set,
6114 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00006115 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006116
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006117 // Scan until we find the definition we already emitted of this operand.
6118 // When we find it, create a RegsForValue operand.
Chris Lattnerdecc2672010-04-07 05:20:54 +00006119 unsigned CurOp = InlineAsm::Op_FirstOperand;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006120 for (; OperandNo; --OperandNo) {
6121 // Advance to the next operand.
Evan Cheng697cbbf2009-03-20 18:03:34 +00006122 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006123 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00006124 assert((InlineAsm::isRegDefKind(OpFlag) ||
6125 InlineAsm::isRegDefEarlyClobberKind(OpFlag) ||
6126 InlineAsm::isMemKind(OpFlag)) && "Skipped past definitions?");
Evan Cheng697cbbf2009-03-20 18:03:34 +00006127 CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006128 }
6129
Evan Cheng697cbbf2009-03-20 18:03:34 +00006130 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006131 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00006132 if (InlineAsm::isRegDefKind(OpFlag) ||
6133 InlineAsm::isRegDefEarlyClobberKind(OpFlag)) {
Evan Cheng697cbbf2009-03-20 18:03:34 +00006134 // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
Chris Lattner6129c372010-04-08 00:09:16 +00006135 if (OpInfo.isIndirect) {
6136 // This happens on gcc/testsuite/gcc.dg/pr8788-1.c
Dan Gohman99be8ae2010-04-19 22:41:47 +00006137 LLVMContext &Ctx = *DAG.getContext();
Chris Lattner6129c372010-04-08 00:09:16 +00006138 Ctx.emitError(CS.getInstruction(), "inline asm not supported yet:"
6139 " don't know how to handle tied "
6140 "indirect register inputs");
Chad Rosier75900222013-03-01 19:12:05 +00006141 report_fatal_error("Cannot handle indirect register inputs!");
Chris Lattner6129c372010-04-08 00:09:16 +00006142 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00006143
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006144 RegsForValue MatchedRegs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006145 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00006146 MVT RegVT = AsmNodeOperands[CurOp+1].getSimpleValueType();
Evan Chengfb112882009-03-23 08:01:15 +00006147 MatchedRegs.RegVTs.push_back(RegVT);
6148 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
Evan Cheng697cbbf2009-03-20 18:03:34 +00006149 for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag);
Chad Rosier2871ba92013-04-24 22:53:10 +00006150 i != e; ++i) {
6151 if (const TargetRegisterClass *RC = TLI.getRegClassFor(RegVT))
6152 MatchedRegs.Regs.push_back(RegInfo.createVirtualRegister(RC));
6153 else {
6154 LLVMContext &Ctx = *DAG.getContext();
6155 Ctx.emitError(CS.getInstruction(), "inline asm error: This value"
6156 " type register class is not natively supported!");
6157 report_fatal_error("inline asm error: This value type register "
6158 "class is not natively supported!");
6159 }
6160 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006161 // Use the produced MatchedRegs object to
Andrew Trickac6d9be2013-05-25 02:42:55 +00006162 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurSDLoc(),
Bill Wendlingf18eb582012-09-26 06:16:18 +00006163 Chain, &Flag, CS.getInstruction());
Chris Lattnerdecc2672010-04-07 05:20:54 +00006164 MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse,
Evan Chengfb112882009-03-23 08:01:15 +00006165 true, OpInfo.getMatchedOperand(),
Bill Wendling46ada192010-03-02 01:55:18 +00006166 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006167 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006168 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00006169
Chris Lattnerdecc2672010-04-07 05:20:54 +00006170 assert(InlineAsm::isMemKind(OpFlag) && "Unknown matching constraint!");
6171 assert(InlineAsm::getNumOperandRegisters(OpFlag) == 1 &&
6172 "Unexpected number of operands");
6173 // Add information to the INLINEASM node to know about this input.
6174 // See InlineAsm.h isUseOperandTiedToDef.
6175 OpFlag = InlineAsm::getFlagWordForMatchingOp(OpFlag,
6176 OpInfo.getMatchedOperand());
6177 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag,
6178 TLI.getPointerTy()));
6179 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
6180 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006181 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006182
Dale Johannesenb5611a62010-07-13 20:17:05 +00006183 // Treat indirect 'X' constraint as memory.
Michael J. Spencere70c5262010-10-16 08:25:21 +00006184 if (OpInfo.ConstraintType == TargetLowering::C_Other &&
6185 OpInfo.isIndirect)
Dale Johannesenb5611a62010-07-13 20:17:05 +00006186 OpInfo.ConstraintType = TargetLowering::C_Memory;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006187
Dale Johannesenb5611a62010-07-13 20:17:05 +00006188 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006189 std::vector<SDValue> Ops;
Eric Christopher100c8332011-06-02 23:16:42 +00006190 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode,
Dale Johannesen1784d162010-06-25 21:55:36 +00006191 Ops, DAG);
Chris Lattnerfcd70902012-01-03 23:51:01 +00006192 if (Ops.empty()) {
6193 LLVMContext &Ctx = *DAG.getContext();
6194 Ctx.emitError(CS.getInstruction(),
6195 "invalid operand for inline asm constraint '" +
6196 Twine(OpInfo.ConstraintCode) + "'");
6197 break;
6198 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006199
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006200 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00006201 unsigned ResOpType =
6202 InlineAsm::getFlagWord(InlineAsm::Kind_Imm, Ops.size());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006203 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006204 TLI.getPointerTy()));
6205 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
6206 break;
Chris Lattnerdecc2672010-04-07 05:20:54 +00006207 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00006208
Chris Lattnerdecc2672010-04-07 05:20:54 +00006209 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006210 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
6211 assert(InOperandVal.getValueType() == TLI.getPointerTy() &&
6212 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006213
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006214 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00006215 unsigned ResOpType = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
Dale Johannesen86b49f82008-09-24 01:07:17 +00006216 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006217 TLI.getPointerTy()));
6218 AsmNodeOperands.push_back(InOperandVal);
6219 break;
6220 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006221
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006222 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
6223 OpInfo.ConstraintType == TargetLowering::C_Register) &&
6224 "Unknown constraint type!");
Eric Christopher9eb4f8a2012-07-02 21:16:43 +00006225
6226 // TODO: Support this.
6227 if (OpInfo.isIndirect) {
6228 LLVMContext &Ctx = *DAG.getContext();
6229 Ctx.emitError(CS.getInstruction(),
6230 "Don't know how to handle indirect register inputs yet "
6231 "for constraint '" + Twine(OpInfo.ConstraintCode) + "'");
6232 break;
6233 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006234
6235 // Copy the input into the appropriate registers.
Chris Lattnerfcd70902012-01-03 23:51:01 +00006236 if (OpInfo.AssignedRegs.Regs.empty()) {
6237 LLVMContext &Ctx = *DAG.getContext();
6238 Ctx.emitError(CS.getInstruction(),
6239 "couldn't allocate input reg for constraint '" +
6240 Twine(OpInfo.ConstraintCode) + "'");
6241 break;
6242 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006243
Andrew Trickac6d9be2013-05-25 02:42:55 +00006244 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurSDLoc(),
Bill Wendlingf18eb582012-09-26 06:16:18 +00006245 Chain, &Flag, CS.getInstruction());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006246
Chris Lattnerdecc2672010-04-07 05:20:54 +00006247 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse, false, 0,
Bill Wendling46ada192010-03-02 01:55:18 +00006248 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006249 break;
6250 }
6251 case InlineAsm::isClobber: {
6252 // Add the clobbered value to the operand list, so that the register
6253 // allocator is aware that the physreg got clobbered.
6254 if (!OpInfo.AssignedRegs.Regs.empty())
Jakob Stoklund Olesenf792fa92011-06-27 04:08:33 +00006255 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_Clobber,
Bill Wendling46ada192010-03-02 01:55:18 +00006256 false, 0, DAG,
Bill Wendling651ad132009-12-22 01:25:10 +00006257 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006258 break;
6259 }
6260 }
6261 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006262
Chris Lattnerdecc2672010-04-07 05:20:54 +00006263 // Finish up input operands. Set the input chain and add the flag last.
Dale Johannesenf1e309e2010-07-02 20:16:09 +00006264 AsmNodeOperands[InlineAsm::Op_InputChain] = Chain;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006265 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006266
Andrew Trickac6d9be2013-05-25 02:42:55 +00006267 Chain = DAG.getNode(ISD::INLINEASM, getCurSDLoc(),
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006268 DAG.getVTList(MVT::Other, MVT::Glue),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006269 &AsmNodeOperands[0], AsmNodeOperands.size());
6270 Flag = Chain.getValue(1);
6271
6272 // If this asm returns a register value, copy the result from that register
6273 // and set it as the value of the call.
6274 if (!RetValRegs.Regs.empty()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006275 SDValue Val = RetValRegs.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(),
Bill Wendling12931302012-09-26 04:04:19 +00006276 Chain, &Flag, CS.getInstruction());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006277
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006278 // FIXME: Why don't we do this for inline asms with MRVs?
6279 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
Owen Andersone50ed302009-08-10 22:56:29 +00006280 EVT ResultType = TLI.getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006281
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006282 // If any of the results of the inline asm is a vector, it may have the
6283 // wrong width/num elts. This can happen for register classes that can
6284 // contain multiple different value types. The preg or vreg allocated may
6285 // not have the same VT as was expected. Convert it to the right type
6286 // with bit_convert.
6287 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006288 Val = DAG.getNode(ISD::BITCAST, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00006289 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00006290
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006291 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006292 ResultType.isInteger() && Val.getValueType().isInteger()) {
6293 // If a result value was tied to an input value, the computed result may
6294 // have a wider width than the expected result. Extract the relevant
6295 // portion.
Andrew Trickac6d9be2013-05-25 02:42:55 +00006296 Val = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00006297 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006298
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006299 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Chris Lattner0c526442008-10-17 17:52:49 +00006300 }
Dan Gohman95915732008-10-18 01:03:45 +00006301
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006302 setValue(CS.getInstruction(), Val);
Dale Johannesenec65a7d2009-04-14 00:56:56 +00006303 // Don't need to use this as a chain in this case.
6304 if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty())
6305 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006306 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006307
Dan Gohman46510a72010-04-15 01:51:59 +00006308 std::vector<std::pair<SDValue, const Value *> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006309
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006310 // Process indirect outputs, first output all of the flagged copies out of
6311 // physregs.
6312 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
6313 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
Dan Gohman46510a72010-04-15 01:51:59 +00006314 const Value *Ptr = IndirectStoresToEmit[i].second;
Andrew Trickac6d9be2013-05-25 02:42:55 +00006315 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(),
Bill Wendling12931302012-09-26 04:04:19 +00006316 Chain, &Flag, IA);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006317 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
6318 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006319
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006320 // Emit the non-flagged stores from the physregs.
6321 SmallVector<SDValue, 8> OutChains;
Bill Wendling651ad132009-12-22 01:25:10 +00006322 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006323 SDValue Val = DAG.getStore(Chain, getCurSDLoc(),
Bill Wendling651ad132009-12-22 01:25:10 +00006324 StoresToEmit[i].first,
6325 getValue(StoresToEmit[i].second),
Chris Lattner84bd98a2010-09-21 18:58:22 +00006326 MachinePointerInfo(StoresToEmit[i].second),
David Greene1e559442010-02-15 17:00:31 +00006327 false, false, 0);
Bill Wendling651ad132009-12-22 01:25:10 +00006328 OutChains.push_back(Val);
Bill Wendling651ad132009-12-22 01:25:10 +00006329 }
6330
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006331 if (!OutChains.empty())
Andrew Trickac6d9be2013-05-25 02:42:55 +00006332 Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006333 &OutChains[0], OutChains.size());
Bill Wendling651ad132009-12-22 01:25:10 +00006334
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006335 DAG.setRoot(Chain);
6336}
6337
Dan Gohman46510a72010-04-15 01:51:59 +00006338void SelectionDAGBuilder::visitVAStart(const CallInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006339 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurSDLoc(),
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006340 MVT::Other, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00006341 getValue(I.getArgOperand(0)),
6342 DAG.getSrcValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006343}
6344
Dan Gohman46510a72010-04-15 01:51:59 +00006345void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) {
Micah Villmow3574eca2012-10-08 16:38:25 +00006346 const DataLayout &TD = *TLI.getDataLayout();
Andrew Trickac6d9be2013-05-25 02:42:55 +00006347 SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurSDLoc(),
Dale Johannesena04b7572009-02-03 23:04:43 +00006348 getRoot(), getValue(I.getOperand(0)),
Rafael Espindolacbeeae22010-07-11 04:01:49 +00006349 DAG.getSrcValue(I.getOperand(0)),
Rafael Espindola9d544d02010-07-12 18:11:17 +00006350 TD.getABITypeAlignment(I.getType()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006351 setValue(&I, V);
6352 DAG.setRoot(V.getValue(1));
6353}
6354
Dan Gohman46510a72010-04-15 01:51:59 +00006355void SelectionDAGBuilder::visitVAEnd(const CallInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006356 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurSDLoc(),
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006357 MVT::Other, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00006358 getValue(I.getArgOperand(0)),
6359 DAG.getSrcValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006360}
6361
Dan Gohman46510a72010-04-15 01:51:59 +00006362void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006363 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurSDLoc(),
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006364 MVT::Other, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00006365 getValue(I.getArgOperand(0)),
6366 getValue(I.getArgOperand(1)),
6367 DAG.getSrcValue(I.getArgOperand(0)),
6368 DAG.getSrcValue(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006369}
6370
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006371/// TargetLowering::LowerCallTo - This is the default LowerCallTo
Dan Gohman98ca4f22009-08-05 01:29:28 +00006372/// implementation, which just calls LowerCall.
6373/// FIXME: When all targets are
6374/// migrated to using LowerCall, this hook should be integrated into SDISel.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006375std::pair<SDValue, SDValue>
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006376TargetLowering::LowerCallTo(TargetLowering::CallLoweringInfo &CLI) const {
Stephen Lin3484da92013-04-30 22:49:28 +00006377 // Handle the incoming return values from the call.
6378 CLI.Ins.clear();
6379 SmallVector<EVT, 4> RetTys;
6380 ComputeValueVTs(*this, CLI.RetTy, RetTys);
6381 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
6382 EVT VT = RetTys[I];
6383 MVT RegisterVT = getRegisterType(CLI.RetTy->getContext(), VT);
6384 unsigned NumRegs = getNumRegisters(CLI.RetTy->getContext(), VT);
6385 for (unsigned i = 0; i != NumRegs; ++i) {
6386 ISD::InputArg MyFlags;
6387 MyFlags.VT = RegisterVT;
6388 MyFlags.Used = CLI.IsReturnValueUsed;
6389 if (CLI.RetSExt)
6390 MyFlags.Flags.setSExt();
6391 if (CLI.RetZExt)
6392 MyFlags.Flags.setZExt();
6393 if (CLI.IsInReg)
6394 MyFlags.Flags.setInReg();
6395 CLI.Ins.push_back(MyFlags);
6396 }
6397 }
6398
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006399 // Handle all of the outgoing arguments.
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006400 CLI.Outs.clear();
6401 CLI.OutVals.clear();
6402 ArgListTy &Args = CLI.Args;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006403 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00006404 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006405 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
6406 for (unsigned Value = 0, NumValues = ValueVTs.size();
6407 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006408 EVT VT = ValueVTs[Value];
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006409 Type *ArgTy = VT.getTypeForEVT(CLI.RetTy->getContext());
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006410 SDValue Op = SDValue(Args[i].Node.getNode(),
6411 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006412 ISD::ArgFlagsTy Flags;
6413 unsigned OriginalAlignment =
Micah Villmow3574eca2012-10-08 16:38:25 +00006414 getDataLayout()->getABITypeAlignment(ArgTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006415
6416 if (Args[i].isZExt)
6417 Flags.setZExt();
6418 if (Args[i].isSExt)
6419 Flags.setSExt();
6420 if (Args[i].isInReg)
6421 Flags.setInReg();
6422 if (Args[i].isSRet)
6423 Flags.setSRet();
6424 if (Args[i].isByVal) {
6425 Flags.setByVal();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006426 PointerType *Ty = cast<PointerType>(Args[i].Ty);
6427 Type *ElementTy = Ty->getElementType();
Micah Villmow3574eca2012-10-08 16:38:25 +00006428 Flags.setByValSize(getDataLayout()->getTypeAllocSize(ElementTy));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006429 // For ByVal, alignment should come from FE. BE will guess if this
6430 // info is not there but there are cases it cannot get right.
Chris Lattner9db20f32011-05-22 23:23:02 +00006431 unsigned FrameAlign;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006432 if (Args[i].Alignment)
6433 FrameAlign = Args[i].Alignment;
Chris Lattner9db20f32011-05-22 23:23:02 +00006434 else
6435 FrameAlign = getByValTypeAlignment(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006436 Flags.setByValAlign(FrameAlign);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006437 }
6438 if (Args[i].isNest)
6439 Flags.setNest();
6440 Flags.setOrigAlign(OriginalAlignment);
6441
Patrik Hagglunddfcf33a2012-12-19 11:48:16 +00006442 MVT PartVT = getRegisterType(CLI.RetTy->getContext(), VT);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006443 unsigned NumParts = getNumRegisters(CLI.RetTy->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006444 SmallVector<SDValue, 4> Parts(NumParts);
6445 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
6446
6447 if (Args[i].isSExt)
6448 ExtendKind = ISD::SIGN_EXTEND;
6449 else if (Args[i].isZExt)
6450 ExtendKind = ISD::ZERO_EXTEND;
6451
Stephen Lin3484da92013-04-30 22:49:28 +00006452 // Conservatively only handle 'returned' on non-vectors for now
6453 if (Args[i].isReturned && !Op.getValueType().isVector()) {
6454 assert(CLI.RetTy == Args[i].Ty && RetTys.size() == NumValues &&
6455 "unexpected use of 'returned'");
6456 // Before passing 'returned' to the target lowering code, ensure that
6457 // either the register MVT and the actual EVT are the same size or that
6458 // the return value and argument are extended in the same way; in these
6459 // cases it's safe to pass the argument register value unchanged as the
6460 // return register value (although it's at the target's option whether
6461 // to do so)
6462 // TODO: allow code generation to take advantage of partially preserved
6463 // registers rather than clobbering the entire register when the
6464 // parameter extension method is not compatible with the return
6465 // extension method
6466 if ((NumParts * PartVT.getSizeInBits() == VT.getSizeInBits()) ||
6467 (ExtendKind != ISD::ANY_EXTEND &&
6468 CLI.RetSExt == Args[i].isSExt && CLI.RetZExt == Args[i].isZExt))
6469 Flags.setReturned();
6470 }
6471
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006472 getCopyToParts(CLI.DAG, CLI.DL, Op, &Parts[0], NumParts,
Bill Wendlingf18eb582012-09-26 06:16:18 +00006473 PartVT, CLI.CS ? CLI.CS->getInstruction() : 0, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006474
Dan Gohman98ca4f22009-08-05 01:29:28 +00006475 for (unsigned j = 0; j != NumParts; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006476 // if it isn't first piece, alignment must be 1
Dan Gohmanc9403652010-07-07 15:54:55 +00006477 ISD::OutputArg MyFlags(Flags, Parts[j].getValueType(),
Manman Ren0a1544d2012-11-01 23:49:58 +00006478 i < CLI.NumFixedArgs,
6479 i, j*Parts[j].getValueType().getStoreSize());
Dan Gohman98ca4f22009-08-05 01:29:28 +00006480 if (NumParts > 1 && j == 0)
6481 MyFlags.Flags.setSplit();
6482 else if (j != 0)
6483 MyFlags.Flags.setOrigAlign(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006484
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006485 CLI.Outs.push_back(MyFlags);
6486 CLI.OutVals.push_back(Parts[j]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006487 }
6488 }
6489 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006490
Dan Gohman98ca4f22009-08-05 01:29:28 +00006491 SmallVector<SDValue, 4> InVals;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006492 CLI.Chain = LowerCall(CLI, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00006493
6494 // Verify that the target's LowerCall behaved as expected.
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006495 assert(CLI.Chain.getNode() && CLI.Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00006496 "LowerCall didn't return a valid chain!");
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006497 assert((!CLI.IsTailCall || InVals.empty()) &&
Dan Gohman5e866062009-08-06 15:37:27 +00006498 "LowerCall emitted a return value for a tail call!");
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006499 assert((CLI.IsTailCall || InVals.size() == CLI.Ins.size()) &&
Dan Gohman5e866062009-08-06 15:37:27 +00006500 "LowerCall didn't emit the correct number of values!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00006501
6502 // For a tail call, the return value is merely live-out and there aren't
6503 // any nodes in the DAG representing it. Return a special value to
6504 // indicate that a tail call has been emitted and no more Instructions
6505 // should be processed in the current block.
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006506 if (CLI.IsTailCall) {
6507 CLI.DAG.setRoot(CLI.Chain);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006508 return std::make_pair(SDValue(), SDValue());
6509 }
6510
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006511 DEBUG(for (unsigned i = 0, e = CLI.Ins.size(); i != e; ++i) {
Evan Chengaf1871f2010-03-11 19:38:18 +00006512 assert(InVals[i].getNode() &&
6513 "LowerCall emitted a null value!");
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006514 assert(EVT(CLI.Ins[i].VT) == InVals[i].getValueType() &&
Evan Chengaf1871f2010-03-11 19:38:18 +00006515 "LowerCall emitted a value with the wrong type!");
6516 });
6517
Dan Gohman98ca4f22009-08-05 01:29:28 +00006518 // Collect the legal value parts into potentially illegal values
6519 // that correspond to the original function's return values.
6520 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006521 if (CLI.RetSExt)
Dan Gohman98ca4f22009-08-05 01:29:28 +00006522 AssertOp = ISD::AssertSext;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006523 else if (CLI.RetZExt)
Dan Gohman98ca4f22009-08-05 01:29:28 +00006524 AssertOp = ISD::AssertZext;
6525 SmallVector<SDValue, 4> ReturnValues;
6526 unsigned CurReg = 0;
6527 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00006528 EVT VT = RetTys[I];
Patrik Hagglunddfcf33a2012-12-19 11:48:16 +00006529 MVT RegisterVT = getRegisterType(CLI.RetTy->getContext(), VT);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006530 unsigned NumRegs = getNumRegisters(CLI.RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006531
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006532 ReturnValues.push_back(getCopyFromParts(CLI.DAG, CLI.DL, &InVals[CurReg],
Bill Wendling12931302012-09-26 04:04:19 +00006533 NumRegs, RegisterVT, VT, NULL,
Bill Wendling4533cac2010-01-28 21:51:40 +00006534 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00006535 CurReg += NumRegs;
6536 }
6537
6538 // For a function returning void, there is no return value. We can't create
6539 // such a node, so we just return a null return value in that case. In
Chris Lattner7a2bdde2011-04-15 05:18:47 +00006540 // that case, nothing will actually look at the value.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006541 if (ReturnValues.empty())
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006542 return std::make_pair(SDValue(), CLI.Chain);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006543
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006544 SDValue Res = CLI.DAG.getNode(ISD::MERGE_VALUES, CLI.DL,
6545 CLI.DAG.getVTList(&RetTys[0], RetTys.size()),
Dan Gohman98ca4f22009-08-05 01:29:28 +00006546 &ReturnValues[0], ReturnValues.size());
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006547 return std::make_pair(Res, CLI.Chain);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006548}
6549
Duncan Sands9fbc7e22009-01-21 09:00:29 +00006550void TargetLowering::LowerOperationWrapper(SDNode *N,
6551 SmallVectorImpl<SDValue> &Results,
Dan Gohmand858e902010-04-17 15:26:15 +00006552 SelectionDAG &DAG) const {
Duncan Sands9fbc7e22009-01-21 09:00:29 +00006553 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00006554 if (Res.getNode())
6555 Results.push_back(Res);
6556}
6557
Dan Gohmand858e902010-04-17 15:26:15 +00006558SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Torok Edwinc23197a2009-07-14 16:55:14 +00006559 llvm_unreachable("LowerOperation not implemented for this target!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006560}
6561
Dan Gohman46510a72010-04-15 01:51:59 +00006562void
6563SelectionDAGBuilder::CopyValueToVirtualRegister(const Value *V, unsigned Reg) {
Dan Gohman28a17352010-07-01 01:59:43 +00006564 SDValue Op = getNonRegisterValue(V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006565 assert((Op.getOpcode() != ISD::CopyFromReg ||
6566 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
6567 "Copy from a reg to the same reg!");
6568 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
6569
Owen Anderson23b9b192009-08-12 00:36:31 +00006570 RegsForValue RFV(V->getContext(), TLI, Reg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006571 SDValue Chain = DAG.getEntryNode();
Andrew Trickac6d9be2013-05-25 02:42:55 +00006572 RFV.getCopyToRegs(Op, DAG, getCurSDLoc(), Chain, 0, V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006573 PendingExports.push_back(Chain);
6574}
6575
6576#include "llvm/CodeGen/SelectionDAGISel.h"
6577
Eli Friedman23d32432011-05-05 16:53:34 +00006578/// isOnlyUsedInEntryBlock - If the specified argument is only used in the
6579/// entry block, return true. This includes arguments used by switches, since
6580/// the switch may expand into multiple basic blocks.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006581static bool isOnlyUsedInEntryBlock(const Argument *A, bool FastISel) {
Eli Friedman23d32432011-05-05 16:53:34 +00006582 // With FastISel active, we may be splitting blocks, so force creation
6583 // of virtual registers for all non-dead arguments.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006584 if (FastISel)
Eli Friedman23d32432011-05-05 16:53:34 +00006585 return A->use_empty();
6586
6587 const BasicBlock *Entry = A->getParent()->begin();
6588 for (Value::const_use_iterator UI = A->use_begin(), E = A->use_end();
6589 UI != E; ++UI) {
6590 const User *U = *UI;
6591 if (cast<Instruction>(U)->getParent() != Entry || isa<SwitchInst>(U))
6592 return false; // Use not in entry block.
6593 }
6594 return true;
6595}
6596
Eli Bendersky6437d382013-02-28 23:09:18 +00006597void SelectionDAGISel::LowerArguments(const Function &F) {
Dan Gohman2048b852009-11-23 18:04:58 +00006598 SelectionDAG &DAG = SDB->DAG;
Andrew Trickac6d9be2013-05-25 02:42:55 +00006599 SDLoc dl = SDB->getCurSDLoc();
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006600 const DataLayout *TD = TLI->getDataLayout();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006601 SmallVector<ISD::InputArg, 16> Ins;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006602
Dan Gohman7451d3e2010-05-29 17:03:36 +00006603 if (!FuncInfo->CanLowerReturn) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006604 // Put in an sret pointer parameter before all the other parameters.
6605 SmallVector<EVT, 1> ValueVTs;
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006606 ComputeValueVTs(*TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006607
6608 // NOTE: Assuming that a pointer will never break down to more than one VT
6609 // or one register.
6610 ISD::ArgFlagsTy Flags;
6611 Flags.setSRet();
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006612 MVT RegisterVT = TLI->getRegisterType(*DAG.getContext(), ValueVTs[0]);
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00006613 ISD::InputArg RetArg(Flags, RegisterVT, true, 0, 0);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006614 Ins.push_back(RetArg);
6615 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00006616
Dan Gohman98ca4f22009-08-05 01:29:28 +00006617 // Set up the incoming argument description vector.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006618 unsigned Idx = 1;
Dan Gohman46510a72010-04-15 01:51:59 +00006619 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
Dan Gohman98ca4f22009-08-05 01:29:28 +00006620 I != E; ++I, ++Idx) {
Owen Andersone50ed302009-08-10 22:56:29 +00006621 SmallVector<EVT, 4> ValueVTs;
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006622 ComputeValueVTs(*TLI, I->getType(), ValueVTs);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006623 bool isArgValueUsed = !I->use_empty();
6624 for (unsigned Value = 0, NumValues = ValueVTs.size();
6625 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006626 EVT VT = ValueVTs[Value];
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006627 Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00006628 ISD::ArgFlagsTy Flags;
6629 unsigned OriginalAlignment =
6630 TD->getABITypeAlignment(ArgTy);
6631
Bill Wendling39cd0c82012-12-30 12:45:13 +00006632 if (F.getAttributes().hasAttribute(Idx, Attribute::ZExt))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006633 Flags.setZExt();
Bill Wendling39cd0c82012-12-30 12:45:13 +00006634 if (F.getAttributes().hasAttribute(Idx, Attribute::SExt))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006635 Flags.setSExt();
Bill Wendling39cd0c82012-12-30 12:45:13 +00006636 if (F.getAttributes().hasAttribute(Idx, Attribute::InReg))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006637 Flags.setInReg();
Bill Wendling39cd0c82012-12-30 12:45:13 +00006638 if (F.getAttributes().hasAttribute(Idx, Attribute::StructRet))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006639 Flags.setSRet();
Bill Wendling39cd0c82012-12-30 12:45:13 +00006640 if (F.getAttributes().hasAttribute(Idx, Attribute::ByVal)) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00006641 Flags.setByVal();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006642 PointerType *Ty = cast<PointerType>(I->getType());
6643 Type *ElementTy = Ty->getElementType();
Chris Lattner9db20f32011-05-22 23:23:02 +00006644 Flags.setByValSize(TD->getTypeAllocSize(ElementTy));
Dan Gohman98ca4f22009-08-05 01:29:28 +00006645 // For ByVal, alignment should be passed from FE. BE will guess if
6646 // this info is not there but there are cases it cannot get right.
Chris Lattner9db20f32011-05-22 23:23:02 +00006647 unsigned FrameAlign;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006648 if (F.getParamAlignment(Idx))
6649 FrameAlign = F.getParamAlignment(Idx);
Chris Lattner9db20f32011-05-22 23:23:02 +00006650 else
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006651 FrameAlign = TLI->getByValTypeAlignment(ElementTy);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006652 Flags.setByValAlign(FrameAlign);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006653 }
Bill Wendling39cd0c82012-12-30 12:45:13 +00006654 if (F.getAttributes().hasAttribute(Idx, Attribute::Nest))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006655 Flags.setNest();
6656 Flags.setOrigAlign(OriginalAlignment);
6657
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006658 MVT RegisterVT = TLI->getRegisterType(*CurDAG->getContext(), VT);
6659 unsigned NumRegs = TLI->getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006660 for (unsigned i = 0; i != NumRegs; ++i) {
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00006661 ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed,
6662 Idx-1, i*RegisterVT.getStoreSize());
Dan Gohman98ca4f22009-08-05 01:29:28 +00006663 if (NumRegs > 1 && i == 0)
6664 MyFlags.Flags.setSplit();
6665 // if it isn't first piece, alignment must be 1
6666 else if (i > 0)
6667 MyFlags.Flags.setOrigAlign(1);
6668 Ins.push_back(MyFlags);
6669 }
6670 }
6671 }
6672
6673 // Call the target to set up the argument values.
6674 SmallVector<SDValue, 8> InVals;
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006675 SDValue NewRoot = TLI->LowerFormalArguments(DAG.getRoot(), F.getCallingConv(),
6676 F.isVarArg(), Ins,
6677 dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00006678
6679 // Verify that the target's LowerFormalArguments behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00006680 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00006681 "LowerFormalArguments didn't return a valid chain!");
6682 assert(InVals.size() == Ins.size() &&
6683 "LowerFormalArguments didn't emit the correct number of values!");
Bill Wendling3ea58b62009-12-22 21:35:02 +00006684 DEBUG({
6685 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
6686 assert(InVals[i].getNode() &&
6687 "LowerFormalArguments emitted a null value!");
Duncan Sands1440e8b2010-11-03 11:35:31 +00006688 assert(EVT(Ins[i].VT) == InVals[i].getValueType() &&
Bill Wendling3ea58b62009-12-22 21:35:02 +00006689 "LowerFormalArguments emitted a value with the wrong type!");
6690 }
6691 });
Bill Wendling3ea3c242009-12-22 02:10:19 +00006692
Dan Gohman5e866062009-08-06 15:37:27 +00006693 // Update the DAG with the new chain value resulting from argument lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006694 DAG.setRoot(NewRoot);
6695
6696 // Set up the argument values.
6697 unsigned i = 0;
6698 Idx = 1;
Dan Gohman7451d3e2010-05-29 17:03:36 +00006699 if (!FuncInfo->CanLowerReturn) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006700 // Create a virtual register for the sret pointer, and put in a copy
6701 // from the sret argument into it.
6702 SmallVector<EVT, 1> ValueVTs;
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006703 ComputeValueVTs(*TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00006704 MVT VT = ValueVTs[0].getSimpleVT();
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006705 MVT RegVT = TLI->getRegisterType(*CurDAG->getContext(), VT);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006706 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Bill Wendling46ada192010-03-02 01:55:18 +00006707 SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1,
Bill Wendling12931302012-09-26 04:04:19 +00006708 RegVT, VT, NULL, AssertOp);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006709
Dan Gohman2048b852009-11-23 18:04:58 +00006710 MachineFunction& MF = SDB->DAG.getMachineFunction();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006711 MachineRegisterInfo& RegInfo = MF.getRegInfo();
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006712 unsigned SRetReg = RegInfo.createVirtualRegister(TLI->getRegClassFor(RegVT));
Dan Gohman7451d3e2010-05-29 17:03:36 +00006713 FuncInfo->DemoteRegister = SRetReg;
Andrew Trickac6d9be2013-05-25 02:42:55 +00006714 NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurSDLoc(),
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00006715 SRetReg, ArgValue);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006716 DAG.setRoot(NewRoot);
Bill Wendling3ea3c242009-12-22 02:10:19 +00006717
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006718 // i indexes lowered arguments. Bump it past the hidden sret argument.
6719 // Idx indexes LLVM arguments. Don't touch it.
6720 ++i;
6721 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006722
Dan Gohman46510a72010-04-15 01:51:59 +00006723 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006724 ++I, ++Idx) {
6725 SmallVector<SDValue, 4> ArgValues;
Owen Andersone50ed302009-08-10 22:56:29 +00006726 SmallVector<EVT, 4> ValueVTs;
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006727 ComputeValueVTs(*TLI, I->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006728 unsigned NumValues = ValueVTs.size();
Devang Patel9126c0d2010-06-01 19:59:01 +00006729
6730 // If this argument is unused then remember its value. It is used to generate
6731 // debugging information.
Adrian Prantldf688032013-05-16 23:44:12 +00006732 if (I->use_empty() && NumValues) {
Devang Patel9126c0d2010-06-01 19:59:01 +00006733 SDB->setUnusedArgValue(I, InVals[i]);
6734
Adrian Prantldf688032013-05-16 23:44:12 +00006735 // Also remember any frame index for use in FastISel.
6736 if (FrameIndexSDNode *FI =
6737 dyn_cast<FrameIndexSDNode>(InVals[i].getNode()))
6738 FuncInfo->setArgumentFrameIndex(I, FI->getIndex());
6739 }
6740
Eli Friedman23d32432011-05-05 16:53:34 +00006741 for (unsigned Val = 0; Val != NumValues; ++Val) {
6742 EVT VT = ValueVTs[Val];
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006743 MVT PartVT = TLI->getRegisterType(*CurDAG->getContext(), VT);
6744 unsigned NumParts = TLI->getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006745
6746 if (!I->use_empty()) {
6747 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Bill Wendling39cd0c82012-12-30 12:45:13 +00006748 if (F.getAttributes().hasAttribute(Idx, Attribute::SExt))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006749 AssertOp = ISD::AssertSext;
Bill Wendling39cd0c82012-12-30 12:45:13 +00006750 else if (F.getAttributes().hasAttribute(Idx, Attribute::ZExt))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006751 AssertOp = ISD::AssertZext;
6752
Bill Wendling46ada192010-03-02 01:55:18 +00006753 ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i],
Bill Wendling3ea3c242009-12-22 02:10:19 +00006754 NumParts, PartVT, VT,
Bill Wendling12931302012-09-26 04:04:19 +00006755 NULL, AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00006756 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006757
Dan Gohman98ca4f22009-08-05 01:29:28 +00006758 i += NumParts;
6759 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006760
Eli Friedman23d32432011-05-05 16:53:34 +00006761 // We don't need to do anything else for unused arguments.
6762 if (ArgValues.empty())
6763 continue;
6764
Devang Patel9aee3352011-09-08 22:59:09 +00006765 // Note down frame index.
6766 if (FrameIndexSDNode *FI =
Bill Wendling96cb1122012-07-19 00:04:14 +00006767 dyn_cast<FrameIndexSDNode>(ArgValues[0].getNode()))
Devang Patel9aee3352011-09-08 22:59:09 +00006768 FuncInfo->setArgumentFrameIndex(I, FI->getIndex());
Devang Patel0b48ead2010-08-31 22:22:42 +00006769
Eli Friedman23d32432011-05-05 16:53:34 +00006770 SDValue Res = DAG.getMergeValues(&ArgValues[0], NumValues,
Andrew Trickac6d9be2013-05-25 02:42:55 +00006771 SDB->getCurSDLoc());
Devang Patel9aee3352011-09-08 22:59:09 +00006772
Eli Friedman23d32432011-05-05 16:53:34 +00006773 SDB->setValue(I, Res);
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006774 if (!TM.Options.EnableFastISel && Res.getOpcode() == ISD::BUILD_PAIR) {
Devang Patel9aee3352011-09-08 22:59:09 +00006775 if (LoadSDNode *LNode =
6776 dyn_cast<LoadSDNode>(Res.getOperand(0).getNode()))
6777 if (FrameIndexSDNode *FI =
6778 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
6779 FuncInfo->setArgumentFrameIndex(I, FI->getIndex());
6780 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006781
Eli Friedman23d32432011-05-05 16:53:34 +00006782 // If this argument is live outside of the entry block, insert a copy from
6783 // wherever we got it to the vreg that other BB's will reference it as.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006784 if (!TM.Options.EnableFastISel && Res.getOpcode() == ISD::CopyFromReg) {
Eli Friedman23d32432011-05-05 16:53:34 +00006785 // If we can, though, try to skip creating an unnecessary vreg.
6786 // FIXME: This isn't very clean... it would be nice to make this more
Eli Friedman7f33d672011-05-10 21:50:58 +00006787 // general. It's also subtly incompatible with the hacks FastISel
6788 // uses with vregs.
Eli Friedman23d32432011-05-05 16:53:34 +00006789 unsigned Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg();
6790 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
6791 FuncInfo->ValueMap[I] = Reg;
6792 continue;
6793 }
6794 }
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006795 if (!isOnlyUsedInEntryBlock(I, TM.Options.EnableFastISel)) {
Eli Friedman23d32432011-05-05 16:53:34 +00006796 FuncInfo->InitializeRegForValue(I);
Dan Gohman2048b852009-11-23 18:04:58 +00006797 SDB->CopyToExportRegsIfNeeded(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006798 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006799 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006800
Dan Gohman98ca4f22009-08-05 01:29:28 +00006801 assert(i == InVals.size() && "Argument register count mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006802
6803 // Finally, if the target has anything special to do, allow it to do so.
6804 // FIXME: this should insert code into the DAG!
Dan Gohman64652652010-04-14 20:17:22 +00006805 EmitFunctionEntryCode();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006806}
6807
6808/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
6809/// ensure constants are generated when needed. Remember the virtual registers
6810/// that need to be added to the Machine PHI nodes as input. We cannot just
6811/// directly add them, because expansion might result in multiple MBB's for one
6812/// BB. As such, the start of the BB might correspond to a different MBB than
6813/// the end.
6814///
6815void
Dan Gohmanf81eca02010-04-22 20:46:50 +00006816SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
Dan Gohman46510a72010-04-15 01:51:59 +00006817 const TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006818
6819 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
6820
6821 // Check successor nodes' PHI nodes that expect a constant to be available
6822 // from this block.
6823 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
Dan Gohman46510a72010-04-15 01:51:59 +00006824 const BasicBlock *SuccBB = TI->getSuccessor(succ);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006825 if (!isa<PHINode>(SuccBB->begin())) continue;
Dan Gohmanf81eca02010-04-22 20:46:50 +00006826 MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006827
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006828 // If this terminator has multiple identical successors (common for
6829 // switches), only handle each succ once.
6830 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006831
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006832 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006833
6834 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6835 // nodes and Machine PHI nodes, but the incoming operands have not been
6836 // emitted yet.
Dan Gohman46510a72010-04-15 01:51:59 +00006837 for (BasicBlock::const_iterator I = SuccBB->begin();
6838 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006839 // Ignore dead phi's.
6840 if (PN->use_empty()) continue;
6841
Rafael Espindola3fa82832011-05-13 15:18:06 +00006842 // Skip empty types
6843 if (PN->getType()->isEmptyTy())
6844 continue;
6845
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006846 unsigned Reg;
Dan Gohman46510a72010-04-15 01:51:59 +00006847 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006848
Dan Gohman46510a72010-04-15 01:51:59 +00006849 if (const Constant *C = dyn_cast<Constant>(PHIOp)) {
Dan Gohmanf81eca02010-04-22 20:46:50 +00006850 unsigned &RegOut = ConstantsOut[C];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006851 if (RegOut == 0) {
Dan Gohman89496d02010-07-02 00:10:16 +00006852 RegOut = FuncInfo.CreateRegs(C->getType());
Dan Gohmanf81eca02010-04-22 20:46:50 +00006853 CopyValueToVirtualRegister(C, RegOut);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006854 }
6855 Reg = RegOut;
6856 } else {
Dan Gohmanc25ad632010-07-01 01:33:21 +00006857 DenseMap<const Value *, unsigned>::iterator I =
6858 FuncInfo.ValueMap.find(PHIOp);
6859 if (I != FuncInfo.ValueMap.end())
6860 Reg = I->second;
6861 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006862 assert(isa<AllocaInst>(PHIOp) &&
Dan Gohmanf81eca02010-04-22 20:46:50 +00006863 FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006864 "Didn't codegen value into a register!??");
Dan Gohman89496d02010-07-02 00:10:16 +00006865 Reg = FuncInfo.CreateRegs(PHIOp->getType());
Dan Gohmanf81eca02010-04-22 20:46:50 +00006866 CopyValueToVirtualRegister(PHIOp, Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006867 }
6868 }
6869
6870 // Remember that this register needs to added to the machine PHI node as
6871 // the input for this MBB.
Owen Andersone50ed302009-08-10 22:56:29 +00006872 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006873 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
6874 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +00006875 EVT VT = ValueVTs[vti];
Dan Gohmanf81eca02010-04-22 20:46:50 +00006876 unsigned NumRegisters = TLI.getNumRegisters(*DAG.getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006877 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
Dan Gohmanf81eca02010-04-22 20:46:50 +00006878 FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006879 Reg += NumRegisters;
6880 }
6881 }
6882 }
Dan Gohmanf81eca02010-04-22 20:46:50 +00006883 ConstantsOut.clear();
Dan Gohman3df24e62008-09-03 23:12:08 +00006884}