blob: 625bc162fbb7cc3ab4e38f2c32b1ad98d7d1638f [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"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000052#include "llvm/Support/MathExtras.h"
53#include "llvm/Support/raw_ostream.h"
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000054#include "llvm/Target/TargetFrameLowering.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000055#include "llvm/Target/TargetInstrInfo.h"
Dale Johannesen49de9822009-02-05 01:49:45 +000056#include "llvm/Target/TargetIntrinsicInfo.h"
Owen Anderson243eb9e2011-12-08 22:15:21 +000057#include "llvm/Target/TargetLibraryInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000058#include "llvm/Target/TargetLowering.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000059#include "llvm/Target/TargetOptions.h"
Richard Sandifordac168b82013-08-12 10:28:10 +000060#include "llvm/Target/TargetSelectionDAGInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000061#include <algorithm>
62using namespace llvm;
63
Dale Johannesen601d3c02008-09-05 01:48:15 +000064/// LimitFloatPrecision - Generate low-precision inline sequences for
65/// some float libcalls (6, 8 or 12 bits).
66static unsigned LimitFloatPrecision;
67
68static cl::opt<unsigned, true>
69LimitFPPrecision("limit-float-precision",
70 cl::desc("Generate low-precision inline sequences "
71 "for some float libcalls"),
72 cl::location(LimitFloatPrecision),
73 cl::init(0));
74
Andrew Trickde91f3c2010-11-12 17:50:46 +000075// Limit the width of DAG chains. This is important in general to prevent
76// prevent DAG-based analysis from blowing up. For example, alias analysis and
77// load clustering may not complete in reasonable time. It is difficult to
78// recognize and avoid this situation within each individual analysis, and
79// future analyses are likely to have the same behavior. Limiting DAG width is
Andrew Trickb9e6fe12010-11-20 07:26:51 +000080// the safe approach, and will be especially important with global DAGs.
Andrew Trickde91f3c2010-11-12 17:50:46 +000081//
82// MaxParallelChains default is arbitrarily high to avoid affecting
83// optimization, but could be lowered to improve compile time. Any ld-ld-st-st
Andrew Trickb9e6fe12010-11-20 07:26:51 +000084// sequence over this should have been converted to llvm.memcpy by the
85// frontend. It easy to induce this behavior with .ll code such as:
86// %buffer = alloca [4096 x i8]
87// %data = load [4096 x i8]* %argPtr
88// store [4096 x i8] %data, [4096 x i8]* %buffer
Andrew Trick778583a2011-03-11 17:46:59 +000089static const unsigned MaxParallelChains = 64;
Andrew Trickde91f3c2010-11-12 17:50:46 +000090
Andrew Trickac6d9be2013-05-25 02:42:55 +000091static SDValue getCopyFromPartsVector(SelectionDAG &DAG, SDLoc DL,
Chris Lattner3ac18842010-08-24 23:20:40 +000092 const SDValue *Parts, unsigned NumParts,
Patrik Hagglundb9e12e52012-12-19 12:33:30 +000093 MVT PartVT, EVT ValueVT, const Value *V);
Michael J. Spencere70c5262010-10-16 08:25:21 +000094
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000095/// getCopyFromParts - Create a value that contains the specified legal parts
96/// combined into the value they represent. If the parts combine to a type
97/// larger then ValueVT then AssertOp can be used to specify whether the extra
98/// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT
99/// (ISD::AssertSext).
Andrew Trickac6d9be2013-05-25 02:42:55 +0000100static SDValue getCopyFromParts(SelectionDAG &DAG, SDLoc DL,
Dale Johannesen66978ee2009-01-31 02:22:37 +0000101 const SDValue *Parts,
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000102 unsigned NumParts, MVT PartVT, EVT ValueVT,
Bill Wendling12931302012-09-26 04:04:19 +0000103 const Value *V,
Duncan Sands0b3aa262009-01-28 14:42:54 +0000104 ISD::NodeType AssertOp = ISD::DELETED_NODE) {
Chris Lattner3ac18842010-08-24 23:20:40 +0000105 if (ValueVT.isVector())
Bill Wendling12931302012-09-26 04:04:19 +0000106 return getCopyFromPartsVector(DAG, DL, Parts, NumParts,
107 PartVT, ValueVT, V);
Michael J. Spencere70c5262010-10-16 08:25:21 +0000108
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000109 assert(NumParts > 0 && "No parts to assemble!");
Dan Gohmane9530ec2009-01-15 16:58:17 +0000110 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000111 SDValue Val = Parts[0];
112
113 if (NumParts > 1) {
114 // Assemble the value from multiple parts.
Chris Lattner3ac18842010-08-24 23:20:40 +0000115 if (ValueVT.isInteger()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000116 unsigned PartBits = PartVT.getSizeInBits();
117 unsigned ValueBits = ValueVT.getSizeInBits();
118
119 // Assemble the power of 2 part.
120 unsigned RoundParts = NumParts & (NumParts - 1) ?
121 1 << Log2_32(NumParts) : NumParts;
122 unsigned RoundBits = PartBits * RoundParts;
Owen Andersone50ed302009-08-10 22:56:29 +0000123 EVT RoundVT = RoundBits == ValueBits ?
Owen Anderson23b9b192009-08-12 00:36:31 +0000124 ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000125 SDValue Lo, Hi;
126
Owen Anderson23b9b192009-08-12 00:36:31 +0000127 EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2);
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000128
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000129 if (RoundParts > 2) {
Chris Lattner3ac18842010-08-24 23:20:40 +0000130 Lo = getCopyFromParts(DAG, DL, Parts, RoundParts / 2,
Bill Wendling12931302012-09-26 04:04:19 +0000131 PartVT, HalfVT, V);
Chris Lattner3ac18842010-08-24 23:20:40 +0000132 Hi = getCopyFromParts(DAG, DL, Parts + RoundParts / 2,
Bill Wendling12931302012-09-26 04:04:19 +0000133 RoundParts / 2, PartVT, HalfVT, V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000134 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000135 Lo = DAG.getNode(ISD::BITCAST, DL, HalfVT, Parts[0]);
136 Hi = DAG.getNode(ISD::BITCAST, DL, HalfVT, Parts[1]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000137 }
Bill Wendling3ea3c242009-12-22 02:10:19 +0000138
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000139 if (TLI.isBigEndian())
140 std::swap(Lo, Hi);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000141
Chris Lattner3ac18842010-08-24 23:20:40 +0000142 Val = DAG.getNode(ISD::BUILD_PAIR, DL, RoundVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000143
144 if (RoundParts < NumParts) {
145 // Assemble the trailing non-power-of-2 part.
146 unsigned OddParts = NumParts - RoundParts;
Owen Anderson23b9b192009-08-12 00:36:31 +0000147 EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits);
Chris Lattner3ac18842010-08-24 23:20:40 +0000148 Hi = getCopyFromParts(DAG, DL,
Bill Wendling12931302012-09-26 04:04:19 +0000149 Parts + RoundParts, OddParts, PartVT, OddVT, V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000150
151 // Combine the round and odd parts.
152 Lo = Val;
153 if (TLI.isBigEndian())
154 std::swap(Lo, Hi);
Owen Anderson23b9b192009-08-12 00:36:31 +0000155 EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Chris Lattner3ac18842010-08-24 23:20:40 +0000156 Hi = DAG.getNode(ISD::ANY_EXTEND, DL, TotalVT, Hi);
157 Hi = DAG.getNode(ISD::SHL, DL, TotalVT, Hi,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000158 DAG.getConstant(Lo.getValueType().getSizeInBits(),
Duncan Sands92abc622009-01-31 15:50:11 +0000159 TLI.getPointerTy()));
Chris Lattner3ac18842010-08-24 23:20:40 +0000160 Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, TotalVT, Lo);
161 Val = DAG.getNode(ISD::OR, DL, TotalVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000162 }
Eli Friedman2ac8b322009-05-20 06:02:09 +0000163 } else if (PartVT.isFloatingPoint()) {
164 // FP split into multiple FP parts (for ppcf128)
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000165 assert(ValueVT == EVT(MVT::ppcf128) && PartVT == MVT::f64 &&
Eli Friedman2ac8b322009-05-20 06:02:09 +0000166 "Unexpected split");
167 SDValue Lo, Hi;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000168 Lo = DAG.getNode(ISD::BITCAST, DL, EVT(MVT::f64), Parts[0]);
169 Hi = DAG.getNode(ISD::BITCAST, DL, EVT(MVT::f64), Parts[1]);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000170 if (TLI.isBigEndian())
171 std::swap(Lo, Hi);
Chris Lattner3ac18842010-08-24 23:20:40 +0000172 Val = DAG.getNode(ISD::BUILD_PAIR, DL, ValueVT, Lo, Hi);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000173 } else {
174 // FP split into integer parts (soft fp)
175 assert(ValueVT.isFloatingPoint() && PartVT.isInteger() &&
176 !PartVT.isVector() && "Unexpected split");
Owen Anderson23b9b192009-08-12 00:36:31 +0000177 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
Bill Wendling12931302012-09-26 04:04:19 +0000178 Val = getCopyFromParts(DAG, DL, Parts, NumParts, PartVT, IntVT, V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000179 }
180 }
181
182 // There is now one part, held in Val. Correct it to match ValueVT.
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000183 EVT PartEVT = Val.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000184
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000185 if (PartEVT == ValueVT)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000186 return Val;
187
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000188 if (PartEVT.isInteger() && ValueVT.isInteger()) {
189 if (ValueVT.bitsLT(PartEVT)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000190 // For a truncate, see if we have any information to
191 // indicate whether the truncated bits will always be
192 // zero or sign-extension.
193 if (AssertOp != ISD::DELETED_NODE)
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000194 Val = DAG.getNode(AssertOp, DL, PartEVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000195 DAG.getValueType(ValueVT));
Chris Lattner3ac18842010-08-24 23:20:40 +0000196 return DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000197 }
Chris Lattner3ac18842010-08-24 23:20:40 +0000198 return DAG.getNode(ISD::ANY_EXTEND, DL, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000199 }
200
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000201 if (PartEVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
Chris Lattner3ac18842010-08-24 23:20:40 +0000202 // FP_ROUND's are always exact here.
203 if (ValueVT.bitsLT(Val.getValueType()))
204 return DAG.getNode(ISD::FP_ROUND, DL, ValueVT, Val,
Pete Cooperf57e1c22012-01-17 01:54:07 +0000205 DAG.getTargetConstant(1, TLI.getPointerTy()));
Bill Wendling3ea3c242009-12-22 02:10:19 +0000206
Chris Lattner3ac18842010-08-24 23:20:40 +0000207 return DAG.getNode(ISD::FP_EXTEND, DL, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000208 }
209
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000210 if (PartEVT.getSizeInBits() == ValueVT.getSizeInBits())
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000211 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000212
Torok Edwinc23197a2009-07-14 16:55:14 +0000213 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000214}
215
Bill Wendling12931302012-09-26 04:04:19 +0000216/// getCopyFromPartsVector - Create a value that contains the specified legal
217/// parts combined into the value they represent. If the parts combine to a
218/// type larger then ValueVT then AssertOp can be used to specify whether the
219/// extra bits are known to be zero (ISD::AssertZext) or sign extended from
220/// ValueVT (ISD::AssertSext).
Andrew Trickac6d9be2013-05-25 02:42:55 +0000221static SDValue getCopyFromPartsVector(SelectionDAG &DAG, SDLoc DL,
Chris Lattner3ac18842010-08-24 23:20:40 +0000222 const SDValue *Parts, unsigned NumParts,
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000223 MVT PartVT, EVT ValueVT, const Value *V) {
Chris Lattner3ac18842010-08-24 23:20:40 +0000224 assert(ValueVT.isVector() && "Not a vector value");
225 assert(NumParts > 0 && "No parts to assemble!");
226 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
227 SDValue Val = Parts[0];
Michael J. Spencere70c5262010-10-16 08:25:21 +0000228
Chris Lattner3ac18842010-08-24 23:20:40 +0000229 // Handle a multi-element vector.
230 if (NumParts > 1) {
Patrik Hagglundee211d22012-12-19 11:53:21 +0000231 EVT IntermediateVT;
232 MVT RegisterVT;
Chris Lattner3ac18842010-08-24 23:20:40 +0000233 unsigned NumIntermediates;
234 unsigned NumRegs =
235 TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT,
236 NumIntermediates, RegisterVT);
237 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
238 NumParts = NumRegs; // Silence a compiler warning.
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000239 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
Patrik Hagglundee211d22012-12-19 11:53:21 +0000240 assert(RegisterVT == Parts[0].getSimpleValueType() &&
Chris Lattner3ac18842010-08-24 23:20:40 +0000241 "Part type doesn't match part!");
Michael J. Spencere70c5262010-10-16 08:25:21 +0000242
Chris Lattner3ac18842010-08-24 23:20:40 +0000243 // Assemble the parts into intermediate operands.
244 SmallVector<SDValue, 8> Ops(NumIntermediates);
245 if (NumIntermediates == NumParts) {
246 // If the register was not expanded, truncate or copy the value,
247 // as appropriate.
248 for (unsigned i = 0; i != NumParts; ++i)
249 Ops[i] = getCopyFromParts(DAG, DL, &Parts[i], 1,
Bill Wendling12931302012-09-26 04:04:19 +0000250 PartVT, IntermediateVT, V);
Chris Lattner3ac18842010-08-24 23:20:40 +0000251 } else if (NumParts > 0) {
252 // If the intermediate type was expanded, build the intermediate
253 // operands from the parts.
254 assert(NumParts % NumIntermediates == 0 &&
255 "Must expand into a divisible number of parts!");
256 unsigned Factor = NumParts / NumIntermediates;
257 for (unsigned i = 0; i != NumIntermediates; ++i)
258 Ops[i] = getCopyFromParts(DAG, DL, &Parts[i * Factor], Factor,
Bill Wendling12931302012-09-26 04:04:19 +0000259 PartVT, IntermediateVT, V);
Chris Lattner3ac18842010-08-24 23:20:40 +0000260 }
Michael J. Spencere70c5262010-10-16 08:25:21 +0000261
Chris Lattner3ac18842010-08-24 23:20:40 +0000262 // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the
263 // intermediate operands.
264 Val = DAG.getNode(IntermediateVT.isVector() ?
265 ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, DL,
266 ValueVT, &Ops[0], NumIntermediates);
267 }
Michael J. Spencere70c5262010-10-16 08:25:21 +0000268
Chris Lattner3ac18842010-08-24 23:20:40 +0000269 // There is now one part, held in Val. Correct it to match ValueVT.
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000270 EVT PartEVT = Val.getValueType();
Michael J. Spencere70c5262010-10-16 08:25:21 +0000271
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000272 if (PartEVT == ValueVT)
Chris Lattner3ac18842010-08-24 23:20:40 +0000273 return Val;
Michael J. Spencere70c5262010-10-16 08:25:21 +0000274
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000275 if (PartEVT.isVector()) {
Chris Lattnere6f7c262010-08-25 22:49:25 +0000276 // If the element type of the source/dest vectors are the same, but the
277 // parts vector has more elements than the value vector, then we have a
278 // vector widening case (e.g. <2 x float> -> <4 x float>). Extract the
279 // elements we want.
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000280 if (PartEVT.getVectorElementType() == ValueVT.getVectorElementType()) {
281 assert(PartEVT.getVectorNumElements() > ValueVT.getVectorNumElements() &&
Chris Lattnere6f7c262010-08-25 22:49:25 +0000282 "Cannot narrow, it would be a lossy transformation");
283 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, ValueVT, Val,
Tom Stellard425b76c2013-08-05 22:22:01 +0000284 DAG.getConstant(0, TLI.getVectorIdxTy()));
Michael J. Spencere70c5262010-10-16 08:25:21 +0000285 }
286
Chris Lattnere6f7c262010-08-25 22:49:25 +0000287 // Vector/Vector bitcast.
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000288 if (ValueVT.getSizeInBits() == PartEVT.getSizeInBits())
Nadav Rotem0b666362011-06-04 20:58:08 +0000289 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
290
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000291 assert(PartEVT.getVectorNumElements() == ValueVT.getVectorNumElements() &&
Nadav Rotem0b666362011-06-04 20:58:08 +0000292 "Cannot handle this kind of promotion");
293 // Promoted vector extract
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000294 bool Smaller = ValueVT.bitsLE(PartEVT);
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000295 return DAG.getNode((Smaller ? ISD::TRUNCATE : ISD::ANY_EXTEND),
296 DL, ValueVT, Val);
Nadav Rotem0b666362011-06-04 20:58:08 +0000297
Chris Lattnere6f7c262010-08-25 22:49:25 +0000298 }
Eric Christopher471e4222011-06-08 23:55:35 +0000299
Eric Christopher9aaa02a2011-06-01 19:55:10 +0000300 // Trivial bitcast if the types are the same size and the destination
301 // vector type is legal.
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000302 if (PartEVT.getSizeInBits() == ValueVT.getSizeInBits() &&
Eric Christopher9aaa02a2011-06-01 19:55:10 +0000303 TLI.isTypeLegal(ValueVT))
304 return DAG.getNode(ISD::BITCAST, DL, ValueVT, Val);
Michael J. Spencere70c5262010-10-16 08:25:21 +0000305
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000306 // Handle cases such as i8 -> <1 x i1>
Bill Wendling12931302012-09-26 04:04:19 +0000307 if (ValueVT.getVectorNumElements() != 1) {
308 LLVMContext &Ctx = *DAG.getContext();
309 Twine ErrMsg("non-trivial scalar-to-vector conversion");
310 if (const Instruction *I = dyn_cast_or_null<Instruction>(V)) {
311 if (const CallInst *CI = dyn_cast<CallInst>(I))
312 if (isa<InlineAsm>(CI->getCalledValue()))
313 ErrMsg = ErrMsg + ", possible invalid constraint for vector type";
314 Ctx.emitError(I, ErrMsg);
315 } else {
316 Ctx.emitError(ErrMsg);
317 }
Chad Rosierf0b07552013-05-01 19:49:26 +0000318 return DAG.getUNDEF(ValueVT);
Bill Wendling12931302012-09-26 04:04:19 +0000319 }
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000320
321 if (ValueVT.getVectorNumElements() == 1 &&
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000322 ValueVT.getVectorElementType() != PartEVT) {
323 bool Smaller = ValueVT.bitsLE(PartEVT);
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000324 Val = DAG.getNode((Smaller ? ISD::TRUNCATE : ISD::ANY_EXTEND),
325 DL, ValueVT.getScalarType(), Val);
326 }
327
Chris Lattner3ac18842010-08-24 23:20:40 +0000328 return DAG.getNode(ISD::BUILD_VECTOR, DL, ValueVT, Val);
329}
330
Andrew Trickac6d9be2013-05-25 02:42:55 +0000331static void getCopyToPartsVector(SelectionDAG &DAG, SDLoc dl,
Chris Lattnera13b8602010-08-24 23:10:06 +0000332 SDValue Val, SDValue *Parts, unsigned NumParts,
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000333 MVT PartVT, const Value *V);
Michael J. Spencere70c5262010-10-16 08:25:21 +0000334
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000335/// getCopyToParts - Create a series of nodes that contain the specified value
336/// split into legal parts. If the parts contain more bits than Val, then, for
337/// integers, ExtendKind can be used to specify how to generate the extra bits.
Andrew Trickac6d9be2013-05-25 02:42:55 +0000338static void getCopyToParts(SelectionDAG &DAG, SDLoc DL,
Bill Wendling3ea3c242009-12-22 02:10:19 +0000339 SDValue Val, SDValue *Parts, unsigned NumParts,
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000340 MVT PartVT, const Value *V,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000341 ISD::NodeType ExtendKind = ISD::ANY_EXTEND) {
Owen Andersone50ed302009-08-10 22:56:29 +0000342 EVT ValueVT = Val.getValueType();
Michael J. Spencere70c5262010-10-16 08:25:21 +0000343
Chris Lattnera13b8602010-08-24 23:10:06 +0000344 // Handle the vector case separately.
345 if (ValueVT.isVector())
Bill Wendlingf18eb582012-09-26 06:16:18 +0000346 return getCopyToPartsVector(DAG, DL, Val, Parts, NumParts, PartVT, V);
Michael J. Spencere70c5262010-10-16 08:25:21 +0000347
Chris Lattnera13b8602010-08-24 23:10:06 +0000348 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000349 unsigned PartBits = PartVT.getSizeInBits();
Dale Johannesen8a36f502009-02-25 22:39:13 +0000350 unsigned OrigNumParts = NumParts;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000351 assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!");
352
Chris Lattnera13b8602010-08-24 23:10:06 +0000353 if (NumParts == 0)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000354 return;
355
Chris Lattnera13b8602010-08-24 23:10:06 +0000356 assert(!ValueVT.isVector() && "Vector case handled elsewhere");
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000357 EVT PartEVT = PartVT;
358 if (PartEVT == ValueVT) {
Chris Lattnera13b8602010-08-24 23:10:06 +0000359 assert(NumParts == 1 && "No-op copy with multiple parts!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000360 Parts[0] = Val;
361 return;
362 }
363
Chris Lattnera13b8602010-08-24 23:10:06 +0000364 if (NumParts * PartBits > ValueVT.getSizeInBits()) {
365 // If the parts cover more bits than the value has, promote the value.
366 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
367 assert(NumParts == 1 && "Do not know what to promote to!");
368 Val = DAG.getNode(ISD::FP_EXTEND, DL, PartVT, Val);
369 } else {
Bill Wendling9e8ceb02012-02-23 23:25:25 +0000370 assert((PartVT.isInteger() || PartVT == MVT::x86mmx) &&
371 ValueVT.isInteger() &&
Michael J. Spencere70c5262010-10-16 08:25:21 +0000372 "Unknown mismatch!");
Chris Lattnera13b8602010-08-24 23:10:06 +0000373 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
374 Val = DAG.getNode(ExtendKind, DL, ValueVT, Val);
Bill Wendling9e8ceb02012-02-23 23:25:25 +0000375 if (PartVT == MVT::x86mmx)
376 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
Chris Lattnera13b8602010-08-24 23:10:06 +0000377 }
378 } else if (PartBits == ValueVT.getSizeInBits()) {
379 // Different types of the same size.
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000380 assert(NumParts == 1 && PartEVT != ValueVT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000381 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
Chris Lattnera13b8602010-08-24 23:10:06 +0000382 } else if (NumParts * PartBits < ValueVT.getSizeInBits()) {
383 // If the parts cover less bits than value has, truncate the value.
Bill Wendling9e8ceb02012-02-23 23:25:25 +0000384 assert((PartVT.isInteger() || PartVT == MVT::x86mmx) &&
385 ValueVT.isInteger() &&
Chris Lattnera13b8602010-08-24 23:10:06 +0000386 "Unknown mismatch!");
387 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
388 Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
Bill Wendling9e8ceb02012-02-23 23:25:25 +0000389 if (PartVT == MVT::x86mmx)
390 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
Chris Lattnera13b8602010-08-24 23:10:06 +0000391 }
392
393 // The value may have changed - recompute ValueVT.
394 ValueVT = Val.getValueType();
395 assert(NumParts * PartBits == ValueVT.getSizeInBits() &&
396 "Failed to tile the value with PartVT!");
397
398 if (NumParts == 1) {
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000399 if (PartEVT != ValueVT) {
Bill Wendlingf18eb582012-09-26 06:16:18 +0000400 LLVMContext &Ctx = *DAG.getContext();
401 Twine ErrMsg("scalar-to-vector conversion failed");
402 if (const Instruction *I = dyn_cast_or_null<Instruction>(V)) {
403 if (const CallInst *CI = dyn_cast<CallInst>(I))
404 if (isa<InlineAsm>(CI->getCalledValue()))
405 ErrMsg = ErrMsg + ", possible invalid constraint for vector type";
406 Ctx.emitError(I, ErrMsg);
407 } else {
408 Ctx.emitError(ErrMsg);
409 }
410 }
411
Chris Lattnera13b8602010-08-24 23:10:06 +0000412 Parts[0] = Val;
413 return;
414 }
415
416 // Expand the value into multiple parts.
417 if (NumParts & (NumParts - 1)) {
418 // The number of parts is not a power of 2. Split off and copy the tail.
419 assert(PartVT.isInteger() && ValueVT.isInteger() &&
420 "Do not know what to expand to!");
421 unsigned RoundParts = 1 << Log2_32(NumParts);
422 unsigned RoundBits = RoundParts * PartBits;
423 unsigned OddParts = NumParts - RoundParts;
424 SDValue OddVal = DAG.getNode(ISD::SRL, DL, ValueVT, Val,
425 DAG.getIntPtrConstant(RoundBits));
Bill Wendlingf18eb582012-09-26 06:16:18 +0000426 getCopyToParts(DAG, DL, OddVal, Parts + RoundParts, OddParts, PartVT, V);
Chris Lattnera13b8602010-08-24 23:10:06 +0000427
428 if (TLI.isBigEndian())
429 // The odd parts were reversed by getCopyToParts - unreverse them.
430 std::reverse(Parts + RoundParts, Parts + NumParts);
431
432 NumParts = RoundParts;
433 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
434 Val = DAG.getNode(ISD::TRUNCATE, DL, ValueVT, Val);
435 }
436
437 // The number of parts is a power of 2. Repeatedly bisect the value using
438 // EXTRACT_ELEMENT.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000439 Parts[0] = DAG.getNode(ISD::BITCAST, DL,
Chris Lattnera13b8602010-08-24 23:10:06 +0000440 EVT::getIntegerVT(*DAG.getContext(),
441 ValueVT.getSizeInBits()),
442 Val);
443
444 for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) {
445 for (unsigned i = 0; i < NumParts; i += StepSize) {
446 unsigned ThisBits = StepSize * PartBits / 2;
447 EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits);
448 SDValue &Part0 = Parts[i];
449 SDValue &Part1 = Parts[i+StepSize/2];
450
451 Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL,
452 ThisVT, Part0, DAG.getIntPtrConstant(1));
453 Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL,
454 ThisVT, Part0, DAG.getIntPtrConstant(0));
455
456 if (ThisBits == PartBits && ThisVT != PartVT) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000457 Part0 = DAG.getNode(ISD::BITCAST, DL, PartVT, Part0);
458 Part1 = DAG.getNode(ISD::BITCAST, DL, PartVT, Part1);
Chris Lattnera13b8602010-08-24 23:10:06 +0000459 }
460 }
461 }
462
463 if (TLI.isBigEndian())
464 std::reverse(Parts, Parts + OrigNumParts);
465}
466
467
468/// getCopyToPartsVector - Create a series of nodes that contain the specified
469/// value split into legal parts.
Andrew Trickac6d9be2013-05-25 02:42:55 +0000470static void getCopyToPartsVector(SelectionDAG &DAG, SDLoc DL,
Chris Lattnera13b8602010-08-24 23:10:06 +0000471 SDValue Val, SDValue *Parts, unsigned NumParts,
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000472 MVT PartVT, const Value *V) {
Chris Lattnera13b8602010-08-24 23:10:06 +0000473 EVT ValueVT = Val.getValueType();
474 assert(ValueVT.isVector() && "Not a vector");
475 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Michael J. Spencere70c5262010-10-16 08:25:21 +0000476
Chris Lattnera13b8602010-08-24 23:10:06 +0000477 if (NumParts == 1) {
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000478 EVT PartEVT = PartVT;
479 if (PartEVT == ValueVT) {
Chris Lattnere6f7c262010-08-25 22:49:25 +0000480 // Nothing to do.
481 } else if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) {
482 // Bitconvert vector->vector case.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000483 Val = DAG.getNode(ISD::BITCAST, DL, PartVT, Val);
Chris Lattnere6f7c262010-08-25 22:49:25 +0000484 } else if (PartVT.isVector() &&
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000485 PartEVT.getVectorElementType() == ValueVT.getVectorElementType() &&
486 PartEVT.getVectorNumElements() > ValueVT.getVectorNumElements()) {
Chris Lattnere6f7c262010-08-25 22:49:25 +0000487 EVT ElementVT = PartVT.getVectorElementType();
488 // Vector widening case, e.g. <2 x float> -> <4 x float>. Shuffle in
489 // undef elements.
490 SmallVector<SDValue, 16> Ops;
491 for (unsigned i = 0, e = ValueVT.getVectorNumElements(); i != e; ++i)
492 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
Tom Stellard425b76c2013-08-05 22:22:01 +0000493 ElementVT, Val, DAG.getConstant(i,
494 TLI.getVectorIdxTy())));
Michael J. Spencere70c5262010-10-16 08:25:21 +0000495
Chris Lattnere6f7c262010-08-25 22:49:25 +0000496 for (unsigned i = ValueVT.getVectorNumElements(),
497 e = PartVT.getVectorNumElements(); i != e; ++i)
498 Ops.push_back(DAG.getUNDEF(ElementVT));
499
500 Val = DAG.getNode(ISD::BUILD_VECTOR, DL, PartVT, &Ops[0], Ops.size());
501
502 // FIXME: Use CONCAT for 2x -> 4x.
Michael J. Spencere70c5262010-10-16 08:25:21 +0000503
Chris Lattnere6f7c262010-08-25 22:49:25 +0000504 //SDValue UndefElts = DAG.getUNDEF(VectorTy);
505 //Val = DAG.getNode(ISD::CONCAT_VECTORS, DL, PartVT, Val, UndefElts);
Nadav Rotem0b666362011-06-04 20:58:08 +0000506 } else if (PartVT.isVector() &&
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000507 PartEVT.getVectorElementType().bitsGE(
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000508 ValueVT.getVectorElementType()) &&
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000509 PartEVT.getVectorNumElements() == ValueVT.getVectorNumElements()) {
Nadav Rotem0b666362011-06-04 20:58:08 +0000510
511 // Promoted vector extract
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000512 bool Smaller = PartEVT.bitsLE(ValueVT);
Nadav Rotemc6341e62011-06-19 08:49:38 +0000513 Val = DAG.getNode((Smaller ? ISD::TRUNCATE : ISD::ANY_EXTEND),
514 DL, PartVT, Val);
Nadav Rotem0b666362011-06-04 20:58:08 +0000515 } else{
Chris Lattnere6f7c262010-08-25 22:49:25 +0000516 // Vector -> scalar conversion.
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000517 assert(ValueVT.getVectorNumElements() == 1 &&
Chris Lattnere6f7c262010-08-25 22:49:25 +0000518 "Only trivial vector-to-scalar conversions should get here!");
519 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
Tom Stellard425b76c2013-08-05 22:22:01 +0000520 PartVT, Val, DAG.getConstant(0, TLI.getVectorIdxTy()));
Nadav Rotemb05f14b2011-06-12 14:49:38 +0000521
522 bool Smaller = ValueVT.bitsLE(PartVT);
523 Val = DAG.getNode((Smaller ? ISD::TRUNCATE : ISD::ANY_EXTEND),
524 DL, PartVT, Val);
Chris Lattnera13b8602010-08-24 23:10:06 +0000525 }
Michael J. Spencere70c5262010-10-16 08:25:21 +0000526
Chris Lattnera13b8602010-08-24 23:10:06 +0000527 Parts[0] = Val;
528 return;
529 }
Michael J. Spencere70c5262010-10-16 08:25:21 +0000530
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000531 // Handle a multi-element vector.
Patrik Hagglundee211d22012-12-19 11:53:21 +0000532 EVT IntermediateVT;
533 MVT RegisterVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000534 unsigned NumIntermediates;
Owen Anderson23b9b192009-08-12 00:36:31 +0000535 unsigned NumRegs = TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT,
Devang Patel8f09bea2010-08-26 20:32:32 +0000536 IntermediateVT,
537 NumIntermediates, RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000538 unsigned NumElements = ValueVT.getVectorNumElements();
Michael J. Spencere70c5262010-10-16 08:25:21 +0000539
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000540 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
541 NumParts = NumRegs; // Silence a compiler warning.
Patrik Hagglundb9e12e52012-12-19 12:33:30 +0000542 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
Michael J. Spencere70c5262010-10-16 08:25:21 +0000543
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000544 // Split the vector into intermediate operands.
545 SmallVector<SDValue, 8> Ops(NumIntermediates);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000546 for (unsigned i = 0; i != NumIntermediates; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000547 if (IntermediateVT.isVector())
Chris Lattnera13b8602010-08-24 23:10:06 +0000548 Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000549 IntermediateVT, Val,
Tom Stellard425b76c2013-08-05 22:22:01 +0000550 DAG.getConstant(i * (NumElements / NumIntermediates),
551 TLI.getVectorIdxTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000552 else
Chris Lattnera13b8602010-08-24 23:10:06 +0000553 Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
Tom Stellard425b76c2013-08-05 22:22:01 +0000554 IntermediateVT, Val,
555 DAG.getConstant(i, TLI.getVectorIdxTy()));
Bill Wendling3ea3c242009-12-22 02:10:19 +0000556 }
Michael J. Spencere70c5262010-10-16 08:25:21 +0000557
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000558 // Split the intermediate operands into legal parts.
559 if (NumParts == NumIntermediates) {
560 // If the register was not expanded, promote or copy the value,
561 // as appropriate.
562 for (unsigned i = 0; i != NumParts; ++i)
Bill Wendlingf18eb582012-09-26 06:16:18 +0000563 getCopyToParts(DAG, DL, Ops[i], &Parts[i], 1, PartVT, V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000564 } else if (NumParts > 0) {
565 // If the intermediate type was expanded, split each the value into
566 // legal parts.
567 assert(NumParts % NumIntermediates == 0 &&
568 "Must expand into a divisible number of parts!");
569 unsigned Factor = NumParts / NumIntermediates;
570 for (unsigned i = 0; i != NumIntermediates; ++i)
Bill Wendlingf18eb582012-09-26 06:16:18 +0000571 getCopyToParts(DAG, DL, Ops[i], &Parts[i*Factor], Factor, PartVT, V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000572 }
573}
574
Dan Gohman462f6b52010-05-29 17:53:24 +0000575namespace {
576 /// RegsForValue - This struct represents the registers (physical or virtual)
577 /// that a particular set of values is assigned, and the type information
578 /// about the value. The most common situation is to represent one value at a
579 /// time, but struct or array values are handled element-wise as multiple
580 /// values. The splitting of aggregates is performed recursively, so that we
581 /// never have aggregate-typed registers. The values at this point do not
582 /// necessarily have legal types, so each value may require one or more
583 /// registers of some legal type.
584 ///
585 struct RegsForValue {
586 /// ValueVTs - The value types of the values, which may not be legal, and
587 /// may need be promoted or synthesized from one or more registers.
588 ///
589 SmallVector<EVT, 4> ValueVTs;
590
591 /// RegVTs - The value types of the registers. This is the same size as
592 /// ValueVTs and it records, for each value, what the type of the assigned
593 /// register or registers are. (Individual values are never synthesized
594 /// from more than one type of register.)
595 ///
596 /// With virtual registers, the contents of RegVTs is redundant with TLI's
597 /// getRegisterType member function, however when with physical registers
598 /// it is necessary to have a separate record of the types.
599 ///
Patrik Hagglund8963fec2012-12-19 12:23:01 +0000600 SmallVector<MVT, 4> RegVTs;
Dan Gohman462f6b52010-05-29 17:53:24 +0000601
602 /// Regs - This list holds the registers assigned to the values.
603 /// Each legal or promoted value requires one register, and each
604 /// expanded value requires multiple registers.
605 ///
606 SmallVector<unsigned, 4> Regs;
607
608 RegsForValue() {}
609
610 RegsForValue(const SmallVector<unsigned, 4> &regs,
Patrik Hagglund8963fec2012-12-19 12:23:01 +0000611 MVT regvt, EVT valuevt)
Dan Gohman462f6b52010-05-29 17:53:24 +0000612 : ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs) {}
613
Dan Gohman462f6b52010-05-29 17:53:24 +0000614 RegsForValue(LLVMContext &Context, const TargetLowering &tli,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000615 unsigned Reg, Type *Ty) {
Dan Gohman462f6b52010-05-29 17:53:24 +0000616 ComputeValueVTs(tli, Ty, ValueVTs);
617
618 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
619 EVT ValueVT = ValueVTs[Value];
620 unsigned NumRegs = tli.getNumRegisters(Context, ValueVT);
Patrik Hagglunddfcf33a2012-12-19 11:48:16 +0000621 MVT RegisterVT = tli.getRegisterType(Context, ValueVT);
Dan Gohman462f6b52010-05-29 17:53:24 +0000622 for (unsigned i = 0; i != NumRegs; ++i)
623 Regs.push_back(Reg + i);
624 RegVTs.push_back(RegisterVT);
625 Reg += NumRegs;
626 }
627 }
628
629 /// areValueTypesLegal - Return true if types of all the values are legal.
630 bool areValueTypesLegal(const TargetLowering &TLI) {
631 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
Patrik Hagglund8963fec2012-12-19 12:23:01 +0000632 MVT RegisterVT = RegVTs[Value];
Dan Gohman462f6b52010-05-29 17:53:24 +0000633 if (!TLI.isTypeLegal(RegisterVT))
634 return false;
635 }
636 return true;
637 }
638
639 /// append - Add the specified values to this one.
640 void append(const RegsForValue &RHS) {
641 ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end());
642 RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end());
643 Regs.append(RHS.Regs.begin(), RHS.Regs.end());
644 }
645
646 /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
647 /// this value and returns the result as a ValueVTs value. This uses
648 /// Chain/Flag as the input and updates them for the output Chain/Flag.
649 /// If the Flag pointer is NULL, no flag is used.
650 SDValue getCopyFromRegs(SelectionDAG &DAG, FunctionLoweringInfo &FuncInfo,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000651 SDLoc dl,
Bill Wendling12931302012-09-26 04:04:19 +0000652 SDValue &Chain, SDValue *Flag,
653 const Value *V = 0) const;
Dan Gohman462f6b52010-05-29 17:53:24 +0000654
655 /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
656 /// specified value into the registers specified by this object. This uses
657 /// Chain/Flag as the input and updates them for the output Chain/Flag.
658 /// If the Flag pointer is NULL, no flag is used.
Andrew Trickac6d9be2013-05-25 02:42:55 +0000659 void getCopyToRegs(SDValue Val, SelectionDAG &DAG, SDLoc dl,
Bill Wendlingf18eb582012-09-26 06:16:18 +0000660 SDValue &Chain, SDValue *Flag, const Value *V) const;
Dan Gohman462f6b52010-05-29 17:53:24 +0000661
662 /// AddInlineAsmOperands - Add this value to the specified inlineasm node
663 /// operand list. This adds the code marker, matching input operand index
664 /// (if applicable), and includes the number of values added into it.
665 void AddInlineAsmOperands(unsigned Kind,
666 bool HasMatching, unsigned MatchingIdx,
667 SelectionDAG &DAG,
668 std::vector<SDValue> &Ops) const;
669 };
670}
671
672/// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
673/// this value and returns the result as a ValueVT value. This uses
674/// Chain/Flag as the input and updates them for the output Chain/Flag.
675/// If the Flag pointer is NULL, no flag is used.
676SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG,
677 FunctionLoweringInfo &FuncInfo,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000678 SDLoc dl,
Bill Wendling12931302012-09-26 04:04:19 +0000679 SDValue &Chain, SDValue *Flag,
680 const Value *V) const {
Dan Gohman7da5d3f2010-07-26 18:15:41 +0000681 // A Value with type {} or [0 x %t] needs no registers.
682 if (ValueVTs.empty())
683 return SDValue();
684
Dan Gohman462f6b52010-05-29 17:53:24 +0000685 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
686
687 // Assemble the legal parts into the final values.
688 SmallVector<SDValue, 4> Values(ValueVTs.size());
689 SmallVector<SDValue, 8> Parts;
690 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
691 // Copy the legal parts from the registers.
692 EVT ValueVT = ValueVTs[Value];
693 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVT);
Patrik Hagglund8963fec2012-12-19 12:23:01 +0000694 MVT RegisterVT = RegVTs[Value];
Dan Gohman462f6b52010-05-29 17:53:24 +0000695
696 Parts.resize(NumRegs);
697 for (unsigned i = 0; i != NumRegs; ++i) {
698 SDValue P;
699 if (Flag == 0) {
700 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
701 } else {
702 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag);
703 *Flag = P.getValue(2);
704 }
705
706 Chain = P.getValue(1);
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000707 Parts[i] = P;
Dan Gohman462f6b52010-05-29 17:53:24 +0000708
709 // If the source register was virtual and if we know something about it,
710 // add an assert node.
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000711 if (!TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) ||
Cameron Zwariche1497b92011-02-24 10:00:08 +0000712 !RegisterVT.isInteger() || RegisterVT.isVector())
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000713 continue;
Cameron Zwariche1497b92011-02-24 10:00:08 +0000714
715 const FunctionLoweringInfo::LiveOutInfo *LOI =
716 FuncInfo.GetLiveOutRegInfo(Regs[Part+i]);
717 if (!LOI)
718 continue;
Dan Gohman462f6b52010-05-29 17:53:24 +0000719
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000720 unsigned RegSize = RegisterVT.getSizeInBits();
Cameron Zwariche1497b92011-02-24 10:00:08 +0000721 unsigned NumSignBits = LOI->NumSignBits;
722 unsigned NumZeroBits = LOI->KnownZero.countLeadingOnes();
Dan Gohman462f6b52010-05-29 17:53:24 +0000723
Quentin Colombeta3fb49c2013-06-18 20:14:39 +0000724 if (NumZeroBits == RegSize) {
725 // The current value is a zero.
726 // Explicitly express that as it would be easier for
727 // optimizations to kick in.
728 Parts[i] = DAG.getConstant(0, RegisterVT);
729 continue;
730 }
731
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000732 // FIXME: We capture more information than the dag can represent. For
733 // now, just use the tightest assertzext/assertsext possible.
734 bool isSExt = true;
735 EVT FromVT(MVT::Other);
736 if (NumSignBits == RegSize)
737 isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1
738 else if (NumZeroBits >= RegSize-1)
739 isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1
740 else if (NumSignBits > RegSize-8)
741 isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8
742 else if (NumZeroBits >= RegSize-8)
743 isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8
744 else if (NumSignBits > RegSize-16)
745 isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16
746 else if (NumZeroBits >= RegSize-16)
747 isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16
748 else if (NumSignBits > RegSize-32)
749 isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32
750 else if (NumZeroBits >= RegSize-32)
751 isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32
752 else
753 continue;
Dan Gohman462f6b52010-05-29 17:53:24 +0000754
Chris Lattnerd5b4db92010-12-13 01:11:17 +0000755 // Add an assertion node.
756 assert(FromVT != MVT::Other);
757 Parts[i] = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
758 RegisterVT, P, DAG.getValueType(FromVT));
Dan Gohman462f6b52010-05-29 17:53:24 +0000759 }
760
761 Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(),
Bill Wendling12931302012-09-26 04:04:19 +0000762 NumRegs, RegisterVT, ValueVT, V);
Dan Gohman462f6b52010-05-29 17:53:24 +0000763 Part += NumRegs;
764 Parts.clear();
765 }
766
767 return DAG.getNode(ISD::MERGE_VALUES, dl,
768 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
769 &Values[0], ValueVTs.size());
770}
771
772/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
773/// specified value into the registers specified by this object. This uses
774/// Chain/Flag as the input and updates them for the output Chain/Flag.
775/// If the Flag pointer is NULL, no flag is used.
Andrew Trickac6d9be2013-05-25 02:42:55 +0000776void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, SDLoc dl,
Bill Wendlingf18eb582012-09-26 06:16:18 +0000777 SDValue &Chain, SDValue *Flag,
778 const Value *V) const {
Dan Gohman462f6b52010-05-29 17:53:24 +0000779 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
780
781 // Get the list of the values's legal parts.
782 unsigned NumRegs = Regs.size();
783 SmallVector<SDValue, 8> Parts(NumRegs);
784 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
785 EVT ValueVT = ValueVTs[Value];
786 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), ValueVT);
Patrik Hagglund8963fec2012-12-19 12:23:01 +0000787 MVT RegisterVT = RegVTs[Value];
Evan Cheng2766a472012-12-06 19:13:27 +0000788 ISD::NodeType ExtendKind =
789 TLI.isZExtFree(Val, RegisterVT)? ISD::ZERO_EXTEND: ISD::ANY_EXTEND;
Dan Gohman462f6b52010-05-29 17:53:24 +0000790
Chris Lattner3ac18842010-08-24 23:20:40 +0000791 getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value),
Evan Cheng2766a472012-12-06 19:13:27 +0000792 &Parts[Part], NumParts, RegisterVT, V, ExtendKind);
Dan Gohman462f6b52010-05-29 17:53:24 +0000793 Part += NumParts;
794 }
795
796 // Copy the parts into the registers.
797 SmallVector<SDValue, 8> Chains(NumRegs);
798 for (unsigned i = 0; i != NumRegs; ++i) {
799 SDValue Part;
800 if (Flag == 0) {
801 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
802 } else {
803 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag);
804 *Flag = Part.getValue(1);
805 }
806
807 Chains[i] = Part.getValue(0);
808 }
809
810 if (NumRegs == 1 || Flag)
811 // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
812 // flagged to it. That is the CopyToReg nodes and the user are considered
813 // a single scheduling unit. If we create a TokenFactor and return it as
814 // chain, then the TokenFactor is both a predecessor (operand) of the
815 // user as well as a successor (the TF operands are flagged to the user).
816 // c1, f1 = CopyToReg
817 // c2, f2 = CopyToReg
818 // c3 = TokenFactor c1, c2
819 // ...
820 // = op c3, ..., f2
821 Chain = Chains[NumRegs-1];
822 else
823 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs);
824}
825
826/// AddInlineAsmOperands - Add this value to the specified inlineasm node
827/// operand list. This adds the code marker and includes the number of
828/// values added into it.
829void RegsForValue::AddInlineAsmOperands(unsigned Code, bool HasMatching,
830 unsigned MatchingIdx,
831 SelectionDAG &DAG,
832 std::vector<SDValue> &Ops) const {
833 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
834
835 unsigned Flag = InlineAsm::getFlagWord(Code, Regs.size());
836 if (HasMatching)
837 Flag = InlineAsm::getFlagWordForMatchingOp(Flag, MatchingIdx);
Jakob Stoklund Olesen459b74b2011-10-12 23:37:29 +0000838 else if (!Regs.empty() &&
839 TargetRegisterInfo::isVirtualRegister(Regs.front())) {
840 // Put the register class of the virtual registers in the flag word. That
841 // way, later passes can recompute register class constraints for inline
842 // assembly as well as normal instructions.
843 // Don't do this for tied operands that can use the regclass information
844 // from the def.
845 const MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
846 const TargetRegisterClass *RC = MRI.getRegClass(Regs.front());
847 Flag = InlineAsm::getFlagWordForRegClass(Flag, RC->getID());
848 }
849
Dan Gohman462f6b52010-05-29 17:53:24 +0000850 SDValue Res = DAG.getTargetConstant(Flag, MVT::i32);
851 Ops.push_back(Res);
852
853 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
854 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), ValueVTs[Value]);
Patrik Hagglund8963fec2012-12-19 12:23:01 +0000855 MVT RegisterVT = RegVTs[Value];
Dan Gohman462f6b52010-05-29 17:53:24 +0000856 for (unsigned i = 0; i != NumRegs; ++i) {
857 assert(Reg < Regs.size() && "Mismatch in # registers expected");
858 Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT));
859 }
860 }
861}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000862
Owen Anderson243eb9e2011-12-08 22:15:21 +0000863void SelectionDAGBuilder::init(GCFunctionInfo *gfi, AliasAnalysis &aa,
864 const TargetLibraryInfo *li) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000865 AA = &aa;
866 GFI = gfi;
Owen Anderson243eb9e2011-12-08 22:15:21 +0000867 LibInfo = li;
Micah Villmow3574eca2012-10-08 16:38:25 +0000868 TD = DAG.getTarget().getDataLayout();
Richard Smithcb1f68d2012-08-22 00:42:39 +0000869 Context = DAG.getContext();
Bill Wendling4ed1fb02011-10-15 01:00:26 +0000870 LPadToCallSiteMap.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000871}
872
Dan Gohmanb02b62a2010-04-14 18:24:06 +0000873/// clear - Clear out the current SelectionDAG and the associated
Dan Gohman2048b852009-11-23 18:04:58 +0000874/// state and prepare this SelectionDAGBuilder object to be used
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000875/// for a new block. This doesn't clear out information about
876/// additional blocks that are needed to complete switch lowering
877/// or PHI node updating; that information is cleared out as it is
878/// consumed.
Dan Gohman2048b852009-11-23 18:04:58 +0000879void SelectionDAGBuilder::clear() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000880 NodeMap.clear();
Devang Patel9126c0d2010-06-01 19:59:01 +0000881 UnusedArgNodeMap.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000882 PendingLoads.clear();
883 PendingExports.clear();
Andrew Trickea5db0c2013-05-25 02:20:36 +0000884 CurInst = NULL;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000885 HasTailCall = false;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000886}
887
Devang Patel23385752011-05-23 17:44:13 +0000888/// clearDanglingDebugInfo - Clear the dangling debug information
Benjamin Kramerd9b0b022012-06-02 10:20:22 +0000889/// map. This function is separated from the clear so that debug
Devang Patel23385752011-05-23 17:44:13 +0000890/// information that is dangling in a basic block can be properly
891/// resolved in a different basic block. This allows the
892/// SelectionDAG to resolve dangling debug information attached
893/// to PHI nodes.
894void SelectionDAGBuilder::clearDanglingDebugInfo() {
895 DanglingDebugInfoMap.clear();
896}
897
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000898/// getRoot - Return the current virtual root of the Selection DAG,
899/// flushing any PendingLoad items. This must be done before emitting
900/// a store or any other node that may need to be ordered after any
901/// prior load instructions.
902///
Dan Gohman2048b852009-11-23 18:04:58 +0000903SDValue SelectionDAGBuilder::getRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000904 if (PendingLoads.empty())
905 return DAG.getRoot();
906
907 if (PendingLoads.size() == 1) {
908 SDValue Root = PendingLoads[0];
909 DAG.setRoot(Root);
910 PendingLoads.clear();
911 return Root;
912 }
913
914 // Otherwise, we have to make a token factor node.
Andrew Trickac6d9be2013-05-25 02:42:55 +0000915 SDValue Root = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000916 &PendingLoads[0], PendingLoads.size());
917 PendingLoads.clear();
918 DAG.setRoot(Root);
919 return Root;
920}
921
922/// getControlRoot - Similar to getRoot, but instead of flushing all the
923/// PendingLoad items, flush all the PendingExports items. It is necessary
924/// to do this before emitting a terminator instruction.
925///
Dan Gohman2048b852009-11-23 18:04:58 +0000926SDValue SelectionDAGBuilder::getControlRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000927 SDValue Root = DAG.getRoot();
928
929 if (PendingExports.empty())
930 return Root;
931
932 // Turn all of the CopyToReg chains into one factored node.
933 if (Root.getOpcode() != ISD::EntryToken) {
934 unsigned i = 0, e = PendingExports.size();
935 for (; i != e; ++i) {
936 assert(PendingExports[i].getNode()->getNumOperands() > 1);
937 if (PendingExports[i].getNode()->getOperand(0) == Root)
938 break; // Don't add the root if we already indirectly depend on it.
939 }
940
941 if (i == e)
942 PendingExports.push_back(Root);
943 }
944
Andrew Trickac6d9be2013-05-25 02:42:55 +0000945 Root = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000946 &PendingExports[0],
947 PendingExports.size());
948 PendingExports.clear();
949 DAG.setRoot(Root);
950 return Root;
951}
952
Dan Gohman46510a72010-04-15 01:51:59 +0000953void SelectionDAGBuilder::visit(const Instruction &I) {
Dan Gohmanc105a2b2010-04-22 20:55:53 +0000954 // Set up outgoing PHI node register values before emitting the terminator.
955 if (isa<TerminatorInst>(&I))
956 HandlePHINodesInSuccessorBlocks(I.getParent());
957
Andrew Trickdd0fb012013-05-25 03:08:10 +0000958 ++SDNodeOrder;
959
Andrew Trickea5db0c2013-05-25 02:20:36 +0000960 CurInst = &I;
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000961
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000962 visit(I.getOpcode(), I);
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000963
Dan Gohman92884f72010-04-20 15:03:56 +0000964 if (!isa<TerminatorInst>(&I) && !HasTailCall)
965 CopyToExportRegsIfNeeded(&I);
966
Andrew Trickea5db0c2013-05-25 02:20:36 +0000967 CurInst = NULL;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000968}
969
Dan Gohmanba5be5c2010-04-20 15:00:41 +0000970void SelectionDAGBuilder::visitPHI(const PHINode &) {
971 llvm_unreachable("SelectionDAGBuilder shouldn't visit PHI nodes!");
972}
973
Dan Gohman46510a72010-04-15 01:51:59 +0000974void SelectionDAGBuilder::visit(unsigned Opcode, const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000975 // Note: this doesn't use InstVisitor, because it has to work with
976 // ConstantExpr's in addition to instructions.
977 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000978 default: llvm_unreachable("Unknown instruction type encountered!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000979 // Build the switch statement using the Instruction.def file.
980#define HANDLE_INST(NUM, OPCODE, CLASS) \
Galina Kistanova72ea0c92012-07-19 04:50:12 +0000981 case Instruction::OPCODE: visit##OPCODE((const CLASS&)I); break;
Chandler Carruth0b8c9a82013-01-02 11:36:10 +0000982#include "llvm/IR/Instruction.def"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000983 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000984}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000985
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000986// resolveDanglingDebugInfo - if we saw an earlier dbg_value referring to V,
987// generate the debug data structures now that we've seen its definition.
988void SelectionDAGBuilder::resolveDanglingDebugInfo(const Value *V,
989 SDValue Val) {
990 DanglingDebugInfo &DDI = DanglingDebugInfoMap[V];
Devang Patel4cf81c42010-08-26 23:35:15 +0000991 if (DDI.getDI()) {
992 const DbgValueInst *DI = DDI.getDI();
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000993 DebugLoc dl = DDI.getdl();
994 unsigned DbgSDNodeOrder = DDI.getSDNodeOrder();
Devang Patel4cf81c42010-08-26 23:35:15 +0000995 MDNode *Variable = DI->getVariable();
996 uint64_t Offset = DI->getOffset();
Dale Johannesenbdc09d92010-07-16 00:02:08 +0000997 SDDbgValue *SDV;
998 if (Val.getNode()) {
Devang Patel78a06e52010-08-25 20:39:26 +0000999 if (!EmitFuncArgumentDbgValue(V, Variable, Offset, Val)) {
Dale Johannesenbdc09d92010-07-16 00:02:08 +00001000 SDV = DAG.getDbgValue(Variable, Val.getNode(),
1001 Val.getResNo(), Offset, dl, DbgSDNodeOrder);
1002 DAG.AddDbgValue(SDV, Val.getNode(), false);
1003 }
Owen Anderson95771af2011-02-25 21:41:48 +00001004 } else
Adrian Prantl5da4e4f2013-05-22 18:02:19 +00001005 DEBUG(dbgs() << "Dropping debug info for " << *DI << "\n");
Dale Johannesenbdc09d92010-07-16 00:02:08 +00001006 DanglingDebugInfoMap[V] = DanglingDebugInfo();
1007 }
1008}
1009
Nick Lewycky8de34002011-09-30 22:19:53 +00001010/// getValue - Return an SDValue for the given Value.
Dan Gohman2048b852009-11-23 18:04:58 +00001011SDValue SelectionDAGBuilder::getValue(const Value *V) {
Dan Gohman28a17352010-07-01 01:59:43 +00001012 // If we already have an SDValue for this value, use it. It's important
1013 // to do this first, so that we don't create a CopyFromReg if we already
1014 // have a regular SDValue.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001015 SDValue &N = NodeMap[V];
1016 if (N.getNode()) return N;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001017
Dan Gohman28a17352010-07-01 01:59:43 +00001018 // If there's a virtual register allocated and initialized for this
1019 // value, use it.
1020 DenseMap<const Value *, unsigned>::iterator It = FuncInfo.ValueMap.find(V);
1021 if (It != FuncInfo.ValueMap.end()) {
1022 unsigned InReg = It->second;
Bill Wendlingba54bca2013-06-19 21:36:55 +00001023 RegsForValue RFV(*DAG.getContext(), *TM.getTargetLowering(),
1024 InReg, V->getType());
Dan Gohman28a17352010-07-01 01:59:43 +00001025 SDValue Chain = DAG.getEntryNode();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001026 N = RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, NULL, V);
Devang Patel8f314282011-01-25 18:09:58 +00001027 resolveDanglingDebugInfo(V, N);
1028 return N;
Dan Gohman28a17352010-07-01 01:59:43 +00001029 }
1030
1031 // Otherwise create a new SDValue and remember it.
1032 SDValue Val = getValueImpl(V);
1033 NodeMap[V] = Val;
Dale Johannesenbdc09d92010-07-16 00:02:08 +00001034 resolveDanglingDebugInfo(V, Val);
Dan Gohman28a17352010-07-01 01:59:43 +00001035 return Val;
1036}
1037
1038/// getNonRegisterValue - Return an SDValue for the given Value, but
1039/// don't look in FuncInfo.ValueMap for a virtual register.
1040SDValue SelectionDAGBuilder::getNonRegisterValue(const Value *V) {
1041 // If we already have an SDValue for this value, use it.
1042 SDValue &N = NodeMap[V];
1043 if (N.getNode()) return N;
1044
1045 // Otherwise create a new SDValue and remember it.
1046 SDValue Val = getValueImpl(V);
1047 NodeMap[V] = Val;
Dale Johannesenbdc09d92010-07-16 00:02:08 +00001048 resolveDanglingDebugInfo(V, Val);
Dan Gohman28a17352010-07-01 01:59:43 +00001049 return Val;
1050}
1051
Dale Johannesenbdc09d92010-07-16 00:02:08 +00001052/// getValueImpl - Helper function for getValue and getNonRegisterValue.
Dan Gohman28a17352010-07-01 01:59:43 +00001053/// Create an SDValue for the given value.
1054SDValue SelectionDAGBuilder::getValueImpl(const Value *V) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00001055 const TargetLowering *TLI = TM.getTargetLowering();
1056
Dan Gohman383b5f62010-04-17 15:32:28 +00001057 if (const Constant *C = dyn_cast<Constant>(V)) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00001058 EVT VT = TLI->getValueType(V->getType(), true);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001059
Dan Gohman383b5f62010-04-17 15:32:28 +00001060 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C))
Dan Gohman28a17352010-07-01 01:59:43 +00001061 return DAG.getConstant(*CI, VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001062
Dan Gohman383b5f62010-04-17 15:32:28 +00001063 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001064 return DAG.getGlobalAddress(GV, getCurSDLoc(), VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001065
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001066 if (isa<ConstantPointerNull>(C))
Bill Wendlingba54bca2013-06-19 21:36:55 +00001067 return DAG.getConstant(0, TLI->getPointerTy());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001068
Dan Gohman383b5f62010-04-17 15:32:28 +00001069 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Dan Gohman28a17352010-07-01 01:59:43 +00001070 return DAG.getConstantFP(*CFP, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001071
Nate Begeman9008ca62009-04-27 18:41:29 +00001072 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
Dan Gohman28a17352010-07-01 01:59:43 +00001073 return DAG.getUNDEF(VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001074
Dan Gohman383b5f62010-04-17 15:32:28 +00001075 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001076 visit(CE->getOpcode(), *CE);
1077 SDValue N1 = NodeMap[V];
Dan Gohmanac7d05c2010-04-16 16:55:18 +00001078 assert(N1.getNode() && "visit didn't populate the NodeMap!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001079 return N1;
1080 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001081
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001082 if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
1083 SmallVector<SDValue, 4> Constants;
1084 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
1085 OI != OE; ++OI) {
1086 SDNode *Val = getValue(*OI).getNode();
Dan Gohmaned48caf2009-09-08 01:44:02 +00001087 // If the operand is an empty aggregate, there are no values.
1088 if (!Val) continue;
1089 // Add each leaf value from the operand to the Constants list
1090 // to form a flattened list of all the values.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001091 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
1092 Constants.push_back(SDValue(Val, i));
1093 }
Bill Wendling87710f02009-12-21 23:47:40 +00001094
Bill Wendling4533cac2010-01-28 21:51:40 +00001095 return DAG.getMergeValues(&Constants[0], Constants.size(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001096 getCurSDLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001097 }
Stephen Lin155615d2013-07-08 00:37:03 +00001098
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001099 if (const ConstantDataSequential *CDS =
1100 dyn_cast<ConstantDataSequential>(C)) {
1101 SmallVector<SDValue, 4> Ops;
Chris Lattner0f193b82012-01-25 01:27:20 +00001102 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001103 SDNode *Val = getValue(CDS->getElementAsConstant(i)).getNode();
1104 // Add each leaf value from the operand to the Constants list
1105 // to form a flattened list of all the values.
1106 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
1107 Ops.push_back(SDValue(Val, i));
1108 }
1109
1110 if (isa<ArrayType>(CDS->getType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001111 return DAG.getMergeValues(&Ops[0], Ops.size(), getCurSDLoc());
1112 return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurSDLoc(),
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001113 VT, &Ops[0], Ops.size());
1114 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001115
Duncan Sands1df98592010-02-16 11:11:14 +00001116 if (C->getType()->isStructTy() || C->getType()->isArrayTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001117 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
1118 "Unknown struct or array constant!");
1119
Owen Andersone50ed302009-08-10 22:56:29 +00001120 SmallVector<EVT, 4> ValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00001121 ComputeValueVTs(*TLI, C->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001122 unsigned NumElts = ValueVTs.size();
1123 if (NumElts == 0)
1124 return SDValue(); // empty struct
1125 SmallVector<SDValue, 4> Constants(NumElts);
1126 for (unsigned i = 0; i != NumElts; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00001127 EVT EltVT = ValueVTs[i];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001128 if (isa<UndefValue>(C))
Dale Johannesene8d72302009-02-06 23:05:02 +00001129 Constants[i] = DAG.getUNDEF(EltVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001130 else if (EltVT.isFloatingPoint())
1131 Constants[i] = DAG.getConstantFP(0, EltVT);
1132 else
1133 Constants[i] = DAG.getConstant(0, EltVT);
1134 }
Bill Wendling87710f02009-12-21 23:47:40 +00001135
Bill Wendling4533cac2010-01-28 21:51:40 +00001136 return DAG.getMergeValues(&Constants[0], NumElts,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001137 getCurSDLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001138 }
1139
Dan Gohman383b5f62010-04-17 15:32:28 +00001140 if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
Dan Gohman29cbade2009-11-20 23:18:13 +00001141 return DAG.getBlockAddress(BA, VT);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001142
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001143 VectorType *VecTy = cast<VectorType>(V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001144 unsigned NumElements = VecTy->getNumElements();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001145
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001146 // Now that we know the number and type of the elements, get that number of
1147 // elements into the Ops array based on what kind of constant it is.
1148 SmallVector<SDValue, 16> Ops;
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001149 if (const ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001150 for (unsigned i = 0; i != NumElements; ++i)
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001151 Ops.push_back(getValue(CV->getOperand(i)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001152 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00001153 assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!");
Bill Wendlingba54bca2013-06-19 21:36:55 +00001154 EVT EltVT = TLI->getValueType(VecTy->getElementType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001155
1156 SDValue Op;
Nate Begeman9008ca62009-04-27 18:41:29 +00001157 if (EltVT.isFloatingPoint())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001158 Op = DAG.getConstantFP(0, EltVT);
1159 else
1160 Op = DAG.getConstant(0, EltVT);
1161 Ops.assign(NumElements, Op);
1162 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001163
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001164 // Create a BUILD_VECTOR node.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001165 return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00001166 VT, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001167 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001168
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001169 // If this is a static alloca, generate it as the frameindex instead of
1170 // computation.
1171 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
1172 DenseMap<const AllocaInst*, int>::iterator SI =
1173 FuncInfo.StaticAllocaMap.find(AI);
1174 if (SI != FuncInfo.StaticAllocaMap.end())
Bill Wendlingba54bca2013-06-19 21:36:55 +00001175 return DAG.getFrameIndex(SI->second, TLI->getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001176 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001177
Dan Gohman28a17352010-07-01 01:59:43 +00001178 // If this is an instruction which fast-isel has deferred, select it now.
1179 if (const Instruction *Inst = dyn_cast<Instruction>(V)) {
Dan Gohman84023e02010-07-10 09:00:22 +00001180 unsigned InReg = FuncInfo.InitializeRegForValue(Inst);
Bill Wendlingba54bca2013-06-19 21:36:55 +00001181 RegsForValue RFV(*DAG.getContext(), *TLI, InReg, Inst->getType());
Dan Gohman84023e02010-07-10 09:00:22 +00001182 SDValue Chain = DAG.getEntryNode();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001183 return RFV.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(), Chain, NULL, V);
Dan Gohman28a17352010-07-01 01:59:43 +00001184 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001185
Dan Gohman28a17352010-07-01 01:59:43 +00001186 llvm_unreachable("Can't get register for value!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001187}
1188
Dan Gohman46510a72010-04-15 01:51:59 +00001189void SelectionDAGBuilder::visitRet(const ReturnInst &I) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00001190 const TargetLowering *TLI = TM.getTargetLowering();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001191 SDValue Chain = getControlRoot();
1192 SmallVector<ISD::OutputArg, 8> Outs;
Dan Gohmanc9403652010-07-07 15:54:55 +00001193 SmallVector<SDValue, 8> OutVals;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001194
Dan Gohman7451d3e2010-05-29 17:03:36 +00001195 if (!FuncInfo.CanLowerReturn) {
1196 unsigned DemoteReg = FuncInfo.DemoteRegister;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001197 const Function *F = I.getParent()->getParent();
1198
1199 // Emit a store of the return value through the virtual register.
1200 // Leave Outs empty so that LowerReturn won't try to load return
1201 // registers the usual way.
1202 SmallVector<EVT, 1> PtrValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00001203 ComputeValueVTs(*TLI, PointerType::getUnqual(F->getReturnType()),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001204 PtrValueVTs);
1205
1206 SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]);
1207 SDValue RetOp = getValue(I.getOperand(0));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001208
Owen Andersone50ed302009-08-10 22:56:29 +00001209 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001210 SmallVector<uint64_t, 4> Offsets;
Bill Wendlingba54bca2013-06-19 21:36:55 +00001211 ComputeValueVTs(*TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets);
Dan Gohman7ea1ca62008-10-21 20:00:42 +00001212 unsigned NumValues = ValueVTs.size();
Dan Gohman7ea1ca62008-10-21 20:00:42 +00001213
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001214 SmallVector<SDValue, 4> Chains(NumValues);
Bill Wendling87710f02009-12-21 23:47:40 +00001215 for (unsigned i = 0; i != NumValues; ++i) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001216 SDValue Add = DAG.getNode(ISD::ADD, getCurSDLoc(),
Chris Lattnera13b8602010-08-24 23:10:06 +00001217 RetPtr.getValueType(), RetPtr,
1218 DAG.getIntPtrConstant(Offsets[i]));
Bill Wendling87710f02009-12-21 23:47:40 +00001219 Chains[i] =
Andrew Trickac6d9be2013-05-25 02:42:55 +00001220 DAG.getStore(Chain, getCurSDLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001221 SDValue(RetOp.getNode(), RetOp.getResNo() + i),
Chris Lattner84bd98a2010-09-21 18:58:22 +00001222 // FIXME: better loc info would be nice.
1223 Add, MachinePointerInfo(), false, false, 0);
Bill Wendling87710f02009-12-21 23:47:40 +00001224 }
1225
Andrew Trickac6d9be2013-05-25 02:42:55 +00001226 Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001227 MVT::Other, &Chains[0], NumValues);
Chris Lattner25d58372010-02-28 18:53:13 +00001228 } else if (I.getNumOperands() != 0) {
1229 SmallVector<EVT, 4> ValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00001230 ComputeValueVTs(*TLI, I.getOperand(0)->getType(), ValueVTs);
Chris Lattner25d58372010-02-28 18:53:13 +00001231 unsigned NumValues = ValueVTs.size();
1232 if (NumValues) {
1233 SDValue RetOp = getValue(I.getOperand(0));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001234 for (unsigned j = 0, f = NumValues; j != f; ++j) {
1235 EVT VT = ValueVTs[j];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001236
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001237 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001238
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001239 const Function *F = I.getParent()->getParent();
Bill Wendling8b62abd2012-12-30 13:01:51 +00001240 if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
1241 Attribute::SExt))
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001242 ExtendKind = ISD::SIGN_EXTEND;
Bill Wendling8b62abd2012-12-30 13:01:51 +00001243 else if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
1244 Attribute::ZExt))
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001245 ExtendKind = ISD::ZERO_EXTEND;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001246
Cameron Zwarich7bbf0ee2011-03-17 14:53:37 +00001247 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger())
Bill Wendlingba54bca2013-06-19 21:36:55 +00001248 VT = TLI->getTypeForExtArgOrReturn(VT.getSimpleVT(), ExtendKind);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001249
Bill Wendlingba54bca2013-06-19 21:36:55 +00001250 unsigned NumParts = TLI->getNumRegisters(*DAG.getContext(), VT);
1251 MVT PartVT = TLI->getRegisterType(*DAG.getContext(), VT);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001252 SmallVector<SDValue, 4> Parts(NumParts);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001253 getCopyToParts(DAG, getCurSDLoc(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001254 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
Bill Wendlingf18eb582012-09-26 06:16:18 +00001255 &Parts[0], NumParts, PartVT, &I, ExtendKind);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001256
1257 // 'inreg' on function refers to return value
1258 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
Bill Wendling8b62abd2012-12-30 13:01:51 +00001259 if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
1260 Attribute::InReg))
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001261 Flags.setInReg();
1262
1263 // Propagate extension type if any
Cameron Zwarich8df6bf52011-03-16 22:20:07 +00001264 if (ExtendKind == ISD::SIGN_EXTEND)
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001265 Flags.setSExt();
Cameron Zwarich8df6bf52011-03-16 22:20:07 +00001266 else if (ExtendKind == ISD::ZERO_EXTEND)
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00001267 Flags.setZExt();
1268
Dan Gohmanc9403652010-07-07 15:54:55 +00001269 for (unsigned i = 0; i < NumParts; ++i) {
1270 Outs.push_back(ISD::OutputArg(Flags, Parts[i].getValueType(),
Manman Ren0a1544d2012-11-01 23:49:58 +00001271 /*isfixed=*/true, 0, 0));
Dan Gohmanc9403652010-07-07 15:54:55 +00001272 OutVals.push_back(Parts[i]);
1273 }
Evan Cheng3927f432009-03-25 20:20:11 +00001274 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001275 }
1276 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001277
1278 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001279 CallingConv::ID CallConv =
1280 DAG.getMachineFunction().getFunction()->getCallingConv();
Bill Wendlingba54bca2013-06-19 21:36:55 +00001281 Chain = TM.getTargetLowering()->LowerReturn(Chain, CallConv, isVarArg,
1282 Outs, OutVals, getCurSDLoc(),
1283 DAG);
Dan Gohman5e866062009-08-06 15:37:27 +00001284
1285 // Verify that the target's LowerReturn behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00001286 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00001287 "LowerReturn didn't return a valid chain!");
1288
1289 // Update the DAG with the new chain value resulting from return lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001290 DAG.setRoot(Chain);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001291}
1292
Dan Gohmanad62f532009-04-23 23:13:24 +00001293/// CopyToExportRegsIfNeeded - If the given value has virtual registers
1294/// created for it, emit nodes to copy the value into the virtual
1295/// registers.
Dan Gohman46510a72010-04-15 01:51:59 +00001296void SelectionDAGBuilder::CopyToExportRegsIfNeeded(const Value *V) {
Rafael Espindola3fa82832011-05-13 15:18:06 +00001297 // Skip empty types
1298 if (V->getType()->isEmptyTy())
1299 return;
1300
Dan Gohman33b7a292010-04-16 17:15:02 +00001301 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
1302 if (VMI != FuncInfo.ValueMap.end()) {
1303 assert(!V->use_empty() && "Unused value assigned virtual registers!");
1304 CopyValueToVirtualRegister(V, VMI->second);
Dan Gohmanad62f532009-04-23 23:13:24 +00001305 }
1306}
1307
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001308/// ExportFromCurrentBlock - If this condition isn't known to be exported from
1309/// the current basic block, add it to ValueMap now so that we'll get a
1310/// CopyTo/FromReg.
Dan Gohman46510a72010-04-15 01:51:59 +00001311void SelectionDAGBuilder::ExportFromCurrentBlock(const Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001312 // No need to export constants.
1313 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001314
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001315 // Already exported?
1316 if (FuncInfo.isExportedInst(V)) return;
1317
1318 unsigned Reg = FuncInfo.InitializeRegForValue(V);
1319 CopyValueToVirtualRegister(V, Reg);
1320}
1321
Dan Gohman46510a72010-04-15 01:51:59 +00001322bool SelectionDAGBuilder::isExportableFromCurrentBlock(const Value *V,
Dan Gohman2048b852009-11-23 18:04:58 +00001323 const BasicBlock *FromBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001324 // The operands of the setcc have to be in this block. We don't know
1325 // how to export them from some other block.
Dan Gohman46510a72010-04-15 01:51:59 +00001326 if (const Instruction *VI = dyn_cast<Instruction>(V)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001327 // Can export from current BB.
1328 if (VI->getParent() == FromBB)
1329 return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001330
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001331 // Is already exported, noop.
1332 return FuncInfo.isExportedInst(V);
1333 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001334
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001335 // If this is an argument, we can export it if the BB is the entry block or
1336 // if it is already exported.
1337 if (isa<Argument>(V)) {
1338 if (FromBB == &FromBB->getParent()->getEntryBlock())
1339 return true;
1340
1341 // Otherwise, can only export this if it is already exported.
1342 return FuncInfo.isExportedInst(V);
1343 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001344
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001345 // Otherwise, constants can always be exported.
1346 return true;
1347}
1348
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001349/// Return branch probability calculated by BranchProbabilityInfo for IR blocks.
Jakub Staszak25101bb2011-12-20 20:03:10 +00001350uint32_t SelectionDAGBuilder::getEdgeWeight(const MachineBasicBlock *Src,
1351 const MachineBasicBlock *Dst) const {
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001352 BranchProbabilityInfo *BPI = FuncInfo.BPI;
1353 if (!BPI)
1354 return 0;
Jakub Staszak95ece8e2011-07-29 20:05:36 +00001355 const BasicBlock *SrcBB = Src->getBasicBlock();
1356 const BasicBlock *DstBB = Dst->getBasicBlock();
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001357 return BPI->getEdgeWeight(SrcBB, DstBB);
1358}
1359
Jakub Staszakc8f34de2011-07-29 22:25:21 +00001360void SelectionDAGBuilder::
1361addSuccessorWithWeight(MachineBasicBlock *Src, MachineBasicBlock *Dst,
1362 uint32_t Weight /* = 0 */) {
1363 if (!Weight)
1364 Weight = getEdgeWeight(Src, Dst);
1365 Src->addSuccessor(Dst, Weight);
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001366}
1367
1368
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001369static bool InBlock(const Value *V, const BasicBlock *BB) {
1370 if (const Instruction *I = dyn_cast<Instruction>(V))
1371 return I->getParent() == BB;
1372 return true;
1373}
1374
Dan Gohmanc2277342008-10-17 21:16:08 +00001375/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
1376/// This function emits a branch and is used at the leaves of an OR or an
1377/// AND operator tree.
1378///
1379void
Dan Gohman46510a72010-04-15 01:51:59 +00001380SelectionDAGBuilder::EmitBranchForMergedCondition(const Value *Cond,
Dan Gohman2048b852009-11-23 18:04:58 +00001381 MachineBasicBlock *TBB,
1382 MachineBasicBlock *FBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001383 MachineBasicBlock *CurBB,
1384 MachineBasicBlock *SwitchBB) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001385 const BasicBlock *BB = CurBB->getBasicBlock();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001386
Dan Gohmanc2277342008-10-17 21:16:08 +00001387 // If the leaf of the tree is a comparison, merge the condition into
1388 // the caseblock.
Dan Gohman46510a72010-04-15 01:51:59 +00001389 if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001390 // The operands of the cmp have to be in this block. We don't know
1391 // how to export them from some other block. If this is the first block
1392 // of the sequence, no exporting is needed.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001393 if (CurBB == SwitchBB ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001394 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
1395 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001396 ISD::CondCode Condition;
Dan Gohman46510a72010-04-15 01:51:59 +00001397 if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001398 Condition = getICmpCondCode(IC->getPredicate());
Dan Gohman46510a72010-04-15 01:51:59 +00001399 } else if (const FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001400 Condition = getFCmpCondCode(FC->getPredicate());
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001401 if (TM.Options.NoNaNsFPMath)
1402 Condition = getFCmpCodeWithoutNaN(Condition);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001403 } else {
1404 Condition = ISD::SETEQ; // silence warning.
Torok Edwinc23197a2009-07-14 16:55:14 +00001405 llvm_unreachable("Unknown compare instruction");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001406 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001407
1408 CaseBlock CB(Condition, BOp->getOperand(0),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001409 BOp->getOperand(1), NULL, TBB, FBB, CurBB);
1410 SwitchCases.push_back(CB);
1411 return;
1412 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001413 }
1414
1415 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001416 CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanc2277342008-10-17 21:16:08 +00001417 NULL, TBB, FBB, CurBB);
1418 SwitchCases.push_back(CB);
1419}
1420
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001421/// FindMergedConditions - If Cond is an expression like
Dan Gohman46510a72010-04-15 01:51:59 +00001422void SelectionDAGBuilder::FindMergedConditions(const Value *Cond,
Dan Gohman2048b852009-11-23 18:04:58 +00001423 MachineBasicBlock *TBB,
1424 MachineBasicBlock *FBB,
1425 MachineBasicBlock *CurBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001426 MachineBasicBlock *SwitchBB,
Dan Gohman2048b852009-11-23 18:04:58 +00001427 unsigned Opc) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001428 // If this node is not part of the or/and tree, emit it as a branch.
Dan Gohman46510a72010-04-15 01:51:59 +00001429 const Instruction *BOp = dyn_cast<Instruction>(Cond);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001430 if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001431 (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
1432 BOp->getParent() != CurBB->getBasicBlock() ||
1433 !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1434 !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001435 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001436 return;
1437 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001438
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001439 // Create TmpBB after CurBB.
1440 MachineFunction::iterator BBI = CurBB;
1441 MachineFunction &MF = DAG.getMachineFunction();
1442 MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
1443 CurBB->getParent()->insert(++BBI, TmpBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001444
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001445 if (Opc == Instruction::Or) {
1446 // Codegen X | Y as:
1447 // jmp_if_X TBB
1448 // jmp TmpBB
1449 // TmpBB:
1450 // jmp_if_Y TBB
1451 // jmp FBB
1452 //
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001453
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001454 // Emit the LHS condition.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001455 FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, SwitchBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001456
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001457 // Emit the RHS condition into TmpBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001458 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001459 } else {
1460 assert(Opc == Instruction::And && "Unknown merge op!");
1461 // Codegen X & Y as:
1462 // jmp_if_X TmpBB
1463 // jmp FBB
1464 // TmpBB:
1465 // jmp_if_Y TBB
1466 // jmp FBB
1467 //
1468 // This requires creation of TmpBB after CurBB.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001469
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001470 // Emit the LHS condition.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001471 FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, SwitchBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001472
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001473 // Emit the RHS condition into TmpBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001474 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001475 }
1476}
1477
1478/// If the set of cases should be emitted as a series of branches, return true.
1479/// If we should emit this as a bunch of and/or'd together conditions, return
1480/// false.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001481bool
Stephen Lin09f8ca32013-07-06 21:44:25 +00001482SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001483 if (Cases.size() != 2) return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001484
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001485 // If this is two comparisons of the same values or'd or and'd together, they
1486 // will get folded into a single comparison, so don't emit two blocks.
1487 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1488 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1489 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1490 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1491 return false;
1492 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001493
Chris Lattner133ce872010-01-02 00:00:03 +00001494 // Handle: (X != null) | (Y != null) --> (X|Y) != 0
1495 // Handle: (X == null) & (Y == null) --> (X|Y) == 0
1496 if (Cases[0].CmpRHS == Cases[1].CmpRHS &&
1497 Cases[0].CC == Cases[1].CC &&
1498 isa<Constant>(Cases[0].CmpRHS) &&
1499 cast<Constant>(Cases[0].CmpRHS)->isNullValue()) {
1500 if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB)
1501 return false;
1502 if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB)
1503 return false;
1504 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00001505
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001506 return true;
1507}
1508
Dan Gohman46510a72010-04-15 01:51:59 +00001509void SelectionDAGBuilder::visitBr(const BranchInst &I) {
Dan Gohman84023e02010-07-10 09:00:22 +00001510 MachineBasicBlock *BrMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001511
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001512 // Update machine-CFG edges.
1513 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1514
1515 // Figure out which block is immediately after the current one.
1516 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001517 MachineFunction::iterator BBI = BrMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001518 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001519 NextBlock = BBI;
1520
1521 if (I.isUnconditional()) {
1522 // Update machine-CFG edges.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001523 BrMBB->addSuccessor(Succ0MBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001524
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001525 // If this is not a fall-through branch, emit the branch.
Bill Wendling4533cac2010-01-28 21:51:40 +00001526 if (Succ0MBB != NextBlock)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001527 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001528 MVT::Other, getControlRoot(),
Bill Wendling4533cac2010-01-28 21:51:40 +00001529 DAG.getBasicBlock(Succ0MBB)));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001530
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001531 return;
1532 }
1533
1534 // If this condition is one of the special cases we handle, do special stuff
1535 // now.
Dan Gohman46510a72010-04-15 01:51:59 +00001536 const Value *CondVal = I.getCondition();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001537 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1538
1539 // If this is a series of conditions that are or'd or and'd together, emit
1540 // this as a sequence of branches instead of setcc's with and/or operations.
Chris Lattnerde189be2010-11-30 18:12:52 +00001541 // As long as jumps are not expensive, this should improve performance.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001542 // For example, instead of something like:
1543 // cmp A, B
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001544 // C = seteq
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001545 // cmp D, E
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001546 // F = setle
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001547 // or C, F
1548 // jnz foo
1549 // Emit:
1550 // cmp A, B
1551 // je foo
1552 // cmp D, E
1553 // jle foo
1554 //
Dan Gohman46510a72010-04-15 01:51:59 +00001555 if (const BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00001556 if (!TM.getTargetLowering()->isJumpExpensive() &&
Chris Lattnerde189be2010-11-30 18:12:52 +00001557 BOp->hasOneUse() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001558 (BOp->getOpcode() == Instruction::And ||
1559 BOp->getOpcode() == Instruction::Or)) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001560 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, BrMBB, BrMBB,
1561 BOp->getOpcode());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001562 // If the compares in later blocks need to use values not currently
1563 // exported from this block, export them now. This block should always
1564 // be the first entry.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001565 assert(SwitchCases[0].ThisBB == BrMBB && "Unexpected lowering!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001566
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001567 // Allow some cases to be rejected.
1568 if (ShouldEmitAsBranches(SwitchCases)) {
1569 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1570 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1571 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1572 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001573
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001574 // Emit the branch for this block.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001575 visitSwitchCase(SwitchCases[0], BrMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001576 SwitchCases.erase(SwitchCases.begin());
1577 return;
1578 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001579
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001580 // Okay, we decided not to do this, remove any inserted MBB's and clear
1581 // SwitchCases.
1582 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001583 FuncInfo.MF->erase(SwitchCases[i].ThisBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001584
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001585 SwitchCases.clear();
1586 }
1587 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001588
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001589 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001590 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohman99be8ae2010-04-19 22:41:47 +00001591 NULL, Succ0MBB, Succ1MBB, BrMBB);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001592
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001593 // Use visitSwitchCase to actually insert the fast branch sequence for this
1594 // cond branch.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001595 visitSwitchCase(CB, BrMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001596}
1597
1598/// visitSwitchCase - Emits the necessary code to represent a single node in
1599/// the binary search tree resulting from lowering a switch instruction.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001600void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB,
1601 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001602 SDValue Cond;
1603 SDValue CondLHS = getValue(CB.CmpLHS);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001604 SDLoc dl = getCurSDLoc();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001605
1606 // Build the setcc now.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001607 if (CB.CmpMHS == NULL) {
1608 // Fold "(X == true)" to X and "(X == false)" to !X to
1609 // handle common cases produced by branch lowering.
Owen Anderson5defacc2009-07-31 17:39:07 +00001610 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001611 CB.CC == ISD::SETEQ)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001612 Cond = CondLHS;
Owen Anderson5defacc2009-07-31 17:39:07 +00001613 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001614 CB.CC == ISD::SETEQ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001615 SDValue True = DAG.getConstant(1, CondLHS.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001616 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001617 } else
Owen Anderson825b72b2009-08-11 20:47:22 +00001618 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001619 } else {
Bob Wilsondb3a9e62013-09-09 19:14:35 +00001620 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001621
Anton Korobeynikov23218582008-12-23 22:25:27 +00001622 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
1623 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001624
1625 SDValue CmpOp = getValue(CB.CmpMHS);
Owen Andersone50ed302009-08-10 22:56:29 +00001626 EVT VT = CmpOp.getValueType();
Stephen Lin155615d2013-07-08 00:37:03 +00001627
Bob Wilsondb3a9e62013-09-09 19:14:35 +00001628 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001629 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT),
Bob Wilsondb3a9e62013-09-09 19:14:35 +00001630 ISD::SETLE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001631 } else {
Dale Johannesenf5d97892009-02-04 01:48:28 +00001632 SDValue SUB = DAG.getNode(ISD::SUB, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001633 VT, CmpOp, DAG.getConstant(Low, VT));
Owen Anderson825b72b2009-08-11 20:47:22 +00001634 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001635 DAG.getConstant(High-Low, VT), ISD::SETULE);
1636 }
1637 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001638
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001639 // Update successor info
Jakub Staszakc8f34de2011-07-29 22:25:21 +00001640 addSuccessorWithWeight(SwitchBB, CB.TrueBB, CB.TrueWeight);
Jakob Stoklund Olesene7fdef42012-08-20 21:39:52 +00001641 // TrueBB and FalseBB are always different unless the incoming IR is
1642 // degenerate. This only happens when running llc on weird IR.
1643 if (CB.TrueBB != CB.FalseBB)
1644 addSuccessorWithWeight(SwitchBB, CB.FalseBB, CB.FalseWeight);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001645
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001646 // Set NextBlock to be the MBB immediately after the current one, if any.
1647 // This is used to avoid emitting unnecessary branches to the next block.
1648 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001649 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001650 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001651 NextBlock = BBI;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001652
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001653 // If the lhs block is the next block, invert the condition so that we can
1654 // fall through to the lhs instead of the rhs block.
1655 if (CB.TrueBB == NextBlock) {
1656 std::swap(CB.TrueBB, CB.FalseBB);
1657 SDValue True = DAG.getConstant(1, Cond.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001658 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001659 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001660
Dale Johannesenf5d97892009-02-04 01:48:28 +00001661 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001662 MVT::Other, getControlRoot(), Cond,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001663 DAG.getBasicBlock(CB.TrueBB));
Bill Wendling87710f02009-12-21 23:47:40 +00001664
Evan Cheng266a99d2010-09-23 06:51:55 +00001665 // Insert the false branch. Do this even if it's a fall through branch,
1666 // this makes it easier to do DAG optimizations which require inverting
1667 // the branch condition.
1668 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
1669 DAG.getBasicBlock(CB.FalseBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001670
1671 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001672}
1673
1674/// visitJumpTable - Emit JumpTable node in the current MBB
Dan Gohman2048b852009-11-23 18:04:58 +00001675void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001676 // Emit the code for the jump table
1677 assert(JT.Reg != -1U && "Should lower JT Header first!");
Bill Wendlingba54bca2013-06-19 21:36:55 +00001678 EVT PTy = TM.getTargetLowering()->getPointerTy();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001679 SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurSDLoc(),
Dale Johannesena04b7572009-02-03 23:04:43 +00001680 JT.Reg, PTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001681 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001682 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurSDLoc(),
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001683 MVT::Other, Index.getValue(1),
1684 Table, Index);
1685 DAG.setRoot(BrJumpTable);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001686}
1687
1688/// visitJumpTableHeader - This function emits necessary code to produce index
1689/// in the JumpTable from switch case.
Dan Gohman2048b852009-11-23 18:04:58 +00001690void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001691 JumpTableHeader &JTH,
1692 MachineBasicBlock *SwitchBB) {
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001693 // Subtract the lowest switch case value from the value being switched on and
1694 // conditional branch to default mbb if the result is greater than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001695 // difference between smallest and largest cases.
1696 SDValue SwitchOp = getValue(JTH.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001697 EVT VT = SwitchOp.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001698 SDValue Sub = DAG.getNode(ISD::SUB, getCurSDLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001699 DAG.getConstant(JTH.First, VT));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001700
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001701 // The SDNode we just created, which holds the value being switched on minus
Dan Gohmanf451cb82010-02-10 16:03:48 +00001702 // the smallest case value, needs to be copied to a virtual register so it
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001703 // can be used as an index into the jump table in a subsequent basic block.
1704 // This value may be smaller or larger than the target's pointer type, and
1705 // therefore require extension or truncating.
Bill Wendlingba54bca2013-06-19 21:36:55 +00001706 const TargetLowering *TLI = TM.getTargetLowering();
1707 SwitchOp = DAG.getZExtOrTrunc(Sub, getCurSDLoc(), TLI->getPointerTy());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001708
Bill Wendlingba54bca2013-06-19 21:36:55 +00001709 unsigned JumpTableReg = FuncInfo.CreateReg(TLI->getPointerTy());
Andrew Trickac6d9be2013-05-25 02:42:55 +00001710 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurSDLoc(),
Dale Johannesena04b7572009-02-03 23:04:43 +00001711 JumpTableReg, SwitchOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001712 JT.Reg = JumpTableReg;
1713
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001714 // Emit the range check for the jump table, and branch to the default block
1715 // for the switch statement if the value being switched on exceeds the largest
1716 // case in the switch.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001717 SDValue CMP = DAG.getSetCC(getCurSDLoc(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00001718 TLI->getSetCCResultType(*DAG.getContext(),
1719 Sub.getValueType()),
Matt Arsenault225ed702013-05-18 00:21:46 +00001720 Sub,
1721 DAG.getConstant(JTH.Last - JTH.First,VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001722 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001723
1724 // Set NextBlock to be the MBB immediately after the current one, if any.
1725 // This is used to avoid emitting unnecessary branches to the next block.
1726 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001727 MachineFunction::iterator BBI = SwitchBB;
Bill Wendling87710f02009-12-21 23:47:40 +00001728
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001729 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001730 NextBlock = BBI;
1731
Andrew Trickac6d9be2013-05-25 02:42:55 +00001732 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurSDLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001733 MVT::Other, CopyTo, CMP,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001734 DAG.getBasicBlock(JT.Default));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001735
Bill Wendling4533cac2010-01-28 21:51:40 +00001736 if (JT.MBB != NextBlock)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001737 BrCond = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, BrCond,
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001738 DAG.getBasicBlock(JT.MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001739
Bill Wendling87710f02009-12-21 23:47:40 +00001740 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001741}
1742
Michael Gottesman657484f2013-08-20 07:00:16 +00001743/// Codegen a new tail for a stack protector check ParentMBB which has had its
1744/// tail spliced into a stack protector check success bb.
1745///
1746/// For a high level explanation of how this fits into the stack protector
1747/// generation see the comment on the declaration of class
1748/// StackProtectorDescriptor.
1749void SelectionDAGBuilder::visitSPDescriptorParent(StackProtectorDescriptor &SPD,
1750 MachineBasicBlock *ParentBB) {
1751
1752 // First create the loads to the guard/stack slot for the comparison.
1753 const TargetLowering *TLI = TM.getTargetLowering();
1754 EVT PtrTy = TLI->getPointerTy();
1755
1756 MachineFrameInfo *MFI = ParentBB->getParent()->getFrameInfo();
1757 int FI = MFI->getStackProtectorIndex();
1758
1759 const Value *IRGuard = SPD.getGuard();
1760 SDValue GuardPtr = getValue(IRGuard);
1761 SDValue StackSlotPtr = DAG.getFrameIndex(FI, PtrTy);
1762
1763 unsigned Align =
1764 TLI->getDataLayout()->getPrefTypeAlignment(IRGuard->getType());
1765 SDValue Guard = DAG.getLoad(PtrTy, getCurSDLoc(), DAG.getEntryNode(),
1766 GuardPtr, MachinePointerInfo(IRGuard, 0),
1767 true, false, false, Align);
1768
1769 SDValue StackSlot = DAG.getLoad(PtrTy, getCurSDLoc(), DAG.getEntryNode(),
1770 StackSlotPtr,
1771 MachinePointerInfo::getFixedStack(FI),
1772 true, false, false, Align);
1773
1774 // Perform the comparison via a subtract/getsetcc.
1775 EVT VT = Guard.getValueType();
1776 SDValue Sub = DAG.getNode(ISD::SUB, getCurSDLoc(), VT, Guard, StackSlot);
1777
1778 SDValue Cmp = DAG.getSetCC(getCurSDLoc(),
1779 TLI->getSetCCResultType(*DAG.getContext(),
1780 Sub.getValueType()),
1781 Sub, DAG.getConstant(0, VT),
1782 ISD::SETNE);
1783
1784 // If the sub is not 0, then we know the guard/stackslot do not equal, so
1785 // branch to failure MBB.
1786 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurSDLoc(),
1787 MVT::Other, StackSlot.getOperand(0),
1788 Cmp, DAG.getBasicBlock(SPD.getFailureMBB()));
1789 // Otherwise branch to success MBB.
1790 SDValue Br = DAG.getNode(ISD::BR, getCurSDLoc(),
1791 MVT::Other, BrCond,
1792 DAG.getBasicBlock(SPD.getSuccessMBB()));
1793
1794 DAG.setRoot(Br);
1795}
1796
1797/// Codegen the failure basic block for a stack protector check.
1798///
1799/// A failure stack protector machine basic block consists simply of a call to
1800/// __stack_chk_fail().
1801///
1802/// For a high level explanation of how this fits into the stack protector
1803/// generation see the comment on the declaration of class
1804/// StackProtectorDescriptor.
1805void
1806SelectionDAGBuilder::visitSPDescriptorFailure(StackProtectorDescriptor &SPD) {
1807 const TargetLowering *TLI = TM.getTargetLowering();
1808 SDValue Chain = TLI->makeLibCall(DAG, RTLIB::STACKPROTECTOR_CHECK_FAIL,
1809 MVT::isVoid, 0, 0, false, getCurSDLoc(),
Michael Gottesman58a9b432013-08-22 23:45:24 +00001810 false, false).second;
Michael Gottesman657484f2013-08-20 07:00:16 +00001811 DAG.setRoot(Chain);
1812}
1813
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001814/// visitBitTestHeader - This function emits necessary code to produce value
1815/// suitable for "bit tests"
Dan Gohman99be8ae2010-04-19 22:41:47 +00001816void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B,
1817 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001818 // Subtract the minimum value
1819 SDValue SwitchOp = getValue(B.SValue);
Patrik Hagglund34525f92012-12-11 11:14:33 +00001820 EVT VT = SwitchOp.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001821 SDValue Sub = DAG.getNode(ISD::SUB, getCurSDLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001822 DAG.getConstant(B.First, VT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001823
1824 // Check range
Bill Wendlingba54bca2013-06-19 21:36:55 +00001825 const TargetLowering *TLI = TM.getTargetLowering();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001826 SDValue RangeCmp = DAG.getSetCC(getCurSDLoc(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00001827 TLI->getSetCCResultType(*DAG.getContext(),
Matt Arsenault225ed702013-05-18 00:21:46 +00001828 Sub.getValueType()),
Bill Wendling87710f02009-12-21 23:47:40 +00001829 Sub, DAG.getConstant(B.Range, VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001830 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001831
Evan Chengd08e5b42011-01-06 01:02:44 +00001832 // Determine the type of the test operands.
1833 bool UsePtrType = false;
Bill Wendlingba54bca2013-06-19 21:36:55 +00001834 if (!TLI->isTypeLegal(VT))
Evan Chengd08e5b42011-01-06 01:02:44 +00001835 UsePtrType = true;
1836 else {
1837 for (unsigned i = 0, e = B.Cases.size(); i != e; ++i)
Eli Friedman5c75af62011-10-12 22:46:45 +00001838 if (!isUIntN(VT.getSizeInBits(), B.Cases[i].Mask)) {
Evan Chengd08e5b42011-01-06 01:02:44 +00001839 // Switch table case range are encoded into series of masks.
1840 // Just use pointer type, it's guaranteed to fit.
1841 UsePtrType = true;
1842 break;
1843 }
1844 }
1845 if (UsePtrType) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00001846 VT = TLI->getPointerTy();
Andrew Trickac6d9be2013-05-25 02:42:55 +00001847 Sub = DAG.getZExtOrTrunc(Sub, getCurSDLoc(), VT);
Evan Chengd08e5b42011-01-06 01:02:44 +00001848 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001849
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00001850 B.RegVT = VT.getSimpleVT();
Patrik Hagglund8963fec2012-12-19 12:23:01 +00001851 B.Reg = FuncInfo.CreateReg(B.RegVT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001852 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurSDLoc(),
Evan Chengd08e5b42011-01-06 01:02:44 +00001853 B.Reg, Sub);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001854
1855 // Set NextBlock to be the MBB immediately after the current one, if any.
1856 // This is used to avoid emitting unnecessary branches to the next block.
1857 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001858 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001859 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001860 NextBlock = BBI;
1861
1862 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1863
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001864 addSuccessorWithWeight(SwitchBB, B.Default);
1865 addSuccessorWithWeight(SwitchBB, MBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001866
Andrew Trickac6d9be2013-05-25 02:42:55 +00001867 SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurSDLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001868 MVT::Other, CopyTo, RangeCmp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001869 DAG.getBasicBlock(B.Default));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001870
Evan Cheng8c1f4322010-09-23 18:32:19 +00001871 if (MBB != NextBlock)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001872 BrRange = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, CopyTo,
Evan Cheng8c1f4322010-09-23 18:32:19 +00001873 DAG.getBasicBlock(MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001874
Bill Wendling87710f02009-12-21 23:47:40 +00001875 DAG.setRoot(BrRange);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001876}
1877
1878/// visitBitTestCase - this function produces one "bit test"
Evan Chengd08e5b42011-01-06 01:02:44 +00001879void SelectionDAGBuilder::visitBitTestCase(BitTestBlock &BB,
1880 MachineBasicBlock* NextMBB,
Manman Ren1a710fd2012-08-24 18:14:27 +00001881 uint32_t BranchWeightToNext,
Dan Gohman2048b852009-11-23 18:04:58 +00001882 unsigned Reg,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001883 BitTestCase &B,
1884 MachineBasicBlock *SwitchBB) {
Patrik Hagglund8963fec2012-12-19 12:23:01 +00001885 MVT VT = BB.RegVT;
Andrew Trickac6d9be2013-05-25 02:42:55 +00001886 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurSDLoc(),
Evan Chengd08e5b42011-01-06 01:02:44 +00001887 Reg, VT);
Dan Gohman8e0163a2010-06-24 02:06:24 +00001888 SDValue Cmp;
Benjamin Kramer3ff25512011-07-14 01:38:42 +00001889 unsigned PopCount = CountPopulation_64(B.Mask);
Bill Wendlingba54bca2013-06-19 21:36:55 +00001890 const TargetLowering *TLI = TM.getTargetLowering();
Benjamin Kramer3ff25512011-07-14 01:38:42 +00001891 if (PopCount == 1) {
Dan Gohman8e0163a2010-06-24 02:06:24 +00001892 // Testing for a single bit; just compare the shift count with what it
1893 // would need to be to shift a 1 bit in that position.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001894 Cmp = DAG.getSetCC(getCurSDLoc(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00001895 TLI->getSetCCResultType(*DAG.getContext(), VT),
Dan Gohman8e0163a2010-06-24 02:06:24 +00001896 ShiftOp,
Michael J. Spencerc6af2432013-05-24 22:23:49 +00001897 DAG.getConstant(countTrailingZeros(B.Mask), VT),
Dan Gohman8e0163a2010-06-24 02:06:24 +00001898 ISD::SETEQ);
Benjamin Kramer3ff25512011-07-14 01:38:42 +00001899 } else if (PopCount == BB.Range) {
1900 // There is only one zero bit in the range, test for it directly.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001901 Cmp = DAG.getSetCC(getCurSDLoc(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00001902 TLI->getSetCCResultType(*DAG.getContext(), VT),
Benjamin Kramer3ff25512011-07-14 01:38:42 +00001903 ShiftOp,
1904 DAG.getConstant(CountTrailingOnes_64(B.Mask), VT),
1905 ISD::SETNE);
Dan Gohman8e0163a2010-06-24 02:06:24 +00001906 } else {
1907 // Make desired shift
Andrew Trickac6d9be2013-05-25 02:42:55 +00001908 SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurSDLoc(), VT,
Evan Chengd08e5b42011-01-06 01:02:44 +00001909 DAG.getConstant(1, VT), ShiftOp);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001910
Dan Gohman8e0163a2010-06-24 02:06:24 +00001911 // Emit bit tests and jumps
Andrew Trickac6d9be2013-05-25 02:42:55 +00001912 SDValue AndOp = DAG.getNode(ISD::AND, getCurSDLoc(),
Evan Chengd08e5b42011-01-06 01:02:44 +00001913 VT, SwitchVal, DAG.getConstant(B.Mask, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00001914 Cmp = DAG.getSetCC(getCurSDLoc(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00001915 TLI->getSetCCResultType(*DAG.getContext(), VT),
Evan Chengd08e5b42011-01-06 01:02:44 +00001916 AndOp, DAG.getConstant(0, VT),
Dan Gohman8e0163a2010-06-24 02:06:24 +00001917 ISD::SETNE);
1918 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001919
Manman Ren1a710fd2012-08-24 18:14:27 +00001920 // The branch weight from SwitchBB to B.TargetBB is B.ExtraWeight.
1921 addSuccessorWithWeight(SwitchBB, B.TargetBB, B.ExtraWeight);
1922 // The branch weight from SwitchBB to NextMBB is BranchWeightToNext.
1923 addSuccessorWithWeight(SwitchBB, NextMBB, BranchWeightToNext);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001924
Andrew Trickac6d9be2013-05-25 02:42:55 +00001925 SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurSDLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001926 MVT::Other, getControlRoot(),
Dan Gohman8e0163a2010-06-24 02:06:24 +00001927 Cmp, DAG.getBasicBlock(B.TargetBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001928
1929 // Set NextBlock to be the MBB immediately after the current one, if any.
1930 // This is used to avoid emitting unnecessary branches to the next block.
1931 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001932 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001933 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001934 NextBlock = BBI;
1935
Evan Cheng8c1f4322010-09-23 18:32:19 +00001936 if (NextMBB != NextBlock)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001937 BrAnd = DAG.getNode(ISD::BR, getCurSDLoc(), MVT::Other, BrAnd,
Evan Cheng8c1f4322010-09-23 18:32:19 +00001938 DAG.getBasicBlock(NextMBB));
Bill Wendling0777e922009-12-21 21:59:52 +00001939
Bill Wendling87710f02009-12-21 23:47:40 +00001940 DAG.setRoot(BrAnd);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001941}
1942
Dan Gohman46510a72010-04-15 01:51:59 +00001943void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) {
Dan Gohman84023e02010-07-10 09:00:22 +00001944 MachineBasicBlock *InvokeMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001945
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001946 // Retrieve successors.
1947 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1948 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1949
Gabor Greifb67e6b32009-01-15 11:10:44 +00001950 const Value *Callee(I.getCalledValue());
Nuno Lopes85b40892012-06-28 22:30:12 +00001951 const Function *Fn = dyn_cast<Function>(Callee);
Gabor Greifb67e6b32009-01-15 11:10:44 +00001952 if (isa<InlineAsm>(Callee))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001953 visitInlineAsm(&I);
Nuno Lopes85b40892012-06-28 22:30:12 +00001954 else if (Fn && Fn->isIntrinsic()) {
1955 assert(Fn->getIntrinsicID() == Intrinsic::donothing);
Nuno Lopes4532bf62012-07-18 00:07:17 +00001956 // Ignore invokes to @llvm.donothing: jump directly to the next BB.
Nuno Lopes85b40892012-06-28 22:30:12 +00001957 } else
Gabor Greifb67e6b32009-01-15 11:10:44 +00001958 LowerCallTo(&I, getValue(Callee), false, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001959
1960 // If the value of the invoke is used outside of its defining block, make it
1961 // available as a virtual register.
Dan Gohmanad62f532009-04-23 23:13:24 +00001962 CopyToExportRegsIfNeeded(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001963
1964 // Update successor info
Chandler Carruthf2645682011-11-22 11:37:46 +00001965 addSuccessorWithWeight(InvokeMBB, Return);
1966 addSuccessorWithWeight(InvokeMBB, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001967
1968 // Drop into normal successor.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001969 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00001970 MVT::Other, getControlRoot(),
1971 DAG.getBasicBlock(Return)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001972}
1973
Bill Wendlingdccc03b2011-07-31 06:30:59 +00001974void SelectionDAGBuilder::visitResume(const ResumeInst &RI) {
1975 llvm_unreachable("SelectionDAGBuilder shouldn't visit resume instructions!");
1976}
1977
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00001978void SelectionDAGBuilder::visitLandingPad(const LandingPadInst &LP) {
1979 assert(FuncInfo.MBB->isLandingPad() &&
1980 "Call to landingpad not in landing pad!");
1981
1982 MachineBasicBlock *MBB = FuncInfo.MBB;
1983 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
1984 AddLandingPadInfo(LP, MMI, MBB);
1985
Bill Wendlingbdf9db62012-02-13 23:47:16 +00001986 // If there aren't registers to copy the values into (e.g., during SjLj
1987 // exceptions), then don't bother to create these DAG nodes.
Bill Wendlingba54bca2013-06-19 21:36:55 +00001988 const TargetLowering *TLI = TM.getTargetLowering();
1989 if (TLI->getExceptionPointerRegister() == 0 &&
1990 TLI->getExceptionSelectorRegister() == 0)
Bill Wendlingbdf9db62012-02-13 23:47:16 +00001991 return;
1992
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00001993 SmallVector<EVT, 2> ValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00001994 ComputeValueVTs(*TLI, LP.getType(), ValueVTs);
Jakob Stoklund Olesen918b7c82013-07-04 04:53:45 +00001995 assert(ValueVTs.size() == 2 && "Only two-valued landingpads are supported");
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00001996
Jakob Stoklund Olesen918b7c82013-07-04 04:53:45 +00001997 // Get the two live-in registers as SDValues. The physregs have already been
1998 // copied into virtual registers.
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00001999 SDValue Ops[2];
Jakob Stoklund Olesen918b7c82013-07-04 04:53:45 +00002000 Ops[0] = DAG.getZExtOrTrunc(
2001 DAG.getCopyFromReg(DAG.getEntryNode(), getCurSDLoc(),
2002 FuncInfo.ExceptionPointerVirtReg, TLI->getPointerTy()),
2003 getCurSDLoc(), ValueVTs[0]);
2004 Ops[1] = DAG.getZExtOrTrunc(
2005 DAG.getCopyFromReg(DAG.getEntryNode(), getCurSDLoc(),
2006 FuncInfo.ExceptionSelectorVirtReg, TLI->getPointerTy()),
2007 getCurSDLoc(), ValueVTs[1]);
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00002008
Jakob Stoklund Olesen918b7c82013-07-04 04:53:45 +00002009 // Merge into one.
Andrew Trickac6d9be2013-05-25 02:42:55 +00002010 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00002011 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
2012 &Ops[0], 2);
Jakob Stoklund Olesen918b7c82013-07-04 04:53:45 +00002013 setValue(&LP, Res);
Bill Wendling2ac0e6b2011-08-17 21:56:44 +00002014}
2015
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002016/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
2017/// small case ranges).
Dan Gohman2048b852009-11-23 18:04:58 +00002018bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR,
2019 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00002020 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00002021 MachineBasicBlock *Default,
2022 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002023 // Size is the number of Cases represented by this range.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002024 size_t Size = CR.Range.second - CR.Range.first;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002025 if (Size > 3)
Anton Korobeynikov23218582008-12-23 22:25:27 +00002026 return false;
2027
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002028 // Get the MachineFunction which holds the current MBB. This is used when
2029 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002030 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002031
2032 // Figure out which block is immediately after the current one.
2033 MachineBasicBlock *NextBlock = 0;
2034 MachineFunction::iterator BBI = CR.CaseBB;
2035
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002036 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002037 NextBlock = BBI;
2038
Manman Ren1a710fd2012-08-24 18:14:27 +00002039 BranchProbabilityInfo *BPI = FuncInfo.BPI;
Benjamin Kramerce750f02010-11-22 09:45:38 +00002040 // If any two of the cases has the same destination, and if one value
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002041 // is the same as the other, but has one bit unset that the other has set,
2042 // use bit manipulation to do two compares at once. For example:
2043 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
Benjamin Kramerce750f02010-11-22 09:45:38 +00002044 // TODO: This could be extended to merge any 2 cases in switches with 3 cases.
2045 // TODO: Handle cases where CR.CaseBB != SwitchBB.
2046 if (Size == 2 && CR.CaseBB == SwitchBB) {
2047 Case &Small = *CR.Range.first;
2048 Case &Big = *(CR.Range.second-1);
2049
2050 if (Small.Low == Small.High && Big.Low == Big.High && Small.BB == Big.BB) {
2051 const APInt& SmallValue = cast<ConstantInt>(Small.Low)->getValue();
2052 const APInt& BigValue = cast<ConstantInt>(Big.Low)->getValue();
2053
2054 // Check that there is only one bit different.
2055 if (BigValue.countPopulation() == SmallValue.countPopulation() + 1 &&
2056 (SmallValue | BigValue) == BigValue) {
2057 // Isolate the common bit.
2058 APInt CommonBit = BigValue & ~SmallValue;
2059 assert((SmallValue | CommonBit) == BigValue &&
2060 CommonBit.countPopulation() == 1 && "Not a common bit?");
2061
2062 SDValue CondLHS = getValue(SV);
2063 EVT VT = CondLHS.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00002064 SDLoc DL = getCurSDLoc();
Benjamin Kramerce750f02010-11-22 09:45:38 +00002065
2066 SDValue Or = DAG.getNode(ISD::OR, DL, VT, CondLHS,
2067 DAG.getConstant(CommonBit, VT));
2068 SDValue Cond = DAG.getSetCC(DL, MVT::i1,
2069 Or, DAG.getConstant(BigValue, VT),
2070 ISD::SETEQ);
2071
2072 // Update successor info.
Manman Ren1a710fd2012-08-24 18:14:27 +00002073 // Both Small and Big will jump to Small.BB, so we sum up the weights.
2074 addSuccessorWithWeight(SwitchBB, Small.BB,
2075 Small.ExtraWeight + Big.ExtraWeight);
2076 addSuccessorWithWeight(SwitchBB, Default,
2077 // The default destination is the first successor in IR.
2078 BPI ? BPI->getEdgeWeight(SwitchBB->getBasicBlock(), (unsigned)0) : 0);
Benjamin Kramerce750f02010-11-22 09:45:38 +00002079
2080 // Insert the true branch.
2081 SDValue BrCond = DAG.getNode(ISD::BRCOND, DL, MVT::Other,
2082 getControlRoot(), Cond,
2083 DAG.getBasicBlock(Small.BB));
2084
2085 // Insert the false branch.
2086 BrCond = DAG.getNode(ISD::BR, DL, MVT::Other, BrCond,
2087 DAG.getBasicBlock(Default));
2088
2089 DAG.setRoot(BrCond);
2090 return true;
2091 }
2092 }
2093 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002094
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002095 // Order cases by weight so the most likely case will be checked first.
Manman Ren1a710fd2012-08-24 18:14:27 +00002096 uint32_t UnhandledWeights = 0;
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002097 if (BPI) {
2098 for (CaseItr I = CR.Range.first, IE = CR.Range.second; I != IE; ++I) {
Manman Ren1a710fd2012-08-24 18:14:27 +00002099 uint32_t IWeight = I->ExtraWeight;
2100 UnhandledWeights += IWeight;
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002101 for (CaseItr J = CR.Range.first; J < I; ++J) {
Manman Ren1a710fd2012-08-24 18:14:27 +00002102 uint32_t JWeight = J->ExtraWeight;
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002103 if (IWeight > JWeight)
2104 std::swap(*I, *J);
2105 }
2106 }
2107 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002108 // Rearrange the case blocks so that the last one falls through if possible.
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002109 Case &BackCase = *(CR.Range.second-1);
Benjamin Kramer5db954d2012-05-26 21:19:12 +00002110 if (Size > 1 &&
2111 NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002112 // The last case block won't fall through into 'NextBlock' if we emit the
2113 // branches in this order. See if rearranging a case value would help.
Benjamin Kramerc511b2a2012-05-26 20:01:32 +00002114 // We start at the bottom as it's the case with the least weight.
Stephen Lin09f8ca32013-07-06 21:44:25 +00002115 for (Case *I = &*(CR.Range.second-2), *E = &*CR.Range.first-1; I != E; --I)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002116 if (I->BB == NextBlock) {
2117 std::swap(*I, BackCase);
2118 break;
2119 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002120 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002121
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002122 // Create a CaseBlock record representing a conditional branch to
2123 // the Case's target mbb if the value being switched on SV is equal
2124 // to C.
2125 MachineBasicBlock *CurBlock = CR.CaseBB;
2126 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
2127 MachineBasicBlock *FallThrough;
2128 if (I != E-1) {
2129 FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
2130 CurMF->insert(BBI, FallThrough);
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002131
2132 // Put SV in a virtual register to make it available from the new blocks.
2133 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002134 } else {
2135 // If the last case doesn't match, go to the default block.
2136 FallThrough = Default;
2137 }
2138
Dan Gohman46510a72010-04-15 01:51:59 +00002139 const Value *RHS, *LHS, *MHS;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002140 ISD::CondCode CC;
2141 if (I->High == I->Low) {
2142 // This is just small small case range :) containing exactly 1 case
2143 CC = ISD::SETEQ;
2144 LHS = SV; RHS = I->High; MHS = NULL;
2145 } else {
Bob Wilsondb3a9e62013-09-09 19:14:35 +00002146 CC = ISD::SETLE;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002147 LHS = I->Low; MHS = SV; RHS = I->High;
2148 }
Jakub Staszakc8f34de2011-07-29 22:25:21 +00002149
Manman Ren1a710fd2012-08-24 18:14:27 +00002150 // The false weight should be sum of all un-handled cases.
2151 UnhandledWeights -= I->ExtraWeight;
Jakub Staszakc8f34de2011-07-29 22:25:21 +00002152 CaseBlock CB(CC, LHS, RHS, MHS, /* truebb */ I->BB, /* falsebb */ FallThrough,
2153 /* me */ CurBlock,
Manman Ren1a710fd2012-08-24 18:14:27 +00002154 /* trueweight */ I->ExtraWeight,
2155 /* falseweight */ UnhandledWeights);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002156
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002157 // If emitting the first comparison, just call visitSwitchCase to emit the
2158 // code into the current block. Otherwise, push the CaseBlock onto the
2159 // vector to be later processed by SDISel, and insert the node's MBB
2160 // before the next MBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002161 if (CurBlock == SwitchBB)
2162 visitSwitchCase(CB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002163 else
2164 SwitchCases.push_back(CB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002165
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002166 CurBlock = FallThrough;
2167 }
2168
2169 return true;
2170}
2171
2172static inline bool areJTsAllowed(const TargetLowering &TLI) {
Evan Cheng769951f2012-07-02 22:39:56 +00002173 return TLI.supportJumpTables() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00002174 (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
2175 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002176}
Anton Korobeynikov23218582008-12-23 22:25:27 +00002177
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002178static APInt ComputeRange(const APInt &First, const APInt &Last) {
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002179 uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1;
Bob Wilsondb3a9e62013-09-09 19:14:35 +00002180 APInt LastExt = Last.sext(BitWidth), FirstExt = First.sext(BitWidth);
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002181 return (LastExt - FirstExt + 1ULL);
2182}
2183
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002184/// handleJTSwitchCase - Emit jumptable for current switch case range
Chris Lattnerc3ab3882011-09-09 22:06:59 +00002185bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec &CR,
2186 CaseRecVector &WorkList,
2187 const Value *SV,
2188 MachineBasicBlock *Default,
Dan Gohman99be8ae2010-04-19 22:41:47 +00002189 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002190 Case& FrontCase = *CR.Range.first;
2191 Case& BackCase = *(CR.Range.second-1);
2192
Chris Lattnere880efe2009-11-07 07:50:34 +00002193 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
2194 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002195
Chris Lattnere880efe2009-11-07 07:50:34 +00002196 APInt TSize(First.getBitWidth(), 0);
Chris Lattnerc3ab3882011-09-09 22:06:59 +00002197 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002198 TSize += I->size();
2199
Bill Wendlingba54bca2013-06-19 21:36:55 +00002200 const TargetLowering *TLI = TM.getTargetLowering();
2201 if (!areJTsAllowed(*TLI) || TSize.ult(TLI->getMinimumJumpTableEntries()))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002202 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002203
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002204 APInt Range = ComputeRange(First, Last);
Jakob Stoklund Olesen79443912011-10-26 01:47:48 +00002205 // The density is TSize / Range. Require at least 40%.
2206 // It should not be possible for IntTSize to saturate for sane code, but make
2207 // sure we handle Range saturation correctly.
2208 uint64_t IntRange = Range.getLimitedValue(UINT64_MAX/10);
2209 uint64_t IntTSize = TSize.getLimitedValue(UINT64_MAX/10);
2210 if (IntTSize * 10 < IntRange * 4)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002211 return false;
2212
David Greene4b69d992010-01-05 01:24:57 +00002213 DEBUG(dbgs() << "Lowering jump table\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002214 << "First entry: " << First << ". Last entry: " << Last << '\n'
Jakob Stoklund Olesen79443912011-10-26 01:47:48 +00002215 << "Range: " << Range << ". Size: " << TSize << ".\n\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002216
2217 // Get the MachineFunction which holds the current MBB. This is used when
2218 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002219 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002220
2221 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002222 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00002223 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002224
2225 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
2226
2227 // Create a new basic block to hold the code for loading the address
2228 // of the jump table, and jumping to it. Update successor information;
2229 // we will either branch to the default case for the switch, or the jump
2230 // table.
2231 MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2232 CurMF->insert(BBI, JumpTableBB);
Jakub Staszak7cc2b072011-06-16 20:22:37 +00002233
2234 addSuccessorWithWeight(CR.CaseBB, Default);
2235 addSuccessorWithWeight(CR.CaseBB, JumpTableBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002236
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002237 // Build a vector of destination BBs, corresponding to each target
2238 // of the jump table. If the value of the jump table slot corresponds to
2239 // a case statement, push the case's BB onto the vector, otherwise, push
2240 // the default BB.
2241 std::vector<MachineBasicBlock*> DestBBs;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002242 APInt TEI = First;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002243 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
Chris Lattner071c62f2010-01-25 23:26:13 +00002244 const APInt &Low = cast<ConstantInt>(I->Low)->getValue();
2245 const APInt &High = cast<ConstantInt>(I->High)->getValue();
Anton Korobeynikov23218582008-12-23 22:25:27 +00002246
Bob Wilsondb3a9e62013-09-09 19:14:35 +00002247 if (Low.sle(TEI) && TEI.sle(High)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002248 DestBBs.push_back(I->BB);
2249 if (TEI==High)
2250 ++I;
2251 } else {
2252 DestBBs.push_back(Default);
2253 }
2254 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002255
Manman Ren1a710fd2012-08-24 18:14:27 +00002256 // Calculate weight for each unique destination in CR.
2257 DenseMap<MachineBasicBlock*, uint32_t> DestWeights;
2258 if (FuncInfo.BPI)
2259 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
2260 DenseMap<MachineBasicBlock*, uint32_t>::iterator Itr =
2261 DestWeights.find(I->BB);
Stephen Lin155615d2013-07-08 00:37:03 +00002262 if (Itr != DestWeights.end())
Manman Ren1a710fd2012-08-24 18:14:27 +00002263 Itr->second += I->ExtraWeight;
2264 else
2265 DestWeights[I->BB] = I->ExtraWeight;
2266 }
2267
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002268 // Update successor info. Add one edge to each unique successor.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002269 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
2270 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002271 E = DestBBs.end(); I != E; ++I) {
2272 if (!SuccsHandled[(*I)->getNumber()]) {
2273 SuccsHandled[(*I)->getNumber()] = true;
Manman Ren1a710fd2012-08-24 18:14:27 +00002274 DenseMap<MachineBasicBlock*, uint32_t>::iterator Itr =
2275 DestWeights.find(*I);
2276 addSuccessorWithWeight(JumpTableBB, *I,
2277 Itr != DestWeights.end() ? Itr->second : 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002278 }
2279 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002280
Bob Wilsond1ec31d2010-03-18 18:42:41 +00002281 // Create a jump table index for this jump table.
Bill Wendlingba54bca2013-06-19 21:36:55 +00002282 unsigned JTEncoding = TLI->getJumpTableEncoding();
Chris Lattner071c62f2010-01-25 23:26:13 +00002283 unsigned JTI = CurMF->getOrCreateJumpTableInfo(JTEncoding)
Bob Wilsond1ec31d2010-03-18 18:42:41 +00002284 ->createJumpTableIndex(DestBBs);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002285
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002286 // Set the jump table information so that we can codegen it as a second
2287 // MachineBasicBlock
2288 JumpTable JT(-1U, JTI, JumpTableBB, Default);
Dan Gohman99be8ae2010-04-19 22:41:47 +00002289 JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == SwitchBB));
2290 if (CR.CaseBB == SwitchBB)
2291 visitJumpTableHeader(JT, JTH, SwitchBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002292
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002293 JTCases.push_back(JumpTableBlock(JTH, JT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002294 return true;
2295}
2296
2297/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
2298/// 2 subtrees.
Dan Gohman2048b852009-11-23 18:04:58 +00002299bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR,
2300 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00002301 const Value* SV,
Stephen Lin09f8ca32013-07-06 21:44:25 +00002302 MachineBasicBlock* Default,
2303 MachineBasicBlock* SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002304 // Get the MachineFunction which holds the current MBB. This is used when
2305 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002306 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002307
2308 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002309 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00002310 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002311
2312 Case& FrontCase = *CR.Range.first;
2313 Case& BackCase = *(CR.Range.second-1);
2314 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
2315
2316 // Size is the number of Cases represented by this range.
2317 unsigned Size = CR.Range.second - CR.Range.first;
2318
Chris Lattnere880efe2009-11-07 07:50:34 +00002319 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
2320 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002321 double FMetric = 0;
2322 CaseItr Pivot = CR.Range.first + Size/2;
2323
2324 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
2325 // (heuristically) allow us to emit JumpTable's later.
Chris Lattnere880efe2009-11-07 07:50:34 +00002326 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002327 for (CaseItr I = CR.Range.first, E = CR.Range.second;
2328 I!=E; ++I)
2329 TSize += I->size();
2330
Chris Lattnere880efe2009-11-07 07:50:34 +00002331 APInt LSize = FrontCase.size();
2332 APInt RSize = TSize-LSize;
David Greene4b69d992010-01-05 01:24:57 +00002333 DEBUG(dbgs() << "Selecting best pivot: \n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002334 << "First: " << First << ", Last: " << Last <<'\n'
2335 << "LSize: " << LSize << ", RSize: " << RSize << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002336 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
2337 J!=E; ++I, ++J) {
Chris Lattnere880efe2009-11-07 07:50:34 +00002338 const APInt &LEnd = cast<ConstantInt>(I->High)->getValue();
2339 const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002340 APInt Range = ComputeRange(LEnd, RBegin);
Stepan Dyatkovskiyc2c52a62012-05-15 06:50:18 +00002341 assert((Range - 2ULL).isNonNegative() &&
2342 "Invalid case distance");
Chris Lattnerc3e4e592011-04-09 06:57:13 +00002343 // Use volatile double here to avoid excess precision issues on some hosts,
2344 // e.g. that use 80-bit X87 registers.
2345 volatile double LDensity =
2346 (double)LSize.roundToDouble() /
Chris Lattnere880efe2009-11-07 07:50:34 +00002347 (LEnd - First + 1ULL).roundToDouble();
Chris Lattnerc3e4e592011-04-09 06:57:13 +00002348 volatile double RDensity =
2349 (double)RSize.roundToDouble() /
Chris Lattnere880efe2009-11-07 07:50:34 +00002350 (Last - RBegin + 1ULL).roundToDouble();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002351 double Metric = Range.logBase2()*(LDensity+RDensity);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002352 // Should always split in some non-trivial place
David Greene4b69d992010-01-05 01:24:57 +00002353 DEBUG(dbgs() <<"=>Step\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002354 << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n'
2355 << "LDensity: " << LDensity
2356 << ", RDensity: " << RDensity << '\n'
2357 << "Metric: " << Metric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002358 if (FMetric < Metric) {
2359 Pivot = J;
2360 FMetric = Metric;
David Greene4b69d992010-01-05 01:24:57 +00002361 DEBUG(dbgs() << "Current metric set to: " << FMetric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002362 }
2363
2364 LSize += J->size();
2365 RSize -= J->size();
2366 }
Bill Wendlingba54bca2013-06-19 21:36:55 +00002367
2368 const TargetLowering *TLI = TM.getTargetLowering();
2369 if (areJTsAllowed(*TLI)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002370 // If our case is dense we *really* should handle it earlier!
2371 assert((FMetric > 0) && "Should handle dense range earlier!");
2372 } else {
2373 Pivot = CR.Range.first + Size/2;
2374 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002375
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002376 CaseRange LHSR(CR.Range.first, Pivot);
2377 CaseRange RHSR(Pivot, CR.Range.second);
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00002378 const Constant *C = Pivot->Low;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002379 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002380
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002381 // We know that we branch to the LHS if the Value being switched on is
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002382 // less than the Pivot value, C. We use this to optimize our binary
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002383 // tree a bit, by recognizing that if SV is greater than or equal to the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002384 // LHS's Case Value, and that Case Value is exactly one less than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002385 // Pivot's Value, then we can branch directly to the LHS's Target,
2386 // rather than creating a leaf node for it.
2387 if ((LHSR.second - LHSR.first) == 1 &&
2388 LHSR.first->High == CR.GE &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00002389 cast<ConstantInt>(C)->getValue() ==
2390 (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002391 TrueBB = LHSR.first->BB;
2392 } else {
2393 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2394 CurMF->insert(BBI, TrueBB);
2395 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002396
2397 // Put SV in a virtual register to make it available from the new blocks.
2398 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002399 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002400
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002401 // Similar to the optimization above, if the Value being switched on is
2402 // known to be less than the Constant CR.LT, and the current Case Value
2403 // is CR.LT - 1, then we can branch directly to the target block for
2404 // the current Case Value, rather than emitting a RHS leaf node for it.
2405 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00002406 cast<ConstantInt>(RHSR.first->Low)->getValue() ==
2407 (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002408 FalseBB = RHSR.first->BB;
2409 } else {
2410 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2411 CurMF->insert(BBI, FalseBB);
2412 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002413
2414 // Put SV in a virtual register to make it available from the new blocks.
2415 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002416 }
2417
2418 // Create a CaseBlock record representing a conditional branch to
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002419 // the LHS node if the value being switched on SV is less than C.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002420 // Otherwise, branch to LHS.
Bob Wilsondb3a9e62013-09-09 19:14:35 +00002421 CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002422
Dan Gohman99be8ae2010-04-19 22:41:47 +00002423 if (CR.CaseBB == SwitchBB)
2424 visitSwitchCase(CB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002425 else
2426 SwitchCases.push_back(CB);
2427
2428 return true;
2429}
2430
2431/// handleBitTestsSwitchCase - if current case range has few destination and
2432/// range span less, than machine word bitwidth, encode case range into series
2433/// of masks and emit bit tests with these masks.
Dan Gohman2048b852009-11-23 18:04:58 +00002434bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR,
2435 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00002436 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00002437 MachineBasicBlock* Default,
Stephen Lin09f8ca32013-07-06 21:44:25 +00002438 MachineBasicBlock* SwitchBB) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00002439 const TargetLowering *TLI = TM.getTargetLowering();
2440 EVT PTy = TLI->getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002441 unsigned IntPtrBits = PTy.getSizeInBits();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002442
2443 Case& FrontCase = *CR.Range.first;
2444 Case& BackCase = *(CR.Range.second-1);
2445
2446 // Get the MachineFunction which holds the current MBB. This is used when
2447 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002448 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002449
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00002450 // If target does not have legal shift left, do not emit bit tests at all.
Matt Arsenault599c0af2013-10-21 19:24:15 +00002451 if (!TLI->isOperationLegal(ISD::SHL, PTy))
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00002452 return false;
2453
Anton Korobeynikov23218582008-12-23 22:25:27 +00002454 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002455 for (CaseItr I = CR.Range.first, E = CR.Range.second;
2456 I!=E; ++I) {
2457 // Single case counts one, case range - two.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002458 numCmps += (I->Low == I->High ? 1 : 2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002459 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002460
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002461 // Count unique destinations
2462 SmallSet<MachineBasicBlock*, 4> Dests;
2463 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
2464 Dests.insert(I->BB);
2465 if (Dests.size() > 3)
2466 // Don't bother the code below, if there are too much unique destinations
2467 return false;
2468 }
David Greene4b69d992010-01-05 01:24:57 +00002469 DEBUG(dbgs() << "Total number of unique destinations: "
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002470 << Dests.size() << '\n'
2471 << "Total number of comparisons: " << numCmps << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00002472
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002473 // Compute span of values.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002474 const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue();
2475 const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002476 APInt cmpRange = maxValue - minValue;
2477
David Greene4b69d992010-01-05 01:24:57 +00002478 DEBUG(dbgs() << "Compare range: " << cmpRange << '\n'
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002479 << "Low bound: " << minValue << '\n'
2480 << "High bound: " << maxValue << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00002481
Dan Gohmane0567812010-04-08 23:03:40 +00002482 if (cmpRange.uge(IntPtrBits) ||
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002483 (!(Dests.size() == 1 && numCmps >= 3) &&
2484 !(Dests.size() == 2 && numCmps >= 5) &&
2485 !(Dests.size() >= 3 && numCmps >= 6)))
2486 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002487
David Greene4b69d992010-01-05 01:24:57 +00002488 DEBUG(dbgs() << "Emitting bit tests\n");
Anton Korobeynikov23218582008-12-23 22:25:27 +00002489 APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth());
2490
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002491 // Optimize the case where all the case values fit in a
2492 // word without having to subtract minValue. In this case,
2493 // we can optimize away the subtraction.
Bob Wilsondb3a9e62013-09-09 19:14:35 +00002494 if (minValue.isNonNegative() && maxValue.slt(IntPtrBits)) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002495 cmpRange = maxValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002496 } else {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002497 lowBound = minValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002498 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002499
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002500 CaseBitsVector CasesBits;
2501 unsigned i, count = 0;
2502
2503 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
2504 MachineBasicBlock* Dest = I->BB;
2505 for (i = 0; i < count; ++i)
2506 if (Dest == CasesBits[i].BB)
2507 break;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002508
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002509 if (i == count) {
2510 assert((count < 3) && "Too much destinations to test!");
Manman Ren1a710fd2012-08-24 18:14:27 +00002511 CasesBits.push_back(CaseBits(0, Dest, 0, 0/*Weight*/));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002512 count++;
2513 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002514
2515 const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue();
2516 const APInt& highValue = cast<ConstantInt>(I->High)->getValue();
2517
2518 uint64_t lo = (lowValue - lowBound).getZExtValue();
2519 uint64_t hi = (highValue - lowBound).getZExtValue();
Manman Ren1a710fd2012-08-24 18:14:27 +00002520 CasesBits[i].ExtraWeight += I->ExtraWeight;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002521
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002522 for (uint64_t j = lo; j <= hi; j++) {
2523 CasesBits[i].Mask |= 1ULL << j;
2524 CasesBits[i].Bits++;
2525 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002526
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002527 }
2528 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
Anton Korobeynikov23218582008-12-23 22:25:27 +00002529
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002530 BitTestInfo BTC;
2531
2532 // Figure out which block is immediately after the current one.
2533 MachineFunction::iterator BBI = CR.CaseBB;
2534 ++BBI;
2535
2536 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
2537
David Greene4b69d992010-01-05 01:24:57 +00002538 DEBUG(dbgs() << "Cases:\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002539 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
David Greene4b69d992010-01-05 01:24:57 +00002540 DEBUG(dbgs() << "Mask: " << CasesBits[i].Mask
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002541 << ", Bits: " << CasesBits[i].Bits
2542 << ", BB: " << CasesBits[i].BB << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002543
2544 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2545 CurMF->insert(BBI, CaseBB);
2546 BTC.push_back(BitTestCase(CasesBits[i].Mask,
2547 CaseBB,
Manman Ren1a710fd2012-08-24 18:14:27 +00002548 CasesBits[i].BB, CasesBits[i].ExtraWeight));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002549
2550 // Put SV in a virtual register to make it available from the new blocks.
2551 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002552 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002553
2554 BitTestBlock BTB(lowBound, cmpRange, SV,
Evan Chengd08e5b42011-01-06 01:02:44 +00002555 -1U, MVT::Other, (CR.CaseBB == SwitchBB),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002556 CR.CaseBB, Default, BTC);
2557
Dan Gohman99be8ae2010-04-19 22:41:47 +00002558 if (CR.CaseBB == SwitchBB)
2559 visitBitTestHeader(BTB, SwitchBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002560
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002561 BitTestCases.push_back(BTB);
2562
2563 return true;
2564}
2565
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002566/// Clusterify - Transform simple list of Cases into list of CaseRange's
Dan Gohman2048b852009-11-23 18:04:58 +00002567size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases,
2568 const SwitchInst& SI) {
Bob Wilsondb3a9e62013-09-09 19:14:35 +00002569 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002570
Manman Ren1a710fd2012-08-24 18:14:27 +00002571 BranchProbabilityInfo *BPI = FuncInfo.BPI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002572 // Start with "simple" cases
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00002573 for (SwitchInst::ConstCaseIt i = SI.case_begin(), e = SI.case_end();
Stepan Dyatkovskiyc10fa6c2012-03-08 07:06:20 +00002574 i != e; ++i) {
2575 const BasicBlock *SuccBB = i.getCaseSuccessor();
Jakub Staszakc8f34de2011-07-29 22:25:21 +00002576 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SuccBB];
2577
Bob Wilsondb3a9e62013-09-09 19:14:35 +00002578 uint32_t ExtraWeight =
2579 BPI ? BPI->getEdgeWeight(SI.getParent(), i.getSuccessorIndex()) : 0;
2580
2581 Cases.push_back(Case(i.getCaseValue(), i.getCaseValue(),
2582 SMBB, ExtraWeight));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002583 }
Bob Wilsondb3a9e62013-09-09 19:14:35 +00002584 std::sort(Cases.begin(), Cases.end(), CaseCmp());
Stephen Lin155615d2013-07-08 00:37:03 +00002585
Bob Wilsondb3a9e62013-09-09 19:14:35 +00002586 // Merge case into clusters
2587 if (Cases.size() >= 2)
2588 // Must recompute end() each iteration because it may be
2589 // invalidated by erase if we hold on to it
2590 for (CaseItr I = Cases.begin(), J = llvm::next(Cases.begin());
2591 J != Cases.end(); ) {
2592 const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue();
2593 const APInt& currentValue = cast<ConstantInt>(I->High)->getValue();
2594 MachineBasicBlock* nextBB = J->BB;
2595 MachineBasicBlock* currentBB = I->BB;
Stephen Lin155615d2013-07-08 00:37:03 +00002596
Bob Wilsondb3a9e62013-09-09 19:14:35 +00002597 // If the two neighboring cases go to the same destination, merge them
2598 // into a single case.
2599 if ((nextValue - currentValue == 1) && (currentBB == nextBB)) {
2600 I->High = J->High;
2601 I->ExtraWeight += J->ExtraWeight;
2602 J = Cases.erase(J);
2603 } else {
2604 I = J++;
2605 }
2606 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002607
Bob Wilsondb3a9e62013-09-09 19:14:35 +00002608 for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
2609 if (I->Low != I->High)
2610 // A range counts double, since it requires two compares.
2611 ++numCmps;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002612 }
2613
2614 return numCmps;
2615}
2616
Jakob Stoklund Olesen2622f462010-09-30 19:44:31 +00002617void SelectionDAGBuilder::UpdateSplitBlock(MachineBasicBlock *First,
2618 MachineBasicBlock *Last) {
2619 // Update JTCases.
2620 for (unsigned i = 0, e = JTCases.size(); i != e; ++i)
2621 if (JTCases[i].first.HeaderBB == First)
2622 JTCases[i].first.HeaderBB = Last;
2623
2624 // Update BitTestCases.
2625 for (unsigned i = 0, e = BitTestCases.size(); i != e; ++i)
2626 if (BitTestCases[i].Parent == First)
2627 BitTestCases[i].Parent = Last;
2628}
2629
Dan Gohman46510a72010-04-15 01:51:59 +00002630void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) {
Dan Gohman84023e02010-07-10 09:00:22 +00002631 MachineBasicBlock *SwitchMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00002632
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002633 // Figure out which block is immediately after the current one.
2634 MachineBasicBlock *NextBlock = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002635 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
2636
2637 // If there is only the default destination, branch to it if it is not the
2638 // next basic block. Otherwise, just fall through.
Stepan Dyatkovskiy24473122012-02-01 07:49:51 +00002639 if (!SI.getNumCases()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002640 // Update machine-CFG edges.
2641
2642 // If this is not a fall-through branch, emit the branch.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002643 SwitchMBB->addSuccessor(Default);
Bill Wendling4533cac2010-01-28 21:51:40 +00002644 if (Default != NextBlock)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002645 DAG.setRoot(DAG.getNode(ISD::BR, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002646 MVT::Other, getControlRoot(),
2647 DAG.getBasicBlock(Default)));
Bill Wendling49fcff82009-12-21 22:30:11 +00002648
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002649 return;
2650 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002651
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002652 // If there are any non-default case statements, create a vector of Cases
2653 // representing each one, and sort the vector so that we can efficiently
2654 // create a binary search tree from them.
2655 CaseVector Cases;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002656 size_t numCmps = Clusterify(Cases, SI);
David Greene4b69d992010-01-05 01:24:57 +00002657 DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size()
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002658 << ". Total compares: " << numCmps << '\n');
Duncan Sands17001ce2011-10-18 12:44:00 +00002659 (void)numCmps;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002660
2661 // Get the Value to be switched on and default basic blocks, which will be
2662 // inserted into CaseBlock records, representing basic blocks in the binary
2663 // search tree.
Eli Friedmanbb5a7442011-09-29 20:21:17 +00002664 const Value *SV = SI.getCondition();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002665
2666 // Push the initial CaseRec onto the worklist
2667 CaseRecVector WorkList;
Dan Gohman99be8ae2010-04-19 22:41:47 +00002668 WorkList.push_back(CaseRec(SwitchMBB,0,0,
2669 CaseRange(Cases.begin(),Cases.end())));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002670
2671 while (!WorkList.empty()) {
2672 // Grab a record representing a case range to process off the worklist
2673 CaseRec CR = WorkList.back();
2674 WorkList.pop_back();
2675
Dan Gohman99be8ae2010-04-19 22:41:47 +00002676 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002677 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002678
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002679 // If the range has few cases (two or less) emit a series of specific
2680 // tests.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002681 if (handleSmallSwitchRange(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002682 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002683
Sebastian Pop1a37d7e2012-09-25 20:35:36 +00002684 // If the switch has more than N blocks, and is at least 40% dense, and the
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002685 // target supports indirect branches, then emit a jump table rather than
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002686 // lowering the switch to a binary tree of conditional branches.
Sebastian Pop1a37d7e2012-09-25 20:35:36 +00002687 // N defaults to 4 and is controlled via TLS.getMinimumJumpTableEntries().
Dan Gohman99be8ae2010-04-19 22:41:47 +00002688 if (handleJTSwitchCase(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002689 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002690
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002691 // Emit binary tree. We need to pick a pivot, and push left and right ranges
2692 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002693 handleBTSplitSwitchCase(CR, WorkList, SV, Default, SwitchMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002694 }
2695}
2696
Dan Gohman46510a72010-04-15 01:51:59 +00002697void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) {
Dan Gohman84023e02010-07-10 09:00:22 +00002698 MachineBasicBlock *IndirectBrMBB = FuncInfo.MBB;
Dan Gohman99be8ae2010-04-19 22:41:47 +00002699
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002700 // Update machine-CFG edges with unique successors.
Nadav Rotemee0ce152012-10-23 21:05:33 +00002701 SmallSet<BasicBlock*, 32> Done;
2702 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i) {
2703 BasicBlock *BB = I.getSuccessor(i);
2704 bool Inserted = Done.insert(BB);
2705 if (!Inserted)
2706 continue;
2707
2708 MachineBasicBlock *Succ = FuncInfo.MBBMap[BB];
Jakub Staszak7cc2b072011-06-16 20:22:37 +00002709 addSuccessorWithWeight(IndirectBrMBB, Succ);
2710 }
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002711
Andrew Trickac6d9be2013-05-25 02:42:55 +00002712 DAG.setRoot(DAG.getNode(ISD::BRIND, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002713 MVT::Other, getControlRoot(),
2714 getValue(I.getAddress())));
Bill Wendling49fcff82009-12-21 22:30:11 +00002715}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002716
Dan Gohman46510a72010-04-15 01:51:59 +00002717void SelectionDAGBuilder::visitFSub(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002718 // -0.0 - X --> fneg
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002719 Type *Ty = I.getType();
Chris Lattner2ca5c862011-02-15 00:14:00 +00002720 if (isa<Constant>(I.getOperand(0)) &&
2721 I.getOperand(0) == ConstantFP::getZeroValueForNegation(Ty)) {
2722 SDValue Op2 = getValue(I.getOperand(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00002723 setValue(&I, DAG.getNode(ISD::FNEG, getCurSDLoc(),
Chris Lattner2ca5c862011-02-15 00:14:00 +00002724 Op2.getValueType(), Op2));
2725 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002726 }
Bill Wendling49fcff82009-12-21 22:30:11 +00002727
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002728 visitBinary(I, ISD::FSUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002729}
2730
Dan Gohman46510a72010-04-15 01:51:59 +00002731void SelectionDAGBuilder::visitBinary(const User &I, unsigned OpCode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002732 SDValue Op1 = getValue(I.getOperand(0));
2733 SDValue Op2 = getValue(I.getOperand(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00002734 setValue(&I, DAG.getNode(OpCode, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002735 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002736}
2737
Dan Gohman46510a72010-04-15 01:51:59 +00002738void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002739 SDValue Op1 = getValue(I.getOperand(0));
2740 SDValue Op2 = getValue(I.getOperand(1));
Owen Anderson95771af2011-02-25 21:41:48 +00002741
Bill Wendlingba54bca2013-06-19 21:36:55 +00002742 EVT ShiftTy = TM.getTargetLowering()->getShiftAmountTy(Op2.getValueType());
Owen Anderson95771af2011-02-25 21:41:48 +00002743
Chris Lattnerd3027732011-02-13 09:02:52 +00002744 // Coerce the shift amount to the right type if we can.
2745 if (!I.getType()->isVectorTy() && Op2.getValueType() != ShiftTy) {
Chris Lattner915eeb42011-02-13 09:10:56 +00002746 unsigned ShiftSize = ShiftTy.getSizeInBits();
2747 unsigned Op2Size = Op2.getValueType().getSizeInBits();
Andrew Trickac6d9be2013-05-25 02:42:55 +00002748 SDLoc DL = getCurSDLoc();
Owen Anderson95771af2011-02-25 21:41:48 +00002749
Dan Gohman57fc82d2009-04-09 03:51:29 +00002750 // If the operand is smaller than the shift count type, promote it.
Chris Lattnerd3027732011-02-13 09:02:52 +00002751 if (ShiftSize > Op2Size)
2752 Op2 = DAG.getNode(ISD::ZERO_EXTEND, DL, ShiftTy, Op2);
Owen Anderson95771af2011-02-25 21:41:48 +00002753
Dan Gohman57fc82d2009-04-09 03:51:29 +00002754 // If the operand is larger than the shift count type but the shift
2755 // count type has enough bits to represent any shift value, truncate
2756 // it now. This is a common case and it exposes the truncate to
2757 // optimization early.
Chris Lattnerd3027732011-02-13 09:02:52 +00002758 else if (ShiftSize >= Log2_32_Ceil(Op2.getValueType().getSizeInBits()))
2759 Op2 = DAG.getNode(ISD::TRUNCATE, DL, ShiftTy, Op2);
2760 // Otherwise we'll need to temporarily settle for some other convenient
Chris Lattnere0751182011-02-13 19:09:16 +00002761 // type. Type legalization will make adjustments once the shiftee is split.
Chris Lattnerd3027732011-02-13 09:02:52 +00002762 else
Chris Lattnere0751182011-02-13 19:09:16 +00002763 Op2 = DAG.getZExtOrTrunc(Op2, DL, MVT::i32);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002764 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002765
Andrew Trickac6d9be2013-05-25 02:42:55 +00002766 setValue(&I, DAG.getNode(Opcode, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002767 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002768}
2769
Benjamin Kramer9c640302011-07-08 10:31:30 +00002770void SelectionDAGBuilder::visitSDiv(const User &I) {
Benjamin Kramer9c640302011-07-08 10:31:30 +00002771 SDValue Op1 = getValue(I.getOperand(0));
2772 SDValue Op2 = getValue(I.getOperand(1));
2773
2774 // Turn exact SDivs into multiplications.
2775 // FIXME: This should be in DAGCombiner, but it doesn't have access to the
2776 // exact bit.
Benjamin Kramer3492a4a2011-07-08 12:08:24 +00002777 if (isa<BinaryOperator>(&I) && cast<BinaryOperator>(&I)->isExact() &&
2778 !isa<ConstantSDNode>(Op1) &&
Benjamin Kramer9c640302011-07-08 10:31:30 +00002779 isa<ConstantSDNode>(Op2) && !cast<ConstantSDNode>(Op2)->isNullValue())
Bill Wendlingba54bca2013-06-19 21:36:55 +00002780 setValue(&I, TM.getTargetLowering()->BuildExactSDIV(Op1, Op2,
2781 getCurSDLoc(), DAG));
Benjamin Kramer9c640302011-07-08 10:31:30 +00002782 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00002783 setValue(&I, DAG.getNode(ISD::SDIV, getCurSDLoc(), Op1.getValueType(),
Benjamin Kramer9c640302011-07-08 10:31:30 +00002784 Op1, Op2));
2785}
2786
Dan Gohman46510a72010-04-15 01:51:59 +00002787void SelectionDAGBuilder::visitICmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002788 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002789 if (const ICmpInst *IC = dyn_cast<ICmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002790 predicate = IC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002791 else if (const ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002792 predicate = ICmpInst::Predicate(IC->getPredicate());
2793 SDValue Op1 = getValue(I.getOperand(0));
2794 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002795 ISD::CondCode Opcode = getICmpCondCode(predicate);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002796
Bill Wendlingba54bca2013-06-19 21:36:55 +00002797 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002798 setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Opcode));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002799}
2800
Dan Gohman46510a72010-04-15 01:51:59 +00002801void SelectionDAGBuilder::visitFCmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002802 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002803 if (const FCmpInst *FC = dyn_cast<FCmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002804 predicate = FC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002805 else if (const ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002806 predicate = FCmpInst::Predicate(FC->getPredicate());
2807 SDValue Op1 = getValue(I.getOperand(0));
2808 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002809 ISD::CondCode Condition = getFCmpCondCode(predicate);
Nick Lewycky8a8d4792011-12-02 22:16:29 +00002810 if (TM.Options.NoNaNsFPMath)
2811 Condition = getFCmpCodeWithoutNaN(Condition);
Bill Wendlingba54bca2013-06-19 21:36:55 +00002812 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002813 setValue(&I, DAG.getSetCC(getCurSDLoc(), DestVT, Op1, Op2, Condition));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002814}
2815
Dan Gohman46510a72010-04-15 01:51:59 +00002816void SelectionDAGBuilder::visitSelect(const User &I) {
Owen Andersone50ed302009-08-10 22:56:29 +00002817 SmallVector<EVT, 4> ValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00002818 ComputeValueVTs(*TM.getTargetLowering(), I.getType(), ValueVTs);
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002819 unsigned NumValues = ValueVTs.size();
Bill Wendling49fcff82009-12-21 22:30:11 +00002820 if (NumValues == 0) return;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002821
Bill Wendling49fcff82009-12-21 22:30:11 +00002822 SmallVector<SDValue, 4> Values(NumValues);
2823 SDValue Cond = getValue(I.getOperand(0));
2824 SDValue TrueVal = getValue(I.getOperand(1));
2825 SDValue FalseVal = getValue(I.getOperand(2));
Duncan Sands28b77e92011-09-06 19:07:46 +00002826 ISD::NodeType OpCode = Cond.getValueType().isVector() ?
2827 ISD::VSELECT : ISD::SELECT;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002828
Bill Wendling4533cac2010-01-28 21:51:40 +00002829 for (unsigned i = 0; i != NumValues; ++i)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002830 Values[i] = DAG.getNode(OpCode, getCurSDLoc(),
Duncan Sands28b77e92011-09-06 19:07:46 +00002831 TrueVal.getNode()->getValueType(TrueVal.getResNo()+i),
Chris Lattnerb3e87b22010-03-12 07:15:36 +00002832 Cond,
Bill Wendling49fcff82009-12-21 22:30:11 +00002833 SDValue(TrueVal.getNode(),
2834 TrueVal.getResNo() + i),
2835 SDValue(FalseVal.getNode(),
2836 FalseVal.getResNo() + i));
2837
Andrew Trickac6d9be2013-05-25 02:42:55 +00002838 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002839 DAG.getVTList(&ValueVTs[0], NumValues),
2840 &Values[0], NumValues));
Bill Wendling49fcff82009-12-21 22:30:11 +00002841}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002842
Dan Gohman46510a72010-04-15 01:51:59 +00002843void SelectionDAGBuilder::visitTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002844 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2845 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002846 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002847 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002848}
2849
Dan Gohman46510a72010-04-15 01:51:59 +00002850void SelectionDAGBuilder::visitZExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002851 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2852 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2853 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002854 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002855 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002856}
2857
Dan Gohman46510a72010-04-15 01:51:59 +00002858void SelectionDAGBuilder::visitSExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002859 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2860 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2861 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002862 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002863 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002864}
2865
Dan Gohman46510a72010-04-15 01:51:59 +00002866void SelectionDAGBuilder::visitFPTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002867 // FPTrunc is never a no-op cast, no need to check
2868 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002869 const TargetLowering *TLI = TM.getTargetLowering();
2870 EVT DestVT = TLI->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002871 setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurSDLoc(),
Pete Cooperf57e1c22012-01-17 01:54:07 +00002872 DestVT, N,
Bill Wendlingba54bca2013-06-19 21:36:55 +00002873 DAG.getTargetConstant(0, TLI->getPointerTy())));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002874}
2875
Stephen Lin09f8ca32013-07-06 21:44:25 +00002876void SelectionDAGBuilder::visitFPExt(const User &I) {
Hal Finkel46bb70c2011-10-18 03:51:57 +00002877 // FPExt is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002878 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002879 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002880 setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002881}
2882
Dan Gohman46510a72010-04-15 01:51:59 +00002883void SelectionDAGBuilder::visitFPToUI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002884 // FPToUI is never a no-op cast, no need to check
2885 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002886 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002887 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002888}
2889
Dan Gohman46510a72010-04-15 01:51:59 +00002890void SelectionDAGBuilder::visitFPToSI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002891 // FPToSI is never a no-op cast, no need to check
2892 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002893 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002894 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002895}
2896
Dan Gohman46510a72010-04-15 01:51:59 +00002897void SelectionDAGBuilder::visitUIToFP(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002898 // UIToFP is never a no-op cast, no need to check
2899 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002900 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002901 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002902}
2903
Stephen Lin09f8ca32013-07-06 21:44:25 +00002904void SelectionDAGBuilder::visitSIToFP(const User &I) {
Bill Wendling181b6272008-10-19 20:34:04 +00002905 // SIToFP is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002906 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002907 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002908 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurSDLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002909}
2910
Dan Gohman46510a72010-04-15 01:51:59 +00002911void SelectionDAGBuilder::visitPtrToInt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002912 // What to do depends on the size of the integer and the size of the pointer.
2913 // We can either truncate, zero extend, or no-op, accordingly.
2914 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002915 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002916 setValue(&I, DAG.getZExtOrTrunc(N, getCurSDLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002917}
2918
Dan Gohman46510a72010-04-15 01:51:59 +00002919void SelectionDAGBuilder::visitIntToPtr(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002920 // What to do depends on the size of the integer and the size of the pointer.
2921 // We can either truncate, zero extend, or no-op, accordingly.
2922 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002923 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002924 setValue(&I, DAG.getZExtOrTrunc(N, getCurSDLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002925}
2926
Dan Gohman46510a72010-04-15 01:51:59 +00002927void SelectionDAGBuilder::visitBitCast(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002928 SDValue N = getValue(I.getOperand(0));
Bill Wendlingba54bca2013-06-19 21:36:55 +00002929 EVT DestVT = TM.getTargetLowering()->getValueType(I.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002930
Bill Wendling49fcff82009-12-21 22:30:11 +00002931 // BitCast assures us that source and destination are the same size so this is
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002932 // either a BITCAST or a no-op.
Bill Wendling4533cac2010-01-28 21:51:40 +00002933 if (DestVT != N.getValueType())
Andrew Trickac6d9be2013-05-25 02:42:55 +00002934 setValue(&I, DAG.getNode(ISD::BITCAST, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00002935 DestVT, N)); // convert types.
2936 else
Bill Wendling49fcff82009-12-21 22:30:11 +00002937 setValue(&I, N); // noop cast.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002938}
2939
Dan Gohman46510a72010-04-15 01:51:59 +00002940void SelectionDAGBuilder::visitInsertElement(const User &I) {
Tom Stellard425b76c2013-08-05 22:22:01 +00002941 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002942 SDValue InVec = getValue(I.getOperand(0));
2943 SDValue InVal = getValue(I.getOperand(1));
Tom Stellard425b76c2013-08-05 22:22:01 +00002944 SDValue InIdx = DAG.getSExtOrTrunc(getValue(I.getOperand(2)),
2945 getCurSDLoc(), TLI.getVectorIdxTy());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002946 setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurSDLoc(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00002947 TM.getTargetLowering()->getValueType(I.getType()),
Bill Wendling4533cac2010-01-28 21:51:40 +00002948 InVec, InVal, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002949}
2950
Dan Gohman46510a72010-04-15 01:51:59 +00002951void SelectionDAGBuilder::visitExtractElement(const User &I) {
Tom Stellard425b76c2013-08-05 22:22:01 +00002952 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002953 SDValue InVec = getValue(I.getOperand(0));
Tom Stellard425b76c2013-08-05 22:22:01 +00002954 SDValue InIdx = DAG.getSExtOrTrunc(getValue(I.getOperand(1)),
2955 getCurSDLoc(), TLI.getVectorIdxTy());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002956 setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurSDLoc(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00002957 TM.getTargetLowering()->getValueType(I.getType()),
2958 InVec, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002959}
2960
Craig Topper51578342012-01-04 09:23:09 +00002961// Utility for visitShuffleVector - Return true if every element in Mask,
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00002962// beginning from position Pos and ending in Pos+Size, falls within the
Craig Topper51578342012-01-04 09:23:09 +00002963// specified sequential range [L, L+Pos). or is undef.
2964static bool isSequentialInRange(const SmallVectorImpl<int> &Mask,
Craig Topper23de31b2012-04-11 03:06:35 +00002965 unsigned Pos, unsigned Size, int Low) {
2966 for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
Craig Topper51578342012-01-04 09:23:09 +00002967 if (Mask[i] >= 0 && Mask[i] != Low)
Nate Begeman9008ca62009-04-27 18:41:29 +00002968 return false;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002969 return true;
2970}
2971
Dan Gohman46510a72010-04-15 01:51:59 +00002972void SelectionDAGBuilder::visitShuffleVector(const User &I) {
Mon P Wang230e4fa2008-11-21 04:25:21 +00002973 SDValue Src1 = getValue(I.getOperand(0));
2974 SDValue Src2 = getValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002975
Chris Lattner56243b82012-01-26 02:51:13 +00002976 SmallVector<int, 8> Mask;
2977 ShuffleVectorInst::getShuffleMask(cast<Constant>(I.getOperand(2)), Mask);
2978 unsigned MaskNumElts = Mask.size();
Bill Wendlingba54bca2013-06-19 21:36:55 +00002979
2980 const TargetLowering *TLI = TM.getTargetLowering();
2981 EVT VT = TLI->getValueType(I.getType());
Owen Andersone50ed302009-08-10 22:56:29 +00002982 EVT SrcVT = Src1.getValueType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00002983 unsigned SrcNumElts = SrcVT.getVectorNumElements();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002984
Mon P Wangc7849c22008-11-16 05:06:27 +00002985 if (SrcNumElts == MaskNumElts) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002986 setValue(&I, DAG.getVectorShuffle(VT, getCurSDLoc(), Src1, Src2,
Bill Wendling4533cac2010-01-28 21:51:40 +00002987 &Mask[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002988 return;
2989 }
2990
2991 // Normalize the shuffle vector since mask and vector length don't match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002992 if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) {
2993 // Mask is longer than the source vectors and is a multiple of the source
2994 // vectors. We can use concatenate vector to make the mask and vectors
Mon P Wang230e4fa2008-11-21 04:25:21 +00002995 // lengths match.
Craig Topper51578342012-01-04 09:23:09 +00002996 if (SrcNumElts*2 == MaskNumElts) {
2997 // First check for Src1 in low and Src2 in high
2998 if (isSequentialInRange(Mask, 0, SrcNumElts, 0) &&
2999 isSequentialInRange(Mask, SrcNumElts, SrcNumElts, SrcNumElts)) {
3000 // The shuffle is concatenating two vectors together.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003001 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurSDLoc(),
Craig Topper51578342012-01-04 09:23:09 +00003002 VT, Src1, Src2));
3003 return;
3004 }
3005 // Then check for Src2 in low and Src1 in high
3006 if (isSequentialInRange(Mask, 0, SrcNumElts, SrcNumElts) &&
3007 isSequentialInRange(Mask, SrcNumElts, SrcNumElts, 0)) {
3008 // The shuffle is concatenating two vectors together.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003009 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurSDLoc(),
Craig Topper51578342012-01-04 09:23:09 +00003010 VT, Src2, Src1));
3011 return;
3012 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00003013 }
3014
Mon P Wangc7849c22008-11-16 05:06:27 +00003015 // Pad both vectors with undefs to make them the same length as the mask.
3016 unsigned NumConcat = MaskNumElts / SrcNumElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00003017 bool Src1U = Src1.getOpcode() == ISD::UNDEF;
3018 bool Src2U = Src2.getOpcode() == ISD::UNDEF;
Dale Johannesene8d72302009-02-06 23:05:02 +00003019 SDValue UndefVal = DAG.getUNDEF(SrcVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00003020
Nate Begeman9008ca62009-04-27 18:41:29 +00003021 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
3022 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
Mon P Wang230e4fa2008-11-21 04:25:21 +00003023 MOps1[0] = Src1;
3024 MOps2[0] = Src2;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003025
3026 Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003027 getCurSDLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003028 &MOps1[0], NumConcat);
3029 Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003030 getCurSDLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003031 &MOps2[0], NumConcat);
Mon P Wang230e4fa2008-11-21 04:25:21 +00003032
Mon P Wangaeb06d22008-11-10 04:46:22 +00003033 // Readjust mask for new input vector length.
Nate Begeman9008ca62009-04-27 18:41:29 +00003034 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003035 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003036 int Idx = Mask[i];
Craig Topper23de31b2012-04-11 03:06:35 +00003037 if (Idx >= (int)SrcNumElts)
3038 Idx -= SrcNumElts - MaskNumElts;
3039 MappedOps.push_back(Idx);
Mon P Wangaeb06d22008-11-10 04:46:22 +00003040 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003041
Andrew Trickac6d9be2013-05-25 02:42:55 +00003042 setValue(&I, DAG.getVectorShuffle(VT, getCurSDLoc(), Src1, Src2,
Bill Wendling4533cac2010-01-28 21:51:40 +00003043 &MappedOps[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00003044 return;
3045 }
3046
Mon P Wangc7849c22008-11-16 05:06:27 +00003047 if (SrcNumElts > MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00003048 // Analyze the access pattern of the vector to see if we can extract
3049 // two subvectors and do the shuffle. The analysis is done by calculating
3050 // the range of elements the mask access on both vectors.
Craig Topper10612dc2012-04-08 23:15:04 +00003051 int MinRange[2] = { static_cast<int>(SrcNumElts),
3052 static_cast<int>(SrcNumElts)};
Mon P Wangc7849c22008-11-16 05:06:27 +00003053 int MaxRange[2] = {-1, -1};
3054
Nate Begeman5a5ca152009-04-29 05:20:52 +00003055 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003056 int Idx = Mask[i];
Craig Topper10612dc2012-04-08 23:15:04 +00003057 unsigned Input = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003058 if (Idx < 0)
3059 continue;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003060
Nate Begeman5a5ca152009-04-29 05:20:52 +00003061 if (Idx >= (int)SrcNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003062 Input = 1;
3063 Idx -= SrcNumElts;
Mon P Wangaeb06d22008-11-10 04:46:22 +00003064 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003065 if (Idx > MaxRange[Input])
3066 MaxRange[Input] = Idx;
3067 if (Idx < MinRange[Input])
3068 MinRange[Input] = Idx;
Mon P Wangaeb06d22008-11-10 04:46:22 +00003069 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00003070
Mon P Wangc7849c22008-11-16 05:06:27 +00003071 // Check if the access is smaller than the vector size and can we find
3072 // a reasonable extract index.
Craig Topper10612dc2012-04-08 23:15:04 +00003073 int RangeUse[2] = { -1, -1 }; // 0 = Unused, 1 = Extract, -1 = Can not
3074 // Extract.
Mon P Wangc7849c22008-11-16 05:06:27 +00003075 int StartIdx[2]; // StartIdx to extract from
Craig Topper10612dc2012-04-08 23:15:04 +00003076 for (unsigned Input = 0; Input < 2; ++Input) {
3077 if (MinRange[Input] >= (int)SrcNumElts && MaxRange[Input] < 0) {
Mon P Wangc7849c22008-11-16 05:06:27 +00003078 RangeUse[Input] = 0; // Unused
3079 StartIdx[Input] = 0;
Craig Topperf873dde2012-04-08 17:53:33 +00003080 continue;
Mon P Wang230e4fa2008-11-21 04:25:21 +00003081 }
Craig Topperf873dde2012-04-08 17:53:33 +00003082
3083 // Find a good start index that is a multiple of the mask length. Then
3084 // see if the rest of the elements are in range.
3085 StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts;
3086 if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts &&
3087 StartIdx[Input] + MaskNumElts <= SrcNumElts)
3088 RangeUse[Input] = 1; // Extract from a multiple of the mask length.
Mon P Wangc7849c22008-11-16 05:06:27 +00003089 }
3090
Bill Wendling636e2582009-08-21 18:16:06 +00003091 if (RangeUse[0] == 0 && RangeUse[1] == 0) {
Bill Wendling4533cac2010-01-28 21:51:40 +00003092 setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
Mon P Wangc7849c22008-11-16 05:06:27 +00003093 return;
3094 }
Craig Topper10612dc2012-04-08 23:15:04 +00003095 if (RangeUse[0] >= 0 && RangeUse[1] >= 0) {
Mon P Wangc7849c22008-11-16 05:06:27 +00003096 // Extract appropriate subvector and generate a vector shuffle
Craig Topper10612dc2012-04-08 23:15:04 +00003097 for (unsigned Input = 0; Input < 2; ++Input) {
Bill Wendling87710f02009-12-21 23:47:40 +00003098 SDValue &Src = Input == 0 ? Src1 : Src2;
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003099 if (RangeUse[Input] == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00003100 Src = DAG.getUNDEF(VT);
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003101 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00003102 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurSDLoc(), VT,
Tom Stellard425b76c2013-08-05 22:22:01 +00003103 Src, DAG.getConstant(StartIdx[Input],
3104 TLI->getVectorIdxTy()));
Mon P Wangaeb06d22008-11-10 04:46:22 +00003105 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003106
Mon P Wangc7849c22008-11-16 05:06:27 +00003107 // Calculate new mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00003108 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003109 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003110 int Idx = Mask[i];
Craig Topper23de31b2012-04-11 03:06:35 +00003111 if (Idx >= 0) {
3112 if (Idx < (int)SrcNumElts)
3113 Idx -= StartIdx[0];
3114 else
3115 Idx -= SrcNumElts + StartIdx[1] - MaskNumElts;
3116 }
3117 MappedOps.push_back(Idx);
Mon P Wangc7849c22008-11-16 05:06:27 +00003118 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003119
Andrew Trickac6d9be2013-05-25 02:42:55 +00003120 setValue(&I, DAG.getVectorShuffle(VT, getCurSDLoc(), Src1, Src2,
Bill Wendling4533cac2010-01-28 21:51:40 +00003121 &MappedOps[0]));
Mon P Wangc7849c22008-11-16 05:06:27 +00003122 return;
Mon P Wangaeb06d22008-11-10 04:46:22 +00003123 }
3124 }
3125
Mon P Wangc7849c22008-11-16 05:06:27 +00003126 // We can't use either concat vectors or extract subvectors so fall back to
3127 // replacing the shuffle with extract and build vector.
3128 // to insert and build vector.
Owen Andersone50ed302009-08-10 22:56:29 +00003129 EVT EltVT = VT.getVectorElementType();
Tom Stellard425b76c2013-08-05 22:22:01 +00003130 EVT IdxVT = TLI->getVectorIdxTy();
Mon P Wangaeb06d22008-11-10 04:46:22 +00003131 SmallVector<SDValue,8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003132 for (unsigned i = 0; i != MaskNumElts; ++i) {
Craig Topper23de31b2012-04-11 03:06:35 +00003133 int Idx = Mask[i];
3134 SDValue Res;
3135
3136 if (Idx < 0) {
3137 Res = DAG.getUNDEF(EltVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00003138 } else {
Craig Topper23de31b2012-04-11 03:06:35 +00003139 SDValue &Src = Idx < (int)SrcNumElts ? Src1 : Src2;
3140 if (Idx >= (int)SrcNumElts) Idx -= SrcNumElts;
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003141
Andrew Trickac6d9be2013-05-25 02:42:55 +00003142 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurSDLoc(),
Tom Stellard425b76c2013-08-05 22:22:01 +00003143 EltVT, Src, DAG.getConstant(Idx, IdxVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00003144 }
Craig Topper23de31b2012-04-11 03:06:35 +00003145
3146 Ops.push_back(Res);
Mon P Wangaeb06d22008-11-10 04:46:22 +00003147 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00003148
Andrew Trickac6d9be2013-05-25 02:42:55 +00003149 setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00003150 VT, &Ops[0], Ops.size()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003151}
3152
Dan Gohman46510a72010-04-15 01:51:59 +00003153void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003154 const Value *Op0 = I.getOperand(0);
3155 const Value *Op1 = I.getOperand(1);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003156 Type *AggTy = I.getType();
3157 Type *ValTy = Op1->getType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003158 bool IntoUndef = isa<UndefValue>(Op0);
3159 bool FromUndef = isa<UndefValue>(Op1);
3160
Jay Foadfc6d3a42011-07-13 10:26:04 +00003161 unsigned LinearIndex = ComputeLinearIndex(AggTy, I.getIndices());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003162
Bill Wendlingba54bca2013-06-19 21:36:55 +00003163 const TargetLowering *TLI = TM.getTargetLowering();
Owen Andersone50ed302009-08-10 22:56:29 +00003164 SmallVector<EVT, 4> AggValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003165 ComputeValueVTs(*TLI, AggTy, AggValueVTs);
Owen Andersone50ed302009-08-10 22:56:29 +00003166 SmallVector<EVT, 4> ValValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003167 ComputeValueVTs(*TLI, ValTy, ValValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003168
3169 unsigned NumAggValues = AggValueVTs.size();
3170 unsigned NumValValues = ValValueVTs.size();
3171 SmallVector<SDValue, 4> Values(NumAggValues);
3172
3173 SDValue Agg = getValue(Op0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003174 unsigned i = 0;
3175 // Copy the beginning value(s) from the original aggregate.
3176 for (; i != LinearIndex; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00003177 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003178 SDValue(Agg.getNode(), Agg.getResNo() + i);
3179 // Copy values from the inserted value(s).
Rafael Espindola3fa82832011-05-13 15:18:06 +00003180 if (NumValValues) {
3181 SDValue Val = getValue(Op1);
3182 for (; i != LinearIndex + NumValValues; ++i)
3183 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
3184 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
3185 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003186 // Copy remaining value(s) from the original aggregate.
3187 for (; i != NumAggValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00003188 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003189 SDValue(Agg.getNode(), Agg.getResNo() + i);
3190
Andrew Trickac6d9be2013-05-25 02:42:55 +00003191 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00003192 DAG.getVTList(&AggValueVTs[0], NumAggValues),
3193 &Values[0], NumAggValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003194}
3195
Dan Gohman46510a72010-04-15 01:51:59 +00003196void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003197 const Value *Op0 = I.getOperand(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003198 Type *AggTy = Op0->getType();
3199 Type *ValTy = I.getType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003200 bool OutOfUndef = isa<UndefValue>(Op0);
3201
Jay Foadfc6d3a42011-07-13 10:26:04 +00003202 unsigned LinearIndex = ComputeLinearIndex(AggTy, I.getIndices());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003203
Bill Wendlingba54bca2013-06-19 21:36:55 +00003204 const TargetLowering *TLI = TM.getTargetLowering();
Owen Andersone50ed302009-08-10 22:56:29 +00003205 SmallVector<EVT, 4> ValValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003206 ComputeValueVTs(*TLI, ValTy, ValValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003207
3208 unsigned NumValValues = ValValueVTs.size();
Rafael Espindola3fa82832011-05-13 15:18:06 +00003209
3210 // Ignore a extractvalue that produces an empty object
3211 if (!NumValValues) {
3212 setValue(&I, DAG.getUNDEF(MVT(MVT::Other)));
3213 return;
3214 }
3215
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003216 SmallVector<SDValue, 4> Values(NumValValues);
3217
3218 SDValue Agg = getValue(Op0);
3219 // Copy out the selected value(s).
3220 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
3221 Values[i - LinearIndex] =
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00003222 OutOfUndef ?
Dale Johannesene8d72302009-02-06 23:05:02 +00003223 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00003224 SDValue(Agg.getNode(), Agg.getResNo() + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003225
Andrew Trickac6d9be2013-05-25 02:42:55 +00003226 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00003227 DAG.getVTList(&ValValueVTs[0], NumValValues),
3228 &Values[0], NumValValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003229}
3230
Dan Gohman46510a72010-04-15 01:51:59 +00003231void SelectionDAGBuilder::visitGetElementPtr(const User &I) {
Matt Arsenaultff718122013-10-21 20:03:54 +00003232 Value *Op0 = I.getOperand(0);
Nadav Rotem1c239202012-02-28 14:13:19 +00003233 // Note that the pointer operand may be a vector of pointers. Take the scalar
3234 // element which holds a pointer.
Matt Arsenaultff718122013-10-21 20:03:54 +00003235 Type *Ty = Op0->getType()->getScalarType();
3236 unsigned AS = Ty->getPointerAddressSpace();
3237 SDValue N = getValue(Op0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003238
Dan Gohman46510a72010-04-15 01:51:59 +00003239 for (GetElementPtrInst::const_op_iterator OI = I.op_begin()+1, E = I.op_end();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003240 OI != E; ++OI) {
Dan Gohman46510a72010-04-15 01:51:59 +00003241 const Value *Idx = *OI;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003242 if (StructType *StTy = dyn_cast<StructType>(Ty)) {
Duncan Sandsb2df01a2012-11-13 13:01:58 +00003243 unsigned Field = cast<Constant>(Idx)->getUniqueInteger().getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003244 if (Field) {
3245 // N = N + Offset
3246 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003247 N = DAG.getNode(ISD::ADD, getCurSDLoc(), N.getValueType(), N,
Duncan Sandsb2df01a2012-11-13 13:01:58 +00003248 DAG.getConstant(Offset, N.getValueType()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003249 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00003250
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003251 Ty = StTy->getElementType(Field);
3252 } else {
3253 Ty = cast<SequentialType>(Ty)->getElementType();
3254
3255 // If this is a constant subscript, handle it quickly.
Bill Wendlingba54bca2013-06-19 21:36:55 +00003256 const TargetLowering *TLI = TM.getTargetLowering();
Dan Gohman46510a72010-04-15 01:51:59 +00003257 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Dan Gohmane368b462010-06-18 14:22:04 +00003258 if (CI->isZero()) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003259 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +00003260 TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Evan Cheng65b52df2009-02-09 21:01:06 +00003261 SDValue OffsVal;
Tom Stellardda25cd32013-08-26 15:05:36 +00003262 EVT PTy = TLI->getPointerTy(AS);
Owen Anderson77547be2009-08-10 18:56:59 +00003263 unsigned PtrBits = PTy.getSizeInBits();
Bill Wendlinge1a90422009-12-21 23:10:19 +00003264 if (PtrBits < 64)
Tom Stellardda25cd32013-08-26 15:05:36 +00003265 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), PTy,
Owen Anderson825b72b2009-08-11 20:47:22 +00003266 DAG.getConstant(Offs, MVT::i64));
Bill Wendlinge1a90422009-12-21 23:10:19 +00003267 else
Tom Stellardda25cd32013-08-26 15:05:36 +00003268 OffsVal = DAG.getConstant(Offs, PTy);
Bill Wendlinge1a90422009-12-21 23:10:19 +00003269
Andrew Trickac6d9be2013-05-25 02:42:55 +00003270 N = DAG.getNode(ISD::ADD, getCurSDLoc(), N.getValueType(), N,
Evan Chengb1032a82009-02-09 20:54:38 +00003271 OffsVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003272 continue;
3273 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003274
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003275 // N = N + Idx * ElementSize;
Tom Stellardda25cd32013-08-26 15:05:36 +00003276 APInt ElementSize = APInt(TLI->getPointerSizeInBits(AS),
Dan Gohman7abbd042009-10-23 17:57:43 +00003277 TD->getTypeAllocSize(Ty));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003278 SDValue IdxN = getValue(Idx);
3279
3280 // If the index is smaller or larger than intptr_t, truncate or extend
3281 // it.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003282 IdxN = DAG.getSExtOrTrunc(IdxN, getCurSDLoc(), N.getValueType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003283
3284 // If this is a multiply by a power of two, turn it into a shl
3285 // immediately. This is a very common case.
3286 if (ElementSize != 1) {
Dan Gohman7abbd042009-10-23 17:57:43 +00003287 if (ElementSize.isPowerOf2()) {
3288 unsigned Amt = ElementSize.logBase2();
Andrew Trickac6d9be2013-05-25 02:42:55 +00003289 IdxN = DAG.getNode(ISD::SHL, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003290 N.getValueType(), IdxN,
Nadav Rotem16087692011-12-05 06:29:09 +00003291 DAG.getConstant(Amt, IdxN.getValueType()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003292 } else {
Duncan Sandsb2df01a2012-11-13 13:01:58 +00003293 SDValue Scale = DAG.getConstant(ElementSize, IdxN.getValueType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003294 IdxN = DAG.getNode(ISD::MUL, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003295 N.getValueType(), IdxN, Scale);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003296 }
3297 }
3298
Andrew Trickac6d9be2013-05-25 02:42:55 +00003299 N = DAG.getNode(ISD::ADD, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003300 N.getValueType(), N, IdxN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003301 }
3302 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00003303
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003304 setValue(&I, N);
3305}
3306
Dan Gohman46510a72010-04-15 01:51:59 +00003307void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003308 // If this is a fixed sized alloca in the entry block of the function,
3309 // allocate it statically on the stack.
3310 if (FuncInfo.StaticAllocaMap.count(&I))
3311 return; // getValue will auto-populate this.
3312
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003313 Type *Ty = I.getAllocatedType();
Bill Wendlingba54bca2013-06-19 21:36:55 +00003314 const TargetLowering *TLI = TM.getTargetLowering();
3315 uint64_t TySize = TLI->getDataLayout()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003316 unsigned Align =
Bill Wendlingba54bca2013-06-19 21:36:55 +00003317 std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003318 I.getAlignment());
3319
3320 SDValue AllocSize = getValue(I.getArraySize());
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003321
Bill Wendlingba54bca2013-06-19 21:36:55 +00003322 EVT IntPtr = TLI->getPointerTy();
Dan Gohmanf75a7d32010-05-28 01:14:11 +00003323 if (AllocSize.getValueType() != IntPtr)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003324 AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurSDLoc(), IntPtr);
Dan Gohmanf75a7d32010-05-28 01:14:11 +00003325
Andrew Trickac6d9be2013-05-25 02:42:55 +00003326 AllocSize = DAG.getNode(ISD::MUL, getCurSDLoc(), IntPtr,
Dan Gohmanf75a7d32010-05-28 01:14:11 +00003327 AllocSize,
3328 DAG.getConstant(TySize, IntPtr));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003329
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003330 // Handle alignment. If the requested alignment is less than or equal to
3331 // the stack alignment, ignore it. If the size is greater than or equal to
3332 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00003333 unsigned StackAlign = TM.getFrameLowering()->getStackAlignment();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003334 if (Align <= StackAlign)
3335 Align = 0;
3336
3337 // Round the size of the allocation up to the stack alignment size
3338 // by add SA-1 to the size.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003339 AllocSize = DAG.getNode(ISD::ADD, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003340 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003341 DAG.getIntPtrConstant(StackAlign-1));
Bill Wendling856ff412009-12-22 00:12:37 +00003342
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003343 // Mask out the low bits for alignment purposes.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003344 AllocSize = DAG.getNode(ISD::AND, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003345 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003346 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
3347
3348 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Owen Anderson825b72b2009-08-11 20:47:22 +00003349 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003350 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurSDLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003351 VTs, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003352 setValue(&I, DSA);
3353 DAG.setRoot(DSA.getValue(1));
Bill Wendling856ff412009-12-22 00:12:37 +00003354
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003355 // Inform the Frame Information that we have just allocated a variable-sized
3356 // object.
Bob Wilson8f637ad2013-02-08 20:35:15 +00003357 FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject(Align ? Align : 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003358}
3359
Dan Gohman46510a72010-04-15 01:51:59 +00003360void SelectionDAGBuilder::visitLoad(const LoadInst &I) {
Eli Friedman327236c2011-08-24 20:50:09 +00003361 if (I.isAtomic())
3362 return visitAtomicLoad(I);
3363
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003364 const Value *SV = I.getOperand(0);
3365 SDValue Ptr = getValue(SV);
3366
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003367 Type *Ty = I.getType();
David Greene1e559442010-02-15 17:00:31 +00003368
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003369 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00003370 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Pete Cooperd752e0f2011-11-08 18:42:53 +00003371 bool isInvariant = I.getMetadata("invariant.load") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003372 unsigned Alignment = I.getAlignment();
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00003373 const MDNode *TBAAInfo = I.getMetadata(LLVMContext::MD_tbaa);
Rafael Espindola95d594c2012-03-31 18:14:00 +00003374 const MDNode *Ranges = I.getMetadata(LLVMContext::MD_range);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003375
Owen Andersone50ed302009-08-10 22:56:29 +00003376 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003377 SmallVector<uint64_t, 4> Offsets;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003378 ComputeValueVTs(*TM.getTargetLowering(), Ty, ValueVTs, &Offsets);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003379 unsigned NumValues = ValueVTs.size();
3380 if (NumValues == 0)
3381 return;
3382
3383 SDValue Root;
3384 bool ConstantMemory = false;
Andrew Trickde91f3c2010-11-12 17:50:46 +00003385 if (I.isVolatile() || NumValues > MaxParallelChains)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003386 // Serialize volatile loads with other side effects.
3387 Root = getRoot();
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00003388 else if (AA->pointsToConstantMemory(
3389 AliasAnalysis::Location(SV, AA->getTypeStoreSize(Ty), TBAAInfo))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003390 // Do not serialize (non-volatile) loads of constant memory with anything.
3391 Root = DAG.getEntryNode();
3392 ConstantMemory = true;
3393 } else {
3394 // Do not serialize non-volatile loads against each other.
3395 Root = DAG.getRoot();
3396 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003397
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003398 SmallVector<SDValue, 4> Values(NumValues);
Andrew Trickde91f3c2010-11-12 17:50:46 +00003399 SmallVector<SDValue, 4> Chains(std::min(unsigned(MaxParallelChains),
3400 NumValues));
Owen Andersone50ed302009-08-10 22:56:29 +00003401 EVT PtrVT = Ptr.getValueType();
Andrew Trickde91f3c2010-11-12 17:50:46 +00003402 unsigned ChainI = 0;
3403 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
3404 // Serializing loads here may result in excessive register pressure, and
3405 // TokenFactor places arbitrary choke points on the scheduler. SD scheduling
3406 // could recover a bit by hoisting nodes upward in the chain by recognizing
3407 // they are side-effect free or do not alias. The optimizer should really
3408 // avoid this case by converting large object/array copies to llvm.memcpy
3409 // (MaxParallelChains should always remain as failsafe).
3410 if (ChainI == MaxParallelChains) {
3411 assert(PendingLoads.empty() && "PendingLoads must be serialized first");
Andrew Trickac6d9be2013-05-25 02:42:55 +00003412 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003413 MVT::Other, &Chains[0], ChainI);
3414 Root = Chain;
3415 ChainI = 0;
3416 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00003417 SDValue A = DAG.getNode(ISD::ADD, getCurSDLoc(),
Bill Wendling856ff412009-12-22 00:12:37 +00003418 PtrVT, Ptr,
3419 DAG.getConstant(Offsets[i], PtrVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003420 SDValue L = DAG.getLoad(ValueVTs[i], getCurSDLoc(), Root,
Michael J. Spencere70c5262010-10-16 08:25:21 +00003421 A, MachinePointerInfo(SV, Offsets[i]), isVolatile,
Rafael Espindola95d594c2012-03-31 18:14:00 +00003422 isNonTemporal, isInvariant, Alignment, TBAAInfo,
3423 Ranges);
Bill Wendling856ff412009-12-22 00:12:37 +00003424
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003425 Values[i] = L;
Andrew Trickde91f3c2010-11-12 17:50:46 +00003426 Chains[ChainI] = L.getValue(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003427 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003428
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003429 if (!ConstantMemory) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003430 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003431 MVT::Other, &Chains[0], ChainI);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003432 if (isVolatile)
3433 DAG.setRoot(Chain);
3434 else
3435 PendingLoads.push_back(Chain);
3436 }
3437
Andrew Trickac6d9be2013-05-25 02:42:55 +00003438 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00003439 DAG.getVTList(&ValueVTs[0], NumValues),
3440 &Values[0], NumValues));
Bill Wendling856ff412009-12-22 00:12:37 +00003441}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003442
Dan Gohman46510a72010-04-15 01:51:59 +00003443void SelectionDAGBuilder::visitStore(const StoreInst &I) {
Eli Friedman327236c2011-08-24 20:50:09 +00003444 if (I.isAtomic())
3445 return visitAtomicStore(I);
3446
Dan Gohman46510a72010-04-15 01:51:59 +00003447 const Value *SrcV = I.getOperand(0);
3448 const Value *PtrV = I.getOperand(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003449
Owen Andersone50ed302009-08-10 22:56:29 +00003450 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003451 SmallVector<uint64_t, 4> Offsets;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003452 ComputeValueVTs(*TM.getTargetLowering(), SrcV->getType(), ValueVTs, &Offsets);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003453 unsigned NumValues = ValueVTs.size();
3454 if (NumValues == 0)
3455 return;
3456
3457 // Get the lowered operands. Note that we do this after
3458 // checking if NumResults is zero, because with zero results
3459 // the operands won't have values in the map.
3460 SDValue Src = getValue(SrcV);
3461 SDValue Ptr = getValue(PtrV);
3462
3463 SDValue Root = getRoot();
Andrew Trickde91f3c2010-11-12 17:50:46 +00003464 SmallVector<SDValue, 4> Chains(std::min(unsigned(MaxParallelChains),
3465 NumValues));
Owen Andersone50ed302009-08-10 22:56:29 +00003466 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003467 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00003468 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003469 unsigned Alignment = I.getAlignment();
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00003470 const MDNode *TBAAInfo = I.getMetadata(LLVMContext::MD_tbaa);
Bill Wendling856ff412009-12-22 00:12:37 +00003471
Andrew Trickde91f3c2010-11-12 17:50:46 +00003472 unsigned ChainI = 0;
3473 for (unsigned i = 0; i != NumValues; ++i, ++ChainI) {
3474 // See visitLoad comments.
3475 if (ChainI == MaxParallelChains) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003476 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003477 MVT::Other, &Chains[0], ChainI);
3478 Root = Chain;
3479 ChainI = 0;
3480 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00003481 SDValue Add = DAG.getNode(ISD::ADD, getCurSDLoc(), PtrVT, Ptr,
Bill Wendling856ff412009-12-22 00:12:37 +00003482 DAG.getConstant(Offsets[i], PtrVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003483 SDValue St = DAG.getStore(Root, getCurSDLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003484 SDValue(Src.getNode(), Src.getResNo() + i),
3485 Add, MachinePointerInfo(PtrV, Offsets[i]),
3486 isVolatile, isNonTemporal, Alignment, TBAAInfo);
3487 Chains[ChainI] = St;
Bill Wendling856ff412009-12-22 00:12:37 +00003488 }
3489
Andrew Trickac6d9be2013-05-25 02:42:55 +00003490 SDValue StoreNode = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Andrew Trickde91f3c2010-11-12 17:50:46 +00003491 MVT::Other, &Chains[0], ChainI);
Devang Patel7e13efa2010-10-26 22:14:52 +00003492 DAG.setRoot(StoreNode);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003493}
3494
Eli Friedman26689ac2011-08-03 21:06:02 +00003495static SDValue InsertFenceForAtomic(SDValue Chain, AtomicOrdering Order,
Eli Friedman327236c2011-08-24 20:50:09 +00003496 SynchronizationScope Scope,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003497 bool Before, SDLoc dl,
Eli Friedman26689ac2011-08-03 21:06:02 +00003498 SelectionDAG &DAG,
3499 const TargetLowering &TLI) {
3500 // Fence, if necessary
3501 if (Before) {
Eli Friedman069e2ed2011-08-26 02:59:24 +00003502 if (Order == AcquireRelease || Order == SequentiallyConsistent)
Eli Friedman26689ac2011-08-03 21:06:02 +00003503 Order = Release;
3504 else if (Order == Acquire || Order == Monotonic)
3505 return Chain;
3506 } else {
3507 if (Order == AcquireRelease)
3508 Order = Acquire;
3509 else if (Order == Release || Order == Monotonic)
3510 return Chain;
3511 }
3512 SDValue Ops[3];
3513 Ops[0] = Chain;
Eli Friedman327236c2011-08-24 20:50:09 +00003514 Ops[1] = DAG.getConstant(Order, TLI.getPointerTy());
3515 Ops[2] = DAG.getConstant(Scope, TLI.getPointerTy());
Eli Friedman26689ac2011-08-03 21:06:02 +00003516 return DAG.getNode(ISD::ATOMIC_FENCE, dl, MVT::Other, Ops, 3);
3517}
3518
Eli Friedmanff030482011-07-28 21:48:00 +00003519void SelectionDAGBuilder::visitAtomicCmpXchg(const AtomicCmpXchgInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003520 SDLoc dl = getCurSDLoc();
Eli Friedman26689ac2011-08-03 21:06:02 +00003521 AtomicOrdering Order = I.getOrdering();
Eli Friedman327236c2011-08-24 20:50:09 +00003522 SynchronizationScope Scope = I.getSynchScope();
Eli Friedman26689ac2011-08-03 21:06:02 +00003523
3524 SDValue InChain = getRoot();
3525
Bill Wendlingba54bca2013-06-19 21:36:55 +00003526 const TargetLowering *TLI = TM.getTargetLowering();
3527 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003528 InChain = InsertFenceForAtomic(InChain, Order, Scope, true, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003529 DAG, *TLI);
Eli Friedman26689ac2011-08-03 21:06:02 +00003530
Eli Friedman55ba8162011-07-29 03:05:32 +00003531 SDValue L =
Eli Friedman26689ac2011-08-03 21:06:02 +00003532 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl,
Craig Topper0ff11902013-08-15 02:44:19 +00003533 getValue(I.getCompareOperand()).getSimpleValueType(),
Eli Friedman26689ac2011-08-03 21:06:02 +00003534 InChain,
Eli Friedman55ba8162011-07-29 03:05:32 +00003535 getValue(I.getPointerOperand()),
3536 getValue(I.getCompareOperand()),
3537 getValue(I.getNewValOperand()),
3538 MachinePointerInfo(I.getPointerOperand()), 0 /* Alignment */,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003539 TLI->getInsertFencesForAtomic() ? Monotonic : Order,
Eli Friedman327236c2011-08-24 20:50:09 +00003540 Scope);
Eli Friedman26689ac2011-08-03 21:06:02 +00003541
3542 SDValue OutChain = L.getValue(1);
3543
Bill Wendlingba54bca2013-06-19 21:36:55 +00003544 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003545 OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003546 DAG, *TLI);
Eli Friedman26689ac2011-08-03 21:06:02 +00003547
Eli Friedman55ba8162011-07-29 03:05:32 +00003548 setValue(&I, L);
Eli Friedman26689ac2011-08-03 21:06:02 +00003549 DAG.setRoot(OutChain);
Eli Friedmanff030482011-07-28 21:48:00 +00003550}
3551
3552void SelectionDAGBuilder::visitAtomicRMW(const AtomicRMWInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003553 SDLoc dl = getCurSDLoc();
Eli Friedman55ba8162011-07-29 03:05:32 +00003554 ISD::NodeType NT;
3555 switch (I.getOperation()) {
David Blaikie4d6ccb52012-01-20 21:51:11 +00003556 default: llvm_unreachable("Unknown atomicrmw operation");
Eli Friedman55ba8162011-07-29 03:05:32 +00003557 case AtomicRMWInst::Xchg: NT = ISD::ATOMIC_SWAP; break;
3558 case AtomicRMWInst::Add: NT = ISD::ATOMIC_LOAD_ADD; break;
3559 case AtomicRMWInst::Sub: NT = ISD::ATOMIC_LOAD_SUB; break;
3560 case AtomicRMWInst::And: NT = ISD::ATOMIC_LOAD_AND; break;
3561 case AtomicRMWInst::Nand: NT = ISD::ATOMIC_LOAD_NAND; break;
3562 case AtomicRMWInst::Or: NT = ISD::ATOMIC_LOAD_OR; break;
3563 case AtomicRMWInst::Xor: NT = ISD::ATOMIC_LOAD_XOR; break;
3564 case AtomicRMWInst::Max: NT = ISD::ATOMIC_LOAD_MAX; break;
3565 case AtomicRMWInst::Min: NT = ISD::ATOMIC_LOAD_MIN; break;
3566 case AtomicRMWInst::UMax: NT = ISD::ATOMIC_LOAD_UMAX; break;
3567 case AtomicRMWInst::UMin: NT = ISD::ATOMIC_LOAD_UMIN; break;
3568 }
Eli Friedman26689ac2011-08-03 21:06:02 +00003569 AtomicOrdering Order = I.getOrdering();
Eli Friedman327236c2011-08-24 20:50:09 +00003570 SynchronizationScope Scope = I.getSynchScope();
Eli Friedman26689ac2011-08-03 21:06:02 +00003571
3572 SDValue InChain = getRoot();
3573
Bill Wendlingba54bca2013-06-19 21:36:55 +00003574 const TargetLowering *TLI = TM.getTargetLowering();
3575 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003576 InChain = InsertFenceForAtomic(InChain, Order, Scope, true, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003577 DAG, *TLI);
Eli Friedman26689ac2011-08-03 21:06:02 +00003578
Eli Friedman55ba8162011-07-29 03:05:32 +00003579 SDValue L =
Eli Friedman26689ac2011-08-03 21:06:02 +00003580 DAG.getAtomic(NT, dl,
Craig Topper0ff11902013-08-15 02:44:19 +00003581 getValue(I.getValOperand()).getSimpleValueType(),
Eli Friedman26689ac2011-08-03 21:06:02 +00003582 InChain,
Eli Friedman55ba8162011-07-29 03:05:32 +00003583 getValue(I.getPointerOperand()),
3584 getValue(I.getValOperand()),
3585 I.getPointerOperand(), 0 /* Alignment */,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003586 TLI->getInsertFencesForAtomic() ? Monotonic : Order,
Eli Friedman327236c2011-08-24 20:50:09 +00003587 Scope);
Eli Friedman26689ac2011-08-03 21:06:02 +00003588
3589 SDValue OutChain = L.getValue(1);
3590
Bill Wendlingba54bca2013-06-19 21:36:55 +00003591 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003592 OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003593 DAG, *TLI);
Eli Friedman26689ac2011-08-03 21:06:02 +00003594
Eli Friedman55ba8162011-07-29 03:05:32 +00003595 setValue(&I, L);
Eli Friedman26689ac2011-08-03 21:06:02 +00003596 DAG.setRoot(OutChain);
Eli Friedmanff030482011-07-28 21:48:00 +00003597}
3598
Eli Friedman47f35132011-07-25 23:16:38 +00003599void SelectionDAGBuilder::visitFence(const FenceInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003600 SDLoc dl = getCurSDLoc();
Bill Wendlingba54bca2013-06-19 21:36:55 +00003601 const TargetLowering *TLI = TM.getTargetLowering();
Eli Friedman14648462011-07-27 22:21:52 +00003602 SDValue Ops[3];
3603 Ops[0] = getRoot();
Bill Wendlingba54bca2013-06-19 21:36:55 +00003604 Ops[1] = DAG.getConstant(I.getOrdering(), TLI->getPointerTy());
3605 Ops[2] = DAG.getConstant(I.getSynchScope(), TLI->getPointerTy());
Eli Friedman14648462011-07-27 22:21:52 +00003606 DAG.setRoot(DAG.getNode(ISD::ATOMIC_FENCE, dl, MVT::Other, Ops, 3));
Eli Friedman47f35132011-07-25 23:16:38 +00003607}
3608
Eli Friedman327236c2011-08-24 20:50:09 +00003609void SelectionDAGBuilder::visitAtomicLoad(const LoadInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003610 SDLoc dl = getCurSDLoc();
Eli Friedman327236c2011-08-24 20:50:09 +00003611 AtomicOrdering Order = I.getOrdering();
3612 SynchronizationScope Scope = I.getSynchScope();
3613
3614 SDValue InChain = getRoot();
3615
Bill Wendlingba54bca2013-06-19 21:36:55 +00003616 const TargetLowering *TLI = TM.getTargetLowering();
3617 EVT VT = TLI->getValueType(I.getType());
Eli Friedman327236c2011-08-24 20:50:09 +00003618
Evan Cheng607acd62013-02-06 02:06:33 +00003619 if (I.getAlignment() < VT.getSizeInBits() / 8)
Eli Friedmanfe731212011-09-13 20:50:54 +00003620 report_fatal_error("Cannot generate unaligned atomic load");
3621
Eli Friedman327236c2011-08-24 20:50:09 +00003622 SDValue L =
3623 DAG.getAtomic(ISD::ATOMIC_LOAD, dl, VT, VT, InChain,
3624 getValue(I.getPointerOperand()),
3625 I.getPointerOperand(), I.getAlignment(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00003626 TLI->getInsertFencesForAtomic() ? Monotonic : Order,
Eli Friedman327236c2011-08-24 20:50:09 +00003627 Scope);
3628
3629 SDValue OutChain = L.getValue(1);
3630
Bill Wendlingba54bca2013-06-19 21:36:55 +00003631 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003632 OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003633 DAG, *TLI);
Eli Friedman327236c2011-08-24 20:50:09 +00003634
3635 setValue(&I, L);
3636 DAG.setRoot(OutChain);
3637}
3638
3639void SelectionDAGBuilder::visitAtomicStore(const StoreInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003640 SDLoc dl = getCurSDLoc();
Eli Friedman327236c2011-08-24 20:50:09 +00003641
3642 AtomicOrdering Order = I.getOrdering();
3643 SynchronizationScope Scope = I.getSynchScope();
3644
3645 SDValue InChain = getRoot();
3646
Bill Wendlingba54bca2013-06-19 21:36:55 +00003647 const TargetLowering *TLI = TM.getTargetLowering();
3648 EVT VT = TLI->getValueType(I.getValueOperand()->getType());
Eli Friedmanfe731212011-09-13 20:50:54 +00003649
Evan Cheng607acd62013-02-06 02:06:33 +00003650 if (I.getAlignment() < VT.getSizeInBits() / 8)
Eli Friedmanfe731212011-09-13 20:50:54 +00003651 report_fatal_error("Cannot generate unaligned atomic store");
3652
Bill Wendlingba54bca2013-06-19 21:36:55 +00003653 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003654 InChain = InsertFenceForAtomic(InChain, Order, Scope, true, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003655 DAG, *TLI);
Eli Friedman327236c2011-08-24 20:50:09 +00003656
3657 SDValue OutChain =
Eli Friedmanfe731212011-09-13 20:50:54 +00003658 DAG.getAtomic(ISD::ATOMIC_STORE, dl, VT,
Eli Friedman327236c2011-08-24 20:50:09 +00003659 InChain,
3660 getValue(I.getPointerOperand()),
3661 getValue(I.getValueOperand()),
3662 I.getPointerOperand(), I.getAlignment(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00003663 TLI->getInsertFencesForAtomic() ? Monotonic : Order,
Eli Friedman327236c2011-08-24 20:50:09 +00003664 Scope);
3665
Bill Wendlingba54bca2013-06-19 21:36:55 +00003666 if (TLI->getInsertFencesForAtomic())
Eli Friedman327236c2011-08-24 20:50:09 +00003667 OutChain = InsertFenceForAtomic(OutChain, Order, Scope, false, dl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00003668 DAG, *TLI);
Eli Friedman327236c2011-08-24 20:50:09 +00003669
3670 DAG.setRoot(OutChain);
3671}
3672
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003673/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
3674/// node.
Dan Gohman46510a72010-04-15 01:51:59 +00003675void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I,
Dan Gohman2048b852009-11-23 18:04:58 +00003676 unsigned Intrinsic) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003677 bool HasChain = !I.doesNotAccessMemory();
3678 bool OnlyLoad = HasChain && I.onlyReadsMemory();
3679
3680 // Build the operand list.
3681 SmallVector<SDValue, 8> Ops;
3682 if (HasChain) { // If this intrinsic has side-effects, chainify it.
3683 if (OnlyLoad) {
3684 // We don't need to serialize loads against other loads.
3685 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003686 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003687 Ops.push_back(getRoot());
3688 }
3689 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003690
3691 // Info is set by getTgtMemInstrinsic
3692 TargetLowering::IntrinsicInfo Info;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003693 const TargetLowering *TLI = TM.getTargetLowering();
3694 bool IsTgtIntrinsic = TLI->getTgtMemIntrinsic(Info, I, Intrinsic);
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003695
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003696 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Bob Wilson65ffec42010-09-21 17:56:22 +00003697 if (!IsTgtIntrinsic || Info.opc == ISD::INTRINSIC_VOID ||
3698 Info.opc == ISD::INTRINSIC_W_CHAIN)
Bill Wendlingba54bca2013-06-19 21:36:55 +00003699 Ops.push_back(DAG.getTargetConstant(Intrinsic, TLI->getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003700
3701 // Add all operands of the call to the operand list.
Gabor Greif0635f352010-06-25 09:38:13 +00003702 for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) {
3703 SDValue Op = getValue(I.getArgOperand(i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003704 Ops.push_back(Op);
3705 }
3706
Owen Andersone50ed302009-08-10 22:56:29 +00003707 SmallVector<EVT, 4> ValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00003708 ComputeValueVTs(*TLI, I.getType(), ValueVTs);
Bill Wendling856ff412009-12-22 00:12:37 +00003709
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003710 if (HasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00003711 ValueVTs.push_back(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003712
Bob Wilson8d919552009-07-31 22:41:21 +00003713 SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003714
3715 // Create the node.
3716 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003717 if (IsTgtIntrinsic) {
3718 // This is target intrinsic that touches memory
Andrew Trickac6d9be2013-05-25 02:42:55 +00003719 Result = DAG.getMemIntrinsicNode(Info.opc, getCurSDLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003720 VTs, &Ops[0], Ops.size(),
Chris Lattnere9ba5dd2010-09-21 04:57:15 +00003721 Info.memVT,
3722 MachinePointerInfo(Info.ptrVal, Info.offset),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003723 Info.align, Info.vol,
3724 Info.readMem, Info.writeMem);
Bill Wendling856ff412009-12-22 00:12:37 +00003725 } else if (!HasChain) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003726 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurSDLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003727 VTs, &Ops[0], Ops.size());
Benjamin Kramerf0127052010-01-05 13:12:22 +00003728 } else if (!I.getType()->isVoidTy()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003729 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurSDLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003730 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003731 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003732 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurSDLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003733 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003734 }
3735
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003736 if (HasChain) {
3737 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
3738 if (OnlyLoad)
3739 PendingLoads.push_back(Chain);
3740 else
3741 DAG.setRoot(Chain);
3742 }
Bill Wendling856ff412009-12-22 00:12:37 +00003743
Benjamin Kramerf0127052010-01-05 13:12:22 +00003744 if (!I.getType()->isVoidTy()) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00003745 if (VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00003746 EVT VT = TLI->getValueType(PTy);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003747 Result = DAG.getNode(ISD::BITCAST, getCurSDLoc(), VT, Result);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003748 }
Bill Wendling856ff412009-12-22 00:12:37 +00003749
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003750 setValue(&I, Result);
3751 }
3752}
3753
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003754/// GetSignificand - Get the significand and build it into a floating-point
3755/// number with exponent of 1:
3756///
3757/// Op = (Op & 0x007fffff) | 0x3f800000;
3758///
Matt Beaumont-Gay50e75bf2013-02-25 18:11:18 +00003759/// where Op is the hexadecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003760static SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +00003761GetSignificand(SelectionDAG &DAG, SDValue Op, SDLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003762 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3763 DAG.getConstant(0x007fffff, MVT::i32));
3764 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
3765 DAG.getConstant(0x3f800000, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003766 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00003767}
3768
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003769/// GetExponent - Get the exponent:
3770///
Bill Wendlinge9a72862009-01-20 21:17:57 +00003771/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003772///
Matt Beaumont-Gay50e75bf2013-02-25 18:11:18 +00003773/// where Op is the hexadecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003774static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00003775GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003776 SDLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003777 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3778 DAG.getConstant(0x7f800000, MVT::i32));
3779 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Duncan Sands92abc622009-01-31 15:50:11 +00003780 DAG.getConstant(23, TLI.getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00003781 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
3782 DAG.getConstant(127, MVT::i32));
Bill Wendling4533cac2010-01-28 21:51:40 +00003783 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00003784}
3785
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003786/// getF32Constant - Get 32-bit floating point constant.
3787static SDValue
3788getF32Constant(SelectionDAG &DAG, unsigned Flt) {
Tim Northover0a29cb02013-01-22 09:46:31 +00003789 return DAG.getConstantFP(APFloat(APFloat::IEEEsingle, APInt(32, Flt)),
3790 MVT::f32);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003791}
3792
Craig Topper538cd482012-11-24 18:52:06 +00003793/// expandExp - Lower an exp intrinsic. Handles the special sequences for
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003794/// limited-precision mode.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003795static SDValue expandExp(SDLoc dl, SDValue Op, SelectionDAG &DAG,
Craig Topper538cd482012-11-24 18:52:06 +00003796 const TargetLowering &TLI) {
3797 if (Op.getValueType() == MVT::f32 &&
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003798 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003799
3800 // Put the exponent in the right bit position for later addition to the
3801 // final result:
3802 //
3803 // #define LOG2OFe 1.4426950f
3804 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Owen Anderson825b72b2009-08-11 20:47:22 +00003805 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003806 getF32Constant(DAG, 0x3fb8aa3b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003807 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003808
3809 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003810 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3811 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003812
3813 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003814 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003815 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendling856ff412009-12-22 00:12:37 +00003816
Craig Topperb3157722012-11-24 08:22:37 +00003817 SDValue TwoToFracPartOfX;
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003818 if (LimitFloatPrecision <= 6) {
3819 // For floating-point precision of 6:
3820 //
3821 // TwoToFractionalPartOfX =
3822 // 0.997535578f +
3823 // (0.735607626f + 0.252464424f * x) * x;
3824 //
3825 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003826 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003827 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003828 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003829 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003830 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
Craig Topperb3157722012-11-24 08:22:37 +00003831 TwoToFracPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
3832 getF32Constant(DAG, 0x3f7f5e7e));
Craig Topper08ac4692012-11-16 20:01:39 +00003833 } else if (LimitFloatPrecision <= 12) {
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003834 // For floating-point precision of 12:
3835 //
3836 // TwoToFractionalPartOfX =
3837 // 0.999892986f +
3838 // (0.696457318f +
3839 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3840 //
3841 // 0.000107046256 error, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003842 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003843 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003844 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003845 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003846 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3847 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003848 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003849 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
Craig Topperb3157722012-11-24 08:22:37 +00003850 TwoToFracPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
3851 getF32Constant(DAG, 0x3f7ff8fd));
Craig Topper08ac4692012-11-16 20:01:39 +00003852 } else { // LimitFloatPrecision <= 18
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003853 // For floating-point precision of 18:
3854 //
3855 // TwoToFractionalPartOfX =
3856 // 0.999999982f +
3857 // (0.693148872f +
3858 // (0.240227044f +
3859 // (0.554906021e-1f +
3860 // (0.961591928e-2f +
3861 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3862 //
3863 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003864 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003865 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003866 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003867 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003868 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3869 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003870 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003871 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3872 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003873 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003874 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3875 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003876 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003877 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3878 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003879 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003880 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
Craig Topperb3157722012-11-24 08:22:37 +00003881 TwoToFracPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
3882 getF32Constant(DAG, 0x3f800000));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003883 }
Craig Topperb3157722012-11-24 08:22:37 +00003884
3885 // Add the exponent into the result in integer domain.
3886 SDValue t13 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, TwoToFracPartOfX);
Craig Topper538cd482012-11-24 18:52:06 +00003887 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3888 DAG.getNode(ISD::ADD, dl, MVT::i32,
3889 t13, IntegerPartOfX));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003890 }
3891
Craig Topper538cd482012-11-24 18:52:06 +00003892 // No special expansion.
3893 return DAG.getNode(ISD::FEXP, dl, Op.getValueType(), Op);
Dale Johannesen59e577f2008-09-05 18:38:42 +00003894}
3895
Craig Topper5d1e0892012-11-23 18:38:31 +00003896/// expandLog - Lower a log intrinsic. Handles the special sequences for
Bill Wendling39150252008-09-09 20:39:27 +00003897/// limited-precision mode.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003898static SDValue expandLog(SDLoc dl, SDValue Op, SelectionDAG &DAG,
Craig Topper5d1e0892012-11-23 18:38:31 +00003899 const TargetLowering &TLI) {
3900 if (Op.getValueType() == MVT::f32 &&
Bill Wendling39150252008-09-09 20:39:27 +00003901 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003902 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00003903
3904 // Scale the exponent by log(2) [0.69314718f].
Bill Wendling46ada192010-03-02 01:55:18 +00003905 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003906 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003907 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003908
3909 // Get the significand and build it into a floating-point number with
3910 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003911 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling39150252008-09-09 20:39:27 +00003912
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003913 SDValue LogOfMantissa;
Bill Wendling39150252008-09-09 20:39:27 +00003914 if (LimitFloatPrecision <= 6) {
3915 // For floating-point precision of 6:
3916 //
3917 // LogofMantissa =
3918 // -1.1609546f +
3919 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003920 //
Bill Wendling39150252008-09-09 20:39:27 +00003921 // error 0.0034276066, which is better than 8 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003922 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003923 getF32Constant(DAG, 0xbe74c456));
Owen Anderson825b72b2009-08-11 20:47:22 +00003924 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003925 getF32Constant(DAG, 0x3fb3a2b1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003926 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003927 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
3928 getF32Constant(DAG, 0x3f949a29));
Craig Topper08ac4692012-11-16 20:01:39 +00003929 } else if (LimitFloatPrecision <= 12) {
Bill Wendling39150252008-09-09 20:39:27 +00003930 // For floating-point precision of 12:
3931 //
3932 // LogOfMantissa =
3933 // -1.7417939f +
3934 // (2.8212026f +
3935 // (-1.4699568f +
3936 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3937 //
3938 // error 0.000061011436, which is 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003939 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003940 getF32Constant(DAG, 0xbd67b6d6));
Owen Anderson825b72b2009-08-11 20:47:22 +00003941 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003942 getF32Constant(DAG, 0x3ee4f4b8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003943 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3944 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003945 getF32Constant(DAG, 0x3fbc278b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003946 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3947 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003948 getF32Constant(DAG, 0x40348e95));
Owen Anderson825b72b2009-08-11 20:47:22 +00003949 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003950 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
3951 getF32Constant(DAG, 0x3fdef31a));
Craig Topper08ac4692012-11-16 20:01:39 +00003952 } else { // LimitFloatPrecision <= 18
Bill Wendling39150252008-09-09 20:39:27 +00003953 // For floating-point precision of 18:
3954 //
3955 // LogOfMantissa =
3956 // -2.1072184f +
3957 // (4.2372794f +
3958 // (-3.7029485f +
3959 // (2.2781945f +
3960 // (-0.87823314f +
3961 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3962 //
3963 // error 0.0000023660568, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003964 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003965 getF32Constant(DAG, 0xbc91e5ac));
Owen Anderson825b72b2009-08-11 20:47:22 +00003966 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003967 getF32Constant(DAG, 0x3e4350aa));
Owen Anderson825b72b2009-08-11 20:47:22 +00003968 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3969 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003970 getF32Constant(DAG, 0x3f60d3e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003971 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3972 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003973 getF32Constant(DAG, 0x4011cdf0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003974 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3975 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003976 getF32Constant(DAG, 0x406cfd1c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003977 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3978 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003979 getF32Constant(DAG, 0x408797cb));
Owen Anderson825b72b2009-08-11 20:47:22 +00003980 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003981 LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
3982 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003983 }
Craig Topperdf0ea8d2012-11-16 19:08:44 +00003984
Craig Topper5d1e0892012-11-23 18:38:31 +00003985 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003986 }
3987
Craig Topper5d1e0892012-11-23 18:38:31 +00003988 // No special expansion.
3989 return DAG.getNode(ISD::FLOG, dl, Op.getValueType(), Op);
Dale Johannesen59e577f2008-09-05 18:38:42 +00003990}
3991
Craig Topper5d1e0892012-11-23 18:38:31 +00003992/// expandLog2 - Lower a log2 intrinsic. Handles the special sequences for
Bill Wendling3eb59402008-09-09 00:28:24 +00003993/// limited-precision mode.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003994static SDValue expandLog2(SDLoc dl, SDValue Op, SelectionDAG &DAG,
Craig Topper5d1e0892012-11-23 18:38:31 +00003995 const TargetLowering &TLI) {
3996 if (Op.getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003997 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003998 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003999
Bill Wendling39150252008-09-09 20:39:27 +00004000 // Get the exponent.
Bill Wendling46ada192010-03-02 01:55:18 +00004001 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
Bill Wendling856ff412009-12-22 00:12:37 +00004002
Bill Wendling3eb59402008-09-09 00:28:24 +00004003 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00004004 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00004005 SDValue X = GetSignificand(DAG, Op1, dl);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004006
Bill Wendling3eb59402008-09-09 00:28:24 +00004007 // Different possible minimax approximations of significand in
4008 // floating-point for various degrees of accuracy over [1,2].
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004009 SDValue Log2ofMantissa;
Bill Wendling3eb59402008-09-09 00:28:24 +00004010 if (LimitFloatPrecision <= 6) {
4011 // For floating-point precision of 6:
4012 //
4013 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
4014 //
4015 // error 0.0049451742, which is more than 7 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004016 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004017 getF32Constant(DAG, 0xbeb08fe0));
Owen Anderson825b72b2009-08-11 20:47:22 +00004018 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004019 getF32Constant(DAG, 0x40019463));
Owen Anderson825b72b2009-08-11 20:47:22 +00004020 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004021 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
4022 getF32Constant(DAG, 0x3fd6633d));
Craig Topper08ac4692012-11-16 20:01:39 +00004023 } else if (LimitFloatPrecision <= 12) {
Bill Wendling3eb59402008-09-09 00:28:24 +00004024 // For floating-point precision of 12:
4025 //
4026 // Log2ofMantissa =
4027 // -2.51285454f +
4028 // (4.07009056f +
4029 // (-2.12067489f +
4030 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004031 //
Bill Wendling3eb59402008-09-09 00:28:24 +00004032 // error 0.0000876136000, which is better than 13 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004033 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004034 getF32Constant(DAG, 0xbda7262e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004035 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004036 getF32Constant(DAG, 0x3f25280b));
Owen Anderson825b72b2009-08-11 20:47:22 +00004037 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
4038 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004039 getF32Constant(DAG, 0x4007b923));
Owen Anderson825b72b2009-08-11 20:47:22 +00004040 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4041 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004042 getF32Constant(DAG, 0x40823e2f));
Owen Anderson825b72b2009-08-11 20:47:22 +00004043 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004044 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
4045 getF32Constant(DAG, 0x4020d29c));
Craig Topper08ac4692012-11-16 20:01:39 +00004046 } else { // LimitFloatPrecision <= 18
Bill Wendling3eb59402008-09-09 00:28:24 +00004047 // For floating-point precision of 18:
4048 //
4049 // Log2ofMantissa =
4050 // -3.0400495f +
4051 // (6.1129976f +
4052 // (-5.3420409f +
4053 // (3.2865683f +
4054 // (-1.2669343f +
4055 // (0.27515199f -
4056 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
4057 //
4058 // error 0.0000018516, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004059 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004060 getF32Constant(DAG, 0xbcd2769e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004061 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004062 getF32Constant(DAG, 0x3e8ce0b9));
Owen Anderson825b72b2009-08-11 20:47:22 +00004063 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
4064 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004065 getF32Constant(DAG, 0x3fa22ae7));
Owen Anderson825b72b2009-08-11 20:47:22 +00004066 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4067 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004068 getF32Constant(DAG, 0x40525723));
Owen Anderson825b72b2009-08-11 20:47:22 +00004069 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4070 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004071 getF32Constant(DAG, 0x40aaf200));
Owen Anderson825b72b2009-08-11 20:47:22 +00004072 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
4073 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004074 getF32Constant(DAG, 0x40c39dad));
Owen Anderson825b72b2009-08-11 20:47:22 +00004075 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004076 Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
4077 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00004078 }
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004079
Craig Topper5d1e0892012-11-23 18:38:31 +00004080 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log2ofMantissa);
Dale Johannesen853244f2008-09-05 23:49:37 +00004081 }
Bill Wendling3eb59402008-09-09 00:28:24 +00004082
Craig Topper5d1e0892012-11-23 18:38:31 +00004083 // No special expansion.
4084 return DAG.getNode(ISD::FLOG2, dl, Op.getValueType(), Op);
Dale Johannesen59e577f2008-09-05 18:38:42 +00004085}
4086
Craig Topper5d1e0892012-11-23 18:38:31 +00004087/// expandLog10 - Lower a log10 intrinsic. Handles the special sequences for
Bill Wendling3eb59402008-09-09 00:28:24 +00004088/// limited-precision mode.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004089static SDValue expandLog10(SDLoc dl, SDValue Op, SelectionDAG &DAG,
Craig Topper5d1e0892012-11-23 18:38:31 +00004090 const TargetLowering &TLI) {
4091 if (Op.getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00004092 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004093 SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00004094
Bill Wendling39150252008-09-09 20:39:27 +00004095 // Scale the exponent by log10(2) [0.30102999f].
Bill Wendling46ada192010-03-02 01:55:18 +00004096 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00004097 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004098 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00004099
4100 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00004101 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00004102 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling3eb59402008-09-09 00:28:24 +00004103
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004104 SDValue Log10ofMantissa;
Bill Wendling3eb59402008-09-09 00:28:24 +00004105 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00004106 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004107 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00004108 // Log10ofMantissa =
4109 // -0.50419619f +
4110 // (0.60948995f - 0.10380950f * x) * x;
4111 //
4112 // error 0.0014886165, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004113 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004114 getF32Constant(DAG, 0xbdd49a13));
Owen Anderson825b72b2009-08-11 20:47:22 +00004115 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004116 getF32Constant(DAG, 0x3f1c0789));
Owen Anderson825b72b2009-08-11 20:47:22 +00004117 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004118 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
4119 getF32Constant(DAG, 0x3f011300));
Craig Topper08ac4692012-11-16 20:01:39 +00004120 } else if (LimitFloatPrecision <= 12) {
Bill Wendling3eb59402008-09-09 00:28:24 +00004121 // For floating-point precision of 12:
4122 //
4123 // Log10ofMantissa =
4124 // -0.64831180f +
4125 // (0.91751397f +
4126 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
4127 //
4128 // error 0.00019228036, which is better than 12 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004129 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004130 getF32Constant(DAG, 0x3d431f31));
Owen Anderson825b72b2009-08-11 20:47:22 +00004131 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004132 getF32Constant(DAG, 0x3ea21fb2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004133 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
4134 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004135 getF32Constant(DAG, 0x3f6ae232));
Owen Anderson825b72b2009-08-11 20:47:22 +00004136 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004137 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
4138 getF32Constant(DAG, 0x3f25f7c3));
Craig Topper08ac4692012-11-16 20:01:39 +00004139 } else { // LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00004140 // For floating-point precision of 18:
4141 //
4142 // Log10ofMantissa =
4143 // -0.84299375f +
4144 // (1.5327582f +
4145 // (-1.0688956f +
4146 // (0.49102474f +
4147 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
4148 //
4149 // error 0.0000037995730, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004150 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004151 getF32Constant(DAG, 0x3c5d51ce));
Owen Anderson825b72b2009-08-11 20:47:22 +00004152 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004153 getF32Constant(DAG, 0x3e00685a));
Owen Anderson825b72b2009-08-11 20:47:22 +00004154 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
4155 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004156 getF32Constant(DAG, 0x3efb6798));
Owen Anderson825b72b2009-08-11 20:47:22 +00004157 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4158 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004159 getF32Constant(DAG, 0x3f88d192));
Owen Anderson825b72b2009-08-11 20:47:22 +00004160 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4161 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004162 getF32Constant(DAG, 0x3fc4316c));
Owen Anderson825b72b2009-08-11 20:47:22 +00004163 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004164 Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
4165 getF32Constant(DAG, 0x3f57ce70));
Bill Wendling3eb59402008-09-09 00:28:24 +00004166 }
Craig Topperdf0ea8d2012-11-16 19:08:44 +00004167
Craig Topper5d1e0892012-11-23 18:38:31 +00004168 return DAG.getNode(ISD::FADD, dl, MVT::f32, LogOfExponent, Log10ofMantissa);
Dale Johannesen852680a2008-09-05 21:27:19 +00004169 }
Bill Wendling3eb59402008-09-09 00:28:24 +00004170
Craig Topper5d1e0892012-11-23 18:38:31 +00004171 // No special expansion.
4172 return DAG.getNode(ISD::FLOG10, dl, Op.getValueType(), Op);
Dale Johannesen59e577f2008-09-05 18:38:42 +00004173}
4174
Craig Topper538cd482012-11-24 18:52:06 +00004175/// expandExp2 - Lower an exp2 intrinsic. Handles the special sequences for
Bill Wendlinge10c8142008-09-09 22:39:21 +00004176/// limited-precision mode.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004177static SDValue expandExp2(SDLoc dl, SDValue Op, SelectionDAG &DAG,
Craig Topper538cd482012-11-24 18:52:06 +00004178 const TargetLowering &TLI) {
4179 if (Op.getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00004180 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004181 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00004182
4183 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00004184 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
4185 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00004186
4187 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00004188 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00004189 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00004190
Craig Topperb3157722012-11-24 08:22:37 +00004191 SDValue TwoToFractionalPartOfX;
Bill Wendlinge10c8142008-09-09 22:39:21 +00004192 if (LimitFloatPrecision <= 6) {
4193 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004194 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00004195 // TwoToFractionalPartOfX =
4196 // 0.997535578f +
4197 // (0.735607626f + 0.252464424f * x) * x;
4198 //
4199 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004200 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004201 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00004202 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004203 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004204 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
Craig Topperb3157722012-11-24 08:22:37 +00004205 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
4206 getF32Constant(DAG, 0x3f7f5e7e));
Craig Topper08ac4692012-11-16 20:01:39 +00004207 } else if (LimitFloatPrecision <= 12) {
Bill Wendlinge10c8142008-09-09 22:39:21 +00004208 // For floating-point precision of 12:
4209 //
4210 // TwoToFractionalPartOfX =
4211 // 0.999892986f +
4212 // (0.696457318f +
4213 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
4214 //
4215 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004216 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004217 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004218 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004219 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004220 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4221 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004222 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00004223 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
Craig Topperb3157722012-11-24 08:22:37 +00004224 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
4225 getF32Constant(DAG, 0x3f7ff8fd));
Craig Topper08ac4692012-11-16 20:01:39 +00004226 } else { // LimitFloatPrecision <= 18
Bill Wendlinge10c8142008-09-09 22:39:21 +00004227 // For floating-point precision of 18:
4228 //
4229 // TwoToFractionalPartOfX =
4230 // 0.999999982f +
4231 // (0.693148872f +
4232 // (0.240227044f +
4233 // (0.554906021e-1f +
4234 // (0.961591928e-2f +
4235 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
4236 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004237 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004238 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004239 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004240 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00004241 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4242 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004243 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00004244 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4245 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004246 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00004247 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
4248 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004249 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00004250 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
4251 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004252 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00004253 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
Craig Topperb3157722012-11-24 08:22:37 +00004254 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
4255 getF32Constant(DAG, 0x3f800000));
Bill Wendlinge10c8142008-09-09 22:39:21 +00004256 }
Craig Topperb3157722012-11-24 08:22:37 +00004257
4258 // Add the exponent into the result in integer domain.
4259 SDValue t13 = DAG.getNode(ISD::BITCAST, dl, MVT::i32,
4260 TwoToFractionalPartOfX);
Craig Topper538cd482012-11-24 18:52:06 +00004261 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
4262 DAG.getNode(ISD::ADD, dl, MVT::i32,
4263 t13, IntegerPartOfX));
Dale Johannesen601d3c02008-09-05 01:48:15 +00004264 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00004265
Craig Topper538cd482012-11-24 18:52:06 +00004266 // No special expansion.
4267 return DAG.getNode(ISD::FEXP2, dl, Op.getValueType(), Op);
Dale Johannesen601d3c02008-09-05 01:48:15 +00004268}
4269
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004270/// visitPow - Lower a pow intrinsic. Handles the special sequences for
4271/// limited-precision mode with x == 10.0f.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004272static SDValue expandPow(SDLoc dl, SDValue LHS, SDValue RHS,
Craig Topper327e4cb2012-11-25 08:08:58 +00004273 SelectionDAG &DAG, const TargetLowering &TLI) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004274 bool IsExp10 = false;
Craig Topper327e4cb2012-11-25 08:08:58 +00004275 if (LHS.getValueType() == MVT::f32 && LHS.getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004276 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Craig Topper327e4cb2012-11-25 08:08:58 +00004277 if (ConstantFPSDNode *LHSC = dyn_cast<ConstantFPSDNode>(LHS)) {
4278 APFloat Ten(10.0f);
4279 IsExp10 = LHSC->isExactlyValue(Ten);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004280 }
4281 }
4282
Craig Topperc1aa6382012-11-25 00:48:58 +00004283 if (IsExp10) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004284 // Put the exponent in the right bit position for later addition to the
4285 // final result:
4286 //
4287 // #define LOG2OF10 3.3219281f
4288 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Craig Topper327e4cb2012-11-25 08:08:58 +00004289 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, RHS,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004290 getF32Constant(DAG, 0x40549a78));
Owen Anderson825b72b2009-08-11 20:47:22 +00004291 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004292
4293 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00004294 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
4295 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004296
4297 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00004298 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00004299 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004300
Craig Topper915562e2012-11-25 00:15:07 +00004301 SDValue TwoToFractionalPartOfX;
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004302 if (LimitFloatPrecision <= 6) {
4303 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004304 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004305 // twoToFractionalPartOfX =
4306 // 0.997535578f +
4307 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004308 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004309 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004310 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004311 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00004312 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004313 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004314 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
Craig Topper915562e2012-11-25 00:15:07 +00004315 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
4316 getF32Constant(DAG, 0x3f7f5e7e));
Craig Topper08ac4692012-11-16 20:01:39 +00004317 } else if (LimitFloatPrecision <= 12) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004318 // For floating-point precision of 12:
4319 //
4320 // TwoToFractionalPartOfX =
4321 // 0.999892986f +
4322 // (0.696457318f +
4323 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
4324 //
4325 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004326 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004327 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004328 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004329 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004330 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4331 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004332 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00004333 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
Craig Topper915562e2012-11-25 00:15:07 +00004334 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
4335 getF32Constant(DAG, 0x3f7ff8fd));
Craig Topper08ac4692012-11-16 20:01:39 +00004336 } else { // LimitFloatPrecision <= 18
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004337 // For floating-point precision of 18:
4338 //
4339 // TwoToFractionalPartOfX =
4340 // 0.999999982f +
4341 // (0.693148872f +
4342 // (0.240227044f +
4343 // (0.554906021e-1f +
4344 // (0.961591928e-2f +
4345 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
4346 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004347 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004348 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004349 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004350 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00004351 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4352 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004353 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00004354 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4355 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004356 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00004357 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
4358 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004359 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00004360 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
4361 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004362 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00004363 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
Craig Topper915562e2012-11-25 00:15:07 +00004364 TwoToFractionalPartOfX = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
4365 getF32Constant(DAG, 0x3f800000));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004366 }
Craig Topper915562e2012-11-25 00:15:07 +00004367
4368 SDValue t13 = DAG.getNode(ISD::BITCAST, dl,MVT::i32,TwoToFractionalPartOfX);
Craig Topper327e4cb2012-11-25 08:08:58 +00004369 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
4370 DAG.getNode(ISD::ADD, dl, MVT::i32,
4371 t13, IntegerPartOfX));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004372 }
4373
Craig Topper327e4cb2012-11-25 08:08:58 +00004374 // No special expansion.
4375 return DAG.getNode(ISD::FPOW, dl, LHS.getValueType(), LHS, RHS);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004376}
4377
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004378
4379/// ExpandPowI - Expand a llvm.powi intrinsic.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004380static SDValue ExpandPowI(SDLoc DL, SDValue LHS, SDValue RHS,
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004381 SelectionDAG &DAG) {
4382 // If RHS is a constant, we can expand this out to a multiplication tree,
4383 // otherwise we end up lowering to a call to __powidf2 (for example). When
4384 // optimizing for size, we only want to do this if the expansion would produce
4385 // a small number of multiplies, otherwise we do the full expansion.
4386 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
4387 // Get the exponent as a positive value.
4388 unsigned Val = RHSC->getSExtValue();
4389 if ((int)Val < 0) Val = -Val;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004390
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004391 // powi(x, 0) -> 1.0
4392 if (Val == 0)
4393 return DAG.getConstantFP(1.0, LHS.getValueType());
4394
Dan Gohmanae541aa2010-04-15 04:33:49 +00004395 const Function *F = DAG.getMachineFunction().getFunction();
Bill Wendling831737d2012-12-30 10:32:01 +00004396 if (!F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
4397 Attribute::OptimizeForSize) ||
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004398 // If optimizing for size, don't insert too many multiplies. This
4399 // inserts up to 5 multiplies.
4400 CountPopulation_32(Val)+Log2_32(Val) < 7) {
4401 // We use the simple binary decomposition method to generate the multiply
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004402 // sequence. There are more optimal ways to do this (for example,
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004403 // powi(x,15) generates one more multiply than it should), but this has
4404 // the benefit of being both really simple and much better than a libcall.
4405 SDValue Res; // Logically starts equal to 1.0
4406 SDValue CurSquare = LHS;
4407 while (Val) {
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00004408 if (Val & 1) {
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004409 if (Res.getNode())
4410 Res = DAG.getNode(ISD::FMUL, DL,Res.getValueType(), Res, CurSquare);
4411 else
4412 Res = CurSquare; // 1.0*CurSquare.
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00004413 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004414
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004415 CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
4416 CurSquare, CurSquare);
4417 Val >>= 1;
4418 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004419
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004420 // If the original was negative, invert the result, producing 1/(x*x*x).
4421 if (RHSC->getSExtValue() < 0)
4422 Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(),
4423 DAG.getConstantFP(1.0, LHS.getValueType()), Res);
4424 return Res;
4425 }
4426 }
4427
4428 // Otherwise, expand to a libcall.
4429 return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS);
4430}
4431
Devang Patel227dfdb2011-05-16 21:24:05 +00004432// getTruncatedArgReg - Find underlying register used for an truncated
4433// argument.
4434static unsigned getTruncatedArgReg(const SDValue &N) {
4435 if (N.getOpcode() != ISD::TRUNCATE)
4436 return 0;
4437
4438 const SDValue &Ext = N.getOperand(0);
Stephen Lin09f8ca32013-07-06 21:44:25 +00004439 if (Ext.getOpcode() == ISD::AssertZext ||
4440 Ext.getOpcode() == ISD::AssertSext) {
Devang Patel227dfdb2011-05-16 21:24:05 +00004441 const SDValue &CFR = Ext.getOperand(0);
4442 if (CFR.getOpcode() == ISD::CopyFromReg)
4443 return cast<RegisterSDNode>(CFR.getOperand(1))->getReg();
Craig Topper7eb46d82012-04-11 04:55:51 +00004444 if (CFR.getOpcode() == ISD::TRUNCATE)
4445 return getTruncatedArgReg(CFR);
Devang Patel227dfdb2011-05-16 21:24:05 +00004446 }
4447 return 0;
4448}
4449
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004450/// EmitFuncArgumentDbgValue - If the DbgValueInst is a dbg_value of a function
4451/// argument, create the corresponding DBG_VALUE machine instruction for it now.
4452/// At the end of instruction selection, they will be inserted to the entry BB.
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004453bool
Devang Patel78a06e52010-08-25 20:39:26 +00004454SelectionDAGBuilder::EmitFuncArgumentDbgValue(const Value *V, MDNode *Variable,
Michael J. Spencere70c5262010-10-16 08:25:21 +00004455 int64_t Offset,
Dan Gohman5d11ea32010-05-01 00:33:16 +00004456 const SDValue &N) {
Devang Patel0b48ead2010-08-31 22:22:42 +00004457 const Argument *Arg = dyn_cast<Argument>(V);
4458 if (!Arg)
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004459 return false;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004460
Devang Patel719f6a92010-04-29 20:40:36 +00004461 MachineFunction &MF = DAG.getMachineFunction();
Devang Patela90b3052010-11-02 17:01:30 +00004462 const TargetInstrInfo *TII = DAG.getTarget().getInstrInfo();
Devang Patela90b3052010-11-02 17:01:30 +00004463
Devang Patela83ce982010-04-29 18:50:36 +00004464 // Ignore inlined function arguments here.
4465 DIVariable DV(Variable);
Devang Patel719f6a92010-04-29 20:40:36 +00004466 if (DV.isInlinedFnArgument(MF.getFunction()))
Devang Patela83ce982010-04-29 18:50:36 +00004467 return false;
4468
David Blaikie6d9dbd52013-06-16 20:34:15 +00004469 Optional<MachineOperand> Op;
Devang Patel9aee3352011-09-08 22:59:09 +00004470 // Some arguments' frame index is recorded during argument lowering.
David Blaikie6d9dbd52013-06-16 20:34:15 +00004471 if (int FI = FuncInfo.getArgumentFrameIndex(Arg))
4472 Op = MachineOperand::CreateFI(FI);
Devang Patel0b48ead2010-08-31 22:22:42 +00004473
David Blaikie6d9dbd52013-06-16 20:34:15 +00004474 if (!Op && N.getNode()) {
4475 unsigned Reg;
Devang Patel227dfdb2011-05-16 21:24:05 +00004476 if (N.getOpcode() == ISD::CopyFromReg)
4477 Reg = cast<RegisterSDNode>(N.getOperand(1))->getReg();
4478 else
4479 Reg = getTruncatedArgReg(N);
4480 if (Reg && TargetRegisterInfo::isVirtualRegister(Reg)) {
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004481 MachineRegisterInfo &RegInfo = MF.getRegInfo();
4482 unsigned PR = RegInfo.getLiveInPhysReg(Reg);
4483 if (PR)
4484 Reg = PR;
4485 }
David Blaikie6d9dbd52013-06-16 20:34:15 +00004486 if (Reg)
4487 Op = MachineOperand::CreateReg(Reg, false);
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004488 }
4489
David Blaikie6d9dbd52013-06-16 20:34:15 +00004490 if (!Op) {
Devang Patela90b3052010-11-02 17:01:30 +00004491 // Check if ValueMap has reg number.
Evan Chenga36acad2010-04-29 06:33:38 +00004492 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
Devang Patel8bc9ef72010-11-02 17:19:03 +00004493 if (VMI != FuncInfo.ValueMap.end())
David Blaikie6d9dbd52013-06-16 20:34:15 +00004494 Op = MachineOperand::CreateReg(VMI->second, false);
Evan Chenga36acad2010-04-29 06:33:38 +00004495 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004496
David Blaikie6d9dbd52013-06-16 20:34:15 +00004497 if (!Op && N.getNode())
Devang Patela90b3052010-11-02 17:01:30 +00004498 // Check if frame index is available.
4499 if (LoadSDNode *LNode = dyn_cast<LoadSDNode>(N.getNode()))
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004500 if (FrameIndexSDNode *FINode =
David Blaikie6d9dbd52013-06-16 20:34:15 +00004501 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
4502 Op = MachineOperand::CreateFI(FINode->getIndex());
Devang Patel8bc9ef72010-11-02 17:19:03 +00004503
David Blaikie6d9dbd52013-06-16 20:34:15 +00004504 if (!Op)
Devang Patel8bc9ef72010-11-02 17:19:03 +00004505 return false;
Devang Patela90b3052010-11-02 17:01:30 +00004506
Adrian Prantl893ae832013-07-10 01:53:30 +00004507 // FIXME: This does not handle register-indirect values at offset 0.
4508 bool IsIndirect = Offset != 0;
David Blaikie6d9dbd52013-06-16 20:34:15 +00004509 if (Op->isReg())
Adrian Prantl35176402013-07-09 20:28:37 +00004510 FuncInfo.ArgDbgValues.push_back(BuildMI(MF, getCurDebugLoc(),
4511 TII->get(TargetOpcode::DBG_VALUE),
Adrian Prantl893ae832013-07-10 01:53:30 +00004512 IsIndirect,
Adrian Prantl35176402013-07-09 20:28:37 +00004513 Op->getReg(), Offset, Variable));
4514 else
4515 FuncInfo.ArgDbgValues.push_back(
David Blaikie6d9dbd52013-06-16 20:34:15 +00004516 BuildMI(MF, getCurDebugLoc(), TII->get(TargetOpcode::DBG_VALUE))
4517 .addOperand(*Op).addImm(Offset).addMetadata(Variable));
Adrian Prantl35176402013-07-09 20:28:37 +00004518
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004519 return true;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00004520}
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004521
Douglas Gregor7d9663c2010-05-11 06:17:44 +00004522// VisualStudio defines setjmp as _setjmp
Michael J. Spencer1f409602010-09-24 19:48:47 +00004523#if defined(_MSC_VER) && defined(setjmp) && \
4524 !defined(setjmp_undefined_for_msvc)
4525# pragma push_macro("setjmp")
4526# undef setjmp
4527# define setjmp_undefined_for_msvc
Douglas Gregor7d9663c2010-05-11 06:17:44 +00004528#endif
4529
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004530/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
4531/// we want to emit this as a call to a named external function, return the name
4532/// otherwise lower it and return null.
4533const char *
Dan Gohman46510a72010-04-15 01:51:59 +00004534SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, unsigned Intrinsic) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00004535 const TargetLowering *TLI = TM.getTargetLowering();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004536 SDLoc sdl = getCurSDLoc();
Dale Johannesen66978ee2009-01-31 02:22:37 +00004537 DebugLoc dl = getCurDebugLoc();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004538 SDValue Res;
4539
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004540 switch (Intrinsic) {
4541 default:
4542 // By default, turn this into a target intrinsic node.
4543 visitTargetIntrinsic(I, Intrinsic);
4544 return 0;
4545 case Intrinsic::vastart: visitVAStart(I); return 0;
4546 case Intrinsic::vaend: visitVAEnd(I); return 0;
4547 case Intrinsic::vacopy: visitVACopy(I); return 0;
4548 case Intrinsic::returnaddress:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004549 setValue(&I, DAG.getNode(ISD::RETURNADDR, sdl, TLI->getPointerTy(),
Gabor Greif0635f352010-06-25 09:38:13 +00004550 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004551 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00004552 case Intrinsic::frameaddress:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004553 setValue(&I, DAG.getNode(ISD::FRAMEADDR, sdl, TLI->getPointerTy(),
Gabor Greif0635f352010-06-25 09:38:13 +00004554 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004555 return 0;
4556 case Intrinsic::setjmp:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004557 return &"_setjmp"[!TLI->usesUnderscoreSetJmp()];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004558 case Intrinsic::longjmp:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004559 return &"_longjmp"[!TLI->usesUnderscoreLongJmp()];
Chris Lattner824b9582008-11-21 16:42:48 +00004560 case Intrinsic::memcpy: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00004561 // Assert for address < 256 since we support only user defined address
4562 // spaces.
Gabor Greif0635f352010-06-25 09:38:13 +00004563 assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004564 < 256 &&
Gabor Greif0635f352010-06-25 09:38:13 +00004565 cast<PointerType>(I.getArgOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004566 < 256 &&
4567 "Unknown address space");
Gabor Greif0635f352010-06-25 09:38:13 +00004568 SDValue Op1 = getValue(I.getArgOperand(0));
4569 SDValue Op2 = getValue(I.getArgOperand(1));
4570 SDValue Op3 = getValue(I.getArgOperand(2));
4571 unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
Chandler Carruthaf23f8e2013-02-25 14:20:21 +00004572 if (!Align)
4573 Align = 1; // @llvm.memcpy defines 0 and 1 to both mean no alignment.
Gabor Greif0635f352010-06-25 09:38:13 +00004574 bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004575 DAG.setRoot(DAG.getMemcpy(getRoot(), sdl, Op1, Op2, Op3, Align, isVol, false,
Chris Lattnere72f2022010-09-21 05:40:29 +00004576 MachinePointerInfo(I.getArgOperand(0)),
4577 MachinePointerInfo(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004578 return 0;
4579 }
Chris Lattner824b9582008-11-21 16:42:48 +00004580 case Intrinsic::memset: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00004581 // Assert for address < 256 since we support only user defined address
4582 // spaces.
Gabor Greif0635f352010-06-25 09:38:13 +00004583 assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004584 < 256 &&
4585 "Unknown address space");
Gabor Greif0635f352010-06-25 09:38:13 +00004586 SDValue Op1 = getValue(I.getArgOperand(0));
4587 SDValue Op2 = getValue(I.getArgOperand(1));
4588 SDValue Op3 = getValue(I.getArgOperand(2));
4589 unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
Chandler Carruthaf23f8e2013-02-25 14:20:21 +00004590 if (!Align)
4591 Align = 1; // @llvm.memset defines 0 and 1 to both mean no alignment.
Gabor Greif0635f352010-06-25 09:38:13 +00004592 bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004593 DAG.setRoot(DAG.getMemset(getRoot(), sdl, Op1, Op2, Op3, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004594 MachinePointerInfo(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004595 return 0;
4596 }
Chris Lattner824b9582008-11-21 16:42:48 +00004597 case Intrinsic::memmove: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00004598 // Assert for address < 256 since we support only user defined address
4599 // spaces.
Gabor Greif0635f352010-06-25 09:38:13 +00004600 assert(cast<PointerType>(I.getArgOperand(0)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004601 < 256 &&
Gabor Greif0635f352010-06-25 09:38:13 +00004602 cast<PointerType>(I.getArgOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00004603 < 256 &&
4604 "Unknown address space");
Gabor Greif0635f352010-06-25 09:38:13 +00004605 SDValue Op1 = getValue(I.getArgOperand(0));
4606 SDValue Op2 = getValue(I.getArgOperand(1));
4607 SDValue Op3 = getValue(I.getArgOperand(2));
4608 unsigned Align = cast<ConstantInt>(I.getArgOperand(3))->getZExtValue();
Chandler Carruthaf23f8e2013-02-25 14:20:21 +00004609 if (!Align)
4610 Align = 1; // @llvm.memmove defines 0 and 1 to both mean no alignment.
Gabor Greif0635f352010-06-25 09:38:13 +00004611 bool isVol = cast<ConstantInt>(I.getArgOperand(4))->getZExtValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004612 DAG.setRoot(DAG.getMemmove(getRoot(), sdl, Op1, Op2, Op3, Align, isVol,
Chris Lattnere72f2022010-09-21 05:40:29 +00004613 MachinePointerInfo(I.getArgOperand(0)),
4614 MachinePointerInfo(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004615 return 0;
4616 }
Bill Wendling92c1e122009-02-13 02:16:35 +00004617 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00004618 const DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
Devang Patelac1ceb32009-10-09 22:42:28 +00004619 MDNode *Variable = DI.getVariable();
Dan Gohman46510a72010-04-15 01:51:59 +00004620 const Value *Address = DI.getAddress();
Manman Rencbafae62013-06-28 05:43:10 +00004621 DIVariable DIVar(Variable);
4622 assert((!DIVar || DIVar.isVariable()) &&
4623 "Variable in DbgDeclareInst should be either null or a DIVariable.");
4624 if (!Address || !DIVar) {
Eric Christopher8f2a88d2012-03-15 21:33:41 +00004625 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
Dale Johannesen8ac38f22010-02-08 21:53:27 +00004626 return 0;
Eric Christopher8f2a88d2012-03-15 21:33:41 +00004627 }
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004628
Devang Patel3f74a112010-09-02 21:29:42 +00004629 // Check if address has undef value.
4630 if (isa<UndefValue>(Address) ||
4631 (Address->use_empty() && !isa<Argument>(Address))) {
Eric Christopher24413672012-02-23 03:39:39 +00004632 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
Devang Patel3f74a112010-09-02 21:29:42 +00004633 return 0;
4634 }
4635
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004636 SDValue &N = NodeMap[Address];
Devang Patel0b48ead2010-08-31 22:22:42 +00004637 if (!N.getNode() && isa<Argument>(Address))
4638 // Check unused arguments map.
4639 N = UnusedArgNodeMap[Address];
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004640 SDDbgValue *SDV;
4641 if (N.getNode()) {
Devang Patel8e741ed2010-09-02 21:02:27 +00004642 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
4643 Address = BCI->getOperand(0);
Eric Christopher178606d2012-02-24 01:59:08 +00004644 // Parameters are handled specially.
4645 bool isParameter =
4646 (DIVariable(Variable).getTag() == dwarf::DW_TAG_arg_variable ||
4647 isa<Argument>(Address));
4648
Devang Patel8e741ed2010-09-02 21:02:27 +00004649 const AllocaInst *AI = dyn_cast<AllocaInst>(Address);
4650
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004651 if (isParameter && !AI) {
4652 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N.getNode());
4653 if (FINode)
4654 // Byval parameter. We have a frame index at this point.
4655 SDV = DAG.getDbgValue(Variable, FINode->getIndex(),
4656 0, dl, SDNodeOrder);
Devang Patelafeaae72010-12-06 22:39:26 +00004657 else {
Devang Patel227dfdb2011-05-16 21:24:05 +00004658 // Address is an argument, so try to emit its dbg value using
4659 // virtual register info from the FuncInfo.ValueMap.
4660 EmitFuncArgumentDbgValue(Address, Variable, 0, N);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004661 return 0;
Devang Patelafeaae72010-12-06 22:39:26 +00004662 }
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004663 } else if (AI)
4664 SDV = DAG.getDbgValue(Variable, N.getNode(), N.getResNo(),
4665 0, dl, SDNodeOrder);
Devang Patelafeaae72010-12-06 22:39:26 +00004666 else {
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004667 // Can't do anything with other non-AI cases yet.
Eric Christopher24413672012-02-23 03:39:39 +00004668 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
Eric Christopher178606d2012-02-24 01:59:08 +00004669 DEBUG(dbgs() << "non-AllocaInst issue for Address: \n\t");
4670 DEBUG(Address->dump());
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004671 return 0;
Devang Patelafeaae72010-12-06 22:39:26 +00004672 }
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004673 DAG.AddDbgValue(SDV, N.getNode(), isParameter);
4674 } else {
Gabor Greiffb4032f2010-10-01 10:32:19 +00004675 // If Address is an argument then try to emit its dbg value using
Michael J. Spencere70c5262010-10-16 08:25:21 +00004676 // virtual register info from the FuncInfo.ValueMap.
Devang Patel6cd467b2010-08-26 22:53:27 +00004677 if (!EmitFuncArgumentDbgValue(Address, Variable, 0, N)) {
Devang Patel1397fdc2010-09-15 14:48:53 +00004678 // If variable is pinned by a alloca in dominating bb then
4679 // use StaticAllocaMap.
4680 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Address)) {
Devang Patel27ede1b2010-09-15 18:13:55 +00004681 if (AI->getParent() != DI.getParent()) {
4682 DenseMap<const AllocaInst*, int>::iterator SI =
4683 FuncInfo.StaticAllocaMap.find(AI);
4684 if (SI != FuncInfo.StaticAllocaMap.end()) {
4685 SDV = DAG.getDbgValue(Variable, SI->second,
4686 0, dl, SDNodeOrder);
4687 DAG.AddDbgValue(SDV, 0, false);
4688 return 0;
4689 }
Devang Patel1397fdc2010-09-15 14:48:53 +00004690 }
4691 }
Eric Christopher0822e012012-02-23 03:39:43 +00004692 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
Devang Patel6cd467b2010-08-26 22:53:27 +00004693 }
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004694 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004695 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00004696 }
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004697 case Intrinsic::dbg_value: {
Dan Gohman46510a72010-04-15 01:51:59 +00004698 const DbgValueInst &DI = cast<DbgValueInst>(I);
Manman Rencbafae62013-06-28 05:43:10 +00004699 DIVariable DIVar(DI.getVariable());
4700 assert((!DIVar || DIVar.isVariable()) &&
4701 "Variable in DbgValueInst should be either null or a DIVariable.");
4702 if (!DIVar)
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004703 return 0;
4704
4705 MDNode *Variable = DI.getVariable();
Devang Patel00190342010-03-15 19:15:44 +00004706 uint64_t Offset = DI.getOffset();
Dan Gohman46510a72010-04-15 01:51:59 +00004707 const Value *V = DI.getValue();
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004708 if (!V)
4709 return 0;
Devang Patel00190342010-03-15 19:15:44 +00004710
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004711 SDDbgValue *SDV;
Devang Patel57871242011-08-03 23:13:55 +00004712 if (isa<ConstantInt>(V) || isa<ConstantFP>(V) || isa<UndefValue>(V)) {
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004713 SDV = DAG.getDbgValue(Variable, V, Offset, dl, SDNodeOrder);
4714 DAG.AddDbgValue(SDV, 0, false);
Devang Patel00190342010-03-15 19:15:44 +00004715 } else {
Dale Johannesenbdc09d92010-07-16 00:02:08 +00004716 // Do not use getValue() in here; we don't want to generate code at
4717 // this point if it hasn't been done yet.
Devang Patel9126c0d2010-06-01 19:59:01 +00004718 SDValue N = NodeMap[V];
4719 if (!N.getNode() && isa<Argument>(V))
4720 // Check unused arguments map.
4721 N = UnusedArgNodeMap[V];
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004722 if (N.getNode()) {
Devang Patel78a06e52010-08-25 20:39:26 +00004723 if (!EmitFuncArgumentDbgValue(V, Variable, Offset, N)) {
Evan Cheng9e8a2b92010-04-29 01:40:30 +00004724 SDV = DAG.getDbgValue(Variable, N.getNode(),
4725 N.getResNo(), Offset, dl, SDNodeOrder);
4726 DAG.AddDbgValue(SDV, N.getNode(), false);
4727 }
Devang Patela778f5c2011-02-18 22:43:42 +00004728 } else if (!V->use_empty() ) {
Dale Johannesenbdc09d92010-07-16 00:02:08 +00004729 // Do not call getValue(V) yet, as we don't want to generate code.
4730 // Remember it for later.
4731 DanglingDebugInfo DDI(&DI, dl, SDNodeOrder);
4732 DanglingDebugInfoMap[V] = DDI;
Devang Patel0991dfb2010-08-27 22:25:51 +00004733 } else {
Devang Patel00190342010-03-15 19:15:44 +00004734 // We may expand this to cover more cases. One case where we have no
Devang Patelafeaae72010-12-06 22:39:26 +00004735 // data available is an unreferenced parameter.
Eric Christopher0822e012012-02-23 03:39:43 +00004736 DEBUG(dbgs() << "Dropping debug info for " << DI << "\n");
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00004737 }
Devang Patel00190342010-03-15 19:15:44 +00004738 }
4739
4740 // Build a debug info table entry.
Dan Gohman46510a72010-04-15 01:51:59 +00004741 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(V))
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004742 V = BCI->getOperand(0);
Dan Gohman46510a72010-04-15 01:51:59 +00004743 const AllocaInst *AI = dyn_cast<AllocaInst>(V);
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004744 // Don't handle byval struct arguments or VLAs, for example.
Eric Christopher7e1e18f2012-03-26 06:10:32 +00004745 if (!AI) {
Eric Christopher9fc5c832012-03-28 07:34:36 +00004746 DEBUG(dbgs() << "Dropping debug location info for:\n " << DI << "\n");
4747 DEBUG(dbgs() << " Last seen at:\n " << *V << "\n");
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004748 return 0;
Eric Christopher7e1e18f2012-03-26 06:10:32 +00004749 }
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004750 DenseMap<const AllocaInst*, int>::iterator SI =
4751 FuncInfo.StaticAllocaMap.find(AI);
4752 if (SI == FuncInfo.StaticAllocaMap.end())
4753 return 0; // VLAs.
4754 int FI = SI->second;
Michael J. Spencere70c5262010-10-16 08:25:21 +00004755
Chris Lattner512063d2010-04-05 06:19:28 +00004756 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
4757 if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo())
4758 MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc());
Dale Johannesen904c2fa2010-02-01 19:54:53 +00004759 return 0;
4760 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004761
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004762 case Intrinsic::eh_typeid_for: {
Chris Lattner512063d2010-04-05 06:19:28 +00004763 // Find the type id for the given typeinfo.
Gabor Greif0635f352010-06-25 09:38:13 +00004764 GlobalVariable *GV = ExtractTypeInfo(I.getArgOperand(0));
Chris Lattner512063d2010-04-05 06:19:28 +00004765 unsigned TypeID = DAG.getMachineFunction().getMMI().getTypeIDFor(GV);
4766 Res = DAG.getConstant(TypeID, MVT::i32);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004767 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004768 return 0;
4769 }
4770
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004771 case Intrinsic::eh_return_i32:
4772 case Intrinsic::eh_return_i64:
Chris Lattner512063d2010-04-05 06:19:28 +00004773 DAG.getMachineFunction().getMMI().setCallsEHReturn(true);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004774 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, sdl,
Chris Lattner512063d2010-04-05 06:19:28 +00004775 MVT::Other,
4776 getControlRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00004777 getValue(I.getArgOperand(0)),
4778 getValue(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004779 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004780 case Intrinsic::eh_unwind_init:
Chris Lattner512063d2010-04-05 06:19:28 +00004781 DAG.getMachineFunction().getMMI().setCallsUnwindInit(true);
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004782 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004783 case Intrinsic::eh_dwarf_cfa: {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004784 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getArgOperand(0)), sdl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00004785 TLI->getPointerTy());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004786 SDValue Offset = DAG.getNode(ISD::ADD, sdl,
Tom Stellardedd08f72013-08-26 15:06:10 +00004787 CfaArg.getValueType(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004788 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, sdl,
Tom Stellardedd08f72013-08-26 15:06:10 +00004789 CfaArg.getValueType()),
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004790 CfaArg);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004791 SDValue FA = DAG.getNode(ISD::FRAMEADDR, sdl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00004792 TLI->getPointerTy(),
4793 DAG.getConstant(0, TLI->getPointerTy()));
Tom Stellardedd08f72013-08-26 15:06:10 +00004794 setValue(&I, DAG.getNode(ISD::ADD, sdl, FA.getValueType(),
Bill Wendling4533cac2010-01-28 21:51:40 +00004795 FA, Offset));
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004796 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004797 }
Jim Grosbachca752c92010-01-28 01:45:32 +00004798 case Intrinsic::eh_sjlj_callsite: {
Chris Lattner512063d2010-04-05 06:19:28 +00004799 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Gabor Greif0635f352010-06-25 09:38:13 +00004800 ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(0));
Jim Grosbachca752c92010-01-28 01:45:32 +00004801 assert(CI && "Non-constant call site value in eh.sjlj.callsite!");
Chris Lattner512063d2010-04-05 06:19:28 +00004802 assert(MMI.getCurrentCallSite() == 0 && "Overlapping call sites!");
Jim Grosbachca752c92010-01-28 01:45:32 +00004803
Chris Lattner512063d2010-04-05 06:19:28 +00004804 MMI.setCurrentCallSite(CI->getZExtValue());
Jim Grosbachca752c92010-01-28 01:45:32 +00004805 return 0;
4806 }
Bill Wendling6ef94172011-09-28 03:36:43 +00004807 case Intrinsic::eh_sjlj_functioncontext: {
4808 // Get and store the index of the function context.
4809 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bill Wendlingadbf7b22011-09-28 03:52:41 +00004810 AllocaInst *FnCtx =
4811 cast<AllocaInst>(I.getArgOperand(0)->stripPointerCasts());
Bill Wendling6ef94172011-09-28 03:36:43 +00004812 int FI = FuncInfo.StaticAllocaMap[FnCtx];
4813 MFI->setFunctionContextIndex(FI);
4814 return 0;
4815 }
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004816 case Intrinsic::eh_sjlj_setjmp: {
Bill Wendlingce370cf2011-10-07 21:25:38 +00004817 SDValue Ops[2];
4818 Ops[0] = getRoot();
4819 Ops[1] = getValue(I.getArgOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004820 SDValue Op = DAG.getNode(ISD::EH_SJLJ_SETJMP, sdl,
Bill Wendlingce370cf2011-10-07 21:25:38 +00004821 DAG.getVTList(MVT::i32, MVT::Other),
4822 Ops, 2);
4823 setValue(&I, Op.getValue(0));
4824 DAG.setRoot(Op.getValue(1));
Jim Grosbach23ff7cf2010-05-26 20:22:18 +00004825 return 0;
4826 }
Jim Grosbach5eb19512010-05-22 01:06:18 +00004827 case Intrinsic::eh_sjlj_longjmp: {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004828 DAG.setRoot(DAG.getNode(ISD::EH_SJLJ_LONGJMP, sdl, MVT::Other,
Jim Grosbache4ad3872010-10-19 23:27:08 +00004829 getRoot(), getValue(I.getArgOperand(0))));
4830 return 0;
4831 }
Jim Grosbachca752c92010-01-28 01:45:32 +00004832
Dale Johannesen0488fb62010-09-30 23:57:10 +00004833 case Intrinsic::x86_mmx_pslli_w:
4834 case Intrinsic::x86_mmx_pslli_d:
4835 case Intrinsic::x86_mmx_pslli_q:
4836 case Intrinsic::x86_mmx_psrli_w:
4837 case Intrinsic::x86_mmx_psrli_d:
4838 case Intrinsic::x86_mmx_psrli_q:
4839 case Intrinsic::x86_mmx_psrai_w:
4840 case Intrinsic::x86_mmx_psrai_d: {
4841 SDValue ShAmt = getValue(I.getArgOperand(1));
4842 if (isa<ConstantSDNode>(ShAmt)) {
4843 visitTargetIntrinsic(I, Intrinsic);
4844 return 0;
4845 }
4846 unsigned NewIntrinsic = 0;
4847 EVT ShAmtVT = MVT::v2i32;
4848 switch (Intrinsic) {
4849 case Intrinsic::x86_mmx_pslli_w:
4850 NewIntrinsic = Intrinsic::x86_mmx_psll_w;
4851 break;
4852 case Intrinsic::x86_mmx_pslli_d:
4853 NewIntrinsic = Intrinsic::x86_mmx_psll_d;
4854 break;
4855 case Intrinsic::x86_mmx_pslli_q:
4856 NewIntrinsic = Intrinsic::x86_mmx_psll_q;
4857 break;
4858 case Intrinsic::x86_mmx_psrli_w:
4859 NewIntrinsic = Intrinsic::x86_mmx_psrl_w;
4860 break;
4861 case Intrinsic::x86_mmx_psrli_d:
4862 NewIntrinsic = Intrinsic::x86_mmx_psrl_d;
4863 break;
4864 case Intrinsic::x86_mmx_psrli_q:
4865 NewIntrinsic = Intrinsic::x86_mmx_psrl_q;
4866 break;
4867 case Intrinsic::x86_mmx_psrai_w:
4868 NewIntrinsic = Intrinsic::x86_mmx_psra_w;
4869 break;
4870 case Intrinsic::x86_mmx_psrai_d:
4871 NewIntrinsic = Intrinsic::x86_mmx_psra_d;
4872 break;
4873 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
4874 }
4875
4876 // The vector shift intrinsics with scalars uses 32b shift amounts but
4877 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
4878 // to be zero.
4879 // We must do this early because v2i32 is not a legal type.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004880 SDValue ShOps[2];
4881 ShOps[0] = ShAmt;
4882 ShOps[1] = DAG.getConstant(0, MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004883 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, sdl, ShAmtVT, &ShOps[0], 2);
Bill Wendlingba54bca2013-06-19 21:36:55 +00004884 EVT DestVT = TLI->getValueType(I.getType());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004885 ShAmt = DAG.getNode(ISD::BITCAST, sdl, DestVT, ShAmt);
4886 Res = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, sdl, DestVT,
Dale Johannesen0488fb62010-09-30 23:57:10 +00004887 DAG.getConstant(NewIntrinsic, MVT::i32),
4888 getValue(I.getArgOperand(0)), ShAmt);
4889 setValue(&I, Res);
4890 return 0;
4891 }
Pete Cooperd18134f2012-02-24 03:51:49 +00004892 case Intrinsic::x86_avx_vinsertf128_pd_256:
4893 case Intrinsic::x86_avx_vinsertf128_ps_256:
Craig Topperb45c9692012-04-07 22:32:29 +00004894 case Intrinsic::x86_avx_vinsertf128_si_256:
4895 case Intrinsic::x86_avx2_vinserti128: {
Bill Wendlingba54bca2013-06-19 21:36:55 +00004896 EVT DestVT = TLI->getValueType(I.getType());
4897 EVT ElVT = TLI->getValueType(I.getArgOperand(1)->getType());
Pete Cooperd18134f2012-02-24 03:51:49 +00004898 uint64_t Idx = (cast<ConstantInt>(I.getArgOperand(2))->getZExtValue() & 1) *
4899 ElVT.getVectorNumElements();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004900 Res = DAG.getNode(ISD::INSERT_SUBVECTOR, sdl, DestVT,
Pete Cooperd18134f2012-02-24 03:51:49 +00004901 getValue(I.getArgOperand(0)),
4902 getValue(I.getArgOperand(1)),
Tom Stellard425b76c2013-08-05 22:22:01 +00004903 DAG.getConstant(Idx, TLI->getVectorIdxTy()));
Craig Topperf6dc7922012-09-05 05:48:09 +00004904 setValue(&I, Res);
4905 return 0;
4906 }
4907 case Intrinsic::x86_avx_vextractf128_pd_256:
4908 case Intrinsic::x86_avx_vextractf128_ps_256:
4909 case Intrinsic::x86_avx_vextractf128_si_256:
4910 case Intrinsic::x86_avx2_vextracti128: {
Bill Wendlingba54bca2013-06-19 21:36:55 +00004911 EVT DestVT = TLI->getValueType(I.getType());
Craig Topperf6dc7922012-09-05 05:48:09 +00004912 uint64_t Idx = (cast<ConstantInt>(I.getArgOperand(1))->getZExtValue() & 1) *
4913 DestVT.getVectorNumElements();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004914 Res = DAG.getNode(ISD::EXTRACT_SUBVECTOR, sdl, DestVT,
Craig Topperf6dc7922012-09-05 05:48:09 +00004915 getValue(I.getArgOperand(0)),
Tom Stellard425b76c2013-08-05 22:22:01 +00004916 DAG.getConstant(Idx, TLI->getVectorIdxTy()));
Pete Cooperd18134f2012-02-24 03:51:49 +00004917 setValue(&I, Res);
4918 return 0;
4919 }
Mon P Wang77cdf302008-11-10 20:54:11 +00004920 case Intrinsic::convertff:
4921 case Intrinsic::convertfsi:
4922 case Intrinsic::convertfui:
4923 case Intrinsic::convertsif:
4924 case Intrinsic::convertuif:
4925 case Intrinsic::convertss:
4926 case Intrinsic::convertsu:
4927 case Intrinsic::convertus:
4928 case Intrinsic::convertuu: {
4929 ISD::CvtCode Code = ISD::CVT_INVALID;
4930 switch (Intrinsic) {
Craig Topperc42e6402012-04-11 04:34:11 +00004931 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Mon P Wang77cdf302008-11-10 20:54:11 +00004932 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
4933 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
4934 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
4935 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
4936 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
4937 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
4938 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
4939 case Intrinsic::convertus: Code = ISD::CVT_US; break;
4940 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
4941 }
Bill Wendlingba54bca2013-06-19 21:36:55 +00004942 EVT DestVT = TLI->getValueType(I.getType());
Gabor Greif0635f352010-06-25 09:38:13 +00004943 const Value *Op1 = I.getArgOperand(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004944 Res = DAG.getConvertRndSat(DestVT, sdl, getValue(Op1),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004945 DAG.getValueType(DestVT),
4946 DAG.getValueType(getValue(Op1).getValueType()),
Gabor Greif0635f352010-06-25 09:38:13 +00004947 getValue(I.getArgOperand(1)),
4948 getValue(I.getArgOperand(2)),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004949 Code);
4950 setValue(&I, Res);
Mon P Wang77cdf302008-11-10 20:54:11 +00004951 return 0;
4952 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004953 case Intrinsic::powi:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004954 setValue(&I, ExpandPowI(sdl, getValue(I.getArgOperand(0)),
Gabor Greif0635f352010-06-25 09:38:13 +00004955 getValue(I.getArgOperand(1)), DAG));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004956 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004957 case Intrinsic::log:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004958 setValue(&I, expandLog(sdl, getValue(I.getArgOperand(0)), DAG, *TLI));
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004959 return 0;
4960 case Intrinsic::log2:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004961 setValue(&I, expandLog2(sdl, getValue(I.getArgOperand(0)), DAG, *TLI));
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004962 return 0;
4963 case Intrinsic::log10:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004964 setValue(&I, expandLog10(sdl, getValue(I.getArgOperand(0)), DAG, *TLI));
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004965 return 0;
4966 case Intrinsic::exp:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004967 setValue(&I, expandExp(sdl, getValue(I.getArgOperand(0)), DAG, *TLI));
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004968 return 0;
4969 case Intrinsic::exp2:
Bill Wendlingba54bca2013-06-19 21:36:55 +00004970 setValue(&I, expandExp2(sdl, getValue(I.getArgOperand(0)), DAG, *TLI));
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004971 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004972 case Intrinsic::pow:
Andrew Trickac6d9be2013-05-25 02:42:55 +00004973 setValue(&I, expandPow(sdl, getValue(I.getArgOperand(0)),
Bill Wendlingba54bca2013-06-19 21:36:55 +00004974 getValue(I.getArgOperand(1)), DAG, *TLI));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004975 return 0;
Craig Topper9bd4dd72012-11-16 07:48:23 +00004976 case Intrinsic::sqrt:
Peter Collingbourneb34d3aa2012-05-28 21:48:37 +00004977 case Intrinsic::fabs:
Craig Topper9bd4dd72012-11-16 07:48:23 +00004978 case Intrinsic::sin:
4979 case Intrinsic::cos:
Dan Gohman27db99f2012-07-26 17:43:27 +00004980 case Intrinsic::floor:
Craig Topper49010472012-11-15 06:51:10 +00004981 case Intrinsic::ceil:
Craig Topper49010472012-11-15 06:51:10 +00004982 case Intrinsic::trunc:
Craig Topper49010472012-11-15 06:51:10 +00004983 case Intrinsic::rint:
Hal Finkel41418d12013-08-07 22:49:12 +00004984 case Intrinsic::nearbyint:
4985 case Intrinsic::round: {
Craig Topper9bd4dd72012-11-16 07:48:23 +00004986 unsigned Opcode;
4987 switch (Intrinsic) {
4988 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
4989 case Intrinsic::sqrt: Opcode = ISD::FSQRT; break;
4990 case Intrinsic::fabs: Opcode = ISD::FABS; break;
4991 case Intrinsic::sin: Opcode = ISD::FSIN; break;
4992 case Intrinsic::cos: Opcode = ISD::FCOS; break;
4993 case Intrinsic::floor: Opcode = ISD::FFLOOR; break;
4994 case Intrinsic::ceil: Opcode = ISD::FCEIL; break;
4995 case Intrinsic::trunc: Opcode = ISD::FTRUNC; break;
4996 case Intrinsic::rint: Opcode = ISD::FRINT; break;
4997 case Intrinsic::nearbyint: Opcode = ISD::FNEARBYINT; break;
Hal Finkel41418d12013-08-07 22:49:12 +00004998 case Intrinsic::round: Opcode = ISD::FROUND; break;
Craig Topper9bd4dd72012-11-16 07:48:23 +00004999 }
5000
Andrew Trickac6d9be2013-05-25 02:42:55 +00005001 setValue(&I, DAG.getNode(Opcode, sdl,
Craig Topper49010472012-11-15 06:51:10 +00005002 getValue(I.getArgOperand(0)).getValueType(),
5003 getValue(I.getArgOperand(0))));
5004 return 0;
Craig Topper9bd4dd72012-11-16 07:48:23 +00005005 }
Hal Finkel66d1fa62013-08-19 23:35:46 +00005006 case Intrinsic::copysign:
5007 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, sdl,
5008 getValue(I.getArgOperand(0)).getValueType(),
5009 getValue(I.getArgOperand(0)),
5010 getValue(I.getArgOperand(1))));
5011 return 0;
Cameron Zwarich33390842011-07-08 21:39:21 +00005012 case Intrinsic::fma:
Andrew Trickac6d9be2013-05-25 02:42:55 +00005013 setValue(&I, DAG.getNode(ISD::FMA, sdl,
Cameron Zwarich33390842011-07-08 21:39:21 +00005014 getValue(I.getArgOperand(0)).getValueType(),
5015 getValue(I.getArgOperand(0)),
5016 getValue(I.getArgOperand(1)),
5017 getValue(I.getArgOperand(2))));
5018 return 0;
Lang Hames5afba6f2012-06-05 19:07:46 +00005019 case Intrinsic::fmuladd: {
Bill Wendlingba54bca2013-06-19 21:36:55 +00005020 EVT VT = TLI->getValueType(I.getType());
Lang Hamese0231412012-06-22 01:09:09 +00005021 if (TM.Options.AllowFPOpFusion != FPOpFusion::Strict &&
Stephen Line54885a2013-07-09 18:16:56 +00005022 TLI->isFMAFasterThanFMulAndFAdd(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005023 setValue(&I, DAG.getNode(ISD::FMA, sdl,
Lang Hames5afba6f2012-06-05 19:07:46 +00005024 getValue(I.getArgOperand(0)).getValueType(),
5025 getValue(I.getArgOperand(0)),
5026 getValue(I.getArgOperand(1)),
5027 getValue(I.getArgOperand(2))));
5028 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005029 SDValue Mul = DAG.getNode(ISD::FMUL, sdl,
Lang Hames5afba6f2012-06-05 19:07:46 +00005030 getValue(I.getArgOperand(0)).getValueType(),
5031 getValue(I.getArgOperand(0)),
5032 getValue(I.getArgOperand(1)));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005033 SDValue Add = DAG.getNode(ISD::FADD, sdl,
Lang Hames5afba6f2012-06-05 19:07:46 +00005034 getValue(I.getArgOperand(0)).getValueType(),
5035 Mul,
5036 getValue(I.getArgOperand(2)));
5037 setValue(&I, Add);
5038 }
5039 return 0;
5040 }
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00005041 case Intrinsic::convert_to_fp16:
Andrew Trickac6d9be2013-05-25 02:42:55 +00005042 setValue(&I, DAG.getNode(ISD::FP32_TO_FP16, sdl,
Gabor Greif0635f352010-06-25 09:38:13 +00005043 MVT::i16, getValue(I.getArgOperand(0))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00005044 return 0;
5045 case Intrinsic::convert_from_fp16:
Andrew Trickac6d9be2013-05-25 02:42:55 +00005046 setValue(&I, DAG.getNode(ISD::FP16_TO_FP32, sdl,
Gabor Greif0635f352010-06-25 09:38:13 +00005047 MVT::f32, getValue(I.getArgOperand(0))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00005048 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005049 case Intrinsic::pcmarker: {
Gabor Greif0635f352010-06-25 09:38:13 +00005050 SDValue Tmp = getValue(I.getArgOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005051 DAG.setRoot(DAG.getNode(ISD::PCMARKER, sdl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005052 return 0;
5053 }
5054 case Intrinsic::readcyclecounter: {
5055 SDValue Op = getRoot();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005056 Res = DAG.getNode(ISD::READCYCLECOUNTER, sdl,
Bill Wendlingd0283fa2009-12-22 00:40:51 +00005057 DAG.getVTList(MVT::i64, MVT::Other),
5058 &Op, 1);
5059 setValue(&I, Res);
5060 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005061 return 0;
5062 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005063 case Intrinsic::bswap:
Andrew Trickac6d9be2013-05-25 02:42:55 +00005064 setValue(&I, DAG.getNode(ISD::BSWAP, sdl,
Gabor Greif0635f352010-06-25 09:38:13 +00005065 getValue(I.getArgOperand(0)).getValueType(),
5066 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005067 return 0;
5068 case Intrinsic::cttz: {
Gabor Greif0635f352010-06-25 09:38:13 +00005069 SDValue Arg = getValue(I.getArgOperand(0));
Chandler Carruth63974b22011-12-13 01:56:10 +00005070 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00005071 EVT Ty = Arg.getValueType();
Chandler Carruth63974b22011-12-13 01:56:10 +00005072 setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTTZ : ISD::CTTZ_ZERO_UNDEF,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005073 sdl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005074 return 0;
5075 }
5076 case Intrinsic::ctlz: {
Gabor Greif0635f352010-06-25 09:38:13 +00005077 SDValue Arg = getValue(I.getArgOperand(0));
Chandler Carruth63974b22011-12-13 01:56:10 +00005078 ConstantInt *CI = cast<ConstantInt>(I.getArgOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00005079 EVT Ty = Arg.getValueType();
Chandler Carruth63974b22011-12-13 01:56:10 +00005080 setValue(&I, DAG.getNode(CI->isZero() ? ISD::CTLZ : ISD::CTLZ_ZERO_UNDEF,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005081 sdl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005082 return 0;
5083 }
5084 case Intrinsic::ctpop: {
Gabor Greif0635f352010-06-25 09:38:13 +00005085 SDValue Arg = getValue(I.getArgOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00005086 EVT Ty = Arg.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005087 setValue(&I, DAG.getNode(ISD::CTPOP, sdl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005088 return 0;
5089 }
5090 case Intrinsic::stacksave: {
5091 SDValue Op = getRoot();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005092 Res = DAG.getNode(ISD::STACKSAVE, sdl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00005093 DAG.getVTList(TLI->getPointerTy(), MVT::Other), &Op, 1);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00005094 setValue(&I, Res);
5095 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005096 return 0;
5097 }
5098 case Intrinsic::stackrestore: {
Gabor Greif0635f352010-06-25 09:38:13 +00005099 Res = getValue(I.getArgOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005100 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, sdl, MVT::Other, getRoot(), Res));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005101 return 0;
5102 }
Bill Wendling57344502008-11-18 11:01:33 +00005103 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00005104 // Emit code into the DAG to store the stack guard onto the stack.
5105 MachineFunction &MF = DAG.getMachineFunction();
5106 MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendlingba54bca2013-06-19 21:36:55 +00005107 EVT PtrTy = TLI->getPointerTy();
Bill Wendlingb2a42982008-11-06 02:29:10 +00005108
Gabor Greif0635f352010-06-25 09:38:13 +00005109 SDValue Src = getValue(I.getArgOperand(0)); // The guard's value.
5110 AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Bill Wendlingb2a42982008-11-06 02:29:10 +00005111
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00005112 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00005113 MFI->setStackProtectorIndex(FI);
5114
5115 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
5116
5117 // Store the stack protector onto the stack.
Andrew Trickac6d9be2013-05-25 02:42:55 +00005118 Res = DAG.getStore(getRoot(), sdl, Src, FIN,
Chris Lattner84bd98a2010-09-21 18:58:22 +00005119 MachinePointerInfo::getFixedStack(FI),
5120 true, false, 0);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00005121 setValue(&I, Res);
5122 DAG.setRoot(Res);
Bill Wendlingb2a42982008-11-06 02:29:10 +00005123 return 0;
5124 }
Eric Christopher7b5e6172009-10-27 00:52:25 +00005125 case Intrinsic::objectsize: {
5126 // If we don't know by now, we're never going to know.
Gabor Greif0635f352010-06-25 09:38:13 +00005127 ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(1));
Eric Christopher7b5e6172009-10-27 00:52:25 +00005128
5129 assert(CI && "Non-constant type in __builtin_object_size?");
5130
Gabor Greif0635f352010-06-25 09:38:13 +00005131 SDValue Arg = getValue(I.getCalledValue());
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00005132 EVT Ty = Arg.getValueType();
5133
Dan Gohmane368b462010-06-18 14:22:04 +00005134 if (CI->isZero())
Bill Wendlingd0283fa2009-12-22 00:40:51 +00005135 Res = DAG.getConstant(-1ULL, Ty);
Eric Christopher7b5e6172009-10-27 00:52:25 +00005136 else
Bill Wendlingd0283fa2009-12-22 00:40:51 +00005137 Res = DAG.getConstant(0, Ty);
5138
5139 setValue(&I, Res);
Eric Christopher7b5e6172009-10-27 00:52:25 +00005140 return 0;
5141 }
Justin Holewinskic2b7f5f2013-05-21 14:37:16 +00005142 case Intrinsic::annotation:
5143 case Intrinsic::ptr_annotation:
5144 // Drop the intrinsic, but forward the value
5145 setValue(&I, getValue(I.getOperand(0)));
5146 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005147 case Intrinsic::var_annotation:
5148 // Discard annotate attributes
5149 return 0;
5150
5151 case Intrinsic::init_trampoline: {
Gabor Greif0635f352010-06-25 09:38:13 +00005152 const Function *F = cast<Function>(I.getArgOperand(1)->stripPointerCasts());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005153
5154 SDValue Ops[6];
5155 Ops[0] = getRoot();
Gabor Greif0635f352010-06-25 09:38:13 +00005156 Ops[1] = getValue(I.getArgOperand(0));
5157 Ops[2] = getValue(I.getArgOperand(1));
5158 Ops[3] = getValue(I.getArgOperand(2));
5159 Ops[4] = DAG.getSrcValue(I.getArgOperand(0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005160 Ops[5] = DAG.getSrcValue(F);
5161
Andrew Trickac6d9be2013-05-25 02:42:55 +00005162 Res = DAG.getNode(ISD::INIT_TRAMPOLINE, sdl, MVT::Other, Ops, 6);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005163
Duncan Sands4a544a72011-09-06 13:37:06 +00005164 DAG.setRoot(Res);
5165 return 0;
5166 }
5167 case Intrinsic::adjust_trampoline: {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005168 setValue(&I, DAG.getNode(ISD::ADJUST_TRAMPOLINE, sdl,
Bill Wendlingba54bca2013-06-19 21:36:55 +00005169 TLI->getPointerTy(),
Duncan Sands4a544a72011-09-06 13:37:06 +00005170 getValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005171 return 0;
5172 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005173 case Intrinsic::gcroot:
5174 if (GFI) {
Bill Wendling95dd4422012-05-01 22:50:45 +00005175 const Value *Alloca = I.getArgOperand(0)->stripPointerCasts();
Gabor Greif0635f352010-06-25 09:38:13 +00005176 const Constant *TypeMap = cast<Constant>(I.getArgOperand(1));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005177
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005178 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
5179 GFI->addStackRoot(FI->getIndex(), TypeMap);
5180 }
5181 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005182 case Intrinsic::gcread:
5183 case Intrinsic::gcwrite:
Torok Edwinc23197a2009-07-14 16:55:14 +00005184 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
Bill Wendlingd0283fa2009-12-22 00:40:51 +00005185 case Intrinsic::flt_rounds:
Andrew Trickac6d9be2013-05-25 02:42:55 +00005186 setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, sdl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005187 return 0;
Jakub Staszak9da99342011-07-06 18:22:43 +00005188
5189 case Intrinsic::expect: {
5190 // Just replace __builtin_expect(exp, c) with EXP.
5191 setValue(&I, getValue(I.getArgOperand(0)));
5192 return 0;
5193 }
5194
Shuxin Yang970755e2012-10-19 20:11:16 +00005195 case Intrinsic::debugtrap:
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005196 case Intrinsic::trap: {
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005197 StringRef TrapFuncName = TM.Options.getTrapFunctionName();
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005198 if (TrapFuncName.empty()) {
Stephen Lin155615d2013-07-08 00:37:03 +00005199 ISD::NodeType Op = (Intrinsic == Intrinsic::trap) ?
Shuxin Yang970755e2012-10-19 20:11:16 +00005200 ISD::TRAP : ISD::DEBUGTRAP;
Andrew Trickac6d9be2013-05-25 02:42:55 +00005201 DAG.setRoot(DAG.getNode(Op, sdl,MVT::Other, getRoot()));
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005202 return 0;
5203 }
5204 TargetLowering::ArgListTy Args;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00005205 TargetLowering::
5206 CallLoweringInfo CLI(getRoot(), I.getType(),
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005207 false, false, false, false, 0, CallingConv::C,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00005208 /*isTailCall=*/false,
5209 /*doesNotRet=*/false, /*isReturnValueUsed=*/true,
Bill Wendlingba54bca2013-06-19 21:36:55 +00005210 DAG.getExternalSymbol(TrapFuncName.data(),
5211 TLI->getPointerTy()),
Andrew Trickac6d9be2013-05-25 02:42:55 +00005212 Args, DAG, sdl);
Bill Wendlingba54bca2013-06-19 21:36:55 +00005213 std::pair<SDValue, SDValue> Result = TLI->LowerCallTo(CLI);
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005214 DAG.setRoot(Result.second);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005215 return 0;
Evan Cheng4da0c7c2011-04-08 21:37:21 +00005216 }
Shuxin Yang970755e2012-10-19 20:11:16 +00005217
Bill Wendlingef375462008-11-21 02:38:44 +00005218 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00005219 case Intrinsic::sadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00005220 case Intrinsic::usub_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00005221 case Intrinsic::ssub_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00005222 case Intrinsic::umul_with_overflow:
Craig Topperc42e6402012-04-11 04:34:11 +00005223 case Intrinsic::smul_with_overflow: {
5224 ISD::NodeType Op;
5225 switch (Intrinsic) {
5226 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
5227 case Intrinsic::uadd_with_overflow: Op = ISD::UADDO; break;
5228 case Intrinsic::sadd_with_overflow: Op = ISD::SADDO; break;
5229 case Intrinsic::usub_with_overflow: Op = ISD::USUBO; break;
5230 case Intrinsic::ssub_with_overflow: Op = ISD::SSUBO; break;
5231 case Intrinsic::umul_with_overflow: Op = ISD::UMULO; break;
5232 case Intrinsic::smul_with_overflow: Op = ISD::SMULO; break;
5233 }
5234 SDValue Op1 = getValue(I.getArgOperand(0));
5235 SDValue Op2 = getValue(I.getArgOperand(1));
Bill Wendling7cdc3c82008-11-21 02:03:52 +00005236
Craig Topperc42e6402012-04-11 04:34:11 +00005237 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005238 setValue(&I, DAG.getNode(Op, sdl, VTs, Op1, Op2));
Craig Topperc42e6402012-04-11 04:34:11 +00005239 return 0;
5240 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005241 case Intrinsic::prefetch: {
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00005242 SDValue Ops[5];
Dale Johannesen1de4aa92010-10-26 23:11:10 +00005243 unsigned rw = cast<ConstantInt>(I.getArgOperand(1))->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005244 Ops[0] = getRoot();
Gabor Greif0635f352010-06-25 09:38:13 +00005245 Ops[1] = getValue(I.getArgOperand(0));
5246 Ops[2] = getValue(I.getArgOperand(1));
5247 Ops[3] = getValue(I.getArgOperand(2));
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00005248 Ops[4] = getValue(I.getArgOperand(3));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005249 DAG.setRoot(DAG.getMemIntrinsicNode(ISD::PREFETCH, sdl,
Dale Johannesen1de4aa92010-10-26 23:11:10 +00005250 DAG.getVTList(MVT::Other),
Bruno Cardoso Lopes9a767332011-06-14 04:58:37 +00005251 &Ops[0], 5,
Dale Johannesen1de4aa92010-10-26 23:11:10 +00005252 EVT::getIntegerVT(*Context, 8),
5253 MachinePointerInfo(I.getArgOperand(0)),
5254 0, /* align */
5255 false, /* volatile */
5256 rw==0, /* read */
5257 rw==1)); /* write */
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005258 return 0;
5259 }
Duncan Sandsf07c9492009-11-10 09:08:09 +00005260 case Intrinsic::lifetime_start:
Nadav Rotemc05d3062012-09-06 09:17:37 +00005261 case Intrinsic::lifetime_end: {
Nadav Rotemc05d3062012-09-06 09:17:37 +00005262 bool IsStart = (Intrinsic == Intrinsic::lifetime_start);
Nadav Rotem9a2ae002012-09-10 08:43:23 +00005263 // Stack coloring is not enabled in O0, discard region information.
5264 if (TM.getOptLevel() == CodeGenOpt::None)
5265 return 0;
Nadav Rotemc05d3062012-09-06 09:17:37 +00005266
Nadav Rotem9a2ae002012-09-10 08:43:23 +00005267 SmallVector<Value *, 4> Allocas;
5268 GetUnderlyingObjects(I.getArgOperand(1), Allocas, TD);
5269
Craig Topperf22fd3f2013-07-03 05:11:49 +00005270 for (SmallVectorImpl<Value*>::iterator Object = Allocas.begin(),
5271 E = Allocas.end(); Object != E; ++Object) {
Nadav Rotem9a2ae002012-09-10 08:43:23 +00005272 AllocaInst *LifetimeObject = dyn_cast_or_null<AllocaInst>(*Object);
5273
5274 // Could not find an Alloca.
5275 if (!LifetimeObject)
5276 continue;
5277
5278 int FI = FuncInfo.StaticAllocaMap[LifetimeObject];
5279
5280 SDValue Ops[2];
5281 Ops[0] = getRoot();
Bill Wendlingba54bca2013-06-19 21:36:55 +00005282 Ops[1] = DAG.getFrameIndex(FI, TLI->getPointerTy(), true);
Nadav Rotem9a2ae002012-09-10 08:43:23 +00005283 unsigned Opcode = (IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END);
5284
Andrew Trickac6d9be2013-05-25 02:42:55 +00005285 Res = DAG.getNode(Opcode, sdl, MVT::Other, Ops, 2);
Nadav Rotem9a2ae002012-09-10 08:43:23 +00005286 DAG.setRoot(Res);
5287 }
Nadav Rotem5882e562013-02-01 19:25:23 +00005288 return 0;
Nadav Rotemc05d3062012-09-06 09:17:37 +00005289 }
5290 case Intrinsic::invariant_start:
Duncan Sandsf07c9492009-11-10 09:08:09 +00005291 // Discard region information.
Bill Wendlingba54bca2013-06-19 21:36:55 +00005292 setValue(&I, DAG.getUNDEF(TLI->getPointerTy()));
Duncan Sandsf07c9492009-11-10 09:08:09 +00005293 return 0;
5294 case Intrinsic::invariant_end:
Duncan Sandsf07c9492009-11-10 09:08:09 +00005295 // Discard region information.
5296 return 0;
Michael Gottesman657484f2013-08-20 07:00:16 +00005297 case Intrinsic::stackprotectorcheck: {
5298 // Do not actually emit anything for this basic block. Instead we initialize
5299 // the stack protector descriptor and export the guard variable so we can
5300 // access it in FinishBasicBlock.
5301 const BasicBlock *BB = I.getParent();
5302 SPDescriptor.initialize(BB, FuncInfo.MBBMap[BB], I);
5303 ExportFromCurrentBlock(SPDescriptor.getGuard());
5304
5305 // Flush our exports since we are going to process a terminator.
5306 (void)getControlRoot();
5307 return 0;
5308 }
Nuno Lopes85b40892012-06-28 22:30:12 +00005309 case Intrinsic::donothing:
5310 // ignore
5311 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005312 }
5313}
5314
Dan Gohman46510a72010-04-15 01:51:59 +00005315void SelectionDAGBuilder::LowerCallTo(ImmutableCallSite CS, SDValue Callee,
Dan Gohman2048b852009-11-23 18:04:58 +00005316 bool isTailCall,
5317 MachineBasicBlock *LandingPad) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005318 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
5319 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
5320 Type *RetTy = FTy->getReturnType();
Chris Lattner512063d2010-04-05 06:19:28 +00005321 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Chris Lattner16112732010-03-14 01:41:15 +00005322 MCSymbol *BeginLabel = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005323
5324 TargetLowering::ArgListTy Args;
5325 TargetLowering::ArgListEntry Entry;
5326 Args.reserve(CS.arg_size());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005327
5328 // Check whether the function can return without sret-demotion.
Dan Gohman84023e02010-07-10 09:00:22 +00005329 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00005330 const TargetLowering *TLI = TM.getTargetLowering();
5331 GetReturnInfo(RetTy, CS.getAttributes(), Outs, *TLI);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005332
Bill Wendlingba54bca2013-06-19 21:36:55 +00005333 bool CanLowerReturn = TLI->CanLowerReturn(CS.getCallingConv(),
5334 DAG.getMachineFunction(),
5335 FTy->isVarArg(), Outs,
5336 FTy->getContext());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005337
5338 SDValue DemoteStackSlot;
Chris Lattnerecf42c42010-09-21 16:36:31 +00005339 int DemoteStackIdx = -100;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005340
5341 if (!CanLowerReturn) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00005342 uint64_t TySize = TLI->getDataLayout()->getTypeAllocSize(
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005343 FTy->getReturnType());
Bill Wendlingba54bca2013-06-19 21:36:55 +00005344 unsigned Align = TLI->getDataLayout()->getPrefTypeAlignment(
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005345 FTy->getReturnType());
5346 MachineFunction &MF = DAG.getMachineFunction();
Chris Lattnerecf42c42010-09-21 16:36:31 +00005347 DemoteStackIdx = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005348 Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005349
Bill Wendlingba54bca2013-06-19 21:36:55 +00005350 DemoteStackSlot = DAG.getFrameIndex(DemoteStackIdx, TLI->getPointerTy());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005351 Entry.Node = DemoteStackSlot;
5352 Entry.Ty = StackSlotPtrType;
5353 Entry.isSExt = false;
5354 Entry.isZExt = false;
5355 Entry.isInReg = false;
5356 Entry.isSRet = true;
5357 Entry.isNest = false;
5358 Entry.isByVal = false;
Stephen Lin456ca042013-04-20 05:14:40 +00005359 Entry.isReturned = false;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005360 Entry.Alignment = Align;
5361 Args.push_back(Entry);
5362 RetTy = Type::getVoidTy(FTy->getContext());
5363 }
5364
Dan Gohman46510a72010-04-15 01:51:59 +00005365 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005366 i != e; ++i) {
Rafael Espindola3fa82832011-05-13 15:18:06 +00005367 const Value *V = *i;
5368
5369 // Skip empty types
5370 if (V->getType()->isEmptyTy())
5371 continue;
5372
5373 SDValue ArgNode = getValue(V);
5374 Entry.Node = ArgNode; Entry.Ty = V->getType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005375
5376 unsigned attrInd = i - CS.arg_begin() + 1;
Stephen Lin456ca042013-04-20 05:14:40 +00005377 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
5378 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
5379 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
5380 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
5381 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
5382 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
5383 Entry.isReturned = CS.paramHasAttr(attrInd, Attribute::Returned);
5384 Entry.Alignment = CS.getParamAlignment(attrInd);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005385 Args.push_back(Entry);
5386 }
5387
Chris Lattner512063d2010-04-05 06:19:28 +00005388 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005389 // Insert a label before the invoke call to mark the try range. This can be
5390 // used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00005391 BeginLabel = MMI.getContext().CreateTempSymbol();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00005392
Jim Grosbachca752c92010-01-28 01:45:32 +00005393 // For SjLj, keep track of which landing pads go with which invokes
5394 // so as to maintain the ordering of pads in the LSDA.
Chris Lattner512063d2010-04-05 06:19:28 +00005395 unsigned CallSiteIndex = MMI.getCurrentCallSite();
Jim Grosbachca752c92010-01-28 01:45:32 +00005396 if (CallSiteIndex) {
Chris Lattner512063d2010-04-05 06:19:28 +00005397 MMI.setCallSiteBeginLabel(BeginLabel, CallSiteIndex);
Bill Wendling30e67402011-10-05 22:24:35 +00005398 LPadToCallSiteMap[LandingPad].push_back(CallSiteIndex);
Bill Wendlinga8512ed2011-10-04 22:00:35 +00005399
Jim Grosbachca752c92010-01-28 01:45:32 +00005400 // Now that the call site is handled, stop tracking it.
Chris Lattner512063d2010-04-05 06:19:28 +00005401 MMI.setCurrentCallSite(0);
Jim Grosbachca752c92010-01-28 01:45:32 +00005402 }
5403
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005404 // Both PendingLoads and PendingExports must be flushed here;
5405 // this call might not return.
5406 (void)getRoot();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005407 DAG.setRoot(DAG.getEHLabel(getCurSDLoc(), getControlRoot(), BeginLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005408 }
5409
Dan Gohman98ca4f22009-08-05 01:29:28 +00005410 // Check if target-independent constraints permit a tail call here.
Bill Wendlingba54bca2013-06-19 21:36:55 +00005411 // Target-dependent constraints are checked within TLI->LowerCallTo.
5412 if (isTailCall && !isInTailCallPosition(CS, *TLI))
Dan Gohman98ca4f22009-08-05 01:29:28 +00005413 isTailCall = false;
5414
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00005415 TargetLowering::
5416 CallLoweringInfo CLI(getRoot(), RetTy, FTy, isTailCall, Callee, Args, DAG,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005417 getCurSDLoc(), CS);
Bill Wendlingba54bca2013-06-19 21:36:55 +00005418 std::pair<SDValue,SDValue> Result = TLI->LowerCallTo(CLI);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005419 assert((isTailCall || Result.second.getNode()) &&
5420 "Non-null chain expected with non-tail call!");
5421 assert((Result.second.getNode() || !Result.first.getNode()) &&
5422 "Null value expected with tail call!");
Bill Wendlinge80ae832009-12-22 00:50:32 +00005423 if (Result.first.getNode()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005424 setValue(CS.getInstruction(), Result.first);
Bill Wendlinge80ae832009-12-22 00:50:32 +00005425 } else if (!CanLowerReturn && Result.second.getNode()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005426 // The instruction result is the result of loading from the
5427 // hidden sret parameter.
5428 SmallVector<EVT, 1> PVTs;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005429 Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005430
Bill Wendlingba54bca2013-06-19 21:36:55 +00005431 ComputeValueVTs(*TLI, PtrRetTy, PVTs);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005432 assert(PVTs.size() == 1 && "Pointers should fit in one register");
5433 EVT PtrVT = PVTs[0];
Eli Friedman2db0e9e2012-05-25 00:09:29 +00005434
5435 SmallVector<EVT, 4> RetTys;
5436 SmallVector<uint64_t, 4> Offsets;
5437 RetTy = FTy->getReturnType();
Bill Wendlingba54bca2013-06-19 21:36:55 +00005438 ComputeValueVTs(*TLI, RetTy, RetTys, &Offsets);
Eli Friedman2db0e9e2012-05-25 00:09:29 +00005439
5440 unsigned NumValues = RetTys.size();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005441 SmallVector<SDValue, 4> Values(NumValues);
5442 SmallVector<SDValue, 4> Chains(NumValues);
5443
5444 for (unsigned i = 0; i < NumValues; ++i) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005445 SDValue Add = DAG.getNode(ISD::ADD, getCurSDLoc(), PtrVT,
Bill Wendlinge80ae832009-12-22 00:50:32 +00005446 DemoteStackSlot,
5447 DAG.getConstant(Offsets[i], PtrVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005448 SDValue L = DAG.getLoad(RetTys[i], getCurSDLoc(), Result.second, Add,
Chris Lattnerecf42c42010-09-21 16:36:31 +00005449 MachinePointerInfo::getFixedStack(DemoteStackIdx, Offsets[i]),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005450 false, false, false, 1);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005451 Values[i] = L;
5452 Chains[i] = L.getValue(1);
5453 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00005454
Andrew Trickac6d9be2013-05-25 02:42:55 +00005455 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005456 MVT::Other, &Chains[0], NumValues);
5457 PendingLoads.push_back(Chain);
Michael J. Spencere70c5262010-10-16 08:25:21 +00005458
Bill Wendling4533cac2010-01-28 21:51:40 +00005459 setValue(CS.getInstruction(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00005460 DAG.getNode(ISD::MERGE_VALUES, getCurSDLoc(),
Bill Wendling4533cac2010-01-28 21:51:40 +00005461 DAG.getVTList(&RetTys[0], RetTys.size()),
Eli Friedman2db0e9e2012-05-25 00:09:29 +00005462 &Values[0], Values.size()));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005463 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00005464
Evan Cheng8380c032011-04-01 19:42:22 +00005465 if (!Result.second.getNode()) {
Evan Chengc249e482011-04-01 19:57:01 +00005466 // As a special case, a null chain means that a tail call has been emitted and
5467 // the DAG root is already updated.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005468 HasTailCall = true;
Tim Northovere5a81a12013-07-06 12:58:45 +00005469
5470 // Since there's no actual continuation from this block, nothing can be
5471 // relying on us setting vregs for them.
5472 PendingExports.clear();
Evan Cheng8380c032011-04-01 19:42:22 +00005473 } else {
5474 DAG.setRoot(Result.second);
Evan Cheng8380c032011-04-01 19:42:22 +00005475 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005476
Chris Lattner512063d2010-04-05 06:19:28 +00005477 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005478 // Insert a label at the end of the invoke call to mark the try range. This
5479 // can be used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00005480 MCSymbol *EndLabel = MMI.getContext().CreateTempSymbol();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005481 DAG.setRoot(DAG.getEHLabel(getCurSDLoc(), getRoot(), EndLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005482
5483 // Inform MachineModuleInfo of range.
Chris Lattner512063d2010-04-05 06:19:28 +00005484 MMI.addInvoke(LandingPad, BeginLabel, EndLabel);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005485 }
5486}
5487
Chris Lattner8047d9a2009-12-24 00:37:38 +00005488/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
5489/// value is equal or not-equal to zero.
Dan Gohman46510a72010-04-15 01:51:59 +00005490static bool IsOnlyUsedInZeroEqualityComparison(const Value *V) {
5491 for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end();
Chris Lattner8047d9a2009-12-24 00:37:38 +00005492 UI != E; ++UI) {
Dan Gohman46510a72010-04-15 01:51:59 +00005493 if (const ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
Chris Lattner8047d9a2009-12-24 00:37:38 +00005494 if (IC->isEquality())
Dan Gohman46510a72010-04-15 01:51:59 +00005495 if (const Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
Chris Lattner8047d9a2009-12-24 00:37:38 +00005496 if (C->isNullValue())
5497 continue;
5498 // Unknown instruction.
5499 return false;
5500 }
5501 return true;
5502}
5503
Dan Gohman46510a72010-04-15 01:51:59 +00005504static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005505 Type *LoadTy,
Chris Lattner8047d9a2009-12-24 00:37:38 +00005506 SelectionDAGBuilder &Builder) {
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005507
Chris Lattner8047d9a2009-12-24 00:37:38 +00005508 // Check to see if this load can be trivially constant folded, e.g. if the
5509 // input is from a string literal.
Dan Gohman46510a72010-04-15 01:51:59 +00005510 if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00005511 // Cast pointer to the type we really want to load.
Dan Gohman46510a72010-04-15 01:51:59 +00005512 LoadInput = ConstantExpr::getBitCast(const_cast<Constant *>(LoadInput),
Chris Lattner8047d9a2009-12-24 00:37:38 +00005513 PointerType::getUnqual(LoadTy));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005514
Dan Gohman46510a72010-04-15 01:51:59 +00005515 if (const Constant *LoadCst =
5516 ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput),
5517 Builder.TD))
Chris Lattner8047d9a2009-12-24 00:37:38 +00005518 return Builder.getValue(LoadCst);
5519 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005520
Chris Lattner8047d9a2009-12-24 00:37:38 +00005521 // Otherwise, we have to emit the load. If the pointer is to unfoldable but
5522 // still constant memory, the input chain can be the entry node.
5523 SDValue Root;
5524 bool ConstantMemory = false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005525
Chris Lattner8047d9a2009-12-24 00:37:38 +00005526 // Do not serialize (non-volatile) loads of constant memory with anything.
5527 if (Builder.AA->pointsToConstantMemory(PtrVal)) {
5528 Root = Builder.DAG.getEntryNode();
5529 ConstantMemory = true;
5530 } else {
5531 // Do not serialize non-volatile loads against each other.
5532 Root = Builder.DAG.getRoot();
5533 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005534
Chris Lattner8047d9a2009-12-24 00:37:38 +00005535 SDValue Ptr = Builder.getValue(PtrVal);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005536 SDValue LoadVal = Builder.DAG.getLoad(LoadVT, Builder.getCurSDLoc(), Root,
Chris Lattnerecf42c42010-09-21 16:36:31 +00005537 Ptr, MachinePointerInfo(PtrVal),
David Greene1e559442010-02-15 17:00:31 +00005538 false /*volatile*/,
Stephen Lin155615d2013-07-08 00:37:03 +00005539 false /*nontemporal*/,
Pete Cooperd752e0f2011-11-08 18:42:53 +00005540 false /*isinvariant*/, 1 /* align=1 */);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005541
Chris Lattner8047d9a2009-12-24 00:37:38 +00005542 if (!ConstantMemory)
5543 Builder.PendingLoads.push_back(LoadVal.getValue(1));
5544 return LoadVal;
5545}
5546
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005547/// processIntegerCallValue - Record the value for an instruction that
5548/// produces an integer result, converting the type where necessary.
5549void SelectionDAGBuilder::processIntegerCallValue(const Instruction &I,
5550 SDValue Value,
5551 bool IsSigned) {
5552 EVT VT = TM.getTargetLowering()->getValueType(I.getType(), true);
5553 if (IsSigned)
5554 Value = DAG.getSExtOrTrunc(Value, getCurSDLoc(), VT);
5555 else
5556 Value = DAG.getZExtOrTrunc(Value, getCurSDLoc(), VT);
5557 setValue(&I, Value);
5558}
Chris Lattner8047d9a2009-12-24 00:37:38 +00005559
5560/// visitMemCmpCall - See if we can lower a call to memcmp in an optimized form.
5561/// If so, return true and lower it, otherwise return false and it will be
5562/// lowered like a normal call.
Dan Gohman46510a72010-04-15 01:51:59 +00005563bool SelectionDAGBuilder::visitMemCmpCall(const CallInst &I) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00005564 // Verify that the prototype makes sense. int memcmp(void*,void*,size_t)
Gabor Greif37387d52010-06-30 12:55:46 +00005565 if (I.getNumArgOperands() != 3)
Chris Lattner8047d9a2009-12-24 00:37:38 +00005566 return false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005567
Gabor Greif0635f352010-06-25 09:38:13 +00005568 const Value *LHS = I.getArgOperand(0), *RHS = I.getArgOperand(1);
Duncan Sands1df98592010-02-16 11:11:14 +00005569 if (!LHS->getType()->isPointerTy() || !RHS->getType()->isPointerTy() ||
Gabor Greif0635f352010-06-25 09:38:13 +00005570 !I.getArgOperand(2)->getType()->isIntegerTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +00005571 !I.getType()->isIntegerTy())
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005572 return false;
5573
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005574 const Value *Size = I.getArgOperand(2);
5575 const ConstantInt *CSize = dyn_cast<ConstantInt>(Size);
5576 if (CSize && CSize->getZExtValue() == 0) {
Richard Sandifordac168b82013-08-12 10:28:10 +00005577 EVT CallVT = TM.getTargetLowering()->getValueType(I.getType(), true);
5578 setValue(&I, DAG.getConstant(0, CallVT));
5579 return true;
5580 }
5581
Richard Sandifordac168b82013-08-12 10:28:10 +00005582 const TargetSelectionDAGInfo &TSI = DAG.getSelectionDAGInfo();
5583 std::pair<SDValue, SDValue> Res =
5584 TSI.EmitTargetCodeForMemcmp(DAG, getCurSDLoc(), DAG.getRoot(),
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005585 getValue(LHS), getValue(RHS), getValue(Size),
5586 MachinePointerInfo(LHS),
5587 MachinePointerInfo(RHS));
Richard Sandifordac168b82013-08-12 10:28:10 +00005588 if (Res.first.getNode()) {
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005589 processIntegerCallValue(I, Res.first, true);
5590 PendingLoads.push_back(Res.second);
Richard Sandifordac168b82013-08-12 10:28:10 +00005591 return true;
5592 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005593
Chris Lattner8047d9a2009-12-24 00:37:38 +00005594 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
5595 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005596 if (CSize && IsOnlyUsedInZeroEqualityComparison(&I)) {
Chris Lattner04b091a2009-12-24 01:07:17 +00005597 bool ActuallyDoIt = true;
5598 MVT LoadVT;
Chris Lattnerdb125cf2011-07-18 04:54:35 +00005599 Type *LoadTy;
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005600 switch (CSize->getZExtValue()) {
Chris Lattner04b091a2009-12-24 01:07:17 +00005601 default:
5602 LoadVT = MVT::Other;
5603 LoadTy = 0;
5604 ActuallyDoIt = false;
5605 break;
5606 case 2:
5607 LoadVT = MVT::i16;
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005608 LoadTy = Type::getInt16Ty(CSize->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005609 break;
5610 case 4:
5611 LoadVT = MVT::i32;
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005612 LoadTy = Type::getInt32Ty(CSize->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005613 break;
5614 case 8:
5615 LoadVT = MVT::i64;
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005616 LoadTy = Type::getInt64Ty(CSize->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005617 break;
5618 /*
5619 case 16:
5620 LoadVT = MVT::v4i32;
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005621 LoadTy = Type::getInt32Ty(CSize->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005622 LoadTy = VectorType::get(LoadTy, 4);
5623 break;
5624 */
5625 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005626
Chris Lattner04b091a2009-12-24 01:07:17 +00005627 // This turns into unaligned loads. We only do this if the target natively
5628 // supports the MVT we'll be loading or if it is small enough (<= 4) that
5629 // we'll only produce a small number of byte loads.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005630
Chris Lattner04b091a2009-12-24 01:07:17 +00005631 // Require that we can find a legal MVT, and only do this if the target
5632 // supports unaligned loads of that type. Expanding into byte loads would
5633 // bloat the code.
Bill Wendlingba54bca2013-06-19 21:36:55 +00005634 const TargetLowering *TLI = TM.getTargetLowering();
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005635 if (ActuallyDoIt && CSize->getZExtValue() > 4) {
Chris Lattner04b091a2009-12-24 01:07:17 +00005636 // TODO: Handle 5 byte compare as 4-byte + 1 byte.
5637 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
Bill Wendlingba54bca2013-06-19 21:36:55 +00005638 if (!TLI->isTypeLegal(LoadVT) ||!TLI->allowsUnalignedMemoryAccesses(LoadVT))
Chris Lattner04b091a2009-12-24 01:07:17 +00005639 ActuallyDoIt = false;
5640 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005641
Chris Lattner04b091a2009-12-24 01:07:17 +00005642 if (ActuallyDoIt) {
5643 SDValue LHSVal = getMemCmpLoad(LHS, LoadVT, LoadTy, *this);
5644 SDValue RHSVal = getMemCmpLoad(RHS, LoadVT, LoadTy, *this);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005645
Andrew Trickac6d9be2013-05-25 02:42:55 +00005646 SDValue Res = DAG.getSetCC(getCurSDLoc(), MVT::i1, LHSVal, RHSVal,
Chris Lattner04b091a2009-12-24 01:07:17 +00005647 ISD::SETNE);
Richard Sandiford6a079fe2013-08-16 10:55:47 +00005648 processIntegerCallValue(I, Res, false);
Chris Lattner04b091a2009-12-24 01:07:17 +00005649 return true;
5650 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00005651 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005652
5653
Chris Lattner8047d9a2009-12-24 00:37:38 +00005654 return false;
5655}
5656
Richard Sandiford8c201582013-08-20 09:38:48 +00005657/// visitMemChrCall -- See if we can lower a memchr call into an optimized
5658/// form. If so, return true and lower it, otherwise return false and it
5659/// will be lowered like a normal call.
5660bool SelectionDAGBuilder::visitMemChrCall(const CallInst &I) {
5661 // Verify that the prototype makes sense. void *memchr(void *, int, size_t)
5662 if (I.getNumArgOperands() != 3)
5663 return false;
5664
5665 const Value *Src = I.getArgOperand(0);
5666 const Value *Char = I.getArgOperand(1);
5667 const Value *Length = I.getArgOperand(2);
5668 if (!Src->getType()->isPointerTy() ||
5669 !Char->getType()->isIntegerTy() ||
5670 !Length->getType()->isIntegerTy() ||
5671 !I.getType()->isPointerTy())
5672 return false;
5673
5674 const TargetSelectionDAGInfo &TSI = DAG.getSelectionDAGInfo();
5675 std::pair<SDValue, SDValue> Res =
5676 TSI.EmitTargetCodeForMemchr(DAG, getCurSDLoc(), DAG.getRoot(),
5677 getValue(Src), getValue(Char), getValue(Length),
5678 MachinePointerInfo(Src));
5679 if (Res.first.getNode()) {
5680 setValue(&I, Res.first);
5681 PendingLoads.push_back(Res.second);
5682 return true;
5683 }
5684
5685 return false;
5686}
5687
Richard Sandiford4fc73552013-08-16 11:29:37 +00005688/// visitStrCpyCall -- See if we can lower a strcpy or stpcpy call into an
5689/// optimized form. If so, return true and lower it, otherwise return false
5690/// and it will be lowered like a normal call.
5691bool SelectionDAGBuilder::visitStrCpyCall(const CallInst &I, bool isStpcpy) {
5692 // Verify that the prototype makes sense. char *strcpy(char *, char *)
5693 if (I.getNumArgOperands() != 2)
5694 return false;
5695
5696 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
5697 if (!Arg0->getType()->isPointerTy() ||
5698 !Arg1->getType()->isPointerTy() ||
5699 !I.getType()->isPointerTy())
5700 return false;
5701
5702 const TargetSelectionDAGInfo &TSI = DAG.getSelectionDAGInfo();
5703 std::pair<SDValue, SDValue> Res =
5704 TSI.EmitTargetCodeForStrcpy(DAG, getCurSDLoc(), getRoot(),
5705 getValue(Arg0), getValue(Arg1),
5706 MachinePointerInfo(Arg0),
5707 MachinePointerInfo(Arg1), isStpcpy);
5708 if (Res.first.getNode()) {
5709 setValue(&I, Res.first);
5710 DAG.setRoot(Res.second);
5711 return true;
5712 }
5713
5714 return false;
5715}
5716
Richard Sandiforde1b2af72013-08-16 11:21:54 +00005717/// visitStrCmpCall - See if we can lower a call to strcmp in an optimized form.
5718/// If so, return true and lower it, otherwise return false and it will be
5719/// lowered like a normal call.
5720bool SelectionDAGBuilder::visitStrCmpCall(const CallInst &I) {
5721 // Verify that the prototype makes sense. int strcmp(void*,void*)
5722 if (I.getNumArgOperands() != 2)
5723 return false;
5724
5725 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
5726 if (!Arg0->getType()->isPointerTy() ||
5727 !Arg1->getType()->isPointerTy() ||
5728 !I.getType()->isIntegerTy())
5729 return false;
5730
5731 const TargetSelectionDAGInfo &TSI = DAG.getSelectionDAGInfo();
5732 std::pair<SDValue, SDValue> Res =
5733 TSI.EmitTargetCodeForStrcmp(DAG, getCurSDLoc(), DAG.getRoot(),
5734 getValue(Arg0), getValue(Arg1),
5735 MachinePointerInfo(Arg0),
5736 MachinePointerInfo(Arg1));
5737 if (Res.first.getNode()) {
5738 processIntegerCallValue(I, Res.first, true);
5739 PendingLoads.push_back(Res.second);
5740 return true;
5741 }
5742
5743 return false;
5744}
5745
Richard Sandiford19262ee2013-08-16 11:41:43 +00005746/// visitStrLenCall -- See if we can lower a strlen call into an optimized
5747/// form. If so, return true and lower it, otherwise return false and it
5748/// will be lowered like a normal call.
5749bool SelectionDAGBuilder::visitStrLenCall(const CallInst &I) {
5750 // Verify that the prototype makes sense. size_t strlen(char *)
5751 if (I.getNumArgOperands() != 1)
5752 return false;
5753
5754 const Value *Arg0 = I.getArgOperand(0);
5755 if (!Arg0->getType()->isPointerTy() || !I.getType()->isIntegerTy())
5756 return false;
5757
5758 const TargetSelectionDAGInfo &TSI = DAG.getSelectionDAGInfo();
5759 std::pair<SDValue, SDValue> Res =
5760 TSI.EmitTargetCodeForStrlen(DAG, getCurSDLoc(), DAG.getRoot(),
5761 getValue(Arg0), MachinePointerInfo(Arg0));
5762 if (Res.first.getNode()) {
5763 processIntegerCallValue(I, Res.first, false);
5764 PendingLoads.push_back(Res.second);
5765 return true;
5766 }
5767
5768 return false;
5769}
5770
5771/// visitStrNLenCall -- See if we can lower a strnlen call into an optimized
5772/// form. If so, return true and lower it, otherwise return false and it
5773/// will be lowered like a normal call.
5774bool SelectionDAGBuilder::visitStrNLenCall(const CallInst &I) {
5775 // Verify that the prototype makes sense. size_t strnlen(char *, size_t)
5776 if (I.getNumArgOperands() != 2)
5777 return false;
5778
5779 const Value *Arg0 = I.getArgOperand(0), *Arg1 = I.getArgOperand(1);
5780 if (!Arg0->getType()->isPointerTy() ||
5781 !Arg1->getType()->isIntegerTy() ||
5782 !I.getType()->isIntegerTy())
5783 return false;
5784
5785 const TargetSelectionDAGInfo &TSI = DAG.getSelectionDAGInfo();
5786 std::pair<SDValue, SDValue> Res =
5787 TSI.EmitTargetCodeForStrnlen(DAG, getCurSDLoc(), DAG.getRoot(),
5788 getValue(Arg0), getValue(Arg1),
5789 MachinePointerInfo(Arg0));
5790 if (Res.first.getNode()) {
5791 processIntegerCallValue(I, Res.first, false);
5792 PendingLoads.push_back(Res.second);
5793 return true;
5794 }
5795
5796 return false;
5797}
5798
Bob Wilson53624a22012-08-03 23:29:17 +00005799/// visitUnaryFloatCall - If a call instruction is a unary floating-point
5800/// operation (as expected), translate it to an SDNode with the specified opcode
5801/// and return true.
5802bool SelectionDAGBuilder::visitUnaryFloatCall(const CallInst &I,
5803 unsigned Opcode) {
5804 // Sanity check that it really is a unary floating-point call.
5805 if (I.getNumArgOperands() != 1 ||
5806 !I.getArgOperand(0)->getType()->isFloatingPointTy() ||
5807 I.getType() != I.getArgOperand(0)->getType() ||
5808 !I.onlyReadsMemory())
5809 return false;
5810
5811 SDValue Tmp = getValue(I.getArgOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005812 setValue(&I, DAG.getNode(Opcode, getCurSDLoc(), Tmp.getValueType(), Tmp));
Bob Wilson53624a22012-08-03 23:29:17 +00005813 return true;
5814}
Chris Lattner8047d9a2009-12-24 00:37:38 +00005815
Dan Gohman46510a72010-04-15 01:51:59 +00005816void SelectionDAGBuilder::visitCall(const CallInst &I) {
Chris Lattner598751e2010-07-05 05:36:21 +00005817 // Handle inline assembly differently.
5818 if (isa<InlineAsm>(I.getCalledValue())) {
5819 visitInlineAsm(&I);
5820 return;
5821 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00005822
Michael J. Spencer391b43b2010-10-21 20:49:23 +00005823 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Michael J. Spencerc9c137b2012-02-22 19:06:13 +00005824 ComputeUsesVAFloatArgument(I, &MMI);
Michael J. Spencer391b43b2010-10-21 20:49:23 +00005825
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005826 const char *RenameFn = 0;
5827 if (Function *F = I.getCalledFunction()) {
5828 if (F->isDeclaration()) {
Chris Lattner598751e2010-07-05 05:36:21 +00005829 if (const TargetIntrinsicInfo *II = TM.getIntrinsicInfo()) {
Dale Johannesen49de9822009-02-05 01:49:45 +00005830 if (unsigned IID = II->getIntrinsicID(F)) {
5831 RenameFn = visitIntrinsicCall(I, IID);
5832 if (!RenameFn)
5833 return;
5834 }
5835 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005836 if (unsigned IID = F->getIntrinsicID()) {
5837 RenameFn = visitIntrinsicCall(I, IID);
5838 if (!RenameFn)
5839 return;
5840 }
5841 }
5842
5843 // Check for well-known libc/libm calls. If the function is internal, it
5844 // can't be a library call.
Bob Wilson982dc842012-08-03 21:26:24 +00005845 LibFunc::Func Func;
5846 if (!F->hasLocalLinkage() && F->hasName() &&
5847 LibInfo->getLibFunc(F->getName(), Func) &&
5848 LibInfo->hasOptimizedCodeGen(Func)) {
5849 switch (Func) {
5850 default: break;
5851 case LibFunc::copysign:
5852 case LibFunc::copysignf:
5853 case LibFunc::copysignl:
Gabor Greif37387d52010-06-30 12:55:46 +00005854 if (I.getNumArgOperands() == 2 && // Basic sanity checks.
Gabor Greif0635f352010-06-25 09:38:13 +00005855 I.getArgOperand(0)->getType()->isFloatingPointTy() &&
5856 I.getType() == I.getArgOperand(0)->getType() &&
Bob Wilson53624a22012-08-03 23:29:17 +00005857 I.getType() == I.getArgOperand(1)->getType() &&
5858 I.onlyReadsMemory()) {
Gabor Greif0635f352010-06-25 09:38:13 +00005859 SDValue LHS = getValue(I.getArgOperand(0));
5860 SDValue RHS = getValue(I.getArgOperand(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005861 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurSDLoc(),
Bill Wendling0d580132009-12-23 01:28:19 +00005862 LHS.getValueType(), LHS, RHS));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005863 return;
5864 }
Bob Wilson982dc842012-08-03 21:26:24 +00005865 break;
5866 case LibFunc::fabs:
5867 case LibFunc::fabsf:
5868 case LibFunc::fabsl:
Bob Wilson53624a22012-08-03 23:29:17 +00005869 if (visitUnaryFloatCall(I, ISD::FABS))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005870 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005871 break;
5872 case LibFunc::sin:
5873 case LibFunc::sinf:
5874 case LibFunc::sinl:
Bob Wilson53624a22012-08-03 23:29:17 +00005875 if (visitUnaryFloatCall(I, ISD::FSIN))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005876 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005877 break;
5878 case LibFunc::cos:
5879 case LibFunc::cosf:
5880 case LibFunc::cosl:
Bob Wilson53624a22012-08-03 23:29:17 +00005881 if (visitUnaryFloatCall(I, ISD::FCOS))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005882 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005883 break;
5884 case LibFunc::sqrt:
5885 case LibFunc::sqrtf:
5886 case LibFunc::sqrtl:
Preston Gurdb704d232013-05-27 15:44:35 +00005887 case LibFunc::sqrt_finite:
5888 case LibFunc::sqrtf_finite:
5889 case LibFunc::sqrtl_finite:
Bob Wilson53624a22012-08-03 23:29:17 +00005890 if (visitUnaryFloatCall(I, ISD::FSQRT))
Dale Johannesen52fb79b2009-09-25 17:23:22 +00005891 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005892 break;
5893 case LibFunc::floor:
5894 case LibFunc::floorf:
5895 case LibFunc::floorl:
Bob Wilson53624a22012-08-03 23:29:17 +00005896 if (visitUnaryFloatCall(I, ISD::FFLOOR))
Owen Anderson4a4fdf32011-12-08 19:32:14 +00005897 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005898 break;
5899 case LibFunc::nearbyint:
5900 case LibFunc::nearbyintf:
5901 case LibFunc::nearbyintl:
Bob Wilson53624a22012-08-03 23:29:17 +00005902 if (visitUnaryFloatCall(I, ISD::FNEARBYINT))
Owen Anderson4a4fdf32011-12-08 19:32:14 +00005903 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005904 break;
5905 case LibFunc::ceil:
5906 case LibFunc::ceilf:
5907 case LibFunc::ceill:
Bob Wilson53624a22012-08-03 23:29:17 +00005908 if (visitUnaryFloatCall(I, ISD::FCEIL))
Owen Anderson4a4fdf32011-12-08 19:32:14 +00005909 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005910 break;
5911 case LibFunc::rint:
5912 case LibFunc::rintf:
5913 case LibFunc::rintl:
Bob Wilson53624a22012-08-03 23:29:17 +00005914 if (visitUnaryFloatCall(I, ISD::FRINT))
Owen Anderson4a4fdf32011-12-08 19:32:14 +00005915 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005916 break;
Hal Finkel41418d12013-08-07 22:49:12 +00005917 case LibFunc::round:
5918 case LibFunc::roundf:
5919 case LibFunc::roundl:
5920 if (visitUnaryFloatCall(I, ISD::FROUND))
5921 return;
5922 break;
Bob Wilson982dc842012-08-03 21:26:24 +00005923 case LibFunc::trunc:
5924 case LibFunc::truncf:
5925 case LibFunc::truncl:
Bob Wilson53624a22012-08-03 23:29:17 +00005926 if (visitUnaryFloatCall(I, ISD::FTRUNC))
Owen Anderson4a4fdf32011-12-08 19:32:14 +00005927 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005928 break;
5929 case LibFunc::log2:
5930 case LibFunc::log2f:
5931 case LibFunc::log2l:
Bob Wilson53624a22012-08-03 23:29:17 +00005932 if (visitUnaryFloatCall(I, ISD::FLOG2))
Owen Anderson4e0adfa2011-12-15 00:54:12 +00005933 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005934 break;
5935 case LibFunc::exp2:
5936 case LibFunc::exp2f:
5937 case LibFunc::exp2l:
Bob Wilson53624a22012-08-03 23:29:17 +00005938 if (visitUnaryFloatCall(I, ISD::FEXP2))
Owen Anderson4e0adfa2011-12-15 00:54:12 +00005939 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005940 break;
5941 case LibFunc::memcmp:
Chris Lattner8047d9a2009-12-24 00:37:38 +00005942 if (visitMemCmpCall(I))
5943 return;
Bob Wilson982dc842012-08-03 21:26:24 +00005944 break;
Richard Sandiford8c201582013-08-20 09:38:48 +00005945 case LibFunc::memchr:
5946 if (visitMemChrCall(I))
5947 return;
5948 break;
Richard Sandiford4fc73552013-08-16 11:29:37 +00005949 case LibFunc::strcpy:
5950 if (visitStrCpyCall(I, false))
5951 return;
5952 break;
5953 case LibFunc::stpcpy:
5954 if (visitStrCpyCall(I, true))
5955 return;
5956 break;
Richard Sandiforde1b2af72013-08-16 11:21:54 +00005957 case LibFunc::strcmp:
5958 if (visitStrCmpCall(I))
5959 return;
5960 break;
Richard Sandiford19262ee2013-08-16 11:41:43 +00005961 case LibFunc::strlen:
5962 if (visitStrLenCall(I))
5963 return;
5964 break;
5965 case LibFunc::strnlen:
5966 if (visitStrNLenCall(I))
5967 return;
5968 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005969 }
5970 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005971 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00005972
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005973 SDValue Callee;
5974 if (!RenameFn)
Gabor Greif0635f352010-06-25 09:38:13 +00005975 Callee = getValue(I.getCalledValue());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005976 else
Bill Wendlingba54bca2013-06-19 21:36:55 +00005977 Callee = DAG.getExternalSymbol(RenameFn,
5978 TM.getTargetLowering()->getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005979
Bill Wendling0d580132009-12-23 01:28:19 +00005980 // Check if we can potentially perform a tail call. More detailed checking is
5981 // be done within LowerCallTo, after more information about the call is known.
Evan Cheng11e67932010-01-26 23:13:04 +00005982 LowerCallTo(&I, Callee, I.isTailCall());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005983}
5984
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005985namespace {
Dan Gohman462f6b52010-05-29 17:53:24 +00005986
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005987/// AsmOperandInfo - This contains information for each constraint that we are
5988/// lowering.
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00005989class SDISelAsmOperandInfo : public TargetLowering::AsmOperandInfo {
Cedric Venetaff9c272009-02-14 16:06:42 +00005990public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005991 /// CallOperand - If this is the result output operand or a clobber
5992 /// this is null, otherwise it is the incoming operand to the CallInst.
5993 /// This gets modified as the asm is processed.
5994 SDValue CallOperand;
5995
5996 /// AssignedRegs - If this is a register or register class operand, this
5997 /// contains the set of register corresponding to the operand.
5998 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005999
John Thompsoneac6e1d2010-09-13 18:15:37 +00006000 explicit SDISelAsmOperandInfo(const TargetLowering::AsmOperandInfo &info)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006001 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
6002 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006003
Owen Andersone50ed302009-08-10 22:56:29 +00006004 /// getCallOperandValEVT - Return the EVT of the Value* that this operand
Chris Lattner81249c92008-10-17 17:05:25 +00006005 /// corresponds to. If there is no Value* for this operand, it returns
Owen Anderson825b72b2009-08-11 20:47:22 +00006006 /// MVT::Other.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00006007 EVT getCallOperandValEVT(LLVMContext &Context,
Owen Anderson1d0be152009-08-13 21:58:54 +00006008 const TargetLowering &TLI,
Micah Villmow3574eca2012-10-08 16:38:25 +00006009 const DataLayout *TD) const {
Owen Anderson825b72b2009-08-11 20:47:22 +00006010 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006011
Chris Lattner81249c92008-10-17 17:05:25 +00006012 if (isa<BasicBlock>(CallOperandVal))
6013 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006014
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006015 llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006016
Eric Christophercef81b72011-05-09 20:04:43 +00006017 // FIXME: code duplicated from TargetLowering::ParseConstraints().
Chris Lattner81249c92008-10-17 17:05:25 +00006018 // If this is an indirect operand, the operand is a pointer to the
6019 // accessed type.
Bob Wilsone261b0c2009-12-22 18:34:19 +00006020 if (isIndirect) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006021 llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
Bob Wilsone261b0c2009-12-22 18:34:19 +00006022 if (!PtrTy)
Chris Lattner75361b62010-04-07 22:58:41 +00006023 report_fatal_error("Indirect operand for inline asm not a pointer!");
Bob Wilsone261b0c2009-12-22 18:34:19 +00006024 OpTy = PtrTy->getElementType();
6025 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006026
Eric Christophercef81b72011-05-09 20:04:43 +00006027 // Look for vector wrapped in a struct. e.g. { <16 x i8> }.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006028 if (StructType *STy = dyn_cast<StructType>(OpTy))
Eric Christophercef81b72011-05-09 20:04:43 +00006029 if (STy->getNumElements() == 1)
6030 OpTy = STy->getElementType(0);
6031
Chris Lattner81249c92008-10-17 17:05:25 +00006032 // If OpTy is not a single value, it may be a struct/union that we
6033 // can tile with integers.
6034 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
6035 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
6036 switch (BitSize) {
6037 default: break;
6038 case 1:
6039 case 8:
6040 case 16:
6041 case 32:
6042 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00006043 case 128:
Owen Anderson1d0be152009-08-13 21:58:54 +00006044 OpTy = IntegerType::get(Context, BitSize);
Chris Lattner81249c92008-10-17 17:05:25 +00006045 break;
6046 }
6047 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006048
Chris Lattner81249c92008-10-17 17:05:25 +00006049 return TLI.getValueType(OpTy, true);
6050 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006051};
Dan Gohman462f6b52010-05-29 17:53:24 +00006052
John Thompson44ab89e2010-10-29 17:29:13 +00006053typedef SmallVector<SDISelAsmOperandInfo,16> SDISelAsmOperandInfoVector;
6054
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00006055} // end anonymous namespace
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006056
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006057/// GetRegistersForValue - Assign registers (virtual or physical) for the
6058/// specified operand. We prefer to assign virtual registers, to allow the
Bob Wilson266d9452009-12-17 05:07:36 +00006059/// register allocator to handle the assignment process. However, if the asm
6060/// uses features that we can't model on machineinstrs, we have SDISel do the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006061/// allocation. This produces generally horrible, but correct, code.
6062///
6063/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006064///
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00006065static void GetRegistersForValue(SelectionDAG &DAG,
6066 const TargetLowering &TLI,
Andrew Trickac6d9be2013-05-25 02:42:55 +00006067 SDLoc DL,
Benjamin Kramer8b93ff22012-02-24 14:01:17 +00006068 SDISelAsmOperandInfo &OpInfo) {
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00006069 LLVMContext &Context = *DAG.getContext();
Owen Anderson23b9b192009-08-12 00:36:31 +00006070
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006071 MachineFunction &MF = DAG.getMachineFunction();
6072 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006073
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006074 // If this is a constraint for a single physreg, or a constraint for a
6075 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006076 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006077 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
6078 OpInfo.ConstraintVT);
6079
6080 unsigned NumRegs = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00006081 if (OpInfo.ConstraintVT != MVT::Other) {
Chris Lattner01426e12008-10-21 00:45:36 +00006082 // If this is a FP input in an integer register (or visa versa) insert a bit
6083 // cast of the input value. More generally, handle any case where the input
6084 // value disagrees with the register class we plan to stick this in.
6085 if (OpInfo.Type == InlineAsm::isInput &&
6086 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00006087 // Try to convert to the first EVT that the reg class contains. If the
Chris Lattner01426e12008-10-21 00:45:36 +00006088 // types are identical size, use a bitcast to convert (e.g. two differing
6089 // vector types).
Patrik Hagglund8963fec2012-12-19 12:23:01 +00006090 MVT RegVT = *PhysReg.second->vt_begin();
Chris Lattner01426e12008-10-21 00:45:36 +00006091 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00006092 OpInfo.CallOperand = DAG.getNode(ISD::BITCAST, DL,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00006093 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00006094 OpInfo.ConstraintVT = RegVT;
6095 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
6096 // If the input is a FP value and we want it in FP registers, do a
6097 // bitcast to the corresponding integer type. This turns an f64 value
6098 // into i64, which can be passed with two i32 values on a 32-bit
6099 // machine.
Patrik Hagglund8963fec2012-12-19 12:23:01 +00006100 RegVT = MVT::getIntegerVT(OpInfo.ConstraintVT.getSizeInBits());
Benjamin Kramer7d706ed2011-03-26 16:35:10 +00006101 OpInfo.CallOperand = DAG.getNode(ISD::BITCAST, DL,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00006102 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00006103 OpInfo.ConstraintVT = RegVT;
6104 }
6105 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006106
Owen Anderson23b9b192009-08-12 00:36:31 +00006107 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00006108 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006109
Patrik Hagglund8963fec2012-12-19 12:23:01 +00006110 MVT RegVT;
Owen Andersone50ed302009-08-10 22:56:29 +00006111 EVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006112
6113 // If this is a constraint for a specific physical register, like {r17},
6114 // assign it now.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00006115 if (unsigned AssignedReg = PhysReg.first) {
6116 const TargetRegisterClass *RC = PhysReg.second;
Owen Anderson825b72b2009-08-11 20:47:22 +00006117 if (OpInfo.ConstraintVT == MVT::Other)
Chris Lattnere2f7bf82009-03-24 15:27:37 +00006118 ValueVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006119
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006120 // Get the actual register value type. This is important, because the user
6121 // may have asked for (e.g.) the AX register in i32 type. We need to
6122 // remember that AX is actually i16 to get the right extension.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00006123 RegVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006124
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006125 // This is a explicit reference to a physical register.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00006126 Regs.push_back(AssignedReg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006127
6128 // If this is an expanded reference, add the rest of the regs to Regs.
6129 if (NumRegs != 1) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00006130 TargetRegisterClass::iterator I = RC->begin();
6131 for (; *I != AssignedReg; ++I)
6132 assert(I != RC->end() && "Didn't find reg!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006133
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006134 // Already added the first reg.
6135 --NumRegs; ++I;
6136 for (; NumRegs; --NumRegs, ++I) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00006137 assert(I != RC->end() && "Ran out of registers to allocate!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006138 Regs.push_back(*I);
6139 }
6140 }
Bill Wendling651ad132009-12-22 01:25:10 +00006141
Dan Gohman7451d3e2010-05-29 17:03:36 +00006142 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006143 return;
6144 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006145
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006146 // Otherwise, if this was a reference to an LLVM register class, create vregs
6147 // for this reference.
Chris Lattnerb3b44842009-03-24 15:25:07 +00006148 if (const TargetRegisterClass *RC = PhysReg.second) {
6149 RegVT = *RC->vt_begin();
Owen Anderson825b72b2009-08-11 20:47:22 +00006150 if (OpInfo.ConstraintVT == MVT::Other)
Evan Chengfb112882009-03-23 08:01:15 +00006151 ValueVT = RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006152
Evan Chengfb112882009-03-23 08:01:15 +00006153 // Create the appropriate number of virtual registers.
6154 MachineRegisterInfo &RegInfo = MF.getRegInfo();
6155 for (; NumRegs; --NumRegs)
Chris Lattnerb3b44842009-03-24 15:25:07 +00006156 Regs.push_back(RegInfo.createVirtualRegister(RC));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006157
Dan Gohman7451d3e2010-05-29 17:03:36 +00006158 OpInfo.AssignedRegs = RegsForValue(Regs, RegVT, ValueVT);
Evan Chengfb112882009-03-23 08:01:15 +00006159 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006160 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00006161
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006162 // Otherwise, we couldn't allocate enough registers for this.
6163}
6164
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006165/// visitInlineAsm - Handle a call to an InlineAsm object.
6166///
Dan Gohman46510a72010-04-15 01:51:59 +00006167void SelectionDAGBuilder::visitInlineAsm(ImmutableCallSite CS) {
6168 const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006169
6170 /// ConstraintOperands - Information about all of the constraints.
John Thompson44ab89e2010-10-29 17:29:13 +00006171 SDISelAsmOperandInfoVector ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006172
Bill Wendlingba54bca2013-06-19 21:36:55 +00006173 const TargetLowering *TLI = TM.getTargetLowering();
Evan Chengce1cdac2011-05-06 20:52:23 +00006174 TargetLowering::AsmOperandInfoVector
Bill Wendlingba54bca2013-06-19 21:36:55 +00006175 TargetConstraints = TLI->ParseConstraints(CS);
Evan Chengce1cdac2011-05-06 20:52:23 +00006176
John Thompsoneac6e1d2010-09-13 18:15:37 +00006177 bool hasMemory = false;
Michael J. Spencere70c5262010-10-16 08:25:21 +00006178
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006179 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
6180 unsigned ResNo = 0; // ResNo - The result number of the next output.
John Thompsoneac6e1d2010-09-13 18:15:37 +00006181 for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) {
6182 ConstraintOperands.push_back(SDISelAsmOperandInfo(TargetConstraints[i]));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006183 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Michael J. Spencere70c5262010-10-16 08:25:21 +00006184
Patrik Hagglundc698d3a2012-12-19 15:19:11 +00006185 MVT OpVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006186
6187 // Compute the value type for each operand.
6188 switch (OpInfo.Type) {
6189 case InlineAsm::isOutput:
6190 // Indirect outputs just consume an argument.
6191 if (OpInfo.isIndirect) {
Dan Gohman46510a72010-04-15 01:51:59 +00006192 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006193 break;
6194 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006195
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006196 // The return value of the call is this value. As such, there is no
6197 // corresponding argument.
Nick Lewycky8de34002011-09-30 22:19:53 +00006198 assert(!CS.getType()->isVoidTy() && "Bad inline asm!");
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006199 if (StructType *STy = dyn_cast<StructType>(CS.getType())) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00006200 OpVT = TLI->getSimpleValueType(STy->getElementType(ResNo));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006201 } else {
6202 assert(ResNo == 0 && "Asm only has one result!");
Bill Wendlingba54bca2013-06-19 21:36:55 +00006203 OpVT = TLI->getSimpleValueType(CS.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006204 }
6205 ++ResNo;
6206 break;
6207 case InlineAsm::isInput:
Dan Gohman46510a72010-04-15 01:51:59 +00006208 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006209 break;
6210 case InlineAsm::isClobber:
6211 // Nothing to do.
6212 break;
6213 }
6214
6215 // If this is an input or an indirect output, process the call argument.
6216 // BasicBlocks are labels, currently appearing only in asm's.
6217 if (OpInfo.CallOperandVal) {
Dan Gohman46510a72010-04-15 01:51:59 +00006218 if (const BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006219 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00006220 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006221 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006222 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006223
Bill Wendlingba54bca2013-06-19 21:36:55 +00006224 OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), *TLI, TD).
Patrik Hagglundc698d3a2012-12-19 15:19:11 +00006225 getSimpleVT();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006226 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006227
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006228 OpInfo.ConstraintVT = OpVT;
Michael J. Spencere70c5262010-10-16 08:25:21 +00006229
John Thompsoneac6e1d2010-09-13 18:15:37 +00006230 // Indirect operand accesses access memory.
6231 if (OpInfo.isIndirect)
6232 hasMemory = true;
6233 else {
6234 for (unsigned j = 0, ee = OpInfo.Codes.size(); j != ee; ++j) {
Evan Chengce1cdac2011-05-06 20:52:23 +00006235 TargetLowering::ConstraintType
Bill Wendlingba54bca2013-06-19 21:36:55 +00006236 CType = TLI->getConstraintType(OpInfo.Codes[j]);
John Thompsoneac6e1d2010-09-13 18:15:37 +00006237 if (CType == TargetLowering::C_Memory) {
6238 hasMemory = true;
6239 break;
6240 }
6241 }
6242 }
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006243 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006244
John Thompsoneac6e1d2010-09-13 18:15:37 +00006245 SDValue Chain, Flag;
6246
6247 // We won't need to flush pending loads if this asm doesn't touch
6248 // memory and is nonvolatile.
6249 if (hasMemory || IA->hasSideEffects())
6250 Chain = getRoot();
6251 else
6252 Chain = DAG.getRoot();
6253
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006254 // Second pass over the constraints: compute which constraint option to use
6255 // and assign registers to constraints that want a specific physreg.
John Thompsoneac6e1d2010-09-13 18:15:37 +00006256 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006257 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006258
John Thompson54584742010-09-24 22:24:05 +00006259 // If this is an output operand with a matching input operand, look up the
6260 // matching input. If their types mismatch, e.g. one is an integer, the
6261 // other is floating point, or their sizes are different, flag it as an
6262 // error.
6263 if (OpInfo.hasMatchingInput()) {
6264 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
Michael J. Spencere70c5262010-10-16 08:25:21 +00006265
John Thompson54584742010-09-24 22:24:05 +00006266 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Bill Wendling96cb1122012-07-19 00:04:14 +00006267 std::pair<unsigned, const TargetRegisterClass*> MatchRC =
Bill Wendlingba54bca2013-06-19 21:36:55 +00006268 TLI->getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
6269 OpInfo.ConstraintVT);
Bill Wendling96cb1122012-07-19 00:04:14 +00006270 std::pair<unsigned, const TargetRegisterClass*> InputRC =
Bill Wendlingba54bca2013-06-19 21:36:55 +00006271 TLI->getRegForInlineAsmConstraint(Input.ConstraintCode,
6272 Input.ConstraintVT);
John Thompson54584742010-09-24 22:24:05 +00006273 if ((OpInfo.ConstraintVT.isInteger() !=
6274 Input.ConstraintVT.isInteger()) ||
Eric Christopher5427ede2011-07-14 20:13:52 +00006275 (MatchRC.second != InputRC.second)) {
John Thompson54584742010-09-24 22:24:05 +00006276 report_fatal_error("Unsupported asm: input constraint"
6277 " with a matching output constraint of"
6278 " incompatible type!");
6279 }
6280 Input.ConstraintVT = OpInfo.ConstraintVT;
6281 }
6282 }
6283
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006284 // Compute the constraint code and ConstraintType to use.
Bill Wendlingba54bca2013-06-19 21:36:55 +00006285 TLI->ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006286
Eric Christopherfffe3632013-01-11 18:12:39 +00006287 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
6288 OpInfo.Type == InlineAsm::isClobber)
6289 continue;
6290
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006291 // If this is a memory input, and if the operand is not indirect, do what we
6292 // need to to provide an address for the memory input.
6293 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
6294 !OpInfo.isIndirect) {
Evan Chengce1cdac2011-05-06 20:52:23 +00006295 assert((OpInfo.isMultipleAlternative ||
6296 (OpInfo.Type == InlineAsm::isInput)) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006297 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006298
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006299 // Memory operands really want the address of the value. If we don't have
6300 // an indirect input, put it in the constpool if we can, otherwise spill
6301 // it to a stack slot.
Eric Christophere0b42c02011-06-03 17:21:23 +00006302 // TODO: This isn't quite right. We need to handle these according to
6303 // the addressing mode that the constraint wants. Also, this may take
6304 // an additional register for the computation and we don't want that
6305 // either.
Eric Christopher471e4222011-06-08 23:55:35 +00006306
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006307 // If the operand is a float, integer, or vector constant, spill to a
6308 // constant pool entry to get its address.
Dan Gohman46510a72010-04-15 01:51:59 +00006309 const Value *OpVal = OpInfo.CallOperandVal;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006310 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
Chris Lattnera78fa8c2012-01-27 03:08:05 +00006311 isa<ConstantVector>(OpVal) || isa<ConstantDataVector>(OpVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006312 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
Bill Wendlingba54bca2013-06-19 21:36:55 +00006313 TLI->getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006314 } else {
6315 // Otherwise, create a stack slot and emit a store to it before the
6316 // asm.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006317 Type *Ty = OpVal->getType();
Bill Wendlingba54bca2013-06-19 21:36:55 +00006318 uint64_t TySize = TLI->getDataLayout()->getTypeAllocSize(Ty);
6319 unsigned Align = TLI->getDataLayout()->getPrefTypeAlignment(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006320 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006321 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Bill Wendlingba54bca2013-06-19 21:36:55 +00006322 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI->getPointerTy());
Andrew Trickac6d9be2013-05-25 02:42:55 +00006323 Chain = DAG.getStore(Chain, getCurSDLoc(),
Chris Lattnerecf42c42010-09-21 16:36:31 +00006324 OpInfo.CallOperand, StackSlot,
6325 MachinePointerInfo::getFixedStack(SSFI),
David Greene1e559442010-02-15 17:00:31 +00006326 false, false, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006327 OpInfo.CallOperand = StackSlot;
6328 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006329
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006330 // There is no longer a Value* corresponding to this operand.
6331 OpInfo.CallOperandVal = 0;
Bill Wendling651ad132009-12-22 01:25:10 +00006332
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006333 // It is now an indirect operand.
6334 OpInfo.isIndirect = true;
6335 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006336
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006337 // If this constraint is for a specific register, allocate it before
6338 // anything else.
6339 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Bill Wendlingba54bca2013-06-19 21:36:55 +00006340 GetRegistersForValue(DAG, *TLI, getCurSDLoc(), OpInfo);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006341 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006342
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006343 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00006344 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006345 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
6346 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006347
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006348 // C_Register operands have already been allocated, Other/Memory don't need
6349 // to be.
6350 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Bill Wendlingba54bca2013-06-19 21:36:55 +00006351 GetRegistersForValue(DAG, *TLI, getCurSDLoc(), OpInfo);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006352 }
6353
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006354 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
6355 std::vector<SDValue> AsmNodeOperands;
6356 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
6357 AsmNodeOperands.push_back(
Dan Gohmanf2d7fb32010-01-04 21:00:54 +00006358 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(),
Bill Wendlingba54bca2013-06-19 21:36:55 +00006359 TLI->getPointerTy()));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006360
Chris Lattnerdecc2672010-04-07 05:20:54 +00006361 // If we have a !srcloc metadata node associated with it, we want to attach
6362 // this to the ultimately generated inline asm machineinstr. To do this, we
6363 // pass in the third operand as this (potentially null) inline asm MDNode.
6364 const MDNode *SrcLoc = CS.getInstruction()->getMetadata("srcloc");
6365 AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006366
Chad Rosier3d716882012-10-30 19:11:54 +00006367 // Remember the HasSideEffect, AlignStack, AsmDialect, MayLoad and MayStore
6368 // bits as operand 3.
Evan Chengc36b7062011-01-07 23:50:32 +00006369 unsigned ExtraInfo = 0;
6370 if (IA->hasSideEffects())
6371 ExtraInfo |= InlineAsm::Extra_HasSideEffects;
6372 if (IA->isAlignStack())
6373 ExtraInfo |= InlineAsm::Extra_IsAlignStack;
Chad Rosier77fffa62012-09-05 22:17:43 +00006374 // Set the asm dialect.
Chad Rosier2f1d8152012-09-05 22:40:13 +00006375 ExtraInfo |= IA->getDialect() * InlineAsm::Extra_AsmDialect;
Chad Rosier3d716882012-10-30 19:11:54 +00006376
6377 // Determine if this InlineAsm MayLoad or MayStore based on the constraints.
6378 for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) {
6379 TargetLowering::AsmOperandInfo &OpInfo = TargetConstraints[i];
6380
6381 // Compute the constraint code and ConstraintType to use.
Bill Wendlingba54bca2013-06-19 21:36:55 +00006382 TLI->ComputeConstraintToUse(OpInfo, SDValue());
Chad Rosier3d716882012-10-30 19:11:54 +00006383
Chad Rosierdfa4cec2012-10-30 20:01:12 +00006384 // Ideally, we would only check against memory constraints. However, the
6385 // meaning of an other constraint can be target-specific and we can't easily
6386 // reason about it. Therefore, be conservative and set MayLoad/MayStore
6387 // for other constriants as well.
Chad Rosier3d716882012-10-30 19:11:54 +00006388 if (OpInfo.ConstraintType == TargetLowering::C_Memory ||
6389 OpInfo.ConstraintType == TargetLowering::C_Other) {
6390 if (OpInfo.Type == InlineAsm::isInput)
6391 ExtraInfo |= InlineAsm::Extra_MayLoad;
6392 else if (OpInfo.Type == InlineAsm::isOutput)
6393 ExtraInfo |= InlineAsm::Extra_MayStore;
Eric Christopherfffe3632013-01-11 18:12:39 +00006394 else if (OpInfo.Type == InlineAsm::isClobber)
6395 ExtraInfo |= (InlineAsm::Extra_MayLoad | InlineAsm::Extra_MayStore);
Chad Rosier3d716882012-10-30 19:11:54 +00006396 }
6397 }
6398
Evan Chengc36b7062011-01-07 23:50:32 +00006399 AsmNodeOperands.push_back(DAG.getTargetConstant(ExtraInfo,
Bill Wendlingba54bca2013-06-19 21:36:55 +00006400 TLI->getPointerTy()));
Dale Johannesenf1e309e2010-07-02 20:16:09 +00006401
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006402 // Loop over all of the inputs, copying the operand values into the
6403 // appropriate registers and processing the output regs.
6404 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006405
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006406 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
6407 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006408
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006409 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
6410 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
6411
6412 switch (OpInfo.Type) {
6413 case InlineAsm::isOutput: {
6414 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
6415 OpInfo.ConstraintType != TargetLowering::C_Register) {
6416 // Memory output, or 'other' output (e.g. 'X' constraint).
6417 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
6418
6419 // Add information to the INLINEASM node to know about this output.
Chris Lattnerdecc2672010-04-07 05:20:54 +00006420 unsigned OpFlags = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
6421 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags,
Bill Wendlingba54bca2013-06-19 21:36:55 +00006422 TLI->getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006423 AsmNodeOperands.push_back(OpInfo.CallOperand);
6424 break;
6425 }
6426
6427 // Otherwise, this is a register or register class output.
6428
6429 // Copy the output from the appropriate register. Find a register that
6430 // we can use.
Chris Lattnerfcd70902012-01-03 23:51:01 +00006431 if (OpInfo.AssignedRegs.Regs.empty()) {
6432 LLVMContext &Ctx = *DAG.getContext();
Stephen Lin155615d2013-07-08 00:37:03 +00006433 Ctx.emitError(CS.getInstruction(),
Chris Lattnerfcd70902012-01-03 23:51:01 +00006434 "couldn't allocate output register for constraint '" +
Eric Christopher1a54c572013-07-31 01:26:24 +00006435 Twine(OpInfo.ConstraintCode) + "'");
6436 return;
Chris Lattnerfcd70902012-01-03 23:51:01 +00006437 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006438
6439 // If this is an indirect operand, store through the pointer after the
6440 // asm.
6441 if (OpInfo.isIndirect) {
6442 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
6443 OpInfo.CallOperandVal));
6444 } else {
6445 // This is the result value of the call.
Benjamin Kramerf0127052010-01-05 13:12:22 +00006446 assert(!CS.getType()->isVoidTy() && "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006447 // Concatenate this output onto the outputs list.
6448 RetValRegs.append(OpInfo.AssignedRegs);
6449 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006450
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006451 // Add information to the INLINEASM node to know that this register is
6452 // set.
Eric Christopherb0bee812013-07-30 22:50:44 +00006453 OpInfo.AssignedRegs
6454 .AddInlineAsmOperands(OpInfo.isEarlyClobber
6455 ? InlineAsm::Kind_RegDefEarlyClobber
6456 : InlineAsm::Kind_RegDef,
6457 false, 0, DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006458 break;
6459 }
6460 case InlineAsm::isInput: {
6461 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006462
Chris Lattner6bdcda32008-10-17 16:47:46 +00006463 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006464 // If this is required to match an output register we have already set,
6465 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00006466 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006467
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006468 // Scan until we find the definition we already emitted of this operand.
6469 // When we find it, create a RegsForValue operand.
Chris Lattnerdecc2672010-04-07 05:20:54 +00006470 unsigned CurOp = InlineAsm::Op_FirstOperand;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006471 for (; OperandNo; --OperandNo) {
6472 // Advance to the next operand.
Evan Cheng697cbbf2009-03-20 18:03:34 +00006473 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006474 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00006475 assert((InlineAsm::isRegDefKind(OpFlag) ||
6476 InlineAsm::isRegDefEarlyClobberKind(OpFlag) ||
6477 InlineAsm::isMemKind(OpFlag)) && "Skipped past definitions?");
Evan Cheng697cbbf2009-03-20 18:03:34 +00006478 CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006479 }
6480
Evan Cheng697cbbf2009-03-20 18:03:34 +00006481 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006482 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00006483 if (InlineAsm::isRegDefKind(OpFlag) ||
6484 InlineAsm::isRegDefEarlyClobberKind(OpFlag)) {
Evan Cheng697cbbf2009-03-20 18:03:34 +00006485 // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
Chris Lattner6129c372010-04-08 00:09:16 +00006486 if (OpInfo.isIndirect) {
6487 // This happens on gcc/testsuite/gcc.dg/pr8788-1.c
Dan Gohman99be8ae2010-04-19 22:41:47 +00006488 LLVMContext &Ctx = *DAG.getContext();
Eric Christopher1a54c572013-07-31 01:26:24 +00006489 Ctx.emitError(CS.getInstruction(), "inline asm not supported yet:"
6490 " don't know how to handle tied "
6491 "indirect register inputs");
6492 return;
Chris Lattner6129c372010-04-08 00:09:16 +00006493 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00006494
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006495 RegsForValue MatchedRegs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006496 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00006497 MVT RegVT = AsmNodeOperands[CurOp+1].getSimpleValueType();
Evan Chengfb112882009-03-23 08:01:15 +00006498 MatchedRegs.RegVTs.push_back(RegVT);
6499 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
Evan Cheng697cbbf2009-03-20 18:03:34 +00006500 for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag);
Chad Rosier2871ba92013-04-24 22:53:10 +00006501 i != e; ++i) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00006502 if (const TargetRegisterClass *RC = TLI->getRegClassFor(RegVT))
Chad Rosier2871ba92013-04-24 22:53:10 +00006503 MatchedRegs.Regs.push_back(RegInfo.createVirtualRegister(RC));
6504 else {
6505 LLVMContext &Ctx = *DAG.getContext();
Eric Christopher1a54c572013-07-31 01:26:24 +00006506 Ctx.emitError(CS.getInstruction(),
6507 "inline asm error: This value"
Chad Rosier2871ba92013-04-24 22:53:10 +00006508 " type register class is not natively supported!");
Eric Christopher1a54c572013-07-31 01:26:24 +00006509 return;
Chad Rosier2871ba92013-04-24 22:53:10 +00006510 }
6511 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006512 // Use the produced MatchedRegs object to
Andrew Trickac6d9be2013-05-25 02:42:55 +00006513 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurSDLoc(),
Bill Wendlingf18eb582012-09-26 06:16:18 +00006514 Chain, &Flag, CS.getInstruction());
Chris Lattnerdecc2672010-04-07 05:20:54 +00006515 MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse,
Evan Chengfb112882009-03-23 08:01:15 +00006516 true, OpInfo.getMatchedOperand(),
Bill Wendling46ada192010-03-02 01:55:18 +00006517 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006518 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006519 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00006520
Chris Lattnerdecc2672010-04-07 05:20:54 +00006521 assert(InlineAsm::isMemKind(OpFlag) && "Unknown matching constraint!");
6522 assert(InlineAsm::getNumOperandRegisters(OpFlag) == 1 &&
6523 "Unexpected number of operands");
6524 // Add information to the INLINEASM node to know about this input.
6525 // See InlineAsm.h isUseOperandTiedToDef.
6526 OpFlag = InlineAsm::getFlagWordForMatchingOp(OpFlag,
6527 OpInfo.getMatchedOperand());
6528 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag,
Bill Wendlingba54bca2013-06-19 21:36:55 +00006529 TLI->getPointerTy()));
Chris Lattnerdecc2672010-04-07 05:20:54 +00006530 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
6531 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006532 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006533
Dale Johannesenb5611a62010-07-13 20:17:05 +00006534 // Treat indirect 'X' constraint as memory.
Michael J. Spencere70c5262010-10-16 08:25:21 +00006535 if (OpInfo.ConstraintType == TargetLowering::C_Other &&
6536 OpInfo.isIndirect)
Dale Johannesenb5611a62010-07-13 20:17:05 +00006537 OpInfo.ConstraintType = TargetLowering::C_Memory;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006538
Dale Johannesenb5611a62010-07-13 20:17:05 +00006539 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006540 std::vector<SDValue> Ops;
Bill Wendlingba54bca2013-06-19 21:36:55 +00006541 TLI->LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode,
6542 Ops, DAG);
Chris Lattnerfcd70902012-01-03 23:51:01 +00006543 if (Ops.empty()) {
6544 LLVMContext &Ctx = *DAG.getContext();
6545 Ctx.emitError(CS.getInstruction(),
6546 "invalid operand for inline asm constraint '" +
Eric Christopher1a54c572013-07-31 01:26:24 +00006547 Twine(OpInfo.ConstraintCode) + "'");
6548 return;
Chris Lattnerfcd70902012-01-03 23:51:01 +00006549 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006550
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006551 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00006552 unsigned ResOpType =
6553 InlineAsm::getFlagWord(InlineAsm::Kind_Imm, Ops.size());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006554 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Bill Wendlingba54bca2013-06-19 21:36:55 +00006555 TLI->getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006556 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
6557 break;
Chris Lattnerdecc2672010-04-07 05:20:54 +00006558 }
Michael J. Spencere70c5262010-10-16 08:25:21 +00006559
Chris Lattnerdecc2672010-04-07 05:20:54 +00006560 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006561 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
Bill Wendlingba54bca2013-06-19 21:36:55 +00006562 assert(InOperandVal.getValueType() == TLI->getPointerTy() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006563 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006564
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006565 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00006566 unsigned ResOpType = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
Dale Johannesen86b49f82008-09-24 01:07:17 +00006567 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Bill Wendlingba54bca2013-06-19 21:36:55 +00006568 TLI->getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006569 AsmNodeOperands.push_back(InOperandVal);
6570 break;
6571 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006572
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006573 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
6574 OpInfo.ConstraintType == TargetLowering::C_Register) &&
6575 "Unknown constraint type!");
Eric Christopher9eb4f8a2012-07-02 21:16:43 +00006576
6577 // TODO: Support this.
6578 if (OpInfo.isIndirect) {
6579 LLVMContext &Ctx = *DAG.getContext();
6580 Ctx.emitError(CS.getInstruction(),
6581 "Don't know how to handle indirect register inputs yet "
Eric Christopher1a54c572013-07-31 01:26:24 +00006582 "for constraint '" +
6583 Twine(OpInfo.ConstraintCode) + "'");
6584 return;
Eric Christopher9eb4f8a2012-07-02 21:16:43 +00006585 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006586
6587 // Copy the input into the appropriate registers.
Chris Lattnerfcd70902012-01-03 23:51:01 +00006588 if (OpInfo.AssignedRegs.Regs.empty()) {
6589 LLVMContext &Ctx = *DAG.getContext();
Stephen Lin155615d2013-07-08 00:37:03 +00006590 Ctx.emitError(CS.getInstruction(),
Chris Lattnerfcd70902012-01-03 23:51:01 +00006591 "couldn't allocate input reg for constraint '" +
Eric Christopher1a54c572013-07-31 01:26:24 +00006592 Twine(OpInfo.ConstraintCode) + "'");
6593 return;
Chris Lattnerfcd70902012-01-03 23:51:01 +00006594 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006595
Andrew Trickac6d9be2013-05-25 02:42:55 +00006596 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurSDLoc(),
Bill Wendlingf18eb582012-09-26 06:16:18 +00006597 Chain, &Flag, CS.getInstruction());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006598
Chris Lattnerdecc2672010-04-07 05:20:54 +00006599 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse, false, 0,
Bill Wendling46ada192010-03-02 01:55:18 +00006600 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006601 break;
6602 }
6603 case InlineAsm::isClobber: {
6604 // Add the clobbered value to the operand list, so that the register
6605 // allocator is aware that the physreg got clobbered.
6606 if (!OpInfo.AssignedRegs.Regs.empty())
Jakob Stoklund Olesenf792fa92011-06-27 04:08:33 +00006607 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_Clobber,
Bill Wendling46ada192010-03-02 01:55:18 +00006608 false, 0, DAG,
Bill Wendling651ad132009-12-22 01:25:10 +00006609 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006610 break;
6611 }
6612 }
6613 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006614
Chris Lattnerdecc2672010-04-07 05:20:54 +00006615 // Finish up input operands. Set the input chain and add the flag last.
Dale Johannesenf1e309e2010-07-02 20:16:09 +00006616 AsmNodeOperands[InlineAsm::Op_InputChain] = Chain;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006617 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006618
Andrew Trickac6d9be2013-05-25 02:42:55 +00006619 Chain = DAG.getNode(ISD::INLINEASM, getCurSDLoc(),
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006620 DAG.getVTList(MVT::Other, MVT::Glue),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006621 &AsmNodeOperands[0], AsmNodeOperands.size());
6622 Flag = Chain.getValue(1);
6623
6624 // If this asm returns a register value, copy the result from that register
6625 // and set it as the value of the call.
6626 if (!RetValRegs.Regs.empty()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006627 SDValue Val = RetValRegs.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(),
Bill Wendling12931302012-09-26 04:04:19 +00006628 Chain, &Flag, CS.getInstruction());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006629
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006630 // FIXME: Why don't we do this for inline asms with MRVs?
6631 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00006632 EVT ResultType = TLI->getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006633
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006634 // If any of the results of the inline asm is a vector, it may have the
6635 // wrong width/num elts. This can happen for register classes that can
6636 // contain multiple different value types. The preg or vreg allocated may
6637 // not have the same VT as was expected. Convert it to the right type
6638 // with bit_convert.
6639 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006640 Val = DAG.getNode(ISD::BITCAST, getCurSDLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00006641 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00006642
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006643 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006644 ResultType.isInteger() && Val.getValueType().isInteger()) {
6645 // If a result value was tied to an input value, the computed result may
6646 // have a wider width than the expected result. Extract the relevant
6647 // portion.
Andrew Trickac6d9be2013-05-25 02:42:55 +00006648 Val = DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00006649 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006650
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006651 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Chris Lattner0c526442008-10-17 17:52:49 +00006652 }
Dan Gohman95915732008-10-18 01:03:45 +00006653
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006654 setValue(CS.getInstruction(), Val);
Dale Johannesenec65a7d2009-04-14 00:56:56 +00006655 // Don't need to use this as a chain in this case.
6656 if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty())
6657 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006658 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006659
Dan Gohman46510a72010-04-15 01:51:59 +00006660 std::vector<std::pair<SDValue, const Value *> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006661
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006662 // Process indirect outputs, first output all of the flagged copies out of
6663 // physregs.
6664 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
6665 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
Dan Gohman46510a72010-04-15 01:51:59 +00006666 const Value *Ptr = IndirectStoresToEmit[i].second;
Andrew Trickac6d9be2013-05-25 02:42:55 +00006667 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, FuncInfo, getCurSDLoc(),
Bill Wendling12931302012-09-26 04:04:19 +00006668 Chain, &Flag, IA);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006669 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
6670 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006671
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006672 // Emit the non-flagged stores from the physregs.
6673 SmallVector<SDValue, 8> OutChains;
Bill Wendling651ad132009-12-22 01:25:10 +00006674 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006675 SDValue Val = DAG.getStore(Chain, getCurSDLoc(),
Bill Wendling651ad132009-12-22 01:25:10 +00006676 StoresToEmit[i].first,
6677 getValue(StoresToEmit[i].second),
Chris Lattner84bd98a2010-09-21 18:58:22 +00006678 MachinePointerInfo(StoresToEmit[i].second),
David Greene1e559442010-02-15 17:00:31 +00006679 false, false, 0);
Bill Wendling651ad132009-12-22 01:25:10 +00006680 OutChains.push_back(Val);
Bill Wendling651ad132009-12-22 01:25:10 +00006681 }
6682
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006683 if (!OutChains.empty())
Andrew Trickac6d9be2013-05-25 02:42:55 +00006684 Chain = DAG.getNode(ISD::TokenFactor, getCurSDLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006685 &OutChains[0], OutChains.size());
Bill Wendling651ad132009-12-22 01:25:10 +00006686
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006687 DAG.setRoot(Chain);
6688}
6689
Dan Gohman46510a72010-04-15 01:51:59 +00006690void SelectionDAGBuilder::visitVAStart(const CallInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006691 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurSDLoc(),
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006692 MVT::Other, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00006693 getValue(I.getArgOperand(0)),
6694 DAG.getSrcValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006695}
6696
Dan Gohman46510a72010-04-15 01:51:59 +00006697void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) {
Bill Wendlingba54bca2013-06-19 21:36:55 +00006698 const TargetLowering *TLI = TM.getTargetLowering();
6699 const DataLayout &TD = *TLI->getDataLayout();
6700 SDValue V = DAG.getVAArg(TLI->getValueType(I.getType()), getCurSDLoc(),
Dale Johannesena04b7572009-02-03 23:04:43 +00006701 getRoot(), getValue(I.getOperand(0)),
Rafael Espindolacbeeae22010-07-11 04:01:49 +00006702 DAG.getSrcValue(I.getOperand(0)),
Rafael Espindola9d544d02010-07-12 18:11:17 +00006703 TD.getABITypeAlignment(I.getType()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006704 setValue(&I, V);
6705 DAG.setRoot(V.getValue(1));
6706}
6707
Dan Gohman46510a72010-04-15 01:51:59 +00006708void SelectionDAGBuilder::visitVAEnd(const CallInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006709 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurSDLoc(),
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006710 MVT::Other, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00006711 getValue(I.getArgOperand(0)),
6712 DAG.getSrcValue(I.getArgOperand(0))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006713}
6714
Dan Gohman46510a72010-04-15 01:51:59 +00006715void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006716 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurSDLoc(),
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006717 MVT::Other, getRoot(),
Gabor Greif0635f352010-06-25 09:38:13 +00006718 getValue(I.getArgOperand(0)),
6719 getValue(I.getArgOperand(1)),
6720 DAG.getSrcValue(I.getArgOperand(0)),
6721 DAG.getSrcValue(I.getArgOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006722}
6723
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006724/// TargetLowering::LowerCallTo - This is the default LowerCallTo
Dan Gohman98ca4f22009-08-05 01:29:28 +00006725/// implementation, which just calls LowerCall.
6726/// FIXME: When all targets are
6727/// migrated to using LowerCall, this hook should be integrated into SDISel.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006728std::pair<SDValue, SDValue>
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006729TargetLowering::LowerCallTo(TargetLowering::CallLoweringInfo &CLI) const {
Stephen Lin3484da92013-04-30 22:49:28 +00006730 // Handle the incoming return values from the call.
6731 CLI.Ins.clear();
6732 SmallVector<EVT, 4> RetTys;
6733 ComputeValueVTs(*this, CLI.RetTy, RetTys);
6734 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
6735 EVT VT = RetTys[I];
6736 MVT RegisterVT = getRegisterType(CLI.RetTy->getContext(), VT);
6737 unsigned NumRegs = getNumRegisters(CLI.RetTy->getContext(), VT);
6738 for (unsigned i = 0; i != NumRegs; ++i) {
6739 ISD::InputArg MyFlags;
6740 MyFlags.VT = RegisterVT;
6741 MyFlags.Used = CLI.IsReturnValueUsed;
6742 if (CLI.RetSExt)
6743 MyFlags.Flags.setSExt();
6744 if (CLI.RetZExt)
6745 MyFlags.Flags.setZExt();
6746 if (CLI.IsInReg)
6747 MyFlags.Flags.setInReg();
6748 CLI.Ins.push_back(MyFlags);
6749 }
6750 }
6751
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006752 // Handle all of the outgoing arguments.
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006753 CLI.Outs.clear();
6754 CLI.OutVals.clear();
6755 ArgListTy &Args = CLI.Args;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006756 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00006757 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006758 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
6759 for (unsigned Value = 0, NumValues = ValueVTs.size();
6760 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006761 EVT VT = ValueVTs[Value];
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006762 Type *ArgTy = VT.getTypeForEVT(CLI.RetTy->getContext());
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006763 SDValue Op = SDValue(Args[i].Node.getNode(),
6764 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006765 ISD::ArgFlagsTy Flags;
6766 unsigned OriginalAlignment =
Micah Villmow3574eca2012-10-08 16:38:25 +00006767 getDataLayout()->getABITypeAlignment(ArgTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006768
6769 if (Args[i].isZExt)
6770 Flags.setZExt();
6771 if (Args[i].isSExt)
6772 Flags.setSExt();
6773 if (Args[i].isInReg)
6774 Flags.setInReg();
6775 if (Args[i].isSRet)
6776 Flags.setSRet();
6777 if (Args[i].isByVal) {
6778 Flags.setByVal();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006779 PointerType *Ty = cast<PointerType>(Args[i].Ty);
6780 Type *ElementTy = Ty->getElementType();
Micah Villmow3574eca2012-10-08 16:38:25 +00006781 Flags.setByValSize(getDataLayout()->getTypeAllocSize(ElementTy));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006782 // For ByVal, alignment should come from FE. BE will guess if this
6783 // info is not there but there are cases it cannot get right.
Chris Lattner9db20f32011-05-22 23:23:02 +00006784 unsigned FrameAlign;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006785 if (Args[i].Alignment)
6786 FrameAlign = Args[i].Alignment;
Chris Lattner9db20f32011-05-22 23:23:02 +00006787 else
6788 FrameAlign = getByValTypeAlignment(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006789 Flags.setByValAlign(FrameAlign);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006790 }
6791 if (Args[i].isNest)
6792 Flags.setNest();
6793 Flags.setOrigAlign(OriginalAlignment);
6794
Patrik Hagglunddfcf33a2012-12-19 11:48:16 +00006795 MVT PartVT = getRegisterType(CLI.RetTy->getContext(), VT);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006796 unsigned NumParts = getNumRegisters(CLI.RetTy->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006797 SmallVector<SDValue, 4> Parts(NumParts);
6798 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
6799
6800 if (Args[i].isSExt)
6801 ExtendKind = ISD::SIGN_EXTEND;
6802 else if (Args[i].isZExt)
6803 ExtendKind = ISD::ZERO_EXTEND;
6804
Stephen Lin3484da92013-04-30 22:49:28 +00006805 // Conservatively only handle 'returned' on non-vectors for now
6806 if (Args[i].isReturned && !Op.getValueType().isVector()) {
6807 assert(CLI.RetTy == Args[i].Ty && RetTys.size() == NumValues &&
6808 "unexpected use of 'returned'");
6809 // Before passing 'returned' to the target lowering code, ensure that
6810 // either the register MVT and the actual EVT are the same size or that
6811 // the return value and argument are extended in the same way; in these
6812 // cases it's safe to pass the argument register value unchanged as the
6813 // return register value (although it's at the target's option whether
6814 // to do so)
6815 // TODO: allow code generation to take advantage of partially preserved
6816 // registers rather than clobbering the entire register when the
6817 // parameter extension method is not compatible with the return
6818 // extension method
6819 if ((NumParts * PartVT.getSizeInBits() == VT.getSizeInBits()) ||
6820 (ExtendKind != ISD::ANY_EXTEND &&
6821 CLI.RetSExt == Args[i].isSExt && CLI.RetZExt == Args[i].isZExt))
6822 Flags.setReturned();
6823 }
6824
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006825 getCopyToParts(CLI.DAG, CLI.DL, Op, &Parts[0], NumParts,
Bill Wendlingf18eb582012-09-26 06:16:18 +00006826 PartVT, CLI.CS ? CLI.CS->getInstruction() : 0, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006827
Dan Gohman98ca4f22009-08-05 01:29:28 +00006828 for (unsigned j = 0; j != NumParts; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006829 // if it isn't first piece, alignment must be 1
Dan Gohmanc9403652010-07-07 15:54:55 +00006830 ISD::OutputArg MyFlags(Flags, Parts[j].getValueType(),
Manman Ren0a1544d2012-11-01 23:49:58 +00006831 i < CLI.NumFixedArgs,
6832 i, j*Parts[j].getValueType().getStoreSize());
Dan Gohman98ca4f22009-08-05 01:29:28 +00006833 if (NumParts > 1 && j == 0)
6834 MyFlags.Flags.setSplit();
6835 else if (j != 0)
6836 MyFlags.Flags.setOrigAlign(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006837
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006838 CLI.Outs.push_back(MyFlags);
6839 CLI.OutVals.push_back(Parts[j]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006840 }
6841 }
6842 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006843
Dan Gohman98ca4f22009-08-05 01:29:28 +00006844 SmallVector<SDValue, 4> InVals;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006845 CLI.Chain = LowerCall(CLI, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00006846
6847 // Verify that the target's LowerCall behaved as expected.
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006848 assert(CLI.Chain.getNode() && CLI.Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00006849 "LowerCall didn't return a valid chain!");
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006850 assert((!CLI.IsTailCall || InVals.empty()) &&
Dan Gohman5e866062009-08-06 15:37:27 +00006851 "LowerCall emitted a return value for a tail call!");
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006852 assert((CLI.IsTailCall || InVals.size() == CLI.Ins.size()) &&
Dan Gohman5e866062009-08-06 15:37:27 +00006853 "LowerCall didn't emit the correct number of values!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00006854
6855 // For a tail call, the return value is merely live-out and there aren't
6856 // any nodes in the DAG representing it. Return a special value to
6857 // indicate that a tail call has been emitted and no more Instructions
6858 // should be processed in the current block.
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006859 if (CLI.IsTailCall) {
6860 CLI.DAG.setRoot(CLI.Chain);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006861 return std::make_pair(SDValue(), SDValue());
6862 }
6863
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006864 DEBUG(for (unsigned i = 0, e = CLI.Ins.size(); i != e; ++i) {
Evan Chengaf1871f2010-03-11 19:38:18 +00006865 assert(InVals[i].getNode() &&
6866 "LowerCall emitted a null value!");
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006867 assert(EVT(CLI.Ins[i].VT) == InVals[i].getValueType() &&
Evan Chengaf1871f2010-03-11 19:38:18 +00006868 "LowerCall emitted a value with the wrong type!");
6869 });
6870
Dan Gohman98ca4f22009-08-05 01:29:28 +00006871 // Collect the legal value parts into potentially illegal values
6872 // that correspond to the original function's return values.
6873 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006874 if (CLI.RetSExt)
Dan Gohman98ca4f22009-08-05 01:29:28 +00006875 AssertOp = ISD::AssertSext;
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006876 else if (CLI.RetZExt)
Dan Gohman98ca4f22009-08-05 01:29:28 +00006877 AssertOp = ISD::AssertZext;
6878 SmallVector<SDValue, 4> ReturnValues;
6879 unsigned CurReg = 0;
6880 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00006881 EVT VT = RetTys[I];
Patrik Hagglunddfcf33a2012-12-19 11:48:16 +00006882 MVT RegisterVT = getRegisterType(CLI.RetTy->getContext(), VT);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006883 unsigned NumRegs = getNumRegisters(CLI.RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006884
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006885 ReturnValues.push_back(getCopyFromParts(CLI.DAG, CLI.DL, &InVals[CurReg],
Bill Wendling12931302012-09-26 04:04:19 +00006886 NumRegs, RegisterVT, VT, NULL,
Bill Wendling4533cac2010-01-28 21:51:40 +00006887 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00006888 CurReg += NumRegs;
6889 }
6890
6891 // For a function returning void, there is no return value. We can't create
6892 // such a node, so we just return a null return value in that case. In
Chris Lattner7a2bdde2011-04-15 05:18:47 +00006893 // that case, nothing will actually look at the value.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006894 if (ReturnValues.empty())
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006895 return std::make_pair(SDValue(), CLI.Chain);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006896
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006897 SDValue Res = CLI.DAG.getNode(ISD::MERGE_VALUES, CLI.DL,
6898 CLI.DAG.getVTList(&RetTys[0], RetTys.size()),
Dan Gohman98ca4f22009-08-05 01:29:28 +00006899 &ReturnValues[0], ReturnValues.size());
Justin Holewinskid2ea0e12012-05-25 16:35:28 +00006900 return std::make_pair(Res, CLI.Chain);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006901}
6902
Duncan Sands9fbc7e22009-01-21 09:00:29 +00006903void TargetLowering::LowerOperationWrapper(SDNode *N,
6904 SmallVectorImpl<SDValue> &Results,
Dan Gohmand858e902010-04-17 15:26:15 +00006905 SelectionDAG &DAG) const {
Duncan Sands9fbc7e22009-01-21 09:00:29 +00006906 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00006907 if (Res.getNode())
6908 Results.push_back(Res);
6909}
6910
Dan Gohmand858e902010-04-17 15:26:15 +00006911SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Torok Edwinc23197a2009-07-14 16:55:14 +00006912 llvm_unreachable("LowerOperation not implemented for this target!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006913}
6914
Dan Gohman46510a72010-04-15 01:51:59 +00006915void
6916SelectionDAGBuilder::CopyValueToVirtualRegister(const Value *V, unsigned Reg) {
Dan Gohman28a17352010-07-01 01:59:43 +00006917 SDValue Op = getNonRegisterValue(V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006918 assert((Op.getOpcode() != ISD::CopyFromReg ||
6919 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
6920 "Copy from a reg to the same reg!");
6921 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
6922
Bill Wendlingba54bca2013-06-19 21:36:55 +00006923 const TargetLowering *TLI = TM.getTargetLowering();
6924 RegsForValue RFV(V->getContext(), *TLI, Reg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006925 SDValue Chain = DAG.getEntryNode();
Andrew Trickac6d9be2013-05-25 02:42:55 +00006926 RFV.getCopyToRegs(Op, DAG, getCurSDLoc(), Chain, 0, V);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006927 PendingExports.push_back(Chain);
6928}
6929
6930#include "llvm/CodeGen/SelectionDAGISel.h"
6931
Eli Friedman23d32432011-05-05 16:53:34 +00006932/// isOnlyUsedInEntryBlock - If the specified argument is only used in the
6933/// entry block, return true. This includes arguments used by switches, since
6934/// the switch may expand into multiple basic blocks.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006935static bool isOnlyUsedInEntryBlock(const Argument *A, bool FastISel) {
Eli Friedman23d32432011-05-05 16:53:34 +00006936 // With FastISel active, we may be splitting blocks, so force creation
6937 // of virtual registers for all non-dead arguments.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006938 if (FastISel)
Eli Friedman23d32432011-05-05 16:53:34 +00006939 return A->use_empty();
6940
6941 const BasicBlock *Entry = A->getParent()->begin();
6942 for (Value::const_use_iterator UI = A->use_begin(), E = A->use_end();
6943 UI != E; ++UI) {
6944 const User *U = *UI;
6945 if (cast<Instruction>(U)->getParent() != Entry || isa<SwitchInst>(U))
6946 return false; // Use not in entry block.
6947 }
6948 return true;
6949}
6950
Eli Bendersky6437d382013-02-28 23:09:18 +00006951void SelectionDAGISel::LowerArguments(const Function &F) {
Dan Gohman2048b852009-11-23 18:04:58 +00006952 SelectionDAG &DAG = SDB->DAG;
Andrew Trickac6d9be2013-05-25 02:42:55 +00006953 SDLoc dl = SDB->getCurSDLoc();
Bill Wendlingba54bca2013-06-19 21:36:55 +00006954 const TargetLowering *TLI = getTargetLowering();
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006955 const DataLayout *TD = TLI->getDataLayout();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006956 SmallVector<ISD::InputArg, 16> Ins;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006957
Dan Gohman7451d3e2010-05-29 17:03:36 +00006958 if (!FuncInfo->CanLowerReturn) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006959 // Put in an sret pointer parameter before all the other parameters.
6960 SmallVector<EVT, 1> ValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00006961 ComputeValueVTs(*getTargetLowering(),
6962 PointerType::getUnqual(F.getReturnType()), ValueVTs);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006963
6964 // NOTE: Assuming that a pointer will never break down to more than one VT
6965 // or one register.
6966 ISD::ArgFlagsTy Flags;
6967 Flags.setSRet();
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006968 MVT RegisterVT = TLI->getRegisterType(*DAG.getContext(), ValueVTs[0]);
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00006969 ISD::InputArg RetArg(Flags, RegisterVT, true, 0, 0);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006970 Ins.push_back(RetArg);
6971 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00006972
Dan Gohman98ca4f22009-08-05 01:29:28 +00006973 // Set up the incoming argument description vector.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006974 unsigned Idx = 1;
Dan Gohman46510a72010-04-15 01:51:59 +00006975 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
Dan Gohman98ca4f22009-08-05 01:29:28 +00006976 I != E; ++I, ++Idx) {
Owen Andersone50ed302009-08-10 22:56:29 +00006977 SmallVector<EVT, 4> ValueVTs;
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00006978 ComputeValueVTs(*TLI, I->getType(), ValueVTs);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006979 bool isArgValueUsed = !I->use_empty();
6980 for (unsigned Value = 0, NumValues = ValueVTs.size();
6981 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006982 EVT VT = ValueVTs[Value];
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006983 Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00006984 ISD::ArgFlagsTy Flags;
6985 unsigned OriginalAlignment =
6986 TD->getABITypeAlignment(ArgTy);
6987
Bill Wendling39cd0c82012-12-30 12:45:13 +00006988 if (F.getAttributes().hasAttribute(Idx, Attribute::ZExt))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006989 Flags.setZExt();
Bill Wendling39cd0c82012-12-30 12:45:13 +00006990 if (F.getAttributes().hasAttribute(Idx, Attribute::SExt))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006991 Flags.setSExt();
Bill Wendling39cd0c82012-12-30 12:45:13 +00006992 if (F.getAttributes().hasAttribute(Idx, Attribute::InReg))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006993 Flags.setInReg();
Bill Wendling39cd0c82012-12-30 12:45:13 +00006994 if (F.getAttributes().hasAttribute(Idx, Attribute::StructRet))
Dan Gohman98ca4f22009-08-05 01:29:28 +00006995 Flags.setSRet();
Bill Wendling39cd0c82012-12-30 12:45:13 +00006996 if (F.getAttributes().hasAttribute(Idx, Attribute::ByVal)) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00006997 Flags.setByVal();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00006998 PointerType *Ty = cast<PointerType>(I->getType());
6999 Type *ElementTy = Ty->getElementType();
Chris Lattner9db20f32011-05-22 23:23:02 +00007000 Flags.setByValSize(TD->getTypeAllocSize(ElementTy));
Dan Gohman98ca4f22009-08-05 01:29:28 +00007001 // For ByVal, alignment should be passed from FE. BE will guess if
7002 // this info is not there but there are cases it cannot get right.
Chris Lattner9db20f32011-05-22 23:23:02 +00007003 unsigned FrameAlign;
Dan Gohman98ca4f22009-08-05 01:29:28 +00007004 if (F.getParamAlignment(Idx))
7005 FrameAlign = F.getParamAlignment(Idx);
Chris Lattner9db20f32011-05-22 23:23:02 +00007006 else
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00007007 FrameAlign = TLI->getByValTypeAlignment(ElementTy);
Dan Gohman98ca4f22009-08-05 01:29:28 +00007008 Flags.setByValAlign(FrameAlign);
Dan Gohman98ca4f22009-08-05 01:29:28 +00007009 }
Bill Wendling39cd0c82012-12-30 12:45:13 +00007010 if (F.getAttributes().hasAttribute(Idx, Attribute::Nest))
Dan Gohman98ca4f22009-08-05 01:29:28 +00007011 Flags.setNest();
7012 Flags.setOrigAlign(OriginalAlignment);
7013
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00007014 MVT RegisterVT = TLI->getRegisterType(*CurDAG->getContext(), VT);
7015 unsigned NumRegs = TLI->getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00007016 for (unsigned i = 0; i != NumRegs; ++i) {
Stepan Dyatkovskiy661afe72012-10-10 11:37:36 +00007017 ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed,
7018 Idx-1, i*RegisterVT.getStoreSize());
Dan Gohman98ca4f22009-08-05 01:29:28 +00007019 if (NumRegs > 1 && i == 0)
7020 MyFlags.Flags.setSplit();
7021 // if it isn't first piece, alignment must be 1
7022 else if (i > 0)
7023 MyFlags.Flags.setOrigAlign(1);
7024 Ins.push_back(MyFlags);
7025 }
7026 }
7027 }
7028
7029 // Call the target to set up the argument values.
7030 SmallVector<SDValue, 8> InVals;
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00007031 SDValue NewRoot = TLI->LowerFormalArguments(DAG.getRoot(), F.getCallingConv(),
7032 F.isVarArg(), Ins,
7033 dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00007034
7035 // Verify that the target's LowerFormalArguments behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00007036 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00007037 "LowerFormalArguments didn't return a valid chain!");
7038 assert(InVals.size() == Ins.size() &&
7039 "LowerFormalArguments didn't emit the correct number of values!");
Bill Wendling3ea58b62009-12-22 21:35:02 +00007040 DEBUG({
7041 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
7042 assert(InVals[i].getNode() &&
7043 "LowerFormalArguments emitted a null value!");
Duncan Sands1440e8b2010-11-03 11:35:31 +00007044 assert(EVT(Ins[i].VT) == InVals[i].getValueType() &&
Bill Wendling3ea58b62009-12-22 21:35:02 +00007045 "LowerFormalArguments emitted a value with the wrong type!");
7046 }
7047 });
Bill Wendling3ea3c242009-12-22 02:10:19 +00007048
Dan Gohman5e866062009-08-06 15:37:27 +00007049 // Update the DAG with the new chain value resulting from argument lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00007050 DAG.setRoot(NewRoot);
7051
7052 // Set up the argument values.
7053 unsigned i = 0;
7054 Idx = 1;
Dan Gohman7451d3e2010-05-29 17:03:36 +00007055 if (!FuncInfo->CanLowerReturn) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00007056 // Create a virtual register for the sret pointer, and put in a copy
7057 // from the sret argument into it.
7058 SmallVector<EVT, 1> ValueVTs;
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00007059 ComputeValueVTs(*TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
Patrik Hagglunda61b17c2012-12-13 06:34:11 +00007060 MVT VT = ValueVTs[0].getSimpleVT();
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00007061 MVT RegVT = TLI->getRegisterType(*CurDAG->getContext(), VT);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00007062 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Bill Wendling46ada192010-03-02 01:55:18 +00007063 SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1,
Bill Wendling12931302012-09-26 04:04:19 +00007064 RegVT, VT, NULL, AssertOp);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00007065
Dan Gohman2048b852009-11-23 18:04:58 +00007066 MachineFunction& MF = SDB->DAG.getMachineFunction();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00007067 MachineRegisterInfo& RegInfo = MF.getRegInfo();
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00007068 unsigned SRetReg = RegInfo.createVirtualRegister(TLI->getRegClassFor(RegVT));
Dan Gohman7451d3e2010-05-29 17:03:36 +00007069 FuncInfo->DemoteRegister = SRetReg;
Andrew Trickac6d9be2013-05-25 02:42:55 +00007070 NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurSDLoc(),
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00007071 SRetReg, ArgValue);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00007072 DAG.setRoot(NewRoot);
Bill Wendling3ea3c242009-12-22 02:10:19 +00007073
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00007074 // i indexes lowered arguments. Bump it past the hidden sret argument.
7075 // Idx indexes LLVM arguments. Don't touch it.
7076 ++i;
7077 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00007078
Dan Gohman46510a72010-04-15 01:51:59 +00007079 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
Dan Gohman98ca4f22009-08-05 01:29:28 +00007080 ++I, ++Idx) {
7081 SmallVector<SDValue, 4> ArgValues;
Owen Andersone50ed302009-08-10 22:56:29 +00007082 SmallVector<EVT, 4> ValueVTs;
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00007083 ComputeValueVTs(*TLI, I->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007084 unsigned NumValues = ValueVTs.size();
Devang Patel9126c0d2010-06-01 19:59:01 +00007085
7086 // If this argument is unused then remember its value. It is used to generate
7087 // debugging information.
Adrian Prantldf688032013-05-16 23:44:12 +00007088 if (I->use_empty() && NumValues) {
Devang Patel9126c0d2010-06-01 19:59:01 +00007089 SDB->setUnusedArgValue(I, InVals[i]);
7090
Adrian Prantldf688032013-05-16 23:44:12 +00007091 // Also remember any frame index for use in FastISel.
7092 if (FrameIndexSDNode *FI =
7093 dyn_cast<FrameIndexSDNode>(InVals[i].getNode()))
7094 FuncInfo->setArgumentFrameIndex(I, FI->getIndex());
7095 }
7096
Eli Friedman23d32432011-05-05 16:53:34 +00007097 for (unsigned Val = 0; Val != NumValues; ++Val) {
7098 EVT VT = ValueVTs[Val];
Bill Wendling6a2e7ac2013-06-06 00:43:09 +00007099 MVT PartVT = TLI->getRegisterType(*CurDAG->getContext(), VT);
7100 unsigned NumParts = TLI->getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00007101
7102 if (!I->use_empty()) {
7103 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Bill Wendling39cd0c82012-12-30 12:45:13 +00007104 if (F.getAttributes().hasAttribute(Idx, Attribute::SExt))
Dan Gohman98ca4f22009-08-05 01:29:28 +00007105 AssertOp = ISD::AssertSext;
Bill Wendling39cd0c82012-12-30 12:45:13 +00007106 else if (F.getAttributes().hasAttribute(Idx, Attribute::ZExt))
Dan Gohman98ca4f22009-08-05 01:29:28 +00007107 AssertOp = ISD::AssertZext;
7108
Bill Wendling46ada192010-03-02 01:55:18 +00007109 ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i],
Bill Wendling3ea3c242009-12-22 02:10:19 +00007110 NumParts, PartVT, VT,
Bill Wendling12931302012-09-26 04:04:19 +00007111 NULL, AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00007112 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00007113
Dan Gohman98ca4f22009-08-05 01:29:28 +00007114 i += NumParts;
7115 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00007116
Eli Friedman23d32432011-05-05 16:53:34 +00007117 // We don't need to do anything else for unused arguments.
7118 if (ArgValues.empty())
7119 continue;
7120
Devang Patel9aee3352011-09-08 22:59:09 +00007121 // Note down frame index.
7122 if (FrameIndexSDNode *FI =
Bill Wendling96cb1122012-07-19 00:04:14 +00007123 dyn_cast<FrameIndexSDNode>(ArgValues[0].getNode()))
Devang Patel9aee3352011-09-08 22:59:09 +00007124 FuncInfo->setArgumentFrameIndex(I, FI->getIndex());
Devang Patel0b48ead2010-08-31 22:22:42 +00007125
Eli Friedman23d32432011-05-05 16:53:34 +00007126 SDValue Res = DAG.getMergeValues(&ArgValues[0], NumValues,
Andrew Trickac6d9be2013-05-25 02:42:55 +00007127 SDB->getCurSDLoc());
Devang Patel9aee3352011-09-08 22:59:09 +00007128
Eli Friedman23d32432011-05-05 16:53:34 +00007129 SDB->setValue(I, Res);
Nick Lewycky8a8d4792011-12-02 22:16:29 +00007130 if (!TM.Options.EnableFastISel && Res.getOpcode() == ISD::BUILD_PAIR) {
Stephen Lin155615d2013-07-08 00:37:03 +00007131 if (LoadSDNode *LNode =
Devang Patel9aee3352011-09-08 22:59:09 +00007132 dyn_cast<LoadSDNode>(Res.getOperand(0).getNode()))
7133 if (FrameIndexSDNode *FI =
7134 dyn_cast<FrameIndexSDNode>(LNode->getBasePtr().getNode()))
7135 FuncInfo->setArgumentFrameIndex(I, FI->getIndex());
7136 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00007137
Eli Friedman23d32432011-05-05 16:53:34 +00007138 // If this argument is live outside of the entry block, insert a copy from
7139 // wherever we got it to the vreg that other BB's will reference it as.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00007140 if (!TM.Options.EnableFastISel && Res.getOpcode() == ISD::CopyFromReg) {
Eli Friedman23d32432011-05-05 16:53:34 +00007141 // If we can, though, try to skip creating an unnecessary vreg.
7142 // FIXME: This isn't very clean... it would be nice to make this more
Eli Friedman7f33d672011-05-10 21:50:58 +00007143 // general. It's also subtly incompatible with the hacks FastISel
7144 // uses with vregs.
Eli Friedman23d32432011-05-05 16:53:34 +00007145 unsigned Reg = cast<RegisterSDNode>(Res.getOperand(1))->getReg();
7146 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
7147 FuncInfo->ValueMap[I] = Reg;
7148 continue;
7149 }
7150 }
Nick Lewycky8a8d4792011-12-02 22:16:29 +00007151 if (!isOnlyUsedInEntryBlock(I, TM.Options.EnableFastISel)) {
Eli Friedman23d32432011-05-05 16:53:34 +00007152 FuncInfo->InitializeRegForValue(I);
Dan Gohman2048b852009-11-23 18:04:58 +00007153 SDB->CopyToExportRegsIfNeeded(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007154 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007155 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00007156
Dan Gohman98ca4f22009-08-05 01:29:28 +00007157 assert(i == InVals.size() && "Argument register count mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007158
7159 // Finally, if the target has anything special to do, allow it to do so.
7160 // FIXME: this should insert code into the DAG!
Dan Gohman64652652010-04-14 20:17:22 +00007161 EmitFunctionEntryCode();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007162}
7163
7164/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
7165/// ensure constants are generated when needed. Remember the virtual registers
7166/// that need to be added to the Machine PHI nodes as input. We cannot just
7167/// directly add them, because expansion might result in multiple MBB's for one
7168/// BB. As such, the start of the BB might correspond to a different MBB than
7169/// the end.
7170///
7171void
Dan Gohmanf81eca02010-04-22 20:46:50 +00007172SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
Dan Gohman46510a72010-04-15 01:51:59 +00007173 const TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007174
7175 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
7176
7177 // Check successor nodes' PHI nodes that expect a constant to be available
7178 // from this block.
7179 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
Dan Gohman46510a72010-04-15 01:51:59 +00007180 const BasicBlock *SuccBB = TI->getSuccessor(succ);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007181 if (!isa<PHINode>(SuccBB->begin())) continue;
Dan Gohmanf81eca02010-04-22 20:46:50 +00007182 MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00007183
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007184 // If this terminator has multiple identical successors (common for
7185 // switches), only handle each succ once.
7186 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00007187
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007188 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007189
7190 // At this point we know that there is a 1-1 correspondence between LLVM PHI
7191 // nodes and Machine PHI nodes, but the incoming operands have not been
7192 // emitted yet.
Dan Gohman46510a72010-04-15 01:51:59 +00007193 for (BasicBlock::const_iterator I = SuccBB->begin();
7194 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007195 // Ignore dead phi's.
7196 if (PN->use_empty()) continue;
7197
Rafael Espindola3fa82832011-05-13 15:18:06 +00007198 // Skip empty types
7199 if (PN->getType()->isEmptyTy())
7200 continue;
7201
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007202 unsigned Reg;
Dan Gohman46510a72010-04-15 01:51:59 +00007203 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007204
Dan Gohman46510a72010-04-15 01:51:59 +00007205 if (const Constant *C = dyn_cast<Constant>(PHIOp)) {
Dan Gohmanf81eca02010-04-22 20:46:50 +00007206 unsigned &RegOut = ConstantsOut[C];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007207 if (RegOut == 0) {
Dan Gohman89496d02010-07-02 00:10:16 +00007208 RegOut = FuncInfo.CreateRegs(C->getType());
Dan Gohmanf81eca02010-04-22 20:46:50 +00007209 CopyValueToVirtualRegister(C, RegOut);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007210 }
7211 Reg = RegOut;
7212 } else {
Dan Gohmanc25ad632010-07-01 01:33:21 +00007213 DenseMap<const Value *, unsigned>::iterator I =
7214 FuncInfo.ValueMap.find(PHIOp);
7215 if (I != FuncInfo.ValueMap.end())
7216 Reg = I->second;
7217 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007218 assert(isa<AllocaInst>(PHIOp) &&
Dan Gohmanf81eca02010-04-22 20:46:50 +00007219 FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007220 "Didn't codegen value into a register!??");
Dan Gohman89496d02010-07-02 00:10:16 +00007221 Reg = FuncInfo.CreateRegs(PHIOp->getType());
Dan Gohmanf81eca02010-04-22 20:46:50 +00007222 CopyValueToVirtualRegister(PHIOp, Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007223 }
7224 }
7225
7226 // Remember that this register needs to added to the machine PHI node as
7227 // the input for this MBB.
Owen Andersone50ed302009-08-10 22:56:29 +00007228 SmallVector<EVT, 4> ValueVTs;
Bill Wendlingba54bca2013-06-19 21:36:55 +00007229 const TargetLowering *TLI = TM.getTargetLowering();
7230 ComputeValueVTs(*TLI, PN->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007231 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +00007232 EVT VT = ValueVTs[vti];
Bill Wendlingba54bca2013-06-19 21:36:55 +00007233 unsigned NumRegisters = TLI->getNumRegisters(*DAG.getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007234 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
Dan Gohmanf81eca02010-04-22 20:46:50 +00007235 FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00007236 Reg += NumRegisters;
7237 }
7238 }
7239 }
Bill Wendlingba54bca2013-06-19 21:36:55 +00007240
Dan Gohmanf81eca02010-04-22 20:46:50 +00007241 ConstantsOut.clear();
Dan Gohman3df24e62008-09-03 23:12:08 +00007242}
Michael Gottesman657484f2013-08-20 07:00:16 +00007243
7244/// Add a successor MBB to ParentMBB< creating a new MachineBB for BB if SuccMBB
7245/// is 0.
7246MachineBasicBlock *
7247SelectionDAGBuilder::StackProtectorDescriptor::
7248AddSuccessorMBB(const BasicBlock *BB,
7249 MachineBasicBlock *ParentMBB,
7250 MachineBasicBlock *SuccMBB) {
7251 // If SuccBB has not been created yet, create it.
7252 if (!SuccMBB) {
7253 MachineFunction *MF = ParentMBB->getParent();
7254 MachineFunction::iterator BBI = ParentMBB;
7255 SuccMBB = MF->CreateMachineBasicBlock(BB);
7256 MF->insert(++BBI, SuccMBB);
7257 }
7258 // Add it as a successor of ParentMBB.
7259 ParentMBB->addSuccessor(SuccMBB);
7260 return SuccMBB;
7261}