blob: ade38a5121cc30fcbd5ea2aad9c277463cb951bd [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"
Devang Patel00190342010-03-15 19:15:44 +000015#include "SDNodeDbgValue.h"
Dan Gohman2048b852009-11-23 18:04:58 +000016#include "SelectionDAGBuilder.h"
Dan Gohman6277eb22009-11-23 17:16:22 +000017#include "FunctionLoweringInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000018#include "llvm/ADT/BitVector.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"
Chris Lattner8047d9a2009-12-24 00:37:38 +000021#include "llvm/Analysis/ConstantFolding.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000022#include "llvm/Constants.h"
23#include "llvm/CallingConv.h"
24#include "llvm/DerivedTypes.h"
25#include "llvm/Function.h"
26#include "llvm/GlobalVariable.h"
27#include "llvm/InlineAsm.h"
28#include "llvm/Instructions.h"
29#include "llvm/Intrinsics.h"
30#include "llvm/IntrinsicInst.h"
Chris Lattner6129c372010-04-08 00:09:16 +000031#include "llvm/LLVMContext.h"
Bill Wendlingb2a42982008-11-06 02:29:10 +000032#include "llvm/Module.h"
Dan Gohman5eb6d652010-04-21 01:22:34 +000033#include "llvm/CodeGen/Analysis.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000034#include "llvm/CodeGen/FastISel.h"
35#include "llvm/CodeGen/GCStrategy.h"
36#include "llvm/CodeGen/GCMetadata.h"
37#include "llvm/CodeGen/MachineFunction.h"
38#include "llvm/CodeGen/MachineFrameInfo.h"
39#include "llvm/CodeGen/MachineInstrBuilder.h"
40#include "llvm/CodeGen/MachineJumpTableInfo.h"
41#include "llvm/CodeGen/MachineModuleInfo.h"
42#include "llvm/CodeGen/MachineRegisterInfo.h"
Bill Wendlingb2a42982008-11-06 02:29:10 +000043#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000044#include "llvm/CodeGen/SelectionDAG.h"
Devang Patel83489bb2009-01-13 00:35:13 +000045#include "llvm/Analysis/DebugInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000046#include "llvm/Target/TargetRegisterInfo.h"
47#include "llvm/Target/TargetData.h"
48#include "llvm/Target/TargetFrameInfo.h"
49#include "llvm/Target/TargetInstrInfo.h"
Dale Johannesen49de9822009-02-05 01:49:45 +000050#include "llvm/Target/TargetIntrinsicInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000051#include "llvm/Target/TargetLowering.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000052#include "llvm/Target/TargetOptions.h"
53#include "llvm/Support/Compiler.h"
Mikhail Glushenkov2388a582009-01-16 07:02:28 +000054#include "llvm/Support/CommandLine.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000055#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000056#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000057#include "llvm/Support/MathExtras.h"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +000058#include "llvm/Support/raw_ostream.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000059#include <algorithm>
60using namespace llvm;
61
Dale Johannesen601d3c02008-09-05 01:48:15 +000062/// LimitFloatPrecision - Generate low-precision inline sequences for
63/// some float libcalls (6, 8 or 12 bits).
64static unsigned LimitFloatPrecision;
65
66static cl::opt<unsigned, true>
67LimitFPPrecision("limit-float-precision",
68 cl::desc("Generate low-precision inline sequences "
69 "for some float libcalls"),
70 cl::location(LimitFloatPrecision),
71 cl::init(0));
72
Dan Gohmanf9bd4502009-11-23 17:46:23 +000073namespace {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000074 /// RegsForValue - This struct represents the registers (physical or virtual)
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +000075 /// that a particular set of values is assigned, and the type information
76 /// about the value. The most common situation is to represent one value at a
77 /// time, but struct or array values are handled element-wise as multiple
78 /// values. The splitting of aggregates is performed recursively, so that we
79 /// never have aggregate-typed registers. The values at this point do not
80 /// necessarily have legal types, so each value may require one or more
81 /// registers of some legal type.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000082 ///
Dan Gohmanf9bd4502009-11-23 17:46:23 +000083 struct RegsForValue {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000084 /// TLI - The TargetLowering object.
85 ///
86 const TargetLowering *TLI;
87
88 /// ValueVTs - The value types of the values, which may not be legal, and
89 /// may need be promoted or synthesized from one or more registers.
90 ///
Owen Andersone50ed302009-08-10 22:56:29 +000091 SmallVector<EVT, 4> ValueVTs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000092
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000093 /// RegVTs - The value types of the registers. This is the same size as
94 /// ValueVTs and it records, for each value, what the type of the assigned
95 /// register or registers are. (Individual values are never synthesized
96 /// from more than one type of register.)
97 ///
98 /// With virtual registers, the contents of RegVTs is redundant with TLI's
99 /// getRegisterType member function, however when with physical registers
100 /// it is necessary to have a separate record of the types.
101 ///
Owen Andersone50ed302009-08-10 22:56:29 +0000102 SmallVector<EVT, 4> RegVTs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000103
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000104 /// Regs - This list holds the registers assigned to the values.
105 /// Each legal or promoted value requires one register, and each
106 /// expanded value requires multiple registers.
107 ///
108 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000109
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000110 RegsForValue() : TLI(0) {}
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000111
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000112 RegsForValue(const TargetLowering &tli,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000113 const SmallVector<unsigned, 4> &regs,
Owen Andersone50ed302009-08-10 22:56:29 +0000114 EVT regvt, EVT valuevt)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000115 : TLI(&tli), ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs) {}
116 RegsForValue(const TargetLowering &tli,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000117 const SmallVector<unsigned, 4> &regs,
Owen Andersone50ed302009-08-10 22:56:29 +0000118 const SmallVector<EVT, 4> &regvts,
119 const SmallVector<EVT, 4> &valuevts)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000120 : TLI(&tli), ValueVTs(valuevts), RegVTs(regvts), Regs(regs) {}
Owen Anderson23b9b192009-08-12 00:36:31 +0000121 RegsForValue(LLVMContext &Context, const TargetLowering &tli,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000122 unsigned Reg, const Type *Ty) : TLI(&tli) {
123 ComputeValueVTs(tli, Ty, ValueVTs);
124
125 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +0000126 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +0000127 unsigned NumRegs = TLI->getNumRegisters(Context, ValueVT);
128 EVT RegisterVT = TLI->getRegisterType(Context, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000129 for (unsigned i = 0; i != NumRegs; ++i)
130 Regs.push_back(Reg + i);
131 RegVTs.push_back(RegisterVT);
132 Reg += NumRegs;
133 }
134 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000135
Evan Cheng8112b532010-02-10 01:21:02 +0000136 /// areValueTypesLegal - Return true if types of all the values are legal.
137 bool areValueTypesLegal() {
138 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
139 EVT RegisterVT = RegVTs[Value];
140 if (!TLI->isTypeLegal(RegisterVT))
141 return false;
142 }
143 return true;
144 }
145
146
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000147 /// append - Add the specified values to this one.
148 void append(const RegsForValue &RHS) {
149 TLI = RHS.TLI;
150 ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end());
151 RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end());
152 Regs.append(RHS.Regs.begin(), RHS.Regs.end());
153 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000154
155
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000156 /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000157 /// this value and returns the result as a ValueVTs value. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000158 /// Chain/Flag as the input and updates them for the output Chain/Flag.
159 /// If the Flag pointer is NULL, no flag is used.
Bill Wendling46ada192010-03-02 01:55:18 +0000160 SDValue getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl,
Bill Wendlingec72e322009-12-22 01:11:43 +0000161 SDValue &Chain, SDValue *Flag) const;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000162
163 /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000164 /// specified value into the registers specified by this object. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000165 /// Chain/Flag as the input and updates them for the output Chain/Flag.
166 /// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +0000167 void getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
Bill Wendling46ada192010-03-02 01:55:18 +0000168 SDValue &Chain, SDValue *Flag) const;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000169
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000170 /// AddInlineAsmOperands - Add this value to the specified inlineasm node
Evan Cheng697cbbf2009-03-20 18:03:34 +0000171 /// operand list. This adds the code marker, matching input operand index
172 /// (if applicable), and includes the number of values added into it.
Chris Lattnerdecc2672010-04-07 05:20:54 +0000173 void AddInlineAsmOperands(unsigned Kind,
Evan Cheng697cbbf2009-03-20 18:03:34 +0000174 bool HasMatching, unsigned MatchingIdx,
Bill Wendling46ada192010-03-02 01:55:18 +0000175 SelectionDAG &DAG,
Bill Wendling651ad132009-12-22 01:25:10 +0000176 std::vector<SDValue> &Ops) const;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000177 };
178}
179
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000180/// getCopyFromParts - Create a value that contains the specified legal parts
181/// combined into the value they represent. If the parts combine to a type
182/// larger then ValueVT then AssertOp can be used to specify whether the extra
183/// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT
184/// (ISD::AssertSext).
Bill Wendling46ada192010-03-02 01:55:18 +0000185static SDValue getCopyFromParts(SelectionDAG &DAG, DebugLoc dl,
Dale Johannesen66978ee2009-01-31 02:22:37 +0000186 const SDValue *Parts,
Owen Andersone50ed302009-08-10 22:56:29 +0000187 unsigned NumParts, EVT PartVT, EVT ValueVT,
Duncan Sands0b3aa262009-01-28 14:42:54 +0000188 ISD::NodeType AssertOp = ISD::DELETED_NODE) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000189 assert(NumParts > 0 && "No parts to assemble!");
Dan Gohmane9530ec2009-01-15 16:58:17 +0000190 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000191 SDValue Val = Parts[0];
192
193 if (NumParts > 1) {
194 // Assemble the value from multiple parts.
Eli Friedman2ac8b322009-05-20 06:02:09 +0000195 if (!ValueVT.isVector() && ValueVT.isInteger()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000196 unsigned PartBits = PartVT.getSizeInBits();
197 unsigned ValueBits = ValueVT.getSizeInBits();
198
199 // Assemble the power of 2 part.
200 unsigned RoundParts = NumParts & (NumParts - 1) ?
201 1 << Log2_32(NumParts) : NumParts;
202 unsigned RoundBits = PartBits * RoundParts;
Owen Andersone50ed302009-08-10 22:56:29 +0000203 EVT RoundVT = RoundBits == ValueBits ?
Owen Anderson23b9b192009-08-12 00:36:31 +0000204 ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000205 SDValue Lo, Hi;
206
Owen Anderson23b9b192009-08-12 00:36:31 +0000207 EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2);
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000208
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000209 if (RoundParts > 2) {
Bill Wendling46ada192010-03-02 01:55:18 +0000210 Lo = getCopyFromParts(DAG, dl, Parts, RoundParts / 2,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000211 PartVT, HalfVT);
Bill Wendling46ada192010-03-02 01:55:18 +0000212 Hi = getCopyFromParts(DAG, dl, Parts + RoundParts / 2,
Bill Wendling3ea3c242009-12-22 02:10:19 +0000213 RoundParts / 2, PartVT, HalfVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000214 } else {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000215 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[0]);
216 Hi = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[1]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000217 }
Bill Wendling3ea3c242009-12-22 02:10:19 +0000218
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000219 if (TLI.isBigEndian())
220 std::swap(Lo, Hi);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000221
Dale Johannesen66978ee2009-01-31 02:22:37 +0000222 Val = DAG.getNode(ISD::BUILD_PAIR, dl, RoundVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000223
224 if (RoundParts < NumParts) {
225 // Assemble the trailing non-power-of-2 part.
226 unsigned OddParts = NumParts - RoundParts;
Owen Anderson23b9b192009-08-12 00:36:31 +0000227 EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits);
Bill Wendling46ada192010-03-02 01:55:18 +0000228 Hi = getCopyFromParts(DAG, dl,
Bill Wendling3ea3c242009-12-22 02:10:19 +0000229 Parts + RoundParts, OddParts, PartVT, OddVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000230
231 // Combine the round and odd parts.
232 Lo = Val;
233 if (TLI.isBigEndian())
234 std::swap(Lo, Hi);
Owen Anderson23b9b192009-08-12 00:36:31 +0000235 EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000236 Hi = DAG.getNode(ISD::ANY_EXTEND, dl, TotalVT, Hi);
237 Hi = DAG.getNode(ISD::SHL, dl, TotalVT, Hi,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000238 DAG.getConstant(Lo.getValueType().getSizeInBits(),
Duncan Sands92abc622009-01-31 15:50:11 +0000239 TLI.getPointerTy()));
Dale Johannesen66978ee2009-01-31 02:22:37 +0000240 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, TotalVT, Lo);
241 Val = DAG.getNode(ISD::OR, dl, TotalVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000242 }
Eli Friedman2ac8b322009-05-20 06:02:09 +0000243 } else if (ValueVT.isVector()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000244 // Handle a multi-element vector.
Owen Andersone50ed302009-08-10 22:56:29 +0000245 EVT IntermediateVT, RegisterVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000246 unsigned NumIntermediates;
247 unsigned NumRegs =
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000248 TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT,
Owen Anderson23b9b192009-08-12 00:36:31 +0000249 NumIntermediates, RegisterVT);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000250 assert(NumRegs == NumParts
251 && "Part count doesn't match vector breakdown!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000252 NumParts = NumRegs; // Silence a compiler warning.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000253 assert(RegisterVT == PartVT
254 && "Part type doesn't match vector breakdown!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000255 assert(RegisterVT == Parts[0].getValueType() &&
256 "Part type doesn't match part!");
257
258 // Assemble the parts into intermediate operands.
259 SmallVector<SDValue, 8> Ops(NumIntermediates);
260 if (NumIntermediates == NumParts) {
261 // If the register was not expanded, truncate or copy the value,
262 // as appropriate.
263 for (unsigned i = 0; i != NumParts; ++i)
Bill Wendling46ada192010-03-02 01:55:18 +0000264 Ops[i] = getCopyFromParts(DAG, dl, &Parts[i], 1,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000265 PartVT, IntermediateVT);
266 } else if (NumParts > 0) {
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000267 // If the intermediate type was expanded, build the intermediate
268 // operands from the parts.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000269 assert(NumParts % NumIntermediates == 0 &&
270 "Must expand into a divisible number of parts!");
271 unsigned Factor = NumParts / NumIntermediates;
272 for (unsigned i = 0; i != NumIntermediates; ++i)
Bill Wendling46ada192010-03-02 01:55:18 +0000273 Ops[i] = getCopyFromParts(DAG, dl, &Parts[i * Factor], Factor,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000274 PartVT, IntermediateVT);
275 }
276
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000277 // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the
278 // intermediate operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000279 Val = DAG.getNode(IntermediateVT.isVector() ?
Dale Johannesen66978ee2009-01-31 02:22:37 +0000280 ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000281 ValueVT, &Ops[0], NumIntermediates);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000282 } else if (PartVT.isFloatingPoint()) {
283 // FP split into multiple FP parts (for ppcf128)
Owen Anderson825b72b2009-08-11 20:47:22 +0000284 assert(ValueVT == EVT(MVT::ppcf128) && PartVT == EVT(MVT::f64) &&
Eli Friedman2ac8b322009-05-20 06:02:09 +0000285 "Unexpected split");
286 SDValue Lo, Hi;
Owen Anderson825b72b2009-08-11 20:47:22 +0000287 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[0]);
288 Hi = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[1]);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000289 if (TLI.isBigEndian())
290 std::swap(Lo, Hi);
291 Val = DAG.getNode(ISD::BUILD_PAIR, dl, ValueVT, Lo, Hi);
292 } else {
293 // FP split into integer parts (soft fp)
294 assert(ValueVT.isFloatingPoint() && PartVT.isInteger() &&
295 !PartVT.isVector() && "Unexpected split");
Owen Anderson23b9b192009-08-12 00:36:31 +0000296 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
Bill Wendling46ada192010-03-02 01:55:18 +0000297 Val = getCopyFromParts(DAG, dl, Parts, NumParts, PartVT, IntVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000298 }
299 }
300
301 // There is now one part, held in Val. Correct it to match ValueVT.
302 PartVT = Val.getValueType();
303
304 if (PartVT == ValueVT)
305 return Val;
306
307 if (PartVT.isVector()) {
308 assert(ValueVT.isVector() && "Unknown vector conversion!");
Bill Wendling4533cac2010-01-28 21:51:40 +0000309 return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000310 }
311
312 if (ValueVT.isVector()) {
313 assert(ValueVT.getVectorElementType() == PartVT &&
314 ValueVT.getVectorNumElements() == 1 &&
315 "Only trivial scalar-to-vector conversions should get here!");
Bill Wendling4533cac2010-01-28 21:51:40 +0000316 return DAG.getNode(ISD::BUILD_VECTOR, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000317 }
318
319 if (PartVT.isInteger() &&
320 ValueVT.isInteger()) {
321 if (ValueVT.bitsLT(PartVT)) {
322 // For a truncate, see if we have any information to
323 // indicate whether the truncated bits will always be
324 // zero or sign-extension.
325 if (AssertOp != ISD::DELETED_NODE)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000326 Val = DAG.getNode(AssertOp, dl, PartVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000327 DAG.getValueType(ValueVT));
Bill Wendling4533cac2010-01-28 21:51:40 +0000328 return DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000329 } else {
Bill Wendling4533cac2010-01-28 21:51:40 +0000330 return DAG.getNode(ISD::ANY_EXTEND, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000331 }
332 }
333
334 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
Bill Wendling3ea3c242009-12-22 02:10:19 +0000335 if (ValueVT.bitsLT(Val.getValueType())) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000336 // FP_ROUND's are always exact here.
Bill Wendling4533cac2010-01-28 21:51:40 +0000337 return DAG.getNode(ISD::FP_ROUND, dl, ValueVT, Val,
338 DAG.getIntPtrConstant(1));
Bill Wendling3ea3c242009-12-22 02:10:19 +0000339 }
340
Bill Wendling4533cac2010-01-28 21:51:40 +0000341 return DAG.getNode(ISD::FP_EXTEND, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000342 }
343
Bill Wendling4533cac2010-01-28 21:51:40 +0000344 if (PartVT.getSizeInBits() == ValueVT.getSizeInBits())
345 return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000346
Torok Edwinc23197a2009-07-14 16:55:14 +0000347 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000348 return SDValue();
349}
350
351/// getCopyToParts - Create a series of nodes that contain the specified value
352/// split into legal parts. If the parts contain more bits than Val, then, for
353/// integers, ExtendKind can be used to specify how to generate the extra bits.
Bill Wendling46ada192010-03-02 01:55:18 +0000354static void getCopyToParts(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling3ea3c242009-12-22 02:10:19 +0000355 SDValue Val, SDValue *Parts, unsigned NumParts,
356 EVT PartVT,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000357 ISD::NodeType ExtendKind = ISD::ANY_EXTEND) {
Dan Gohmane9530ec2009-01-15 16:58:17 +0000358 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +0000359 EVT PtrVT = TLI.getPointerTy();
360 EVT ValueVT = Val.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000361 unsigned PartBits = PartVT.getSizeInBits();
Dale Johannesen8a36f502009-02-25 22:39:13 +0000362 unsigned OrigNumParts = NumParts;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000363 assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!");
364
365 if (!NumParts)
366 return;
367
368 if (!ValueVT.isVector()) {
369 if (PartVT == ValueVT) {
370 assert(NumParts == 1 && "No-op copy with multiple parts!");
371 Parts[0] = Val;
372 return;
373 }
374
375 if (NumParts * PartBits > ValueVT.getSizeInBits()) {
376 // If the parts cover more bits than the value has, promote the value.
377 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
378 assert(NumParts == 1 && "Do not know what to promote to!");
Dale Johannesen66978ee2009-01-31 02:22:37 +0000379 Val = DAG.getNode(ISD::FP_EXTEND, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000380 } else if (PartVT.isInteger() && ValueVT.isInteger()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000381 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000382 Val = DAG.getNode(ExtendKind, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000383 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000384 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000385 }
386 } else if (PartBits == ValueVT.getSizeInBits()) {
387 // Different types of the same size.
388 assert(NumParts == 1 && PartVT != ValueVT);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000389 Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000390 } else if (NumParts * PartBits < ValueVT.getSizeInBits()) {
391 // If the parts cover less bits than value has, truncate the value.
392 if (PartVT.isInteger() && ValueVT.isInteger()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000393 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000394 Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000395 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000396 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000397 }
398 }
399
400 // The value may have changed - recompute ValueVT.
401 ValueVT = Val.getValueType();
402 assert(NumParts * PartBits == ValueVT.getSizeInBits() &&
403 "Failed to tile the value with PartVT!");
404
405 if (NumParts == 1) {
406 assert(PartVT == ValueVT && "Type conversion failed!");
407 Parts[0] = Val;
408 return;
409 }
410
411 // Expand the value into multiple parts.
412 if (NumParts & (NumParts - 1)) {
413 // The number of parts is not a power of 2. Split off and copy the tail.
414 assert(PartVT.isInteger() && ValueVT.isInteger() &&
415 "Do not know what to expand to!");
416 unsigned RoundParts = 1 << Log2_32(NumParts);
417 unsigned RoundBits = RoundParts * PartBits;
418 unsigned OddParts = NumParts - RoundParts;
Dale Johannesen66978ee2009-01-31 02:22:37 +0000419 SDValue OddVal = DAG.getNode(ISD::SRL, dl, ValueVT, Val,
Duncan Sands0b3aa262009-01-28 14:42:54 +0000420 DAG.getConstant(RoundBits,
Duncan Sands92abc622009-01-31 15:50:11 +0000421 TLI.getPointerTy()));
Bill Wendling46ada192010-03-02 01:55:18 +0000422 getCopyToParts(DAG, dl, OddVal, Parts + RoundParts,
Bill Wendling3ea3c242009-12-22 02:10:19 +0000423 OddParts, PartVT);
424
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000425 if (TLI.isBigEndian())
426 // The odd parts were reversed by getCopyToParts - unreverse them.
427 std::reverse(Parts + RoundParts, Parts + NumParts);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000428
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000429 NumParts = RoundParts;
Owen Anderson23b9b192009-08-12 00:36:31 +0000430 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000431 Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000432 }
433
434 // The number of parts is a power of 2. Repeatedly bisect the value using
435 // EXTRACT_ELEMENT.
Scott Michelfdc40a02009-02-17 22:15:04 +0000436 Parts[0] = DAG.getNode(ISD::BIT_CONVERT, dl,
Chris Lattnerf031e8a2010-01-01 03:32:16 +0000437 EVT::getIntegerVT(*DAG.getContext(),
438 ValueVT.getSizeInBits()),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000439 Val);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000440
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000441 for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) {
442 for (unsigned i = 0; i < NumParts; i += StepSize) {
443 unsigned ThisBits = StepSize * PartBits / 2;
Owen Anderson23b9b192009-08-12 00:36:31 +0000444 EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000445 SDValue &Part0 = Parts[i];
446 SDValue &Part1 = Parts[i+StepSize/2];
447
Scott Michelfdc40a02009-02-17 22:15:04 +0000448 Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000449 ThisVT, Part0,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000450 DAG.getConstant(1, PtrVT));
Scott Michelfdc40a02009-02-17 22:15:04 +0000451 Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000452 ThisVT, Part0,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000453 DAG.getConstant(0, PtrVT));
454
455 if (ThisBits == PartBits && ThisVT != PartVT) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000456 Part0 = DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000457 PartVT, Part0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000458 Part1 = DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000459 PartVT, Part1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000460 }
461 }
462 }
463
464 if (TLI.isBigEndian())
Dale Johannesen8a36f502009-02-25 22:39:13 +0000465 std::reverse(Parts, Parts + OrigNumParts);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000466
467 return;
468 }
469
470 // Vector ValueVT.
471 if (NumParts == 1) {
472 if (PartVT != ValueVT) {
Bob Wilson5afffae2009-12-18 01:03:29 +0000473 if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000474 Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000475 } else {
476 assert(ValueVT.getVectorElementType() == PartVT &&
477 ValueVT.getVectorNumElements() == 1 &&
478 "Only trivial vector-to-scalar conversions should get here!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000479 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000480 PartVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000481 DAG.getConstant(0, PtrVT));
482 }
483 }
484
485 Parts[0] = Val;
486 return;
487 }
488
489 // Handle a multi-element vector.
Owen Andersone50ed302009-08-10 22:56:29 +0000490 EVT IntermediateVT, RegisterVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000491 unsigned NumIntermediates;
Owen Anderson23b9b192009-08-12 00:36:31 +0000492 unsigned NumRegs = TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT,
493 IntermediateVT, NumIntermediates, RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000494 unsigned NumElements = ValueVT.getVectorNumElements();
495
496 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
497 NumParts = NumRegs; // Silence a compiler warning.
498 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
499
500 // Split the vector into intermediate operands.
501 SmallVector<SDValue, 8> Ops(NumIntermediates);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000502 for (unsigned i = 0; i != NumIntermediates; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000503 if (IntermediateVT.isVector())
Scott Michelfdc40a02009-02-17 22:15:04 +0000504 Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000505 IntermediateVT, Val,
506 DAG.getConstant(i * (NumElements / NumIntermediates),
507 PtrVT));
508 else
Scott Michelfdc40a02009-02-17 22:15:04 +0000509 Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000510 IntermediateVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000511 DAG.getConstant(i, PtrVT));
Bill Wendling3ea3c242009-12-22 02:10:19 +0000512 }
513
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000514 // Split the intermediate operands into legal parts.
515 if (NumParts == NumIntermediates) {
516 // If the register was not expanded, promote or copy the value,
517 // as appropriate.
518 for (unsigned i = 0; i != NumParts; ++i)
Bill Wendling46ada192010-03-02 01:55:18 +0000519 getCopyToParts(DAG, dl, Ops[i], &Parts[i], 1, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000520 } else if (NumParts > 0) {
521 // If the intermediate type was expanded, split each the value into
522 // legal parts.
523 assert(NumParts % NumIntermediates == 0 &&
524 "Must expand into a divisible number of parts!");
525 unsigned Factor = NumParts / NumIntermediates;
526 for (unsigned i = 0; i != NumIntermediates; ++i)
Bill Wendling46ada192010-03-02 01:55:18 +0000527 getCopyToParts(DAG, dl, Ops[i], &Parts[i*Factor], Factor, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000528 }
529}
530
531
Dan Gohman2048b852009-11-23 18:04:58 +0000532void SelectionDAGBuilder::init(GCFunctionInfo *gfi, AliasAnalysis &aa) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000533 AA = &aa;
534 GFI = gfi;
535 TD = DAG.getTarget().getTargetData();
536}
537
Dan Gohmanb02b62a2010-04-14 18:24:06 +0000538/// clear - Clear out the current SelectionDAG and the associated
Dan Gohman2048b852009-11-23 18:04:58 +0000539/// state and prepare this SelectionDAGBuilder object to be used
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000540/// for a new block. This doesn't clear out information about
541/// additional blocks that are needed to complete switch lowering
542/// or PHI node updating; that information is cleared out as it is
543/// consumed.
Dan Gohman2048b852009-11-23 18:04:58 +0000544void SelectionDAGBuilder::clear() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000545 NodeMap.clear();
546 PendingLoads.clear();
547 PendingExports.clear();
548 DAG.clear();
Chris Lattnera4f2bb02010-04-02 20:17:23 +0000549 CurDebugLoc = DebugLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000550 HasTailCall = false;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000551}
552
553/// getRoot - Return the current virtual root of the Selection DAG,
554/// flushing any PendingLoad items. This must be done before emitting
555/// a store or any other node that may need to be ordered after any
556/// prior load instructions.
557///
Dan Gohman2048b852009-11-23 18:04:58 +0000558SDValue SelectionDAGBuilder::getRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000559 if (PendingLoads.empty())
560 return DAG.getRoot();
561
562 if (PendingLoads.size() == 1) {
563 SDValue Root = PendingLoads[0];
564 DAG.setRoot(Root);
565 PendingLoads.clear();
566 return Root;
567 }
568
569 // Otherwise, we have to make a token factor node.
Owen Anderson825b72b2009-08-11 20:47:22 +0000570 SDValue Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000571 &PendingLoads[0], PendingLoads.size());
572 PendingLoads.clear();
573 DAG.setRoot(Root);
574 return Root;
575}
576
577/// getControlRoot - Similar to getRoot, but instead of flushing all the
578/// PendingLoad items, flush all the PendingExports items. It is necessary
579/// to do this before emitting a terminator instruction.
580///
Dan Gohman2048b852009-11-23 18:04:58 +0000581SDValue SelectionDAGBuilder::getControlRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000582 SDValue Root = DAG.getRoot();
583
584 if (PendingExports.empty())
585 return Root;
586
587 // Turn all of the CopyToReg chains into one factored node.
588 if (Root.getOpcode() != ISD::EntryToken) {
589 unsigned i = 0, e = PendingExports.size();
590 for (; i != e; ++i) {
591 assert(PendingExports[i].getNode()->getNumOperands() > 1);
592 if (PendingExports[i].getNode()->getOperand(0) == Root)
593 break; // Don't add the root if we already indirectly depend on it.
594 }
595
596 if (i == e)
597 PendingExports.push_back(Root);
598 }
599
Owen Anderson825b72b2009-08-11 20:47:22 +0000600 Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000601 &PendingExports[0],
602 PendingExports.size());
603 PendingExports.clear();
604 DAG.setRoot(Root);
605 return Root;
606}
607
Bill Wendling4533cac2010-01-28 21:51:40 +0000608void SelectionDAGBuilder::AssignOrderingToNode(const SDNode *Node) {
609 if (DAG.GetOrdering(Node) != 0) return; // Already has ordering.
610 DAG.AssignOrdering(Node, SDNodeOrder);
611
612 for (unsigned I = 0, E = Node->getNumOperands(); I != E; ++I)
613 AssignOrderingToNode(Node->getOperand(I).getNode());
614}
615
Dan Gohman46510a72010-04-15 01:51:59 +0000616void SelectionDAGBuilder::visit(const Instruction &I) {
Dan Gohmanc105a2b2010-04-22 20:55:53 +0000617 // Set up outgoing PHI node register values before emitting the terminator.
618 if (isa<TerminatorInst>(&I))
619 HandlePHINodesInSuccessorBlocks(I.getParent());
620
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000621 CurDebugLoc = I.getDebugLoc();
622
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000623 visit(I.getOpcode(), I);
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000624
Dan Gohman92884f72010-04-20 15:03:56 +0000625 if (!isa<TerminatorInst>(&I) && !HasTailCall)
626 CopyToExportRegsIfNeeded(&I);
627
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000628 CurDebugLoc = DebugLoc();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000629}
630
Dan Gohmanba5be5c2010-04-20 15:00:41 +0000631void SelectionDAGBuilder::visitPHI(const PHINode &) {
632 llvm_unreachable("SelectionDAGBuilder shouldn't visit PHI nodes!");
633}
634
Dan Gohman46510a72010-04-15 01:51:59 +0000635void SelectionDAGBuilder::visit(unsigned Opcode, const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000636 // Note: this doesn't use InstVisitor, because it has to work with
637 // ConstantExpr's in addition to instructions.
638 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000639 default: llvm_unreachable("Unknown instruction type encountered!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000640 // Build the switch statement using the Instruction.def file.
641#define HANDLE_INST(NUM, OPCODE, CLASS) \
Bill Wendling4533cac2010-01-28 21:51:40 +0000642 case Instruction::OPCODE: visit##OPCODE((CLASS&)I); break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000643#include "llvm/Instruction.def"
644 }
Bill Wendling4533cac2010-01-28 21:51:40 +0000645
646 // Assign the ordering to the freshly created DAG nodes.
647 if (NodeMap.count(&I)) {
648 ++SDNodeOrder;
649 AssignOrderingToNode(getValue(&I).getNode());
650 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000651}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000652
Dan Gohman2048b852009-11-23 18:04:58 +0000653SDValue SelectionDAGBuilder::getValue(const Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000654 SDValue &N = NodeMap[V];
655 if (N.getNode()) return N;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000656
Dan Gohman383b5f62010-04-17 15:32:28 +0000657 if (const Constant *C = dyn_cast<Constant>(V)) {
Owen Andersone50ed302009-08-10 22:56:29 +0000658 EVT VT = TLI.getValueType(V->getType(), true);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000659
Dan Gohman383b5f62010-04-17 15:32:28 +0000660 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000661 return N = DAG.getConstant(*CI, VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000662
Dan Gohman383b5f62010-04-17 15:32:28 +0000663 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000664 return N = DAG.getGlobalAddress(GV, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000665
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000666 if (isa<ConstantPointerNull>(C))
667 return N = DAG.getConstant(0, TLI.getPointerTy());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000668
Dan Gohman383b5f62010-04-17 15:32:28 +0000669 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000670 return N = DAG.getConstantFP(*CFP, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000671
Nate Begeman9008ca62009-04-27 18:41:29 +0000672 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
Dale Johannesene8d72302009-02-06 23:05:02 +0000673 return N = DAG.getUNDEF(VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000674
Dan Gohman383b5f62010-04-17 15:32:28 +0000675 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000676 visit(CE->getOpcode(), *CE);
677 SDValue N1 = NodeMap[V];
Dan Gohmanac7d05c2010-04-16 16:55:18 +0000678 assert(N1.getNode() && "visit didn't populate the NodeMap!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000679 return N1;
680 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000681
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000682 if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
683 SmallVector<SDValue, 4> Constants;
684 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
685 OI != OE; ++OI) {
686 SDNode *Val = getValue(*OI).getNode();
Dan Gohmaned48caf2009-09-08 01:44:02 +0000687 // If the operand is an empty aggregate, there are no values.
688 if (!Val) continue;
689 // Add each leaf value from the operand to the Constants list
690 // to form a flattened list of all the values.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000691 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
692 Constants.push_back(SDValue(Val, i));
693 }
Bill Wendling87710f02009-12-21 23:47:40 +0000694
Bill Wendling4533cac2010-01-28 21:51:40 +0000695 return DAG.getMergeValues(&Constants[0], Constants.size(),
696 getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000697 }
698
Duncan Sands1df98592010-02-16 11:11:14 +0000699 if (C->getType()->isStructTy() || C->getType()->isArrayTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000700 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
701 "Unknown struct or array constant!");
702
Owen Andersone50ed302009-08-10 22:56:29 +0000703 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000704 ComputeValueVTs(TLI, C->getType(), ValueVTs);
705 unsigned NumElts = ValueVTs.size();
706 if (NumElts == 0)
707 return SDValue(); // empty struct
708 SmallVector<SDValue, 4> Constants(NumElts);
709 for (unsigned i = 0; i != NumElts; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +0000710 EVT EltVT = ValueVTs[i];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000711 if (isa<UndefValue>(C))
Dale Johannesene8d72302009-02-06 23:05:02 +0000712 Constants[i] = DAG.getUNDEF(EltVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000713 else if (EltVT.isFloatingPoint())
714 Constants[i] = DAG.getConstantFP(0, EltVT);
715 else
716 Constants[i] = DAG.getConstant(0, EltVT);
717 }
Bill Wendling87710f02009-12-21 23:47:40 +0000718
Bill Wendling4533cac2010-01-28 21:51:40 +0000719 return DAG.getMergeValues(&Constants[0], NumElts,
720 getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000721 }
722
Dan Gohman383b5f62010-04-17 15:32:28 +0000723 if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
Dan Gohman29cbade2009-11-20 23:18:13 +0000724 return DAG.getBlockAddress(BA, VT);
Dan Gohman8c2b5252009-10-30 01:27:03 +0000725
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000726 const VectorType *VecTy = cast<VectorType>(V->getType());
727 unsigned NumElements = VecTy->getNumElements();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000728
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000729 // Now that we know the number and type of the elements, get that number of
730 // elements into the Ops array based on what kind of constant it is.
731 SmallVector<SDValue, 16> Ops;
Dan Gohman383b5f62010-04-17 15:32:28 +0000732 if (const ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000733 for (unsigned i = 0; i != NumElements; ++i)
734 Ops.push_back(getValue(CP->getOperand(i)));
735 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +0000736 assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!");
Owen Andersone50ed302009-08-10 22:56:29 +0000737 EVT EltVT = TLI.getValueType(VecTy->getElementType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000738
739 SDValue Op;
Nate Begeman9008ca62009-04-27 18:41:29 +0000740 if (EltVT.isFloatingPoint())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000741 Op = DAG.getConstantFP(0, EltVT);
742 else
743 Op = DAG.getConstant(0, EltVT);
744 Ops.assign(NumElements, Op);
745 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000746
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000747 // Create a BUILD_VECTOR node.
Bill Wendling4533cac2010-01-28 21:51:40 +0000748 return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
749 VT, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000750 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000751
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000752 // If this is a static alloca, generate it as the frameindex instead of
753 // computation.
754 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
755 DenseMap<const AllocaInst*, int>::iterator SI =
756 FuncInfo.StaticAllocaMap.find(AI);
757 if (SI != FuncInfo.StaticAllocaMap.end())
758 return DAG.getFrameIndex(SI->second, TLI.getPointerTy());
759 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000760
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000761 unsigned InReg = FuncInfo.ValueMap[V];
762 assert(InReg && "Value not in map!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000763
Owen Anderson23b9b192009-08-12 00:36:31 +0000764 RegsForValue RFV(*DAG.getContext(), TLI, InReg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000765 SDValue Chain = DAG.getEntryNode();
Bill Wendling46ada192010-03-02 01:55:18 +0000766 return RFV.getCopyFromRegs(DAG, getCurDebugLoc(), Chain, NULL);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000767}
768
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000769/// Get the EVTs and ArgFlags collections that represent the legalized return
770/// type of the given function. This does not require a DAG or a return value,
771/// and is suitable for use before any DAGs for the function are constructed.
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000772static void getReturnInfo(const Type* ReturnType,
773 Attributes attr, SmallVectorImpl<EVT> &OutVTs,
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000774 SmallVectorImpl<ISD::ArgFlagsTy> &OutFlags,
Dan Gohmand858e902010-04-17 15:26:15 +0000775 const TargetLowering &TLI,
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000776 SmallVectorImpl<uint64_t> *Offsets = 0) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000777 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000778 ComputeValueVTs(TLI, ReturnType, ValueVTs);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000779 unsigned NumValues = ValueVTs.size();
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000780 if (NumValues == 0) return;
781 unsigned Offset = 0;
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000782
783 for (unsigned j = 0, f = NumValues; j != f; ++j) {
784 EVT VT = ValueVTs[j];
785 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000786
787 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000788 ExtendKind = ISD::SIGN_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000789 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000790 ExtendKind = ISD::ZERO_EXTEND;
791
792 // FIXME: C calling convention requires the return type to be promoted to
793 // at least 32-bit. But this is not necessary for non-C calling
794 // conventions. The frontend should mark functions whose return values
795 // require promoting with signext or zeroext attributes.
796 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000797 EVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000798 if (VT.bitsLT(MinVT))
799 VT = MinVT;
800 }
801
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000802 unsigned NumParts = TLI.getNumRegisters(ReturnType->getContext(), VT);
803 EVT PartVT = TLI.getRegisterType(ReturnType->getContext(), VT);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000804 unsigned PartSize = TLI.getTargetData()->getTypeAllocSize(
805 PartVT.getTypeForEVT(ReturnType->getContext()));
806
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000807 // 'inreg' on function refers to return value
808 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000809 if (attr & Attribute::InReg)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000810 Flags.setInReg();
811
812 // Propagate extension type if any
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000813 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000814 Flags.setSExt();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000815 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000816 Flags.setZExt();
817
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000818 for (unsigned i = 0; i < NumParts; ++i) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000819 OutVTs.push_back(PartVT);
820 OutFlags.push_back(Flags);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000821 if (Offsets)
822 {
823 Offsets->push_back(Offset);
824 Offset += PartSize;
825 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000826 }
827 }
828}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000829
Dan Gohman46510a72010-04-15 01:51:59 +0000830void SelectionDAGBuilder::visitRet(const ReturnInst &I) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000831 SDValue Chain = getControlRoot();
832 SmallVector<ISD::OutputArg, 8> Outs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000833 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000834
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000835 if (!FLI.CanLowerReturn) {
836 unsigned DemoteReg = FLI.DemoteRegister;
837 const Function *F = I.getParent()->getParent();
838
839 // Emit a store of the return value through the virtual register.
840 // Leave Outs empty so that LowerReturn won't try to load return
841 // registers the usual way.
842 SmallVector<EVT, 1> PtrValueVTs;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000843 ComputeValueVTs(TLI, PointerType::getUnqual(F->getReturnType()),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000844 PtrValueVTs);
845
846 SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]);
847 SDValue RetOp = getValue(I.getOperand(0));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000848
Owen Andersone50ed302009-08-10 22:56:29 +0000849 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000850 SmallVector<uint64_t, 4> Offsets;
851 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets);
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000852 unsigned NumValues = ValueVTs.size();
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000853
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000854 SmallVector<SDValue, 4> Chains(NumValues);
855 EVT PtrVT = PtrValueVTs[0];
Bill Wendling87710f02009-12-21 23:47:40 +0000856 for (unsigned i = 0; i != NumValues; ++i) {
857 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, RetPtr,
858 DAG.getConstant(Offsets[i], PtrVT));
859 Chains[i] =
860 DAG.getStore(Chain, getCurDebugLoc(),
861 SDValue(RetOp.getNode(), RetOp.getResNo() + i),
David Greene1e559442010-02-15 17:00:31 +0000862 Add, NULL, Offsets[i], false, false, 0);
Bill Wendling87710f02009-12-21 23:47:40 +0000863 }
864
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000865 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
866 MVT::Other, &Chains[0], NumValues);
Chris Lattner25d58372010-02-28 18:53:13 +0000867 } else if (I.getNumOperands() != 0) {
868 SmallVector<EVT, 4> ValueVTs;
869 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs);
870 unsigned NumValues = ValueVTs.size();
871 if (NumValues) {
872 SDValue RetOp = getValue(I.getOperand(0));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000873 for (unsigned j = 0, f = NumValues; j != f; ++j) {
874 EVT VT = ValueVTs[j];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000875
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000876 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000877
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000878 const Function *F = I.getParent()->getParent();
879 if (F->paramHasAttr(0, Attribute::SExt))
880 ExtendKind = ISD::SIGN_EXTEND;
881 else if (F->paramHasAttr(0, Attribute::ZExt))
882 ExtendKind = ISD::ZERO_EXTEND;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000883
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000884 // FIXME: C calling convention requires the return type to be promoted
885 // to at least 32-bit. But this is not necessary for non-C calling
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000886 // conventions. The frontend should mark functions whose return values
887 // require promoting with signext or zeroext attributes.
888 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
889 EVT MinVT = TLI.getRegisterType(*DAG.getContext(), MVT::i32);
890 if (VT.bitsLT(MinVT))
891 VT = MinVT;
892 }
893
894 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), VT);
895 EVT PartVT = TLI.getRegisterType(*DAG.getContext(), VT);
896 SmallVector<SDValue, 4> Parts(NumParts);
Bill Wendling46ada192010-03-02 01:55:18 +0000897 getCopyToParts(DAG, getCurDebugLoc(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000898 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
899 &Parts[0], NumParts, PartVT, ExtendKind);
900
901 // 'inreg' on function refers to return value
902 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
903 if (F->paramHasAttr(0, Attribute::InReg))
904 Flags.setInReg();
905
906 // Propagate extension type if any
907 if (F->paramHasAttr(0, Attribute::SExt))
908 Flags.setSExt();
909 else if (F->paramHasAttr(0, Attribute::ZExt))
910 Flags.setZExt();
911
912 for (unsigned i = 0; i < NumParts; ++i)
913 Outs.push_back(ISD::OutputArg(Flags, Parts[i], /*isfixed=*/true));
Evan Cheng3927f432009-03-25 20:20:11 +0000914 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000915 }
916 }
Dan Gohman98ca4f22009-08-05 01:29:28 +0000917
918 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000919 CallingConv::ID CallConv =
920 DAG.getMachineFunction().getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000921 Chain = TLI.LowerReturn(Chain, CallConv, isVarArg,
922 Outs, getCurDebugLoc(), DAG);
Dan Gohman5e866062009-08-06 15:37:27 +0000923
924 // Verify that the target's LowerReturn behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +0000925 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +0000926 "LowerReturn didn't return a valid chain!");
927
928 // Update the DAG with the new chain value resulting from return lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000929 DAG.setRoot(Chain);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000930}
931
Dan Gohmanad62f532009-04-23 23:13:24 +0000932/// CopyToExportRegsIfNeeded - If the given value has virtual registers
933/// created for it, emit nodes to copy the value into the virtual
934/// registers.
Dan Gohman46510a72010-04-15 01:51:59 +0000935void SelectionDAGBuilder::CopyToExportRegsIfNeeded(const Value *V) {
Dan Gohman33b7a292010-04-16 17:15:02 +0000936 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
937 if (VMI != FuncInfo.ValueMap.end()) {
938 assert(!V->use_empty() && "Unused value assigned virtual registers!");
939 CopyValueToVirtualRegister(V, VMI->second);
Dan Gohmanad62f532009-04-23 23:13:24 +0000940 }
941}
942
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000943/// ExportFromCurrentBlock - If this condition isn't known to be exported from
944/// the current basic block, add it to ValueMap now so that we'll get a
945/// CopyTo/FromReg.
Dan Gohman46510a72010-04-15 01:51:59 +0000946void SelectionDAGBuilder::ExportFromCurrentBlock(const Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000947 // No need to export constants.
948 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000949
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000950 // Already exported?
951 if (FuncInfo.isExportedInst(V)) return;
952
953 unsigned Reg = FuncInfo.InitializeRegForValue(V);
954 CopyValueToVirtualRegister(V, Reg);
955}
956
Dan Gohman46510a72010-04-15 01:51:59 +0000957bool SelectionDAGBuilder::isExportableFromCurrentBlock(const Value *V,
Dan Gohman2048b852009-11-23 18:04:58 +0000958 const BasicBlock *FromBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000959 // The operands of the setcc have to be in this block. We don't know
960 // how to export them from some other block.
Dan Gohman46510a72010-04-15 01:51:59 +0000961 if (const Instruction *VI = dyn_cast<Instruction>(V)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000962 // Can export from current BB.
963 if (VI->getParent() == FromBB)
964 return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000965
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000966 // Is already exported, noop.
967 return FuncInfo.isExportedInst(V);
968 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000969
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000970 // If this is an argument, we can export it if the BB is the entry block or
971 // if it is already exported.
972 if (isa<Argument>(V)) {
973 if (FromBB == &FromBB->getParent()->getEntryBlock())
974 return true;
975
976 // Otherwise, can only export this if it is already exported.
977 return FuncInfo.isExportedInst(V);
978 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000979
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000980 // Otherwise, constants can always be exported.
981 return true;
982}
983
984static bool InBlock(const Value *V, const BasicBlock *BB) {
985 if (const Instruction *I = dyn_cast<Instruction>(V))
986 return I->getParent() == BB;
987 return true;
988}
989
Dan Gohmanc2277342008-10-17 21:16:08 +0000990/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
991/// This function emits a branch and is used at the leaves of an OR or an
992/// AND operator tree.
993///
994void
Dan Gohman46510a72010-04-15 01:51:59 +0000995SelectionDAGBuilder::EmitBranchForMergedCondition(const Value *Cond,
Dan Gohman2048b852009-11-23 18:04:58 +0000996 MachineBasicBlock *TBB,
997 MachineBasicBlock *FBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +0000998 MachineBasicBlock *CurBB,
999 MachineBasicBlock *SwitchBB) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001000 const BasicBlock *BB = CurBB->getBasicBlock();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001001
Dan Gohmanc2277342008-10-17 21:16:08 +00001002 // If the leaf of the tree is a comparison, merge the condition into
1003 // the caseblock.
Dan Gohman46510a72010-04-15 01:51:59 +00001004 if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001005 // The operands of the cmp have to be in this block. We don't know
1006 // how to export them from some other block. If this is the first block
1007 // of the sequence, no exporting is needed.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001008 if (CurBB == SwitchBB ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001009 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
1010 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001011 ISD::CondCode Condition;
Dan Gohman46510a72010-04-15 01:51:59 +00001012 if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001013 Condition = getICmpCondCode(IC->getPredicate());
Dan Gohman46510a72010-04-15 01:51:59 +00001014 } else if (const FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001015 Condition = getFCmpCondCode(FC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001016 } else {
1017 Condition = ISD::SETEQ; // silence warning.
Torok Edwinc23197a2009-07-14 16:55:14 +00001018 llvm_unreachable("Unknown compare instruction");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001019 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001020
1021 CaseBlock CB(Condition, BOp->getOperand(0),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001022 BOp->getOperand(1), NULL, TBB, FBB, CurBB);
1023 SwitchCases.push_back(CB);
1024 return;
1025 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001026 }
1027
1028 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001029 CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanc2277342008-10-17 21:16:08 +00001030 NULL, TBB, FBB, CurBB);
1031 SwitchCases.push_back(CB);
1032}
1033
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001034/// FindMergedConditions - If Cond is an expression like
Dan Gohman46510a72010-04-15 01:51:59 +00001035void SelectionDAGBuilder::FindMergedConditions(const Value *Cond,
Dan Gohman2048b852009-11-23 18:04:58 +00001036 MachineBasicBlock *TBB,
1037 MachineBasicBlock *FBB,
1038 MachineBasicBlock *CurBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001039 MachineBasicBlock *SwitchBB,
Dan Gohman2048b852009-11-23 18:04:58 +00001040 unsigned Opc) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001041 // If this node is not part of the or/and tree, emit it as a branch.
Dan Gohman46510a72010-04-15 01:51:59 +00001042 const Instruction *BOp = dyn_cast<Instruction>(Cond);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001043 if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001044 (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
1045 BOp->getParent() != CurBB->getBasicBlock() ||
1046 !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1047 !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001048 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001049 return;
1050 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001051
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001052 // Create TmpBB after CurBB.
1053 MachineFunction::iterator BBI = CurBB;
1054 MachineFunction &MF = DAG.getMachineFunction();
1055 MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
1056 CurBB->getParent()->insert(++BBI, TmpBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001057
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001058 if (Opc == Instruction::Or) {
1059 // Codegen X | Y as:
1060 // jmp_if_X TBB
1061 // jmp TmpBB
1062 // TmpBB:
1063 // jmp_if_Y TBB
1064 // jmp FBB
1065 //
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001066
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001067 // Emit the LHS condition.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001068 FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, SwitchBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001069
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001070 // Emit the RHS condition into TmpBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001071 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001072 } else {
1073 assert(Opc == Instruction::And && "Unknown merge op!");
1074 // Codegen X & Y as:
1075 // jmp_if_X TmpBB
1076 // jmp FBB
1077 // TmpBB:
1078 // jmp_if_Y TBB
1079 // jmp FBB
1080 //
1081 // This requires creation of TmpBB after CurBB.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001082
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001083 // Emit the LHS condition.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001084 FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, SwitchBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001085
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001086 // Emit the RHS condition into TmpBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001087 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001088 }
1089}
1090
1091/// If the set of cases should be emitted as a series of branches, return true.
1092/// If we should emit this as a bunch of and/or'd together conditions, return
1093/// false.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001094bool
Dan Gohman2048b852009-11-23 18:04:58 +00001095SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001096 if (Cases.size() != 2) return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001097
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001098 // If this is two comparisons of the same values or'd or and'd together, they
1099 // will get folded into a single comparison, so don't emit two blocks.
1100 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1101 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1102 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1103 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1104 return false;
1105 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001106
Chris Lattner133ce872010-01-02 00:00:03 +00001107 // Handle: (X != null) | (Y != null) --> (X|Y) != 0
1108 // Handle: (X == null) & (Y == null) --> (X|Y) == 0
1109 if (Cases[0].CmpRHS == Cases[1].CmpRHS &&
1110 Cases[0].CC == Cases[1].CC &&
1111 isa<Constant>(Cases[0].CmpRHS) &&
1112 cast<Constant>(Cases[0].CmpRHS)->isNullValue()) {
1113 if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB)
1114 return false;
1115 if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB)
1116 return false;
1117 }
1118
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001119 return true;
1120}
1121
Dan Gohman46510a72010-04-15 01:51:59 +00001122void SelectionDAGBuilder::visitBr(const BranchInst &I) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001123 MachineBasicBlock *BrMBB = FuncInfo.MBBMap[I.getParent()];
1124
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001125 // Update machine-CFG edges.
1126 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1127
1128 // Figure out which block is immediately after the current one.
1129 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001130 MachineFunction::iterator BBI = BrMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001131 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001132 NextBlock = BBI;
1133
1134 if (I.isUnconditional()) {
1135 // Update machine-CFG edges.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001136 BrMBB->addSuccessor(Succ0MBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001137
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001138 // If this is not a fall-through branch, emit the branch.
Bill Wendling4533cac2010-01-28 21:51:40 +00001139 if (Succ0MBB != NextBlock)
1140 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001141 MVT::Other, getControlRoot(),
Bill Wendling4533cac2010-01-28 21:51:40 +00001142 DAG.getBasicBlock(Succ0MBB)));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001143
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001144 return;
1145 }
1146
1147 // If this condition is one of the special cases we handle, do special stuff
1148 // now.
Dan Gohman46510a72010-04-15 01:51:59 +00001149 const Value *CondVal = I.getCondition();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001150 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1151
1152 // If this is a series of conditions that are or'd or and'd together, emit
1153 // this as a sequence of branches instead of setcc's with and/or operations.
1154 // For example, instead of something like:
1155 // cmp A, B
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001156 // C = seteq
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001157 // cmp D, E
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001158 // F = setle
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001159 // or C, F
1160 // jnz foo
1161 // Emit:
1162 // cmp A, B
1163 // je foo
1164 // cmp D, E
1165 // jle foo
1166 //
Dan Gohman46510a72010-04-15 01:51:59 +00001167 if (const BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001168 if (BOp->hasOneUse() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001169 (BOp->getOpcode() == Instruction::And ||
1170 BOp->getOpcode() == Instruction::Or)) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001171 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, BrMBB, BrMBB,
1172 BOp->getOpcode());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001173 // If the compares in later blocks need to use values not currently
1174 // exported from this block, export them now. This block should always
1175 // be the first entry.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001176 assert(SwitchCases[0].ThisBB == BrMBB && "Unexpected lowering!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001177
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001178 // Allow some cases to be rejected.
1179 if (ShouldEmitAsBranches(SwitchCases)) {
1180 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1181 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1182 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1183 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001184
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001185 // Emit the branch for this block.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001186 visitSwitchCase(SwitchCases[0], BrMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001187 SwitchCases.erase(SwitchCases.begin());
1188 return;
1189 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001190
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001191 // Okay, we decided not to do this, remove any inserted MBB's and clear
1192 // SwitchCases.
1193 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001194 FuncInfo.MF->erase(SwitchCases[i].ThisBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001195
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001196 SwitchCases.clear();
1197 }
1198 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001199
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001200 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001201 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohman99be8ae2010-04-19 22:41:47 +00001202 NULL, Succ0MBB, Succ1MBB, BrMBB);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001203
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001204 // Use visitSwitchCase to actually insert the fast branch sequence for this
1205 // cond branch.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001206 visitSwitchCase(CB, BrMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001207}
1208
1209/// visitSwitchCase - Emits the necessary code to represent a single node in
1210/// the binary search tree resulting from lowering a switch instruction.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001211void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB,
1212 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001213 SDValue Cond;
1214 SDValue CondLHS = getValue(CB.CmpLHS);
Dale Johannesenf5d97892009-02-04 01:48:28 +00001215 DebugLoc dl = getCurDebugLoc();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001216
1217 // Build the setcc now.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001218 if (CB.CmpMHS == NULL) {
1219 // Fold "(X == true)" to X and "(X == false)" to !X to
1220 // handle common cases produced by branch lowering.
Owen Anderson5defacc2009-07-31 17:39:07 +00001221 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001222 CB.CC == ISD::SETEQ)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001223 Cond = CondLHS;
Owen Anderson5defacc2009-07-31 17:39:07 +00001224 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001225 CB.CC == ISD::SETEQ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001226 SDValue True = DAG.getConstant(1, CondLHS.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001227 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001228 } else
Owen Anderson825b72b2009-08-11 20:47:22 +00001229 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001230 } else {
1231 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
1232
Anton Korobeynikov23218582008-12-23 22:25:27 +00001233 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
1234 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001235
1236 SDValue CmpOp = getValue(CB.CmpMHS);
Owen Andersone50ed302009-08-10 22:56:29 +00001237 EVT VT = CmpOp.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001238
1239 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001240 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT),
Dale Johannesenf5d97892009-02-04 01:48:28 +00001241 ISD::SETLE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001242 } else {
Dale Johannesenf5d97892009-02-04 01:48:28 +00001243 SDValue SUB = DAG.getNode(ISD::SUB, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001244 VT, CmpOp, DAG.getConstant(Low, VT));
Owen Anderson825b72b2009-08-11 20:47:22 +00001245 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001246 DAG.getConstant(High-Low, VT), ISD::SETULE);
1247 }
1248 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001249
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001250 // Update successor info
Dan Gohman99be8ae2010-04-19 22:41:47 +00001251 SwitchBB->addSuccessor(CB.TrueBB);
1252 SwitchBB->addSuccessor(CB.FalseBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001253
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001254 // Set NextBlock to be the MBB immediately after the current one, if any.
1255 // This is used to avoid emitting unnecessary branches to the next block.
1256 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001257 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001258 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001259 NextBlock = BBI;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001260
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001261 // If the lhs block is the next block, invert the condition so that we can
1262 // fall through to the lhs instead of the rhs block.
1263 if (CB.TrueBB == NextBlock) {
1264 std::swap(CB.TrueBB, CB.FalseBB);
1265 SDValue True = DAG.getConstant(1, Cond.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001266 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001267 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001268
Dale Johannesenf5d97892009-02-04 01:48:28 +00001269 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001270 MVT::Other, getControlRoot(), Cond,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001271 DAG.getBasicBlock(CB.TrueBB));
Bill Wendling87710f02009-12-21 23:47:40 +00001272
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001273 // If the branch was constant folded, fix up the CFG.
1274 if (BrCond.getOpcode() == ISD::BR) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001275 SwitchBB->removeSuccessor(CB.FalseBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001276 } else {
1277 // Otherwise, go ahead and insert the false branch.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001278 if (BrCond == getControlRoot())
Dan Gohman99be8ae2010-04-19 22:41:47 +00001279 SwitchBB->removeSuccessor(CB.TrueBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001280
Bill Wendling4533cac2010-01-28 21:51:40 +00001281 if (CB.FalseBB != NextBlock)
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001282 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
1283 DAG.getBasicBlock(CB.FalseBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001284 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001285
1286 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001287}
1288
1289/// visitJumpTable - Emit JumpTable node in the current MBB
Dan Gohman2048b852009-11-23 18:04:58 +00001290void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001291 // Emit the code for the jump table
1292 assert(JT.Reg != -1U && "Should lower JT Header first!");
Owen Andersone50ed302009-08-10 22:56:29 +00001293 EVT PTy = TLI.getPointerTy();
Dale Johannesena04b7572009-02-03 23:04:43 +00001294 SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(),
1295 JT.Reg, PTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001296 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001297 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurDebugLoc(),
1298 MVT::Other, Index.getValue(1),
1299 Table, Index);
1300 DAG.setRoot(BrJumpTable);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001301}
1302
1303/// visitJumpTableHeader - This function emits necessary code to produce index
1304/// in the JumpTable from switch case.
Dan Gohman2048b852009-11-23 18:04:58 +00001305void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001306 JumpTableHeader &JTH,
1307 MachineBasicBlock *SwitchBB) {
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001308 // Subtract the lowest switch case value from the value being switched on and
1309 // conditional branch to default mbb if the result is greater than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001310 // difference between smallest and largest cases.
1311 SDValue SwitchOp = getValue(JTH.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001312 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001313 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001314 DAG.getConstant(JTH.First, VT));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001315
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001316 // The SDNode we just created, which holds the value being switched on minus
Dan Gohmanf451cb82010-02-10 16:03:48 +00001317 // the smallest case value, needs to be copied to a virtual register so it
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001318 // can be used as an index into the jump table in a subsequent basic block.
1319 // This value may be smaller or larger than the target's pointer type, and
1320 // therefore require extension or truncating.
Bill Wendling87710f02009-12-21 23:47:40 +00001321 SwitchOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(), TLI.getPointerTy());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001322
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001323 unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001324 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1325 JumpTableReg, SwitchOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001326 JT.Reg = JumpTableReg;
1327
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001328 // Emit the range check for the jump table, and branch to the default block
1329 // for the switch statement if the value being switched on exceeds the largest
1330 // case in the switch.
Dale Johannesenf5d97892009-02-04 01:48:28 +00001331 SDValue CMP = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001332 TLI.getSetCCResultType(Sub.getValueType()), Sub,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001333 DAG.getConstant(JTH.Last-JTH.First,VT),
1334 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001335
1336 // Set NextBlock to be the MBB immediately after the current one, if any.
1337 // This is used to avoid emitting unnecessary branches to the next block.
1338 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001339 MachineFunction::iterator BBI = SwitchBB;
Bill Wendling87710f02009-12-21 23:47:40 +00001340
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001341 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001342 NextBlock = BBI;
1343
Dale Johannesen66978ee2009-01-31 02:22:37 +00001344 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001345 MVT::Other, CopyTo, CMP,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001346 DAG.getBasicBlock(JT.Default));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001347
Bill Wendling4533cac2010-01-28 21:51:40 +00001348 if (JT.MBB != NextBlock)
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001349 BrCond = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond,
1350 DAG.getBasicBlock(JT.MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001351
Bill Wendling87710f02009-12-21 23:47:40 +00001352 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001353}
1354
1355/// visitBitTestHeader - This function emits necessary code to produce value
1356/// suitable for "bit tests"
Dan Gohman99be8ae2010-04-19 22:41:47 +00001357void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B,
1358 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001359 // Subtract the minimum value
1360 SDValue SwitchOp = getValue(B.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001361 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001362 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001363 DAG.getConstant(B.First, VT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001364
1365 // Check range
Dale Johannesenf5d97892009-02-04 01:48:28 +00001366 SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001367 TLI.getSetCCResultType(Sub.getValueType()),
1368 Sub, DAG.getConstant(B.Range, VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001369 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001370
Bill Wendling87710f02009-12-21 23:47:40 +00001371 SDValue ShiftOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(),
1372 TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001373
Duncan Sands92abc622009-01-31 15:50:11 +00001374 B.Reg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001375 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1376 B.Reg, ShiftOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001377
1378 // Set NextBlock to be the MBB immediately after the current one, if any.
1379 // This is used to avoid emitting unnecessary branches to the next block.
1380 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001381 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001382 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001383 NextBlock = BBI;
1384
1385 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1386
Dan Gohman99be8ae2010-04-19 22:41:47 +00001387 SwitchBB->addSuccessor(B.Default);
1388 SwitchBB->addSuccessor(MBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001389
Dale Johannesen66978ee2009-01-31 02:22:37 +00001390 SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001391 MVT::Other, CopyTo, RangeCmp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001392 DAG.getBasicBlock(B.Default));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001393
Bill Wendling4533cac2010-01-28 21:51:40 +00001394 if (MBB != NextBlock)
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001395 BrRange = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo,
1396 DAG.getBasicBlock(MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001397
Bill Wendling87710f02009-12-21 23:47:40 +00001398 DAG.setRoot(BrRange);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001399}
1400
1401/// visitBitTestCase - this function produces one "bit test"
Dan Gohman2048b852009-11-23 18:04:58 +00001402void SelectionDAGBuilder::visitBitTestCase(MachineBasicBlock* NextMBB,
1403 unsigned Reg,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001404 BitTestCase &B,
1405 MachineBasicBlock *SwitchBB) {
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001406 // Make desired shift
Dale Johannesena04b7572009-02-03 23:04:43 +00001407 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), Reg,
Duncan Sands92abc622009-01-31 15:50:11 +00001408 TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001409 SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001410 TLI.getPointerTy(),
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001411 DAG.getConstant(1, TLI.getPointerTy()),
1412 ShiftOp);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001413
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001414 // Emit bit tests and jumps
Scott Michelfdc40a02009-02-17 22:15:04 +00001415 SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001416 TLI.getPointerTy(), SwitchVal,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001417 DAG.getConstant(B.Mask, TLI.getPointerTy()));
Dale Johannesenf5d97892009-02-04 01:48:28 +00001418 SDValue AndCmp = DAG.getSetCC(getCurDebugLoc(),
1419 TLI.getSetCCResultType(AndOp.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00001420 AndOp, DAG.getConstant(0, TLI.getPointerTy()),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001421 ISD::SETNE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001422
Dan Gohman99be8ae2010-04-19 22:41:47 +00001423 SwitchBB->addSuccessor(B.TargetBB);
1424 SwitchBB->addSuccessor(NextMBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001425
Dale Johannesen66978ee2009-01-31 02:22:37 +00001426 SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001427 MVT::Other, getControlRoot(),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001428 AndCmp, DAG.getBasicBlock(B.TargetBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001429
1430 // Set NextBlock to be the MBB immediately after the current one, if any.
1431 // This is used to avoid emitting unnecessary branches to the next block.
1432 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001433 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001434 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001435 NextBlock = BBI;
1436
Bill Wendling4533cac2010-01-28 21:51:40 +00001437 if (NextMBB != NextBlock)
Bill Wendling0777e922009-12-21 21:59:52 +00001438 BrAnd = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd,
1439 DAG.getBasicBlock(NextMBB));
Bill Wendling0777e922009-12-21 21:59:52 +00001440
Bill Wendling87710f02009-12-21 23:47:40 +00001441 DAG.setRoot(BrAnd);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001442}
1443
Dan Gohman46510a72010-04-15 01:51:59 +00001444void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001445 MachineBasicBlock *InvokeMBB = FuncInfo.MBBMap[I.getParent()];
1446
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001447 // Retrieve successors.
1448 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1449 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1450
Gabor Greifb67e6b32009-01-15 11:10:44 +00001451 const Value *Callee(I.getCalledValue());
1452 if (isa<InlineAsm>(Callee))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001453 visitInlineAsm(&I);
1454 else
Gabor Greifb67e6b32009-01-15 11:10:44 +00001455 LowerCallTo(&I, getValue(Callee), false, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001456
1457 // If the value of the invoke is used outside of its defining block, make it
1458 // available as a virtual register.
Dan Gohmanad62f532009-04-23 23:13:24 +00001459 CopyToExportRegsIfNeeded(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001460
1461 // Update successor info
Dan Gohman99be8ae2010-04-19 22:41:47 +00001462 InvokeMBB->addSuccessor(Return);
1463 InvokeMBB->addSuccessor(LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001464
1465 // Drop into normal successor.
Bill Wendling4533cac2010-01-28 21:51:40 +00001466 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
1467 MVT::Other, getControlRoot(),
1468 DAG.getBasicBlock(Return)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001469}
1470
Dan Gohman46510a72010-04-15 01:51:59 +00001471void SelectionDAGBuilder::visitUnwind(const UnwindInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001472}
1473
1474/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
1475/// small case ranges).
Dan Gohman2048b852009-11-23 18:04:58 +00001476bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR,
1477 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001478 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001479 MachineBasicBlock *Default,
1480 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001481 Case& BackCase = *(CR.Range.second-1);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001482
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001483 // Size is the number of Cases represented by this range.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001484 size_t Size = CR.Range.second - CR.Range.first;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001485 if (Size > 3)
Anton Korobeynikov23218582008-12-23 22:25:27 +00001486 return false;
1487
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001488 // Get the MachineFunction which holds the current MBB. This is used when
1489 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001490 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001491
1492 // Figure out which block is immediately after the current one.
1493 MachineBasicBlock *NextBlock = 0;
1494 MachineFunction::iterator BBI = CR.CaseBB;
1495
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001496 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001497 NextBlock = BBI;
1498
1499 // TODO: If any two of the cases has the same destination, and if one value
1500 // is the same as the other, but has one bit unset that the other has set,
1501 // use bit manipulation to do two compares at once. For example:
1502 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
Anton Korobeynikov23218582008-12-23 22:25:27 +00001503
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001504 // Rearrange the case blocks so that the last one falls through if possible.
1505 if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
1506 // The last case block won't fall through into 'NextBlock' if we emit the
1507 // branches in this order. See if rearranging a case value would help.
1508 for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) {
1509 if (I->BB == NextBlock) {
1510 std::swap(*I, BackCase);
1511 break;
1512 }
1513 }
1514 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001515
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001516 // Create a CaseBlock record representing a conditional branch to
1517 // the Case's target mbb if the value being switched on SV is equal
1518 // to C.
1519 MachineBasicBlock *CurBlock = CR.CaseBB;
1520 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
1521 MachineBasicBlock *FallThrough;
1522 if (I != E-1) {
1523 FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
1524 CurMF->insert(BBI, FallThrough);
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001525
1526 // Put SV in a virtual register to make it available from the new blocks.
1527 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001528 } else {
1529 // If the last case doesn't match, go to the default block.
1530 FallThrough = Default;
1531 }
1532
Dan Gohman46510a72010-04-15 01:51:59 +00001533 const Value *RHS, *LHS, *MHS;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001534 ISD::CondCode CC;
1535 if (I->High == I->Low) {
1536 // This is just small small case range :) containing exactly 1 case
1537 CC = ISD::SETEQ;
1538 LHS = SV; RHS = I->High; MHS = NULL;
1539 } else {
1540 CC = ISD::SETLE;
1541 LHS = I->Low; MHS = SV; RHS = I->High;
1542 }
1543 CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001544
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001545 // If emitting the first comparison, just call visitSwitchCase to emit the
1546 // code into the current block. Otherwise, push the CaseBlock onto the
1547 // vector to be later processed by SDISel, and insert the node's MBB
1548 // before the next MBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001549 if (CurBlock == SwitchBB)
1550 visitSwitchCase(CB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001551 else
1552 SwitchCases.push_back(CB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001553
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001554 CurBlock = FallThrough;
1555 }
1556
1557 return true;
1558}
1559
1560static inline bool areJTsAllowed(const TargetLowering &TLI) {
1561 return !DisableJumpTables &&
Owen Anderson825b72b2009-08-11 20:47:22 +00001562 (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
1563 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001564}
Anton Korobeynikov23218582008-12-23 22:25:27 +00001565
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001566static APInt ComputeRange(const APInt &First, const APInt &Last) {
1567 APInt LastExt(Last), FirstExt(First);
1568 uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1;
1569 LastExt.sext(BitWidth); FirstExt.sext(BitWidth);
1570 return (LastExt - FirstExt + 1ULL);
1571}
1572
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001573/// handleJTSwitchCase - Emit jumptable for current switch case range
Dan Gohman2048b852009-11-23 18:04:58 +00001574bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec& CR,
1575 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001576 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001577 MachineBasicBlock* Default,
1578 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001579 Case& FrontCase = *CR.Range.first;
1580 Case& BackCase = *(CR.Range.second-1);
1581
Chris Lattnere880efe2009-11-07 07:50:34 +00001582 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1583 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001584
Chris Lattnere880efe2009-11-07 07:50:34 +00001585 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001586 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1587 I!=E; ++I)
1588 TSize += I->size();
1589
Dan Gohmane0567812010-04-08 23:03:40 +00001590 if (!areJTsAllowed(TLI) || TSize.ult(4))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001591 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001592
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001593 APInt Range = ComputeRange(First, Last);
Chris Lattnere880efe2009-11-07 07:50:34 +00001594 double Density = TSize.roundToDouble() / Range.roundToDouble();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001595 if (Density < 0.4)
1596 return false;
1597
David Greene4b69d992010-01-05 01:24:57 +00001598 DEBUG(dbgs() << "Lowering jump table\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001599 << "First entry: " << First << ". Last entry: " << Last << '\n'
1600 << "Range: " << Range
1601 << "Size: " << TSize << ". Density: " << Density << "\n\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001602
1603 // Get the MachineFunction which holds the current MBB. This is used when
1604 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001605 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001606
1607 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001608 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001609 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001610
1611 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1612
1613 // Create a new basic block to hold the code for loading the address
1614 // of the jump table, and jumping to it. Update successor information;
1615 // we will either branch to the default case for the switch, or the jump
1616 // table.
1617 MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1618 CurMF->insert(BBI, JumpTableBB);
1619 CR.CaseBB->addSuccessor(Default);
1620 CR.CaseBB->addSuccessor(JumpTableBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001621
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001622 // Build a vector of destination BBs, corresponding to each target
1623 // of the jump table. If the value of the jump table slot corresponds to
1624 // a case statement, push the case's BB onto the vector, otherwise, push
1625 // the default BB.
1626 std::vector<MachineBasicBlock*> DestBBs;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001627 APInt TEI = First;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001628 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
Chris Lattner071c62f2010-01-25 23:26:13 +00001629 const APInt &Low = cast<ConstantInt>(I->Low)->getValue();
1630 const APInt &High = cast<ConstantInt>(I->High)->getValue();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001631
1632 if (Low.sle(TEI) && TEI.sle(High)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001633 DestBBs.push_back(I->BB);
1634 if (TEI==High)
1635 ++I;
1636 } else {
1637 DestBBs.push_back(Default);
1638 }
1639 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001640
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001641 // Update successor info. Add one edge to each unique successor.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001642 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
1643 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001644 E = DestBBs.end(); I != E; ++I) {
1645 if (!SuccsHandled[(*I)->getNumber()]) {
1646 SuccsHandled[(*I)->getNumber()] = true;
1647 JumpTableBB->addSuccessor(*I);
1648 }
1649 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001650
Bob Wilsond1ec31d2010-03-18 18:42:41 +00001651 // Create a jump table index for this jump table.
Chris Lattner071c62f2010-01-25 23:26:13 +00001652 unsigned JTEncoding = TLI.getJumpTableEncoding();
1653 unsigned JTI = CurMF->getOrCreateJumpTableInfo(JTEncoding)
Bob Wilsond1ec31d2010-03-18 18:42:41 +00001654 ->createJumpTableIndex(DestBBs);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001655
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001656 // Set the jump table information so that we can codegen it as a second
1657 // MachineBasicBlock
1658 JumpTable JT(-1U, JTI, JumpTableBB, Default);
Dan Gohman99be8ae2010-04-19 22:41:47 +00001659 JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == SwitchBB));
1660 if (CR.CaseBB == SwitchBB)
1661 visitJumpTableHeader(JT, JTH, SwitchBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001662
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001663 JTCases.push_back(JumpTableBlock(JTH, JT));
1664
1665 return true;
1666}
1667
1668/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
1669/// 2 subtrees.
Dan Gohman2048b852009-11-23 18:04:58 +00001670bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR,
1671 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001672 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001673 MachineBasicBlock *Default,
1674 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001675 // Get the MachineFunction which holds the current MBB. This is used when
1676 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001677 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001678
1679 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001680 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001681 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001682
1683 Case& FrontCase = *CR.Range.first;
1684 Case& BackCase = *(CR.Range.second-1);
1685 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1686
1687 // Size is the number of Cases represented by this range.
1688 unsigned Size = CR.Range.second - CR.Range.first;
1689
Chris Lattnere880efe2009-11-07 07:50:34 +00001690 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1691 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001692 double FMetric = 0;
1693 CaseItr Pivot = CR.Range.first + Size/2;
1694
1695 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
1696 // (heuristically) allow us to emit JumpTable's later.
Chris Lattnere880efe2009-11-07 07:50:34 +00001697 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001698 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1699 I!=E; ++I)
1700 TSize += I->size();
1701
Chris Lattnere880efe2009-11-07 07:50:34 +00001702 APInt LSize = FrontCase.size();
1703 APInt RSize = TSize-LSize;
David Greene4b69d992010-01-05 01:24:57 +00001704 DEBUG(dbgs() << "Selecting best pivot: \n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001705 << "First: " << First << ", Last: " << Last <<'\n'
1706 << "LSize: " << LSize << ", RSize: " << RSize << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001707 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
1708 J!=E; ++I, ++J) {
Chris Lattnere880efe2009-11-07 07:50:34 +00001709 const APInt &LEnd = cast<ConstantInt>(I->High)->getValue();
1710 const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001711 APInt Range = ComputeRange(LEnd, RBegin);
1712 assert((Range - 2ULL).isNonNegative() &&
1713 "Invalid case distance");
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001714 double LDensity = (double)LSize.roundToDouble() /
Chris Lattnere880efe2009-11-07 07:50:34 +00001715 (LEnd - First + 1ULL).roundToDouble();
1716 double RDensity = (double)RSize.roundToDouble() /
1717 (Last - RBegin + 1ULL).roundToDouble();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001718 double Metric = Range.logBase2()*(LDensity+RDensity);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001719 // Should always split in some non-trivial place
David Greene4b69d992010-01-05 01:24:57 +00001720 DEBUG(dbgs() <<"=>Step\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001721 << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n'
1722 << "LDensity: " << LDensity
1723 << ", RDensity: " << RDensity << '\n'
1724 << "Metric: " << Metric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001725 if (FMetric < Metric) {
1726 Pivot = J;
1727 FMetric = Metric;
David Greene4b69d992010-01-05 01:24:57 +00001728 DEBUG(dbgs() << "Current metric set to: " << FMetric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001729 }
1730
1731 LSize += J->size();
1732 RSize -= J->size();
1733 }
1734 if (areJTsAllowed(TLI)) {
1735 // If our case is dense we *really* should handle it earlier!
1736 assert((FMetric > 0) && "Should handle dense range earlier!");
1737 } else {
1738 Pivot = CR.Range.first + Size/2;
1739 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001740
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001741 CaseRange LHSR(CR.Range.first, Pivot);
1742 CaseRange RHSR(Pivot, CR.Range.second);
1743 Constant *C = Pivot->Low;
1744 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001745
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001746 // We know that we branch to the LHS if the Value being switched on is
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001747 // less than the Pivot value, C. We use this to optimize our binary
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001748 // tree a bit, by recognizing that if SV is greater than or equal to the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001749 // LHS's Case Value, and that Case Value is exactly one less than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001750 // Pivot's Value, then we can branch directly to the LHS's Target,
1751 // rather than creating a leaf node for it.
1752 if ((LHSR.second - LHSR.first) == 1 &&
1753 LHSR.first->High == CR.GE &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001754 cast<ConstantInt>(C)->getValue() ==
1755 (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001756 TrueBB = LHSR.first->BB;
1757 } else {
1758 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1759 CurMF->insert(BBI, TrueBB);
1760 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001761
1762 // Put SV in a virtual register to make it available from the new blocks.
1763 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001764 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001765
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001766 // Similar to the optimization above, if the Value being switched on is
1767 // known to be less than the Constant CR.LT, and the current Case Value
1768 // is CR.LT - 1, then we can branch directly to the target block for
1769 // the current Case Value, rather than emitting a RHS leaf node for it.
1770 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001771 cast<ConstantInt>(RHSR.first->Low)->getValue() ==
1772 (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001773 FalseBB = RHSR.first->BB;
1774 } else {
1775 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1776 CurMF->insert(BBI, FalseBB);
1777 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001778
1779 // Put SV in a virtual register to make it available from the new blocks.
1780 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001781 }
1782
1783 // Create a CaseBlock record representing a conditional branch to
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001784 // the LHS node if the value being switched on SV is less than C.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001785 // Otherwise, branch to LHS.
1786 CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
1787
Dan Gohman99be8ae2010-04-19 22:41:47 +00001788 if (CR.CaseBB == SwitchBB)
1789 visitSwitchCase(CB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001790 else
1791 SwitchCases.push_back(CB);
1792
1793 return true;
1794}
1795
1796/// handleBitTestsSwitchCase - if current case range has few destination and
1797/// range span less, than machine word bitwidth, encode case range into series
1798/// of masks and emit bit tests with these masks.
Dan Gohman2048b852009-11-23 18:04:58 +00001799bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR,
1800 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001801 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001802 MachineBasicBlock* Default,
1803 MachineBasicBlock *SwitchBB){
Owen Andersone50ed302009-08-10 22:56:29 +00001804 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00001805 unsigned IntPtrBits = PTy.getSizeInBits();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001806
1807 Case& FrontCase = *CR.Range.first;
1808 Case& BackCase = *(CR.Range.second-1);
1809
1810 // Get the MachineFunction which holds the current MBB. This is used when
1811 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001812 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001813
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00001814 // If target does not have legal shift left, do not emit bit tests at all.
1815 if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy()))
1816 return false;
1817
Anton Korobeynikov23218582008-12-23 22:25:27 +00001818 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001819 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1820 I!=E; ++I) {
1821 // Single case counts one, case range - two.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001822 numCmps += (I->Low == I->High ? 1 : 2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001823 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001824
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001825 // Count unique destinations
1826 SmallSet<MachineBasicBlock*, 4> Dests;
1827 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1828 Dests.insert(I->BB);
1829 if (Dests.size() > 3)
1830 // Don't bother the code below, if there are too much unique destinations
1831 return false;
1832 }
David Greene4b69d992010-01-05 01:24:57 +00001833 DEBUG(dbgs() << "Total number of unique destinations: "
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001834 << Dests.size() << '\n'
1835 << "Total number of comparisons: " << numCmps << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001836
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001837 // Compute span of values.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001838 const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue();
1839 const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001840 APInt cmpRange = maxValue - minValue;
1841
David Greene4b69d992010-01-05 01:24:57 +00001842 DEBUG(dbgs() << "Compare range: " << cmpRange << '\n'
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001843 << "Low bound: " << minValue << '\n'
1844 << "High bound: " << maxValue << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001845
Dan Gohmane0567812010-04-08 23:03:40 +00001846 if (cmpRange.uge(IntPtrBits) ||
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001847 (!(Dests.size() == 1 && numCmps >= 3) &&
1848 !(Dests.size() == 2 && numCmps >= 5) &&
1849 !(Dests.size() >= 3 && numCmps >= 6)))
1850 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001851
David Greene4b69d992010-01-05 01:24:57 +00001852 DEBUG(dbgs() << "Emitting bit tests\n");
Anton Korobeynikov23218582008-12-23 22:25:27 +00001853 APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth());
1854
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001855 // Optimize the case where all the case values fit in a
1856 // word without having to subtract minValue. In this case,
1857 // we can optimize away the subtraction.
Dan Gohmane0567812010-04-08 23:03:40 +00001858 if (minValue.isNonNegative() && maxValue.slt(IntPtrBits)) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001859 cmpRange = maxValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001860 } else {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001861 lowBound = minValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001862 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001863
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001864 CaseBitsVector CasesBits;
1865 unsigned i, count = 0;
1866
1867 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1868 MachineBasicBlock* Dest = I->BB;
1869 for (i = 0; i < count; ++i)
1870 if (Dest == CasesBits[i].BB)
1871 break;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001872
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001873 if (i == count) {
1874 assert((count < 3) && "Too much destinations to test!");
1875 CasesBits.push_back(CaseBits(0, Dest, 0));
1876 count++;
1877 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001878
1879 const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue();
1880 const APInt& highValue = cast<ConstantInt>(I->High)->getValue();
1881
1882 uint64_t lo = (lowValue - lowBound).getZExtValue();
1883 uint64_t hi = (highValue - lowBound).getZExtValue();
1884
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001885 for (uint64_t j = lo; j <= hi; j++) {
1886 CasesBits[i].Mask |= 1ULL << j;
1887 CasesBits[i].Bits++;
1888 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001889
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001890 }
1891 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001892
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001893 BitTestInfo BTC;
1894
1895 // Figure out which block is immediately after the current one.
1896 MachineFunction::iterator BBI = CR.CaseBB;
1897 ++BBI;
1898
1899 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1900
David Greene4b69d992010-01-05 01:24:57 +00001901 DEBUG(dbgs() << "Cases:\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001902 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
David Greene4b69d992010-01-05 01:24:57 +00001903 DEBUG(dbgs() << "Mask: " << CasesBits[i].Mask
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001904 << ", Bits: " << CasesBits[i].Bits
1905 << ", BB: " << CasesBits[i].BB << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001906
1907 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1908 CurMF->insert(BBI, CaseBB);
1909 BTC.push_back(BitTestCase(CasesBits[i].Mask,
1910 CaseBB,
1911 CasesBits[i].BB));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001912
1913 // Put SV in a virtual register to make it available from the new blocks.
1914 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001915 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001916
1917 BitTestBlock BTB(lowBound, cmpRange, SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001918 -1U, (CR.CaseBB == SwitchBB),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001919 CR.CaseBB, Default, BTC);
1920
Dan Gohman99be8ae2010-04-19 22:41:47 +00001921 if (CR.CaseBB == SwitchBB)
1922 visitBitTestHeader(BTB, SwitchBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001923
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001924 BitTestCases.push_back(BTB);
1925
1926 return true;
1927}
1928
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001929/// Clusterify - Transform simple list of Cases into list of CaseRange's
Dan Gohman2048b852009-11-23 18:04:58 +00001930size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases,
1931 const SwitchInst& SI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001932 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001933
1934 // Start with "simple" cases
Anton Korobeynikov23218582008-12-23 22:25:27 +00001935 for (size_t i = 1; i < SI.getNumSuccessors(); ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001936 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)];
1937 Cases.push_back(Case(SI.getSuccessorValue(i),
1938 SI.getSuccessorValue(i),
1939 SMBB));
1940 }
1941 std::sort(Cases.begin(), Cases.end(), CaseCmp());
1942
1943 // Merge case into clusters
Anton Korobeynikov23218582008-12-23 22:25:27 +00001944 if (Cases.size() >= 2)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001945 // Must recompute end() each iteration because it may be
1946 // invalidated by erase if we hold on to it
Anton Korobeynikov23218582008-12-23 22:25:27 +00001947 for (CaseItr I = Cases.begin(), J = ++(Cases.begin()); J != Cases.end(); ) {
1948 const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue();
1949 const APInt& currentValue = cast<ConstantInt>(I->High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001950 MachineBasicBlock* nextBB = J->BB;
1951 MachineBasicBlock* currentBB = I->BB;
1952
1953 // If the two neighboring cases go to the same destination, merge them
1954 // into a single case.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001955 if ((nextValue - currentValue == 1) && (currentBB == nextBB)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001956 I->High = J->High;
1957 J = Cases.erase(J);
1958 } else {
1959 I = J++;
1960 }
1961 }
1962
1963 for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
1964 if (I->Low != I->High)
1965 // A range counts double, since it requires two compares.
1966 ++numCmps;
1967 }
1968
1969 return numCmps;
1970}
1971
Dan Gohman46510a72010-04-15 01:51:59 +00001972void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001973 MachineBasicBlock *SwitchMBB = FuncInfo.MBBMap[SI.getParent()];
1974
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001975 // Figure out which block is immediately after the current one.
1976 MachineBasicBlock *NextBlock = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001977 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
1978
1979 // If there is only the default destination, branch to it if it is not the
1980 // next basic block. Otherwise, just fall through.
1981 if (SI.getNumOperands() == 2) {
1982 // Update machine-CFG edges.
1983
1984 // If this is not a fall-through branch, emit the branch.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001985 SwitchMBB->addSuccessor(Default);
Bill Wendling4533cac2010-01-28 21:51:40 +00001986 if (Default != NextBlock)
1987 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
1988 MVT::Other, getControlRoot(),
1989 DAG.getBasicBlock(Default)));
Bill Wendling49fcff82009-12-21 22:30:11 +00001990
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001991 return;
1992 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001993
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001994 // If there are any non-default case statements, create a vector of Cases
1995 // representing each one, and sort the vector so that we can efficiently
1996 // create a binary search tree from them.
1997 CaseVector Cases;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001998 size_t numCmps = Clusterify(Cases, SI);
David Greene4b69d992010-01-05 01:24:57 +00001999 DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size()
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002000 << ". Total compares: " << numCmps << '\n');
Devang Patel8a84e442009-01-05 17:31:22 +00002001 numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002002
2003 // Get the Value to be switched on and default basic blocks, which will be
2004 // inserted into CaseBlock records, representing basic blocks in the binary
2005 // search tree.
Dan Gohman46510a72010-04-15 01:51:59 +00002006 const Value *SV = SI.getOperand(0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002007
2008 // Push the initial CaseRec onto the worklist
2009 CaseRecVector WorkList;
Dan Gohman99be8ae2010-04-19 22:41:47 +00002010 WorkList.push_back(CaseRec(SwitchMBB,0,0,
2011 CaseRange(Cases.begin(),Cases.end())));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002012
2013 while (!WorkList.empty()) {
2014 // Grab a record representing a case range to process off the worklist
2015 CaseRec CR = WorkList.back();
2016 WorkList.pop_back();
2017
Dan Gohman99be8ae2010-04-19 22:41:47 +00002018 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002019 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002020
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002021 // If the range has few cases (two or less) emit a series of specific
2022 // tests.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002023 if (handleSmallSwitchRange(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002024 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002025
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002026 // If the switch has more than 5 blocks, and at least 40% dense, and the
2027 // target supports indirect branches, then emit a jump table rather than
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002028 // lowering the switch to a binary tree of conditional branches.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002029 if (handleJTSwitchCase(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002030 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002031
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002032 // Emit binary tree. We need to pick a pivot, and push left and right ranges
2033 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002034 handleBTSplitSwitchCase(CR, WorkList, SV, Default, SwitchMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002035 }
2036}
2037
Dan Gohman46510a72010-04-15 01:51:59 +00002038void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00002039 MachineBasicBlock *IndirectBrMBB = FuncInfo.MBBMap[I.getParent()];
2040
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002041 // Update machine-CFG edges with unique successors.
Jakob Stoklund Olesenb5b90ed2010-02-11 18:06:56 +00002042 SmallVector<BasicBlock*, 32> succs;
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002043 succs.reserve(I.getNumSuccessors());
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002044 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i)
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002045 succs.push_back(I.getSuccessor(i));
Jakob Stoklund Olesenb5b90ed2010-02-11 18:06:56 +00002046 array_pod_sort(succs.begin(), succs.end());
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002047 succs.erase(std::unique(succs.begin(), succs.end()), succs.end());
2048 for (unsigned i = 0, e = succs.size(); i != e; ++i)
Dan Gohman99be8ae2010-04-19 22:41:47 +00002049 IndirectBrMBB->addSuccessor(FuncInfo.MBBMap[succs[i]]);
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002050
Bill Wendling4533cac2010-01-28 21:51:40 +00002051 DAG.setRoot(DAG.getNode(ISD::BRIND, getCurDebugLoc(),
2052 MVT::Other, getControlRoot(),
2053 getValue(I.getAddress())));
Bill Wendling49fcff82009-12-21 22:30:11 +00002054}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002055
Dan Gohman46510a72010-04-15 01:51:59 +00002056void SelectionDAGBuilder::visitFSub(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002057 // -0.0 - X --> fneg
2058 const Type *Ty = I.getType();
Duncan Sands1df98592010-02-16 11:11:14 +00002059 if (Ty->isVectorTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002060 if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) {
2061 const VectorType *DestTy = cast<VectorType>(I.getType());
2062 const Type *ElTy = DestTy->getElementType();
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002063 unsigned VL = DestTy->getNumElements();
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002064 std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy));
Owen Andersonaf7ec972009-07-28 21:19:26 +00002065 Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size());
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002066 if (CV == CNZ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002067 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00002068 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2069 Op2.getValueType(), Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002070 return;
2071 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002072 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002073 }
Bill Wendling49fcff82009-12-21 22:30:11 +00002074
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002075 if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002076 if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002077 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00002078 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2079 Op2.getValueType(), Op2));
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002080 return;
2081 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002082
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002083 visitBinary(I, ISD::FSUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002084}
2085
Dan Gohman46510a72010-04-15 01:51:59 +00002086void SelectionDAGBuilder::visitBinary(const User &I, unsigned OpCode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002087 SDValue Op1 = getValue(I.getOperand(0));
2088 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00002089 setValue(&I, DAG.getNode(OpCode, getCurDebugLoc(),
2090 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002091}
2092
Dan Gohman46510a72010-04-15 01:51:59 +00002093void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002094 SDValue Op1 = getValue(I.getOperand(0));
2095 SDValue Op2 = getValue(I.getOperand(1));
Duncan Sands1df98592010-02-16 11:11:14 +00002096 if (!I.getType()->isVectorTy() &&
Dan Gohman57fc82d2009-04-09 03:51:29 +00002097 Op2.getValueType() != TLI.getShiftAmountTy()) {
2098 // If the operand is smaller than the shift count type, promote it.
Owen Andersone50ed302009-08-10 22:56:29 +00002099 EVT PTy = TLI.getPointerTy();
2100 EVT STy = TLI.getShiftAmountTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002101 if (STy.bitsGT(Op2.getValueType()))
Dan Gohman57fc82d2009-04-09 03:51:29 +00002102 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
2103 TLI.getShiftAmountTy(), Op2);
2104 // If the operand is larger than the shift count type but the shift
2105 // count type has enough bits to represent any shift value, truncate
2106 // it now. This is a common case and it exposes the truncate to
2107 // optimization early.
Owen Anderson77547be2009-08-10 18:56:59 +00002108 else if (STy.getSizeInBits() >=
Dan Gohman57fc82d2009-04-09 03:51:29 +00002109 Log2_32_Ceil(Op2.getValueType().getSizeInBits()))
2110 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2111 TLI.getShiftAmountTy(), Op2);
2112 // Otherwise we'll need to temporarily settle for some other
2113 // convenient type; type legalization will make adjustments as
2114 // needed.
Owen Anderson77547be2009-08-10 18:56:59 +00002115 else if (PTy.bitsLT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002116 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002117 TLI.getPointerTy(), Op2);
Owen Anderson77547be2009-08-10 18:56:59 +00002118 else if (PTy.bitsGT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002119 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002120 TLI.getPointerTy(), Op2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002121 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002122
Bill Wendling4533cac2010-01-28 21:51:40 +00002123 setValue(&I, DAG.getNode(Opcode, getCurDebugLoc(),
2124 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002125}
2126
Dan Gohman46510a72010-04-15 01:51:59 +00002127void SelectionDAGBuilder::visitICmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002128 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002129 if (const ICmpInst *IC = dyn_cast<ICmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002130 predicate = IC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002131 else if (const ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002132 predicate = ICmpInst::Predicate(IC->getPredicate());
2133 SDValue Op1 = getValue(I.getOperand(0));
2134 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002135 ISD::CondCode Opcode = getICmpCondCode(predicate);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002136
Owen Andersone50ed302009-08-10 22:56:29 +00002137 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002138 setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Opcode));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002139}
2140
Dan Gohman46510a72010-04-15 01:51:59 +00002141void SelectionDAGBuilder::visitFCmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002142 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002143 if (const FCmpInst *FC = dyn_cast<FCmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002144 predicate = FC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002145 else if (const ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002146 predicate = FCmpInst::Predicate(FC->getPredicate());
2147 SDValue Op1 = getValue(I.getOperand(0));
2148 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002149 ISD::CondCode Condition = getFCmpCondCode(predicate);
Owen Andersone50ed302009-08-10 22:56:29 +00002150 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002151 setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002152}
2153
Dan Gohman46510a72010-04-15 01:51:59 +00002154void SelectionDAGBuilder::visitSelect(const User &I) {
Owen Andersone50ed302009-08-10 22:56:29 +00002155 SmallVector<EVT, 4> ValueVTs;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002156 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2157 unsigned NumValues = ValueVTs.size();
Bill Wendling49fcff82009-12-21 22:30:11 +00002158 if (NumValues == 0) return;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002159
Bill Wendling49fcff82009-12-21 22:30:11 +00002160 SmallVector<SDValue, 4> Values(NumValues);
2161 SDValue Cond = getValue(I.getOperand(0));
2162 SDValue TrueVal = getValue(I.getOperand(1));
2163 SDValue FalseVal = getValue(I.getOperand(2));
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002164
Bill Wendling4533cac2010-01-28 21:51:40 +00002165 for (unsigned i = 0; i != NumValues; ++i)
Bill Wendling49fcff82009-12-21 22:30:11 +00002166 Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(),
Chris Lattnerb3e87b22010-03-12 07:15:36 +00002167 TrueVal.getNode()->getValueType(TrueVal.getResNo()+i),
2168 Cond,
Bill Wendling49fcff82009-12-21 22:30:11 +00002169 SDValue(TrueVal.getNode(),
2170 TrueVal.getResNo() + i),
2171 SDValue(FalseVal.getNode(),
2172 FalseVal.getResNo() + i));
2173
Bill Wendling4533cac2010-01-28 21:51:40 +00002174 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2175 DAG.getVTList(&ValueVTs[0], NumValues),
2176 &Values[0], NumValues));
Bill Wendling49fcff82009-12-21 22:30:11 +00002177}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002178
Dan Gohman46510a72010-04-15 01:51:59 +00002179void SelectionDAGBuilder::visitTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002180 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2181 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002182 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002183 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002184}
2185
Dan Gohman46510a72010-04-15 01:51:59 +00002186void SelectionDAGBuilder::visitZExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002187 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2188 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2189 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002190 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002191 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002192}
2193
Dan Gohman46510a72010-04-15 01:51:59 +00002194void SelectionDAGBuilder::visitSExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002195 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2196 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2197 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002198 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002199 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002200}
2201
Dan Gohman46510a72010-04-15 01:51:59 +00002202void SelectionDAGBuilder::visitFPTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002203 // FPTrunc is never a no-op cast, no need to check
2204 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002205 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002206 setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(),
2207 DestVT, N, DAG.getIntPtrConstant(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002208}
2209
Dan Gohman46510a72010-04-15 01:51:59 +00002210void SelectionDAGBuilder::visitFPExt(const User &I){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002211 // FPTrunc is never a no-op cast, no need to check
2212 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002213 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002214 setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002215}
2216
Dan Gohman46510a72010-04-15 01:51:59 +00002217void SelectionDAGBuilder::visitFPToUI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002218 // FPToUI is never a no-op cast, no need to check
2219 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002220 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002221 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002222}
2223
Dan Gohman46510a72010-04-15 01:51:59 +00002224void SelectionDAGBuilder::visitFPToSI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002225 // FPToSI is never a no-op cast, no need to check
2226 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002227 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002228 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002229}
2230
Dan Gohman46510a72010-04-15 01:51:59 +00002231void SelectionDAGBuilder::visitUIToFP(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002232 // UIToFP is never a no-op cast, no need to check
2233 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002234 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002235 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002236}
2237
Dan Gohman46510a72010-04-15 01:51:59 +00002238void SelectionDAGBuilder::visitSIToFP(const User &I){
Bill Wendling181b6272008-10-19 20:34:04 +00002239 // SIToFP is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002240 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002241 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002242 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002243}
2244
Dan Gohman46510a72010-04-15 01:51:59 +00002245void SelectionDAGBuilder::visitPtrToInt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002246 // What to do depends on the size of the integer and the size of the pointer.
2247 // We can either truncate, zero extend, or no-op, accordingly.
2248 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002249 EVT SrcVT = N.getValueType();
2250 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002251 setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002252}
2253
Dan Gohman46510a72010-04-15 01:51:59 +00002254void SelectionDAGBuilder::visitIntToPtr(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002255 // What to do depends on the size of the integer and the size of the pointer.
2256 // We can either truncate, zero extend, or no-op, accordingly.
2257 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002258 EVT SrcVT = N.getValueType();
2259 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002260 setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002261}
2262
Dan Gohman46510a72010-04-15 01:51:59 +00002263void SelectionDAGBuilder::visitBitCast(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002264 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002265 EVT DestVT = TLI.getValueType(I.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002266
Bill Wendling49fcff82009-12-21 22:30:11 +00002267 // BitCast assures us that source and destination are the same size so this is
2268 // either a BIT_CONVERT or a no-op.
Bill Wendling4533cac2010-01-28 21:51:40 +00002269 if (DestVT != N.getValueType())
2270 setValue(&I, DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
2271 DestVT, N)); // convert types.
2272 else
Bill Wendling49fcff82009-12-21 22:30:11 +00002273 setValue(&I, N); // noop cast.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002274}
2275
Dan Gohman46510a72010-04-15 01:51:59 +00002276void SelectionDAGBuilder::visitInsertElement(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002277 SDValue InVec = getValue(I.getOperand(0));
2278 SDValue InVal = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00002279 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002280 TLI.getPointerTy(),
2281 getValue(I.getOperand(2)));
Bill Wendling4533cac2010-01-28 21:51:40 +00002282 setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(),
2283 TLI.getValueType(I.getType()),
2284 InVec, InVal, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002285}
2286
Dan Gohman46510a72010-04-15 01:51:59 +00002287void SelectionDAGBuilder::visitExtractElement(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002288 SDValue InVec = getValue(I.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002289 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002290 TLI.getPointerTy(),
2291 getValue(I.getOperand(1)));
Bill Wendling4533cac2010-01-28 21:51:40 +00002292 setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2293 TLI.getValueType(I.getType()), InVec, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002294}
2295
Mon P Wangaeb06d22008-11-10 04:46:22 +00002296// Utility for visitShuffleVector - Returns true if the mask is mask starting
2297// from SIndx and increasing to the element length (undefs are allowed).
Nate Begeman5a5ca152009-04-29 05:20:52 +00002298static bool SequentialMask(SmallVectorImpl<int> &Mask, unsigned SIndx) {
2299 unsigned MaskNumElts = Mask.size();
2300 for (unsigned i = 0; i != MaskNumElts; ++i)
2301 if ((Mask[i] >= 0) && (Mask[i] != (int)(i + SIndx)))
Nate Begeman9008ca62009-04-27 18:41:29 +00002302 return false;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002303 return true;
2304}
2305
Dan Gohman46510a72010-04-15 01:51:59 +00002306void SelectionDAGBuilder::visitShuffleVector(const User &I) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002307 SmallVector<int, 8> Mask;
Mon P Wang230e4fa2008-11-21 04:25:21 +00002308 SDValue Src1 = getValue(I.getOperand(0));
2309 SDValue Src2 = getValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002310
Nate Begeman9008ca62009-04-27 18:41:29 +00002311 // Convert the ConstantVector mask operand into an array of ints, with -1
2312 // representing undef values.
2313 SmallVector<Constant*, 8> MaskElts;
Chris Lattnerb29d5962010-02-01 20:48:08 +00002314 cast<Constant>(I.getOperand(2))->getVectorElements(MaskElts);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002315 unsigned MaskNumElts = MaskElts.size();
2316 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002317 if (isa<UndefValue>(MaskElts[i]))
2318 Mask.push_back(-1);
2319 else
2320 Mask.push_back(cast<ConstantInt>(MaskElts[i])->getSExtValue());
2321 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002322
Owen Andersone50ed302009-08-10 22:56:29 +00002323 EVT VT = TLI.getValueType(I.getType());
2324 EVT SrcVT = Src1.getValueType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00002325 unsigned SrcNumElts = SrcVT.getVectorNumElements();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002326
Mon P Wangc7849c22008-11-16 05:06:27 +00002327 if (SrcNumElts == MaskNumElts) {
Bill Wendling4533cac2010-01-28 21:51:40 +00002328 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2329 &Mask[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002330 return;
2331 }
2332
2333 // Normalize the shuffle vector since mask and vector length don't match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002334 if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) {
2335 // Mask is longer than the source vectors and is a multiple of the source
2336 // vectors. We can use concatenate vector to make the mask and vectors
Mon P Wang230e4fa2008-11-21 04:25:21 +00002337 // lengths match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002338 if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) {
2339 // The shuffle is concatenating two vectors together.
Bill Wendling4533cac2010-01-28 21:51:40 +00002340 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(),
2341 VT, Src1, Src2));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002342 return;
2343 }
2344
Mon P Wangc7849c22008-11-16 05:06:27 +00002345 // Pad both vectors with undefs to make them the same length as the mask.
2346 unsigned NumConcat = MaskNumElts / SrcNumElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00002347 bool Src1U = Src1.getOpcode() == ISD::UNDEF;
2348 bool Src2U = Src2.getOpcode() == ISD::UNDEF;
Dale Johannesene8d72302009-02-06 23:05:02 +00002349 SDValue UndefVal = DAG.getUNDEF(SrcVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002350
Nate Begeman9008ca62009-04-27 18:41:29 +00002351 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
2352 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002353 MOps1[0] = Src1;
2354 MOps2[0] = Src2;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002355
2356 Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2357 getCurDebugLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002358 &MOps1[0], NumConcat);
2359 Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002360 getCurDebugLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002361 &MOps2[0], NumConcat);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002362
Mon P Wangaeb06d22008-11-10 04:46:22 +00002363 // Readjust mask for new input vector length.
Nate Begeman9008ca62009-04-27 18:41:29 +00002364 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002365 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002366 int Idx = Mask[i];
Nate Begeman5a5ca152009-04-29 05:20:52 +00002367 if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002368 MappedOps.push_back(Idx);
2369 else
2370 MappedOps.push_back(Idx + MaskNumElts - SrcNumElts);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002371 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002372
Bill Wendling4533cac2010-01-28 21:51:40 +00002373 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2374 &MappedOps[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002375 return;
2376 }
2377
Mon P Wangc7849c22008-11-16 05:06:27 +00002378 if (SrcNumElts > MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002379 // Analyze the access pattern of the vector to see if we can extract
2380 // two subvectors and do the shuffle. The analysis is done by calculating
2381 // the range of elements the mask access on both vectors.
2382 int MinRange[2] = { SrcNumElts+1, SrcNumElts+1};
2383 int MaxRange[2] = {-1, -1};
2384
Nate Begeman5a5ca152009-04-29 05:20:52 +00002385 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002386 int Idx = Mask[i];
2387 int Input = 0;
2388 if (Idx < 0)
2389 continue;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002390
Nate Begeman5a5ca152009-04-29 05:20:52 +00002391 if (Idx >= (int)SrcNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002392 Input = 1;
2393 Idx -= SrcNumElts;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002394 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002395 if (Idx > MaxRange[Input])
2396 MaxRange[Input] = Idx;
2397 if (Idx < MinRange[Input])
2398 MinRange[Input] = Idx;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002399 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002400
Mon P Wangc7849c22008-11-16 05:06:27 +00002401 // Check if the access is smaller than the vector size and can we find
2402 // a reasonable extract index.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002403 int RangeUse[2] = { 2, 2 }; // 0 = Unused, 1 = Extract, 2 = Can not
2404 // Extract.
Mon P Wangc7849c22008-11-16 05:06:27 +00002405 int StartIdx[2]; // StartIdx to extract from
2406 for (int Input=0; Input < 2; ++Input) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002407 if (MinRange[Input] == (int)(SrcNumElts+1) && MaxRange[Input] == -1) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002408 RangeUse[Input] = 0; // Unused
2409 StartIdx[Input] = 0;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002410 } else if (MaxRange[Input] - MinRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002411 // Fits within range but we should see if we can find a good
Mon P Wang230e4fa2008-11-21 04:25:21 +00002412 // start index that is a multiple of the mask length.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002413 if (MaxRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002414 RangeUse[Input] = 1; // Extract from beginning of the vector
2415 StartIdx[Input] = 0;
2416 } else {
2417 StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002418 if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts &&
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002419 StartIdx[Input] + MaskNumElts < SrcNumElts)
Mon P Wangc7849c22008-11-16 05:06:27 +00002420 RangeUse[Input] = 1; // Extract from a multiple of the mask length.
Mon P Wangc7849c22008-11-16 05:06:27 +00002421 }
Mon P Wang230e4fa2008-11-21 04:25:21 +00002422 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002423 }
2424
Bill Wendling636e2582009-08-21 18:16:06 +00002425 if (RangeUse[0] == 0 && RangeUse[1] == 0) {
Bill Wendling4533cac2010-01-28 21:51:40 +00002426 setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
Mon P Wangc7849c22008-11-16 05:06:27 +00002427 return;
2428 }
2429 else if (RangeUse[0] < 2 && RangeUse[1] < 2) {
2430 // Extract appropriate subvector and generate a vector shuffle
2431 for (int Input=0; Input < 2; ++Input) {
Bill Wendling87710f02009-12-21 23:47:40 +00002432 SDValue &Src = Input == 0 ? Src1 : Src2;
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002433 if (RangeUse[Input] == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00002434 Src = DAG.getUNDEF(VT);
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002435 else
Dale Johannesen66978ee2009-01-31 02:22:37 +00002436 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002437 Src, DAG.getIntPtrConstant(StartIdx[Input]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002438 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002439
Mon P Wangc7849c22008-11-16 05:06:27 +00002440 // Calculate new mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00002441 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002442 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002443 int Idx = Mask[i];
2444 if (Idx < 0)
2445 MappedOps.push_back(Idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002446 else if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002447 MappedOps.push_back(Idx - StartIdx[0]);
2448 else
2449 MappedOps.push_back(Idx - SrcNumElts - StartIdx[1] + MaskNumElts);
Mon P Wangc7849c22008-11-16 05:06:27 +00002450 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002451
Bill Wendling4533cac2010-01-28 21:51:40 +00002452 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2453 &MappedOps[0]));
Mon P Wangc7849c22008-11-16 05:06:27 +00002454 return;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002455 }
2456 }
2457
Mon P Wangc7849c22008-11-16 05:06:27 +00002458 // We can't use either concat vectors or extract subvectors so fall back to
2459 // replacing the shuffle with extract and build vector.
2460 // to insert and build vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002461 EVT EltVT = VT.getVectorElementType();
2462 EVT PtrVT = TLI.getPointerTy();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002463 SmallVector<SDValue,8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002464 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002465 if (Mask[i] < 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002466 Ops.push_back(DAG.getUNDEF(EltVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002467 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00002468 int Idx = Mask[i];
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002469 SDValue Res;
2470
Nate Begeman5a5ca152009-04-29 05:20:52 +00002471 if (Idx < (int)SrcNumElts)
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002472 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2473 EltVT, Src1, DAG.getConstant(Idx, PtrVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002474 else
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002475 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2476 EltVT, Src2,
2477 DAG.getConstant(Idx - SrcNumElts, PtrVT));
2478
2479 Ops.push_back(Res);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002480 }
2481 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002482
Bill Wendling4533cac2010-01-28 21:51:40 +00002483 setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
2484 VT, &Ops[0], Ops.size()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002485}
2486
Dan Gohman46510a72010-04-15 01:51:59 +00002487void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002488 const Value *Op0 = I.getOperand(0);
2489 const Value *Op1 = I.getOperand(1);
2490 const Type *AggTy = I.getType();
2491 const Type *ValTy = Op1->getType();
2492 bool IntoUndef = isa<UndefValue>(Op0);
2493 bool FromUndef = isa<UndefValue>(Op1);
2494
2495 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2496 I.idx_begin(), I.idx_end());
2497
Owen Andersone50ed302009-08-10 22:56:29 +00002498 SmallVector<EVT, 4> AggValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002499 ComputeValueVTs(TLI, AggTy, AggValueVTs);
Owen Andersone50ed302009-08-10 22:56:29 +00002500 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002501 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2502
2503 unsigned NumAggValues = AggValueVTs.size();
2504 unsigned NumValValues = ValValueVTs.size();
2505 SmallVector<SDValue, 4> Values(NumAggValues);
2506
2507 SDValue Agg = getValue(Op0);
2508 SDValue Val = getValue(Op1);
2509 unsigned i = 0;
2510 // Copy the beginning value(s) from the original aggregate.
2511 for (; i != LinearIndex; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002512 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002513 SDValue(Agg.getNode(), Agg.getResNo() + i);
2514 // Copy values from the inserted value(s).
2515 for (; i != LinearIndex + NumValValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002516 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002517 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
2518 // Copy remaining value(s) from the original aggregate.
2519 for (; i != NumAggValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002520 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002521 SDValue(Agg.getNode(), Agg.getResNo() + i);
2522
Bill Wendling4533cac2010-01-28 21:51:40 +00002523 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2524 DAG.getVTList(&AggValueVTs[0], NumAggValues),
2525 &Values[0], NumAggValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002526}
2527
Dan Gohman46510a72010-04-15 01:51:59 +00002528void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002529 const Value *Op0 = I.getOperand(0);
2530 const Type *AggTy = Op0->getType();
2531 const Type *ValTy = I.getType();
2532 bool OutOfUndef = isa<UndefValue>(Op0);
2533
2534 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2535 I.idx_begin(), I.idx_end());
2536
Owen Andersone50ed302009-08-10 22:56:29 +00002537 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002538 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2539
2540 unsigned NumValValues = ValValueVTs.size();
2541 SmallVector<SDValue, 4> Values(NumValValues);
2542
2543 SDValue Agg = getValue(Op0);
2544 // Copy out the selected value(s).
2545 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
2546 Values[i - LinearIndex] =
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002547 OutOfUndef ?
Dale Johannesene8d72302009-02-06 23:05:02 +00002548 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002549 SDValue(Agg.getNode(), Agg.getResNo() + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002550
Bill Wendling4533cac2010-01-28 21:51:40 +00002551 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2552 DAG.getVTList(&ValValueVTs[0], NumValValues),
2553 &Values[0], NumValValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002554}
2555
Dan Gohman46510a72010-04-15 01:51:59 +00002556void SelectionDAGBuilder::visitGetElementPtr(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002557 SDValue N = getValue(I.getOperand(0));
2558 const Type *Ty = I.getOperand(0)->getType();
2559
Dan Gohman46510a72010-04-15 01:51:59 +00002560 for (GetElementPtrInst::const_op_iterator OI = I.op_begin()+1, E = I.op_end();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002561 OI != E; ++OI) {
Dan Gohman46510a72010-04-15 01:51:59 +00002562 const Value *Idx = *OI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002563 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
2564 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2565 if (Field) {
2566 // N = N + Offset
2567 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002568 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002569 DAG.getIntPtrConstant(Offset));
2570 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002571
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002572 Ty = StTy->getElementType(Field);
Chris Lattner93b122d2010-03-16 21:25:55 +00002573 } else if (const UnionType *UnTy = dyn_cast<UnionType>(Ty)) {
2574 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2575
2576 // Offset canonically 0 for unions, but type changes
2577 Ty = UnTy->getElementType(Field);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002578 } else {
2579 Ty = cast<SequentialType>(Ty)->getElementType();
2580
2581 // If this is a constant subscript, handle it quickly.
Dan Gohman46510a72010-04-15 01:51:59 +00002582 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002583 if (CI->getZExtValue() == 0) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002584 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +00002585 TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Evan Cheng65b52df2009-02-09 21:01:06 +00002586 SDValue OffsVal;
Owen Andersone50ed302009-08-10 22:56:29 +00002587 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002588 unsigned PtrBits = PTy.getSizeInBits();
Bill Wendlinge1a90422009-12-21 23:10:19 +00002589 if (PtrBits < 64)
Evan Cheng65b52df2009-02-09 21:01:06 +00002590 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2591 TLI.getPointerTy(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002592 DAG.getConstant(Offs, MVT::i64));
Bill Wendlinge1a90422009-12-21 23:10:19 +00002593 else
Evan Chengb1032a82009-02-09 20:54:38 +00002594 OffsVal = DAG.getIntPtrConstant(Offs);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002595
Dale Johannesen66978ee2009-01-31 02:22:37 +00002596 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Evan Chengb1032a82009-02-09 20:54:38 +00002597 OffsVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002598 continue;
2599 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002600
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002601 // N = N + Idx * ElementSize;
Dan Gohman7abbd042009-10-23 17:57:43 +00002602 APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(),
2603 TD->getTypeAllocSize(Ty));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002604 SDValue IdxN = getValue(Idx);
2605
2606 // If the index is smaller or larger than intptr_t, truncate or extend
2607 // it.
Duncan Sands3a66a682009-10-13 21:04:12 +00002608 IdxN = DAG.getSExtOrTrunc(IdxN, getCurDebugLoc(), N.getValueType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002609
2610 // If this is a multiply by a power of two, turn it into a shl
2611 // immediately. This is a very common case.
2612 if (ElementSize != 1) {
Dan Gohman7abbd042009-10-23 17:57:43 +00002613 if (ElementSize.isPowerOf2()) {
2614 unsigned Amt = ElementSize.logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00002615 IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002616 N.getValueType(), IdxN,
Duncan Sands92abc622009-01-31 15:50:11 +00002617 DAG.getConstant(Amt, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002618 } else {
Dan Gohman7abbd042009-10-23 17:57:43 +00002619 SDValue Scale = DAG.getConstant(ElementSize, TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00002620 IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002621 N.getValueType(), IdxN, Scale);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002622 }
2623 }
2624
Scott Michelfdc40a02009-02-17 22:15:04 +00002625 N = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002626 N.getValueType(), N, IdxN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002627 }
2628 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002629
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002630 setValue(&I, N);
2631}
2632
Dan Gohman46510a72010-04-15 01:51:59 +00002633void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002634 // If this is a fixed sized alloca in the entry block of the function,
2635 // allocate it statically on the stack.
2636 if (FuncInfo.StaticAllocaMap.count(&I))
2637 return; // getValue will auto-populate this.
2638
2639 const Type *Ty = I.getAllocatedType();
Duncan Sands777d2302009-05-09 07:06:46 +00002640 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002641 unsigned Align =
2642 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
2643 I.getAlignment());
2644
2645 SDValue AllocSize = getValue(I.getArraySize());
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002646
Chris Lattner0b18e592009-03-17 19:36:00 +00002647 AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), AllocSize.getValueType(),
2648 AllocSize,
2649 DAG.getConstant(TySize, AllocSize.getValueType()));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002650
Owen Andersone50ed302009-08-10 22:56:29 +00002651 EVT IntPtr = TLI.getPointerTy();
Duncan Sands3a66a682009-10-13 21:04:12 +00002652 AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002653
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002654 // Handle alignment. If the requested alignment is less than or equal to
2655 // the stack alignment, ignore it. If the size is greater than or equal to
2656 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
Dan Gohman55e59c12010-04-19 19:05:59 +00002657 unsigned StackAlign = TM.getFrameInfo()->getStackAlignment();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002658 if (Align <= StackAlign)
2659 Align = 0;
2660
2661 // Round the size of the allocation up to the stack alignment size
2662 // by add SA-1 to the size.
Scott Michelfdc40a02009-02-17 22:15:04 +00002663 AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002664 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002665 DAG.getIntPtrConstant(StackAlign-1));
Bill Wendling856ff412009-12-22 00:12:37 +00002666
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002667 // Mask out the low bits for alignment purposes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002668 AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002669 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002670 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
2671
2672 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Owen Anderson825b72b2009-08-11 20:47:22 +00002673 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
Scott Michelfdc40a02009-02-17 22:15:04 +00002674 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002675 VTs, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002676 setValue(&I, DSA);
2677 DAG.setRoot(DSA.getValue(1));
Bill Wendling856ff412009-12-22 00:12:37 +00002678
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002679 // Inform the Frame Information that we have just allocated a variable-sized
2680 // object.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002681 FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002682}
2683
Dan Gohman46510a72010-04-15 01:51:59 +00002684void SelectionDAGBuilder::visitLoad(const LoadInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002685 const Value *SV = I.getOperand(0);
2686 SDValue Ptr = getValue(SV);
2687
2688 const Type *Ty = I.getType();
David Greene1e559442010-02-15 17:00:31 +00002689
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002690 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00002691 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002692 unsigned Alignment = I.getAlignment();
2693
Owen Andersone50ed302009-08-10 22:56:29 +00002694 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002695 SmallVector<uint64_t, 4> Offsets;
2696 ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets);
2697 unsigned NumValues = ValueVTs.size();
2698 if (NumValues == 0)
2699 return;
2700
2701 SDValue Root;
2702 bool ConstantMemory = false;
2703 if (I.isVolatile())
2704 // Serialize volatile loads with other side effects.
2705 Root = getRoot();
2706 else if (AA->pointsToConstantMemory(SV)) {
2707 // Do not serialize (non-volatile) loads of constant memory with anything.
2708 Root = DAG.getEntryNode();
2709 ConstantMemory = true;
2710 } else {
2711 // Do not serialize non-volatile loads against each other.
2712 Root = DAG.getRoot();
2713 }
2714
2715 SmallVector<SDValue, 4> Values(NumValues);
2716 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002717 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002718 for (unsigned i = 0; i != NumValues; ++i) {
Bill Wendling856ff412009-12-22 00:12:37 +00002719 SDValue A = DAG.getNode(ISD::ADD, getCurDebugLoc(),
2720 PtrVT, Ptr,
2721 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00002722 SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root,
David Greene1e559442010-02-15 17:00:31 +00002723 A, SV, Offsets[i], isVolatile,
2724 isNonTemporal, Alignment);
Bill Wendling856ff412009-12-22 00:12:37 +00002725
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002726 Values[i] = L;
2727 Chains[i] = L.getValue(1);
2728 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002729
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002730 if (!ConstantMemory) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002731 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Bill Wendling856ff412009-12-22 00:12:37 +00002732 MVT::Other, &Chains[0], NumValues);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002733 if (isVolatile)
2734 DAG.setRoot(Chain);
2735 else
2736 PendingLoads.push_back(Chain);
2737 }
2738
Bill Wendling4533cac2010-01-28 21:51:40 +00002739 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2740 DAG.getVTList(&ValueVTs[0], NumValues),
2741 &Values[0], NumValues));
Bill Wendling856ff412009-12-22 00:12:37 +00002742}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002743
Dan Gohman46510a72010-04-15 01:51:59 +00002744void SelectionDAGBuilder::visitStore(const StoreInst &I) {
2745 const Value *SrcV = I.getOperand(0);
2746 const Value *PtrV = I.getOperand(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002747
Owen Andersone50ed302009-08-10 22:56:29 +00002748 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002749 SmallVector<uint64_t, 4> Offsets;
2750 ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets);
2751 unsigned NumValues = ValueVTs.size();
2752 if (NumValues == 0)
2753 return;
2754
2755 // Get the lowered operands. Note that we do this after
2756 // checking if NumResults is zero, because with zero results
2757 // the operands won't have values in the map.
2758 SDValue Src = getValue(SrcV);
2759 SDValue Ptr = getValue(PtrV);
2760
2761 SDValue Root = getRoot();
2762 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002763 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002764 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00002765 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002766 unsigned Alignment = I.getAlignment();
Bill Wendling856ff412009-12-22 00:12:37 +00002767
2768 for (unsigned i = 0; i != NumValues; ++i) {
2769 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, Ptr,
2770 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00002771 Chains[i] = DAG.getStore(Root, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002772 SDValue(Src.getNode(), Src.getResNo() + i),
David Greene1e559442010-02-15 17:00:31 +00002773 Add, PtrV, Offsets[i], isVolatile,
2774 isNonTemporal, Alignment);
Bill Wendling856ff412009-12-22 00:12:37 +00002775 }
2776
Bill Wendling4533cac2010-01-28 21:51:40 +00002777 DAG.setRoot(DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
2778 MVT::Other, &Chains[0], NumValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002779}
2780
2781/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
2782/// node.
Dan Gohman46510a72010-04-15 01:51:59 +00002783void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I,
Dan Gohman2048b852009-11-23 18:04:58 +00002784 unsigned Intrinsic) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002785 bool HasChain = !I.doesNotAccessMemory();
2786 bool OnlyLoad = HasChain && I.onlyReadsMemory();
2787
2788 // Build the operand list.
2789 SmallVector<SDValue, 8> Ops;
2790 if (HasChain) { // If this intrinsic has side-effects, chainify it.
2791 if (OnlyLoad) {
2792 // We don't need to serialize loads against other loads.
2793 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002794 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002795 Ops.push_back(getRoot());
2796 }
2797 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002798
2799 // Info is set by getTgtMemInstrinsic
2800 TargetLowering::IntrinsicInfo Info;
2801 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic);
2802
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002803 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002804 if (!IsTgtIntrinsic)
2805 Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002806
2807 // Add all operands of the call to the operand list.
Eric Christopher551754c2010-04-16 23:37:20 +00002808 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002809 SDValue Op = getValue(I.getOperand(i));
2810 assert(TLI.isTypeLegal(Op.getValueType()) &&
2811 "Intrinsic uses a non-legal type?");
2812 Ops.push_back(Op);
2813 }
2814
Owen Andersone50ed302009-08-10 22:56:29 +00002815 SmallVector<EVT, 4> ValueVTs;
Bob Wilson8d919552009-07-31 22:41:21 +00002816 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2817#ifndef NDEBUG
2818 for (unsigned Val = 0, E = ValueVTs.size(); Val != E; ++Val) {
2819 assert(TLI.isTypeLegal(ValueVTs[Val]) &&
2820 "Intrinsic uses a non-legal type?");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002821 }
Bob Wilson8d919552009-07-31 22:41:21 +00002822#endif // NDEBUG
Bill Wendling856ff412009-12-22 00:12:37 +00002823
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002824 if (HasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00002825 ValueVTs.push_back(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002826
Bob Wilson8d919552009-07-31 22:41:21 +00002827 SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002828
2829 // Create the node.
2830 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002831 if (IsTgtIntrinsic) {
2832 // This is target intrinsic that touches memory
Dale Johannesen66978ee2009-01-31 02:22:37 +00002833 Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002834 VTs, &Ops[0], Ops.size(),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002835 Info.memVT, Info.ptrVal, Info.offset,
2836 Info.align, Info.vol,
2837 Info.readMem, Info.writeMem);
Bill Wendling856ff412009-12-22 00:12:37 +00002838 } else if (!HasChain) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002839 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002840 VTs, &Ops[0], Ops.size());
Benjamin Kramerf0127052010-01-05 13:12:22 +00002841 } else if (!I.getType()->isVoidTy()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002842 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002843 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00002844 } else {
Scott Michelfdc40a02009-02-17 22:15:04 +00002845 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002846 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00002847 }
2848
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002849 if (HasChain) {
2850 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
2851 if (OnlyLoad)
2852 PendingLoads.push_back(Chain);
2853 else
2854 DAG.setRoot(Chain);
2855 }
Bill Wendling856ff412009-12-22 00:12:37 +00002856
Benjamin Kramerf0127052010-01-05 13:12:22 +00002857 if (!I.getType()->isVoidTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002858 if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Owen Andersone50ed302009-08-10 22:56:29 +00002859 EVT VT = TLI.getValueType(PTy);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002860 Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002861 }
Bill Wendling856ff412009-12-22 00:12:37 +00002862
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002863 setValue(&I, Result);
2864 }
2865}
2866
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002867/// GetSignificand - Get the significand and build it into a floating-point
2868/// number with exponent of 1:
2869///
2870/// Op = (Op & 0x007fffff) | 0x3f800000;
2871///
2872/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00002873static SDValue
Bill Wendling46ada192010-03-02 01:55:18 +00002874GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002875 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
2876 DAG.getConstant(0x007fffff, MVT::i32));
2877 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
2878 DAG.getConstant(0x3f800000, MVT::i32));
Bill Wendling4533cac2010-01-28 21:51:40 +00002879 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00002880}
2881
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002882/// GetExponent - Get the exponent:
2883///
Bill Wendlinge9a72862009-01-20 21:17:57 +00002884/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002885///
2886/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00002887static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00002888GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
Bill Wendling46ada192010-03-02 01:55:18 +00002889 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002890 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
2891 DAG.getConstant(0x7f800000, MVT::i32));
2892 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Duncan Sands92abc622009-01-31 15:50:11 +00002893 DAG.getConstant(23, TLI.getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00002894 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
2895 DAG.getConstant(127, MVT::i32));
Bill Wendling4533cac2010-01-28 21:51:40 +00002896 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00002897}
2898
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002899/// getF32Constant - Get 32-bit floating point constant.
2900static SDValue
2901getF32Constant(SelectionDAG &DAG, unsigned Flt) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002902 return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002903}
2904
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002905/// Inlined utility function to implement binary input atomic intrinsics for
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002906/// visitIntrinsicCall: I is a call instruction
2907/// Op is the associated NodeType for I
2908const char *
Dan Gohman46510a72010-04-15 01:51:59 +00002909SelectionDAGBuilder::implVisitBinaryAtomic(const CallInst& I,
2910 ISD::NodeType Op) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002911 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00002912 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00002913 DAG.getAtomic(Op, getCurDebugLoc(),
Eric Christopher551754c2010-04-16 23:37:20 +00002914 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00002915 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002916 getValue(I.getOperand(1)),
Eric Christopher551754c2010-04-16 23:37:20 +00002917 getValue(I.getOperand(2)),
2918 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002919 setValue(&I, L);
2920 DAG.setRoot(L.getValue(1));
2921 return 0;
2922}
2923
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00002924// implVisitAluOverflow - Lower arithmetic overflow instrinsics.
Bill Wendling74c37652008-12-09 22:08:41 +00002925const char *
Dan Gohman46510a72010-04-15 01:51:59 +00002926SelectionDAGBuilder::implVisitAluOverflow(const CallInst &I, ISD::NodeType Op) {
Eric Christopher551754c2010-04-16 23:37:20 +00002927 SDValue Op1 = getValue(I.getOperand(1));
2928 SDValue Op2 = getValue(I.getOperand(2));
Bill Wendling74c37652008-12-09 22:08:41 +00002929
Owen Anderson825b72b2009-08-11 20:47:22 +00002930 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
Bill Wendling4533cac2010-01-28 21:51:40 +00002931 setValue(&I, DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2));
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00002932 return 0;
2933}
Bill Wendling74c37652008-12-09 22:08:41 +00002934
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002935/// visitExp - Lower an exp intrinsic. Handles the special sequences for
2936/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00002937void
Dan Gohman46510a72010-04-15 01:51:59 +00002938SelectionDAGBuilder::visitExp(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00002939 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00002940 DebugLoc dl = getCurDebugLoc();
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002941
Eric Christopher551754c2010-04-16 23:37:20 +00002942 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002943 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00002944 SDValue Op = getValue(I.getOperand(1));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002945
2946 // Put the exponent in the right bit position for later addition to the
2947 // final result:
2948 //
2949 // #define LOG2OFe 1.4426950f
2950 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Owen Anderson825b72b2009-08-11 20:47:22 +00002951 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002952 getF32Constant(DAG, 0x3fb8aa3b));
Owen Anderson825b72b2009-08-11 20:47:22 +00002953 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002954
2955 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00002956 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
2957 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002958
2959 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00002960 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00002961 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendling856ff412009-12-22 00:12:37 +00002962
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002963 if (LimitFloatPrecision <= 6) {
2964 // For floating-point precision of 6:
2965 //
2966 // TwoToFractionalPartOfX =
2967 // 0.997535578f +
2968 // (0.735607626f + 0.252464424f * x) * x;
2969 //
2970 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00002971 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002972 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00002973 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002974 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00002975 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
2976 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002977 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00002978 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002979
2980 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00002981 SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002982 TwoToFracPartOfX, IntegerPartOfX);
2983
Owen Anderson825b72b2009-08-11 20:47:22 +00002984 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002985 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
2986 // For floating-point precision of 12:
2987 //
2988 // TwoToFractionalPartOfX =
2989 // 0.999892986f +
2990 // (0.696457318f +
2991 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
2992 //
2993 // 0.000107046256 error, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00002994 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002995 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00002996 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002997 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00002998 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
2999 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003000 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003001 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3002 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003003 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003004 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003005
3006 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003007 SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003008 TwoToFracPartOfX, IntegerPartOfX);
3009
Owen Anderson825b72b2009-08-11 20:47:22 +00003010 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t8);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003011 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3012 // For floating-point precision of 18:
3013 //
3014 // TwoToFractionalPartOfX =
3015 // 0.999999982f +
3016 // (0.693148872f +
3017 // (0.240227044f +
3018 // (0.554906021e-1f +
3019 // (0.961591928e-2f +
3020 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3021 //
3022 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003023 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003024 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003025 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003026 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003027 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3028 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003029 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003030 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3031 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003032 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003033 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3034 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003035 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003036 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3037 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003038 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003039 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3040 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003041 getF32Constant(DAG, 0x3f800000));
Scott Michelfdc40a02009-02-17 22:15:04 +00003042 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003043 MVT::i32, t13);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003044
3045 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003046 SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003047 TwoToFracPartOfX, IntegerPartOfX);
3048
Owen Anderson825b72b2009-08-11 20:47:22 +00003049 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003050 }
3051 } else {
3052 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003053 result = DAG.getNode(ISD::FEXP, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003054 getValue(I.getOperand(1)).getValueType(),
3055 getValue(I.getOperand(1)));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003056 }
3057
Dale Johannesen59e577f2008-09-05 18:38:42 +00003058 setValue(&I, result);
3059}
3060
Bill Wendling39150252008-09-09 20:39:27 +00003061/// visitLog - Lower a log intrinsic. Handles the special sequences for
3062/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003063void
Dan Gohman46510a72010-04-15 01:51:59 +00003064SelectionDAGBuilder::visitLog(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003065 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003066 DebugLoc dl = getCurDebugLoc();
Bill Wendling39150252008-09-09 20:39:27 +00003067
Eric Christopher551754c2010-04-16 23:37:20 +00003068 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling39150252008-09-09 20:39:27 +00003069 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003070 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003071 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00003072
3073 // Scale the exponent by log(2) [0.69314718f].
Bill Wendling46ada192010-03-02 01:55:18 +00003074 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003075 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003076 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003077
3078 // Get the significand and build it into a floating-point number with
3079 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003080 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling39150252008-09-09 20:39:27 +00003081
3082 if (LimitFloatPrecision <= 6) {
3083 // For floating-point precision of 6:
3084 //
3085 // LogofMantissa =
3086 // -1.1609546f +
3087 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003088 //
Bill Wendling39150252008-09-09 20:39:27 +00003089 // error 0.0034276066, which is better than 8 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003090 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003091 getF32Constant(DAG, 0xbe74c456));
Owen Anderson825b72b2009-08-11 20:47:22 +00003092 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003093 getF32Constant(DAG, 0x3fb3a2b1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003094 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3095 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003096 getF32Constant(DAG, 0x3f949a29));
Bill Wendling39150252008-09-09 20:39:27 +00003097
Scott Michelfdc40a02009-02-17 22:15:04 +00003098 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003099 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003100 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3101 // For floating-point precision of 12:
3102 //
3103 // LogOfMantissa =
3104 // -1.7417939f +
3105 // (2.8212026f +
3106 // (-1.4699568f +
3107 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3108 //
3109 // error 0.000061011436, which is 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003110 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003111 getF32Constant(DAG, 0xbd67b6d6));
Owen Anderson825b72b2009-08-11 20:47:22 +00003112 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003113 getF32Constant(DAG, 0x3ee4f4b8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003114 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3115 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003116 getF32Constant(DAG, 0x3fbc278b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003117 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3118 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003119 getF32Constant(DAG, 0x40348e95));
Owen Anderson825b72b2009-08-11 20:47:22 +00003120 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3121 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003122 getF32Constant(DAG, 0x3fdef31a));
Bill Wendling39150252008-09-09 20:39:27 +00003123
Scott Michelfdc40a02009-02-17 22:15:04 +00003124 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003125 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003126 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3127 // For floating-point precision of 18:
3128 //
3129 // LogOfMantissa =
3130 // -2.1072184f +
3131 // (4.2372794f +
3132 // (-3.7029485f +
3133 // (2.2781945f +
3134 // (-0.87823314f +
3135 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3136 //
3137 // error 0.0000023660568, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003138 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003139 getF32Constant(DAG, 0xbc91e5ac));
Owen Anderson825b72b2009-08-11 20:47:22 +00003140 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003141 getF32Constant(DAG, 0x3e4350aa));
Owen Anderson825b72b2009-08-11 20:47:22 +00003142 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3143 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003144 getF32Constant(DAG, 0x3f60d3e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003145 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3146 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003147 getF32Constant(DAG, 0x4011cdf0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003148 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3149 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003150 getF32Constant(DAG, 0x406cfd1c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003151 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3152 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003153 getF32Constant(DAG, 0x408797cb));
Owen Anderson825b72b2009-08-11 20:47:22 +00003154 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3155 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003156 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003157
Scott Michelfdc40a02009-02-17 22:15:04 +00003158 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003159 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003160 }
3161 } else {
3162 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003163 result = DAG.getNode(ISD::FLOG, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003164 getValue(I.getOperand(1)).getValueType(),
3165 getValue(I.getOperand(1)));
Bill Wendling39150252008-09-09 20:39:27 +00003166 }
3167
Dale Johannesen59e577f2008-09-05 18:38:42 +00003168 setValue(&I, result);
3169}
3170
Bill Wendling3eb59402008-09-09 00:28:24 +00003171/// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for
3172/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003173void
Dan Gohman46510a72010-04-15 01:51:59 +00003174SelectionDAGBuilder::visitLog2(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003175 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003176 DebugLoc dl = getCurDebugLoc();
Bill Wendling3eb59402008-09-09 00:28:24 +00003177
Eric Christopher551754c2010-04-16 23:37:20 +00003178 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003179 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003180 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003181 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003182
Bill Wendling39150252008-09-09 20:39:27 +00003183 // Get the exponent.
Bill Wendling46ada192010-03-02 01:55:18 +00003184 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
Bill Wendling856ff412009-12-22 00:12:37 +00003185
Bill Wendling3eb59402008-09-09 00:28:24 +00003186 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003187 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003188 SDValue X = GetSignificand(DAG, Op1, dl);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003189
Bill Wendling3eb59402008-09-09 00:28:24 +00003190 // Different possible minimax approximations of significand in
3191 // floating-point for various degrees of accuracy over [1,2].
3192 if (LimitFloatPrecision <= 6) {
3193 // For floating-point precision of 6:
3194 //
3195 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
3196 //
3197 // error 0.0049451742, which is more than 7 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003198 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003199 getF32Constant(DAG, 0xbeb08fe0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003200 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003201 getF32Constant(DAG, 0x40019463));
Owen Anderson825b72b2009-08-11 20:47:22 +00003202 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3203 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003204 getF32Constant(DAG, 0x3fd6633d));
Bill Wendling3eb59402008-09-09 00:28:24 +00003205
Scott Michelfdc40a02009-02-17 22:15:04 +00003206 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003207 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003208 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3209 // For floating-point precision of 12:
3210 //
3211 // Log2ofMantissa =
3212 // -2.51285454f +
3213 // (4.07009056f +
3214 // (-2.12067489f +
3215 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003216 //
Bill Wendling3eb59402008-09-09 00:28:24 +00003217 // error 0.0000876136000, which is better than 13 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003218 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003219 getF32Constant(DAG, 0xbda7262e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003220 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003221 getF32Constant(DAG, 0x3f25280b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003222 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3223 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003224 getF32Constant(DAG, 0x4007b923));
Owen Anderson825b72b2009-08-11 20:47:22 +00003225 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3226 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003227 getF32Constant(DAG, 0x40823e2f));
Owen Anderson825b72b2009-08-11 20:47:22 +00003228 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3229 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003230 getF32Constant(DAG, 0x4020d29c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003231
Scott Michelfdc40a02009-02-17 22:15:04 +00003232 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003233 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003234 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3235 // For floating-point precision of 18:
3236 //
3237 // Log2ofMantissa =
3238 // -3.0400495f +
3239 // (6.1129976f +
3240 // (-5.3420409f +
3241 // (3.2865683f +
3242 // (-1.2669343f +
3243 // (0.27515199f -
3244 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
3245 //
3246 // error 0.0000018516, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003247 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003248 getF32Constant(DAG, 0xbcd2769e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003249 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003250 getF32Constant(DAG, 0x3e8ce0b9));
Owen Anderson825b72b2009-08-11 20:47:22 +00003251 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3252 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003253 getF32Constant(DAG, 0x3fa22ae7));
Owen Anderson825b72b2009-08-11 20:47:22 +00003254 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3255 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003256 getF32Constant(DAG, 0x40525723));
Owen Anderson825b72b2009-08-11 20:47:22 +00003257 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3258 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003259 getF32Constant(DAG, 0x40aaf200));
Owen Anderson825b72b2009-08-11 20:47:22 +00003260 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3261 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003262 getF32Constant(DAG, 0x40c39dad));
Owen Anderson825b72b2009-08-11 20:47:22 +00003263 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3264 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003265 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003266
Scott Michelfdc40a02009-02-17 22:15:04 +00003267 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003268 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003269 }
Dale Johannesen853244f2008-09-05 23:49:37 +00003270 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003271 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003272 result = DAG.getNode(ISD::FLOG2, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003273 getValue(I.getOperand(1)).getValueType(),
3274 getValue(I.getOperand(1)));
Dale Johannesen853244f2008-09-05 23:49:37 +00003275 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003276
Dale Johannesen59e577f2008-09-05 18:38:42 +00003277 setValue(&I, result);
3278}
3279
Bill Wendling3eb59402008-09-09 00:28:24 +00003280/// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for
3281/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003282void
Dan Gohman46510a72010-04-15 01:51:59 +00003283SelectionDAGBuilder::visitLog10(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003284 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003285 DebugLoc dl = getCurDebugLoc();
Bill Wendling181b6272008-10-19 20:34:04 +00003286
Eric Christopher551754c2010-04-16 23:37:20 +00003287 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003288 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003289 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003290 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003291
Bill Wendling39150252008-09-09 20:39:27 +00003292 // Scale the exponent by log10(2) [0.30102999f].
Bill Wendling46ada192010-03-02 01:55:18 +00003293 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003294 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003295 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00003296
3297 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003298 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003299 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling3eb59402008-09-09 00:28:24 +00003300
3301 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003302 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003303 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003304 // Log10ofMantissa =
3305 // -0.50419619f +
3306 // (0.60948995f - 0.10380950f * x) * x;
3307 //
3308 // error 0.0014886165, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003309 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003310 getF32Constant(DAG, 0xbdd49a13));
Owen Anderson825b72b2009-08-11 20:47:22 +00003311 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003312 getF32Constant(DAG, 0x3f1c0789));
Owen Anderson825b72b2009-08-11 20:47:22 +00003313 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3314 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003315 getF32Constant(DAG, 0x3f011300));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003316
Scott Michelfdc40a02009-02-17 22:15:04 +00003317 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003318 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003319 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3320 // For floating-point precision of 12:
3321 //
3322 // Log10ofMantissa =
3323 // -0.64831180f +
3324 // (0.91751397f +
3325 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
3326 //
3327 // error 0.00019228036, which is better than 12 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003328 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003329 getF32Constant(DAG, 0x3d431f31));
Owen Anderson825b72b2009-08-11 20:47:22 +00003330 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003331 getF32Constant(DAG, 0x3ea21fb2));
Owen Anderson825b72b2009-08-11 20:47:22 +00003332 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3333 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003334 getF32Constant(DAG, 0x3f6ae232));
Owen Anderson825b72b2009-08-11 20:47:22 +00003335 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3336 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003337 getF32Constant(DAG, 0x3f25f7c3));
Bill Wendling3eb59402008-09-09 00:28:24 +00003338
Scott Michelfdc40a02009-02-17 22:15:04 +00003339 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003340 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003341 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003342 // For floating-point precision of 18:
3343 //
3344 // Log10ofMantissa =
3345 // -0.84299375f +
3346 // (1.5327582f +
3347 // (-1.0688956f +
3348 // (0.49102474f +
3349 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
3350 //
3351 // error 0.0000037995730, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003352 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003353 getF32Constant(DAG, 0x3c5d51ce));
Owen Anderson825b72b2009-08-11 20:47:22 +00003354 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003355 getF32Constant(DAG, 0x3e00685a));
Owen Anderson825b72b2009-08-11 20:47:22 +00003356 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3357 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003358 getF32Constant(DAG, 0x3efb6798));
Owen Anderson825b72b2009-08-11 20:47:22 +00003359 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3360 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003361 getF32Constant(DAG, 0x3f88d192));
Owen Anderson825b72b2009-08-11 20:47:22 +00003362 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3363 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003364 getF32Constant(DAG, 0x3fc4316c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003365 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3366 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003367 getF32Constant(DAG, 0x3f57ce70));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003368
Scott Michelfdc40a02009-02-17 22:15:04 +00003369 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003370 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003371 }
Dale Johannesen852680a2008-09-05 21:27:19 +00003372 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003373 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003374 result = DAG.getNode(ISD::FLOG10, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003375 getValue(I.getOperand(1)).getValueType(),
3376 getValue(I.getOperand(1)));
Dale Johannesen852680a2008-09-05 21:27:19 +00003377 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003378
Dale Johannesen59e577f2008-09-05 18:38:42 +00003379 setValue(&I, result);
3380}
3381
Bill Wendlinge10c8142008-09-09 22:39:21 +00003382/// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for
3383/// limited-precision mode.
Dale Johannesen601d3c02008-09-05 01:48:15 +00003384void
Dan Gohman46510a72010-04-15 01:51:59 +00003385SelectionDAGBuilder::visitExp2(const CallInst &I) {
Dale Johannesen601d3c02008-09-05 01:48:15 +00003386 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003387 DebugLoc dl = getCurDebugLoc();
Bill Wendlinge10c8142008-09-09 22:39:21 +00003388
Eric Christopher551754c2010-04-16 23:37:20 +00003389 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00003390 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003391 SDValue Op = getValue(I.getOperand(1));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003392
Owen Anderson825b72b2009-08-11 20:47:22 +00003393 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003394
3395 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003396 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3397 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003398
3399 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003400 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003401 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003402
3403 if (LimitFloatPrecision <= 6) {
3404 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003405 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00003406 // TwoToFractionalPartOfX =
3407 // 0.997535578f +
3408 // (0.735607626f + 0.252464424f * x) * x;
3409 //
3410 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003411 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003412 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003413 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003414 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003415 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3416 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003417 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003418 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003419 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003420 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003421
Scott Michelfdc40a02009-02-17 22:15:04 +00003422 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003423 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003424 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3425 // For floating-point precision of 12:
3426 //
3427 // TwoToFractionalPartOfX =
3428 // 0.999892986f +
3429 // (0.696457318f +
3430 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3431 //
3432 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003433 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003434 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003435 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003436 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003437 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3438 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003439 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003440 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3441 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003442 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003443 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003444 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003445 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003446
Scott Michelfdc40a02009-02-17 22:15:04 +00003447 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003448 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003449 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3450 // For floating-point precision of 18:
3451 //
3452 // TwoToFractionalPartOfX =
3453 // 0.999999982f +
3454 // (0.693148872f +
3455 // (0.240227044f +
3456 // (0.554906021e-1f +
3457 // (0.961591928e-2f +
3458 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3459 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003460 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003461 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003462 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003463 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003464 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3465 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003466 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003467 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3468 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003469 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003470 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3471 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003472 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003473 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3474 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003475 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003476 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3477 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003478 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003479 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003480 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003481 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003482
Scott Michelfdc40a02009-02-17 22:15:04 +00003483 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003484 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003485 }
Dale Johannesen601d3c02008-09-05 01:48:15 +00003486 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003487 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003488 result = DAG.getNode(ISD::FEXP2, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003489 getValue(I.getOperand(1)).getValueType(),
3490 getValue(I.getOperand(1)));
Dale Johannesen601d3c02008-09-05 01:48:15 +00003491 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003492
Dale Johannesen601d3c02008-09-05 01:48:15 +00003493 setValue(&I, result);
3494}
3495
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003496/// visitPow - Lower a pow intrinsic. Handles the special sequences for
3497/// limited-precision mode with x == 10.0f.
3498void
Dan Gohman46510a72010-04-15 01:51:59 +00003499SelectionDAGBuilder::visitPow(const CallInst &I) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003500 SDValue result;
Eric Christopher551754c2010-04-16 23:37:20 +00003501 const Value *Val = I.getOperand(1);
Dale Johannesen66978ee2009-01-31 02:22:37 +00003502 DebugLoc dl = getCurDebugLoc();
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003503 bool IsExp10 = false;
3504
Owen Anderson825b72b2009-08-11 20:47:22 +00003505 if (getValue(Val).getValueType() == MVT::f32 &&
Eric Christopher551754c2010-04-16 23:37:20 +00003506 getValue(I.getOperand(2)).getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003507 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3508 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) {
3509 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
3510 APFloat Ten(10.0f);
3511 IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten);
3512 }
3513 }
3514 }
3515
3516 if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003517 SDValue Op = getValue(I.getOperand(2));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003518
3519 // Put the exponent in the right bit position for later addition to the
3520 // final result:
3521 //
3522 // #define LOG2OF10 3.3219281f
3523 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Owen Anderson825b72b2009-08-11 20:47:22 +00003524 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003525 getF32Constant(DAG, 0x40549a78));
Owen Anderson825b72b2009-08-11 20:47:22 +00003526 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003527
3528 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003529 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3530 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003531
3532 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003533 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003534 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003535
3536 if (LimitFloatPrecision <= 6) {
3537 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003538 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003539 // twoToFractionalPartOfX =
3540 // 0.997535578f +
3541 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003542 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003543 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003544 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003545 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003546 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003547 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003548 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3549 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003550 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003551 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003552 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003553 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003554
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003555 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003556 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003557 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3558 // For floating-point precision of 12:
3559 //
3560 // TwoToFractionalPartOfX =
3561 // 0.999892986f +
3562 // (0.696457318f +
3563 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3564 //
3565 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003566 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003567 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003568 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003569 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003570 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3571 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003572 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003573 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3574 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003575 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003576 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003577 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003578 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003579
Scott Michelfdc40a02009-02-17 22:15:04 +00003580 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003581 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003582 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3583 // For floating-point precision of 18:
3584 //
3585 // TwoToFractionalPartOfX =
3586 // 0.999999982f +
3587 // (0.693148872f +
3588 // (0.240227044f +
3589 // (0.554906021e-1f +
3590 // (0.961591928e-2f +
3591 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3592 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003593 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003594 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003595 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003596 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003597 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3598 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003599 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003600 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3601 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003602 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003603 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3604 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003605 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003606 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3607 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003608 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003609 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3610 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003611 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003612 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003613 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003614 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003615
Scott Michelfdc40a02009-02-17 22:15:04 +00003616 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003617 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003618 }
3619 } else {
3620 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003621 result = DAG.getNode(ISD::FPOW, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003622 getValue(I.getOperand(1)).getValueType(),
3623 getValue(I.getOperand(1)),
3624 getValue(I.getOperand(2)));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003625 }
3626
3627 setValue(&I, result);
3628}
3629
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003630
3631/// ExpandPowI - Expand a llvm.powi intrinsic.
3632static SDValue ExpandPowI(DebugLoc DL, SDValue LHS, SDValue RHS,
3633 SelectionDAG &DAG) {
3634 // If RHS is a constant, we can expand this out to a multiplication tree,
3635 // otherwise we end up lowering to a call to __powidf2 (for example). When
3636 // optimizing for size, we only want to do this if the expansion would produce
3637 // a small number of multiplies, otherwise we do the full expansion.
3638 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3639 // Get the exponent as a positive value.
3640 unsigned Val = RHSC->getSExtValue();
3641 if ((int)Val < 0) Val = -Val;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003642
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003643 // powi(x, 0) -> 1.0
3644 if (Val == 0)
3645 return DAG.getConstantFP(1.0, LHS.getValueType());
3646
Dan Gohmanae541aa2010-04-15 04:33:49 +00003647 const Function *F = DAG.getMachineFunction().getFunction();
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003648 if (!F->hasFnAttr(Attribute::OptimizeForSize) ||
3649 // If optimizing for size, don't insert too many multiplies. This
3650 // inserts up to 5 multiplies.
3651 CountPopulation_32(Val)+Log2_32(Val) < 7) {
3652 // We use the simple binary decomposition method to generate the multiply
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003653 // sequence. There are more optimal ways to do this (for example,
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003654 // powi(x,15) generates one more multiply than it should), but this has
3655 // the benefit of being both really simple and much better than a libcall.
3656 SDValue Res; // Logically starts equal to 1.0
3657 SDValue CurSquare = LHS;
3658 while (Val) {
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00003659 if (Val & 1) {
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003660 if (Res.getNode())
3661 Res = DAG.getNode(ISD::FMUL, DL,Res.getValueType(), Res, CurSquare);
3662 else
3663 Res = CurSquare; // 1.0*CurSquare.
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00003664 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003665
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003666 CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
3667 CurSquare, CurSquare);
3668 Val >>= 1;
3669 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003670
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003671 // If the original was negative, invert the result, producing 1/(x*x*x).
3672 if (RHSC->getSExtValue() < 0)
3673 Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(),
3674 DAG.getConstantFP(1.0, LHS.getValueType()), Res);
3675 return Res;
3676 }
3677 }
3678
3679 // Otherwise, expand to a libcall.
3680 return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS);
3681}
3682
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003683/// EmitFuncArgumentDbgValue - If the DbgValueInst is a dbg_value of a function
3684/// argument, create the corresponding DBG_VALUE machine instruction for it now.
3685/// At the end of instruction selection, they will be inserted to the entry BB.
Evan Cheng9e8a2b92010-04-29 01:40:30 +00003686bool
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003687SelectionDAGBuilder::EmitFuncArgumentDbgValue(const DbgValueInst &DI,
3688 const Value *V, MDNode *Variable,
3689 uint64_t Offset, SDValue &N) {
3690 if (!isa<Argument>(V))
Evan Cheng9e8a2b92010-04-29 01:40:30 +00003691 return false;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003692
Devang Patel719f6a92010-04-29 20:40:36 +00003693 MachineFunction &MF = DAG.getMachineFunction();
Devang Patela83ce982010-04-29 18:50:36 +00003694 // Ignore inlined function arguments here.
3695 DIVariable DV(Variable);
Devang Patel719f6a92010-04-29 20:40:36 +00003696 if (DV.isInlinedFnArgument(MF.getFunction()))
Devang Patela83ce982010-04-29 18:50:36 +00003697 return false;
3698
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003699 MachineBasicBlock *MBB = FuncInfo.MBBMap[DI.getParent()];
3700 if (MBB != &MF.front())
Evan Cheng9e8a2b92010-04-29 01:40:30 +00003701 return false;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003702
3703 unsigned Reg = 0;
3704 if (N.getOpcode() == ISD::CopyFromReg) {
3705 Reg = cast<RegisterSDNode>(N.getOperand(1))->getReg();
Evan Cheng1deef272010-04-29 00:59:34 +00003706 if (Reg && TargetRegisterInfo::isVirtualRegister(Reg)) {
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003707 MachineRegisterInfo &RegInfo = MF.getRegInfo();
3708 unsigned PR = RegInfo.getLiveInPhysReg(Reg);
3709 if (PR)
3710 Reg = PR;
3711 }
3712 }
3713
Evan Chenga36acad2010-04-29 06:33:38 +00003714 if (!Reg) {
3715 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
3716 if (VMI == FuncInfo.ValueMap.end())
3717 return false;
3718 Reg = VMI->second;
3719 }
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003720
3721 const TargetInstrInfo *TII = DAG.getTarget().getInstrInfo();
3722 MachineInstrBuilder MIB = BuildMI(MF, getCurDebugLoc(),
3723 TII->get(TargetOpcode::DBG_VALUE))
Evan Chenga36acad2010-04-29 06:33:38 +00003724 .addReg(Reg, RegState::Debug).addImm(Offset).addMetadata(Variable);
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003725 FuncInfo.ArgDbgValues.push_back(&*MIB);
Evan Cheng9e8a2b92010-04-29 01:40:30 +00003726 return true;
Evan Cheng2ad0fcf2010-04-28 23:08:54 +00003727}
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003728
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003729/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
3730/// we want to emit this as a call to a named external function, return the name
3731/// otherwise lower it and return null.
3732const char *
Dan Gohman46510a72010-04-15 01:51:59 +00003733SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, unsigned Intrinsic) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00003734 DebugLoc dl = getCurDebugLoc();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003735 SDValue Res;
3736
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003737 switch (Intrinsic) {
3738 default:
3739 // By default, turn this into a target intrinsic node.
3740 visitTargetIntrinsic(I, Intrinsic);
3741 return 0;
3742 case Intrinsic::vastart: visitVAStart(I); return 0;
3743 case Intrinsic::vaend: visitVAEnd(I); return 0;
3744 case Intrinsic::vacopy: visitVACopy(I); return 0;
3745 case Intrinsic::returnaddress:
Bill Wendling4533cac2010-01-28 21:51:40 +00003746 setValue(&I, DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(),
Eric Christopher551754c2010-04-16 23:37:20 +00003747 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003748 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00003749 case Intrinsic::frameaddress:
Bill Wendling4533cac2010-01-28 21:51:40 +00003750 setValue(&I, DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(),
Eric Christopher551754c2010-04-16 23:37:20 +00003751 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003752 return 0;
3753 case Intrinsic::setjmp:
3754 return "_setjmp"+!TLI.usesUnderscoreSetJmp();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003755 case Intrinsic::longjmp:
3756 return "_longjmp"+!TLI.usesUnderscoreLongJmp();
Chris Lattner824b9582008-11-21 16:42:48 +00003757 case Intrinsic::memcpy: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003758 // Assert for address < 256 since we support only user defined address
3759 // spaces.
Eric Christopher551754c2010-04-16 23:37:20 +00003760 assert(cast<PointerType>(I.getOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003761 < 256 &&
Eric Christopher551754c2010-04-16 23:37:20 +00003762 cast<PointerType>(I.getOperand(2)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003763 < 256 &&
3764 "Unknown address space");
Eric Christopher551754c2010-04-16 23:37:20 +00003765 SDValue Op1 = getValue(I.getOperand(1));
3766 SDValue Op2 = getValue(I.getOperand(2));
3767 SDValue Op3 = getValue(I.getOperand(3));
3768 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3769 bool isVol = cast<ConstantInt>(I.getOperand(5))->getZExtValue();
Mon P Wang20adc9d2010-04-04 03:10:48 +00003770 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, isVol, false,
Eric Christopher551754c2010-04-16 23:37:20 +00003771 I.getOperand(1), 0, I.getOperand(2), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003772 return 0;
3773 }
Chris Lattner824b9582008-11-21 16:42:48 +00003774 case Intrinsic::memset: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003775 // Assert for address < 256 since we support only user defined address
3776 // spaces.
Eric Christopher551754c2010-04-16 23:37:20 +00003777 assert(cast<PointerType>(I.getOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003778 < 256 &&
3779 "Unknown address space");
Eric Christopher551754c2010-04-16 23:37:20 +00003780 SDValue Op1 = getValue(I.getOperand(1));
3781 SDValue Op2 = getValue(I.getOperand(2));
3782 SDValue Op3 = getValue(I.getOperand(3));
3783 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3784 bool isVol = cast<ConstantInt>(I.getOperand(5))->getZExtValue();
Mon P Wang20adc9d2010-04-04 03:10:48 +00003785 DAG.setRoot(DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
Eric Christopher551754c2010-04-16 23:37:20 +00003786 I.getOperand(1), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003787 return 0;
3788 }
Chris Lattner824b9582008-11-21 16:42:48 +00003789 case Intrinsic::memmove: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003790 // Assert for address < 256 since we support only user defined address
3791 // spaces.
Eric Christopher551754c2010-04-16 23:37:20 +00003792 assert(cast<PointerType>(I.getOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003793 < 256 &&
Eric Christopher551754c2010-04-16 23:37:20 +00003794 cast<PointerType>(I.getOperand(2)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003795 < 256 &&
3796 "Unknown address space");
Eric Christopher551754c2010-04-16 23:37:20 +00003797 SDValue Op1 = getValue(I.getOperand(1));
3798 SDValue Op2 = getValue(I.getOperand(2));
3799 SDValue Op3 = getValue(I.getOperand(3));
3800 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3801 bool isVol = cast<ConstantInt>(I.getOperand(5))->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003802
3803 // If the source and destination are known to not be aliases, we can
3804 // lower memmove as memcpy.
3805 uint64_t Size = -1ULL;
3806 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003807 Size = C->getZExtValue();
Eric Christopher551754c2010-04-16 23:37:20 +00003808 if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) ==
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003809 AliasAnalysis::NoAlias) {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003810 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
Eric Christopher551754c2010-04-16 23:37:20 +00003811 false, I.getOperand(1), 0, I.getOperand(2), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003812 return 0;
3813 }
3814
Mon P Wang20adc9d2010-04-04 03:10:48 +00003815 DAG.setRoot(DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
Eric Christopher551754c2010-04-16 23:37:20 +00003816 I.getOperand(1), 0, I.getOperand(2), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003817 return 0;
3818 }
Bill Wendling92c1e122009-02-13 02:16:35 +00003819 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00003820 const DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
Chris Lattnerbf0ca2b2009-12-29 09:32:19 +00003821 if (!DIDescriptor::ValidDebugInfo(DI.getVariable(), CodeGenOpt::None))
Devang Patel7e1e31f2009-07-02 22:43:26 +00003822 return 0;
3823
Devang Patelac1ceb32009-10-09 22:42:28 +00003824 MDNode *Variable = DI.getVariable();
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00003825 // Parameters are handled specially.
Devang Patelf38c6c82010-04-28 23:24:13 +00003826 bool isParameter =
3827 DIVariable(Variable).getTag() == dwarf::DW_TAG_arg_variable;
Dan Gohman46510a72010-04-15 01:51:59 +00003828 const Value *Address = DI.getAddress();
Dale Johannesen8ac38f22010-02-08 21:53:27 +00003829 if (!Address)
3830 return 0;
Dan Gohman46510a72010-04-15 01:51:59 +00003831 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
Devang Patel24f20e02009-08-22 17:12:53 +00003832 Address = BCI->getOperand(0);
Dan Gohman46510a72010-04-15 01:51:59 +00003833 const AllocaInst *AI = dyn_cast<AllocaInst>(Address);
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00003834 if (AI) {
3835 // Don't handle byval arguments or VLAs, for example.
3836 // Non-byval arguments are handled here (they refer to the stack temporary
3837 // alloca at this point).
3838 DenseMap<const AllocaInst*, int>::iterator SI =
3839 FuncInfo.StaticAllocaMap.find(AI);
3840 if (SI == FuncInfo.StaticAllocaMap.end())
3841 return 0; // VLAs.
3842 int FI = SI->second;
Devang Patel70d75ca2009-11-12 19:02:56 +00003843
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00003844 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
3845 if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo())
3846 MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc());
3847 }
3848
3849 // Build an entry in DbgOrdering. Debug info input nodes get an SDNodeOrder
3850 // but do not always have a corresponding SDNode built. The SDNodeOrder
3851 // absolute, but not relative, values are different depending on whether
3852 // debug info exists.
3853 ++SDNodeOrder;
3854 SDValue &N = NodeMap[Address];
3855 SDDbgValue *SDV;
3856 if (N.getNode()) {
3857 if (isParameter && !AI) {
3858 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N.getNode());
3859 if (FINode)
3860 // Byval parameter. We have a frame index at this point.
3861 SDV = DAG.getDbgValue(Variable, FINode->getIndex(),
3862 0, dl, SDNodeOrder);
3863 else
3864 // Can't do anything with other non-AI cases yet. This might be a
3865 // parameter of a callee function that got inlined, for example.
3866 return 0;
3867 } else if (AI)
3868 SDV = DAG.getDbgValue(Variable, N.getNode(), N.getResNo(),
3869 0, dl, SDNodeOrder);
3870 else
3871 // Can't do anything with other non-AI cases yet.
3872 return 0;
3873 DAG.AddDbgValue(SDV, N.getNode(), isParameter);
3874 } else {
3875 // This isn't useful, but it shows what we're missing.
3876 SDV = DAG.getDbgValue(Variable, UndefValue::get(Address->getType()),
3877 0, dl, SDNodeOrder);
3878 DAG.AddDbgValue(SDV, 0, isParameter);
3879 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003880 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00003881 }
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003882 case Intrinsic::dbg_value: {
Dan Gohman46510a72010-04-15 01:51:59 +00003883 const DbgValueInst &DI = cast<DbgValueInst>(I);
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003884 if (!DIDescriptor::ValidDebugInfo(DI.getVariable(), CodeGenOpt::None))
3885 return 0;
3886
3887 MDNode *Variable = DI.getVariable();
Devang Patel00190342010-03-15 19:15:44 +00003888 uint64_t Offset = DI.getOffset();
Dan Gohman46510a72010-04-15 01:51:59 +00003889 const Value *V = DI.getValue();
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003890 if (!V)
3891 return 0;
Devang Patel00190342010-03-15 19:15:44 +00003892
3893 // Build an entry in DbgOrdering. Debug info input nodes get an SDNodeOrder
3894 // but do not always have a corresponding SDNode built. The SDNodeOrder
3895 // absolute, but not relative, values are different depending on whether
3896 // debug info exists.
3897 ++SDNodeOrder;
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00003898 SDDbgValue *SDV;
Devang Patel00190342010-03-15 19:15:44 +00003899 if (isa<ConstantInt>(V) || isa<ConstantFP>(V)) {
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00003900 SDV = DAG.getDbgValue(Variable, V, Offset, dl, SDNodeOrder);
3901 DAG.AddDbgValue(SDV, 0, false);
Devang Patel00190342010-03-15 19:15:44 +00003902 } else {
3903 SDValue &N = NodeMap[V];
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00003904 if (N.getNode()) {
Evan Cheng9e8a2b92010-04-29 01:40:30 +00003905 if (!EmitFuncArgumentDbgValue(DI, V, Variable, Offset, N)) {
3906 SDV = DAG.getDbgValue(Variable, N.getNode(),
3907 N.getResNo(), Offset, dl, SDNodeOrder);
3908 DAG.AddDbgValue(SDV, N.getNode(), false);
3909 }
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00003910 } else {
Devang Patel00190342010-03-15 19:15:44 +00003911 // We may expand this to cover more cases. One case where we have no
3912 // data available is an unreferenced parameter; we need this fallback.
Dale Johannesenfdb42fa2010-04-26 20:06:49 +00003913 SDV = DAG.getDbgValue(Variable, UndefValue::get(V->getType()),
3914 Offset, dl, SDNodeOrder);
3915 DAG.AddDbgValue(SDV, 0, false);
3916 }
Devang Patel00190342010-03-15 19:15:44 +00003917 }
3918
3919 // Build a debug info table entry.
Dan Gohman46510a72010-04-15 01:51:59 +00003920 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(V))
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003921 V = BCI->getOperand(0);
Dan Gohman46510a72010-04-15 01:51:59 +00003922 const AllocaInst *AI = dyn_cast<AllocaInst>(V);
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003923 // Don't handle byval struct arguments or VLAs, for example.
3924 if (!AI)
3925 return 0;
3926 DenseMap<const AllocaInst*, int>::iterator SI =
3927 FuncInfo.StaticAllocaMap.find(AI);
3928 if (SI == FuncInfo.StaticAllocaMap.end())
3929 return 0; // VLAs.
3930 int FI = SI->second;
Chris Lattnerde4845c2010-04-02 19:42:39 +00003931
Chris Lattner512063d2010-04-05 06:19:28 +00003932 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
3933 if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo())
3934 MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc());
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003935 return 0;
3936 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003937 case Intrinsic::eh_exception: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003938 // Insert the EXCEPTIONADDR instruction.
Dan Gohman99be8ae2010-04-19 22:41:47 +00003939 assert(FuncInfo.MBBMap[I.getParent()]->isLandingPad() &&
3940 "Call to eh.exception not in landing pad!");
Owen Anderson825b72b2009-08-11 20:47:22 +00003941 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003942 SDValue Ops[1];
3943 Ops[0] = DAG.getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003944 SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003945 setValue(&I, Op);
3946 DAG.setRoot(Op.getValue(1));
3947 return 0;
3948 }
3949
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003950 case Intrinsic::eh_selector: {
Dan Gohman99be8ae2010-04-19 22:41:47 +00003951 MachineBasicBlock *CallMBB = FuncInfo.MBBMap[I.getParent()];
Chris Lattner512063d2010-04-05 06:19:28 +00003952 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Dan Gohman99be8ae2010-04-19 22:41:47 +00003953 if (CallMBB->isLandingPad())
3954 AddCatchInfo(I, &MMI, CallMBB);
Chris Lattner3a5815f2009-09-17 23:54:54 +00003955 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003956#ifndef NDEBUG
Chris Lattner3a5815f2009-09-17 23:54:54 +00003957 FuncInfo.CatchInfoLost.insert(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003958#endif
Chris Lattner3a5815f2009-09-17 23:54:54 +00003959 // FIXME: Mark exception selector register as live in. Hack for PR1508.
3960 unsigned Reg = TLI.getExceptionSelectorRegister();
Dan Gohman99be8ae2010-04-19 22:41:47 +00003961 if (Reg) FuncInfo.MBBMap[I.getParent()]->addLiveIn(Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003962 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003963
Chris Lattner3a5815f2009-09-17 23:54:54 +00003964 // Insert the EHSELECTION instruction.
3965 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
3966 SDValue Ops[2];
Eric Christopher551754c2010-04-16 23:37:20 +00003967 Ops[0] = getValue(I.getOperand(1));
Chris Lattner3a5815f2009-09-17 23:54:54 +00003968 Ops[1] = getRoot();
3969 SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2);
Chris Lattner3a5815f2009-09-17 23:54:54 +00003970 DAG.setRoot(Op.getValue(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00003971 setValue(&I, DAG.getSExtOrTrunc(Op, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003972 return 0;
3973 }
3974
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003975 case Intrinsic::eh_typeid_for: {
Chris Lattner512063d2010-04-05 06:19:28 +00003976 // Find the type id for the given typeinfo.
Eric Christopher551754c2010-04-16 23:37:20 +00003977 GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1));
Chris Lattner512063d2010-04-05 06:19:28 +00003978 unsigned TypeID = DAG.getMachineFunction().getMMI().getTypeIDFor(GV);
3979 Res = DAG.getConstant(TypeID, MVT::i32);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003980 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003981 return 0;
3982 }
3983
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003984 case Intrinsic::eh_return_i32:
3985 case Intrinsic::eh_return_i64:
Chris Lattner512063d2010-04-05 06:19:28 +00003986 DAG.getMachineFunction().getMMI().setCallsEHReturn(true);
3987 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, dl,
3988 MVT::Other,
3989 getControlRoot(),
Eric Christopher551754c2010-04-16 23:37:20 +00003990 getValue(I.getOperand(1)),
3991 getValue(I.getOperand(2))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003992 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003993 case Intrinsic::eh_unwind_init:
Chris Lattner512063d2010-04-05 06:19:28 +00003994 DAG.getMachineFunction().getMMI().setCallsUnwindInit(true);
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003995 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003996 case Intrinsic::eh_dwarf_cfa: {
Eric Christopher551754c2010-04-16 23:37:20 +00003997 EVT VT = getValue(I.getOperand(1)).getValueType();
3998 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getOperand(1)), dl,
Duncan Sands3a66a682009-10-13 21:04:12 +00003999 TLI.getPointerTy());
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004000 SDValue Offset = DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004001 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004002 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004003 TLI.getPointerTy()),
4004 CfaArg);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004005 SDValue FA = DAG.getNode(ISD::FRAMEADDR, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004006 TLI.getPointerTy(),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004007 DAG.getConstant(0, TLI.getPointerTy()));
Bill Wendling4533cac2010-01-28 21:51:40 +00004008 setValue(&I, DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
4009 FA, Offset));
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004010 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004011 }
Jim Grosbachca752c92010-01-28 01:45:32 +00004012 case Intrinsic::eh_sjlj_callsite: {
Chris Lattner512063d2010-04-05 06:19:28 +00004013 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Eric Christopher551754c2010-04-16 23:37:20 +00004014 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1));
Jim Grosbachca752c92010-01-28 01:45:32 +00004015 assert(CI && "Non-constant call site value in eh.sjlj.callsite!");
Chris Lattner512063d2010-04-05 06:19:28 +00004016 assert(MMI.getCurrentCallSite() == 0 && "Overlapping call sites!");
Jim Grosbachca752c92010-01-28 01:45:32 +00004017
Chris Lattner512063d2010-04-05 06:19:28 +00004018 MMI.setCurrentCallSite(CI->getZExtValue());
Jim Grosbachca752c92010-01-28 01:45:32 +00004019 return 0;
4020 }
4021
Mon P Wang77cdf302008-11-10 20:54:11 +00004022 case Intrinsic::convertff:
4023 case Intrinsic::convertfsi:
4024 case Intrinsic::convertfui:
4025 case Intrinsic::convertsif:
4026 case Intrinsic::convertuif:
4027 case Intrinsic::convertss:
4028 case Intrinsic::convertsu:
4029 case Intrinsic::convertus:
4030 case Intrinsic::convertuu: {
4031 ISD::CvtCode Code = ISD::CVT_INVALID;
4032 switch (Intrinsic) {
4033 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
4034 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
4035 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
4036 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
4037 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
4038 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
4039 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
4040 case Intrinsic::convertus: Code = ISD::CVT_US; break;
4041 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
4042 }
Owen Andersone50ed302009-08-10 22:56:29 +00004043 EVT DestVT = TLI.getValueType(I.getType());
Eric Christopher551754c2010-04-16 23:37:20 +00004044 const Value *Op1 = I.getOperand(1);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004045 Res = DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1),
4046 DAG.getValueType(DestVT),
4047 DAG.getValueType(getValue(Op1).getValueType()),
4048 getValue(I.getOperand(2)),
Eric Christopher551754c2010-04-16 23:37:20 +00004049 getValue(I.getOperand(3)),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004050 Code);
4051 setValue(&I, Res);
Mon P Wang77cdf302008-11-10 20:54:11 +00004052 return 0;
4053 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004054 case Intrinsic::sqrt:
Bill Wendling4533cac2010-01-28 21:51:40 +00004055 setValue(&I, DAG.getNode(ISD::FSQRT, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00004056 getValue(I.getOperand(1)).getValueType(),
4057 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004058 return 0;
4059 case Intrinsic::powi:
Eric Christopher551754c2010-04-16 23:37:20 +00004060 setValue(&I, ExpandPowI(dl, getValue(I.getOperand(1)),
4061 getValue(I.getOperand(2)), DAG));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004062 return 0;
4063 case Intrinsic::sin:
Bill Wendling4533cac2010-01-28 21:51:40 +00004064 setValue(&I, DAG.getNode(ISD::FSIN, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00004065 getValue(I.getOperand(1)).getValueType(),
4066 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004067 return 0;
4068 case Intrinsic::cos:
Bill Wendling4533cac2010-01-28 21:51:40 +00004069 setValue(&I, DAG.getNode(ISD::FCOS, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00004070 getValue(I.getOperand(1)).getValueType(),
4071 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004072 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004073 case Intrinsic::log:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004074 visitLog(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004075 return 0;
4076 case Intrinsic::log2:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004077 visitLog2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004078 return 0;
4079 case Intrinsic::log10:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004080 visitLog10(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004081 return 0;
4082 case Intrinsic::exp:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004083 visitExp(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004084 return 0;
4085 case Intrinsic::exp2:
Dale Johannesen601d3c02008-09-05 01:48:15 +00004086 visitExp2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004087 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004088 case Intrinsic::pow:
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004089 visitPow(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004090 return 0;
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004091 case Intrinsic::convert_to_fp16:
4092 setValue(&I, DAG.getNode(ISD::FP32_TO_FP16, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00004093 MVT::i16, getValue(I.getOperand(1))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004094 return 0;
4095 case Intrinsic::convert_from_fp16:
4096 setValue(&I, DAG.getNode(ISD::FP16_TO_FP32, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00004097 MVT::f32, getValue(I.getOperand(1))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004098 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004099 case Intrinsic::pcmarker: {
Eric Christopher551754c2010-04-16 23:37:20 +00004100 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00004101 DAG.setRoot(DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004102 return 0;
4103 }
4104 case Intrinsic::readcyclecounter: {
4105 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004106 Res = DAG.getNode(ISD::READCYCLECOUNTER, dl,
4107 DAG.getVTList(MVT::i64, MVT::Other),
4108 &Op, 1);
4109 setValue(&I, Res);
4110 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004111 return 0;
4112 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004113 case Intrinsic::bswap:
Bill Wendling4533cac2010-01-28 21:51:40 +00004114 setValue(&I, DAG.getNode(ISD::BSWAP, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00004115 getValue(I.getOperand(1)).getValueType(),
4116 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004117 return 0;
4118 case Intrinsic::cttz: {
Eric Christopher551754c2010-04-16 23:37:20 +00004119 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004120 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004121 setValue(&I, DAG.getNode(ISD::CTTZ, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004122 return 0;
4123 }
4124 case Intrinsic::ctlz: {
Eric Christopher551754c2010-04-16 23:37:20 +00004125 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004126 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004127 setValue(&I, DAG.getNode(ISD::CTLZ, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004128 return 0;
4129 }
4130 case Intrinsic::ctpop: {
Eric Christopher551754c2010-04-16 23:37:20 +00004131 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004132 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004133 setValue(&I, DAG.getNode(ISD::CTPOP, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004134 return 0;
4135 }
4136 case Intrinsic::stacksave: {
4137 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004138 Res = DAG.getNode(ISD::STACKSAVE, dl,
4139 DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1);
4140 setValue(&I, Res);
4141 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004142 return 0;
4143 }
4144 case Intrinsic::stackrestore: {
Eric Christopher551754c2010-04-16 23:37:20 +00004145 Res = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00004146 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Res));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004147 return 0;
4148 }
Bill Wendling57344502008-11-18 11:01:33 +00004149 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00004150 // Emit code into the DAG to store the stack guard onto the stack.
4151 MachineFunction &MF = DAG.getMachineFunction();
4152 MachineFrameInfo *MFI = MF.getFrameInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004153 EVT PtrTy = TLI.getPointerTy();
Bill Wendlingb2a42982008-11-06 02:29:10 +00004154
Eric Christopher551754c2010-04-16 23:37:20 +00004155 SDValue Src = getValue(I.getOperand(1)); // The guard's value.
4156 AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2));
Bill Wendlingb2a42982008-11-06 02:29:10 +00004157
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004158 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00004159 MFI->setStackProtectorIndex(FI);
4160
4161 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4162
4163 // Store the stack protector onto the stack.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004164 Res = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN,
4165 PseudoSourceValue::getFixedStack(FI),
David Greene1e559442010-02-15 17:00:31 +00004166 0, true, false, 0);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004167 setValue(&I, Res);
4168 DAG.setRoot(Res);
Bill Wendlingb2a42982008-11-06 02:29:10 +00004169 return 0;
4170 }
Eric Christopher7b5e6172009-10-27 00:52:25 +00004171 case Intrinsic::objectsize: {
4172 // If we don't know by now, we're never going to know.
Eric Christopher551754c2010-04-16 23:37:20 +00004173 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2));
Eric Christopher7b5e6172009-10-27 00:52:25 +00004174
4175 assert(CI && "Non-constant type in __builtin_object_size?");
4176
Eric Christopher551754c2010-04-16 23:37:20 +00004177 SDValue Arg = getValue(I.getOperand(0));
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00004178 EVT Ty = Arg.getValueType();
4179
Eric Christopherd060b252009-12-23 02:51:48 +00004180 if (CI->getZExtValue() == 0)
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004181 Res = DAG.getConstant(-1ULL, Ty);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004182 else
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004183 Res = DAG.getConstant(0, Ty);
4184
4185 setValue(&I, Res);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004186 return 0;
4187 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004188 case Intrinsic::var_annotation:
4189 // Discard annotate attributes
4190 return 0;
4191
4192 case Intrinsic::init_trampoline: {
Eric Christopher551754c2010-04-16 23:37:20 +00004193 const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004194
4195 SDValue Ops[6];
4196 Ops[0] = getRoot();
Eric Christopher551754c2010-04-16 23:37:20 +00004197 Ops[1] = getValue(I.getOperand(1));
4198 Ops[2] = getValue(I.getOperand(2));
4199 Ops[3] = getValue(I.getOperand(3));
4200 Ops[4] = DAG.getSrcValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004201 Ops[5] = DAG.getSrcValue(F);
4202
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004203 Res = DAG.getNode(ISD::TRAMPOLINE, dl,
4204 DAG.getVTList(TLI.getPointerTy(), MVT::Other),
4205 Ops, 6);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004206
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004207 setValue(&I, Res);
4208 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004209 return 0;
4210 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004211 case Intrinsic::gcroot:
4212 if (GFI) {
Eric Christopher551754c2010-04-16 23:37:20 +00004213 const Value *Alloca = I.getOperand(1);
4214 const Constant *TypeMap = cast<Constant>(I.getOperand(2));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004215
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004216 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
4217 GFI->addStackRoot(FI->getIndex(), TypeMap);
4218 }
4219 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004220 case Intrinsic::gcread:
4221 case Intrinsic::gcwrite:
Torok Edwinc23197a2009-07-14 16:55:14 +00004222 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004223 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004224 case Intrinsic::flt_rounds:
Bill Wendling4533cac2010-01-28 21:51:40 +00004225 setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004226 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004227 case Intrinsic::trap:
Bill Wendling4533cac2010-01-28 21:51:40 +00004228 DAG.setRoot(DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004229 return 0;
Bill Wendlingef375462008-11-21 02:38:44 +00004230 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00004231 return implVisitAluOverflow(I, ISD::UADDO);
4232 case Intrinsic::sadd_with_overflow:
4233 return implVisitAluOverflow(I, ISD::SADDO);
4234 case Intrinsic::usub_with_overflow:
4235 return implVisitAluOverflow(I, ISD::USUBO);
4236 case Intrinsic::ssub_with_overflow:
4237 return implVisitAluOverflow(I, ISD::SSUBO);
4238 case Intrinsic::umul_with_overflow:
4239 return implVisitAluOverflow(I, ISD::UMULO);
4240 case Intrinsic::smul_with_overflow:
4241 return implVisitAluOverflow(I, ISD::SMULO);
Bill Wendling7cdc3c82008-11-21 02:03:52 +00004242
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004243 case Intrinsic::prefetch: {
4244 SDValue Ops[4];
4245 Ops[0] = getRoot();
Eric Christopher551754c2010-04-16 23:37:20 +00004246 Ops[1] = getValue(I.getOperand(1));
4247 Ops[2] = getValue(I.getOperand(2));
4248 Ops[3] = getValue(I.getOperand(3));
Bill Wendling4533cac2010-01-28 21:51:40 +00004249 DAG.setRoot(DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004250 return 0;
4251 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004252
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004253 case Intrinsic::memory_barrier: {
4254 SDValue Ops[6];
4255 Ops[0] = getRoot();
4256 for (int x = 1; x < 6; ++x)
Eric Christopher551754c2010-04-16 23:37:20 +00004257 Ops[x] = getValue(I.getOperand(x));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004258
Bill Wendling4533cac2010-01-28 21:51:40 +00004259 DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004260 return 0;
4261 }
4262 case Intrinsic::atomic_cmp_swap: {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004263 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004264 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00004265 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(),
Eric Christopher551754c2010-04-16 23:37:20 +00004266 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004267 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004268 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004269 getValue(I.getOperand(2)),
Eric Christopher551754c2010-04-16 23:37:20 +00004270 getValue(I.getOperand(3)),
4271 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004272 setValue(&I, L);
4273 DAG.setRoot(L.getValue(1));
4274 return 0;
4275 }
4276 case Intrinsic::atomic_load_add:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004277 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004278 case Intrinsic::atomic_load_sub:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004279 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004280 case Intrinsic::atomic_load_or:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004281 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004282 case Intrinsic::atomic_load_xor:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004283 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004284 case Intrinsic::atomic_load_and:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004285 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004286 case Intrinsic::atomic_load_nand:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004287 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004288 case Intrinsic::atomic_load_max:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004289 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004290 case Intrinsic::atomic_load_min:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004291 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004292 case Intrinsic::atomic_load_umin:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004293 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004294 case Intrinsic::atomic_load_umax:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004295 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004296 case Intrinsic::atomic_swap:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004297 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP);
Duncan Sandsf07c9492009-11-10 09:08:09 +00004298
4299 case Intrinsic::invariant_start:
4300 case Intrinsic::lifetime_start:
4301 // Discard region information.
Bill Wendling4533cac2010-01-28 21:51:40 +00004302 setValue(&I, DAG.getUNDEF(TLI.getPointerTy()));
Duncan Sandsf07c9492009-11-10 09:08:09 +00004303 return 0;
4304 case Intrinsic::invariant_end:
4305 case Intrinsic::lifetime_end:
4306 // Discard region information.
4307 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004308 }
4309}
4310
Dan Gohman46510a72010-04-15 01:51:59 +00004311void SelectionDAGBuilder::LowerCallTo(ImmutableCallSite CS, SDValue Callee,
Dan Gohman2048b852009-11-23 18:04:58 +00004312 bool isTailCall,
4313 MachineBasicBlock *LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004314 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
4315 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004316 const Type *RetTy = FTy->getReturnType();
Chris Lattner512063d2010-04-05 06:19:28 +00004317 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Chris Lattner16112732010-03-14 01:41:15 +00004318 MCSymbol *BeginLabel = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004319
4320 TargetLowering::ArgListTy Args;
4321 TargetLowering::ArgListEntry Entry;
4322 Args.reserve(CS.arg_size());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004323
4324 // Check whether the function can return without sret-demotion.
4325 SmallVector<EVT, 4> OutVTs;
4326 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
4327 SmallVector<uint64_t, 4> Offsets;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004328 getReturnInfo(RetTy, CS.getAttributes().getRetAttributes(),
Bill Wendlinge80ae832009-12-22 00:50:32 +00004329 OutVTs, OutsFlags, TLI, &Offsets);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004330
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004331 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004332 FTy->isVarArg(), OutVTs, OutsFlags, DAG);
4333
4334 SDValue DemoteStackSlot;
4335
4336 if (!CanLowerReturn) {
4337 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(
4338 FTy->getReturnType());
4339 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(
4340 FTy->getReturnType());
4341 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00004342 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004343 const Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType());
4344
4345 DemoteStackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4346 Entry.Node = DemoteStackSlot;
4347 Entry.Ty = StackSlotPtrType;
4348 Entry.isSExt = false;
4349 Entry.isZExt = false;
4350 Entry.isInReg = false;
4351 Entry.isSRet = true;
4352 Entry.isNest = false;
4353 Entry.isByVal = false;
4354 Entry.Alignment = Align;
4355 Args.push_back(Entry);
4356 RetTy = Type::getVoidTy(FTy->getContext());
4357 }
4358
Dan Gohman46510a72010-04-15 01:51:59 +00004359 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004360 i != e; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004361 SDValue ArgNode = getValue(*i);
4362 Entry.Node = ArgNode; Entry.Ty = (*i)->getType();
4363
4364 unsigned attrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00004365 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
4366 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
4367 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
4368 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
4369 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
4370 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004371 Entry.Alignment = CS.getParamAlignment(attrInd);
4372 Args.push_back(Entry);
4373 }
4374
Chris Lattner512063d2010-04-05 06:19:28 +00004375 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004376 // Insert a label before the invoke call to mark the try range. This can be
4377 // used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00004378 BeginLabel = MMI.getContext().CreateTempSymbol();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00004379
Jim Grosbachca752c92010-01-28 01:45:32 +00004380 // For SjLj, keep track of which landing pads go with which invokes
4381 // so as to maintain the ordering of pads in the LSDA.
Chris Lattner512063d2010-04-05 06:19:28 +00004382 unsigned CallSiteIndex = MMI.getCurrentCallSite();
Jim Grosbachca752c92010-01-28 01:45:32 +00004383 if (CallSiteIndex) {
Chris Lattner512063d2010-04-05 06:19:28 +00004384 MMI.setCallSiteBeginLabel(BeginLabel, CallSiteIndex);
Jim Grosbachca752c92010-01-28 01:45:32 +00004385 // Now that the call site is handled, stop tracking it.
Chris Lattner512063d2010-04-05 06:19:28 +00004386 MMI.setCurrentCallSite(0);
Jim Grosbachca752c92010-01-28 01:45:32 +00004387 }
4388
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004389 // Both PendingLoads and PendingExports must be flushed here;
4390 // this call might not return.
4391 (void)getRoot();
Chris Lattner7561d482010-03-14 02:33:54 +00004392 DAG.setRoot(DAG.getEHLabel(getCurDebugLoc(), getControlRoot(), BeginLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004393 }
4394
Dan Gohman98ca4f22009-08-05 01:29:28 +00004395 // Check if target-independent constraints permit a tail call here.
4396 // Target-dependent constraints are checked within TLI.LowerCallTo.
4397 if (isTailCall &&
Evan Cheng86809cc2010-02-03 03:28:02 +00004398 !isInTailCallPosition(CS, CS.getAttributes().getRetAttributes(), TLI))
Dan Gohman98ca4f22009-08-05 01:29:28 +00004399 isTailCall = false;
4400
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004401 std::pair<SDValue,SDValue> Result =
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004402 TLI.LowerCallTo(getRoot(), RetTy,
Devang Patel05988662008-09-25 21:00:45 +00004403 CS.paramHasAttr(0, Attribute::SExt),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004404 CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(),
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00004405 CS.paramHasAttr(0, Attribute::InReg), FTy->getNumParams(),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004406 CS.getCallingConv(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00004407 isTailCall,
4408 !CS.getInstruction()->use_empty(),
Bill Wendling46ada192010-03-02 01:55:18 +00004409 Callee, Args, DAG, getCurDebugLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00004410 assert((isTailCall || Result.second.getNode()) &&
4411 "Non-null chain expected with non-tail call!");
4412 assert((Result.second.getNode() || !Result.first.getNode()) &&
4413 "Null value expected with tail call!");
Bill Wendlinge80ae832009-12-22 00:50:32 +00004414 if (Result.first.getNode()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004415 setValue(CS.getInstruction(), Result.first);
Bill Wendlinge80ae832009-12-22 00:50:32 +00004416 } else if (!CanLowerReturn && Result.second.getNode()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004417 // The instruction result is the result of loading from the
4418 // hidden sret parameter.
4419 SmallVector<EVT, 1> PVTs;
4420 const Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType());
4421
4422 ComputeValueVTs(TLI, PtrRetTy, PVTs);
4423 assert(PVTs.size() == 1 && "Pointers should fit in one register");
4424 EVT PtrVT = PVTs[0];
4425 unsigned NumValues = OutVTs.size();
4426 SmallVector<SDValue, 4> Values(NumValues);
4427 SmallVector<SDValue, 4> Chains(NumValues);
4428
4429 for (unsigned i = 0; i < NumValues; ++i) {
Bill Wendlinge80ae832009-12-22 00:50:32 +00004430 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT,
4431 DemoteStackSlot,
4432 DAG.getConstant(Offsets[i], PtrVT));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004433 SDValue L = DAG.getLoad(OutVTs[i], getCurDebugLoc(), Result.second,
David Greene1e559442010-02-15 17:00:31 +00004434 Add, NULL, Offsets[i], false, false, 1);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004435 Values[i] = L;
4436 Chains[i] = L.getValue(1);
4437 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00004438
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004439 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
4440 MVT::Other, &Chains[0], NumValues);
4441 PendingLoads.push_back(Chain);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004442
4443 // Collect the legal value parts into potentially illegal values
4444 // that correspond to the original function's return values.
4445 SmallVector<EVT, 4> RetTys;
4446 RetTy = FTy->getReturnType();
4447 ComputeValueVTs(TLI, RetTy, RetTys);
4448 ISD::NodeType AssertOp = ISD::DELETED_NODE;
4449 SmallVector<SDValue, 4> ReturnValues;
4450 unsigned CurReg = 0;
4451 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
4452 EVT VT = RetTys[I];
4453 EVT RegisterVT = TLI.getRegisterType(RetTy->getContext(), VT);
4454 unsigned NumRegs = TLI.getNumRegisters(RetTy->getContext(), VT);
4455
4456 SDValue ReturnValue =
Bill Wendling46ada192010-03-02 01:55:18 +00004457 getCopyFromParts(DAG, getCurDebugLoc(), &Values[CurReg], NumRegs,
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004458 RegisterVT, VT, AssertOp);
4459 ReturnValues.push_back(ReturnValue);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004460 CurReg += NumRegs;
4461 }
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004462
Bill Wendling4533cac2010-01-28 21:51:40 +00004463 setValue(CS.getInstruction(),
4464 DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
4465 DAG.getVTList(&RetTys[0], RetTys.size()),
4466 &ReturnValues[0], ReturnValues.size()));
Bill Wendlinge80ae832009-12-22 00:50:32 +00004467
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004468 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00004469
4470 // As a special case, a null chain means that a tail call has been emitted and
4471 // the DAG root is already updated.
Bill Wendling4533cac2010-01-28 21:51:40 +00004472 if (Result.second.getNode())
Dan Gohman98ca4f22009-08-05 01:29:28 +00004473 DAG.setRoot(Result.second);
Bill Wendling4533cac2010-01-28 21:51:40 +00004474 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00004475 HasTailCall = true;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004476
Chris Lattner512063d2010-04-05 06:19:28 +00004477 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004478 // Insert a label at the end of the invoke call to mark the try range. This
4479 // can be used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00004480 MCSymbol *EndLabel = MMI.getContext().CreateTempSymbol();
Chris Lattner7561d482010-03-14 02:33:54 +00004481 DAG.setRoot(DAG.getEHLabel(getCurDebugLoc(), getRoot(), EndLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004482
4483 // Inform MachineModuleInfo of range.
Chris Lattner512063d2010-04-05 06:19:28 +00004484 MMI.addInvoke(LandingPad, BeginLabel, EndLabel);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004485 }
4486}
4487
Chris Lattner8047d9a2009-12-24 00:37:38 +00004488/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
4489/// value is equal or not-equal to zero.
Dan Gohman46510a72010-04-15 01:51:59 +00004490static bool IsOnlyUsedInZeroEqualityComparison(const Value *V) {
4491 for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end();
Chris Lattner8047d9a2009-12-24 00:37:38 +00004492 UI != E; ++UI) {
Dan Gohman46510a72010-04-15 01:51:59 +00004493 if (const ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
Chris Lattner8047d9a2009-12-24 00:37:38 +00004494 if (IC->isEquality())
Dan Gohman46510a72010-04-15 01:51:59 +00004495 if (const Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
Chris Lattner8047d9a2009-12-24 00:37:38 +00004496 if (C->isNullValue())
4497 continue;
4498 // Unknown instruction.
4499 return false;
4500 }
4501 return true;
4502}
4503
Dan Gohman46510a72010-04-15 01:51:59 +00004504static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT,
4505 const Type *LoadTy,
Chris Lattner8047d9a2009-12-24 00:37:38 +00004506 SelectionDAGBuilder &Builder) {
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004507
Chris Lattner8047d9a2009-12-24 00:37:38 +00004508 // Check to see if this load can be trivially constant folded, e.g. if the
4509 // input is from a string literal.
Dan Gohman46510a72010-04-15 01:51:59 +00004510 if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00004511 // Cast pointer to the type we really want to load.
Dan Gohman46510a72010-04-15 01:51:59 +00004512 LoadInput = ConstantExpr::getBitCast(const_cast<Constant *>(LoadInput),
Chris Lattner8047d9a2009-12-24 00:37:38 +00004513 PointerType::getUnqual(LoadTy));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004514
Dan Gohman46510a72010-04-15 01:51:59 +00004515 if (const Constant *LoadCst =
4516 ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput),
4517 Builder.TD))
Chris Lattner8047d9a2009-12-24 00:37:38 +00004518 return Builder.getValue(LoadCst);
4519 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004520
Chris Lattner8047d9a2009-12-24 00:37:38 +00004521 // Otherwise, we have to emit the load. If the pointer is to unfoldable but
4522 // still constant memory, the input chain can be the entry node.
4523 SDValue Root;
4524 bool ConstantMemory = false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004525
Chris Lattner8047d9a2009-12-24 00:37:38 +00004526 // Do not serialize (non-volatile) loads of constant memory with anything.
4527 if (Builder.AA->pointsToConstantMemory(PtrVal)) {
4528 Root = Builder.DAG.getEntryNode();
4529 ConstantMemory = true;
4530 } else {
4531 // Do not serialize non-volatile loads against each other.
4532 Root = Builder.DAG.getRoot();
4533 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004534
Chris Lattner8047d9a2009-12-24 00:37:38 +00004535 SDValue Ptr = Builder.getValue(PtrVal);
4536 SDValue LoadVal = Builder.DAG.getLoad(LoadVT, Builder.getCurDebugLoc(), Root,
4537 Ptr, PtrVal /*SrcValue*/, 0/*SVOffset*/,
David Greene1e559442010-02-15 17:00:31 +00004538 false /*volatile*/,
4539 false /*nontemporal*/, 1 /* align=1 */);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004540
Chris Lattner8047d9a2009-12-24 00:37:38 +00004541 if (!ConstantMemory)
4542 Builder.PendingLoads.push_back(LoadVal.getValue(1));
4543 return LoadVal;
4544}
4545
4546
4547/// visitMemCmpCall - See if we can lower a call to memcmp in an optimized form.
4548/// If so, return true and lower it, otherwise return false and it will be
4549/// lowered like a normal call.
Dan Gohman46510a72010-04-15 01:51:59 +00004550bool SelectionDAGBuilder::visitMemCmpCall(const CallInst &I) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00004551 // Verify that the prototype makes sense. int memcmp(void*,void*,size_t)
4552 if (I.getNumOperands() != 4)
4553 return false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004554
Eric Christopher551754c2010-04-16 23:37:20 +00004555 const Value *LHS = I.getOperand(1), *RHS = I.getOperand(2);
Duncan Sands1df98592010-02-16 11:11:14 +00004556 if (!LHS->getType()->isPointerTy() || !RHS->getType()->isPointerTy() ||
Eric Christopher551754c2010-04-16 23:37:20 +00004557 !I.getOperand(3)->getType()->isIntegerTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +00004558 !I.getType()->isIntegerTy())
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004559 return false;
4560
Eric Christopher551754c2010-04-16 23:37:20 +00004561 const ConstantInt *Size = dyn_cast<ConstantInt>(I.getOperand(3));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004562
Chris Lattner8047d9a2009-12-24 00:37:38 +00004563 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
4564 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
Chris Lattner04b091a2009-12-24 01:07:17 +00004565 if (Size && IsOnlyUsedInZeroEqualityComparison(&I)) {
4566 bool ActuallyDoIt = true;
4567 MVT LoadVT;
4568 const Type *LoadTy;
4569 switch (Size->getZExtValue()) {
4570 default:
4571 LoadVT = MVT::Other;
4572 LoadTy = 0;
4573 ActuallyDoIt = false;
4574 break;
4575 case 2:
4576 LoadVT = MVT::i16;
4577 LoadTy = Type::getInt16Ty(Size->getContext());
4578 break;
4579 case 4:
4580 LoadVT = MVT::i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004581 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00004582 break;
4583 case 8:
4584 LoadVT = MVT::i64;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004585 LoadTy = Type::getInt64Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00004586 break;
4587 /*
4588 case 16:
4589 LoadVT = MVT::v4i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004590 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00004591 LoadTy = VectorType::get(LoadTy, 4);
4592 break;
4593 */
4594 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004595
Chris Lattner04b091a2009-12-24 01:07:17 +00004596 // This turns into unaligned loads. We only do this if the target natively
4597 // supports the MVT we'll be loading or if it is small enough (<= 4) that
4598 // we'll only produce a small number of byte loads.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004599
Chris Lattner04b091a2009-12-24 01:07:17 +00004600 // Require that we can find a legal MVT, and only do this if the target
4601 // supports unaligned loads of that type. Expanding into byte loads would
4602 // bloat the code.
4603 if (ActuallyDoIt && Size->getZExtValue() > 4) {
4604 // TODO: Handle 5 byte compare as 4-byte + 1 byte.
4605 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
4606 if (!TLI.isTypeLegal(LoadVT) ||!TLI.allowsUnalignedMemoryAccesses(LoadVT))
4607 ActuallyDoIt = false;
4608 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004609
Chris Lattner04b091a2009-12-24 01:07:17 +00004610 if (ActuallyDoIt) {
4611 SDValue LHSVal = getMemCmpLoad(LHS, LoadVT, LoadTy, *this);
4612 SDValue RHSVal = getMemCmpLoad(RHS, LoadVT, LoadTy, *this);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004613
Chris Lattner04b091a2009-12-24 01:07:17 +00004614 SDValue Res = DAG.getSetCC(getCurDebugLoc(), MVT::i1, LHSVal, RHSVal,
4615 ISD::SETNE);
4616 EVT CallVT = TLI.getValueType(I.getType(), true);
4617 setValue(&I, DAG.getZExtOrTrunc(Res, getCurDebugLoc(), CallVT));
4618 return true;
4619 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00004620 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004621
4622
Chris Lattner8047d9a2009-12-24 00:37:38 +00004623 return false;
4624}
4625
4626
Dan Gohman46510a72010-04-15 01:51:59 +00004627void SelectionDAGBuilder::visitCall(const CallInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004628 const char *RenameFn = 0;
4629 if (Function *F = I.getCalledFunction()) {
4630 if (F->isDeclaration()) {
Dan Gohman55e59c12010-04-19 19:05:59 +00004631 const TargetIntrinsicInfo *II = TM.getIntrinsicInfo();
Dale Johannesen49de9822009-02-05 01:49:45 +00004632 if (II) {
4633 if (unsigned IID = II->getIntrinsicID(F)) {
4634 RenameFn = visitIntrinsicCall(I, IID);
4635 if (!RenameFn)
4636 return;
4637 }
4638 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004639 if (unsigned IID = F->getIntrinsicID()) {
4640 RenameFn = visitIntrinsicCall(I, IID);
4641 if (!RenameFn)
4642 return;
4643 }
4644 }
4645
4646 // Check for well-known libc/libm calls. If the function is internal, it
4647 // can't be a library call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004648 if (!F->hasLocalLinkage() && F->hasName()) {
4649 StringRef Name = F->getName();
Duncan Sandsd2c817e2010-03-14 21:08:40 +00004650 if (Name == "copysign" || Name == "copysignf" || Name == "copysignl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004651 if (I.getNumOperands() == 3 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004652 I.getOperand(1)->getType()->isFloatingPointTy() &&
4653 I.getType() == I.getOperand(1)->getType() &&
4654 I.getType() == I.getOperand(2)->getType()) {
4655 SDValue LHS = getValue(I.getOperand(1));
4656 SDValue RHS = getValue(I.getOperand(2));
Bill Wendling0d580132009-12-23 01:28:19 +00004657 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(),
4658 LHS.getValueType(), LHS, RHS));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004659 return;
4660 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004661 } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004662 if (I.getNumOperands() == 2 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004663 I.getOperand(1)->getType()->isFloatingPointTy() &&
4664 I.getType() == I.getOperand(1)->getType()) {
4665 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00004666 setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(),
4667 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004668 return;
4669 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004670 } else if (Name == "sin" || Name == "sinf" || Name == "sinl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004671 if (I.getNumOperands() == 2 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004672 I.getOperand(1)->getType()->isFloatingPointTy() &&
4673 I.getType() == I.getOperand(1)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004674 I.onlyReadsMemory()) {
Eric Christopher551754c2010-04-16 23:37:20 +00004675 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00004676 setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(),
4677 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004678 return;
4679 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004680 } else if (Name == "cos" || Name == "cosf" || Name == "cosl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004681 if (I.getNumOperands() == 2 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004682 I.getOperand(1)->getType()->isFloatingPointTy() &&
4683 I.getType() == I.getOperand(1)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004684 I.onlyReadsMemory()) {
Eric Christopher551754c2010-04-16 23:37:20 +00004685 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00004686 setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(),
4687 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004688 return;
4689 }
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004690 } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") {
4691 if (I.getNumOperands() == 2 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004692 I.getOperand(1)->getType()->isFloatingPointTy() &&
4693 I.getType() == I.getOperand(1)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004694 I.onlyReadsMemory()) {
Eric Christopher551754c2010-04-16 23:37:20 +00004695 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00004696 setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(),
4697 Tmp.getValueType(), Tmp));
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004698 return;
4699 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00004700 } else if (Name == "memcmp") {
4701 if (visitMemCmpCall(I))
4702 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004703 }
4704 }
Eric Christopher551754c2010-04-16 23:37:20 +00004705 } else if (isa<InlineAsm>(I.getOperand(0))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004706 visitInlineAsm(&I);
4707 return;
4708 }
4709
4710 SDValue Callee;
4711 if (!RenameFn)
Eric Christopher551754c2010-04-16 23:37:20 +00004712 Callee = getValue(I.getOperand(0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004713 else
Bill Wendling056292f2008-09-16 21:48:12 +00004714 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004715
Bill Wendling0d580132009-12-23 01:28:19 +00004716 // Check if we can potentially perform a tail call. More detailed checking is
4717 // be done within LowerCallTo, after more information about the call is known.
Evan Cheng11e67932010-01-26 23:13:04 +00004718 LowerCallTo(&I, Callee, I.isTailCall());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004719}
4720
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004721/// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004722/// this value and returns the result as a ValueVT value. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004723/// Chain/Flag as the input and updates them for the output Chain/Flag.
4724/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004725SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling46ada192010-03-02 01:55:18 +00004726 SDValue &Chain, SDValue *Flag) const {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004727 // Assemble the legal parts into the final values.
4728 SmallVector<SDValue, 4> Values(ValueVTs.size());
4729 SmallVector<SDValue, 8> Parts;
4730 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
4731 // Copy the legal parts from the registers.
Owen Andersone50ed302009-08-10 22:56:29 +00004732 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00004733 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00004734 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004735
4736 Parts.resize(NumRegs);
4737 for (unsigned i = 0; i != NumRegs; ++i) {
4738 SDValue P;
Bill Wendlingec72e322009-12-22 01:11:43 +00004739 if (Flag == 0) {
Dale Johannesena04b7572009-02-03 23:04:43 +00004740 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
Bill Wendlingec72e322009-12-22 01:11:43 +00004741 } else {
Dale Johannesena04b7572009-02-03 23:04:43 +00004742 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004743 *Flag = P.getValue(2);
4744 }
Bill Wendlingec72e322009-12-22 01:11:43 +00004745
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004746 Chain = P.getValue(1);
Bill Wendlingec72e322009-12-22 01:11:43 +00004747
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004748 // If the source register was virtual and if we know something about it,
4749 // add an assert node.
4750 if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) &&
4751 RegisterVT.isInteger() && !RegisterVT.isVector()) {
4752 unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister;
4753 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
4754 if (FLI.LiveOutRegInfo.size() > SlotNo) {
4755 FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[SlotNo];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004756
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004757 unsigned RegSize = RegisterVT.getSizeInBits();
4758 unsigned NumSignBits = LOI.NumSignBits;
4759 unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004760
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004761 // FIXME: We capture more information than the dag can represent. For
4762 // now, just use the tightest assertzext/assertsext possible.
4763 bool isSExt = true;
Owen Anderson825b72b2009-08-11 20:47:22 +00004764 EVT FromVT(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004765 if (NumSignBits == RegSize)
Owen Anderson825b72b2009-08-11 20:47:22 +00004766 isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004767 else if (NumZeroBits >= RegSize-1)
Owen Anderson825b72b2009-08-11 20:47:22 +00004768 isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004769 else if (NumSignBits > RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004770 isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8
Dan Gohman07c26ee2009-03-31 01:38:29 +00004771 else if (NumZeroBits >= RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004772 isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004773 else if (NumSignBits > RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00004774 isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16
Dan Gohman07c26ee2009-03-31 01:38:29 +00004775 else if (NumZeroBits >= RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00004776 isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004777 else if (NumSignBits > RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00004778 isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32
Dan Gohman07c26ee2009-03-31 01:38:29 +00004779 else if (NumZeroBits >= RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00004780 isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004781
Bill Wendling4533cac2010-01-28 21:51:40 +00004782 if (FromVT != MVT::Other)
Dale Johannesen66978ee2009-01-31 02:22:37 +00004783 P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004784 RegisterVT, P, DAG.getValueType(FromVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004785 }
4786 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004787
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004788 Parts[i] = P;
4789 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004790
Bill Wendling46ada192010-03-02 01:55:18 +00004791 Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00004792 NumRegs, RegisterVT, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004793 Part += NumRegs;
4794 Parts.clear();
4795 }
4796
Bill Wendling4533cac2010-01-28 21:51:40 +00004797 return DAG.getNode(ISD::MERGE_VALUES, dl,
4798 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
4799 &Values[0], ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004800}
4801
4802/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004803/// specified value into the registers specified by this object. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004804/// Chain/Flag as the input and updates them for the output Chain/Flag.
4805/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004806void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
Bill Wendling46ada192010-03-02 01:55:18 +00004807 SDValue &Chain, SDValue *Flag) const {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004808 // Get the list of the values's legal parts.
4809 unsigned NumRegs = Regs.size();
4810 SmallVector<SDValue, 8> Parts(NumRegs);
4811 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00004812 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00004813 unsigned NumParts = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00004814 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004815
Bill Wendling46ada192010-03-02 01:55:18 +00004816 getCopyToParts(DAG, dl,
Bill Wendling3ea3c242009-12-22 02:10:19 +00004817 Val.getValue(Val.getResNo() + Value),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004818 &Parts[Part], NumParts, RegisterVT);
4819 Part += NumParts;
4820 }
4821
4822 // Copy the parts into the registers.
4823 SmallVector<SDValue, 8> Chains(NumRegs);
4824 for (unsigned i = 0; i != NumRegs; ++i) {
4825 SDValue Part;
Bill Wendlingec72e322009-12-22 01:11:43 +00004826 if (Flag == 0) {
Dale Johannesena04b7572009-02-03 23:04:43 +00004827 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
Bill Wendlingec72e322009-12-22 01:11:43 +00004828 } else {
Dale Johannesena04b7572009-02-03 23:04:43 +00004829 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004830 *Flag = Part.getValue(1);
4831 }
Bill Wendlingec72e322009-12-22 01:11:43 +00004832
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004833 Chains[i] = Part.getValue(0);
4834 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004835
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004836 if (NumRegs == 1 || Flag)
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004837 // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004838 // flagged to it. That is the CopyToReg nodes and the user are considered
4839 // a single scheduling unit. If we create a TokenFactor and return it as
4840 // chain, then the TokenFactor is both a predecessor (operand) of the
4841 // user as well as a successor (the TF operands are flagged to the user).
4842 // c1, f1 = CopyToReg
4843 // c2, f2 = CopyToReg
4844 // c3 = TokenFactor c1, c2
4845 // ...
4846 // = op c3, ..., f2
4847 Chain = Chains[NumRegs-1];
4848 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004849 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004850}
4851
4852/// AddInlineAsmOperands - Add this value to the specified inlineasm node
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004853/// operand list. This adds the code marker and includes the number of
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004854/// values added into it.
Chris Lattnerdecc2672010-04-07 05:20:54 +00004855void RegsForValue::AddInlineAsmOperands(unsigned Code, bool HasMatching,
4856 unsigned MatchingIdx,
Bill Wendling46ada192010-03-02 01:55:18 +00004857 SelectionDAG &DAG,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004858 std::vector<SDValue> &Ops) const {
Chris Lattnerdecc2672010-04-07 05:20:54 +00004859 unsigned Flag = InlineAsm::getFlagWord(Code, Regs.size());
Evan Cheng697cbbf2009-03-20 18:03:34 +00004860 if (HasMatching)
Chris Lattnerdecc2672010-04-07 05:20:54 +00004861 Flag = InlineAsm::getFlagWordForMatchingOp(Flag, MatchingIdx);
Dale Johannesen99499332009-12-23 07:32:51 +00004862 SDValue Res = DAG.getTargetConstant(Flag, MVT::i32);
Bill Wendling651ad132009-12-22 01:25:10 +00004863 Ops.push_back(Res);
4864
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004865 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Anderson23b9b192009-08-12 00:36:31 +00004866 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVTs[Value]);
Owen Andersone50ed302009-08-10 22:56:29 +00004867 EVT RegisterVT = RegVTs[Value];
Chris Lattner58f15c42008-10-17 16:21:11 +00004868 for (unsigned i = 0; i != NumRegs; ++i) {
4869 assert(Reg < Regs.size() && "Mismatch in # registers expected");
Bill Wendling4533cac2010-01-28 21:51:40 +00004870 Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT));
Chris Lattner58f15c42008-10-17 16:21:11 +00004871 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004872 }
4873}
4874
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004875/// isAllocatableRegister - If the specified register is safe to allocate,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004876/// i.e. it isn't a stack pointer or some other special register, return the
4877/// register class for the register. Otherwise, return null.
4878static const TargetRegisterClass *
4879isAllocatableRegister(unsigned Reg, MachineFunction &MF,
4880 const TargetLowering &TLI,
4881 const TargetRegisterInfo *TRI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004882 EVT FoundVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004883 const TargetRegisterClass *FoundRC = 0;
4884 for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(),
4885 E = TRI->regclass_end(); RCI != E; ++RCI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004886 EVT ThisVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004887
4888 const TargetRegisterClass *RC = *RCI;
Dan Gohmanf451cb82010-02-10 16:03:48 +00004889 // If none of the value types for this register class are valid, we
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004890 // can't use it. For example, 64-bit reg classes on 32-bit targets.
4891 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
4892 I != E; ++I) {
4893 if (TLI.isTypeLegal(*I)) {
4894 // If we have already found this register in a different register class,
4895 // choose the one with the largest VT specified. For example, on
4896 // PowerPC, we favor f64 register classes over f32.
Owen Anderson825b72b2009-08-11 20:47:22 +00004897 if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004898 ThisVT = *I;
4899 break;
4900 }
4901 }
4902 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004903
Owen Anderson825b72b2009-08-11 20:47:22 +00004904 if (ThisVT == MVT::Other) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004905
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004906 // NOTE: This isn't ideal. In particular, this might allocate the
4907 // frame pointer in functions that need it (due to them not being taken
4908 // out of allocation, because a variable sized allocation hasn't been seen
4909 // yet). This is a slight code pessimization, but should still work.
4910 for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
4911 E = RC->allocation_order_end(MF); I != E; ++I)
4912 if (*I == Reg) {
4913 // We found a matching register class. Keep looking at others in case
4914 // we find one with larger registers that this physreg is also in.
4915 FoundRC = RC;
4916 FoundVT = ThisVT;
4917 break;
4918 }
4919 }
4920 return FoundRC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004921}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004922
4923
4924namespace llvm {
4925/// AsmOperandInfo - This contains information for each constraint that we are
4926/// lowering.
Cedric Venetaff9c272009-02-14 16:06:42 +00004927class VISIBILITY_HIDDEN SDISelAsmOperandInfo :
Daniel Dunbarc0c3b9a2008-09-10 04:16:29 +00004928 public TargetLowering::AsmOperandInfo {
Cedric Venetaff9c272009-02-14 16:06:42 +00004929public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004930 /// CallOperand - If this is the result output operand or a clobber
4931 /// this is null, otherwise it is the incoming operand to the CallInst.
4932 /// This gets modified as the asm is processed.
4933 SDValue CallOperand;
4934
4935 /// AssignedRegs - If this is a register or register class operand, this
4936 /// contains the set of register corresponding to the operand.
4937 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004938
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004939 explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info)
4940 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
4941 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004942
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004943 /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers
4944 /// busy in OutputRegs/InputRegs.
4945 void MarkAllocatedRegs(bool isOutReg, bool isInReg,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004946 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004947 std::set<unsigned> &InputRegs,
4948 const TargetRegisterInfo &TRI) const {
4949 if (isOutReg) {
4950 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4951 MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI);
4952 }
4953 if (isInReg) {
4954 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4955 MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI);
4956 }
4957 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004958
Owen Andersone50ed302009-08-10 22:56:29 +00004959 /// getCallOperandValEVT - Return the EVT of the Value* that this operand
Chris Lattner81249c92008-10-17 17:05:25 +00004960 /// corresponds to. If there is no Value* for this operand, it returns
Owen Anderson825b72b2009-08-11 20:47:22 +00004961 /// MVT::Other.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004962 EVT getCallOperandValEVT(LLVMContext &Context,
Owen Anderson1d0be152009-08-13 21:58:54 +00004963 const TargetLowering &TLI,
Chris Lattner81249c92008-10-17 17:05:25 +00004964 const TargetData *TD) const {
Owen Anderson825b72b2009-08-11 20:47:22 +00004965 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004966
Chris Lattner81249c92008-10-17 17:05:25 +00004967 if (isa<BasicBlock>(CallOperandVal))
4968 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004969
Chris Lattner81249c92008-10-17 17:05:25 +00004970 const llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004971
Chris Lattner81249c92008-10-17 17:05:25 +00004972 // If this is an indirect operand, the operand is a pointer to the
4973 // accessed type.
Bob Wilsone261b0c2009-12-22 18:34:19 +00004974 if (isIndirect) {
4975 const llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
4976 if (!PtrTy)
Chris Lattner75361b62010-04-07 22:58:41 +00004977 report_fatal_error("Indirect operand for inline asm not a pointer!");
Bob Wilsone261b0c2009-12-22 18:34:19 +00004978 OpTy = PtrTy->getElementType();
4979 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004980
Chris Lattner81249c92008-10-17 17:05:25 +00004981 // If OpTy is not a single value, it may be a struct/union that we
4982 // can tile with integers.
4983 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
4984 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
4985 switch (BitSize) {
4986 default: break;
4987 case 1:
4988 case 8:
4989 case 16:
4990 case 32:
4991 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00004992 case 128:
Owen Anderson1d0be152009-08-13 21:58:54 +00004993 OpTy = IntegerType::get(Context, BitSize);
Chris Lattner81249c92008-10-17 17:05:25 +00004994 break;
4995 }
4996 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004997
Chris Lattner81249c92008-10-17 17:05:25 +00004998 return TLI.getValueType(OpTy, true);
4999 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005000
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005001private:
5002 /// MarkRegAndAliases - Mark the specified register and all aliases in the
5003 /// specified set.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005004 static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005005 const TargetRegisterInfo &TRI) {
5006 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg");
5007 Regs.insert(Reg);
5008 if (const unsigned *Aliases = TRI.getAliasSet(Reg))
5009 for (; *Aliases; ++Aliases)
5010 Regs.insert(*Aliases);
5011 }
5012};
5013} // end llvm namespace.
5014
5015
5016/// GetRegistersForValue - Assign registers (virtual or physical) for the
5017/// specified operand. We prefer to assign virtual registers, to allow the
Bob Wilson266d9452009-12-17 05:07:36 +00005018/// register allocator to handle the assignment process. However, if the asm
5019/// uses features that we can't model on machineinstrs, we have SDISel do the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005020/// allocation. This produces generally horrible, but correct, code.
5021///
5022/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005023/// Input and OutputRegs are the set of already allocated physical registers.
5024///
Dan Gohman2048b852009-11-23 18:04:58 +00005025void SelectionDAGBuilder::
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005026GetRegistersForValue(SDISelAsmOperandInfo &OpInfo,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005027 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005028 std::set<unsigned> &InputRegs) {
Dan Gohman0d24bfb2009-08-15 02:06:22 +00005029 LLVMContext &Context = FuncInfo.Fn->getContext();
Owen Anderson23b9b192009-08-12 00:36:31 +00005030
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005031 // Compute whether this value requires an input register, an output register,
5032 // or both.
5033 bool isOutReg = false;
5034 bool isInReg = false;
5035 switch (OpInfo.Type) {
5036 case InlineAsm::isOutput:
5037 isOutReg = true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005038
5039 // If there is an input constraint that matches this, we need to reserve
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005040 // the input register so no other inputs allocate to it.
Chris Lattner6bdcda32008-10-17 16:47:46 +00005041 isInReg = OpInfo.hasMatchingInput();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005042 break;
5043 case InlineAsm::isInput:
5044 isInReg = true;
5045 isOutReg = false;
5046 break;
5047 case InlineAsm::isClobber:
5048 isOutReg = true;
5049 isInReg = true;
5050 break;
5051 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005052
5053
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005054 MachineFunction &MF = DAG.getMachineFunction();
5055 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005056
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005057 // If this is a constraint for a single physreg, or a constraint for a
5058 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005059 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005060 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
5061 OpInfo.ConstraintVT);
5062
5063 unsigned NumRegs = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00005064 if (OpInfo.ConstraintVT != MVT::Other) {
Chris Lattner01426e12008-10-21 00:45:36 +00005065 // If this is a FP input in an integer register (or visa versa) insert a bit
5066 // cast of the input value. More generally, handle any case where the input
5067 // value disagrees with the register class we plan to stick this in.
5068 if (OpInfo.Type == InlineAsm::isInput &&
5069 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00005070 // Try to convert to the first EVT that the reg class contains. If the
Chris Lattner01426e12008-10-21 00:45:36 +00005071 // types are identical size, use a bitcast to convert (e.g. two differing
5072 // vector types).
Owen Andersone50ed302009-08-10 22:56:29 +00005073 EVT RegVT = *PhysReg.second->vt_begin();
Chris Lattner01426e12008-10-21 00:45:36 +00005074 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005075 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005076 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005077 OpInfo.ConstraintVT = RegVT;
5078 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
5079 // If the input is a FP value and we want it in FP registers, do a
5080 // bitcast to the corresponding integer type. This turns an f64 value
5081 // into i64, which can be passed with two i32 values on a 32-bit
5082 // machine.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005083 RegVT = EVT::getIntegerVT(Context,
Owen Anderson23b9b192009-08-12 00:36:31 +00005084 OpInfo.ConstraintVT.getSizeInBits());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005085 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005086 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005087 OpInfo.ConstraintVT = RegVT;
5088 }
5089 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005090
Owen Anderson23b9b192009-08-12 00:36:31 +00005091 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00005092 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005093
Owen Andersone50ed302009-08-10 22:56:29 +00005094 EVT RegVT;
5095 EVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005096
5097 // If this is a constraint for a specific physical register, like {r17},
5098 // assign it now.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005099 if (unsigned AssignedReg = PhysReg.first) {
5100 const TargetRegisterClass *RC = PhysReg.second;
Owen Anderson825b72b2009-08-11 20:47:22 +00005101 if (OpInfo.ConstraintVT == MVT::Other)
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005102 ValueVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005103
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005104 // Get the actual register value type. This is important, because the user
5105 // may have asked for (e.g.) the AX register in i32 type. We need to
5106 // remember that AX is actually i16 to get the right extension.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005107 RegVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005108
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005109 // This is a explicit reference to a physical register.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005110 Regs.push_back(AssignedReg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005111
5112 // If this is an expanded reference, add the rest of the regs to Regs.
5113 if (NumRegs != 1) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005114 TargetRegisterClass::iterator I = RC->begin();
5115 for (; *I != AssignedReg; ++I)
5116 assert(I != RC->end() && "Didn't find reg!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005117
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005118 // Already added the first reg.
5119 --NumRegs; ++I;
5120 for (; NumRegs; --NumRegs, ++I) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005121 assert(I != RC->end() && "Ran out of registers to allocate!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005122 Regs.push_back(*I);
5123 }
5124 }
Bill Wendling651ad132009-12-22 01:25:10 +00005125
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005126 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5127 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5128 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5129 return;
5130 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005131
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005132 // Otherwise, if this was a reference to an LLVM register class, create vregs
5133 // for this reference.
Chris Lattnerb3b44842009-03-24 15:25:07 +00005134 if (const TargetRegisterClass *RC = PhysReg.second) {
5135 RegVT = *RC->vt_begin();
Owen Anderson825b72b2009-08-11 20:47:22 +00005136 if (OpInfo.ConstraintVT == MVT::Other)
Evan Chengfb112882009-03-23 08:01:15 +00005137 ValueVT = RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005138
Evan Chengfb112882009-03-23 08:01:15 +00005139 // Create the appropriate number of virtual registers.
5140 MachineRegisterInfo &RegInfo = MF.getRegInfo();
5141 for (; NumRegs; --NumRegs)
Chris Lattnerb3b44842009-03-24 15:25:07 +00005142 Regs.push_back(RegInfo.createVirtualRegister(RC));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005143
Evan Chengfb112882009-03-23 08:01:15 +00005144 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5145 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005146 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005147
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005148 // This is a reference to a register class that doesn't directly correspond
5149 // to an LLVM register class. Allocate NumRegs consecutive, available,
5150 // registers from the class.
5151 std::vector<unsigned> RegClassRegs
5152 = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode,
5153 OpInfo.ConstraintVT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005154
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005155 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5156 unsigned NumAllocated = 0;
5157 for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
5158 unsigned Reg = RegClassRegs[i];
5159 // See if this register is available.
5160 if ((isOutReg && OutputRegs.count(Reg)) || // Already used.
5161 (isInReg && InputRegs.count(Reg))) { // Already used.
5162 // Make sure we find consecutive registers.
5163 NumAllocated = 0;
5164 continue;
5165 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005166
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005167 // Check to see if this register is allocatable (i.e. don't give out the
5168 // stack pointer).
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005169 const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI);
5170 if (!RC) { // Couldn't allocate this register.
5171 // Reset NumAllocated to make sure we return consecutive registers.
5172 NumAllocated = 0;
5173 continue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005174 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005175
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005176 // Okay, this register is good, we can use it.
5177 ++NumAllocated;
5178
5179 // If we allocated enough consecutive registers, succeed.
5180 if (NumAllocated == NumRegs) {
5181 unsigned RegStart = (i-NumAllocated)+1;
5182 unsigned RegEnd = i+1;
5183 // Mark all of the allocated registers used.
5184 for (unsigned i = RegStart; i != RegEnd; ++i)
5185 Regs.push_back(RegClassRegs[i]);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005186
5187 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005188 OpInfo.ConstraintVT);
5189 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5190 return;
5191 }
5192 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005193
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005194 // Otherwise, we couldn't allocate enough registers for this.
5195}
5196
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005197/// visitInlineAsm - Handle a call to an InlineAsm object.
5198///
Dan Gohman46510a72010-04-15 01:51:59 +00005199void SelectionDAGBuilder::visitInlineAsm(ImmutableCallSite CS) {
5200 const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005201
5202 /// ConstraintOperands - Information about all of the constraints.
5203 std::vector<SDISelAsmOperandInfo> ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005204
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005205 std::set<unsigned> OutputRegs, InputRegs;
5206
5207 // Do a prepass over the constraints, canonicalizing them, and building up the
5208 // ConstraintOperands list.
5209 std::vector<InlineAsm::ConstraintInfo>
5210 ConstraintInfos = IA->ParseConstraints();
5211
Evan Chengda43bcf2008-09-24 00:05:32 +00005212 bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005213
Chris Lattner6c147292009-04-30 00:48:50 +00005214 SDValue Chain, Flag;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005215
Chris Lattner6c147292009-04-30 00:48:50 +00005216 // We won't need to flush pending loads if this asm doesn't touch
5217 // memory and is nonvolatile.
5218 if (hasMemory || IA->hasSideEffects())
Dale Johannesen97d14fc2009-04-18 00:09:40 +00005219 Chain = getRoot();
Chris Lattner6c147292009-04-30 00:48:50 +00005220 else
5221 Chain = DAG.getRoot();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005222
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005223 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
5224 unsigned ResNo = 0; // ResNo - The result number of the next output.
5225 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5226 ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i]));
5227 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005228
Owen Anderson825b72b2009-08-11 20:47:22 +00005229 EVT OpVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005230
5231 // Compute the value type for each operand.
5232 switch (OpInfo.Type) {
5233 case InlineAsm::isOutput:
5234 // Indirect outputs just consume an argument.
5235 if (OpInfo.isIndirect) {
Dan Gohman46510a72010-04-15 01:51:59 +00005236 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005237 break;
5238 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005239
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005240 // The return value of the call is this value. As such, there is no
5241 // corresponding argument.
Benjamin Kramerf0127052010-01-05 13:12:22 +00005242 assert(!CS.getType()->isVoidTy() &&
Owen Anderson1d0be152009-08-13 21:58:54 +00005243 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005244 if (const StructType *STy = dyn_cast<StructType>(CS.getType())) {
5245 OpVT = TLI.getValueType(STy->getElementType(ResNo));
5246 } else {
5247 assert(ResNo == 0 && "Asm only has one result!");
5248 OpVT = TLI.getValueType(CS.getType());
5249 }
5250 ++ResNo;
5251 break;
5252 case InlineAsm::isInput:
Dan Gohman46510a72010-04-15 01:51:59 +00005253 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005254 break;
5255 case InlineAsm::isClobber:
5256 // Nothing to do.
5257 break;
5258 }
5259
5260 // If this is an input or an indirect output, process the call argument.
5261 // BasicBlocks are labels, currently appearing only in asm's.
5262 if (OpInfo.CallOperandVal) {
Dale Johannesen5339c552009-07-20 23:27:39 +00005263 // Strip bitcasts, if any. This mostly comes up for functions.
Dale Johannesen76711242009-08-06 22:45:51 +00005264 OpInfo.CallOperandVal = OpInfo.CallOperandVal->stripPointerCasts();
5265
Dan Gohman46510a72010-04-15 01:51:59 +00005266 if (const BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005267 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00005268 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005269 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005270 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005271
Owen Anderson1d0be152009-08-13 21:58:54 +00005272 OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005273 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005274
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005275 OpInfo.ConstraintVT = OpVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005276 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005277
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005278 // Second pass over the constraints: compute which constraint option to use
5279 // and assign registers to constraints that want a specific physreg.
5280 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5281 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005282
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005283 // If this is an output operand with a matching input operand, look up the
Evan Cheng09dc9c02008-12-16 18:21:39 +00005284 // matching input. If their types mismatch, e.g. one is an integer, the
5285 // other is floating point, or their sizes are different, flag it as an
5286 // error.
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005287 if (OpInfo.hasMatchingInput()) {
5288 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
Chris Lattner87d677c2010-04-07 23:50:38 +00005289
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005290 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Evan Cheng09dc9c02008-12-16 18:21:39 +00005291 if ((OpInfo.ConstraintVT.isInteger() !=
5292 Input.ConstraintVT.isInteger()) ||
5293 (OpInfo.ConstraintVT.getSizeInBits() !=
5294 Input.ConstraintVT.getSizeInBits())) {
Chris Lattner75361b62010-04-07 22:58:41 +00005295 report_fatal_error("Unsupported asm: input constraint"
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005296 " with a matching output constraint of"
5297 " incompatible type!");
Evan Cheng09dc9c02008-12-16 18:21:39 +00005298 }
5299 Input.ConstraintVT = OpInfo.ConstraintVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005300 }
5301 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005302
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005303 // Compute the constraint code and ConstraintType to use.
Evan Chengda43bcf2008-09-24 00:05:32 +00005304 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, hasMemory, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005305
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005306 // If this is a memory input, and if the operand is not indirect, do what we
5307 // need to to provide an address for the memory input.
5308 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
5309 !OpInfo.isIndirect) {
5310 assert(OpInfo.Type == InlineAsm::isInput &&
5311 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005312
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005313 // Memory operands really want the address of the value. If we don't have
5314 // an indirect input, put it in the constpool if we can, otherwise spill
5315 // it to a stack slot.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005316
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005317 // If the operand is a float, integer, or vector constant, spill to a
5318 // constant pool entry to get its address.
Dan Gohman46510a72010-04-15 01:51:59 +00005319 const Value *OpVal = OpInfo.CallOperandVal;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005320 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
5321 isa<ConstantVector>(OpVal)) {
5322 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
5323 TLI.getPointerTy());
5324 } else {
5325 // Otherwise, create a stack slot and emit a store to it before the
5326 // asm.
5327 const Type *Ty = OpVal->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00005328 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005329 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
5330 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005331 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005332 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005333 Chain = DAG.getStore(Chain, getCurDebugLoc(),
David Greene1e559442010-02-15 17:00:31 +00005334 OpInfo.CallOperand, StackSlot, NULL, 0,
5335 false, false, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005336 OpInfo.CallOperand = StackSlot;
5337 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005338
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005339 // There is no longer a Value* corresponding to this operand.
5340 OpInfo.CallOperandVal = 0;
Bill Wendling651ad132009-12-22 01:25:10 +00005341
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005342 // It is now an indirect operand.
5343 OpInfo.isIndirect = true;
5344 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005345
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005346 // If this constraint is for a specific register, allocate it before
5347 // anything else.
5348 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005349 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005350 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005351
Bill Wendling651ad132009-12-22 01:25:10 +00005352 ConstraintInfos.clear();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005353
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005354 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00005355 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005356 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5357 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005358
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005359 // C_Register operands have already been allocated, Other/Memory don't need
5360 // to be.
5361 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005362 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005363 }
5364
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005365 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
5366 std::vector<SDValue> AsmNodeOperands;
5367 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
5368 AsmNodeOperands.push_back(
Dan Gohmanf2d7fb32010-01-04 21:00:54 +00005369 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(),
5370 TLI.getPointerTy()));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005371
Chris Lattnerdecc2672010-04-07 05:20:54 +00005372 // If we have a !srcloc metadata node associated with it, we want to attach
5373 // this to the ultimately generated inline asm machineinstr. To do this, we
5374 // pass in the third operand as this (potentially null) inline asm MDNode.
5375 const MDNode *SrcLoc = CS.getInstruction()->getMetadata("srcloc");
5376 AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005377
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005378 // Loop over all of the inputs, copying the operand values into the
5379 // appropriate registers and processing the output regs.
5380 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005381
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005382 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
5383 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005384
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005385 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5386 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
5387
5388 switch (OpInfo.Type) {
5389 case InlineAsm::isOutput: {
5390 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
5391 OpInfo.ConstraintType != TargetLowering::C_Register) {
5392 // Memory output, or 'other' output (e.g. 'X' constraint).
5393 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
5394
5395 // Add information to the INLINEASM node to know about this output.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005396 unsigned OpFlags = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
5397 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005398 TLI.getPointerTy()));
5399 AsmNodeOperands.push_back(OpInfo.CallOperand);
5400 break;
5401 }
5402
5403 // Otherwise, this is a register or register class output.
5404
5405 // Copy the output from the appropriate register. Find a register that
5406 // we can use.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005407 if (OpInfo.AssignedRegs.Regs.empty())
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005408 report_fatal_error("Couldn't allocate output reg for constraint '" +
5409 Twine(OpInfo.ConstraintCode) + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005410
5411 // If this is an indirect operand, store through the pointer after the
5412 // asm.
5413 if (OpInfo.isIndirect) {
5414 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
5415 OpInfo.CallOperandVal));
5416 } else {
5417 // This is the result value of the call.
Benjamin Kramerf0127052010-01-05 13:12:22 +00005418 assert(!CS.getType()->isVoidTy() && "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005419 // Concatenate this output onto the outputs list.
5420 RetValRegs.append(OpInfo.AssignedRegs);
5421 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005422
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005423 // Add information to the INLINEASM node to know that this register is
5424 // set.
Dale Johannesen913d3df2008-09-12 17:49:03 +00005425 OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ?
Chris Lattnerdecc2672010-04-07 05:20:54 +00005426 InlineAsm::Kind_RegDefEarlyClobber :
5427 InlineAsm::Kind_RegDef,
Evan Chengfb112882009-03-23 08:01:15 +00005428 false,
5429 0,
Bill Wendling46ada192010-03-02 01:55:18 +00005430 DAG,
Bill Wendling651ad132009-12-22 01:25:10 +00005431 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005432 break;
5433 }
5434 case InlineAsm::isInput: {
5435 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005436
Chris Lattner6bdcda32008-10-17 16:47:46 +00005437 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005438 // If this is required to match an output register we have already set,
5439 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00005440 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005441
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005442 // Scan until we find the definition we already emitted of this operand.
5443 // When we find it, create a RegsForValue operand.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005444 unsigned CurOp = InlineAsm::Op_FirstOperand;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005445 for (; OperandNo; --OperandNo) {
5446 // Advance to the next operand.
Evan Cheng697cbbf2009-03-20 18:03:34 +00005447 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005448 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00005449 assert((InlineAsm::isRegDefKind(OpFlag) ||
5450 InlineAsm::isRegDefEarlyClobberKind(OpFlag) ||
5451 InlineAsm::isMemKind(OpFlag)) && "Skipped past definitions?");
Evan Cheng697cbbf2009-03-20 18:03:34 +00005452 CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005453 }
5454
Evan Cheng697cbbf2009-03-20 18:03:34 +00005455 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005456 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00005457 if (InlineAsm::isRegDefKind(OpFlag) ||
5458 InlineAsm::isRegDefEarlyClobberKind(OpFlag)) {
Evan Cheng697cbbf2009-03-20 18:03:34 +00005459 // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
Chris Lattner6129c372010-04-08 00:09:16 +00005460 if (OpInfo.isIndirect) {
5461 // This happens on gcc/testsuite/gcc.dg/pr8788-1.c
Dan Gohman99be8ae2010-04-19 22:41:47 +00005462 LLVMContext &Ctx = *DAG.getContext();
Chris Lattner6129c372010-04-08 00:09:16 +00005463 Ctx.emitError(CS.getInstruction(), "inline asm not supported yet:"
5464 " don't know how to handle tied "
5465 "indirect register inputs");
5466 }
5467
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005468 RegsForValue MatchedRegs;
5469 MatchedRegs.TLI = &TLI;
5470 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
Owen Andersone50ed302009-08-10 22:56:29 +00005471 EVT RegVT = AsmNodeOperands[CurOp+1].getValueType();
Evan Chengfb112882009-03-23 08:01:15 +00005472 MatchedRegs.RegVTs.push_back(RegVT);
5473 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005474 for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag);
Evan Chengfb112882009-03-23 08:01:15 +00005475 i != e; ++i)
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005476 MatchedRegs.Regs.push_back
5477 (RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT)));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005478
5479 // Use the produced MatchedRegs object to
Dale Johannesen66978ee2009-01-31 02:22:37 +00005480 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005481 Chain, &Flag);
Chris Lattnerdecc2672010-04-07 05:20:54 +00005482 MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse,
Evan Chengfb112882009-03-23 08:01:15 +00005483 true, OpInfo.getMatchedOperand(),
Bill Wendling46ada192010-03-02 01:55:18 +00005484 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005485 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005486 }
Chris Lattnerdecc2672010-04-07 05:20:54 +00005487
5488 assert(InlineAsm::isMemKind(OpFlag) && "Unknown matching constraint!");
5489 assert(InlineAsm::getNumOperandRegisters(OpFlag) == 1 &&
5490 "Unexpected number of operands");
5491 // Add information to the INLINEASM node to know about this input.
5492 // See InlineAsm.h isUseOperandTiedToDef.
5493 OpFlag = InlineAsm::getFlagWordForMatchingOp(OpFlag,
5494 OpInfo.getMatchedOperand());
5495 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag,
5496 TLI.getPointerTy()));
5497 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
5498 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005499 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005500
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005501 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005502 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005503 "Don't know how to handle indirect other inputs yet!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005504
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005505 std::vector<SDValue> Ops;
5506 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0],
Evan Chengda43bcf2008-09-24 00:05:32 +00005507 hasMemory, Ops, DAG);
Chris Lattner87d677c2010-04-07 23:50:38 +00005508 if (Ops.empty())
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005509 report_fatal_error("Invalid operand for inline asm constraint '" +
5510 Twine(OpInfo.ConstraintCode) + "'!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005511
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005512 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005513 unsigned ResOpType =
5514 InlineAsm::getFlagWord(InlineAsm::Kind_Imm, Ops.size());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005515 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005516 TLI.getPointerTy()));
5517 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
5518 break;
Chris Lattnerdecc2672010-04-07 05:20:54 +00005519 }
5520
5521 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005522 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
5523 assert(InOperandVal.getValueType() == TLI.getPointerTy() &&
5524 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005525
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005526 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005527 unsigned ResOpType = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
Dale Johannesen86b49f82008-09-24 01:07:17 +00005528 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005529 TLI.getPointerTy()));
5530 AsmNodeOperands.push_back(InOperandVal);
5531 break;
5532 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005533
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005534 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
5535 OpInfo.ConstraintType == TargetLowering::C_Register) &&
5536 "Unknown constraint type!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005537 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005538 "Don't know how to handle indirect register inputs yet!");
5539
5540 // Copy the input into the appropriate registers.
Evan Cheng8112b532010-02-10 01:21:02 +00005541 if (OpInfo.AssignedRegs.Regs.empty() ||
Chris Lattner87d677c2010-04-07 23:50:38 +00005542 !OpInfo.AssignedRegs.areValueTypesLegal())
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005543 report_fatal_error("Couldn't allocate input reg for constraint '" +
5544 Twine(OpInfo.ConstraintCode) + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005545
Dale Johannesen66978ee2009-01-31 02:22:37 +00005546 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005547 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005548
Chris Lattnerdecc2672010-04-07 05:20:54 +00005549 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse, false, 0,
Bill Wendling46ada192010-03-02 01:55:18 +00005550 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005551 break;
5552 }
5553 case InlineAsm::isClobber: {
5554 // Add the clobbered value to the operand list, so that the register
5555 // allocator is aware that the physreg got clobbered.
5556 if (!OpInfo.AssignedRegs.Regs.empty())
Chris Lattnerdecc2672010-04-07 05:20:54 +00005557 OpInfo.AssignedRegs.AddInlineAsmOperands(
5558 InlineAsm::Kind_RegDefEarlyClobber,
Bill Wendling46ada192010-03-02 01:55:18 +00005559 false, 0, DAG,
Bill Wendling651ad132009-12-22 01:25:10 +00005560 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005561 break;
5562 }
5563 }
5564 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005565
Chris Lattnerdecc2672010-04-07 05:20:54 +00005566 // Finish up input operands. Set the input chain and add the flag last.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005567 AsmNodeOperands[0] = Chain;
5568 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005569
Dale Johannesen66978ee2009-01-31 02:22:37 +00005570 Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005571 DAG.getVTList(MVT::Other, MVT::Flag),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005572 &AsmNodeOperands[0], AsmNodeOperands.size());
5573 Flag = Chain.getValue(1);
5574
5575 // If this asm returns a register value, copy the result from that register
5576 // and set it as the value of the call.
5577 if (!RetValRegs.Regs.empty()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005578 SDValue Val = RetValRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005579 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005580
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005581 // FIXME: Why don't we do this for inline asms with MRVs?
5582 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
Owen Andersone50ed302009-08-10 22:56:29 +00005583 EVT ResultType = TLI.getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005584
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005585 // If any of the results of the inline asm is a vector, it may have the
5586 // wrong width/num elts. This can happen for register classes that can
5587 // contain multiple different value types. The preg or vreg allocated may
5588 // not have the same VT as was expected. Convert it to the right type
5589 // with bit_convert.
5590 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005591 Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005592 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005593
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005594 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005595 ResultType.isInteger() && Val.getValueType().isInteger()) {
5596 // If a result value was tied to an input value, the computed result may
5597 // have a wider width than the expected result. Extract the relevant
5598 // portion.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005599 Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005600 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005601
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005602 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Chris Lattner0c526442008-10-17 17:52:49 +00005603 }
Dan Gohman95915732008-10-18 01:03:45 +00005604
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005605 setValue(CS.getInstruction(), Val);
Dale Johannesenec65a7d2009-04-14 00:56:56 +00005606 // Don't need to use this as a chain in this case.
5607 if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty())
5608 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005609 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005610
Dan Gohman46510a72010-04-15 01:51:59 +00005611 std::vector<std::pair<SDValue, const Value *> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005612
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005613 // Process indirect outputs, first output all of the flagged copies out of
5614 // physregs.
5615 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
5616 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
Dan Gohman46510a72010-04-15 01:51:59 +00005617 const Value *Ptr = IndirectStoresToEmit[i].second;
Dale Johannesen66978ee2009-01-31 02:22:37 +00005618 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005619 Chain, &Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005620 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
5621 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005622
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005623 // Emit the non-flagged stores from the physregs.
5624 SmallVector<SDValue, 8> OutChains;
Bill Wendling651ad132009-12-22 01:25:10 +00005625 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) {
5626 SDValue Val = DAG.getStore(Chain, getCurDebugLoc(),
5627 StoresToEmit[i].first,
5628 getValue(StoresToEmit[i].second),
David Greene1e559442010-02-15 17:00:31 +00005629 StoresToEmit[i].second, 0,
5630 false, false, 0);
Bill Wendling651ad132009-12-22 01:25:10 +00005631 OutChains.push_back(Val);
Bill Wendling651ad132009-12-22 01:25:10 +00005632 }
5633
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005634 if (!OutChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00005635 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005636 &OutChains[0], OutChains.size());
Bill Wendling651ad132009-12-22 01:25:10 +00005637
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005638 DAG.setRoot(Chain);
5639}
5640
Dan Gohman46510a72010-04-15 01:51:59 +00005641void SelectionDAGBuilder::visitVAStart(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00005642 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(),
5643 MVT::Other, getRoot(),
Eric Christopher551754c2010-04-16 23:37:20 +00005644 getValue(I.getOperand(1)),
5645 DAG.getSrcValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005646}
5647
Dan Gohman46510a72010-04-15 01:51:59 +00005648void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) {
Dale Johannesena04b7572009-02-03 23:04:43 +00005649 SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(),
5650 getRoot(), getValue(I.getOperand(0)),
5651 DAG.getSrcValue(I.getOperand(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005652 setValue(&I, V);
5653 DAG.setRoot(V.getValue(1));
5654}
5655
Dan Gohman46510a72010-04-15 01:51:59 +00005656void SelectionDAGBuilder::visitVAEnd(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00005657 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(),
5658 MVT::Other, getRoot(),
Eric Christopher551754c2010-04-16 23:37:20 +00005659 getValue(I.getOperand(1)),
5660 DAG.getSrcValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005661}
5662
Dan Gohman46510a72010-04-15 01:51:59 +00005663void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00005664 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(),
5665 MVT::Other, getRoot(),
5666 getValue(I.getOperand(1)),
Eric Christopher551754c2010-04-16 23:37:20 +00005667 getValue(I.getOperand(2)),
5668 DAG.getSrcValue(I.getOperand(1)),
5669 DAG.getSrcValue(I.getOperand(2))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005670}
5671
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005672/// TargetLowering::LowerCallTo - This is the default LowerCallTo
Dan Gohman98ca4f22009-08-05 01:29:28 +00005673/// implementation, which just calls LowerCall.
5674/// FIXME: When all targets are
5675/// migrated to using LowerCall, this hook should be integrated into SDISel.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005676std::pair<SDValue, SDValue>
5677TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy,
5678 bool RetSExt, bool RetZExt, bool isVarArg,
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00005679 bool isInreg, unsigned NumFixedArgs,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00005680 CallingConv::ID CallConv, bool isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00005681 bool isReturnValueUsed,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005682 SDValue Callee,
Dan Gohmand858e902010-04-17 15:26:15 +00005683 ArgListTy &Args, SelectionDAG &DAG,
5684 DebugLoc dl) const {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005685 // Handle all of the outgoing arguments.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005686 SmallVector<ISD::OutputArg, 32> Outs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005687 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00005688 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005689 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
5690 for (unsigned Value = 0, NumValues = ValueVTs.size();
5691 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005692 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005693 const Type *ArgTy = VT.getTypeForEVT(RetTy->getContext());
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005694 SDValue Op = SDValue(Args[i].Node.getNode(),
5695 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005696 ISD::ArgFlagsTy Flags;
5697 unsigned OriginalAlignment =
5698 getTargetData()->getABITypeAlignment(ArgTy);
5699
5700 if (Args[i].isZExt)
5701 Flags.setZExt();
5702 if (Args[i].isSExt)
5703 Flags.setSExt();
5704 if (Args[i].isInReg)
5705 Flags.setInReg();
5706 if (Args[i].isSRet)
5707 Flags.setSRet();
5708 if (Args[i].isByVal) {
5709 Flags.setByVal();
5710 const PointerType *Ty = cast<PointerType>(Args[i].Ty);
5711 const Type *ElementTy = Ty->getElementType();
5712 unsigned FrameAlign = getByValTypeAlignment(ElementTy);
Duncan Sands777d2302009-05-09 07:06:46 +00005713 unsigned FrameSize = getTargetData()->getTypeAllocSize(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005714 // For ByVal, alignment should come from FE. BE will guess if this
5715 // info is not there but there are cases it cannot get right.
5716 if (Args[i].Alignment)
5717 FrameAlign = Args[i].Alignment;
5718 Flags.setByValAlign(FrameAlign);
5719 Flags.setByValSize(FrameSize);
5720 }
5721 if (Args[i].isNest)
5722 Flags.setNest();
5723 Flags.setOrigAlign(OriginalAlignment);
5724
Owen Anderson23b9b192009-08-12 00:36:31 +00005725 EVT PartVT = getRegisterType(RetTy->getContext(), VT);
5726 unsigned NumParts = getNumRegisters(RetTy->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005727 SmallVector<SDValue, 4> Parts(NumParts);
5728 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
5729
5730 if (Args[i].isSExt)
5731 ExtendKind = ISD::SIGN_EXTEND;
5732 else if (Args[i].isZExt)
5733 ExtendKind = ISD::ZERO_EXTEND;
5734
Bill Wendling46ada192010-03-02 01:55:18 +00005735 getCopyToParts(DAG, dl, Op, &Parts[0], NumParts,
Bill Wendling3ea3c242009-12-22 02:10:19 +00005736 PartVT, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005737
Dan Gohman98ca4f22009-08-05 01:29:28 +00005738 for (unsigned j = 0; j != NumParts; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005739 // if it isn't first piece, alignment must be 1
Dan Gohman98ca4f22009-08-05 01:29:28 +00005740 ISD::OutputArg MyFlags(Flags, Parts[j], i < NumFixedArgs);
5741 if (NumParts > 1 && j == 0)
5742 MyFlags.Flags.setSplit();
5743 else if (j != 0)
5744 MyFlags.Flags.setOrigAlign(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005745
Dan Gohman98ca4f22009-08-05 01:29:28 +00005746 Outs.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005747 }
5748 }
5749 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005750
Dan Gohman98ca4f22009-08-05 01:29:28 +00005751 // Handle the incoming return values from the call.
5752 SmallVector<ISD::InputArg, 32> Ins;
Owen Andersone50ed302009-08-10 22:56:29 +00005753 SmallVector<EVT, 4> RetTys;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005754 ComputeValueVTs(*this, RetTy, RetTys);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005755 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00005756 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00005757 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
5758 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005759 for (unsigned i = 0; i != NumRegs; ++i) {
5760 ISD::InputArg MyFlags;
5761 MyFlags.VT = RegisterVT;
5762 MyFlags.Used = isReturnValueUsed;
5763 if (RetSExt)
5764 MyFlags.Flags.setSExt();
5765 if (RetZExt)
5766 MyFlags.Flags.setZExt();
5767 if (isInreg)
5768 MyFlags.Flags.setInReg();
5769 Ins.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005770 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005771 }
5772
Dan Gohman98ca4f22009-08-05 01:29:28 +00005773 SmallVector<SDValue, 4> InVals;
Evan Cheng022d9e12010-02-02 23:55:14 +00005774 Chain = LowerCall(Chain, Callee, CallConv, isVarArg, isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00005775 Outs, Ins, dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00005776
5777 // Verify that the target's LowerCall behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00005778 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00005779 "LowerCall didn't return a valid chain!");
5780 assert((!isTailCall || InVals.empty()) &&
5781 "LowerCall emitted a return value for a tail call!");
5782 assert((isTailCall || InVals.size() == Ins.size()) &&
5783 "LowerCall didn't emit the correct number of values!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00005784
5785 // For a tail call, the return value is merely live-out and there aren't
5786 // any nodes in the DAG representing it. Return a special value to
5787 // indicate that a tail call has been emitted and no more Instructions
5788 // should be processed in the current block.
5789 if (isTailCall) {
5790 DAG.setRoot(Chain);
5791 return std::make_pair(SDValue(), SDValue());
5792 }
5793
Evan Chengaf1871f2010-03-11 19:38:18 +00005794 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
5795 assert(InVals[i].getNode() &&
5796 "LowerCall emitted a null value!");
5797 assert(Ins[i].VT == InVals[i].getValueType() &&
5798 "LowerCall emitted a value with the wrong type!");
5799 });
5800
Dan Gohman98ca4f22009-08-05 01:29:28 +00005801 // Collect the legal value parts into potentially illegal values
5802 // that correspond to the original function's return values.
5803 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5804 if (RetSExt)
5805 AssertOp = ISD::AssertSext;
5806 else if (RetZExt)
5807 AssertOp = ISD::AssertZext;
5808 SmallVector<SDValue, 4> ReturnValues;
5809 unsigned CurReg = 0;
5810 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00005811 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00005812 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
5813 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005814
Bill Wendling46ada192010-03-02 01:55:18 +00005815 ReturnValues.push_back(getCopyFromParts(DAG, dl, &InVals[CurReg],
Bill Wendling4533cac2010-01-28 21:51:40 +00005816 NumRegs, RegisterVT, VT,
5817 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00005818 CurReg += NumRegs;
5819 }
5820
5821 // For a function returning void, there is no return value. We can't create
5822 // such a node, so we just return a null return value in that case. In
5823 // that case, nothing will actualy look at the value.
5824 if (ReturnValues.empty())
5825 return std::make_pair(SDValue(), Chain);
5826
5827 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
5828 DAG.getVTList(&RetTys[0], RetTys.size()),
5829 &ReturnValues[0], ReturnValues.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005830 return std::make_pair(Res, Chain);
5831}
5832
Duncan Sands9fbc7e22009-01-21 09:00:29 +00005833void TargetLowering::LowerOperationWrapper(SDNode *N,
5834 SmallVectorImpl<SDValue> &Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005835 SelectionDAG &DAG) const {
Duncan Sands9fbc7e22009-01-21 09:00:29 +00005836 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00005837 if (Res.getNode())
5838 Results.push_back(Res);
5839}
5840
Dan Gohmand858e902010-04-17 15:26:15 +00005841SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Torok Edwinc23197a2009-07-14 16:55:14 +00005842 llvm_unreachable("LowerOperation not implemented for this target!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005843 return SDValue();
5844}
5845
Dan Gohman46510a72010-04-15 01:51:59 +00005846void
5847SelectionDAGBuilder::CopyValueToVirtualRegister(const Value *V, unsigned Reg) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005848 SDValue Op = getValue(V);
5849 assert((Op.getOpcode() != ISD::CopyFromReg ||
5850 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
5851 "Copy from a reg to the same reg!");
5852 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
5853
Owen Anderson23b9b192009-08-12 00:36:31 +00005854 RegsForValue RFV(V->getContext(), TLI, Reg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005855 SDValue Chain = DAG.getEntryNode();
Bill Wendling46ada192010-03-02 01:55:18 +00005856 RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), Chain, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005857 PendingExports.push_back(Chain);
5858}
5859
5860#include "llvm/CodeGen/SelectionDAGISel.h"
5861
Dan Gohman46510a72010-04-15 01:51:59 +00005862void SelectionDAGISel::LowerArguments(const BasicBlock *LLVMBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005863 // If this is the entry block, emit arguments.
Dan Gohman46510a72010-04-15 01:51:59 +00005864 const Function &F = *LLVMBB->getParent();
Dan Gohman2048b852009-11-23 18:04:58 +00005865 SelectionDAG &DAG = SDB->DAG;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005866 SDValue OldRoot = DAG.getRoot();
Dan Gohman2048b852009-11-23 18:04:58 +00005867 DebugLoc dl = SDB->getCurDebugLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005868 const TargetData *TD = TLI.getTargetData();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005869 SmallVector<ISD::InputArg, 16> Ins;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005870
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00005871 // Check whether the function can return without sret-demotion.
5872 SmallVector<EVT, 4> OutVTs;
5873 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005874 getReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005875 OutVTs, OutsFlags, TLI);
5876 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
5877
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005878 FLI.CanLowerReturn = TLI.CanLowerReturn(F.getCallingConv(), F.isVarArg(),
Bill Wendling3ea3c242009-12-22 02:10:19 +00005879 OutVTs, OutsFlags, DAG);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005880 if (!FLI.CanLowerReturn) {
5881 // Put in an sret pointer parameter before all the other parameters.
5882 SmallVector<EVT, 1> ValueVTs;
5883 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
5884
5885 // NOTE: Assuming that a pointer will never break down to more than one VT
5886 // or one register.
5887 ISD::ArgFlagsTy Flags;
5888 Flags.setSRet();
Dan Gohmanf81eca02010-04-22 20:46:50 +00005889 EVT RegisterVT = TLI.getRegisterType(*DAG.getContext(), ValueVTs[0]);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005890 ISD::InputArg RetArg(Flags, RegisterVT, true);
5891 Ins.push_back(RetArg);
5892 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00005893
Dan Gohman98ca4f22009-08-05 01:29:28 +00005894 // Set up the incoming argument description vector.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005895 unsigned Idx = 1;
Dan Gohman46510a72010-04-15 01:51:59 +00005896 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005897 I != E; ++I, ++Idx) {
Owen Andersone50ed302009-08-10 22:56:29 +00005898 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005899 ComputeValueVTs(TLI, I->getType(), ValueVTs);
5900 bool isArgValueUsed = !I->use_empty();
5901 for (unsigned Value = 0, NumValues = ValueVTs.size();
5902 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005903 EVT VT = ValueVTs[Value];
Owen Anderson1d0be152009-08-13 21:58:54 +00005904 const Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00005905 ISD::ArgFlagsTy Flags;
5906 unsigned OriginalAlignment =
5907 TD->getABITypeAlignment(ArgTy);
5908
5909 if (F.paramHasAttr(Idx, Attribute::ZExt))
5910 Flags.setZExt();
5911 if (F.paramHasAttr(Idx, Attribute::SExt))
5912 Flags.setSExt();
5913 if (F.paramHasAttr(Idx, Attribute::InReg))
5914 Flags.setInReg();
5915 if (F.paramHasAttr(Idx, Attribute::StructRet))
5916 Flags.setSRet();
5917 if (F.paramHasAttr(Idx, Attribute::ByVal)) {
5918 Flags.setByVal();
5919 const PointerType *Ty = cast<PointerType>(I->getType());
5920 const Type *ElementTy = Ty->getElementType();
5921 unsigned FrameAlign = TLI.getByValTypeAlignment(ElementTy);
5922 unsigned FrameSize = TD->getTypeAllocSize(ElementTy);
5923 // For ByVal, alignment should be passed from FE. BE will guess if
5924 // this info is not there but there are cases it cannot get right.
5925 if (F.getParamAlignment(Idx))
5926 FrameAlign = F.getParamAlignment(Idx);
5927 Flags.setByValAlign(FrameAlign);
5928 Flags.setByValSize(FrameSize);
5929 }
5930 if (F.paramHasAttr(Idx, Attribute::Nest))
5931 Flags.setNest();
5932 Flags.setOrigAlign(OriginalAlignment);
5933
Owen Anderson23b9b192009-08-12 00:36:31 +00005934 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5935 unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005936 for (unsigned i = 0; i != NumRegs; ++i) {
5937 ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed);
5938 if (NumRegs > 1 && i == 0)
5939 MyFlags.Flags.setSplit();
5940 // if it isn't first piece, alignment must be 1
5941 else if (i > 0)
5942 MyFlags.Flags.setOrigAlign(1);
5943 Ins.push_back(MyFlags);
5944 }
5945 }
5946 }
5947
5948 // Call the target to set up the argument values.
5949 SmallVector<SDValue, 8> InVals;
5950 SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(),
5951 F.isVarArg(), Ins,
5952 dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00005953
5954 // Verify that the target's LowerFormalArguments behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00005955 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00005956 "LowerFormalArguments didn't return a valid chain!");
5957 assert(InVals.size() == Ins.size() &&
5958 "LowerFormalArguments didn't emit the correct number of values!");
Bill Wendling3ea58b62009-12-22 21:35:02 +00005959 DEBUG({
5960 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
5961 assert(InVals[i].getNode() &&
5962 "LowerFormalArguments emitted a null value!");
5963 assert(Ins[i].VT == InVals[i].getValueType() &&
5964 "LowerFormalArguments emitted a value with the wrong type!");
5965 }
5966 });
Bill Wendling3ea3c242009-12-22 02:10:19 +00005967
Dan Gohman5e866062009-08-06 15:37:27 +00005968 // Update the DAG with the new chain value resulting from argument lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005969 DAG.setRoot(NewRoot);
5970
5971 // Set up the argument values.
5972 unsigned i = 0;
5973 Idx = 1;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005974 if (!FLI.CanLowerReturn) {
5975 // Create a virtual register for the sret pointer, and put in a copy
5976 // from the sret argument into it.
5977 SmallVector<EVT, 1> ValueVTs;
5978 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
5979 EVT VT = ValueVTs[0];
5980 EVT RegVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5981 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Bill Wendling46ada192010-03-02 01:55:18 +00005982 SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1,
Bill Wendling3ea3c242009-12-22 02:10:19 +00005983 RegVT, VT, AssertOp);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005984
Dan Gohman2048b852009-11-23 18:04:58 +00005985 MachineFunction& MF = SDB->DAG.getMachineFunction();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005986 MachineRegisterInfo& RegInfo = MF.getRegInfo();
5987 unsigned SRetReg = RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT));
5988 FLI.DemoteRegister = SRetReg;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005989 NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurDebugLoc(),
5990 SRetReg, ArgValue);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005991 DAG.setRoot(NewRoot);
Bill Wendling3ea3c242009-12-22 02:10:19 +00005992
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005993 // i indexes lowered arguments. Bump it past the hidden sret argument.
5994 // Idx indexes LLVM arguments. Don't touch it.
5995 ++i;
5996 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00005997
Dan Gohman46510a72010-04-15 01:51:59 +00005998 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005999 ++I, ++Idx) {
6000 SmallVector<SDValue, 4> ArgValues;
Owen Andersone50ed302009-08-10 22:56:29 +00006001 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006002 ComputeValueVTs(TLI, I->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006003 unsigned NumValues = ValueVTs.size();
Dan Gohman98ca4f22009-08-05 01:29:28 +00006004 for (unsigned Value = 0; Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006005 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00006006 EVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6007 unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006008
6009 if (!I->use_empty()) {
6010 ISD::NodeType AssertOp = ISD::DELETED_NODE;
6011 if (F.paramHasAttr(Idx, Attribute::SExt))
6012 AssertOp = ISD::AssertSext;
6013 else if (F.paramHasAttr(Idx, Attribute::ZExt))
6014 AssertOp = ISD::AssertZext;
6015
Bill Wendling46ada192010-03-02 01:55:18 +00006016 ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i],
Bill Wendling3ea3c242009-12-22 02:10:19 +00006017 NumParts, PartVT, VT,
6018 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00006019 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006020
Dan Gohman98ca4f22009-08-05 01:29:28 +00006021 i += NumParts;
6022 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006023
Dan Gohman98ca4f22009-08-05 01:29:28 +00006024 if (!I->use_empty()) {
Evan Cheng8e36a5c2010-03-29 21:27:30 +00006025 SDValue Res;
6026 if (!ArgValues.empty())
6027 Res = DAG.getMergeValues(&ArgValues[0], NumValues,
6028 SDB->getCurDebugLoc());
Bill Wendling3ea3c242009-12-22 02:10:19 +00006029 SDB->setValue(I, Res);
6030
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006031 // If this argument is live outside of the entry block, insert a copy from
6032 // whereever we got it to the vreg that other BB's will reference it as.
Dan Gohman2048b852009-11-23 18:04:58 +00006033 SDB->CopyToExportRegsIfNeeded(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006034 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006035 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006036
Dan Gohman98ca4f22009-08-05 01:29:28 +00006037 assert(i == InVals.size() && "Argument register count mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006038
6039 // Finally, if the target has anything special to do, allow it to do so.
6040 // FIXME: this should insert code into the DAG!
Dan Gohman64652652010-04-14 20:17:22 +00006041 EmitFunctionEntryCode();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006042}
6043
6044/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
6045/// ensure constants are generated when needed. Remember the virtual registers
6046/// that need to be added to the Machine PHI nodes as input. We cannot just
6047/// directly add them, because expansion might result in multiple MBB's for one
6048/// BB. As such, the start of the BB might correspond to a different MBB than
6049/// the end.
6050///
6051void
Dan Gohmanf81eca02010-04-22 20:46:50 +00006052SelectionDAGBuilder::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
Dan Gohman46510a72010-04-15 01:51:59 +00006053 const TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006054
6055 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
6056
6057 // Check successor nodes' PHI nodes that expect a constant to be available
6058 // from this block.
6059 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
Dan Gohman46510a72010-04-15 01:51:59 +00006060 const BasicBlock *SuccBB = TI->getSuccessor(succ);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006061 if (!isa<PHINode>(SuccBB->begin())) continue;
Dan Gohmanf81eca02010-04-22 20:46:50 +00006062 MachineBasicBlock *SuccMBB = FuncInfo.MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006063
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006064 // If this terminator has multiple identical successors (common for
6065 // switches), only handle each succ once.
6066 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006067
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006068 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006069
6070 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6071 // nodes and Machine PHI nodes, but the incoming operands have not been
6072 // emitted yet.
Dan Gohman46510a72010-04-15 01:51:59 +00006073 for (BasicBlock::const_iterator I = SuccBB->begin();
6074 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006075 // Ignore dead phi's.
6076 if (PN->use_empty()) continue;
6077
6078 unsigned Reg;
Dan Gohman46510a72010-04-15 01:51:59 +00006079 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006080
Dan Gohman46510a72010-04-15 01:51:59 +00006081 if (const Constant *C = dyn_cast<Constant>(PHIOp)) {
Dan Gohmanf81eca02010-04-22 20:46:50 +00006082 unsigned &RegOut = ConstantsOut[C];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006083 if (RegOut == 0) {
Dan Gohmanf81eca02010-04-22 20:46:50 +00006084 RegOut = FuncInfo.CreateRegForValue(C);
6085 CopyValueToVirtualRegister(C, RegOut);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006086 }
6087 Reg = RegOut;
6088 } else {
Dan Gohmanf81eca02010-04-22 20:46:50 +00006089 Reg = FuncInfo.ValueMap[PHIOp];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006090 if (Reg == 0) {
6091 assert(isa<AllocaInst>(PHIOp) &&
Dan Gohmanf81eca02010-04-22 20:46:50 +00006092 FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006093 "Didn't codegen value into a register!??");
Dan Gohmanf81eca02010-04-22 20:46:50 +00006094 Reg = FuncInfo.CreateRegForValue(PHIOp);
6095 CopyValueToVirtualRegister(PHIOp, Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006096 }
6097 }
6098
6099 // Remember that this register needs to added to the machine PHI node as
6100 // the input for this MBB.
Owen Andersone50ed302009-08-10 22:56:29 +00006101 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006102 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
6103 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +00006104 EVT VT = ValueVTs[vti];
Dan Gohmanf81eca02010-04-22 20:46:50 +00006105 unsigned NumRegisters = TLI.getNumRegisters(*DAG.getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006106 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
Dan Gohmanf81eca02010-04-22 20:46:50 +00006107 FuncInfo.PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006108 Reg += NumRegisters;
6109 }
6110 }
6111 }
Dan Gohmanf81eca02010-04-22 20:46:50 +00006112 ConstantsOut.clear();
Dan Gohman3df24e62008-09-03 23:12:08 +00006113}