blob: ce5a65b7a17950d3098e0f66c13fe912278d401f [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 Gohmanf0cbcd42008-09-03 16:12:24 +000033#include "llvm/CodeGen/FastISel.h"
34#include "llvm/CodeGen/GCStrategy.h"
35#include "llvm/CodeGen/GCMetadata.h"
36#include "llvm/CodeGen/MachineFunction.h"
37#include "llvm/CodeGen/MachineFrameInfo.h"
38#include "llvm/CodeGen/MachineInstrBuilder.h"
39#include "llvm/CodeGen/MachineJumpTableInfo.h"
40#include "llvm/CodeGen/MachineModuleInfo.h"
41#include "llvm/CodeGen/MachineRegisterInfo.h"
Bill Wendlingb2a42982008-11-06 02:29:10 +000042#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000043#include "llvm/CodeGen/SelectionDAG.h"
Devang Patel83489bb2009-01-13 00:35:13 +000044#include "llvm/Analysis/DebugInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000045#include "llvm/Target/TargetRegisterInfo.h"
46#include "llvm/Target/TargetData.h"
47#include "llvm/Target/TargetFrameInfo.h"
48#include "llvm/Target/TargetInstrInfo.h"
Dale Johannesen49de9822009-02-05 01:49:45 +000049#include "llvm/Target/TargetIntrinsicInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000050#include "llvm/Target/TargetLowering.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000051#include "llvm/Target/TargetOptions.h"
52#include "llvm/Support/Compiler.h"
Mikhail Glushenkov2388a582009-01-16 07:02:28 +000053#include "llvm/Support/CommandLine.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000054#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000055#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000056#include "llvm/Support/MathExtras.h"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +000057#include "llvm/Support/raw_ostream.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000058#include <algorithm>
59using namespace llvm;
60
Dale Johannesen601d3c02008-09-05 01:48:15 +000061/// LimitFloatPrecision - Generate low-precision inline sequences for
62/// some float libcalls (6, 8 or 12 bits).
63static unsigned LimitFloatPrecision;
64
65static cl::opt<unsigned, true>
66LimitFPPrecision("limit-float-precision",
67 cl::desc("Generate low-precision inline sequences "
68 "for some float libcalls"),
69 cl::location(LimitFloatPrecision),
70 cl::init(0));
71
Dan Gohmanf9bd4502009-11-23 17:46:23 +000072namespace {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000073 /// RegsForValue - This struct represents the registers (physical or virtual)
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +000074 /// that a particular set of values is assigned, and the type information
75 /// about the value. The most common situation is to represent one value at a
76 /// time, but struct or array values are handled element-wise as multiple
77 /// values. The splitting of aggregates is performed recursively, so that we
78 /// never have aggregate-typed registers. The values at this point do not
79 /// necessarily have legal types, so each value may require one or more
80 /// registers of some legal type.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000081 ///
Dan Gohmanf9bd4502009-11-23 17:46:23 +000082 struct RegsForValue {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000083 /// TLI - The TargetLowering object.
84 ///
85 const TargetLowering *TLI;
86
87 /// ValueVTs - The value types of the values, which may not be legal, and
88 /// may need be promoted or synthesized from one or more registers.
89 ///
Owen Andersone50ed302009-08-10 22:56:29 +000090 SmallVector<EVT, 4> ValueVTs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000091
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000092 /// RegVTs - The value types of the registers. This is the same size as
93 /// ValueVTs and it records, for each value, what the type of the assigned
94 /// register or registers are. (Individual values are never synthesized
95 /// from more than one type of register.)
96 ///
97 /// With virtual registers, the contents of RegVTs is redundant with TLI's
98 /// getRegisterType member function, however when with physical registers
99 /// it is necessary to have a separate record of the types.
100 ///
Owen Andersone50ed302009-08-10 22:56:29 +0000101 SmallVector<EVT, 4> RegVTs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000102
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000103 /// Regs - This list holds the registers assigned to the values.
104 /// Each legal or promoted value requires one register, and each
105 /// expanded value requires multiple registers.
106 ///
107 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000108
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000109 RegsForValue() : TLI(0) {}
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000110
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000111 RegsForValue(const TargetLowering &tli,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000112 const SmallVector<unsigned, 4> &regs,
Owen Andersone50ed302009-08-10 22:56:29 +0000113 EVT regvt, EVT valuevt)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000114 : TLI(&tli), ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs) {}
115 RegsForValue(const TargetLowering &tli,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000116 const SmallVector<unsigned, 4> &regs,
Owen Andersone50ed302009-08-10 22:56:29 +0000117 const SmallVector<EVT, 4> &regvts,
118 const SmallVector<EVT, 4> &valuevts)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000119 : TLI(&tli), ValueVTs(valuevts), RegVTs(regvts), Regs(regs) {}
Owen Anderson23b9b192009-08-12 00:36:31 +0000120 RegsForValue(LLVMContext &Context, const TargetLowering &tli,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000121 unsigned Reg, const Type *Ty) : TLI(&tli) {
122 ComputeValueVTs(tli, Ty, ValueVTs);
123
124 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +0000125 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +0000126 unsigned NumRegs = TLI->getNumRegisters(Context, ValueVT);
127 EVT RegisterVT = TLI->getRegisterType(Context, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000128 for (unsigned i = 0; i != NumRegs; ++i)
129 Regs.push_back(Reg + i);
130 RegVTs.push_back(RegisterVT);
131 Reg += NumRegs;
132 }
133 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000134
Evan Cheng8112b532010-02-10 01:21:02 +0000135 /// areValueTypesLegal - Return true if types of all the values are legal.
136 bool areValueTypesLegal() {
137 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
138 EVT RegisterVT = RegVTs[Value];
139 if (!TLI->isTypeLegal(RegisterVT))
140 return false;
141 }
142 return true;
143 }
144
145
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000146 /// append - Add the specified values to this one.
147 void append(const RegsForValue &RHS) {
148 TLI = RHS.TLI;
149 ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end());
150 RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end());
151 Regs.append(RHS.Regs.begin(), RHS.Regs.end());
152 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000153
154
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000155 /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000156 /// this value and returns the result as a ValueVTs value. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000157 /// Chain/Flag as the input and updates them for the output Chain/Flag.
158 /// If the Flag pointer is NULL, no flag is used.
Bill Wendling46ada192010-03-02 01:55:18 +0000159 SDValue getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl,
Bill Wendlingec72e322009-12-22 01:11:43 +0000160 SDValue &Chain, SDValue *Flag) const;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000161
162 /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000163 /// specified value into the registers specified by this object. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000164 /// Chain/Flag as the input and updates them for the output Chain/Flag.
165 /// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +0000166 void getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
Bill Wendling46ada192010-03-02 01:55:18 +0000167 SDValue &Chain, SDValue *Flag) const;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000168
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000169 /// AddInlineAsmOperands - Add this value to the specified inlineasm node
Evan Cheng697cbbf2009-03-20 18:03:34 +0000170 /// operand list. This adds the code marker, matching input operand index
171 /// (if applicable), and includes the number of values added into it.
Chris Lattnerdecc2672010-04-07 05:20:54 +0000172 void AddInlineAsmOperands(unsigned Kind,
Evan Cheng697cbbf2009-03-20 18:03:34 +0000173 bool HasMatching, unsigned MatchingIdx,
Bill Wendling46ada192010-03-02 01:55:18 +0000174 SelectionDAG &DAG,
Bill Wendling651ad132009-12-22 01:25:10 +0000175 std::vector<SDValue> &Ops) const;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000176 };
177}
178
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000179/// getCopyFromParts - Create a value that contains the specified legal parts
180/// combined into the value they represent. If the parts combine to a type
181/// larger then ValueVT then AssertOp can be used to specify whether the extra
182/// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT
183/// (ISD::AssertSext).
Bill Wendling46ada192010-03-02 01:55:18 +0000184static SDValue getCopyFromParts(SelectionDAG &DAG, DebugLoc dl,
Dale Johannesen66978ee2009-01-31 02:22:37 +0000185 const SDValue *Parts,
Owen Andersone50ed302009-08-10 22:56:29 +0000186 unsigned NumParts, EVT PartVT, EVT ValueVT,
Duncan Sands0b3aa262009-01-28 14:42:54 +0000187 ISD::NodeType AssertOp = ISD::DELETED_NODE) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000188 assert(NumParts > 0 && "No parts to assemble!");
Dan Gohmane9530ec2009-01-15 16:58:17 +0000189 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000190 SDValue Val = Parts[0];
191
192 if (NumParts > 1) {
193 // Assemble the value from multiple parts.
Eli Friedman2ac8b322009-05-20 06:02:09 +0000194 if (!ValueVT.isVector() && ValueVT.isInteger()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000195 unsigned PartBits = PartVT.getSizeInBits();
196 unsigned ValueBits = ValueVT.getSizeInBits();
197
198 // Assemble the power of 2 part.
199 unsigned RoundParts = NumParts & (NumParts - 1) ?
200 1 << Log2_32(NumParts) : NumParts;
201 unsigned RoundBits = PartBits * RoundParts;
Owen Andersone50ed302009-08-10 22:56:29 +0000202 EVT RoundVT = RoundBits == ValueBits ?
Owen Anderson23b9b192009-08-12 00:36:31 +0000203 ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000204 SDValue Lo, Hi;
205
Owen Anderson23b9b192009-08-12 00:36:31 +0000206 EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2);
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000207
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000208 if (RoundParts > 2) {
Bill Wendling46ada192010-03-02 01:55:18 +0000209 Lo = getCopyFromParts(DAG, dl, Parts, RoundParts / 2,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000210 PartVT, HalfVT);
Bill Wendling46ada192010-03-02 01:55:18 +0000211 Hi = getCopyFromParts(DAG, dl, Parts + RoundParts / 2,
Bill Wendling3ea3c242009-12-22 02:10:19 +0000212 RoundParts / 2, PartVT, HalfVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000213 } else {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000214 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[0]);
215 Hi = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[1]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000216 }
Bill Wendling3ea3c242009-12-22 02:10:19 +0000217
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000218 if (TLI.isBigEndian())
219 std::swap(Lo, Hi);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000220
Dale Johannesen66978ee2009-01-31 02:22:37 +0000221 Val = DAG.getNode(ISD::BUILD_PAIR, dl, RoundVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000222
223 if (RoundParts < NumParts) {
224 // Assemble the trailing non-power-of-2 part.
225 unsigned OddParts = NumParts - RoundParts;
Owen Anderson23b9b192009-08-12 00:36:31 +0000226 EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits);
Bill Wendling46ada192010-03-02 01:55:18 +0000227 Hi = getCopyFromParts(DAG, dl,
Bill Wendling3ea3c242009-12-22 02:10:19 +0000228 Parts + RoundParts, OddParts, PartVT, OddVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000229
230 // Combine the round and odd parts.
231 Lo = Val;
232 if (TLI.isBigEndian())
233 std::swap(Lo, Hi);
Owen Anderson23b9b192009-08-12 00:36:31 +0000234 EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000235 Hi = DAG.getNode(ISD::ANY_EXTEND, dl, TotalVT, Hi);
236 Hi = DAG.getNode(ISD::SHL, dl, TotalVT, Hi,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000237 DAG.getConstant(Lo.getValueType().getSizeInBits(),
Duncan Sands92abc622009-01-31 15:50:11 +0000238 TLI.getPointerTy()));
Dale Johannesen66978ee2009-01-31 02:22:37 +0000239 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, TotalVT, Lo);
240 Val = DAG.getNode(ISD::OR, dl, TotalVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000241 }
Eli Friedman2ac8b322009-05-20 06:02:09 +0000242 } else if (ValueVT.isVector()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000243 // Handle a multi-element vector.
Owen Andersone50ed302009-08-10 22:56:29 +0000244 EVT IntermediateVT, RegisterVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000245 unsigned NumIntermediates;
246 unsigned NumRegs =
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000247 TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT,
Owen Anderson23b9b192009-08-12 00:36:31 +0000248 NumIntermediates, RegisterVT);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000249 assert(NumRegs == NumParts
250 && "Part count doesn't match vector breakdown!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000251 NumParts = NumRegs; // Silence a compiler warning.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000252 assert(RegisterVT == PartVT
253 && "Part type doesn't match vector breakdown!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000254 assert(RegisterVT == Parts[0].getValueType() &&
255 "Part type doesn't match part!");
256
257 // Assemble the parts into intermediate operands.
258 SmallVector<SDValue, 8> Ops(NumIntermediates);
259 if (NumIntermediates == NumParts) {
260 // If the register was not expanded, truncate or copy the value,
261 // as appropriate.
262 for (unsigned i = 0; i != NumParts; ++i)
Bill Wendling46ada192010-03-02 01:55:18 +0000263 Ops[i] = getCopyFromParts(DAG, dl, &Parts[i], 1,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000264 PartVT, IntermediateVT);
265 } else if (NumParts > 0) {
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000266 // If the intermediate type was expanded, build the intermediate
267 // operands from the parts.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000268 assert(NumParts % NumIntermediates == 0 &&
269 "Must expand into a divisible number of parts!");
270 unsigned Factor = NumParts / NumIntermediates;
271 for (unsigned i = 0; i != NumIntermediates; ++i)
Bill Wendling46ada192010-03-02 01:55:18 +0000272 Ops[i] = getCopyFromParts(DAG, dl, &Parts[i * Factor], Factor,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000273 PartVT, IntermediateVT);
274 }
275
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000276 // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the
277 // intermediate operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000278 Val = DAG.getNode(IntermediateVT.isVector() ?
Dale Johannesen66978ee2009-01-31 02:22:37 +0000279 ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000280 ValueVT, &Ops[0], NumIntermediates);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000281 } else if (PartVT.isFloatingPoint()) {
282 // FP split into multiple FP parts (for ppcf128)
Owen Anderson825b72b2009-08-11 20:47:22 +0000283 assert(ValueVT == EVT(MVT::ppcf128) && PartVT == EVT(MVT::f64) &&
Eli Friedman2ac8b322009-05-20 06:02:09 +0000284 "Unexpected split");
285 SDValue Lo, Hi;
Owen Anderson825b72b2009-08-11 20:47:22 +0000286 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[0]);
287 Hi = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[1]);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000288 if (TLI.isBigEndian())
289 std::swap(Lo, Hi);
290 Val = DAG.getNode(ISD::BUILD_PAIR, dl, ValueVT, Lo, Hi);
291 } else {
292 // FP split into integer parts (soft fp)
293 assert(ValueVT.isFloatingPoint() && PartVT.isInteger() &&
294 !PartVT.isVector() && "Unexpected split");
Owen Anderson23b9b192009-08-12 00:36:31 +0000295 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
Bill Wendling46ada192010-03-02 01:55:18 +0000296 Val = getCopyFromParts(DAG, dl, Parts, NumParts, PartVT, IntVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000297 }
298 }
299
300 // There is now one part, held in Val. Correct it to match ValueVT.
301 PartVT = Val.getValueType();
302
303 if (PartVT == ValueVT)
304 return Val;
305
306 if (PartVT.isVector()) {
307 assert(ValueVT.isVector() && "Unknown vector conversion!");
Bill Wendling4533cac2010-01-28 21:51:40 +0000308 return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000309 }
310
311 if (ValueVT.isVector()) {
312 assert(ValueVT.getVectorElementType() == PartVT &&
313 ValueVT.getVectorNumElements() == 1 &&
314 "Only trivial scalar-to-vector conversions should get here!");
Bill Wendling4533cac2010-01-28 21:51:40 +0000315 return DAG.getNode(ISD::BUILD_VECTOR, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000316 }
317
318 if (PartVT.isInteger() &&
319 ValueVT.isInteger()) {
320 if (ValueVT.bitsLT(PartVT)) {
321 // For a truncate, see if we have any information to
322 // indicate whether the truncated bits will always be
323 // zero or sign-extension.
324 if (AssertOp != ISD::DELETED_NODE)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000325 Val = DAG.getNode(AssertOp, dl, PartVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000326 DAG.getValueType(ValueVT));
Bill Wendling4533cac2010-01-28 21:51:40 +0000327 return DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000328 } else {
Bill Wendling4533cac2010-01-28 21:51:40 +0000329 return DAG.getNode(ISD::ANY_EXTEND, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000330 }
331 }
332
333 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
Bill Wendling3ea3c242009-12-22 02:10:19 +0000334 if (ValueVT.bitsLT(Val.getValueType())) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000335 // FP_ROUND's are always exact here.
Bill Wendling4533cac2010-01-28 21:51:40 +0000336 return DAG.getNode(ISD::FP_ROUND, dl, ValueVT, Val,
337 DAG.getIntPtrConstant(1));
Bill Wendling3ea3c242009-12-22 02:10:19 +0000338 }
339
Bill Wendling4533cac2010-01-28 21:51:40 +0000340 return DAG.getNode(ISD::FP_EXTEND, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000341 }
342
Bill Wendling4533cac2010-01-28 21:51:40 +0000343 if (PartVT.getSizeInBits() == ValueVT.getSizeInBits())
344 return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000345
Torok Edwinc23197a2009-07-14 16:55:14 +0000346 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000347 return SDValue();
348}
349
350/// getCopyToParts - Create a series of nodes that contain the specified value
351/// split into legal parts. If the parts contain more bits than Val, then, for
352/// integers, ExtendKind can be used to specify how to generate the extra bits.
Bill Wendling46ada192010-03-02 01:55:18 +0000353static void getCopyToParts(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling3ea3c242009-12-22 02:10:19 +0000354 SDValue Val, SDValue *Parts, unsigned NumParts,
355 EVT PartVT,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000356 ISD::NodeType ExtendKind = ISD::ANY_EXTEND) {
Dan Gohmane9530ec2009-01-15 16:58:17 +0000357 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +0000358 EVT PtrVT = TLI.getPointerTy();
359 EVT ValueVT = Val.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000360 unsigned PartBits = PartVT.getSizeInBits();
Dale Johannesen8a36f502009-02-25 22:39:13 +0000361 unsigned OrigNumParts = NumParts;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000362 assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!");
363
364 if (!NumParts)
365 return;
366
367 if (!ValueVT.isVector()) {
368 if (PartVT == ValueVT) {
369 assert(NumParts == 1 && "No-op copy with multiple parts!");
370 Parts[0] = Val;
371 return;
372 }
373
374 if (NumParts * PartBits > ValueVT.getSizeInBits()) {
375 // If the parts cover more bits than the value has, promote the value.
376 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
377 assert(NumParts == 1 && "Do not know what to promote to!");
Dale Johannesen66978ee2009-01-31 02:22:37 +0000378 Val = DAG.getNode(ISD::FP_EXTEND, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000379 } else if (PartVT.isInteger() && ValueVT.isInteger()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000380 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000381 Val = DAG.getNode(ExtendKind, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000382 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000383 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000384 }
385 } else if (PartBits == ValueVT.getSizeInBits()) {
386 // Different types of the same size.
387 assert(NumParts == 1 && PartVT != ValueVT);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000388 Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000389 } else if (NumParts * PartBits < ValueVT.getSizeInBits()) {
390 // If the parts cover less bits than value has, truncate the value.
391 if (PartVT.isInteger() && ValueVT.isInteger()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000392 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000393 Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000394 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000395 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000396 }
397 }
398
399 // The value may have changed - recompute ValueVT.
400 ValueVT = Val.getValueType();
401 assert(NumParts * PartBits == ValueVT.getSizeInBits() &&
402 "Failed to tile the value with PartVT!");
403
404 if (NumParts == 1) {
405 assert(PartVT == ValueVT && "Type conversion failed!");
406 Parts[0] = Val;
407 return;
408 }
409
410 // Expand the value into multiple parts.
411 if (NumParts & (NumParts - 1)) {
412 // The number of parts is not a power of 2. Split off and copy the tail.
413 assert(PartVT.isInteger() && ValueVT.isInteger() &&
414 "Do not know what to expand to!");
415 unsigned RoundParts = 1 << Log2_32(NumParts);
416 unsigned RoundBits = RoundParts * PartBits;
417 unsigned OddParts = NumParts - RoundParts;
Dale Johannesen66978ee2009-01-31 02:22:37 +0000418 SDValue OddVal = DAG.getNode(ISD::SRL, dl, ValueVT, Val,
Duncan Sands0b3aa262009-01-28 14:42:54 +0000419 DAG.getConstant(RoundBits,
Duncan Sands92abc622009-01-31 15:50:11 +0000420 TLI.getPointerTy()));
Bill Wendling46ada192010-03-02 01:55:18 +0000421 getCopyToParts(DAG, dl, OddVal, Parts + RoundParts,
Bill Wendling3ea3c242009-12-22 02:10:19 +0000422 OddParts, PartVT);
423
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000424 if (TLI.isBigEndian())
425 // The odd parts were reversed by getCopyToParts - unreverse them.
426 std::reverse(Parts + RoundParts, Parts + NumParts);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000427
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000428 NumParts = RoundParts;
Owen Anderson23b9b192009-08-12 00:36:31 +0000429 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000430 Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000431 }
432
433 // The number of parts is a power of 2. Repeatedly bisect the value using
434 // EXTRACT_ELEMENT.
Scott Michelfdc40a02009-02-17 22:15:04 +0000435 Parts[0] = DAG.getNode(ISD::BIT_CONVERT, dl,
Chris Lattnerf031e8a2010-01-01 03:32:16 +0000436 EVT::getIntegerVT(*DAG.getContext(),
437 ValueVT.getSizeInBits()),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000438 Val);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000439
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000440 for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) {
441 for (unsigned i = 0; i < NumParts; i += StepSize) {
442 unsigned ThisBits = StepSize * PartBits / 2;
Owen Anderson23b9b192009-08-12 00:36:31 +0000443 EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000444 SDValue &Part0 = Parts[i];
445 SDValue &Part1 = Parts[i+StepSize/2];
446
Scott Michelfdc40a02009-02-17 22:15:04 +0000447 Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000448 ThisVT, Part0,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000449 DAG.getConstant(1, PtrVT));
Scott Michelfdc40a02009-02-17 22:15:04 +0000450 Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000451 ThisVT, Part0,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000452 DAG.getConstant(0, PtrVT));
453
454 if (ThisBits == PartBits && ThisVT != PartVT) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000455 Part0 = DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000456 PartVT, Part0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000457 Part1 = DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000458 PartVT, Part1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000459 }
460 }
461 }
462
463 if (TLI.isBigEndian())
Dale Johannesen8a36f502009-02-25 22:39:13 +0000464 std::reverse(Parts, Parts + OrigNumParts);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000465
466 return;
467 }
468
469 // Vector ValueVT.
470 if (NumParts == 1) {
471 if (PartVT != ValueVT) {
Bob Wilson5afffae2009-12-18 01:03:29 +0000472 if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000473 Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000474 } else {
475 assert(ValueVT.getVectorElementType() == PartVT &&
476 ValueVT.getVectorNumElements() == 1 &&
477 "Only trivial vector-to-scalar conversions should get here!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000478 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000479 PartVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000480 DAG.getConstant(0, PtrVT));
481 }
482 }
483
484 Parts[0] = Val;
485 return;
486 }
487
488 // Handle a multi-element vector.
Owen Andersone50ed302009-08-10 22:56:29 +0000489 EVT IntermediateVT, RegisterVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000490 unsigned NumIntermediates;
Owen Anderson23b9b192009-08-12 00:36:31 +0000491 unsigned NumRegs = TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT,
492 IntermediateVT, NumIntermediates, RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000493 unsigned NumElements = ValueVT.getVectorNumElements();
494
495 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
496 NumParts = NumRegs; // Silence a compiler warning.
497 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
498
499 // Split the vector into intermediate operands.
500 SmallVector<SDValue, 8> Ops(NumIntermediates);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000501 for (unsigned i = 0; i != NumIntermediates; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000502 if (IntermediateVT.isVector())
Scott Michelfdc40a02009-02-17 22:15:04 +0000503 Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000504 IntermediateVT, Val,
505 DAG.getConstant(i * (NumElements / NumIntermediates),
506 PtrVT));
507 else
Scott Michelfdc40a02009-02-17 22:15:04 +0000508 Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000509 IntermediateVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000510 DAG.getConstant(i, PtrVT));
Bill Wendling3ea3c242009-12-22 02:10:19 +0000511 }
512
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000513 // Split the intermediate operands into legal parts.
514 if (NumParts == NumIntermediates) {
515 // If the register was not expanded, promote or copy the value,
516 // as appropriate.
517 for (unsigned i = 0; i != NumParts; ++i)
Bill Wendling46ada192010-03-02 01:55:18 +0000518 getCopyToParts(DAG, dl, Ops[i], &Parts[i], 1, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000519 } else if (NumParts > 0) {
520 // If the intermediate type was expanded, split each the value into
521 // legal parts.
522 assert(NumParts % NumIntermediates == 0 &&
523 "Must expand into a divisible number of parts!");
524 unsigned Factor = NumParts / NumIntermediates;
525 for (unsigned i = 0; i != NumIntermediates; ++i)
Bill Wendling46ada192010-03-02 01:55:18 +0000526 getCopyToParts(DAG, dl, Ops[i], &Parts[i*Factor], Factor, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000527 }
528}
529
530
Dan Gohman2048b852009-11-23 18:04:58 +0000531void SelectionDAGBuilder::init(GCFunctionInfo *gfi, AliasAnalysis &aa) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000532 AA = &aa;
533 GFI = gfi;
534 TD = DAG.getTarget().getTargetData();
535}
536
Dan Gohmanb02b62a2010-04-14 18:24:06 +0000537/// clear - Clear out the current SelectionDAG and the associated
Dan Gohman2048b852009-11-23 18:04:58 +0000538/// state and prepare this SelectionDAGBuilder object to be used
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000539/// for a new block. This doesn't clear out information about
540/// additional blocks that are needed to complete switch lowering
541/// or PHI node updating; that information is cleared out as it is
542/// consumed.
Dan Gohman2048b852009-11-23 18:04:58 +0000543void SelectionDAGBuilder::clear() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000544 NodeMap.clear();
545 PendingLoads.clear();
546 PendingExports.clear();
Evan Chengfb2e7522009-09-18 21:02:19 +0000547 EdgeMapping.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000548 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 Gohman8ba3aa72010-04-20 00:48:35 +0000617 CurDebugLoc = I.getDebugLoc();
618
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000619 visit(I.getOpcode(), I);
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000620
Dan Gohman92884f72010-04-20 15:03:56 +0000621 if (!isa<TerminatorInst>(&I) && !HasTailCall)
622 CopyToExportRegsIfNeeded(&I);
623
Dan Gohman8ba3aa72010-04-20 00:48:35 +0000624 CurDebugLoc = DebugLoc();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000625}
626
Dan Gohmanba5be5c2010-04-20 15:00:41 +0000627void SelectionDAGBuilder::visitPHI(const PHINode &) {
628 llvm_unreachable("SelectionDAGBuilder shouldn't visit PHI nodes!");
629}
630
Dan Gohman46510a72010-04-15 01:51:59 +0000631void SelectionDAGBuilder::visit(unsigned Opcode, const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000632 // Note: this doesn't use InstVisitor, because it has to work with
633 // ConstantExpr's in addition to instructions.
634 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000635 default: llvm_unreachable("Unknown instruction type encountered!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000636 // Build the switch statement using the Instruction.def file.
637#define HANDLE_INST(NUM, OPCODE, CLASS) \
Bill Wendling4533cac2010-01-28 21:51:40 +0000638 case Instruction::OPCODE: visit##OPCODE((CLASS&)I); break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000639#include "llvm/Instruction.def"
640 }
Bill Wendling4533cac2010-01-28 21:51:40 +0000641
642 // Assign the ordering to the freshly created DAG nodes.
643 if (NodeMap.count(&I)) {
644 ++SDNodeOrder;
645 AssignOrderingToNode(getValue(&I).getNode());
646 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000647}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000648
Dan Gohman2048b852009-11-23 18:04:58 +0000649SDValue SelectionDAGBuilder::getValue(const Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000650 SDValue &N = NodeMap[V];
651 if (N.getNode()) return N;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000652
Dan Gohman383b5f62010-04-17 15:32:28 +0000653 if (const Constant *C = dyn_cast<Constant>(V)) {
Owen Andersone50ed302009-08-10 22:56:29 +0000654 EVT VT = TLI.getValueType(V->getType(), true);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000655
Dan Gohman383b5f62010-04-17 15:32:28 +0000656 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000657 return N = DAG.getConstant(*CI, VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000658
Dan Gohman383b5f62010-04-17 15:32:28 +0000659 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000660 return N = DAG.getGlobalAddress(GV, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000661
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000662 if (isa<ConstantPointerNull>(C))
663 return N = DAG.getConstant(0, TLI.getPointerTy());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000664
Dan Gohman383b5f62010-04-17 15:32:28 +0000665 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000666 return N = DAG.getConstantFP(*CFP, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000667
Nate Begeman9008ca62009-04-27 18:41:29 +0000668 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
Dale Johannesene8d72302009-02-06 23:05:02 +0000669 return N = DAG.getUNDEF(VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000670
Dan Gohman383b5f62010-04-17 15:32:28 +0000671 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000672 visit(CE->getOpcode(), *CE);
673 SDValue N1 = NodeMap[V];
Dan Gohmanac7d05c2010-04-16 16:55:18 +0000674 assert(N1.getNode() && "visit didn't populate the NodeMap!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000675 return N1;
676 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000677
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000678 if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
679 SmallVector<SDValue, 4> Constants;
680 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
681 OI != OE; ++OI) {
682 SDNode *Val = getValue(*OI).getNode();
Dan Gohmaned48caf2009-09-08 01:44:02 +0000683 // If the operand is an empty aggregate, there are no values.
684 if (!Val) continue;
685 // Add each leaf value from the operand to the Constants list
686 // to form a flattened list of all the values.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000687 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
688 Constants.push_back(SDValue(Val, i));
689 }
Bill Wendling87710f02009-12-21 23:47:40 +0000690
Bill Wendling4533cac2010-01-28 21:51:40 +0000691 return DAG.getMergeValues(&Constants[0], Constants.size(),
692 getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000693 }
694
Duncan Sands1df98592010-02-16 11:11:14 +0000695 if (C->getType()->isStructTy() || C->getType()->isArrayTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000696 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
697 "Unknown struct or array constant!");
698
Owen Andersone50ed302009-08-10 22:56:29 +0000699 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000700 ComputeValueVTs(TLI, C->getType(), ValueVTs);
701 unsigned NumElts = ValueVTs.size();
702 if (NumElts == 0)
703 return SDValue(); // empty struct
704 SmallVector<SDValue, 4> Constants(NumElts);
705 for (unsigned i = 0; i != NumElts; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +0000706 EVT EltVT = ValueVTs[i];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000707 if (isa<UndefValue>(C))
Dale Johannesene8d72302009-02-06 23:05:02 +0000708 Constants[i] = DAG.getUNDEF(EltVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000709 else if (EltVT.isFloatingPoint())
710 Constants[i] = DAG.getConstantFP(0, EltVT);
711 else
712 Constants[i] = DAG.getConstant(0, EltVT);
713 }
Bill Wendling87710f02009-12-21 23:47:40 +0000714
Bill Wendling4533cac2010-01-28 21:51:40 +0000715 return DAG.getMergeValues(&Constants[0], NumElts,
716 getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000717 }
718
Dan Gohman383b5f62010-04-17 15:32:28 +0000719 if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
Dan Gohman29cbade2009-11-20 23:18:13 +0000720 return DAG.getBlockAddress(BA, VT);
Dan Gohman8c2b5252009-10-30 01:27:03 +0000721
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000722 const VectorType *VecTy = cast<VectorType>(V->getType());
723 unsigned NumElements = VecTy->getNumElements();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000724
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000725 // Now that we know the number and type of the elements, get that number of
726 // elements into the Ops array based on what kind of constant it is.
727 SmallVector<SDValue, 16> Ops;
Dan Gohman383b5f62010-04-17 15:32:28 +0000728 if (const ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000729 for (unsigned i = 0; i != NumElements; ++i)
730 Ops.push_back(getValue(CP->getOperand(i)));
731 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +0000732 assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!");
Owen Andersone50ed302009-08-10 22:56:29 +0000733 EVT EltVT = TLI.getValueType(VecTy->getElementType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000734
735 SDValue Op;
Nate Begeman9008ca62009-04-27 18:41:29 +0000736 if (EltVT.isFloatingPoint())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000737 Op = DAG.getConstantFP(0, EltVT);
738 else
739 Op = DAG.getConstant(0, EltVT);
740 Ops.assign(NumElements, Op);
741 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000742
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000743 // Create a BUILD_VECTOR node.
Bill Wendling4533cac2010-01-28 21:51:40 +0000744 return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
745 VT, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000746 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000747
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000748 // If this is a static alloca, generate it as the frameindex instead of
749 // computation.
750 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
751 DenseMap<const AllocaInst*, int>::iterator SI =
752 FuncInfo.StaticAllocaMap.find(AI);
753 if (SI != FuncInfo.StaticAllocaMap.end())
754 return DAG.getFrameIndex(SI->second, TLI.getPointerTy());
755 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000756
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000757 unsigned InReg = FuncInfo.ValueMap[V];
758 assert(InReg && "Value not in map!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000759
Owen Anderson23b9b192009-08-12 00:36:31 +0000760 RegsForValue RFV(*DAG.getContext(), TLI, InReg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000761 SDValue Chain = DAG.getEntryNode();
Bill Wendling46ada192010-03-02 01:55:18 +0000762 return RFV.getCopyFromRegs(DAG, getCurDebugLoc(), Chain, NULL);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000763}
764
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000765/// Get the EVTs and ArgFlags collections that represent the legalized return
766/// type of the given function. This does not require a DAG or a return value,
767/// and is suitable for use before any DAGs for the function are constructed.
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000768static void getReturnInfo(const Type* ReturnType,
769 Attributes attr, SmallVectorImpl<EVT> &OutVTs,
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000770 SmallVectorImpl<ISD::ArgFlagsTy> &OutFlags,
Dan Gohmand858e902010-04-17 15:26:15 +0000771 const TargetLowering &TLI,
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000772 SmallVectorImpl<uint64_t> *Offsets = 0) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000773 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000774 ComputeValueVTs(TLI, ReturnType, ValueVTs);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000775 unsigned NumValues = ValueVTs.size();
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000776 if (NumValues == 0) return;
777 unsigned Offset = 0;
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000778
779 for (unsigned j = 0, f = NumValues; j != f; ++j) {
780 EVT VT = ValueVTs[j];
781 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000782
783 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000784 ExtendKind = ISD::SIGN_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000785 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000786 ExtendKind = ISD::ZERO_EXTEND;
787
788 // FIXME: C calling convention requires the return type to be promoted to
789 // at least 32-bit. But this is not necessary for non-C calling
790 // conventions. The frontend should mark functions whose return values
791 // require promoting with signext or zeroext attributes.
792 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000793 EVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000794 if (VT.bitsLT(MinVT))
795 VT = MinVT;
796 }
797
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000798 unsigned NumParts = TLI.getNumRegisters(ReturnType->getContext(), VT);
799 EVT PartVT = TLI.getRegisterType(ReturnType->getContext(), VT);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000800 unsigned PartSize = TLI.getTargetData()->getTypeAllocSize(
801 PartVT.getTypeForEVT(ReturnType->getContext()));
802
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000803 // 'inreg' on function refers to return value
804 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000805 if (attr & Attribute::InReg)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000806 Flags.setInReg();
807
808 // Propagate extension type if any
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000809 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000810 Flags.setSExt();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000811 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000812 Flags.setZExt();
813
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000814 for (unsigned i = 0; i < NumParts; ++i) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000815 OutVTs.push_back(PartVT);
816 OutFlags.push_back(Flags);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000817 if (Offsets)
818 {
819 Offsets->push_back(Offset);
820 Offset += PartSize;
821 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000822 }
823 }
824}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000825
Dan Gohman46510a72010-04-15 01:51:59 +0000826void SelectionDAGBuilder::visitRet(const ReturnInst &I) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000827 SDValue Chain = getControlRoot();
828 SmallVector<ISD::OutputArg, 8> Outs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000829 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000830
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000831 if (!FLI.CanLowerReturn) {
832 unsigned DemoteReg = FLI.DemoteRegister;
833 const Function *F = I.getParent()->getParent();
834
835 // Emit a store of the return value through the virtual register.
836 // Leave Outs empty so that LowerReturn won't try to load return
837 // registers the usual way.
838 SmallVector<EVT, 1> PtrValueVTs;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000839 ComputeValueVTs(TLI, PointerType::getUnqual(F->getReturnType()),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000840 PtrValueVTs);
841
842 SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]);
843 SDValue RetOp = getValue(I.getOperand(0));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000844
Owen Andersone50ed302009-08-10 22:56:29 +0000845 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000846 SmallVector<uint64_t, 4> Offsets;
847 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets);
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000848 unsigned NumValues = ValueVTs.size();
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000849
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000850 SmallVector<SDValue, 4> Chains(NumValues);
851 EVT PtrVT = PtrValueVTs[0];
Bill Wendling87710f02009-12-21 23:47:40 +0000852 for (unsigned i = 0; i != NumValues; ++i) {
853 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, RetPtr,
854 DAG.getConstant(Offsets[i], PtrVT));
855 Chains[i] =
856 DAG.getStore(Chain, getCurDebugLoc(),
857 SDValue(RetOp.getNode(), RetOp.getResNo() + i),
David Greene1e559442010-02-15 17:00:31 +0000858 Add, NULL, Offsets[i], false, false, 0);
Bill Wendling87710f02009-12-21 23:47:40 +0000859 }
860
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000861 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
862 MVT::Other, &Chains[0], NumValues);
Chris Lattner25d58372010-02-28 18:53:13 +0000863 } else if (I.getNumOperands() != 0) {
864 SmallVector<EVT, 4> ValueVTs;
865 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs);
866 unsigned NumValues = ValueVTs.size();
867 if (NumValues) {
868 SDValue RetOp = getValue(I.getOperand(0));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000869 for (unsigned j = 0, f = NumValues; j != f; ++j) {
870 EVT VT = ValueVTs[j];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000871
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000872 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000873
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000874 const Function *F = I.getParent()->getParent();
875 if (F->paramHasAttr(0, Attribute::SExt))
876 ExtendKind = ISD::SIGN_EXTEND;
877 else if (F->paramHasAttr(0, Attribute::ZExt))
878 ExtendKind = ISD::ZERO_EXTEND;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000879
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000880 // FIXME: C calling convention requires the return type to be promoted
881 // to at least 32-bit. But this is not necessary for non-C calling
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000882 // conventions. The frontend should mark functions whose return values
883 // require promoting with signext or zeroext attributes.
884 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
885 EVT MinVT = TLI.getRegisterType(*DAG.getContext(), MVT::i32);
886 if (VT.bitsLT(MinVT))
887 VT = MinVT;
888 }
889
890 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), VT);
891 EVT PartVT = TLI.getRegisterType(*DAG.getContext(), VT);
892 SmallVector<SDValue, 4> Parts(NumParts);
Bill Wendling46ada192010-03-02 01:55:18 +0000893 getCopyToParts(DAG, getCurDebugLoc(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000894 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
895 &Parts[0], NumParts, PartVT, ExtendKind);
896
897 // 'inreg' on function refers to return value
898 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
899 if (F->paramHasAttr(0, Attribute::InReg))
900 Flags.setInReg();
901
902 // Propagate extension type if any
903 if (F->paramHasAttr(0, Attribute::SExt))
904 Flags.setSExt();
905 else if (F->paramHasAttr(0, Attribute::ZExt))
906 Flags.setZExt();
907
908 for (unsigned i = 0; i < NumParts; ++i)
909 Outs.push_back(ISD::OutputArg(Flags, Parts[i], /*isfixed=*/true));
Evan Cheng3927f432009-03-25 20:20:11 +0000910 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000911 }
912 }
Dan Gohman98ca4f22009-08-05 01:29:28 +0000913
914 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000915 CallingConv::ID CallConv =
916 DAG.getMachineFunction().getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000917 Chain = TLI.LowerReturn(Chain, CallConv, isVarArg,
918 Outs, getCurDebugLoc(), DAG);
Dan Gohman5e866062009-08-06 15:37:27 +0000919
920 // Verify that the target's LowerReturn behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +0000921 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +0000922 "LowerReturn didn't return a valid chain!");
923
924 // Update the DAG with the new chain value resulting from return lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000925 DAG.setRoot(Chain);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000926}
927
Dan Gohmanad62f532009-04-23 23:13:24 +0000928/// CopyToExportRegsIfNeeded - If the given value has virtual registers
929/// created for it, emit nodes to copy the value into the virtual
930/// registers.
Dan Gohman46510a72010-04-15 01:51:59 +0000931void SelectionDAGBuilder::CopyToExportRegsIfNeeded(const Value *V) {
Dan Gohman33b7a292010-04-16 17:15:02 +0000932 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
933 if (VMI != FuncInfo.ValueMap.end()) {
934 assert(!V->use_empty() && "Unused value assigned virtual registers!");
935 CopyValueToVirtualRegister(V, VMI->second);
Dan Gohmanad62f532009-04-23 23:13:24 +0000936 }
937}
938
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000939/// ExportFromCurrentBlock - If this condition isn't known to be exported from
940/// the current basic block, add it to ValueMap now so that we'll get a
941/// CopyTo/FromReg.
Dan Gohman46510a72010-04-15 01:51:59 +0000942void SelectionDAGBuilder::ExportFromCurrentBlock(const Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000943 // No need to export constants.
944 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000945
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000946 // Already exported?
947 if (FuncInfo.isExportedInst(V)) return;
948
949 unsigned Reg = FuncInfo.InitializeRegForValue(V);
950 CopyValueToVirtualRegister(V, Reg);
951}
952
Dan Gohman46510a72010-04-15 01:51:59 +0000953bool SelectionDAGBuilder::isExportableFromCurrentBlock(const Value *V,
Dan Gohman2048b852009-11-23 18:04:58 +0000954 const BasicBlock *FromBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000955 // The operands of the setcc have to be in this block. We don't know
956 // how to export them from some other block.
Dan Gohman46510a72010-04-15 01:51:59 +0000957 if (const Instruction *VI = dyn_cast<Instruction>(V)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000958 // Can export from current BB.
959 if (VI->getParent() == FromBB)
960 return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000961
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000962 // Is already exported, noop.
963 return FuncInfo.isExportedInst(V);
964 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000965
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000966 // If this is an argument, we can export it if the BB is the entry block or
967 // if it is already exported.
968 if (isa<Argument>(V)) {
969 if (FromBB == &FromBB->getParent()->getEntryBlock())
970 return true;
971
972 // Otherwise, can only export this if it is already exported.
973 return FuncInfo.isExportedInst(V);
974 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000975
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000976 // Otherwise, constants can always be exported.
977 return true;
978}
979
980static bool InBlock(const Value *V, const BasicBlock *BB) {
981 if (const Instruction *I = dyn_cast<Instruction>(V))
982 return I->getParent() == BB;
983 return true;
984}
985
Dan Gohmanc2277342008-10-17 21:16:08 +0000986/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
987/// This function emits a branch and is used at the leaves of an OR or an
988/// AND operator tree.
989///
990void
Dan Gohman46510a72010-04-15 01:51:59 +0000991SelectionDAGBuilder::EmitBranchForMergedCondition(const Value *Cond,
Dan Gohman2048b852009-11-23 18:04:58 +0000992 MachineBasicBlock *TBB,
993 MachineBasicBlock *FBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +0000994 MachineBasicBlock *CurBB,
995 MachineBasicBlock *SwitchBB) {
Dan Gohmanc2277342008-10-17 21:16:08 +0000996 const BasicBlock *BB = CurBB->getBasicBlock();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000997
Dan Gohmanc2277342008-10-17 21:16:08 +0000998 // If the leaf of the tree is a comparison, merge the condition into
999 // the caseblock.
Dan Gohman46510a72010-04-15 01:51:59 +00001000 if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001001 // The operands of the cmp have to be in this block. We don't know
1002 // how to export them from some other block. If this is the first block
1003 // of the sequence, no exporting is needed.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001004 if (CurBB == SwitchBB ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001005 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
1006 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001007 ISD::CondCode Condition;
Dan Gohman46510a72010-04-15 01:51:59 +00001008 if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001009 Condition = getICmpCondCode(IC->getPredicate());
Dan Gohman46510a72010-04-15 01:51:59 +00001010 } else if (const FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001011 Condition = getFCmpCondCode(FC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001012 } else {
1013 Condition = ISD::SETEQ; // silence warning.
Torok Edwinc23197a2009-07-14 16:55:14 +00001014 llvm_unreachable("Unknown compare instruction");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001015 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001016
1017 CaseBlock CB(Condition, BOp->getOperand(0),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001018 BOp->getOperand(1), NULL, TBB, FBB, CurBB);
1019 SwitchCases.push_back(CB);
1020 return;
1021 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001022 }
1023
1024 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001025 CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanc2277342008-10-17 21:16:08 +00001026 NULL, TBB, FBB, CurBB);
1027 SwitchCases.push_back(CB);
1028}
1029
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001030/// FindMergedConditions - If Cond is an expression like
Dan Gohman46510a72010-04-15 01:51:59 +00001031void SelectionDAGBuilder::FindMergedConditions(const Value *Cond,
Dan Gohman2048b852009-11-23 18:04:58 +00001032 MachineBasicBlock *TBB,
1033 MachineBasicBlock *FBB,
1034 MachineBasicBlock *CurBB,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001035 MachineBasicBlock *SwitchBB,
Dan Gohman2048b852009-11-23 18:04:58 +00001036 unsigned Opc) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001037 // If this node is not part of the or/and tree, emit it as a branch.
Dan Gohman46510a72010-04-15 01:51:59 +00001038 const Instruction *BOp = dyn_cast<Instruction>(Cond);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001039 if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001040 (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
1041 BOp->getParent() != CurBB->getBasicBlock() ||
1042 !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1043 !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001044 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001045 return;
1046 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001047
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001048 // Create TmpBB after CurBB.
1049 MachineFunction::iterator BBI = CurBB;
1050 MachineFunction &MF = DAG.getMachineFunction();
1051 MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
1052 CurBB->getParent()->insert(++BBI, TmpBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001053
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001054 if (Opc == Instruction::Or) {
1055 // Codegen X | Y as:
1056 // jmp_if_X TBB
1057 // jmp TmpBB
1058 // TmpBB:
1059 // jmp_if_Y TBB
1060 // jmp FBB
1061 //
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001062
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001063 // Emit the LHS condition.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001064 FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, SwitchBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001065
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001066 // Emit the RHS condition into TmpBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001067 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001068 } else {
1069 assert(Opc == Instruction::And && "Unknown merge op!");
1070 // Codegen X & Y as:
1071 // jmp_if_X TmpBB
1072 // jmp FBB
1073 // TmpBB:
1074 // jmp_if_Y TBB
1075 // jmp FBB
1076 //
1077 // This requires creation of TmpBB after CurBB.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001078
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001079 // Emit the LHS condition.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001080 FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, SwitchBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001081
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001082 // Emit the RHS condition into TmpBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001083 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, SwitchBB, Opc);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001084 }
1085}
1086
1087/// If the set of cases should be emitted as a series of branches, return true.
1088/// If we should emit this as a bunch of and/or'd together conditions, return
1089/// false.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001090bool
Dan Gohman2048b852009-11-23 18:04:58 +00001091SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001092 if (Cases.size() != 2) return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001093
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001094 // If this is two comparisons of the same values or'd or and'd together, they
1095 // will get folded into a single comparison, so don't emit two blocks.
1096 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1097 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1098 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1099 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1100 return false;
1101 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001102
Chris Lattner133ce872010-01-02 00:00:03 +00001103 // Handle: (X != null) | (Y != null) --> (X|Y) != 0
1104 // Handle: (X == null) & (Y == null) --> (X|Y) == 0
1105 if (Cases[0].CmpRHS == Cases[1].CmpRHS &&
1106 Cases[0].CC == Cases[1].CC &&
1107 isa<Constant>(Cases[0].CmpRHS) &&
1108 cast<Constant>(Cases[0].CmpRHS)->isNullValue()) {
1109 if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB)
1110 return false;
1111 if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB)
1112 return false;
1113 }
1114
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001115 return true;
1116}
1117
Dan Gohman46510a72010-04-15 01:51:59 +00001118void SelectionDAGBuilder::visitBr(const BranchInst &I) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001119 MachineBasicBlock *BrMBB = FuncInfo.MBBMap[I.getParent()];
1120
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001121 // Update machine-CFG edges.
1122 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1123
1124 // Figure out which block is immediately after the current one.
1125 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001126 MachineFunction::iterator BBI = BrMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001127 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001128 NextBlock = BBI;
1129
1130 if (I.isUnconditional()) {
1131 // Update machine-CFG edges.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001132 BrMBB->addSuccessor(Succ0MBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001133
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001134 // If this is not a fall-through branch, emit the branch.
Bill Wendling4533cac2010-01-28 21:51:40 +00001135 if (Succ0MBB != NextBlock)
1136 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001137 MVT::Other, getControlRoot(),
Bill Wendling4533cac2010-01-28 21:51:40 +00001138 DAG.getBasicBlock(Succ0MBB)));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001139
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001140 return;
1141 }
1142
1143 // If this condition is one of the special cases we handle, do special stuff
1144 // now.
Dan Gohman46510a72010-04-15 01:51:59 +00001145 const Value *CondVal = I.getCondition();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001146 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1147
1148 // If this is a series of conditions that are or'd or and'd together, emit
1149 // this as a sequence of branches instead of setcc's with and/or operations.
1150 // For example, instead of something like:
1151 // cmp A, B
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001152 // C = seteq
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001153 // cmp D, E
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001154 // F = setle
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001155 // or C, F
1156 // jnz foo
1157 // Emit:
1158 // cmp A, B
1159 // je foo
1160 // cmp D, E
1161 // jle foo
1162 //
Dan Gohman46510a72010-04-15 01:51:59 +00001163 if (const BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001164 if (BOp->hasOneUse() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001165 (BOp->getOpcode() == Instruction::And ||
1166 BOp->getOpcode() == Instruction::Or)) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001167 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, BrMBB, BrMBB,
1168 BOp->getOpcode());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001169 // If the compares in later blocks need to use values not currently
1170 // exported from this block, export them now. This block should always
1171 // be the first entry.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001172 assert(SwitchCases[0].ThisBB == BrMBB && "Unexpected lowering!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001173
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001174 // Allow some cases to be rejected.
1175 if (ShouldEmitAsBranches(SwitchCases)) {
1176 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1177 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1178 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1179 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001180
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001181 // Emit the branch for this block.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001182 visitSwitchCase(SwitchCases[0], BrMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001183 SwitchCases.erase(SwitchCases.begin());
1184 return;
1185 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001186
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001187 // Okay, we decided not to do this, remove any inserted MBB's and clear
1188 // SwitchCases.
1189 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001190 FuncInfo.MF->erase(SwitchCases[i].ThisBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001191
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001192 SwitchCases.clear();
1193 }
1194 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001195
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001196 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001197 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohman99be8ae2010-04-19 22:41:47 +00001198 NULL, Succ0MBB, Succ1MBB, BrMBB);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001199
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001200 // Use visitSwitchCase to actually insert the fast branch sequence for this
1201 // cond branch.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001202 visitSwitchCase(CB, BrMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001203}
1204
1205/// visitSwitchCase - Emits the necessary code to represent a single node in
1206/// the binary search tree resulting from lowering a switch instruction.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001207void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB,
1208 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001209 SDValue Cond;
1210 SDValue CondLHS = getValue(CB.CmpLHS);
Dale Johannesenf5d97892009-02-04 01:48:28 +00001211 DebugLoc dl = getCurDebugLoc();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001212
1213 // Build the setcc now.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001214 if (CB.CmpMHS == NULL) {
1215 // Fold "(X == true)" to X and "(X == false)" to !X to
1216 // handle common cases produced by branch lowering.
Owen Anderson5defacc2009-07-31 17:39:07 +00001217 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001218 CB.CC == ISD::SETEQ)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001219 Cond = CondLHS;
Owen Anderson5defacc2009-07-31 17:39:07 +00001220 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001221 CB.CC == ISD::SETEQ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001222 SDValue True = DAG.getConstant(1, CondLHS.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001223 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001224 } else
Owen Anderson825b72b2009-08-11 20:47:22 +00001225 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001226 } else {
1227 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
1228
Anton Korobeynikov23218582008-12-23 22:25:27 +00001229 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
1230 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001231
1232 SDValue CmpOp = getValue(CB.CmpMHS);
Owen Andersone50ed302009-08-10 22:56:29 +00001233 EVT VT = CmpOp.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001234
1235 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001236 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT),
Dale Johannesenf5d97892009-02-04 01:48:28 +00001237 ISD::SETLE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001238 } else {
Dale Johannesenf5d97892009-02-04 01:48:28 +00001239 SDValue SUB = DAG.getNode(ISD::SUB, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001240 VT, CmpOp, DAG.getConstant(Low, VT));
Owen Anderson825b72b2009-08-11 20:47:22 +00001241 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001242 DAG.getConstant(High-Low, VT), ISD::SETULE);
1243 }
1244 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001245
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001246 // Update successor info
Dan Gohman99be8ae2010-04-19 22:41:47 +00001247 SwitchBB->addSuccessor(CB.TrueBB);
1248 SwitchBB->addSuccessor(CB.FalseBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001249
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001250 // Set NextBlock to be the MBB immediately after the current one, if any.
1251 // This is used to avoid emitting unnecessary branches to the next block.
1252 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001253 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001254 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001255 NextBlock = BBI;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001256
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001257 // If the lhs block is the next block, invert the condition so that we can
1258 // fall through to the lhs instead of the rhs block.
1259 if (CB.TrueBB == NextBlock) {
1260 std::swap(CB.TrueBB, CB.FalseBB);
1261 SDValue True = DAG.getConstant(1, Cond.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001262 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001263 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001264
Dale Johannesenf5d97892009-02-04 01:48:28 +00001265 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001266 MVT::Other, getControlRoot(), Cond,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001267 DAG.getBasicBlock(CB.TrueBB));
Bill Wendling87710f02009-12-21 23:47:40 +00001268
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001269 // If the branch was constant folded, fix up the CFG.
1270 if (BrCond.getOpcode() == ISD::BR) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001271 SwitchBB->removeSuccessor(CB.FalseBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001272 } else {
1273 // Otherwise, go ahead and insert the false branch.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001274 if (BrCond == getControlRoot())
Dan Gohman99be8ae2010-04-19 22:41:47 +00001275 SwitchBB->removeSuccessor(CB.TrueBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001276
Bill Wendling4533cac2010-01-28 21:51:40 +00001277 if (CB.FalseBB != NextBlock)
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001278 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
1279 DAG.getBasicBlock(CB.FalseBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001280 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001281
1282 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001283}
1284
1285/// visitJumpTable - Emit JumpTable node in the current MBB
Dan Gohman2048b852009-11-23 18:04:58 +00001286void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001287 // Emit the code for the jump table
1288 assert(JT.Reg != -1U && "Should lower JT Header first!");
Owen Andersone50ed302009-08-10 22:56:29 +00001289 EVT PTy = TLI.getPointerTy();
Dale Johannesena04b7572009-02-03 23:04:43 +00001290 SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(),
1291 JT.Reg, PTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001292 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001293 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurDebugLoc(),
1294 MVT::Other, Index.getValue(1),
1295 Table, Index);
1296 DAG.setRoot(BrJumpTable);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001297}
1298
1299/// visitJumpTableHeader - This function emits necessary code to produce index
1300/// in the JumpTable from switch case.
Dan Gohman2048b852009-11-23 18:04:58 +00001301void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001302 JumpTableHeader &JTH,
1303 MachineBasicBlock *SwitchBB) {
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001304 // Subtract the lowest switch case value from the value being switched on and
1305 // conditional branch to default mbb if the result is greater than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001306 // difference between smallest and largest cases.
1307 SDValue SwitchOp = getValue(JTH.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001308 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001309 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001310 DAG.getConstant(JTH.First, VT));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001311
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001312 // The SDNode we just created, which holds the value being switched on minus
Dan Gohmanf451cb82010-02-10 16:03:48 +00001313 // the smallest case value, needs to be copied to a virtual register so it
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001314 // can be used as an index into the jump table in a subsequent basic block.
1315 // This value may be smaller or larger than the target's pointer type, and
1316 // therefore require extension or truncating.
Bill Wendling87710f02009-12-21 23:47:40 +00001317 SwitchOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(), TLI.getPointerTy());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001318
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001319 unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001320 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1321 JumpTableReg, SwitchOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001322 JT.Reg = JumpTableReg;
1323
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001324 // Emit the range check for the jump table, and branch to the default block
1325 // for the switch statement if the value being switched on exceeds the largest
1326 // case in the switch.
Dale Johannesenf5d97892009-02-04 01:48:28 +00001327 SDValue CMP = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001328 TLI.getSetCCResultType(Sub.getValueType()), Sub,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001329 DAG.getConstant(JTH.Last-JTH.First,VT),
1330 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001331
1332 // Set NextBlock to be the MBB immediately after the current one, if any.
1333 // This is used to avoid emitting unnecessary branches to the next block.
1334 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001335 MachineFunction::iterator BBI = SwitchBB;
Bill Wendling87710f02009-12-21 23:47:40 +00001336
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001337 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001338 NextBlock = BBI;
1339
Dale Johannesen66978ee2009-01-31 02:22:37 +00001340 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001341 MVT::Other, CopyTo, CMP,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001342 DAG.getBasicBlock(JT.Default));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001343
Bill Wendling4533cac2010-01-28 21:51:40 +00001344 if (JT.MBB != NextBlock)
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001345 BrCond = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond,
1346 DAG.getBasicBlock(JT.MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001347
Bill Wendling87710f02009-12-21 23:47:40 +00001348 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001349}
1350
1351/// visitBitTestHeader - This function emits necessary code to produce value
1352/// suitable for "bit tests"
Dan Gohman99be8ae2010-04-19 22:41:47 +00001353void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B,
1354 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001355 // Subtract the minimum value
1356 SDValue SwitchOp = getValue(B.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001357 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001358 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001359 DAG.getConstant(B.First, VT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001360
1361 // Check range
Dale Johannesenf5d97892009-02-04 01:48:28 +00001362 SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001363 TLI.getSetCCResultType(Sub.getValueType()),
1364 Sub, DAG.getConstant(B.Range, VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001365 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001366
Bill Wendling87710f02009-12-21 23:47:40 +00001367 SDValue ShiftOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(),
1368 TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001369
Duncan Sands92abc622009-01-31 15:50:11 +00001370 B.Reg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001371 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1372 B.Reg, ShiftOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001373
1374 // Set NextBlock to be the MBB immediately after the current one, if any.
1375 // This is used to avoid emitting unnecessary branches to the next block.
1376 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001377 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001378 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001379 NextBlock = BBI;
1380
1381 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1382
Dan Gohman99be8ae2010-04-19 22:41:47 +00001383 SwitchBB->addSuccessor(B.Default);
1384 SwitchBB->addSuccessor(MBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001385
Dale Johannesen66978ee2009-01-31 02:22:37 +00001386 SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001387 MVT::Other, CopyTo, RangeCmp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001388 DAG.getBasicBlock(B.Default));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001389
Bill Wendling4533cac2010-01-28 21:51:40 +00001390 if (MBB != NextBlock)
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001391 BrRange = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo,
1392 DAG.getBasicBlock(MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001393
Bill Wendling87710f02009-12-21 23:47:40 +00001394 DAG.setRoot(BrRange);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001395}
1396
1397/// visitBitTestCase - this function produces one "bit test"
Dan Gohman2048b852009-11-23 18:04:58 +00001398void SelectionDAGBuilder::visitBitTestCase(MachineBasicBlock* NextMBB,
1399 unsigned Reg,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001400 BitTestCase &B,
1401 MachineBasicBlock *SwitchBB) {
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001402 // Make desired shift
Dale Johannesena04b7572009-02-03 23:04:43 +00001403 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), Reg,
Duncan Sands92abc622009-01-31 15:50:11 +00001404 TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001405 SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001406 TLI.getPointerTy(),
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001407 DAG.getConstant(1, TLI.getPointerTy()),
1408 ShiftOp);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001409
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001410 // Emit bit tests and jumps
Scott Michelfdc40a02009-02-17 22:15:04 +00001411 SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001412 TLI.getPointerTy(), SwitchVal,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001413 DAG.getConstant(B.Mask, TLI.getPointerTy()));
Dale Johannesenf5d97892009-02-04 01:48:28 +00001414 SDValue AndCmp = DAG.getSetCC(getCurDebugLoc(),
1415 TLI.getSetCCResultType(AndOp.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00001416 AndOp, DAG.getConstant(0, TLI.getPointerTy()),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001417 ISD::SETNE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001418
Dan Gohman99be8ae2010-04-19 22:41:47 +00001419 SwitchBB->addSuccessor(B.TargetBB);
1420 SwitchBB->addSuccessor(NextMBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001421
Dale Johannesen66978ee2009-01-31 02:22:37 +00001422 SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001423 MVT::Other, getControlRoot(),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001424 AndCmp, DAG.getBasicBlock(B.TargetBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001425
1426 // Set NextBlock to be the MBB immediately after the current one, if any.
1427 // This is used to avoid emitting unnecessary branches to the next block.
1428 MachineBasicBlock *NextBlock = 0;
Dan Gohman99be8ae2010-04-19 22:41:47 +00001429 MachineFunction::iterator BBI = SwitchBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001430 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001431 NextBlock = BBI;
1432
Bill Wendling4533cac2010-01-28 21:51:40 +00001433 if (NextMBB != NextBlock)
Bill Wendling0777e922009-12-21 21:59:52 +00001434 BrAnd = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd,
1435 DAG.getBasicBlock(NextMBB));
Bill Wendling0777e922009-12-21 21:59:52 +00001436
Bill Wendling87710f02009-12-21 23:47:40 +00001437 DAG.setRoot(BrAnd);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001438}
1439
Dan Gohman46510a72010-04-15 01:51:59 +00001440void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001441 MachineBasicBlock *InvokeMBB = FuncInfo.MBBMap[I.getParent()];
1442
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001443 // Retrieve successors.
1444 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1445 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1446
Gabor Greifb67e6b32009-01-15 11:10:44 +00001447 const Value *Callee(I.getCalledValue());
1448 if (isa<InlineAsm>(Callee))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001449 visitInlineAsm(&I);
1450 else
Gabor Greifb67e6b32009-01-15 11:10:44 +00001451 LowerCallTo(&I, getValue(Callee), false, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001452
1453 // If the value of the invoke is used outside of its defining block, make it
1454 // available as a virtual register.
Dan Gohmanad62f532009-04-23 23:13:24 +00001455 CopyToExportRegsIfNeeded(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001456
1457 // Update successor info
Dan Gohman99be8ae2010-04-19 22:41:47 +00001458 InvokeMBB->addSuccessor(Return);
1459 InvokeMBB->addSuccessor(LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001460
1461 // Drop into normal successor.
Bill Wendling4533cac2010-01-28 21:51:40 +00001462 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
1463 MVT::Other, getControlRoot(),
1464 DAG.getBasicBlock(Return)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001465}
1466
Dan Gohman46510a72010-04-15 01:51:59 +00001467void SelectionDAGBuilder::visitUnwind(const UnwindInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001468}
1469
1470/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
1471/// small case ranges).
Dan Gohman2048b852009-11-23 18:04:58 +00001472bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR,
1473 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001474 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001475 MachineBasicBlock *Default,
1476 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001477 Case& BackCase = *(CR.Range.second-1);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001478
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001479 // Size is the number of Cases represented by this range.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001480 size_t Size = CR.Range.second - CR.Range.first;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001481 if (Size > 3)
Anton Korobeynikov23218582008-12-23 22:25:27 +00001482 return false;
1483
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001484 // Get the MachineFunction which holds the current MBB. This is used when
1485 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001486 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001487
1488 // Figure out which block is immediately after the current one.
1489 MachineBasicBlock *NextBlock = 0;
1490 MachineFunction::iterator BBI = CR.CaseBB;
1491
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001492 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001493 NextBlock = BBI;
1494
1495 // TODO: If any two of the cases has the same destination, and if one value
1496 // is the same as the other, but has one bit unset that the other has set,
1497 // use bit manipulation to do two compares at once. For example:
1498 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
Anton Korobeynikov23218582008-12-23 22:25:27 +00001499
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001500 // Rearrange the case blocks so that the last one falls through if possible.
1501 if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
1502 // The last case block won't fall through into 'NextBlock' if we emit the
1503 // branches in this order. See if rearranging a case value would help.
1504 for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) {
1505 if (I->BB == NextBlock) {
1506 std::swap(*I, BackCase);
1507 break;
1508 }
1509 }
1510 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001511
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001512 // Create a CaseBlock record representing a conditional branch to
1513 // the Case's target mbb if the value being switched on SV is equal
1514 // to C.
1515 MachineBasicBlock *CurBlock = CR.CaseBB;
1516 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
1517 MachineBasicBlock *FallThrough;
1518 if (I != E-1) {
1519 FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
1520 CurMF->insert(BBI, FallThrough);
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001521
1522 // Put SV in a virtual register to make it available from the new blocks.
1523 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001524 } else {
1525 // If the last case doesn't match, go to the default block.
1526 FallThrough = Default;
1527 }
1528
Dan Gohman46510a72010-04-15 01:51:59 +00001529 const Value *RHS, *LHS, *MHS;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001530 ISD::CondCode CC;
1531 if (I->High == I->Low) {
1532 // This is just small small case range :) containing exactly 1 case
1533 CC = ISD::SETEQ;
1534 LHS = SV; RHS = I->High; MHS = NULL;
1535 } else {
1536 CC = ISD::SETLE;
1537 LHS = I->Low; MHS = SV; RHS = I->High;
1538 }
1539 CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001540
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001541 // If emitting the first comparison, just call visitSwitchCase to emit the
1542 // code into the current block. Otherwise, push the CaseBlock onto the
1543 // vector to be later processed by SDISel, and insert the node's MBB
1544 // before the next MBB.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001545 if (CurBlock == SwitchBB)
1546 visitSwitchCase(CB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001547 else
1548 SwitchCases.push_back(CB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001549
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001550 CurBlock = FallThrough;
1551 }
1552
1553 return true;
1554}
1555
1556static inline bool areJTsAllowed(const TargetLowering &TLI) {
1557 return !DisableJumpTables &&
Owen Anderson825b72b2009-08-11 20:47:22 +00001558 (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
1559 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001560}
Anton Korobeynikov23218582008-12-23 22:25:27 +00001561
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001562static APInt ComputeRange(const APInt &First, const APInt &Last) {
1563 APInt LastExt(Last), FirstExt(First);
1564 uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1;
1565 LastExt.sext(BitWidth); FirstExt.sext(BitWidth);
1566 return (LastExt - FirstExt + 1ULL);
1567}
1568
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001569/// handleJTSwitchCase - Emit jumptable for current switch case range
Dan Gohman2048b852009-11-23 18:04:58 +00001570bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec& CR,
1571 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001572 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001573 MachineBasicBlock* Default,
1574 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001575 Case& FrontCase = *CR.Range.first;
1576 Case& BackCase = *(CR.Range.second-1);
1577
Chris Lattnere880efe2009-11-07 07:50:34 +00001578 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1579 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001580
Chris Lattnere880efe2009-11-07 07:50:34 +00001581 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001582 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1583 I!=E; ++I)
1584 TSize += I->size();
1585
Dan Gohmane0567812010-04-08 23:03:40 +00001586 if (!areJTsAllowed(TLI) || TSize.ult(4))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001587 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001588
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001589 APInt Range = ComputeRange(First, Last);
Chris Lattnere880efe2009-11-07 07:50:34 +00001590 double Density = TSize.roundToDouble() / Range.roundToDouble();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001591 if (Density < 0.4)
1592 return false;
1593
David Greene4b69d992010-01-05 01:24:57 +00001594 DEBUG(dbgs() << "Lowering jump table\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001595 << "First entry: " << First << ". Last entry: " << Last << '\n'
1596 << "Range: " << Range
1597 << "Size: " << TSize << ". Density: " << Density << "\n\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001598
1599 // Get the MachineFunction which holds the current MBB. This is used when
1600 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001601 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001602
1603 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001604 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001605 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001606
1607 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1608
1609 // Create a new basic block to hold the code for loading the address
1610 // of the jump table, and jumping to it. Update successor information;
1611 // we will either branch to the default case for the switch, or the jump
1612 // table.
1613 MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1614 CurMF->insert(BBI, JumpTableBB);
1615 CR.CaseBB->addSuccessor(Default);
1616 CR.CaseBB->addSuccessor(JumpTableBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001617
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001618 // Build a vector of destination BBs, corresponding to each target
1619 // of the jump table. If the value of the jump table slot corresponds to
1620 // a case statement, push the case's BB onto the vector, otherwise, push
1621 // the default BB.
1622 std::vector<MachineBasicBlock*> DestBBs;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001623 APInt TEI = First;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001624 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
Chris Lattner071c62f2010-01-25 23:26:13 +00001625 const APInt &Low = cast<ConstantInt>(I->Low)->getValue();
1626 const APInt &High = cast<ConstantInt>(I->High)->getValue();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001627
1628 if (Low.sle(TEI) && TEI.sle(High)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001629 DestBBs.push_back(I->BB);
1630 if (TEI==High)
1631 ++I;
1632 } else {
1633 DestBBs.push_back(Default);
1634 }
1635 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001636
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001637 // Update successor info. Add one edge to each unique successor.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001638 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
1639 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001640 E = DestBBs.end(); I != E; ++I) {
1641 if (!SuccsHandled[(*I)->getNumber()]) {
1642 SuccsHandled[(*I)->getNumber()] = true;
1643 JumpTableBB->addSuccessor(*I);
1644 }
1645 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001646
Bob Wilsond1ec31d2010-03-18 18:42:41 +00001647 // Create a jump table index for this jump table.
Chris Lattner071c62f2010-01-25 23:26:13 +00001648 unsigned JTEncoding = TLI.getJumpTableEncoding();
1649 unsigned JTI = CurMF->getOrCreateJumpTableInfo(JTEncoding)
Bob Wilsond1ec31d2010-03-18 18:42:41 +00001650 ->createJumpTableIndex(DestBBs);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001651
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001652 // Set the jump table information so that we can codegen it as a second
1653 // MachineBasicBlock
1654 JumpTable JT(-1U, JTI, JumpTableBB, Default);
Dan Gohman99be8ae2010-04-19 22:41:47 +00001655 JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == SwitchBB));
1656 if (CR.CaseBB == SwitchBB)
1657 visitJumpTableHeader(JT, JTH, SwitchBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001658
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001659 JTCases.push_back(JumpTableBlock(JTH, JT));
1660
1661 return true;
1662}
1663
1664/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
1665/// 2 subtrees.
Dan Gohman2048b852009-11-23 18:04:58 +00001666bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR,
1667 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001668 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001669 MachineBasicBlock *Default,
1670 MachineBasicBlock *SwitchBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001671 // Get the MachineFunction which holds the current MBB. This is used when
1672 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001673 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001674
1675 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001676 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001677 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001678
1679 Case& FrontCase = *CR.Range.first;
1680 Case& BackCase = *(CR.Range.second-1);
1681 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1682
1683 // Size is the number of Cases represented by this range.
1684 unsigned Size = CR.Range.second - CR.Range.first;
1685
Chris Lattnere880efe2009-11-07 07:50:34 +00001686 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1687 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001688 double FMetric = 0;
1689 CaseItr Pivot = CR.Range.first + Size/2;
1690
1691 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
1692 // (heuristically) allow us to emit JumpTable's later.
Chris Lattnere880efe2009-11-07 07:50:34 +00001693 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001694 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1695 I!=E; ++I)
1696 TSize += I->size();
1697
Chris Lattnere880efe2009-11-07 07:50:34 +00001698 APInt LSize = FrontCase.size();
1699 APInt RSize = TSize-LSize;
David Greene4b69d992010-01-05 01:24:57 +00001700 DEBUG(dbgs() << "Selecting best pivot: \n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001701 << "First: " << First << ", Last: " << Last <<'\n'
1702 << "LSize: " << LSize << ", RSize: " << RSize << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001703 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
1704 J!=E; ++I, ++J) {
Chris Lattnere880efe2009-11-07 07:50:34 +00001705 const APInt &LEnd = cast<ConstantInt>(I->High)->getValue();
1706 const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001707 APInt Range = ComputeRange(LEnd, RBegin);
1708 assert((Range - 2ULL).isNonNegative() &&
1709 "Invalid case distance");
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001710 double LDensity = (double)LSize.roundToDouble() /
Chris Lattnere880efe2009-11-07 07:50:34 +00001711 (LEnd - First + 1ULL).roundToDouble();
1712 double RDensity = (double)RSize.roundToDouble() /
1713 (Last - RBegin + 1ULL).roundToDouble();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001714 double Metric = Range.logBase2()*(LDensity+RDensity);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001715 // Should always split in some non-trivial place
David Greene4b69d992010-01-05 01:24:57 +00001716 DEBUG(dbgs() <<"=>Step\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001717 << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n'
1718 << "LDensity: " << LDensity
1719 << ", RDensity: " << RDensity << '\n'
1720 << "Metric: " << Metric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001721 if (FMetric < Metric) {
1722 Pivot = J;
1723 FMetric = Metric;
David Greene4b69d992010-01-05 01:24:57 +00001724 DEBUG(dbgs() << "Current metric set to: " << FMetric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001725 }
1726
1727 LSize += J->size();
1728 RSize -= J->size();
1729 }
1730 if (areJTsAllowed(TLI)) {
1731 // If our case is dense we *really* should handle it earlier!
1732 assert((FMetric > 0) && "Should handle dense range earlier!");
1733 } else {
1734 Pivot = CR.Range.first + Size/2;
1735 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001736
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001737 CaseRange LHSR(CR.Range.first, Pivot);
1738 CaseRange RHSR(Pivot, CR.Range.second);
1739 Constant *C = Pivot->Low;
1740 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001741
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001742 // We know that we branch to the LHS if the Value being switched on is
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001743 // less than the Pivot value, C. We use this to optimize our binary
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001744 // tree a bit, by recognizing that if SV is greater than or equal to the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001745 // LHS's Case Value, and that Case Value is exactly one less than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001746 // Pivot's Value, then we can branch directly to the LHS's Target,
1747 // rather than creating a leaf node for it.
1748 if ((LHSR.second - LHSR.first) == 1 &&
1749 LHSR.first->High == CR.GE &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001750 cast<ConstantInt>(C)->getValue() ==
1751 (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001752 TrueBB = LHSR.first->BB;
1753 } else {
1754 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1755 CurMF->insert(BBI, TrueBB);
1756 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001757
1758 // Put SV in a virtual register to make it available from the new blocks.
1759 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001760 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001761
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001762 // Similar to the optimization above, if the Value being switched on is
1763 // known to be less than the Constant CR.LT, and the current Case Value
1764 // is CR.LT - 1, then we can branch directly to the target block for
1765 // the current Case Value, rather than emitting a RHS leaf node for it.
1766 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001767 cast<ConstantInt>(RHSR.first->Low)->getValue() ==
1768 (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001769 FalseBB = RHSR.first->BB;
1770 } else {
1771 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1772 CurMF->insert(BBI, FalseBB);
1773 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001774
1775 // Put SV in a virtual register to make it available from the new blocks.
1776 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001777 }
1778
1779 // Create a CaseBlock record representing a conditional branch to
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001780 // the LHS node if the value being switched on SV is less than C.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001781 // Otherwise, branch to LHS.
1782 CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
1783
Dan Gohman99be8ae2010-04-19 22:41:47 +00001784 if (CR.CaseBB == SwitchBB)
1785 visitSwitchCase(CB, SwitchBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001786 else
1787 SwitchCases.push_back(CB);
1788
1789 return true;
1790}
1791
1792/// handleBitTestsSwitchCase - if current case range has few destination and
1793/// range span less, than machine word bitwidth, encode case range into series
1794/// of masks and emit bit tests with these masks.
Dan Gohman2048b852009-11-23 18:04:58 +00001795bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR,
1796 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001797 const Value* SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001798 MachineBasicBlock* Default,
1799 MachineBasicBlock *SwitchBB){
Owen Andersone50ed302009-08-10 22:56:29 +00001800 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00001801 unsigned IntPtrBits = PTy.getSizeInBits();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001802
1803 Case& FrontCase = *CR.Range.first;
1804 Case& BackCase = *(CR.Range.second-1);
1805
1806 // Get the MachineFunction which holds the current MBB. This is used when
1807 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001808 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001809
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00001810 // If target does not have legal shift left, do not emit bit tests at all.
1811 if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy()))
1812 return false;
1813
Anton Korobeynikov23218582008-12-23 22:25:27 +00001814 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001815 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1816 I!=E; ++I) {
1817 // Single case counts one, case range - two.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001818 numCmps += (I->Low == I->High ? 1 : 2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001819 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001820
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001821 // Count unique destinations
1822 SmallSet<MachineBasicBlock*, 4> Dests;
1823 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1824 Dests.insert(I->BB);
1825 if (Dests.size() > 3)
1826 // Don't bother the code below, if there are too much unique destinations
1827 return false;
1828 }
David Greene4b69d992010-01-05 01:24:57 +00001829 DEBUG(dbgs() << "Total number of unique destinations: "
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001830 << Dests.size() << '\n'
1831 << "Total number of comparisons: " << numCmps << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001832
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001833 // Compute span of values.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001834 const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue();
1835 const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001836 APInt cmpRange = maxValue - minValue;
1837
David Greene4b69d992010-01-05 01:24:57 +00001838 DEBUG(dbgs() << "Compare range: " << cmpRange << '\n'
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001839 << "Low bound: " << minValue << '\n'
1840 << "High bound: " << maxValue << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001841
Dan Gohmane0567812010-04-08 23:03:40 +00001842 if (cmpRange.uge(IntPtrBits) ||
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001843 (!(Dests.size() == 1 && numCmps >= 3) &&
1844 !(Dests.size() == 2 && numCmps >= 5) &&
1845 !(Dests.size() >= 3 && numCmps >= 6)))
1846 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001847
David Greene4b69d992010-01-05 01:24:57 +00001848 DEBUG(dbgs() << "Emitting bit tests\n");
Anton Korobeynikov23218582008-12-23 22:25:27 +00001849 APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth());
1850
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001851 // Optimize the case where all the case values fit in a
1852 // word without having to subtract minValue. In this case,
1853 // we can optimize away the subtraction.
Dan Gohmane0567812010-04-08 23:03:40 +00001854 if (minValue.isNonNegative() && maxValue.slt(IntPtrBits)) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001855 cmpRange = maxValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001856 } else {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001857 lowBound = minValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001858 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001859
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001860 CaseBitsVector CasesBits;
1861 unsigned i, count = 0;
1862
1863 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1864 MachineBasicBlock* Dest = I->BB;
1865 for (i = 0; i < count; ++i)
1866 if (Dest == CasesBits[i].BB)
1867 break;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001868
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001869 if (i == count) {
1870 assert((count < 3) && "Too much destinations to test!");
1871 CasesBits.push_back(CaseBits(0, Dest, 0));
1872 count++;
1873 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001874
1875 const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue();
1876 const APInt& highValue = cast<ConstantInt>(I->High)->getValue();
1877
1878 uint64_t lo = (lowValue - lowBound).getZExtValue();
1879 uint64_t hi = (highValue - lowBound).getZExtValue();
1880
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001881 for (uint64_t j = lo; j <= hi; j++) {
1882 CasesBits[i].Mask |= 1ULL << j;
1883 CasesBits[i].Bits++;
1884 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001885
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001886 }
1887 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001888
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001889 BitTestInfo BTC;
1890
1891 // Figure out which block is immediately after the current one.
1892 MachineFunction::iterator BBI = CR.CaseBB;
1893 ++BBI;
1894
1895 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1896
David Greene4b69d992010-01-05 01:24:57 +00001897 DEBUG(dbgs() << "Cases:\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001898 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
David Greene4b69d992010-01-05 01:24:57 +00001899 DEBUG(dbgs() << "Mask: " << CasesBits[i].Mask
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001900 << ", Bits: " << CasesBits[i].Bits
1901 << ", BB: " << CasesBits[i].BB << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001902
1903 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1904 CurMF->insert(BBI, CaseBB);
1905 BTC.push_back(BitTestCase(CasesBits[i].Mask,
1906 CaseBB,
1907 CasesBits[i].BB));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001908
1909 // Put SV in a virtual register to make it available from the new blocks.
1910 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001911 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001912
1913 BitTestBlock BTB(lowBound, cmpRange, SV,
Dan Gohman99be8ae2010-04-19 22:41:47 +00001914 -1U, (CR.CaseBB == SwitchBB),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001915 CR.CaseBB, Default, BTC);
1916
Dan Gohman99be8ae2010-04-19 22:41:47 +00001917 if (CR.CaseBB == SwitchBB)
1918 visitBitTestHeader(BTB, SwitchBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001919
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001920 BitTestCases.push_back(BTB);
1921
1922 return true;
1923}
1924
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001925/// Clusterify - Transform simple list of Cases into list of CaseRange's
Dan Gohman2048b852009-11-23 18:04:58 +00001926size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases,
1927 const SwitchInst& SI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001928 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001929
1930 // Start with "simple" cases
Anton Korobeynikov23218582008-12-23 22:25:27 +00001931 for (size_t i = 1; i < SI.getNumSuccessors(); ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001932 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)];
1933 Cases.push_back(Case(SI.getSuccessorValue(i),
1934 SI.getSuccessorValue(i),
1935 SMBB));
1936 }
1937 std::sort(Cases.begin(), Cases.end(), CaseCmp());
1938
1939 // Merge case into clusters
Anton Korobeynikov23218582008-12-23 22:25:27 +00001940 if (Cases.size() >= 2)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001941 // Must recompute end() each iteration because it may be
1942 // invalidated by erase if we hold on to it
Anton Korobeynikov23218582008-12-23 22:25:27 +00001943 for (CaseItr I = Cases.begin(), J = ++(Cases.begin()); J != Cases.end(); ) {
1944 const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue();
1945 const APInt& currentValue = cast<ConstantInt>(I->High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001946 MachineBasicBlock* nextBB = J->BB;
1947 MachineBasicBlock* currentBB = I->BB;
1948
1949 // If the two neighboring cases go to the same destination, merge them
1950 // into a single case.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001951 if ((nextValue - currentValue == 1) && (currentBB == nextBB)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001952 I->High = J->High;
1953 J = Cases.erase(J);
1954 } else {
1955 I = J++;
1956 }
1957 }
1958
1959 for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
1960 if (I->Low != I->High)
1961 // A range counts double, since it requires two compares.
1962 ++numCmps;
1963 }
1964
1965 return numCmps;
1966}
1967
Dan Gohman46510a72010-04-15 01:51:59 +00001968void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00001969 MachineBasicBlock *SwitchMBB = FuncInfo.MBBMap[SI.getParent()];
1970
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001971 // Figure out which block is immediately after the current one.
1972 MachineBasicBlock *NextBlock = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001973 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
1974
1975 // If there is only the default destination, branch to it if it is not the
1976 // next basic block. Otherwise, just fall through.
1977 if (SI.getNumOperands() == 2) {
1978 // Update machine-CFG edges.
1979
1980 // If this is not a fall-through branch, emit the branch.
Dan Gohman99be8ae2010-04-19 22:41:47 +00001981 SwitchMBB->addSuccessor(Default);
Bill Wendling4533cac2010-01-28 21:51:40 +00001982 if (Default != NextBlock)
1983 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
1984 MVT::Other, getControlRoot(),
1985 DAG.getBasicBlock(Default)));
Bill Wendling49fcff82009-12-21 22:30:11 +00001986
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001987 return;
1988 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001989
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001990 // If there are any non-default case statements, create a vector of Cases
1991 // representing each one, and sort the vector so that we can efficiently
1992 // create a binary search tree from them.
1993 CaseVector Cases;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001994 size_t numCmps = Clusterify(Cases, SI);
David Greene4b69d992010-01-05 01:24:57 +00001995 DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size()
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001996 << ". Total compares: " << numCmps << '\n');
Devang Patel8a84e442009-01-05 17:31:22 +00001997 numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001998
1999 // Get the Value to be switched on and default basic blocks, which will be
2000 // inserted into CaseBlock records, representing basic blocks in the binary
2001 // search tree.
Dan Gohman46510a72010-04-15 01:51:59 +00002002 const Value *SV = SI.getOperand(0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002003
2004 // Push the initial CaseRec onto the worklist
2005 CaseRecVector WorkList;
Dan Gohman99be8ae2010-04-19 22:41:47 +00002006 WorkList.push_back(CaseRec(SwitchMBB,0,0,
2007 CaseRange(Cases.begin(),Cases.end())));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002008
2009 while (!WorkList.empty()) {
2010 // Grab a record representing a case range to process off the worklist
2011 CaseRec CR = WorkList.back();
2012 WorkList.pop_back();
2013
Dan Gohman99be8ae2010-04-19 22:41:47 +00002014 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002015 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002016
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002017 // If the range has few cases (two or less) emit a series of specific
2018 // tests.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002019 if (handleSmallSwitchRange(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002020 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002021
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002022 // If the switch has more than 5 blocks, and at least 40% dense, and the
2023 // target supports indirect branches, then emit a jump table rather than
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002024 // lowering the switch to a binary tree of conditional branches.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002025 if (handleJTSwitchCase(CR, WorkList, SV, Default, SwitchMBB))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002026 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002027
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002028 // Emit binary tree. We need to pick a pivot, and push left and right ranges
2029 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
Dan Gohman99be8ae2010-04-19 22:41:47 +00002030 handleBTSplitSwitchCase(CR, WorkList, SV, Default, SwitchMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002031 }
2032}
2033
Dan Gohman46510a72010-04-15 01:51:59 +00002034void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) {
Dan Gohman99be8ae2010-04-19 22:41:47 +00002035 MachineBasicBlock *IndirectBrMBB = FuncInfo.MBBMap[I.getParent()];
2036
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002037 // Update machine-CFG edges with unique successors.
Jakob Stoklund Olesenb5b90ed2010-02-11 18:06:56 +00002038 SmallVector<BasicBlock*, 32> succs;
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002039 succs.reserve(I.getNumSuccessors());
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002040 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i)
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002041 succs.push_back(I.getSuccessor(i));
Jakob Stoklund Olesenb5b90ed2010-02-11 18:06:56 +00002042 array_pod_sort(succs.begin(), succs.end());
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002043 succs.erase(std::unique(succs.begin(), succs.end()), succs.end());
2044 for (unsigned i = 0, e = succs.size(); i != e; ++i)
Dan Gohman99be8ae2010-04-19 22:41:47 +00002045 IndirectBrMBB->addSuccessor(FuncInfo.MBBMap[succs[i]]);
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002046
Bill Wendling4533cac2010-01-28 21:51:40 +00002047 DAG.setRoot(DAG.getNode(ISD::BRIND, getCurDebugLoc(),
2048 MVT::Other, getControlRoot(),
2049 getValue(I.getAddress())));
Bill Wendling49fcff82009-12-21 22:30:11 +00002050}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002051
Dan Gohman46510a72010-04-15 01:51:59 +00002052void SelectionDAGBuilder::visitFSub(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002053 // -0.0 - X --> fneg
2054 const Type *Ty = I.getType();
Duncan Sands1df98592010-02-16 11:11:14 +00002055 if (Ty->isVectorTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002056 if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) {
2057 const VectorType *DestTy = cast<VectorType>(I.getType());
2058 const Type *ElTy = DestTy->getElementType();
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002059 unsigned VL = DestTy->getNumElements();
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002060 std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy));
Owen Andersonaf7ec972009-07-28 21:19:26 +00002061 Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size());
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002062 if (CV == CNZ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002063 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00002064 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2065 Op2.getValueType(), Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002066 return;
2067 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002068 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002069 }
Bill Wendling49fcff82009-12-21 22:30:11 +00002070
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002071 if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002072 if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002073 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00002074 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2075 Op2.getValueType(), Op2));
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002076 return;
2077 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002078
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002079 visitBinary(I, ISD::FSUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002080}
2081
Dan Gohman46510a72010-04-15 01:51:59 +00002082void SelectionDAGBuilder::visitBinary(const User &I, unsigned OpCode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002083 SDValue Op1 = getValue(I.getOperand(0));
2084 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00002085 setValue(&I, DAG.getNode(OpCode, getCurDebugLoc(),
2086 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002087}
2088
Dan Gohman46510a72010-04-15 01:51:59 +00002089void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002090 SDValue Op1 = getValue(I.getOperand(0));
2091 SDValue Op2 = getValue(I.getOperand(1));
Duncan Sands1df98592010-02-16 11:11:14 +00002092 if (!I.getType()->isVectorTy() &&
Dan Gohman57fc82d2009-04-09 03:51:29 +00002093 Op2.getValueType() != TLI.getShiftAmountTy()) {
2094 // If the operand is smaller than the shift count type, promote it.
Owen Andersone50ed302009-08-10 22:56:29 +00002095 EVT PTy = TLI.getPointerTy();
2096 EVT STy = TLI.getShiftAmountTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002097 if (STy.bitsGT(Op2.getValueType()))
Dan Gohman57fc82d2009-04-09 03:51:29 +00002098 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
2099 TLI.getShiftAmountTy(), Op2);
2100 // If the operand is larger than the shift count type but the shift
2101 // count type has enough bits to represent any shift value, truncate
2102 // it now. This is a common case and it exposes the truncate to
2103 // optimization early.
Owen Anderson77547be2009-08-10 18:56:59 +00002104 else if (STy.getSizeInBits() >=
Dan Gohman57fc82d2009-04-09 03:51:29 +00002105 Log2_32_Ceil(Op2.getValueType().getSizeInBits()))
2106 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2107 TLI.getShiftAmountTy(), Op2);
2108 // Otherwise we'll need to temporarily settle for some other
2109 // convenient type; type legalization will make adjustments as
2110 // needed.
Owen Anderson77547be2009-08-10 18:56:59 +00002111 else if (PTy.bitsLT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002112 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002113 TLI.getPointerTy(), Op2);
Owen Anderson77547be2009-08-10 18:56:59 +00002114 else if (PTy.bitsGT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002115 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002116 TLI.getPointerTy(), Op2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002117 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002118
Bill Wendling4533cac2010-01-28 21:51:40 +00002119 setValue(&I, DAG.getNode(Opcode, getCurDebugLoc(),
2120 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002121}
2122
Dan Gohman46510a72010-04-15 01:51:59 +00002123void SelectionDAGBuilder::visitICmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002124 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002125 if (const ICmpInst *IC = dyn_cast<ICmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002126 predicate = IC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002127 else if (const ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002128 predicate = ICmpInst::Predicate(IC->getPredicate());
2129 SDValue Op1 = getValue(I.getOperand(0));
2130 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002131 ISD::CondCode Opcode = getICmpCondCode(predicate);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002132
Owen Andersone50ed302009-08-10 22:56:29 +00002133 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002134 setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Opcode));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002135}
2136
Dan Gohman46510a72010-04-15 01:51:59 +00002137void SelectionDAGBuilder::visitFCmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002138 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002139 if (const FCmpInst *FC = dyn_cast<FCmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002140 predicate = FC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002141 else if (const ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002142 predicate = FCmpInst::Predicate(FC->getPredicate());
2143 SDValue Op1 = getValue(I.getOperand(0));
2144 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002145 ISD::CondCode Condition = getFCmpCondCode(predicate);
Owen Andersone50ed302009-08-10 22:56:29 +00002146 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002147 setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002148}
2149
Dan Gohman46510a72010-04-15 01:51:59 +00002150void SelectionDAGBuilder::visitSelect(const User &I) {
Owen Andersone50ed302009-08-10 22:56:29 +00002151 SmallVector<EVT, 4> ValueVTs;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002152 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2153 unsigned NumValues = ValueVTs.size();
Bill Wendling49fcff82009-12-21 22:30:11 +00002154 if (NumValues == 0) return;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002155
Bill Wendling49fcff82009-12-21 22:30:11 +00002156 SmallVector<SDValue, 4> Values(NumValues);
2157 SDValue Cond = getValue(I.getOperand(0));
2158 SDValue TrueVal = getValue(I.getOperand(1));
2159 SDValue FalseVal = getValue(I.getOperand(2));
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002160
Bill Wendling4533cac2010-01-28 21:51:40 +00002161 for (unsigned i = 0; i != NumValues; ++i)
Bill Wendling49fcff82009-12-21 22:30:11 +00002162 Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(),
Chris Lattnerb3e87b22010-03-12 07:15:36 +00002163 TrueVal.getNode()->getValueType(TrueVal.getResNo()+i),
2164 Cond,
Bill Wendling49fcff82009-12-21 22:30:11 +00002165 SDValue(TrueVal.getNode(),
2166 TrueVal.getResNo() + i),
2167 SDValue(FalseVal.getNode(),
2168 FalseVal.getResNo() + i));
2169
Bill Wendling4533cac2010-01-28 21:51:40 +00002170 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2171 DAG.getVTList(&ValueVTs[0], NumValues),
2172 &Values[0], NumValues));
Bill Wendling49fcff82009-12-21 22:30:11 +00002173}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002174
Dan Gohman46510a72010-04-15 01:51:59 +00002175void SelectionDAGBuilder::visitTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002176 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2177 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002178 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002179 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002180}
2181
Dan Gohman46510a72010-04-15 01:51:59 +00002182void SelectionDAGBuilder::visitZExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002183 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2184 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2185 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002186 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002187 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002188}
2189
Dan Gohman46510a72010-04-15 01:51:59 +00002190void SelectionDAGBuilder::visitSExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002191 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2192 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2193 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002194 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002195 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002196}
2197
Dan Gohman46510a72010-04-15 01:51:59 +00002198void SelectionDAGBuilder::visitFPTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002199 // FPTrunc is never a no-op cast, no need to check
2200 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002201 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002202 setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(),
2203 DestVT, N, DAG.getIntPtrConstant(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002204}
2205
Dan Gohman46510a72010-04-15 01:51:59 +00002206void SelectionDAGBuilder::visitFPExt(const User &I){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002207 // FPTrunc is never a no-op cast, no need to check
2208 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002209 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002210 setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002211}
2212
Dan Gohman46510a72010-04-15 01:51:59 +00002213void SelectionDAGBuilder::visitFPToUI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002214 // FPToUI is never a no-op cast, no need to check
2215 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002216 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002217 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002218}
2219
Dan Gohman46510a72010-04-15 01:51:59 +00002220void SelectionDAGBuilder::visitFPToSI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002221 // FPToSI is never a no-op cast, no need to check
2222 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002223 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002224 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002225}
2226
Dan Gohman46510a72010-04-15 01:51:59 +00002227void SelectionDAGBuilder::visitUIToFP(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002228 // UIToFP is never a no-op cast, no need to check
2229 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002230 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002231 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002232}
2233
Dan Gohman46510a72010-04-15 01:51:59 +00002234void SelectionDAGBuilder::visitSIToFP(const User &I){
Bill Wendling181b6272008-10-19 20:34:04 +00002235 // SIToFP is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002236 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002237 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002238 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002239}
2240
Dan Gohman46510a72010-04-15 01:51:59 +00002241void SelectionDAGBuilder::visitPtrToInt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002242 // What to do depends on the size of the integer and the size of the pointer.
2243 // We can either truncate, zero extend, or no-op, accordingly.
2244 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002245 EVT SrcVT = N.getValueType();
2246 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002247 setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002248}
2249
Dan Gohman46510a72010-04-15 01:51:59 +00002250void SelectionDAGBuilder::visitIntToPtr(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002251 // What to do depends on the size of the integer and the size of the pointer.
2252 // We can either truncate, zero extend, or no-op, accordingly.
2253 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002254 EVT SrcVT = N.getValueType();
2255 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002256 setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002257}
2258
Dan Gohman46510a72010-04-15 01:51:59 +00002259void SelectionDAGBuilder::visitBitCast(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002260 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002261 EVT DestVT = TLI.getValueType(I.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002262
Bill Wendling49fcff82009-12-21 22:30:11 +00002263 // BitCast assures us that source and destination are the same size so this is
2264 // either a BIT_CONVERT or a no-op.
Bill Wendling4533cac2010-01-28 21:51:40 +00002265 if (DestVT != N.getValueType())
2266 setValue(&I, DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
2267 DestVT, N)); // convert types.
2268 else
Bill Wendling49fcff82009-12-21 22:30:11 +00002269 setValue(&I, N); // noop cast.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002270}
2271
Dan Gohman46510a72010-04-15 01:51:59 +00002272void SelectionDAGBuilder::visitInsertElement(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002273 SDValue InVec = getValue(I.getOperand(0));
2274 SDValue InVal = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00002275 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002276 TLI.getPointerTy(),
2277 getValue(I.getOperand(2)));
Bill Wendling4533cac2010-01-28 21:51:40 +00002278 setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(),
2279 TLI.getValueType(I.getType()),
2280 InVec, InVal, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002281}
2282
Dan Gohman46510a72010-04-15 01:51:59 +00002283void SelectionDAGBuilder::visitExtractElement(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002284 SDValue InVec = getValue(I.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002285 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002286 TLI.getPointerTy(),
2287 getValue(I.getOperand(1)));
Bill Wendling4533cac2010-01-28 21:51:40 +00002288 setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2289 TLI.getValueType(I.getType()), InVec, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002290}
2291
Mon P Wangaeb06d22008-11-10 04:46:22 +00002292// Utility for visitShuffleVector - Returns true if the mask is mask starting
2293// from SIndx and increasing to the element length (undefs are allowed).
Nate Begeman5a5ca152009-04-29 05:20:52 +00002294static bool SequentialMask(SmallVectorImpl<int> &Mask, unsigned SIndx) {
2295 unsigned MaskNumElts = Mask.size();
2296 for (unsigned i = 0; i != MaskNumElts; ++i)
2297 if ((Mask[i] >= 0) && (Mask[i] != (int)(i + SIndx)))
Nate Begeman9008ca62009-04-27 18:41:29 +00002298 return false;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002299 return true;
2300}
2301
Dan Gohman46510a72010-04-15 01:51:59 +00002302void SelectionDAGBuilder::visitShuffleVector(const User &I) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002303 SmallVector<int, 8> Mask;
Mon P Wang230e4fa2008-11-21 04:25:21 +00002304 SDValue Src1 = getValue(I.getOperand(0));
2305 SDValue Src2 = getValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002306
Nate Begeman9008ca62009-04-27 18:41:29 +00002307 // Convert the ConstantVector mask operand into an array of ints, with -1
2308 // representing undef values.
2309 SmallVector<Constant*, 8> MaskElts;
Chris Lattnerb29d5962010-02-01 20:48:08 +00002310 cast<Constant>(I.getOperand(2))->getVectorElements(MaskElts);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002311 unsigned MaskNumElts = MaskElts.size();
2312 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002313 if (isa<UndefValue>(MaskElts[i]))
2314 Mask.push_back(-1);
2315 else
2316 Mask.push_back(cast<ConstantInt>(MaskElts[i])->getSExtValue());
2317 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002318
Owen Andersone50ed302009-08-10 22:56:29 +00002319 EVT VT = TLI.getValueType(I.getType());
2320 EVT SrcVT = Src1.getValueType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00002321 unsigned SrcNumElts = SrcVT.getVectorNumElements();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002322
Mon P Wangc7849c22008-11-16 05:06:27 +00002323 if (SrcNumElts == MaskNumElts) {
Bill Wendling4533cac2010-01-28 21:51:40 +00002324 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2325 &Mask[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002326 return;
2327 }
2328
2329 // Normalize the shuffle vector since mask and vector length don't match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002330 if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) {
2331 // Mask is longer than the source vectors and is a multiple of the source
2332 // vectors. We can use concatenate vector to make the mask and vectors
Mon P Wang230e4fa2008-11-21 04:25:21 +00002333 // lengths match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002334 if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) {
2335 // The shuffle is concatenating two vectors together.
Bill Wendling4533cac2010-01-28 21:51:40 +00002336 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(),
2337 VT, Src1, Src2));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002338 return;
2339 }
2340
Mon P Wangc7849c22008-11-16 05:06:27 +00002341 // Pad both vectors with undefs to make them the same length as the mask.
2342 unsigned NumConcat = MaskNumElts / SrcNumElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00002343 bool Src1U = Src1.getOpcode() == ISD::UNDEF;
2344 bool Src2U = Src2.getOpcode() == ISD::UNDEF;
Dale Johannesene8d72302009-02-06 23:05:02 +00002345 SDValue UndefVal = DAG.getUNDEF(SrcVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002346
Nate Begeman9008ca62009-04-27 18:41:29 +00002347 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
2348 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002349 MOps1[0] = Src1;
2350 MOps2[0] = Src2;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002351
2352 Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2353 getCurDebugLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002354 &MOps1[0], NumConcat);
2355 Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002356 getCurDebugLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002357 &MOps2[0], NumConcat);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002358
Mon P Wangaeb06d22008-11-10 04:46:22 +00002359 // Readjust mask for new input vector length.
Nate Begeman9008ca62009-04-27 18:41:29 +00002360 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002361 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002362 int Idx = Mask[i];
Nate Begeman5a5ca152009-04-29 05:20:52 +00002363 if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002364 MappedOps.push_back(Idx);
2365 else
2366 MappedOps.push_back(Idx + MaskNumElts - SrcNumElts);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002367 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002368
Bill Wendling4533cac2010-01-28 21:51:40 +00002369 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2370 &MappedOps[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002371 return;
2372 }
2373
Mon P Wangc7849c22008-11-16 05:06:27 +00002374 if (SrcNumElts > MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002375 // Analyze the access pattern of the vector to see if we can extract
2376 // two subvectors and do the shuffle. The analysis is done by calculating
2377 // the range of elements the mask access on both vectors.
2378 int MinRange[2] = { SrcNumElts+1, SrcNumElts+1};
2379 int MaxRange[2] = {-1, -1};
2380
Nate Begeman5a5ca152009-04-29 05:20:52 +00002381 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002382 int Idx = Mask[i];
2383 int Input = 0;
2384 if (Idx < 0)
2385 continue;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002386
Nate Begeman5a5ca152009-04-29 05:20:52 +00002387 if (Idx >= (int)SrcNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002388 Input = 1;
2389 Idx -= SrcNumElts;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002390 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002391 if (Idx > MaxRange[Input])
2392 MaxRange[Input] = Idx;
2393 if (Idx < MinRange[Input])
2394 MinRange[Input] = Idx;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002395 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002396
Mon P Wangc7849c22008-11-16 05:06:27 +00002397 // Check if the access is smaller than the vector size and can we find
2398 // a reasonable extract index.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002399 int RangeUse[2] = { 2, 2 }; // 0 = Unused, 1 = Extract, 2 = Can not
2400 // Extract.
Mon P Wangc7849c22008-11-16 05:06:27 +00002401 int StartIdx[2]; // StartIdx to extract from
2402 for (int Input=0; Input < 2; ++Input) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002403 if (MinRange[Input] == (int)(SrcNumElts+1) && MaxRange[Input] == -1) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002404 RangeUse[Input] = 0; // Unused
2405 StartIdx[Input] = 0;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002406 } else if (MaxRange[Input] - MinRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002407 // Fits within range but we should see if we can find a good
Mon P Wang230e4fa2008-11-21 04:25:21 +00002408 // start index that is a multiple of the mask length.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002409 if (MaxRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002410 RangeUse[Input] = 1; // Extract from beginning of the vector
2411 StartIdx[Input] = 0;
2412 } else {
2413 StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002414 if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts &&
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002415 StartIdx[Input] + MaskNumElts < SrcNumElts)
Mon P Wangc7849c22008-11-16 05:06:27 +00002416 RangeUse[Input] = 1; // Extract from a multiple of the mask length.
Mon P Wangc7849c22008-11-16 05:06:27 +00002417 }
Mon P Wang230e4fa2008-11-21 04:25:21 +00002418 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002419 }
2420
Bill Wendling636e2582009-08-21 18:16:06 +00002421 if (RangeUse[0] == 0 && RangeUse[1] == 0) {
Bill Wendling4533cac2010-01-28 21:51:40 +00002422 setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
Mon P Wangc7849c22008-11-16 05:06:27 +00002423 return;
2424 }
2425 else if (RangeUse[0] < 2 && RangeUse[1] < 2) {
2426 // Extract appropriate subvector and generate a vector shuffle
2427 for (int Input=0; Input < 2; ++Input) {
Bill Wendling87710f02009-12-21 23:47:40 +00002428 SDValue &Src = Input == 0 ? Src1 : Src2;
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002429 if (RangeUse[Input] == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00002430 Src = DAG.getUNDEF(VT);
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002431 else
Dale Johannesen66978ee2009-01-31 02:22:37 +00002432 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002433 Src, DAG.getIntPtrConstant(StartIdx[Input]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002434 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002435
Mon P Wangc7849c22008-11-16 05:06:27 +00002436 // Calculate new mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00002437 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002438 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002439 int Idx = Mask[i];
2440 if (Idx < 0)
2441 MappedOps.push_back(Idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002442 else if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002443 MappedOps.push_back(Idx - StartIdx[0]);
2444 else
2445 MappedOps.push_back(Idx - SrcNumElts - StartIdx[1] + MaskNumElts);
Mon P Wangc7849c22008-11-16 05:06:27 +00002446 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002447
Bill Wendling4533cac2010-01-28 21:51:40 +00002448 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2449 &MappedOps[0]));
Mon P Wangc7849c22008-11-16 05:06:27 +00002450 return;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002451 }
2452 }
2453
Mon P Wangc7849c22008-11-16 05:06:27 +00002454 // We can't use either concat vectors or extract subvectors so fall back to
2455 // replacing the shuffle with extract and build vector.
2456 // to insert and build vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002457 EVT EltVT = VT.getVectorElementType();
2458 EVT PtrVT = TLI.getPointerTy();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002459 SmallVector<SDValue,8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002460 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002461 if (Mask[i] < 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002462 Ops.push_back(DAG.getUNDEF(EltVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002463 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00002464 int Idx = Mask[i];
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002465 SDValue Res;
2466
Nate Begeman5a5ca152009-04-29 05:20:52 +00002467 if (Idx < (int)SrcNumElts)
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002468 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2469 EltVT, Src1, DAG.getConstant(Idx, PtrVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002470 else
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002471 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2472 EltVT, Src2,
2473 DAG.getConstant(Idx - SrcNumElts, PtrVT));
2474
2475 Ops.push_back(Res);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002476 }
2477 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002478
Bill Wendling4533cac2010-01-28 21:51:40 +00002479 setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
2480 VT, &Ops[0], Ops.size()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002481}
2482
Dan Gohman46510a72010-04-15 01:51:59 +00002483void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002484 const Value *Op0 = I.getOperand(0);
2485 const Value *Op1 = I.getOperand(1);
2486 const Type *AggTy = I.getType();
2487 const Type *ValTy = Op1->getType();
2488 bool IntoUndef = isa<UndefValue>(Op0);
2489 bool FromUndef = isa<UndefValue>(Op1);
2490
2491 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2492 I.idx_begin(), I.idx_end());
2493
Owen Andersone50ed302009-08-10 22:56:29 +00002494 SmallVector<EVT, 4> AggValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002495 ComputeValueVTs(TLI, AggTy, AggValueVTs);
Owen Andersone50ed302009-08-10 22:56:29 +00002496 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002497 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2498
2499 unsigned NumAggValues = AggValueVTs.size();
2500 unsigned NumValValues = ValValueVTs.size();
2501 SmallVector<SDValue, 4> Values(NumAggValues);
2502
2503 SDValue Agg = getValue(Op0);
2504 SDValue Val = getValue(Op1);
2505 unsigned i = 0;
2506 // Copy the beginning value(s) from the original aggregate.
2507 for (; i != LinearIndex; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002508 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002509 SDValue(Agg.getNode(), Agg.getResNo() + i);
2510 // Copy values from the inserted value(s).
2511 for (; i != LinearIndex + NumValValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002512 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002513 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
2514 // Copy remaining value(s) from the original aggregate.
2515 for (; i != NumAggValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002516 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002517 SDValue(Agg.getNode(), Agg.getResNo() + i);
2518
Bill Wendling4533cac2010-01-28 21:51:40 +00002519 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2520 DAG.getVTList(&AggValueVTs[0], NumAggValues),
2521 &Values[0], NumAggValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002522}
2523
Dan Gohman46510a72010-04-15 01:51:59 +00002524void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002525 const Value *Op0 = I.getOperand(0);
2526 const Type *AggTy = Op0->getType();
2527 const Type *ValTy = I.getType();
2528 bool OutOfUndef = isa<UndefValue>(Op0);
2529
2530 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2531 I.idx_begin(), I.idx_end());
2532
Owen Andersone50ed302009-08-10 22:56:29 +00002533 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002534 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2535
2536 unsigned NumValValues = ValValueVTs.size();
2537 SmallVector<SDValue, 4> Values(NumValValues);
2538
2539 SDValue Agg = getValue(Op0);
2540 // Copy out the selected value(s).
2541 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
2542 Values[i - LinearIndex] =
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002543 OutOfUndef ?
Dale Johannesene8d72302009-02-06 23:05:02 +00002544 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002545 SDValue(Agg.getNode(), Agg.getResNo() + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002546
Bill Wendling4533cac2010-01-28 21:51:40 +00002547 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2548 DAG.getVTList(&ValValueVTs[0], NumValValues),
2549 &Values[0], NumValValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002550}
2551
Dan Gohman46510a72010-04-15 01:51:59 +00002552void SelectionDAGBuilder::visitGetElementPtr(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002553 SDValue N = getValue(I.getOperand(0));
2554 const Type *Ty = I.getOperand(0)->getType();
2555
Dan Gohman46510a72010-04-15 01:51:59 +00002556 for (GetElementPtrInst::const_op_iterator OI = I.op_begin()+1, E = I.op_end();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002557 OI != E; ++OI) {
Dan Gohman46510a72010-04-15 01:51:59 +00002558 const Value *Idx = *OI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002559 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
2560 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2561 if (Field) {
2562 // N = N + Offset
2563 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002564 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002565 DAG.getIntPtrConstant(Offset));
2566 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002567
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002568 Ty = StTy->getElementType(Field);
Chris Lattner93b122d2010-03-16 21:25:55 +00002569 } else if (const UnionType *UnTy = dyn_cast<UnionType>(Ty)) {
2570 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2571
2572 // Offset canonically 0 for unions, but type changes
2573 Ty = UnTy->getElementType(Field);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002574 } else {
2575 Ty = cast<SequentialType>(Ty)->getElementType();
2576
2577 // If this is a constant subscript, handle it quickly.
Dan Gohman46510a72010-04-15 01:51:59 +00002578 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002579 if (CI->getZExtValue() == 0) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002580 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +00002581 TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Evan Cheng65b52df2009-02-09 21:01:06 +00002582 SDValue OffsVal;
Owen Andersone50ed302009-08-10 22:56:29 +00002583 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002584 unsigned PtrBits = PTy.getSizeInBits();
Bill Wendlinge1a90422009-12-21 23:10:19 +00002585 if (PtrBits < 64)
Evan Cheng65b52df2009-02-09 21:01:06 +00002586 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2587 TLI.getPointerTy(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002588 DAG.getConstant(Offs, MVT::i64));
Bill Wendlinge1a90422009-12-21 23:10:19 +00002589 else
Evan Chengb1032a82009-02-09 20:54:38 +00002590 OffsVal = DAG.getIntPtrConstant(Offs);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002591
Dale Johannesen66978ee2009-01-31 02:22:37 +00002592 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Evan Chengb1032a82009-02-09 20:54:38 +00002593 OffsVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002594 continue;
2595 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002596
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002597 // N = N + Idx * ElementSize;
Dan Gohman7abbd042009-10-23 17:57:43 +00002598 APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(),
2599 TD->getTypeAllocSize(Ty));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002600 SDValue IdxN = getValue(Idx);
2601
2602 // If the index is smaller or larger than intptr_t, truncate or extend
2603 // it.
Duncan Sands3a66a682009-10-13 21:04:12 +00002604 IdxN = DAG.getSExtOrTrunc(IdxN, getCurDebugLoc(), N.getValueType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002605
2606 // If this is a multiply by a power of two, turn it into a shl
2607 // immediately. This is a very common case.
2608 if (ElementSize != 1) {
Dan Gohman7abbd042009-10-23 17:57:43 +00002609 if (ElementSize.isPowerOf2()) {
2610 unsigned Amt = ElementSize.logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00002611 IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002612 N.getValueType(), IdxN,
Duncan Sands92abc622009-01-31 15:50:11 +00002613 DAG.getConstant(Amt, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002614 } else {
Dan Gohman7abbd042009-10-23 17:57:43 +00002615 SDValue Scale = DAG.getConstant(ElementSize, TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00002616 IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002617 N.getValueType(), IdxN, Scale);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002618 }
2619 }
2620
Scott Michelfdc40a02009-02-17 22:15:04 +00002621 N = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002622 N.getValueType(), N, IdxN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002623 }
2624 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002625
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002626 setValue(&I, N);
2627}
2628
Dan Gohman46510a72010-04-15 01:51:59 +00002629void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002630 // If this is a fixed sized alloca in the entry block of the function,
2631 // allocate it statically on the stack.
2632 if (FuncInfo.StaticAllocaMap.count(&I))
2633 return; // getValue will auto-populate this.
2634
2635 const Type *Ty = I.getAllocatedType();
Duncan Sands777d2302009-05-09 07:06:46 +00002636 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002637 unsigned Align =
2638 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
2639 I.getAlignment());
2640
2641 SDValue AllocSize = getValue(I.getArraySize());
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002642
Chris Lattner0b18e592009-03-17 19:36:00 +00002643 AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), AllocSize.getValueType(),
2644 AllocSize,
2645 DAG.getConstant(TySize, AllocSize.getValueType()));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002646
Owen Andersone50ed302009-08-10 22:56:29 +00002647 EVT IntPtr = TLI.getPointerTy();
Duncan Sands3a66a682009-10-13 21:04:12 +00002648 AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002649
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002650 // Handle alignment. If the requested alignment is less than or equal to
2651 // the stack alignment, ignore it. If the size is greater than or equal to
2652 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
Dan Gohman55e59c12010-04-19 19:05:59 +00002653 unsigned StackAlign = TM.getFrameInfo()->getStackAlignment();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002654 if (Align <= StackAlign)
2655 Align = 0;
2656
2657 // Round the size of the allocation up to the stack alignment size
2658 // by add SA-1 to the size.
Scott Michelfdc40a02009-02-17 22:15:04 +00002659 AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002660 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002661 DAG.getIntPtrConstant(StackAlign-1));
Bill Wendling856ff412009-12-22 00:12:37 +00002662
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002663 // Mask out the low bits for alignment purposes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002664 AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002665 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002666 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
2667
2668 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Owen Anderson825b72b2009-08-11 20:47:22 +00002669 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
Scott Michelfdc40a02009-02-17 22:15:04 +00002670 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002671 VTs, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002672 setValue(&I, DSA);
2673 DAG.setRoot(DSA.getValue(1));
Bill Wendling856ff412009-12-22 00:12:37 +00002674
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002675 // Inform the Frame Information that we have just allocated a variable-sized
2676 // object.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002677 FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002678}
2679
Dan Gohman46510a72010-04-15 01:51:59 +00002680void SelectionDAGBuilder::visitLoad(const LoadInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002681 const Value *SV = I.getOperand(0);
2682 SDValue Ptr = getValue(SV);
2683
2684 const Type *Ty = I.getType();
David Greene1e559442010-02-15 17:00:31 +00002685
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002686 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00002687 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002688 unsigned Alignment = I.getAlignment();
2689
Owen Andersone50ed302009-08-10 22:56:29 +00002690 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002691 SmallVector<uint64_t, 4> Offsets;
2692 ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets);
2693 unsigned NumValues = ValueVTs.size();
2694 if (NumValues == 0)
2695 return;
2696
2697 SDValue Root;
2698 bool ConstantMemory = false;
2699 if (I.isVolatile())
2700 // Serialize volatile loads with other side effects.
2701 Root = getRoot();
2702 else if (AA->pointsToConstantMemory(SV)) {
2703 // Do not serialize (non-volatile) loads of constant memory with anything.
2704 Root = DAG.getEntryNode();
2705 ConstantMemory = true;
2706 } else {
2707 // Do not serialize non-volatile loads against each other.
2708 Root = DAG.getRoot();
2709 }
2710
2711 SmallVector<SDValue, 4> Values(NumValues);
2712 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002713 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002714 for (unsigned i = 0; i != NumValues; ++i) {
Bill Wendling856ff412009-12-22 00:12:37 +00002715 SDValue A = DAG.getNode(ISD::ADD, getCurDebugLoc(),
2716 PtrVT, Ptr,
2717 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00002718 SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root,
David Greene1e559442010-02-15 17:00:31 +00002719 A, SV, Offsets[i], isVolatile,
2720 isNonTemporal, Alignment);
Bill Wendling856ff412009-12-22 00:12:37 +00002721
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002722 Values[i] = L;
2723 Chains[i] = L.getValue(1);
2724 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002725
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002726 if (!ConstantMemory) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002727 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Bill Wendling856ff412009-12-22 00:12:37 +00002728 MVT::Other, &Chains[0], NumValues);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002729 if (isVolatile)
2730 DAG.setRoot(Chain);
2731 else
2732 PendingLoads.push_back(Chain);
2733 }
2734
Bill Wendling4533cac2010-01-28 21:51:40 +00002735 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2736 DAG.getVTList(&ValueVTs[0], NumValues),
2737 &Values[0], NumValues));
Bill Wendling856ff412009-12-22 00:12:37 +00002738}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002739
Dan Gohman46510a72010-04-15 01:51:59 +00002740void SelectionDAGBuilder::visitStore(const StoreInst &I) {
2741 const Value *SrcV = I.getOperand(0);
2742 const Value *PtrV = I.getOperand(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002743
Owen Andersone50ed302009-08-10 22:56:29 +00002744 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002745 SmallVector<uint64_t, 4> Offsets;
2746 ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets);
2747 unsigned NumValues = ValueVTs.size();
2748 if (NumValues == 0)
2749 return;
2750
2751 // Get the lowered operands. Note that we do this after
2752 // checking if NumResults is zero, because with zero results
2753 // the operands won't have values in the map.
2754 SDValue Src = getValue(SrcV);
2755 SDValue Ptr = getValue(PtrV);
2756
2757 SDValue Root = getRoot();
2758 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002759 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002760 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00002761 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002762 unsigned Alignment = I.getAlignment();
Bill Wendling856ff412009-12-22 00:12:37 +00002763
2764 for (unsigned i = 0; i != NumValues; ++i) {
2765 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, Ptr,
2766 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00002767 Chains[i] = DAG.getStore(Root, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002768 SDValue(Src.getNode(), Src.getResNo() + i),
David Greene1e559442010-02-15 17:00:31 +00002769 Add, PtrV, Offsets[i], isVolatile,
2770 isNonTemporal, Alignment);
Bill Wendling856ff412009-12-22 00:12:37 +00002771 }
2772
Bill Wendling4533cac2010-01-28 21:51:40 +00002773 DAG.setRoot(DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
2774 MVT::Other, &Chains[0], NumValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002775}
2776
2777/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
2778/// node.
Dan Gohman46510a72010-04-15 01:51:59 +00002779void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I,
Dan Gohman2048b852009-11-23 18:04:58 +00002780 unsigned Intrinsic) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002781 bool HasChain = !I.doesNotAccessMemory();
2782 bool OnlyLoad = HasChain && I.onlyReadsMemory();
2783
2784 // Build the operand list.
2785 SmallVector<SDValue, 8> Ops;
2786 if (HasChain) { // If this intrinsic has side-effects, chainify it.
2787 if (OnlyLoad) {
2788 // We don't need to serialize loads against other loads.
2789 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002790 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002791 Ops.push_back(getRoot());
2792 }
2793 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002794
2795 // Info is set by getTgtMemInstrinsic
2796 TargetLowering::IntrinsicInfo Info;
2797 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic);
2798
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002799 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002800 if (!IsTgtIntrinsic)
2801 Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002802
2803 // Add all operands of the call to the operand list.
Eric Christopher551754c2010-04-16 23:37:20 +00002804 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002805 SDValue Op = getValue(I.getOperand(i));
2806 assert(TLI.isTypeLegal(Op.getValueType()) &&
2807 "Intrinsic uses a non-legal type?");
2808 Ops.push_back(Op);
2809 }
2810
Owen Andersone50ed302009-08-10 22:56:29 +00002811 SmallVector<EVT, 4> ValueVTs;
Bob Wilson8d919552009-07-31 22:41:21 +00002812 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2813#ifndef NDEBUG
2814 for (unsigned Val = 0, E = ValueVTs.size(); Val != E; ++Val) {
2815 assert(TLI.isTypeLegal(ValueVTs[Val]) &&
2816 "Intrinsic uses a non-legal type?");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002817 }
Bob Wilson8d919552009-07-31 22:41:21 +00002818#endif // NDEBUG
Bill Wendling856ff412009-12-22 00:12:37 +00002819
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002820 if (HasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00002821 ValueVTs.push_back(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002822
Bob Wilson8d919552009-07-31 22:41:21 +00002823 SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002824
2825 // Create the node.
2826 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002827 if (IsTgtIntrinsic) {
2828 // This is target intrinsic that touches memory
Dale Johannesen66978ee2009-01-31 02:22:37 +00002829 Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002830 VTs, &Ops[0], Ops.size(),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002831 Info.memVT, Info.ptrVal, Info.offset,
2832 Info.align, Info.vol,
2833 Info.readMem, Info.writeMem);
Bill Wendling856ff412009-12-22 00:12:37 +00002834 } else if (!HasChain) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002835 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002836 VTs, &Ops[0], Ops.size());
Benjamin Kramerf0127052010-01-05 13:12:22 +00002837 } else if (!I.getType()->isVoidTy()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002838 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002839 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00002840 } else {
Scott Michelfdc40a02009-02-17 22:15:04 +00002841 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002842 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00002843 }
2844
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002845 if (HasChain) {
2846 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
2847 if (OnlyLoad)
2848 PendingLoads.push_back(Chain);
2849 else
2850 DAG.setRoot(Chain);
2851 }
Bill Wendling856ff412009-12-22 00:12:37 +00002852
Benjamin Kramerf0127052010-01-05 13:12:22 +00002853 if (!I.getType()->isVoidTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002854 if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Owen Andersone50ed302009-08-10 22:56:29 +00002855 EVT VT = TLI.getValueType(PTy);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002856 Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002857 }
Bill Wendling856ff412009-12-22 00:12:37 +00002858
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002859 setValue(&I, Result);
2860 }
2861}
2862
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002863/// GetSignificand - Get the significand and build it into a floating-point
2864/// number with exponent of 1:
2865///
2866/// Op = (Op & 0x007fffff) | 0x3f800000;
2867///
2868/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00002869static SDValue
Bill Wendling46ada192010-03-02 01:55:18 +00002870GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002871 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
2872 DAG.getConstant(0x007fffff, MVT::i32));
2873 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
2874 DAG.getConstant(0x3f800000, MVT::i32));
Bill Wendling4533cac2010-01-28 21:51:40 +00002875 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00002876}
2877
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002878/// GetExponent - Get the exponent:
2879///
Bill Wendlinge9a72862009-01-20 21:17:57 +00002880/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002881///
2882/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00002883static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00002884GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
Bill Wendling46ada192010-03-02 01:55:18 +00002885 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002886 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
2887 DAG.getConstant(0x7f800000, MVT::i32));
2888 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Duncan Sands92abc622009-01-31 15:50:11 +00002889 DAG.getConstant(23, TLI.getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00002890 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
2891 DAG.getConstant(127, MVT::i32));
Bill Wendling4533cac2010-01-28 21:51:40 +00002892 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00002893}
2894
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002895/// getF32Constant - Get 32-bit floating point constant.
2896static SDValue
2897getF32Constant(SelectionDAG &DAG, unsigned Flt) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002898 return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002899}
2900
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002901/// Inlined utility function to implement binary input atomic intrinsics for
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002902/// visitIntrinsicCall: I is a call instruction
2903/// Op is the associated NodeType for I
2904const char *
Dan Gohman46510a72010-04-15 01:51:59 +00002905SelectionDAGBuilder::implVisitBinaryAtomic(const CallInst& I,
2906 ISD::NodeType Op) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002907 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00002908 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00002909 DAG.getAtomic(Op, getCurDebugLoc(),
Eric Christopher551754c2010-04-16 23:37:20 +00002910 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00002911 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002912 getValue(I.getOperand(1)),
Eric Christopher551754c2010-04-16 23:37:20 +00002913 getValue(I.getOperand(2)),
2914 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002915 setValue(&I, L);
2916 DAG.setRoot(L.getValue(1));
2917 return 0;
2918}
2919
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00002920// implVisitAluOverflow - Lower arithmetic overflow instrinsics.
Bill Wendling74c37652008-12-09 22:08:41 +00002921const char *
Dan Gohman46510a72010-04-15 01:51:59 +00002922SelectionDAGBuilder::implVisitAluOverflow(const CallInst &I, ISD::NodeType Op) {
Eric Christopher551754c2010-04-16 23:37:20 +00002923 SDValue Op1 = getValue(I.getOperand(1));
2924 SDValue Op2 = getValue(I.getOperand(2));
Bill Wendling74c37652008-12-09 22:08:41 +00002925
Owen Anderson825b72b2009-08-11 20:47:22 +00002926 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
Bill Wendling4533cac2010-01-28 21:51:40 +00002927 setValue(&I, DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2));
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00002928 return 0;
2929}
Bill Wendling74c37652008-12-09 22:08:41 +00002930
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002931/// visitExp - Lower an exp intrinsic. Handles the special sequences for
2932/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00002933void
Dan Gohman46510a72010-04-15 01:51:59 +00002934SelectionDAGBuilder::visitExp(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00002935 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00002936 DebugLoc dl = getCurDebugLoc();
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002937
Eric Christopher551754c2010-04-16 23:37:20 +00002938 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002939 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00002940 SDValue Op = getValue(I.getOperand(1));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002941
2942 // Put the exponent in the right bit position for later addition to the
2943 // final result:
2944 //
2945 // #define LOG2OFe 1.4426950f
2946 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Owen Anderson825b72b2009-08-11 20:47:22 +00002947 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002948 getF32Constant(DAG, 0x3fb8aa3b));
Owen Anderson825b72b2009-08-11 20:47:22 +00002949 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002950
2951 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00002952 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
2953 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002954
2955 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00002956 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00002957 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendling856ff412009-12-22 00:12:37 +00002958
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002959 if (LimitFloatPrecision <= 6) {
2960 // For floating-point precision of 6:
2961 //
2962 // TwoToFractionalPartOfX =
2963 // 0.997535578f +
2964 // (0.735607626f + 0.252464424f * x) * x;
2965 //
2966 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00002967 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002968 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00002969 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002970 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00002971 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
2972 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002973 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00002974 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002975
2976 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00002977 SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002978 TwoToFracPartOfX, IntegerPartOfX);
2979
Owen Anderson825b72b2009-08-11 20:47:22 +00002980 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002981 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
2982 // For floating-point precision of 12:
2983 //
2984 // TwoToFractionalPartOfX =
2985 // 0.999892986f +
2986 // (0.696457318f +
2987 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
2988 //
2989 // 0.000107046256 error, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00002990 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002991 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00002992 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002993 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00002994 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
2995 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002996 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00002997 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
2998 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002999 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003000 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003001
3002 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003003 SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003004 TwoToFracPartOfX, IntegerPartOfX);
3005
Owen Anderson825b72b2009-08-11 20:47:22 +00003006 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t8);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003007 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3008 // For floating-point precision of 18:
3009 //
3010 // TwoToFractionalPartOfX =
3011 // 0.999999982f +
3012 // (0.693148872f +
3013 // (0.240227044f +
3014 // (0.554906021e-1f +
3015 // (0.961591928e-2f +
3016 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3017 //
3018 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003019 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003020 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003021 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003022 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003023 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3024 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003025 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003026 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3027 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003028 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003029 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3030 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003031 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003032 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3033 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003034 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003035 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3036 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003037 getF32Constant(DAG, 0x3f800000));
Scott Michelfdc40a02009-02-17 22:15:04 +00003038 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003039 MVT::i32, t13);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003040
3041 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003042 SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003043 TwoToFracPartOfX, IntegerPartOfX);
3044
Owen Anderson825b72b2009-08-11 20:47:22 +00003045 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003046 }
3047 } else {
3048 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003049 result = DAG.getNode(ISD::FEXP, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003050 getValue(I.getOperand(1)).getValueType(),
3051 getValue(I.getOperand(1)));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003052 }
3053
Dale Johannesen59e577f2008-09-05 18:38:42 +00003054 setValue(&I, result);
3055}
3056
Bill Wendling39150252008-09-09 20:39:27 +00003057/// visitLog - Lower a log intrinsic. Handles the special sequences for
3058/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003059void
Dan Gohman46510a72010-04-15 01:51:59 +00003060SelectionDAGBuilder::visitLog(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003061 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003062 DebugLoc dl = getCurDebugLoc();
Bill Wendling39150252008-09-09 20:39:27 +00003063
Eric Christopher551754c2010-04-16 23:37:20 +00003064 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling39150252008-09-09 20:39:27 +00003065 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003066 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003067 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00003068
3069 // Scale the exponent by log(2) [0.69314718f].
Bill Wendling46ada192010-03-02 01:55:18 +00003070 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003071 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003072 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003073
3074 // Get the significand and build it into a floating-point number with
3075 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003076 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling39150252008-09-09 20:39:27 +00003077
3078 if (LimitFloatPrecision <= 6) {
3079 // For floating-point precision of 6:
3080 //
3081 // LogofMantissa =
3082 // -1.1609546f +
3083 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003084 //
Bill Wendling39150252008-09-09 20:39:27 +00003085 // error 0.0034276066, which is better than 8 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003086 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003087 getF32Constant(DAG, 0xbe74c456));
Owen Anderson825b72b2009-08-11 20:47:22 +00003088 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003089 getF32Constant(DAG, 0x3fb3a2b1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003090 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3091 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003092 getF32Constant(DAG, 0x3f949a29));
Bill Wendling39150252008-09-09 20:39:27 +00003093
Scott Michelfdc40a02009-02-17 22:15:04 +00003094 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003095 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003096 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3097 // For floating-point precision of 12:
3098 //
3099 // LogOfMantissa =
3100 // -1.7417939f +
3101 // (2.8212026f +
3102 // (-1.4699568f +
3103 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3104 //
3105 // error 0.000061011436, which is 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003106 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003107 getF32Constant(DAG, 0xbd67b6d6));
Owen Anderson825b72b2009-08-11 20:47:22 +00003108 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003109 getF32Constant(DAG, 0x3ee4f4b8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003110 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3111 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003112 getF32Constant(DAG, 0x3fbc278b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003113 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3114 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003115 getF32Constant(DAG, 0x40348e95));
Owen Anderson825b72b2009-08-11 20:47:22 +00003116 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3117 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003118 getF32Constant(DAG, 0x3fdef31a));
Bill Wendling39150252008-09-09 20:39:27 +00003119
Scott Michelfdc40a02009-02-17 22:15:04 +00003120 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003121 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003122 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3123 // For floating-point precision of 18:
3124 //
3125 // LogOfMantissa =
3126 // -2.1072184f +
3127 // (4.2372794f +
3128 // (-3.7029485f +
3129 // (2.2781945f +
3130 // (-0.87823314f +
3131 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3132 //
3133 // error 0.0000023660568, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003134 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003135 getF32Constant(DAG, 0xbc91e5ac));
Owen Anderson825b72b2009-08-11 20:47:22 +00003136 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003137 getF32Constant(DAG, 0x3e4350aa));
Owen Anderson825b72b2009-08-11 20:47:22 +00003138 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3139 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003140 getF32Constant(DAG, 0x3f60d3e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003141 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3142 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003143 getF32Constant(DAG, 0x4011cdf0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003144 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3145 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003146 getF32Constant(DAG, 0x406cfd1c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003147 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3148 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003149 getF32Constant(DAG, 0x408797cb));
Owen Anderson825b72b2009-08-11 20:47:22 +00003150 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3151 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003152 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003153
Scott Michelfdc40a02009-02-17 22:15:04 +00003154 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003155 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003156 }
3157 } else {
3158 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003159 result = DAG.getNode(ISD::FLOG, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003160 getValue(I.getOperand(1)).getValueType(),
3161 getValue(I.getOperand(1)));
Bill Wendling39150252008-09-09 20:39:27 +00003162 }
3163
Dale Johannesen59e577f2008-09-05 18:38:42 +00003164 setValue(&I, result);
3165}
3166
Bill Wendling3eb59402008-09-09 00:28:24 +00003167/// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for
3168/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003169void
Dan Gohman46510a72010-04-15 01:51:59 +00003170SelectionDAGBuilder::visitLog2(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003171 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003172 DebugLoc dl = getCurDebugLoc();
Bill Wendling3eb59402008-09-09 00:28:24 +00003173
Eric Christopher551754c2010-04-16 23:37:20 +00003174 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003175 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003176 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003177 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003178
Bill Wendling39150252008-09-09 20:39:27 +00003179 // Get the exponent.
Bill Wendling46ada192010-03-02 01:55:18 +00003180 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
Bill Wendling856ff412009-12-22 00:12:37 +00003181
Bill Wendling3eb59402008-09-09 00:28:24 +00003182 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003183 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003184 SDValue X = GetSignificand(DAG, Op1, dl);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003185
Bill Wendling3eb59402008-09-09 00:28:24 +00003186 // Different possible minimax approximations of significand in
3187 // floating-point for various degrees of accuracy over [1,2].
3188 if (LimitFloatPrecision <= 6) {
3189 // For floating-point precision of 6:
3190 //
3191 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
3192 //
3193 // error 0.0049451742, which is more than 7 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003194 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003195 getF32Constant(DAG, 0xbeb08fe0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003196 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003197 getF32Constant(DAG, 0x40019463));
Owen Anderson825b72b2009-08-11 20:47:22 +00003198 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3199 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003200 getF32Constant(DAG, 0x3fd6633d));
Bill Wendling3eb59402008-09-09 00:28:24 +00003201
Scott Michelfdc40a02009-02-17 22:15:04 +00003202 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003203 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003204 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3205 // For floating-point precision of 12:
3206 //
3207 // Log2ofMantissa =
3208 // -2.51285454f +
3209 // (4.07009056f +
3210 // (-2.12067489f +
3211 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003212 //
Bill Wendling3eb59402008-09-09 00:28:24 +00003213 // error 0.0000876136000, which is better than 13 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003214 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003215 getF32Constant(DAG, 0xbda7262e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003216 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003217 getF32Constant(DAG, 0x3f25280b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003218 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3219 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003220 getF32Constant(DAG, 0x4007b923));
Owen Anderson825b72b2009-08-11 20:47:22 +00003221 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3222 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003223 getF32Constant(DAG, 0x40823e2f));
Owen Anderson825b72b2009-08-11 20:47:22 +00003224 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3225 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003226 getF32Constant(DAG, 0x4020d29c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003227
Scott Michelfdc40a02009-02-17 22:15:04 +00003228 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003229 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003230 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3231 // For floating-point precision of 18:
3232 //
3233 // Log2ofMantissa =
3234 // -3.0400495f +
3235 // (6.1129976f +
3236 // (-5.3420409f +
3237 // (3.2865683f +
3238 // (-1.2669343f +
3239 // (0.27515199f -
3240 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
3241 //
3242 // error 0.0000018516, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003243 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003244 getF32Constant(DAG, 0xbcd2769e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003245 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003246 getF32Constant(DAG, 0x3e8ce0b9));
Owen Anderson825b72b2009-08-11 20:47:22 +00003247 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3248 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003249 getF32Constant(DAG, 0x3fa22ae7));
Owen Anderson825b72b2009-08-11 20:47:22 +00003250 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3251 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003252 getF32Constant(DAG, 0x40525723));
Owen Anderson825b72b2009-08-11 20:47:22 +00003253 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3254 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003255 getF32Constant(DAG, 0x40aaf200));
Owen Anderson825b72b2009-08-11 20:47:22 +00003256 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3257 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003258 getF32Constant(DAG, 0x40c39dad));
Owen Anderson825b72b2009-08-11 20:47:22 +00003259 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3260 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003261 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003262
Scott Michelfdc40a02009-02-17 22:15:04 +00003263 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003264 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003265 }
Dale Johannesen853244f2008-09-05 23:49:37 +00003266 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003267 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003268 result = DAG.getNode(ISD::FLOG2, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003269 getValue(I.getOperand(1)).getValueType(),
3270 getValue(I.getOperand(1)));
Dale Johannesen853244f2008-09-05 23:49:37 +00003271 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003272
Dale Johannesen59e577f2008-09-05 18:38:42 +00003273 setValue(&I, result);
3274}
3275
Bill Wendling3eb59402008-09-09 00:28:24 +00003276/// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for
3277/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003278void
Dan Gohman46510a72010-04-15 01:51:59 +00003279SelectionDAGBuilder::visitLog10(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003280 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003281 DebugLoc dl = getCurDebugLoc();
Bill Wendling181b6272008-10-19 20:34:04 +00003282
Eric Christopher551754c2010-04-16 23:37:20 +00003283 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003284 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003285 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003286 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003287
Bill Wendling39150252008-09-09 20:39:27 +00003288 // Scale the exponent by log10(2) [0.30102999f].
Bill Wendling46ada192010-03-02 01:55:18 +00003289 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003290 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003291 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00003292
3293 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003294 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003295 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling3eb59402008-09-09 00:28:24 +00003296
3297 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003298 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003299 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003300 // Log10ofMantissa =
3301 // -0.50419619f +
3302 // (0.60948995f - 0.10380950f * x) * x;
3303 //
3304 // error 0.0014886165, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003305 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003306 getF32Constant(DAG, 0xbdd49a13));
Owen Anderson825b72b2009-08-11 20:47:22 +00003307 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003308 getF32Constant(DAG, 0x3f1c0789));
Owen Anderson825b72b2009-08-11 20:47:22 +00003309 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3310 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003311 getF32Constant(DAG, 0x3f011300));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003312
Scott Michelfdc40a02009-02-17 22:15:04 +00003313 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003314 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003315 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3316 // For floating-point precision of 12:
3317 //
3318 // Log10ofMantissa =
3319 // -0.64831180f +
3320 // (0.91751397f +
3321 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
3322 //
3323 // error 0.00019228036, which is better than 12 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003324 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003325 getF32Constant(DAG, 0x3d431f31));
Owen Anderson825b72b2009-08-11 20:47:22 +00003326 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003327 getF32Constant(DAG, 0x3ea21fb2));
Owen Anderson825b72b2009-08-11 20:47:22 +00003328 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3329 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003330 getF32Constant(DAG, 0x3f6ae232));
Owen Anderson825b72b2009-08-11 20:47:22 +00003331 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3332 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003333 getF32Constant(DAG, 0x3f25f7c3));
Bill Wendling3eb59402008-09-09 00:28:24 +00003334
Scott Michelfdc40a02009-02-17 22:15:04 +00003335 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003336 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003337 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003338 // For floating-point precision of 18:
3339 //
3340 // Log10ofMantissa =
3341 // -0.84299375f +
3342 // (1.5327582f +
3343 // (-1.0688956f +
3344 // (0.49102474f +
3345 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
3346 //
3347 // error 0.0000037995730, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003348 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003349 getF32Constant(DAG, 0x3c5d51ce));
Owen Anderson825b72b2009-08-11 20:47:22 +00003350 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003351 getF32Constant(DAG, 0x3e00685a));
Owen Anderson825b72b2009-08-11 20:47:22 +00003352 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3353 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003354 getF32Constant(DAG, 0x3efb6798));
Owen Anderson825b72b2009-08-11 20:47:22 +00003355 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3356 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003357 getF32Constant(DAG, 0x3f88d192));
Owen Anderson825b72b2009-08-11 20:47:22 +00003358 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3359 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003360 getF32Constant(DAG, 0x3fc4316c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003361 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3362 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003363 getF32Constant(DAG, 0x3f57ce70));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003364
Scott Michelfdc40a02009-02-17 22:15:04 +00003365 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003366 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003367 }
Dale Johannesen852680a2008-09-05 21:27:19 +00003368 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003369 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003370 result = DAG.getNode(ISD::FLOG10, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003371 getValue(I.getOperand(1)).getValueType(),
3372 getValue(I.getOperand(1)));
Dale Johannesen852680a2008-09-05 21:27:19 +00003373 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003374
Dale Johannesen59e577f2008-09-05 18:38:42 +00003375 setValue(&I, result);
3376}
3377
Bill Wendlinge10c8142008-09-09 22:39:21 +00003378/// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for
3379/// limited-precision mode.
Dale Johannesen601d3c02008-09-05 01:48:15 +00003380void
Dan Gohman46510a72010-04-15 01:51:59 +00003381SelectionDAGBuilder::visitExp2(const CallInst &I) {
Dale Johannesen601d3c02008-09-05 01:48:15 +00003382 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003383 DebugLoc dl = getCurDebugLoc();
Bill Wendlinge10c8142008-09-09 22:39:21 +00003384
Eric Christopher551754c2010-04-16 23:37:20 +00003385 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00003386 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003387 SDValue Op = getValue(I.getOperand(1));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003388
Owen Anderson825b72b2009-08-11 20:47:22 +00003389 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003390
3391 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003392 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3393 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003394
3395 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003396 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003397 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003398
3399 if (LimitFloatPrecision <= 6) {
3400 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003401 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00003402 // TwoToFractionalPartOfX =
3403 // 0.997535578f +
3404 // (0.735607626f + 0.252464424f * x) * x;
3405 //
3406 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003407 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003408 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003409 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003410 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003411 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3412 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003413 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003414 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003415 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003416 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003417
Scott Michelfdc40a02009-02-17 22:15:04 +00003418 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003419 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003420 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3421 // For floating-point precision of 12:
3422 //
3423 // TwoToFractionalPartOfX =
3424 // 0.999892986f +
3425 // (0.696457318f +
3426 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3427 //
3428 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003429 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003430 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003431 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003432 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003433 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3434 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003435 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003436 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3437 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003438 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003439 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003440 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003441 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003442
Scott Michelfdc40a02009-02-17 22:15:04 +00003443 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003444 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003445 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3446 // For floating-point precision of 18:
3447 //
3448 // TwoToFractionalPartOfX =
3449 // 0.999999982f +
3450 // (0.693148872f +
3451 // (0.240227044f +
3452 // (0.554906021e-1f +
3453 // (0.961591928e-2f +
3454 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3455 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003456 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003457 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003458 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003459 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003460 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3461 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003462 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003463 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3464 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003465 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003466 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3467 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003468 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003469 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3470 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003471 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003472 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3473 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003474 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003475 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003476 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003477 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003478
Scott Michelfdc40a02009-02-17 22:15:04 +00003479 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003480 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003481 }
Dale Johannesen601d3c02008-09-05 01:48:15 +00003482 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003483 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003484 result = DAG.getNode(ISD::FEXP2, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003485 getValue(I.getOperand(1)).getValueType(),
3486 getValue(I.getOperand(1)));
Dale Johannesen601d3c02008-09-05 01:48:15 +00003487 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003488
Dale Johannesen601d3c02008-09-05 01:48:15 +00003489 setValue(&I, result);
3490}
3491
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003492/// visitPow - Lower a pow intrinsic. Handles the special sequences for
3493/// limited-precision mode with x == 10.0f.
3494void
Dan Gohman46510a72010-04-15 01:51:59 +00003495SelectionDAGBuilder::visitPow(const CallInst &I) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003496 SDValue result;
Eric Christopher551754c2010-04-16 23:37:20 +00003497 const Value *Val = I.getOperand(1);
Dale Johannesen66978ee2009-01-31 02:22:37 +00003498 DebugLoc dl = getCurDebugLoc();
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003499 bool IsExp10 = false;
3500
Owen Anderson825b72b2009-08-11 20:47:22 +00003501 if (getValue(Val).getValueType() == MVT::f32 &&
Eric Christopher551754c2010-04-16 23:37:20 +00003502 getValue(I.getOperand(2)).getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003503 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3504 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) {
3505 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
3506 APFloat Ten(10.0f);
3507 IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten);
3508 }
3509 }
3510 }
3511
3512 if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003513 SDValue Op = getValue(I.getOperand(2));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003514
3515 // Put the exponent in the right bit position for later addition to the
3516 // final result:
3517 //
3518 // #define LOG2OF10 3.3219281f
3519 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Owen Anderson825b72b2009-08-11 20:47:22 +00003520 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003521 getF32Constant(DAG, 0x40549a78));
Owen Anderson825b72b2009-08-11 20:47:22 +00003522 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003523
3524 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003525 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3526 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003527
3528 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003529 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003530 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003531
3532 if (LimitFloatPrecision <= 6) {
3533 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003534 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003535 // twoToFractionalPartOfX =
3536 // 0.997535578f +
3537 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003538 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003539 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003540 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003541 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003542 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003543 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003544 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3545 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003546 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003547 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003548 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003549 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003550
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003551 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003552 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003553 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3554 // For floating-point precision of 12:
3555 //
3556 // TwoToFractionalPartOfX =
3557 // 0.999892986f +
3558 // (0.696457318f +
3559 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3560 //
3561 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003562 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003563 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003564 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003565 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003566 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3567 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003568 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003569 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3570 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003571 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003572 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003573 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003574 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003575
Scott Michelfdc40a02009-02-17 22:15:04 +00003576 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003577 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003578 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3579 // For floating-point precision of 18:
3580 //
3581 // TwoToFractionalPartOfX =
3582 // 0.999999982f +
3583 // (0.693148872f +
3584 // (0.240227044f +
3585 // (0.554906021e-1f +
3586 // (0.961591928e-2f +
3587 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3588 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003589 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003590 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003591 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003592 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003593 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3594 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003595 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003596 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3597 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003598 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003599 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3600 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003601 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003602 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3603 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003604 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003605 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3606 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003607 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003608 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003609 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003610 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003611
Scott Michelfdc40a02009-02-17 22:15:04 +00003612 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003613 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003614 }
3615 } else {
3616 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003617 result = DAG.getNode(ISD::FPOW, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003618 getValue(I.getOperand(1)).getValueType(),
3619 getValue(I.getOperand(1)),
3620 getValue(I.getOperand(2)));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003621 }
3622
3623 setValue(&I, result);
3624}
3625
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003626
3627/// ExpandPowI - Expand a llvm.powi intrinsic.
3628static SDValue ExpandPowI(DebugLoc DL, SDValue LHS, SDValue RHS,
3629 SelectionDAG &DAG) {
3630 // If RHS is a constant, we can expand this out to a multiplication tree,
3631 // otherwise we end up lowering to a call to __powidf2 (for example). When
3632 // optimizing for size, we only want to do this if the expansion would produce
3633 // a small number of multiplies, otherwise we do the full expansion.
3634 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3635 // Get the exponent as a positive value.
3636 unsigned Val = RHSC->getSExtValue();
3637 if ((int)Val < 0) Val = -Val;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003638
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003639 // powi(x, 0) -> 1.0
3640 if (Val == 0)
3641 return DAG.getConstantFP(1.0, LHS.getValueType());
3642
Dan Gohmanae541aa2010-04-15 04:33:49 +00003643 const Function *F = DAG.getMachineFunction().getFunction();
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003644 if (!F->hasFnAttr(Attribute::OptimizeForSize) ||
3645 // If optimizing for size, don't insert too many multiplies. This
3646 // inserts up to 5 multiplies.
3647 CountPopulation_32(Val)+Log2_32(Val) < 7) {
3648 // We use the simple binary decomposition method to generate the multiply
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003649 // sequence. There are more optimal ways to do this (for example,
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003650 // powi(x,15) generates one more multiply than it should), but this has
3651 // the benefit of being both really simple and much better than a libcall.
3652 SDValue Res; // Logically starts equal to 1.0
3653 SDValue CurSquare = LHS;
3654 while (Val) {
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00003655 if (Val & 1) {
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003656 if (Res.getNode())
3657 Res = DAG.getNode(ISD::FMUL, DL,Res.getValueType(), Res, CurSquare);
3658 else
3659 Res = CurSquare; // 1.0*CurSquare.
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00003660 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003661
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003662 CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
3663 CurSquare, CurSquare);
3664 Val >>= 1;
3665 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003666
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003667 // If the original was negative, invert the result, producing 1/(x*x*x).
3668 if (RHSC->getSExtValue() < 0)
3669 Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(),
3670 DAG.getConstantFP(1.0, LHS.getValueType()), Res);
3671 return Res;
3672 }
3673 }
3674
3675 // Otherwise, expand to a libcall.
3676 return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS);
3677}
3678
3679
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003680/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
3681/// we want to emit this as a call to a named external function, return the name
3682/// otherwise lower it and return null.
3683const char *
Dan Gohman46510a72010-04-15 01:51:59 +00003684SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, unsigned Intrinsic) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00003685 DebugLoc dl = getCurDebugLoc();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003686 SDValue Res;
3687
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003688 switch (Intrinsic) {
3689 default:
3690 // By default, turn this into a target intrinsic node.
3691 visitTargetIntrinsic(I, Intrinsic);
3692 return 0;
3693 case Intrinsic::vastart: visitVAStart(I); return 0;
3694 case Intrinsic::vaend: visitVAEnd(I); return 0;
3695 case Intrinsic::vacopy: visitVACopy(I); return 0;
3696 case Intrinsic::returnaddress:
Bill Wendling4533cac2010-01-28 21:51:40 +00003697 setValue(&I, DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(),
Eric Christopher551754c2010-04-16 23:37:20 +00003698 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003699 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00003700 case Intrinsic::frameaddress:
Bill Wendling4533cac2010-01-28 21:51:40 +00003701 setValue(&I, DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(),
Eric Christopher551754c2010-04-16 23:37:20 +00003702 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003703 return 0;
3704 case Intrinsic::setjmp:
3705 return "_setjmp"+!TLI.usesUnderscoreSetJmp();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003706 case Intrinsic::longjmp:
3707 return "_longjmp"+!TLI.usesUnderscoreLongJmp();
Chris Lattner824b9582008-11-21 16:42:48 +00003708 case Intrinsic::memcpy: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003709 // Assert for address < 256 since we support only user defined address
3710 // spaces.
Eric Christopher551754c2010-04-16 23:37:20 +00003711 assert(cast<PointerType>(I.getOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003712 < 256 &&
Eric Christopher551754c2010-04-16 23:37:20 +00003713 cast<PointerType>(I.getOperand(2)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003714 < 256 &&
3715 "Unknown address space");
Eric Christopher551754c2010-04-16 23:37:20 +00003716 SDValue Op1 = getValue(I.getOperand(1));
3717 SDValue Op2 = getValue(I.getOperand(2));
3718 SDValue Op3 = getValue(I.getOperand(3));
3719 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3720 bool isVol = cast<ConstantInt>(I.getOperand(5))->getZExtValue();
Mon P Wang20adc9d2010-04-04 03:10:48 +00003721 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, isVol, false,
Eric Christopher551754c2010-04-16 23:37:20 +00003722 I.getOperand(1), 0, I.getOperand(2), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003723 return 0;
3724 }
Chris Lattner824b9582008-11-21 16:42:48 +00003725 case Intrinsic::memset: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003726 // Assert for address < 256 since we support only user defined address
3727 // spaces.
Eric Christopher551754c2010-04-16 23:37:20 +00003728 assert(cast<PointerType>(I.getOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003729 < 256 &&
3730 "Unknown address space");
Eric Christopher551754c2010-04-16 23:37:20 +00003731 SDValue Op1 = getValue(I.getOperand(1));
3732 SDValue Op2 = getValue(I.getOperand(2));
3733 SDValue Op3 = getValue(I.getOperand(3));
3734 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3735 bool isVol = cast<ConstantInt>(I.getOperand(5))->getZExtValue();
Mon P Wang20adc9d2010-04-04 03:10:48 +00003736 DAG.setRoot(DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
Eric Christopher551754c2010-04-16 23:37:20 +00003737 I.getOperand(1), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003738 return 0;
3739 }
Chris Lattner824b9582008-11-21 16:42:48 +00003740 case Intrinsic::memmove: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003741 // Assert for address < 256 since we support only user defined address
3742 // spaces.
Eric Christopher551754c2010-04-16 23:37:20 +00003743 assert(cast<PointerType>(I.getOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003744 < 256 &&
Eric Christopher551754c2010-04-16 23:37:20 +00003745 cast<PointerType>(I.getOperand(2)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003746 < 256 &&
3747 "Unknown address space");
Eric Christopher551754c2010-04-16 23:37:20 +00003748 SDValue Op1 = getValue(I.getOperand(1));
3749 SDValue Op2 = getValue(I.getOperand(2));
3750 SDValue Op3 = getValue(I.getOperand(3));
3751 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3752 bool isVol = cast<ConstantInt>(I.getOperand(5))->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003753
3754 // If the source and destination are known to not be aliases, we can
3755 // lower memmove as memcpy.
3756 uint64_t Size = -1ULL;
3757 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003758 Size = C->getZExtValue();
Eric Christopher551754c2010-04-16 23:37:20 +00003759 if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) ==
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003760 AliasAnalysis::NoAlias) {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003761 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
Eric Christopher551754c2010-04-16 23:37:20 +00003762 false, I.getOperand(1), 0, I.getOperand(2), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003763 return 0;
3764 }
3765
Mon P Wang20adc9d2010-04-04 03:10:48 +00003766 DAG.setRoot(DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
Eric Christopher551754c2010-04-16 23:37:20 +00003767 I.getOperand(1), 0, I.getOperand(2), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003768 return 0;
3769 }
Bill Wendling92c1e122009-02-13 02:16:35 +00003770 case Intrinsic::dbg_declare: {
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003771 // FIXME: currently, we get here only if OptLevel != CodeGenOpt::None.
3772 // The real handling of this intrinsic is in FastISel.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003773 if (OptLevel != CodeGenOpt::None)
Devang Patel7e1e31f2009-07-02 22:43:26 +00003774 // FIXME: Variable debug info is not supported here.
3775 return 0;
Dan Gohman46510a72010-04-15 01:51:59 +00003776 const DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
Chris Lattnerbf0ca2b2009-12-29 09:32:19 +00003777 if (!DIDescriptor::ValidDebugInfo(DI.getVariable(), CodeGenOpt::None))
Devang Patel7e1e31f2009-07-02 22:43:26 +00003778 return 0;
3779
Devang Patelac1ceb32009-10-09 22:42:28 +00003780 MDNode *Variable = DI.getVariable();
Dan Gohman46510a72010-04-15 01:51:59 +00003781 const Value *Address = DI.getAddress();
Dale Johannesen8ac38f22010-02-08 21:53:27 +00003782 if (!Address)
3783 return 0;
Dan Gohman46510a72010-04-15 01:51:59 +00003784 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
Devang Patel24f20e02009-08-22 17:12:53 +00003785 Address = BCI->getOperand(0);
Dan Gohman46510a72010-04-15 01:51:59 +00003786 const AllocaInst *AI = dyn_cast<AllocaInst>(Address);
Devang Patel24f20e02009-08-22 17:12:53 +00003787 // Don't handle byval struct arguments or VLAs, for example.
3788 if (!AI)
3789 return 0;
Devang Patelbd1d6a82009-09-05 00:34:14 +00003790 DenseMap<const AllocaInst*, int>::iterator SI =
3791 FuncInfo.StaticAllocaMap.find(AI);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003792 if (SI == FuncInfo.StaticAllocaMap.end())
Devang Patelbd1d6a82009-09-05 00:34:14 +00003793 return 0; // VLAs.
3794 int FI = SI->second;
Devang Patel70d75ca2009-11-12 19:02:56 +00003795
Chris Lattner512063d2010-04-05 06:19:28 +00003796 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
3797 if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo())
3798 MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003799 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00003800 }
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003801 case Intrinsic::dbg_value: {
Dan Gohman46510a72010-04-15 01:51:59 +00003802 const DbgValueInst &DI = cast<DbgValueInst>(I);
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003803 if (!DIDescriptor::ValidDebugInfo(DI.getVariable(), CodeGenOpt::None))
3804 return 0;
3805
3806 MDNode *Variable = DI.getVariable();
Devang Patel00190342010-03-15 19:15:44 +00003807 uint64_t Offset = DI.getOffset();
Dan Gohman46510a72010-04-15 01:51:59 +00003808 const Value *V = DI.getValue();
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003809 if (!V)
3810 return 0;
Devang Patel00190342010-03-15 19:15:44 +00003811
3812 // Build an entry in DbgOrdering. Debug info input nodes get an SDNodeOrder
3813 // but do not always have a corresponding SDNode built. The SDNodeOrder
3814 // absolute, but not relative, values are different depending on whether
3815 // debug info exists.
3816 ++SDNodeOrder;
3817 if (isa<ConstantInt>(V) || isa<ConstantFP>(V)) {
Evan Cheng31441b72010-03-29 20:48:30 +00003818 DAG.AddDbgValue(DAG.getDbgValue(Variable, V, Offset, dl, SDNodeOrder));
Devang Patel00190342010-03-15 19:15:44 +00003819 } else {
3820 SDValue &N = NodeMap[V];
Evan Cheng31441b72010-03-29 20:48:30 +00003821 if (N.getNode())
3822 DAG.AddDbgValue(DAG.getDbgValue(Variable, N.getNode(),
3823 N.getResNo(), Offset, dl, SDNodeOrder),
3824 N.getNode());
3825 else
Devang Patel00190342010-03-15 19:15:44 +00003826 // We may expand this to cover more cases. One case where we have no
3827 // data available is an unreferenced parameter; we need this fallback.
Evan Cheng31441b72010-03-29 20:48:30 +00003828 DAG.AddDbgValue(DAG.getDbgValue(Variable,
Devang Patel00190342010-03-15 19:15:44 +00003829 UndefValue::get(V->getType()),
Evan Cheng31441b72010-03-29 20:48:30 +00003830 Offset, dl, SDNodeOrder));
Devang Patel00190342010-03-15 19:15:44 +00003831 }
3832
3833 // Build a debug info table entry.
Dan Gohman46510a72010-04-15 01:51:59 +00003834 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(V))
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003835 V = BCI->getOperand(0);
Dan Gohman46510a72010-04-15 01:51:59 +00003836 const AllocaInst *AI = dyn_cast<AllocaInst>(V);
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003837 // Don't handle byval struct arguments or VLAs, for example.
3838 if (!AI)
3839 return 0;
3840 DenseMap<const AllocaInst*, int>::iterator SI =
3841 FuncInfo.StaticAllocaMap.find(AI);
3842 if (SI == FuncInfo.StaticAllocaMap.end())
3843 return 0; // VLAs.
3844 int FI = SI->second;
Chris Lattnerde4845c2010-04-02 19:42:39 +00003845
Chris Lattner512063d2010-04-05 06:19:28 +00003846 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
3847 if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo())
3848 MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc());
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003849 return 0;
3850 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003851 case Intrinsic::eh_exception: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003852 // Insert the EXCEPTIONADDR instruction.
Dan Gohman99be8ae2010-04-19 22:41:47 +00003853 assert(FuncInfo.MBBMap[I.getParent()]->isLandingPad() &&
3854 "Call to eh.exception not in landing pad!");
Owen Anderson825b72b2009-08-11 20:47:22 +00003855 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003856 SDValue Ops[1];
3857 Ops[0] = DAG.getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003858 SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003859 setValue(&I, Op);
3860 DAG.setRoot(Op.getValue(1));
3861 return 0;
3862 }
3863
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003864 case Intrinsic::eh_selector: {
Dan Gohman99be8ae2010-04-19 22:41:47 +00003865 MachineBasicBlock *CallMBB = FuncInfo.MBBMap[I.getParent()];
Chris Lattner512063d2010-04-05 06:19:28 +00003866 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Dan Gohman99be8ae2010-04-19 22:41:47 +00003867 if (CallMBB->isLandingPad())
3868 AddCatchInfo(I, &MMI, CallMBB);
Chris Lattner3a5815f2009-09-17 23:54:54 +00003869 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003870#ifndef NDEBUG
Chris Lattner3a5815f2009-09-17 23:54:54 +00003871 FuncInfo.CatchInfoLost.insert(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003872#endif
Chris Lattner3a5815f2009-09-17 23:54:54 +00003873 // FIXME: Mark exception selector register as live in. Hack for PR1508.
3874 unsigned Reg = TLI.getExceptionSelectorRegister();
Dan Gohman99be8ae2010-04-19 22:41:47 +00003875 if (Reg) FuncInfo.MBBMap[I.getParent()]->addLiveIn(Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003876 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003877
Chris Lattner3a5815f2009-09-17 23:54:54 +00003878 // Insert the EHSELECTION instruction.
3879 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
3880 SDValue Ops[2];
Eric Christopher551754c2010-04-16 23:37:20 +00003881 Ops[0] = getValue(I.getOperand(1));
Chris Lattner3a5815f2009-09-17 23:54:54 +00003882 Ops[1] = getRoot();
3883 SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2);
Chris Lattner3a5815f2009-09-17 23:54:54 +00003884 DAG.setRoot(Op.getValue(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00003885 setValue(&I, DAG.getSExtOrTrunc(Op, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003886 return 0;
3887 }
3888
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003889 case Intrinsic::eh_typeid_for: {
Chris Lattner512063d2010-04-05 06:19:28 +00003890 // Find the type id for the given typeinfo.
Eric Christopher551754c2010-04-16 23:37:20 +00003891 GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1));
Chris Lattner512063d2010-04-05 06:19:28 +00003892 unsigned TypeID = DAG.getMachineFunction().getMMI().getTypeIDFor(GV);
3893 Res = DAG.getConstant(TypeID, MVT::i32);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003894 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003895 return 0;
3896 }
3897
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003898 case Intrinsic::eh_return_i32:
3899 case Intrinsic::eh_return_i64:
Chris Lattner512063d2010-04-05 06:19:28 +00003900 DAG.getMachineFunction().getMMI().setCallsEHReturn(true);
3901 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, dl,
3902 MVT::Other,
3903 getControlRoot(),
Eric Christopher551754c2010-04-16 23:37:20 +00003904 getValue(I.getOperand(1)),
3905 getValue(I.getOperand(2))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003906 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003907 case Intrinsic::eh_unwind_init:
Chris Lattner512063d2010-04-05 06:19:28 +00003908 DAG.getMachineFunction().getMMI().setCallsUnwindInit(true);
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003909 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003910 case Intrinsic::eh_dwarf_cfa: {
Eric Christopher551754c2010-04-16 23:37:20 +00003911 EVT VT = getValue(I.getOperand(1)).getValueType();
3912 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getOperand(1)), dl,
Duncan Sands3a66a682009-10-13 21:04:12 +00003913 TLI.getPointerTy());
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003914 SDValue Offset = DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003915 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003916 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003917 TLI.getPointerTy()),
3918 CfaArg);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003919 SDValue FA = DAG.getNode(ISD::FRAMEADDR, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003920 TLI.getPointerTy(),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003921 DAG.getConstant(0, TLI.getPointerTy()));
Bill Wendling4533cac2010-01-28 21:51:40 +00003922 setValue(&I, DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
3923 FA, Offset));
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003924 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003925 }
Jim Grosbachca752c92010-01-28 01:45:32 +00003926 case Intrinsic::eh_sjlj_callsite: {
Chris Lattner512063d2010-04-05 06:19:28 +00003927 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Eric Christopher551754c2010-04-16 23:37:20 +00003928 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1));
Jim Grosbachca752c92010-01-28 01:45:32 +00003929 assert(CI && "Non-constant call site value in eh.sjlj.callsite!");
Chris Lattner512063d2010-04-05 06:19:28 +00003930 assert(MMI.getCurrentCallSite() == 0 && "Overlapping call sites!");
Jim Grosbachca752c92010-01-28 01:45:32 +00003931
Chris Lattner512063d2010-04-05 06:19:28 +00003932 MMI.setCurrentCallSite(CI->getZExtValue());
Jim Grosbachca752c92010-01-28 01:45:32 +00003933 return 0;
3934 }
3935
Mon P Wang77cdf302008-11-10 20:54:11 +00003936 case Intrinsic::convertff:
3937 case Intrinsic::convertfsi:
3938 case Intrinsic::convertfui:
3939 case Intrinsic::convertsif:
3940 case Intrinsic::convertuif:
3941 case Intrinsic::convertss:
3942 case Intrinsic::convertsu:
3943 case Intrinsic::convertus:
3944 case Intrinsic::convertuu: {
3945 ISD::CvtCode Code = ISD::CVT_INVALID;
3946 switch (Intrinsic) {
3947 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
3948 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
3949 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
3950 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
3951 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
3952 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
3953 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
3954 case Intrinsic::convertus: Code = ISD::CVT_US; break;
3955 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
3956 }
Owen Andersone50ed302009-08-10 22:56:29 +00003957 EVT DestVT = TLI.getValueType(I.getType());
Eric Christopher551754c2010-04-16 23:37:20 +00003958 const Value *Op1 = I.getOperand(1);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003959 Res = DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1),
3960 DAG.getValueType(DestVT),
3961 DAG.getValueType(getValue(Op1).getValueType()),
3962 getValue(I.getOperand(2)),
Eric Christopher551754c2010-04-16 23:37:20 +00003963 getValue(I.getOperand(3)),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003964 Code);
3965 setValue(&I, Res);
Mon P Wang77cdf302008-11-10 20:54:11 +00003966 return 0;
3967 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003968 case Intrinsic::sqrt:
Bill Wendling4533cac2010-01-28 21:51:40 +00003969 setValue(&I, DAG.getNode(ISD::FSQRT, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003970 getValue(I.getOperand(1)).getValueType(),
3971 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003972 return 0;
3973 case Intrinsic::powi:
Eric Christopher551754c2010-04-16 23:37:20 +00003974 setValue(&I, ExpandPowI(dl, getValue(I.getOperand(1)),
3975 getValue(I.getOperand(2)), DAG));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003976 return 0;
3977 case Intrinsic::sin:
Bill Wendling4533cac2010-01-28 21:51:40 +00003978 setValue(&I, DAG.getNode(ISD::FSIN, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003979 getValue(I.getOperand(1)).getValueType(),
3980 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003981 return 0;
3982 case Intrinsic::cos:
Bill Wendling4533cac2010-01-28 21:51:40 +00003983 setValue(&I, DAG.getNode(ISD::FCOS, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003984 getValue(I.getOperand(1)).getValueType(),
3985 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003986 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003987 case Intrinsic::log:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003988 visitLog(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003989 return 0;
3990 case Intrinsic::log2:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003991 visitLog2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003992 return 0;
3993 case Intrinsic::log10:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003994 visitLog10(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003995 return 0;
3996 case Intrinsic::exp:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003997 visitExp(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003998 return 0;
3999 case Intrinsic::exp2:
Dale Johannesen601d3c02008-09-05 01:48:15 +00004000 visitExp2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004001 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004002 case Intrinsic::pow:
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004003 visitPow(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004004 return 0;
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004005 case Intrinsic::convert_to_fp16:
4006 setValue(&I, DAG.getNode(ISD::FP32_TO_FP16, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00004007 MVT::i16, getValue(I.getOperand(1))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004008 return 0;
4009 case Intrinsic::convert_from_fp16:
4010 setValue(&I, DAG.getNode(ISD::FP16_TO_FP32, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00004011 MVT::f32, getValue(I.getOperand(1))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00004012 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004013 case Intrinsic::pcmarker: {
Eric Christopher551754c2010-04-16 23:37:20 +00004014 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00004015 DAG.setRoot(DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004016 return 0;
4017 }
4018 case Intrinsic::readcyclecounter: {
4019 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004020 Res = DAG.getNode(ISD::READCYCLECOUNTER, dl,
4021 DAG.getVTList(MVT::i64, MVT::Other),
4022 &Op, 1);
4023 setValue(&I, Res);
4024 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004025 return 0;
4026 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004027 case Intrinsic::bswap:
Bill Wendling4533cac2010-01-28 21:51:40 +00004028 setValue(&I, DAG.getNode(ISD::BSWAP, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00004029 getValue(I.getOperand(1)).getValueType(),
4030 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004031 return 0;
4032 case Intrinsic::cttz: {
Eric Christopher551754c2010-04-16 23:37:20 +00004033 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004034 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004035 setValue(&I, DAG.getNode(ISD::CTTZ, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004036 return 0;
4037 }
4038 case Intrinsic::ctlz: {
Eric Christopher551754c2010-04-16 23:37:20 +00004039 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004040 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004041 setValue(&I, DAG.getNode(ISD::CTLZ, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004042 return 0;
4043 }
4044 case Intrinsic::ctpop: {
Eric Christopher551754c2010-04-16 23:37:20 +00004045 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004046 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004047 setValue(&I, DAG.getNode(ISD::CTPOP, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004048 return 0;
4049 }
4050 case Intrinsic::stacksave: {
4051 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004052 Res = DAG.getNode(ISD::STACKSAVE, dl,
4053 DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1);
4054 setValue(&I, Res);
4055 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004056 return 0;
4057 }
4058 case Intrinsic::stackrestore: {
Eric Christopher551754c2010-04-16 23:37:20 +00004059 Res = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00004060 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Res));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004061 return 0;
4062 }
Bill Wendling57344502008-11-18 11:01:33 +00004063 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00004064 // Emit code into the DAG to store the stack guard onto the stack.
4065 MachineFunction &MF = DAG.getMachineFunction();
4066 MachineFrameInfo *MFI = MF.getFrameInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004067 EVT PtrTy = TLI.getPointerTy();
Bill Wendlingb2a42982008-11-06 02:29:10 +00004068
Eric Christopher551754c2010-04-16 23:37:20 +00004069 SDValue Src = getValue(I.getOperand(1)); // The guard's value.
4070 AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2));
Bill Wendlingb2a42982008-11-06 02:29:10 +00004071
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004072 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00004073 MFI->setStackProtectorIndex(FI);
4074
4075 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4076
4077 // Store the stack protector onto the stack.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004078 Res = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN,
4079 PseudoSourceValue::getFixedStack(FI),
David Greene1e559442010-02-15 17:00:31 +00004080 0, true, false, 0);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004081 setValue(&I, Res);
4082 DAG.setRoot(Res);
Bill Wendlingb2a42982008-11-06 02:29:10 +00004083 return 0;
4084 }
Eric Christopher7b5e6172009-10-27 00:52:25 +00004085 case Intrinsic::objectsize: {
4086 // If we don't know by now, we're never going to know.
Eric Christopher551754c2010-04-16 23:37:20 +00004087 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2));
Eric Christopher7b5e6172009-10-27 00:52:25 +00004088
4089 assert(CI && "Non-constant type in __builtin_object_size?");
4090
Eric Christopher551754c2010-04-16 23:37:20 +00004091 SDValue Arg = getValue(I.getOperand(0));
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00004092 EVT Ty = Arg.getValueType();
4093
Eric Christopherd060b252009-12-23 02:51:48 +00004094 if (CI->getZExtValue() == 0)
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004095 Res = DAG.getConstant(-1ULL, Ty);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004096 else
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004097 Res = DAG.getConstant(0, Ty);
4098
4099 setValue(&I, Res);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004100 return 0;
4101 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004102 case Intrinsic::var_annotation:
4103 // Discard annotate attributes
4104 return 0;
4105
4106 case Intrinsic::init_trampoline: {
Eric Christopher551754c2010-04-16 23:37:20 +00004107 const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004108
4109 SDValue Ops[6];
4110 Ops[0] = getRoot();
Eric Christopher551754c2010-04-16 23:37:20 +00004111 Ops[1] = getValue(I.getOperand(1));
4112 Ops[2] = getValue(I.getOperand(2));
4113 Ops[3] = getValue(I.getOperand(3));
4114 Ops[4] = DAG.getSrcValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004115 Ops[5] = DAG.getSrcValue(F);
4116
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004117 Res = DAG.getNode(ISD::TRAMPOLINE, dl,
4118 DAG.getVTList(TLI.getPointerTy(), MVT::Other),
4119 Ops, 6);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004120
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004121 setValue(&I, Res);
4122 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004123 return 0;
4124 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004125 case Intrinsic::gcroot:
4126 if (GFI) {
Eric Christopher551754c2010-04-16 23:37:20 +00004127 const Value *Alloca = I.getOperand(1);
4128 const Constant *TypeMap = cast<Constant>(I.getOperand(2));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004129
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004130 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
4131 GFI->addStackRoot(FI->getIndex(), TypeMap);
4132 }
4133 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004134 case Intrinsic::gcread:
4135 case Intrinsic::gcwrite:
Torok Edwinc23197a2009-07-14 16:55:14 +00004136 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004137 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004138 case Intrinsic::flt_rounds:
Bill Wendling4533cac2010-01-28 21:51:40 +00004139 setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004140 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004141 case Intrinsic::trap:
Bill Wendling4533cac2010-01-28 21:51:40 +00004142 DAG.setRoot(DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004143 return 0;
Bill Wendlingef375462008-11-21 02:38:44 +00004144 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00004145 return implVisitAluOverflow(I, ISD::UADDO);
4146 case Intrinsic::sadd_with_overflow:
4147 return implVisitAluOverflow(I, ISD::SADDO);
4148 case Intrinsic::usub_with_overflow:
4149 return implVisitAluOverflow(I, ISD::USUBO);
4150 case Intrinsic::ssub_with_overflow:
4151 return implVisitAluOverflow(I, ISD::SSUBO);
4152 case Intrinsic::umul_with_overflow:
4153 return implVisitAluOverflow(I, ISD::UMULO);
4154 case Intrinsic::smul_with_overflow:
4155 return implVisitAluOverflow(I, ISD::SMULO);
Bill Wendling7cdc3c82008-11-21 02:03:52 +00004156
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004157 case Intrinsic::prefetch: {
4158 SDValue Ops[4];
4159 Ops[0] = getRoot();
Eric Christopher551754c2010-04-16 23:37:20 +00004160 Ops[1] = getValue(I.getOperand(1));
4161 Ops[2] = getValue(I.getOperand(2));
4162 Ops[3] = getValue(I.getOperand(3));
Bill Wendling4533cac2010-01-28 21:51:40 +00004163 DAG.setRoot(DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004164 return 0;
4165 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004166
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004167 case Intrinsic::memory_barrier: {
4168 SDValue Ops[6];
4169 Ops[0] = getRoot();
4170 for (int x = 1; x < 6; ++x)
Eric Christopher551754c2010-04-16 23:37:20 +00004171 Ops[x] = getValue(I.getOperand(x));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004172
Bill Wendling4533cac2010-01-28 21:51:40 +00004173 DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004174 return 0;
4175 }
4176 case Intrinsic::atomic_cmp_swap: {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004177 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004178 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00004179 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(),
Eric Christopher551754c2010-04-16 23:37:20 +00004180 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004181 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004182 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004183 getValue(I.getOperand(2)),
Eric Christopher551754c2010-04-16 23:37:20 +00004184 getValue(I.getOperand(3)),
4185 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004186 setValue(&I, L);
4187 DAG.setRoot(L.getValue(1));
4188 return 0;
4189 }
4190 case Intrinsic::atomic_load_add:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004191 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004192 case Intrinsic::atomic_load_sub:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004193 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004194 case Intrinsic::atomic_load_or:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004195 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004196 case Intrinsic::atomic_load_xor:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004197 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004198 case Intrinsic::atomic_load_and:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004199 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004200 case Intrinsic::atomic_load_nand:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004201 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004202 case Intrinsic::atomic_load_max:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004203 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004204 case Intrinsic::atomic_load_min:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004205 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004206 case Intrinsic::atomic_load_umin:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004207 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004208 case Intrinsic::atomic_load_umax:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004209 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004210 case Intrinsic::atomic_swap:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004211 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP);
Duncan Sandsf07c9492009-11-10 09:08:09 +00004212
4213 case Intrinsic::invariant_start:
4214 case Intrinsic::lifetime_start:
4215 // Discard region information.
Bill Wendling4533cac2010-01-28 21:51:40 +00004216 setValue(&I, DAG.getUNDEF(TLI.getPointerTy()));
Duncan Sandsf07c9492009-11-10 09:08:09 +00004217 return 0;
4218 case Intrinsic::invariant_end:
4219 case Intrinsic::lifetime_end:
4220 // Discard region information.
4221 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004222 }
4223}
4224
Dan Gohman46510a72010-04-15 01:51:59 +00004225void SelectionDAGBuilder::LowerCallTo(ImmutableCallSite CS, SDValue Callee,
Dan Gohman2048b852009-11-23 18:04:58 +00004226 bool isTailCall,
4227 MachineBasicBlock *LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004228 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
4229 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004230 const Type *RetTy = FTy->getReturnType();
Chris Lattner512063d2010-04-05 06:19:28 +00004231 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Chris Lattner16112732010-03-14 01:41:15 +00004232 MCSymbol *BeginLabel = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004233
4234 TargetLowering::ArgListTy Args;
4235 TargetLowering::ArgListEntry Entry;
4236 Args.reserve(CS.arg_size());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004237
4238 // Check whether the function can return without sret-demotion.
4239 SmallVector<EVT, 4> OutVTs;
4240 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
4241 SmallVector<uint64_t, 4> Offsets;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004242 getReturnInfo(RetTy, CS.getAttributes().getRetAttributes(),
Bill Wendlinge80ae832009-12-22 00:50:32 +00004243 OutVTs, OutsFlags, TLI, &Offsets);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004244
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004245 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004246 FTy->isVarArg(), OutVTs, OutsFlags, DAG);
4247
4248 SDValue DemoteStackSlot;
4249
4250 if (!CanLowerReturn) {
4251 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(
4252 FTy->getReturnType());
4253 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(
4254 FTy->getReturnType());
4255 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00004256 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004257 const Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType());
4258
4259 DemoteStackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4260 Entry.Node = DemoteStackSlot;
4261 Entry.Ty = StackSlotPtrType;
4262 Entry.isSExt = false;
4263 Entry.isZExt = false;
4264 Entry.isInReg = false;
4265 Entry.isSRet = true;
4266 Entry.isNest = false;
4267 Entry.isByVal = false;
4268 Entry.Alignment = Align;
4269 Args.push_back(Entry);
4270 RetTy = Type::getVoidTy(FTy->getContext());
4271 }
4272
Dan Gohman46510a72010-04-15 01:51:59 +00004273 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004274 i != e; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004275 SDValue ArgNode = getValue(*i);
4276 Entry.Node = ArgNode; Entry.Ty = (*i)->getType();
4277
4278 unsigned attrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00004279 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
4280 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
4281 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
4282 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
4283 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
4284 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004285 Entry.Alignment = CS.getParamAlignment(attrInd);
4286 Args.push_back(Entry);
4287 }
4288
Chris Lattner512063d2010-04-05 06:19:28 +00004289 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004290 // Insert a label before the invoke call to mark the try range. This can be
4291 // used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00004292 BeginLabel = MMI.getContext().CreateTempSymbol();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00004293
Jim Grosbachca752c92010-01-28 01:45:32 +00004294 // For SjLj, keep track of which landing pads go with which invokes
4295 // so as to maintain the ordering of pads in the LSDA.
Chris Lattner512063d2010-04-05 06:19:28 +00004296 unsigned CallSiteIndex = MMI.getCurrentCallSite();
Jim Grosbachca752c92010-01-28 01:45:32 +00004297 if (CallSiteIndex) {
Chris Lattner512063d2010-04-05 06:19:28 +00004298 MMI.setCallSiteBeginLabel(BeginLabel, CallSiteIndex);
Jim Grosbachca752c92010-01-28 01:45:32 +00004299 // Now that the call site is handled, stop tracking it.
Chris Lattner512063d2010-04-05 06:19:28 +00004300 MMI.setCurrentCallSite(0);
Jim Grosbachca752c92010-01-28 01:45:32 +00004301 }
4302
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004303 // Both PendingLoads and PendingExports must be flushed here;
4304 // this call might not return.
4305 (void)getRoot();
Chris Lattner7561d482010-03-14 02:33:54 +00004306 DAG.setRoot(DAG.getEHLabel(getCurDebugLoc(), getControlRoot(), BeginLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004307 }
4308
Dan Gohman98ca4f22009-08-05 01:29:28 +00004309 // Check if target-independent constraints permit a tail call here.
4310 // Target-dependent constraints are checked within TLI.LowerCallTo.
4311 if (isTailCall &&
Evan Cheng86809cc2010-02-03 03:28:02 +00004312 !isInTailCallPosition(CS, CS.getAttributes().getRetAttributes(), TLI))
Dan Gohman98ca4f22009-08-05 01:29:28 +00004313 isTailCall = false;
4314
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004315 std::pair<SDValue,SDValue> Result =
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004316 TLI.LowerCallTo(getRoot(), RetTy,
Devang Patel05988662008-09-25 21:00:45 +00004317 CS.paramHasAttr(0, Attribute::SExt),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004318 CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(),
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00004319 CS.paramHasAttr(0, Attribute::InReg), FTy->getNumParams(),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004320 CS.getCallingConv(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00004321 isTailCall,
4322 !CS.getInstruction()->use_empty(),
Bill Wendling46ada192010-03-02 01:55:18 +00004323 Callee, Args, DAG, getCurDebugLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00004324 assert((isTailCall || Result.second.getNode()) &&
4325 "Non-null chain expected with non-tail call!");
4326 assert((Result.second.getNode() || !Result.first.getNode()) &&
4327 "Null value expected with tail call!");
Bill Wendlinge80ae832009-12-22 00:50:32 +00004328 if (Result.first.getNode()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004329 setValue(CS.getInstruction(), Result.first);
Bill Wendlinge80ae832009-12-22 00:50:32 +00004330 } else if (!CanLowerReturn && Result.second.getNode()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004331 // The instruction result is the result of loading from the
4332 // hidden sret parameter.
4333 SmallVector<EVT, 1> PVTs;
4334 const Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType());
4335
4336 ComputeValueVTs(TLI, PtrRetTy, PVTs);
4337 assert(PVTs.size() == 1 && "Pointers should fit in one register");
4338 EVT PtrVT = PVTs[0];
4339 unsigned NumValues = OutVTs.size();
4340 SmallVector<SDValue, 4> Values(NumValues);
4341 SmallVector<SDValue, 4> Chains(NumValues);
4342
4343 for (unsigned i = 0; i < NumValues; ++i) {
Bill Wendlinge80ae832009-12-22 00:50:32 +00004344 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT,
4345 DemoteStackSlot,
4346 DAG.getConstant(Offsets[i], PtrVT));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004347 SDValue L = DAG.getLoad(OutVTs[i], getCurDebugLoc(), Result.second,
David Greene1e559442010-02-15 17:00:31 +00004348 Add, NULL, Offsets[i], false, false, 1);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004349 Values[i] = L;
4350 Chains[i] = L.getValue(1);
4351 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00004352
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004353 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
4354 MVT::Other, &Chains[0], NumValues);
4355 PendingLoads.push_back(Chain);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004356
4357 // Collect the legal value parts into potentially illegal values
4358 // that correspond to the original function's return values.
4359 SmallVector<EVT, 4> RetTys;
4360 RetTy = FTy->getReturnType();
4361 ComputeValueVTs(TLI, RetTy, RetTys);
4362 ISD::NodeType AssertOp = ISD::DELETED_NODE;
4363 SmallVector<SDValue, 4> ReturnValues;
4364 unsigned CurReg = 0;
4365 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
4366 EVT VT = RetTys[I];
4367 EVT RegisterVT = TLI.getRegisterType(RetTy->getContext(), VT);
4368 unsigned NumRegs = TLI.getNumRegisters(RetTy->getContext(), VT);
4369
4370 SDValue ReturnValue =
Bill Wendling46ada192010-03-02 01:55:18 +00004371 getCopyFromParts(DAG, getCurDebugLoc(), &Values[CurReg], NumRegs,
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004372 RegisterVT, VT, AssertOp);
4373 ReturnValues.push_back(ReturnValue);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004374 CurReg += NumRegs;
4375 }
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004376
Bill Wendling4533cac2010-01-28 21:51:40 +00004377 setValue(CS.getInstruction(),
4378 DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
4379 DAG.getVTList(&RetTys[0], RetTys.size()),
4380 &ReturnValues[0], ReturnValues.size()));
Bill Wendlinge80ae832009-12-22 00:50:32 +00004381
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004382 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00004383
4384 // As a special case, a null chain means that a tail call has been emitted and
4385 // the DAG root is already updated.
Bill Wendling4533cac2010-01-28 21:51:40 +00004386 if (Result.second.getNode())
Dan Gohman98ca4f22009-08-05 01:29:28 +00004387 DAG.setRoot(Result.second);
Bill Wendling4533cac2010-01-28 21:51:40 +00004388 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00004389 HasTailCall = true;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004390
Chris Lattner512063d2010-04-05 06:19:28 +00004391 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004392 // Insert a label at the end of the invoke call to mark the try range. This
4393 // can be used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00004394 MCSymbol *EndLabel = MMI.getContext().CreateTempSymbol();
Chris Lattner7561d482010-03-14 02:33:54 +00004395 DAG.setRoot(DAG.getEHLabel(getCurDebugLoc(), getRoot(), EndLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004396
4397 // Inform MachineModuleInfo of range.
Chris Lattner512063d2010-04-05 06:19:28 +00004398 MMI.addInvoke(LandingPad, BeginLabel, EndLabel);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004399 }
4400}
4401
Chris Lattner8047d9a2009-12-24 00:37:38 +00004402/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
4403/// value is equal or not-equal to zero.
Dan Gohman46510a72010-04-15 01:51:59 +00004404static bool IsOnlyUsedInZeroEqualityComparison(const Value *V) {
4405 for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end();
Chris Lattner8047d9a2009-12-24 00:37:38 +00004406 UI != E; ++UI) {
Dan Gohman46510a72010-04-15 01:51:59 +00004407 if (const ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
Chris Lattner8047d9a2009-12-24 00:37:38 +00004408 if (IC->isEquality())
Dan Gohman46510a72010-04-15 01:51:59 +00004409 if (const Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
Chris Lattner8047d9a2009-12-24 00:37:38 +00004410 if (C->isNullValue())
4411 continue;
4412 // Unknown instruction.
4413 return false;
4414 }
4415 return true;
4416}
4417
Dan Gohman46510a72010-04-15 01:51:59 +00004418static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT,
4419 const Type *LoadTy,
Chris Lattner8047d9a2009-12-24 00:37:38 +00004420 SelectionDAGBuilder &Builder) {
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004421
Chris Lattner8047d9a2009-12-24 00:37:38 +00004422 // Check to see if this load can be trivially constant folded, e.g. if the
4423 // input is from a string literal.
Dan Gohman46510a72010-04-15 01:51:59 +00004424 if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00004425 // Cast pointer to the type we really want to load.
Dan Gohman46510a72010-04-15 01:51:59 +00004426 LoadInput = ConstantExpr::getBitCast(const_cast<Constant *>(LoadInput),
Chris Lattner8047d9a2009-12-24 00:37:38 +00004427 PointerType::getUnqual(LoadTy));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004428
Dan Gohman46510a72010-04-15 01:51:59 +00004429 if (const Constant *LoadCst =
4430 ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput),
4431 Builder.TD))
Chris Lattner8047d9a2009-12-24 00:37:38 +00004432 return Builder.getValue(LoadCst);
4433 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004434
Chris Lattner8047d9a2009-12-24 00:37:38 +00004435 // Otherwise, we have to emit the load. If the pointer is to unfoldable but
4436 // still constant memory, the input chain can be the entry node.
4437 SDValue Root;
4438 bool ConstantMemory = false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004439
Chris Lattner8047d9a2009-12-24 00:37:38 +00004440 // Do not serialize (non-volatile) loads of constant memory with anything.
4441 if (Builder.AA->pointsToConstantMemory(PtrVal)) {
4442 Root = Builder.DAG.getEntryNode();
4443 ConstantMemory = true;
4444 } else {
4445 // Do not serialize non-volatile loads against each other.
4446 Root = Builder.DAG.getRoot();
4447 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004448
Chris Lattner8047d9a2009-12-24 00:37:38 +00004449 SDValue Ptr = Builder.getValue(PtrVal);
4450 SDValue LoadVal = Builder.DAG.getLoad(LoadVT, Builder.getCurDebugLoc(), Root,
4451 Ptr, PtrVal /*SrcValue*/, 0/*SVOffset*/,
David Greene1e559442010-02-15 17:00:31 +00004452 false /*volatile*/,
4453 false /*nontemporal*/, 1 /* align=1 */);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004454
Chris Lattner8047d9a2009-12-24 00:37:38 +00004455 if (!ConstantMemory)
4456 Builder.PendingLoads.push_back(LoadVal.getValue(1));
4457 return LoadVal;
4458}
4459
4460
4461/// visitMemCmpCall - See if we can lower a call to memcmp in an optimized form.
4462/// If so, return true and lower it, otherwise return false and it will be
4463/// lowered like a normal call.
Dan Gohman46510a72010-04-15 01:51:59 +00004464bool SelectionDAGBuilder::visitMemCmpCall(const CallInst &I) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00004465 // Verify that the prototype makes sense. int memcmp(void*,void*,size_t)
4466 if (I.getNumOperands() != 4)
4467 return false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004468
Eric Christopher551754c2010-04-16 23:37:20 +00004469 const Value *LHS = I.getOperand(1), *RHS = I.getOperand(2);
Duncan Sands1df98592010-02-16 11:11:14 +00004470 if (!LHS->getType()->isPointerTy() || !RHS->getType()->isPointerTy() ||
Eric Christopher551754c2010-04-16 23:37:20 +00004471 !I.getOperand(3)->getType()->isIntegerTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +00004472 !I.getType()->isIntegerTy())
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004473 return false;
4474
Eric Christopher551754c2010-04-16 23:37:20 +00004475 const ConstantInt *Size = dyn_cast<ConstantInt>(I.getOperand(3));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004476
Chris Lattner8047d9a2009-12-24 00:37:38 +00004477 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
4478 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
Chris Lattner04b091a2009-12-24 01:07:17 +00004479 if (Size && IsOnlyUsedInZeroEqualityComparison(&I)) {
4480 bool ActuallyDoIt = true;
4481 MVT LoadVT;
4482 const Type *LoadTy;
4483 switch (Size->getZExtValue()) {
4484 default:
4485 LoadVT = MVT::Other;
4486 LoadTy = 0;
4487 ActuallyDoIt = false;
4488 break;
4489 case 2:
4490 LoadVT = MVT::i16;
4491 LoadTy = Type::getInt16Ty(Size->getContext());
4492 break;
4493 case 4:
4494 LoadVT = MVT::i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004495 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00004496 break;
4497 case 8:
4498 LoadVT = MVT::i64;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004499 LoadTy = Type::getInt64Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00004500 break;
4501 /*
4502 case 16:
4503 LoadVT = MVT::v4i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004504 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00004505 LoadTy = VectorType::get(LoadTy, 4);
4506 break;
4507 */
4508 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004509
Chris Lattner04b091a2009-12-24 01:07:17 +00004510 // This turns into unaligned loads. We only do this if the target natively
4511 // supports the MVT we'll be loading or if it is small enough (<= 4) that
4512 // we'll only produce a small number of byte loads.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004513
Chris Lattner04b091a2009-12-24 01:07:17 +00004514 // Require that we can find a legal MVT, and only do this if the target
4515 // supports unaligned loads of that type. Expanding into byte loads would
4516 // bloat the code.
4517 if (ActuallyDoIt && Size->getZExtValue() > 4) {
4518 // TODO: Handle 5 byte compare as 4-byte + 1 byte.
4519 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
4520 if (!TLI.isTypeLegal(LoadVT) ||!TLI.allowsUnalignedMemoryAccesses(LoadVT))
4521 ActuallyDoIt = false;
4522 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004523
Chris Lattner04b091a2009-12-24 01:07:17 +00004524 if (ActuallyDoIt) {
4525 SDValue LHSVal = getMemCmpLoad(LHS, LoadVT, LoadTy, *this);
4526 SDValue RHSVal = getMemCmpLoad(RHS, LoadVT, LoadTy, *this);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004527
Chris Lattner04b091a2009-12-24 01:07:17 +00004528 SDValue Res = DAG.getSetCC(getCurDebugLoc(), MVT::i1, LHSVal, RHSVal,
4529 ISD::SETNE);
4530 EVT CallVT = TLI.getValueType(I.getType(), true);
4531 setValue(&I, DAG.getZExtOrTrunc(Res, getCurDebugLoc(), CallVT));
4532 return true;
4533 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00004534 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004535
4536
Chris Lattner8047d9a2009-12-24 00:37:38 +00004537 return false;
4538}
4539
4540
Dan Gohman46510a72010-04-15 01:51:59 +00004541void SelectionDAGBuilder::visitCall(const CallInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004542 const char *RenameFn = 0;
4543 if (Function *F = I.getCalledFunction()) {
4544 if (F->isDeclaration()) {
Dan Gohman55e59c12010-04-19 19:05:59 +00004545 const TargetIntrinsicInfo *II = TM.getIntrinsicInfo();
Dale Johannesen49de9822009-02-05 01:49:45 +00004546 if (II) {
4547 if (unsigned IID = II->getIntrinsicID(F)) {
4548 RenameFn = visitIntrinsicCall(I, IID);
4549 if (!RenameFn)
4550 return;
4551 }
4552 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004553 if (unsigned IID = F->getIntrinsicID()) {
4554 RenameFn = visitIntrinsicCall(I, IID);
4555 if (!RenameFn)
4556 return;
4557 }
4558 }
4559
4560 // Check for well-known libc/libm calls. If the function is internal, it
4561 // can't be a library call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004562 if (!F->hasLocalLinkage() && F->hasName()) {
4563 StringRef Name = F->getName();
Duncan Sandsd2c817e2010-03-14 21:08:40 +00004564 if (Name == "copysign" || Name == "copysignf" || Name == "copysignl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004565 if (I.getNumOperands() == 3 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004566 I.getOperand(1)->getType()->isFloatingPointTy() &&
4567 I.getType() == I.getOperand(1)->getType() &&
4568 I.getType() == I.getOperand(2)->getType()) {
4569 SDValue LHS = getValue(I.getOperand(1));
4570 SDValue RHS = getValue(I.getOperand(2));
Bill Wendling0d580132009-12-23 01:28:19 +00004571 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(),
4572 LHS.getValueType(), LHS, RHS));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004573 return;
4574 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004575 } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004576 if (I.getNumOperands() == 2 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004577 I.getOperand(1)->getType()->isFloatingPointTy() &&
4578 I.getType() == I.getOperand(1)->getType()) {
4579 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00004580 setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(),
4581 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004582 return;
4583 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004584 } else if (Name == "sin" || Name == "sinf" || Name == "sinl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004585 if (I.getNumOperands() == 2 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004586 I.getOperand(1)->getType()->isFloatingPointTy() &&
4587 I.getType() == I.getOperand(1)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004588 I.onlyReadsMemory()) {
Eric Christopher551754c2010-04-16 23:37:20 +00004589 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00004590 setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(),
4591 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004592 return;
4593 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004594 } else if (Name == "cos" || Name == "cosf" || Name == "cosl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004595 if (I.getNumOperands() == 2 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004596 I.getOperand(1)->getType()->isFloatingPointTy() &&
4597 I.getType() == I.getOperand(1)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004598 I.onlyReadsMemory()) {
Eric Christopher551754c2010-04-16 23:37:20 +00004599 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00004600 setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(),
4601 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004602 return;
4603 }
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004604 } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") {
4605 if (I.getNumOperands() == 2 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004606 I.getOperand(1)->getType()->isFloatingPointTy() &&
4607 I.getType() == I.getOperand(1)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004608 I.onlyReadsMemory()) {
Eric Christopher551754c2010-04-16 23:37:20 +00004609 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00004610 setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(),
4611 Tmp.getValueType(), Tmp));
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004612 return;
4613 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00004614 } else if (Name == "memcmp") {
4615 if (visitMemCmpCall(I))
4616 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004617 }
4618 }
Eric Christopher551754c2010-04-16 23:37:20 +00004619 } else if (isa<InlineAsm>(I.getOperand(0))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004620 visitInlineAsm(&I);
4621 return;
4622 }
4623
4624 SDValue Callee;
4625 if (!RenameFn)
Eric Christopher551754c2010-04-16 23:37:20 +00004626 Callee = getValue(I.getOperand(0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004627 else
Bill Wendling056292f2008-09-16 21:48:12 +00004628 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004629
Bill Wendling0d580132009-12-23 01:28:19 +00004630 // Check if we can potentially perform a tail call. More detailed checking is
4631 // be done within LowerCallTo, after more information about the call is known.
Evan Cheng11e67932010-01-26 23:13:04 +00004632 LowerCallTo(&I, Callee, I.isTailCall());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004633}
4634
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004635/// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004636/// this value and returns the result as a ValueVT value. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004637/// Chain/Flag as the input and updates them for the output Chain/Flag.
4638/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004639SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling46ada192010-03-02 01:55:18 +00004640 SDValue &Chain, SDValue *Flag) const {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004641 // Assemble the legal parts into the final values.
4642 SmallVector<SDValue, 4> Values(ValueVTs.size());
4643 SmallVector<SDValue, 8> Parts;
4644 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
4645 // Copy the legal parts from the registers.
Owen Andersone50ed302009-08-10 22:56:29 +00004646 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00004647 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00004648 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004649
4650 Parts.resize(NumRegs);
4651 for (unsigned i = 0; i != NumRegs; ++i) {
4652 SDValue P;
Bill Wendlingec72e322009-12-22 01:11:43 +00004653 if (Flag == 0) {
Dale Johannesena04b7572009-02-03 23:04:43 +00004654 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
Bill Wendlingec72e322009-12-22 01:11:43 +00004655 } else {
Dale Johannesena04b7572009-02-03 23:04:43 +00004656 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004657 *Flag = P.getValue(2);
4658 }
Bill Wendlingec72e322009-12-22 01:11:43 +00004659
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004660 Chain = P.getValue(1);
Bill Wendlingec72e322009-12-22 01:11:43 +00004661
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004662 // If the source register was virtual and if we know something about it,
4663 // add an assert node.
4664 if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) &&
4665 RegisterVT.isInteger() && !RegisterVT.isVector()) {
4666 unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister;
4667 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
4668 if (FLI.LiveOutRegInfo.size() > SlotNo) {
4669 FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[SlotNo];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004670
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004671 unsigned RegSize = RegisterVT.getSizeInBits();
4672 unsigned NumSignBits = LOI.NumSignBits;
4673 unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004674
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004675 // FIXME: We capture more information than the dag can represent. For
4676 // now, just use the tightest assertzext/assertsext possible.
4677 bool isSExt = true;
Owen Anderson825b72b2009-08-11 20:47:22 +00004678 EVT FromVT(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004679 if (NumSignBits == RegSize)
Owen Anderson825b72b2009-08-11 20:47:22 +00004680 isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004681 else if (NumZeroBits >= RegSize-1)
Owen Anderson825b72b2009-08-11 20:47:22 +00004682 isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004683 else if (NumSignBits > RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004684 isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8
Dan Gohman07c26ee2009-03-31 01:38:29 +00004685 else if (NumZeroBits >= RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004686 isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004687 else if (NumSignBits > RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00004688 isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16
Dan Gohman07c26ee2009-03-31 01:38:29 +00004689 else if (NumZeroBits >= RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00004690 isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004691 else if (NumSignBits > RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00004692 isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32
Dan Gohman07c26ee2009-03-31 01:38:29 +00004693 else if (NumZeroBits >= RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00004694 isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004695
Bill Wendling4533cac2010-01-28 21:51:40 +00004696 if (FromVT != MVT::Other)
Dale Johannesen66978ee2009-01-31 02:22:37 +00004697 P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004698 RegisterVT, P, DAG.getValueType(FromVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004699 }
4700 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004701
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004702 Parts[i] = P;
4703 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004704
Bill Wendling46ada192010-03-02 01:55:18 +00004705 Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00004706 NumRegs, RegisterVT, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004707 Part += NumRegs;
4708 Parts.clear();
4709 }
4710
Bill Wendling4533cac2010-01-28 21:51:40 +00004711 return DAG.getNode(ISD::MERGE_VALUES, dl,
4712 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
4713 &Values[0], ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004714}
4715
4716/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004717/// specified value into the registers specified by this object. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004718/// Chain/Flag as the input and updates them for the output Chain/Flag.
4719/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004720void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
Bill Wendling46ada192010-03-02 01:55:18 +00004721 SDValue &Chain, SDValue *Flag) const {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004722 // Get the list of the values's legal parts.
4723 unsigned NumRegs = Regs.size();
4724 SmallVector<SDValue, 8> Parts(NumRegs);
4725 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00004726 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00004727 unsigned NumParts = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00004728 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004729
Bill Wendling46ada192010-03-02 01:55:18 +00004730 getCopyToParts(DAG, dl,
Bill Wendling3ea3c242009-12-22 02:10:19 +00004731 Val.getValue(Val.getResNo() + Value),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004732 &Parts[Part], NumParts, RegisterVT);
4733 Part += NumParts;
4734 }
4735
4736 // Copy the parts into the registers.
4737 SmallVector<SDValue, 8> Chains(NumRegs);
4738 for (unsigned i = 0; i != NumRegs; ++i) {
4739 SDValue Part;
Bill Wendlingec72e322009-12-22 01:11:43 +00004740 if (Flag == 0) {
Dale Johannesena04b7572009-02-03 23:04:43 +00004741 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
Bill Wendlingec72e322009-12-22 01:11:43 +00004742 } else {
Dale Johannesena04b7572009-02-03 23:04:43 +00004743 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004744 *Flag = Part.getValue(1);
4745 }
Bill Wendlingec72e322009-12-22 01:11:43 +00004746
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004747 Chains[i] = Part.getValue(0);
4748 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004749
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004750 if (NumRegs == 1 || Flag)
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004751 // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004752 // flagged to it. That is the CopyToReg nodes and the user are considered
4753 // a single scheduling unit. If we create a TokenFactor and return it as
4754 // chain, then the TokenFactor is both a predecessor (operand) of the
4755 // user as well as a successor (the TF operands are flagged to the user).
4756 // c1, f1 = CopyToReg
4757 // c2, f2 = CopyToReg
4758 // c3 = TokenFactor c1, c2
4759 // ...
4760 // = op c3, ..., f2
4761 Chain = Chains[NumRegs-1];
4762 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004763 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004764}
4765
4766/// AddInlineAsmOperands - Add this value to the specified inlineasm node
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004767/// operand list. This adds the code marker and includes the number of
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004768/// values added into it.
Chris Lattnerdecc2672010-04-07 05:20:54 +00004769void RegsForValue::AddInlineAsmOperands(unsigned Code, bool HasMatching,
4770 unsigned MatchingIdx,
Bill Wendling46ada192010-03-02 01:55:18 +00004771 SelectionDAG &DAG,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004772 std::vector<SDValue> &Ops) const {
Chris Lattnerdecc2672010-04-07 05:20:54 +00004773 unsigned Flag = InlineAsm::getFlagWord(Code, Regs.size());
Evan Cheng697cbbf2009-03-20 18:03:34 +00004774 if (HasMatching)
Chris Lattnerdecc2672010-04-07 05:20:54 +00004775 Flag = InlineAsm::getFlagWordForMatchingOp(Flag, MatchingIdx);
Dale Johannesen99499332009-12-23 07:32:51 +00004776 SDValue Res = DAG.getTargetConstant(Flag, MVT::i32);
Bill Wendling651ad132009-12-22 01:25:10 +00004777 Ops.push_back(Res);
4778
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004779 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Anderson23b9b192009-08-12 00:36:31 +00004780 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVTs[Value]);
Owen Andersone50ed302009-08-10 22:56:29 +00004781 EVT RegisterVT = RegVTs[Value];
Chris Lattner58f15c42008-10-17 16:21:11 +00004782 for (unsigned i = 0; i != NumRegs; ++i) {
4783 assert(Reg < Regs.size() && "Mismatch in # registers expected");
Bill Wendling4533cac2010-01-28 21:51:40 +00004784 Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT));
Chris Lattner58f15c42008-10-17 16:21:11 +00004785 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004786 }
4787}
4788
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004789/// isAllocatableRegister - If the specified register is safe to allocate,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004790/// i.e. it isn't a stack pointer or some other special register, return the
4791/// register class for the register. Otherwise, return null.
4792static const TargetRegisterClass *
4793isAllocatableRegister(unsigned Reg, MachineFunction &MF,
4794 const TargetLowering &TLI,
4795 const TargetRegisterInfo *TRI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004796 EVT FoundVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004797 const TargetRegisterClass *FoundRC = 0;
4798 for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(),
4799 E = TRI->regclass_end(); RCI != E; ++RCI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004800 EVT ThisVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004801
4802 const TargetRegisterClass *RC = *RCI;
Dan Gohmanf451cb82010-02-10 16:03:48 +00004803 // If none of the value types for this register class are valid, we
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004804 // can't use it. For example, 64-bit reg classes on 32-bit targets.
4805 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
4806 I != E; ++I) {
4807 if (TLI.isTypeLegal(*I)) {
4808 // If we have already found this register in a different register class,
4809 // choose the one with the largest VT specified. For example, on
4810 // PowerPC, we favor f64 register classes over f32.
Owen Anderson825b72b2009-08-11 20:47:22 +00004811 if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004812 ThisVT = *I;
4813 break;
4814 }
4815 }
4816 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004817
Owen Anderson825b72b2009-08-11 20:47:22 +00004818 if (ThisVT == MVT::Other) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004819
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004820 // NOTE: This isn't ideal. In particular, this might allocate the
4821 // frame pointer in functions that need it (due to them not being taken
4822 // out of allocation, because a variable sized allocation hasn't been seen
4823 // yet). This is a slight code pessimization, but should still work.
4824 for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
4825 E = RC->allocation_order_end(MF); I != E; ++I)
4826 if (*I == Reg) {
4827 // We found a matching register class. Keep looking at others in case
4828 // we find one with larger registers that this physreg is also in.
4829 FoundRC = RC;
4830 FoundVT = ThisVT;
4831 break;
4832 }
4833 }
4834 return FoundRC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004835}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004836
4837
4838namespace llvm {
4839/// AsmOperandInfo - This contains information for each constraint that we are
4840/// lowering.
Cedric Venetaff9c272009-02-14 16:06:42 +00004841class VISIBILITY_HIDDEN SDISelAsmOperandInfo :
Daniel Dunbarc0c3b9a2008-09-10 04:16:29 +00004842 public TargetLowering::AsmOperandInfo {
Cedric Venetaff9c272009-02-14 16:06:42 +00004843public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004844 /// CallOperand - If this is the result output operand or a clobber
4845 /// this is null, otherwise it is the incoming operand to the CallInst.
4846 /// This gets modified as the asm is processed.
4847 SDValue CallOperand;
4848
4849 /// AssignedRegs - If this is a register or register class operand, this
4850 /// contains the set of register corresponding to the operand.
4851 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004852
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004853 explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info)
4854 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
4855 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004856
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004857 /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers
4858 /// busy in OutputRegs/InputRegs.
4859 void MarkAllocatedRegs(bool isOutReg, bool isInReg,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004860 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004861 std::set<unsigned> &InputRegs,
4862 const TargetRegisterInfo &TRI) const {
4863 if (isOutReg) {
4864 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4865 MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI);
4866 }
4867 if (isInReg) {
4868 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4869 MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI);
4870 }
4871 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004872
Owen Andersone50ed302009-08-10 22:56:29 +00004873 /// getCallOperandValEVT - Return the EVT of the Value* that this operand
Chris Lattner81249c92008-10-17 17:05:25 +00004874 /// corresponds to. If there is no Value* for this operand, it returns
Owen Anderson825b72b2009-08-11 20:47:22 +00004875 /// MVT::Other.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004876 EVT getCallOperandValEVT(LLVMContext &Context,
Owen Anderson1d0be152009-08-13 21:58:54 +00004877 const TargetLowering &TLI,
Chris Lattner81249c92008-10-17 17:05:25 +00004878 const TargetData *TD) const {
Owen Anderson825b72b2009-08-11 20:47:22 +00004879 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004880
Chris Lattner81249c92008-10-17 17:05:25 +00004881 if (isa<BasicBlock>(CallOperandVal))
4882 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004883
Chris Lattner81249c92008-10-17 17:05:25 +00004884 const llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004885
Chris Lattner81249c92008-10-17 17:05:25 +00004886 // If this is an indirect operand, the operand is a pointer to the
4887 // accessed type.
Bob Wilsone261b0c2009-12-22 18:34:19 +00004888 if (isIndirect) {
4889 const llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
4890 if (!PtrTy)
Chris Lattner75361b62010-04-07 22:58:41 +00004891 report_fatal_error("Indirect operand for inline asm not a pointer!");
Bob Wilsone261b0c2009-12-22 18:34:19 +00004892 OpTy = PtrTy->getElementType();
4893 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004894
Chris Lattner81249c92008-10-17 17:05:25 +00004895 // If OpTy is not a single value, it may be a struct/union that we
4896 // can tile with integers.
4897 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
4898 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
4899 switch (BitSize) {
4900 default: break;
4901 case 1:
4902 case 8:
4903 case 16:
4904 case 32:
4905 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00004906 case 128:
Owen Anderson1d0be152009-08-13 21:58:54 +00004907 OpTy = IntegerType::get(Context, BitSize);
Chris Lattner81249c92008-10-17 17:05:25 +00004908 break;
4909 }
4910 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004911
Chris Lattner81249c92008-10-17 17:05:25 +00004912 return TLI.getValueType(OpTy, true);
4913 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004914
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004915private:
4916 /// MarkRegAndAliases - Mark the specified register and all aliases in the
4917 /// specified set.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004918 static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004919 const TargetRegisterInfo &TRI) {
4920 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg");
4921 Regs.insert(Reg);
4922 if (const unsigned *Aliases = TRI.getAliasSet(Reg))
4923 for (; *Aliases; ++Aliases)
4924 Regs.insert(*Aliases);
4925 }
4926};
4927} // end llvm namespace.
4928
4929
4930/// GetRegistersForValue - Assign registers (virtual or physical) for the
4931/// specified operand. We prefer to assign virtual registers, to allow the
Bob Wilson266d9452009-12-17 05:07:36 +00004932/// register allocator to handle the assignment process. However, if the asm
4933/// uses features that we can't model on machineinstrs, we have SDISel do the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004934/// allocation. This produces generally horrible, but correct, code.
4935///
4936/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004937/// Input and OutputRegs are the set of already allocated physical registers.
4938///
Dan Gohman2048b852009-11-23 18:04:58 +00004939void SelectionDAGBuilder::
Dale Johannesen8e3455b2008-09-24 23:13:09 +00004940GetRegistersForValue(SDISelAsmOperandInfo &OpInfo,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004941 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004942 std::set<unsigned> &InputRegs) {
Dan Gohman0d24bfb2009-08-15 02:06:22 +00004943 LLVMContext &Context = FuncInfo.Fn->getContext();
Owen Anderson23b9b192009-08-12 00:36:31 +00004944
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004945 // Compute whether this value requires an input register, an output register,
4946 // or both.
4947 bool isOutReg = false;
4948 bool isInReg = false;
4949 switch (OpInfo.Type) {
4950 case InlineAsm::isOutput:
4951 isOutReg = true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004952
4953 // If there is an input constraint that matches this, we need to reserve
Dale Johannesen8e3455b2008-09-24 23:13:09 +00004954 // the input register so no other inputs allocate to it.
Chris Lattner6bdcda32008-10-17 16:47:46 +00004955 isInReg = OpInfo.hasMatchingInput();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004956 break;
4957 case InlineAsm::isInput:
4958 isInReg = true;
4959 isOutReg = false;
4960 break;
4961 case InlineAsm::isClobber:
4962 isOutReg = true;
4963 isInReg = true;
4964 break;
4965 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004966
4967
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004968 MachineFunction &MF = DAG.getMachineFunction();
4969 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004970
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004971 // If this is a constraint for a single physreg, or a constraint for a
4972 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004973 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004974 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
4975 OpInfo.ConstraintVT);
4976
4977 unsigned NumRegs = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00004978 if (OpInfo.ConstraintVT != MVT::Other) {
Chris Lattner01426e12008-10-21 00:45:36 +00004979 // If this is a FP input in an integer register (or visa versa) insert a bit
4980 // cast of the input value. More generally, handle any case where the input
4981 // value disagrees with the register class we plan to stick this in.
4982 if (OpInfo.Type == InlineAsm::isInput &&
4983 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00004984 // Try to convert to the first EVT that the reg class contains. If the
Chris Lattner01426e12008-10-21 00:45:36 +00004985 // types are identical size, use a bitcast to convert (e.g. two differing
4986 // vector types).
Owen Andersone50ed302009-08-10 22:56:29 +00004987 EVT RegVT = *PhysReg.second->vt_begin();
Chris Lattner01426e12008-10-21 00:45:36 +00004988 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00004989 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004990 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00004991 OpInfo.ConstraintVT = RegVT;
4992 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
4993 // If the input is a FP value and we want it in FP registers, do a
4994 // bitcast to the corresponding integer type. This turns an f64 value
4995 // into i64, which can be passed with two i32 values on a 32-bit
4996 // machine.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004997 RegVT = EVT::getIntegerVT(Context,
Owen Anderson23b9b192009-08-12 00:36:31 +00004998 OpInfo.ConstraintVT.getSizeInBits());
Dale Johannesen66978ee2009-01-31 02:22:37 +00004999 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005000 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005001 OpInfo.ConstraintVT = RegVT;
5002 }
5003 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005004
Owen Anderson23b9b192009-08-12 00:36:31 +00005005 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00005006 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005007
Owen Andersone50ed302009-08-10 22:56:29 +00005008 EVT RegVT;
5009 EVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005010
5011 // If this is a constraint for a specific physical register, like {r17},
5012 // assign it now.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005013 if (unsigned AssignedReg = PhysReg.first) {
5014 const TargetRegisterClass *RC = PhysReg.second;
Owen Anderson825b72b2009-08-11 20:47:22 +00005015 if (OpInfo.ConstraintVT == MVT::Other)
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005016 ValueVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005017
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005018 // Get the actual register value type. This is important, because the user
5019 // may have asked for (e.g.) the AX register in i32 type. We need to
5020 // remember that AX is actually i16 to get the right extension.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005021 RegVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005022
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005023 // This is a explicit reference to a physical register.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005024 Regs.push_back(AssignedReg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005025
5026 // If this is an expanded reference, add the rest of the regs to Regs.
5027 if (NumRegs != 1) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005028 TargetRegisterClass::iterator I = RC->begin();
5029 for (; *I != AssignedReg; ++I)
5030 assert(I != RC->end() && "Didn't find reg!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005031
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005032 // Already added the first reg.
5033 --NumRegs; ++I;
5034 for (; NumRegs; --NumRegs, ++I) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005035 assert(I != RC->end() && "Ran out of registers to allocate!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005036 Regs.push_back(*I);
5037 }
5038 }
Bill Wendling651ad132009-12-22 01:25:10 +00005039
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005040 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5041 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5042 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5043 return;
5044 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005045
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005046 // Otherwise, if this was a reference to an LLVM register class, create vregs
5047 // for this reference.
Chris Lattnerb3b44842009-03-24 15:25:07 +00005048 if (const TargetRegisterClass *RC = PhysReg.second) {
5049 RegVT = *RC->vt_begin();
Owen Anderson825b72b2009-08-11 20:47:22 +00005050 if (OpInfo.ConstraintVT == MVT::Other)
Evan Chengfb112882009-03-23 08:01:15 +00005051 ValueVT = RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005052
Evan Chengfb112882009-03-23 08:01:15 +00005053 // Create the appropriate number of virtual registers.
5054 MachineRegisterInfo &RegInfo = MF.getRegInfo();
5055 for (; NumRegs; --NumRegs)
Chris Lattnerb3b44842009-03-24 15:25:07 +00005056 Regs.push_back(RegInfo.createVirtualRegister(RC));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005057
Evan Chengfb112882009-03-23 08:01:15 +00005058 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5059 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005060 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005061
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005062 // This is a reference to a register class that doesn't directly correspond
5063 // to an LLVM register class. Allocate NumRegs consecutive, available,
5064 // registers from the class.
5065 std::vector<unsigned> RegClassRegs
5066 = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode,
5067 OpInfo.ConstraintVT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005068
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005069 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5070 unsigned NumAllocated = 0;
5071 for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
5072 unsigned Reg = RegClassRegs[i];
5073 // See if this register is available.
5074 if ((isOutReg && OutputRegs.count(Reg)) || // Already used.
5075 (isInReg && InputRegs.count(Reg))) { // Already used.
5076 // Make sure we find consecutive registers.
5077 NumAllocated = 0;
5078 continue;
5079 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005080
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005081 // Check to see if this register is allocatable (i.e. don't give out the
5082 // stack pointer).
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005083 const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI);
5084 if (!RC) { // Couldn't allocate this register.
5085 // Reset NumAllocated to make sure we return consecutive registers.
5086 NumAllocated = 0;
5087 continue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005088 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005089
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005090 // Okay, this register is good, we can use it.
5091 ++NumAllocated;
5092
5093 // If we allocated enough consecutive registers, succeed.
5094 if (NumAllocated == NumRegs) {
5095 unsigned RegStart = (i-NumAllocated)+1;
5096 unsigned RegEnd = i+1;
5097 // Mark all of the allocated registers used.
5098 for (unsigned i = RegStart; i != RegEnd; ++i)
5099 Regs.push_back(RegClassRegs[i]);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005100
5101 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005102 OpInfo.ConstraintVT);
5103 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5104 return;
5105 }
5106 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005107
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005108 // Otherwise, we couldn't allocate enough registers for this.
5109}
5110
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005111/// visitInlineAsm - Handle a call to an InlineAsm object.
5112///
Dan Gohman46510a72010-04-15 01:51:59 +00005113void SelectionDAGBuilder::visitInlineAsm(ImmutableCallSite CS) {
5114 const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005115
5116 /// ConstraintOperands - Information about all of the constraints.
5117 std::vector<SDISelAsmOperandInfo> ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005118
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005119 std::set<unsigned> OutputRegs, InputRegs;
5120
5121 // Do a prepass over the constraints, canonicalizing them, and building up the
5122 // ConstraintOperands list.
5123 std::vector<InlineAsm::ConstraintInfo>
5124 ConstraintInfos = IA->ParseConstraints();
5125
Evan Chengda43bcf2008-09-24 00:05:32 +00005126 bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005127
Chris Lattner6c147292009-04-30 00:48:50 +00005128 SDValue Chain, Flag;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005129
Chris Lattner6c147292009-04-30 00:48:50 +00005130 // We won't need to flush pending loads if this asm doesn't touch
5131 // memory and is nonvolatile.
5132 if (hasMemory || IA->hasSideEffects())
Dale Johannesen97d14fc2009-04-18 00:09:40 +00005133 Chain = getRoot();
Chris Lattner6c147292009-04-30 00:48:50 +00005134 else
5135 Chain = DAG.getRoot();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005136
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005137 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
5138 unsigned ResNo = 0; // ResNo - The result number of the next output.
5139 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5140 ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i]));
5141 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005142
Owen Anderson825b72b2009-08-11 20:47:22 +00005143 EVT OpVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005144
5145 // Compute the value type for each operand.
5146 switch (OpInfo.Type) {
5147 case InlineAsm::isOutput:
5148 // Indirect outputs just consume an argument.
5149 if (OpInfo.isIndirect) {
Dan Gohman46510a72010-04-15 01:51:59 +00005150 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005151 break;
5152 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005153
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005154 // The return value of the call is this value. As such, there is no
5155 // corresponding argument.
Benjamin Kramerf0127052010-01-05 13:12:22 +00005156 assert(!CS.getType()->isVoidTy() &&
Owen Anderson1d0be152009-08-13 21:58:54 +00005157 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005158 if (const StructType *STy = dyn_cast<StructType>(CS.getType())) {
5159 OpVT = TLI.getValueType(STy->getElementType(ResNo));
5160 } else {
5161 assert(ResNo == 0 && "Asm only has one result!");
5162 OpVT = TLI.getValueType(CS.getType());
5163 }
5164 ++ResNo;
5165 break;
5166 case InlineAsm::isInput:
Dan Gohman46510a72010-04-15 01:51:59 +00005167 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005168 break;
5169 case InlineAsm::isClobber:
5170 // Nothing to do.
5171 break;
5172 }
5173
5174 // If this is an input or an indirect output, process the call argument.
5175 // BasicBlocks are labels, currently appearing only in asm's.
5176 if (OpInfo.CallOperandVal) {
Dale Johannesen5339c552009-07-20 23:27:39 +00005177 // Strip bitcasts, if any. This mostly comes up for functions.
Dale Johannesen76711242009-08-06 22:45:51 +00005178 OpInfo.CallOperandVal = OpInfo.CallOperandVal->stripPointerCasts();
5179
Dan Gohman46510a72010-04-15 01:51:59 +00005180 if (const BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005181 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00005182 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005183 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005184 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005185
Owen Anderson1d0be152009-08-13 21:58:54 +00005186 OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005187 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005188
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005189 OpInfo.ConstraintVT = OpVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005190 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005191
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005192 // Second pass over the constraints: compute which constraint option to use
5193 // and assign registers to constraints that want a specific physreg.
5194 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5195 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005196
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005197 // If this is an output operand with a matching input operand, look up the
Evan Cheng09dc9c02008-12-16 18:21:39 +00005198 // matching input. If their types mismatch, e.g. one is an integer, the
5199 // other is floating point, or their sizes are different, flag it as an
5200 // error.
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005201 if (OpInfo.hasMatchingInput()) {
5202 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
Chris Lattner87d677c2010-04-07 23:50:38 +00005203
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005204 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Evan Cheng09dc9c02008-12-16 18:21:39 +00005205 if ((OpInfo.ConstraintVT.isInteger() !=
5206 Input.ConstraintVT.isInteger()) ||
5207 (OpInfo.ConstraintVT.getSizeInBits() !=
5208 Input.ConstraintVT.getSizeInBits())) {
Chris Lattner75361b62010-04-07 22:58:41 +00005209 report_fatal_error("Unsupported asm: input constraint"
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005210 " with a matching output constraint of"
5211 " incompatible type!");
Evan Cheng09dc9c02008-12-16 18:21:39 +00005212 }
5213 Input.ConstraintVT = OpInfo.ConstraintVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005214 }
5215 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005216
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005217 // Compute the constraint code and ConstraintType to use.
Evan Chengda43bcf2008-09-24 00:05:32 +00005218 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, hasMemory, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005219
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005220 // If this is a memory input, and if the operand is not indirect, do what we
5221 // need to to provide an address for the memory input.
5222 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
5223 !OpInfo.isIndirect) {
5224 assert(OpInfo.Type == InlineAsm::isInput &&
5225 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005226
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005227 // Memory operands really want the address of the value. If we don't have
5228 // an indirect input, put it in the constpool if we can, otherwise spill
5229 // it to a stack slot.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005230
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005231 // If the operand is a float, integer, or vector constant, spill to a
5232 // constant pool entry to get its address.
Dan Gohman46510a72010-04-15 01:51:59 +00005233 const Value *OpVal = OpInfo.CallOperandVal;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005234 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
5235 isa<ConstantVector>(OpVal)) {
5236 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
5237 TLI.getPointerTy());
5238 } else {
5239 // Otherwise, create a stack slot and emit a store to it before the
5240 // asm.
5241 const Type *Ty = OpVal->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00005242 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005243 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
5244 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005245 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005246 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005247 Chain = DAG.getStore(Chain, getCurDebugLoc(),
David Greene1e559442010-02-15 17:00:31 +00005248 OpInfo.CallOperand, StackSlot, NULL, 0,
5249 false, false, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005250 OpInfo.CallOperand = StackSlot;
5251 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005252
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005253 // There is no longer a Value* corresponding to this operand.
5254 OpInfo.CallOperandVal = 0;
Bill Wendling651ad132009-12-22 01:25:10 +00005255
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005256 // It is now an indirect operand.
5257 OpInfo.isIndirect = true;
5258 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005259
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005260 // If this constraint is for a specific register, allocate it before
5261 // anything else.
5262 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005263 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005264 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005265
Bill Wendling651ad132009-12-22 01:25:10 +00005266 ConstraintInfos.clear();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005267
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005268 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00005269 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005270 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5271 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005272
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005273 // C_Register operands have already been allocated, Other/Memory don't need
5274 // to be.
5275 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005276 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005277 }
5278
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005279 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
5280 std::vector<SDValue> AsmNodeOperands;
5281 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
5282 AsmNodeOperands.push_back(
Dan Gohmanf2d7fb32010-01-04 21:00:54 +00005283 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(),
5284 TLI.getPointerTy()));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005285
Chris Lattnerdecc2672010-04-07 05:20:54 +00005286 // If we have a !srcloc metadata node associated with it, we want to attach
5287 // this to the ultimately generated inline asm machineinstr. To do this, we
5288 // pass in the third operand as this (potentially null) inline asm MDNode.
5289 const MDNode *SrcLoc = CS.getInstruction()->getMetadata("srcloc");
5290 AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005291
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005292 // Loop over all of the inputs, copying the operand values into the
5293 // appropriate registers and processing the output regs.
5294 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005295
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005296 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
5297 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005298
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005299 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5300 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
5301
5302 switch (OpInfo.Type) {
5303 case InlineAsm::isOutput: {
5304 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
5305 OpInfo.ConstraintType != TargetLowering::C_Register) {
5306 // Memory output, or 'other' output (e.g. 'X' constraint).
5307 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
5308
5309 // Add information to the INLINEASM node to know about this output.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005310 unsigned OpFlags = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
5311 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005312 TLI.getPointerTy()));
5313 AsmNodeOperands.push_back(OpInfo.CallOperand);
5314 break;
5315 }
5316
5317 // Otherwise, this is a register or register class output.
5318
5319 // Copy the output from the appropriate register. Find a register that
5320 // we can use.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005321 if (OpInfo.AssignedRegs.Regs.empty())
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005322 report_fatal_error("Couldn't allocate output reg for constraint '" +
5323 Twine(OpInfo.ConstraintCode) + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005324
5325 // If this is an indirect operand, store through the pointer after the
5326 // asm.
5327 if (OpInfo.isIndirect) {
5328 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
5329 OpInfo.CallOperandVal));
5330 } else {
5331 // This is the result value of the call.
Benjamin Kramerf0127052010-01-05 13:12:22 +00005332 assert(!CS.getType()->isVoidTy() && "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005333 // Concatenate this output onto the outputs list.
5334 RetValRegs.append(OpInfo.AssignedRegs);
5335 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005336
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005337 // Add information to the INLINEASM node to know that this register is
5338 // set.
Dale Johannesen913d3df2008-09-12 17:49:03 +00005339 OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ?
Chris Lattnerdecc2672010-04-07 05:20:54 +00005340 InlineAsm::Kind_RegDefEarlyClobber :
5341 InlineAsm::Kind_RegDef,
Evan Chengfb112882009-03-23 08:01:15 +00005342 false,
5343 0,
Bill Wendling46ada192010-03-02 01:55:18 +00005344 DAG,
Bill Wendling651ad132009-12-22 01:25:10 +00005345 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005346 break;
5347 }
5348 case InlineAsm::isInput: {
5349 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005350
Chris Lattner6bdcda32008-10-17 16:47:46 +00005351 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005352 // If this is required to match an output register we have already set,
5353 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00005354 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005355
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005356 // Scan until we find the definition we already emitted of this operand.
5357 // When we find it, create a RegsForValue operand.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005358 unsigned CurOp = InlineAsm::Op_FirstOperand;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005359 for (; OperandNo; --OperandNo) {
5360 // Advance to the next operand.
Evan Cheng697cbbf2009-03-20 18:03:34 +00005361 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005362 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00005363 assert((InlineAsm::isRegDefKind(OpFlag) ||
5364 InlineAsm::isRegDefEarlyClobberKind(OpFlag) ||
5365 InlineAsm::isMemKind(OpFlag)) && "Skipped past definitions?");
Evan Cheng697cbbf2009-03-20 18:03:34 +00005366 CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005367 }
5368
Evan Cheng697cbbf2009-03-20 18:03:34 +00005369 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005370 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00005371 if (InlineAsm::isRegDefKind(OpFlag) ||
5372 InlineAsm::isRegDefEarlyClobberKind(OpFlag)) {
Evan Cheng697cbbf2009-03-20 18:03:34 +00005373 // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
Chris Lattner6129c372010-04-08 00:09:16 +00005374 if (OpInfo.isIndirect) {
5375 // This happens on gcc/testsuite/gcc.dg/pr8788-1.c
Dan Gohman99be8ae2010-04-19 22:41:47 +00005376 LLVMContext &Ctx = *DAG.getContext();
Chris Lattner6129c372010-04-08 00:09:16 +00005377 Ctx.emitError(CS.getInstruction(), "inline asm not supported yet:"
5378 " don't know how to handle tied "
5379 "indirect register inputs");
5380 }
5381
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005382 RegsForValue MatchedRegs;
5383 MatchedRegs.TLI = &TLI;
5384 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
Owen Andersone50ed302009-08-10 22:56:29 +00005385 EVT RegVT = AsmNodeOperands[CurOp+1].getValueType();
Evan Chengfb112882009-03-23 08:01:15 +00005386 MatchedRegs.RegVTs.push_back(RegVT);
5387 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005388 for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag);
Evan Chengfb112882009-03-23 08:01:15 +00005389 i != e; ++i)
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005390 MatchedRegs.Regs.push_back
5391 (RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT)));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005392
5393 // Use the produced MatchedRegs object to
Dale Johannesen66978ee2009-01-31 02:22:37 +00005394 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005395 Chain, &Flag);
Chris Lattnerdecc2672010-04-07 05:20:54 +00005396 MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse,
Evan Chengfb112882009-03-23 08:01:15 +00005397 true, OpInfo.getMatchedOperand(),
Bill Wendling46ada192010-03-02 01:55:18 +00005398 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005399 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005400 }
Chris Lattnerdecc2672010-04-07 05:20:54 +00005401
5402 assert(InlineAsm::isMemKind(OpFlag) && "Unknown matching constraint!");
5403 assert(InlineAsm::getNumOperandRegisters(OpFlag) == 1 &&
5404 "Unexpected number of operands");
5405 // Add information to the INLINEASM node to know about this input.
5406 // See InlineAsm.h isUseOperandTiedToDef.
5407 OpFlag = InlineAsm::getFlagWordForMatchingOp(OpFlag,
5408 OpInfo.getMatchedOperand());
5409 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag,
5410 TLI.getPointerTy()));
5411 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
5412 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005413 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005414
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005415 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005416 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005417 "Don't know how to handle indirect other inputs yet!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005418
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005419 std::vector<SDValue> Ops;
5420 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0],
Evan Chengda43bcf2008-09-24 00:05:32 +00005421 hasMemory, Ops, DAG);
Chris Lattner87d677c2010-04-07 23:50:38 +00005422 if (Ops.empty())
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005423 report_fatal_error("Invalid operand for inline asm constraint '" +
5424 Twine(OpInfo.ConstraintCode) + "'!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005425
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005426 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005427 unsigned ResOpType =
5428 InlineAsm::getFlagWord(InlineAsm::Kind_Imm, Ops.size());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005429 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005430 TLI.getPointerTy()));
5431 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
5432 break;
Chris Lattnerdecc2672010-04-07 05:20:54 +00005433 }
5434
5435 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005436 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
5437 assert(InOperandVal.getValueType() == TLI.getPointerTy() &&
5438 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005439
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005440 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005441 unsigned ResOpType = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
Dale Johannesen86b49f82008-09-24 01:07:17 +00005442 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005443 TLI.getPointerTy()));
5444 AsmNodeOperands.push_back(InOperandVal);
5445 break;
5446 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005447
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005448 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
5449 OpInfo.ConstraintType == TargetLowering::C_Register) &&
5450 "Unknown constraint type!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005451 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005452 "Don't know how to handle indirect register inputs yet!");
5453
5454 // Copy the input into the appropriate registers.
Evan Cheng8112b532010-02-10 01:21:02 +00005455 if (OpInfo.AssignedRegs.Regs.empty() ||
Chris Lattner87d677c2010-04-07 23:50:38 +00005456 !OpInfo.AssignedRegs.areValueTypesLegal())
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005457 report_fatal_error("Couldn't allocate input reg for constraint '" +
5458 Twine(OpInfo.ConstraintCode) + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005459
Dale Johannesen66978ee2009-01-31 02:22:37 +00005460 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005461 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005462
Chris Lattnerdecc2672010-04-07 05:20:54 +00005463 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse, false, 0,
Bill Wendling46ada192010-03-02 01:55:18 +00005464 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005465 break;
5466 }
5467 case InlineAsm::isClobber: {
5468 // Add the clobbered value to the operand list, so that the register
5469 // allocator is aware that the physreg got clobbered.
5470 if (!OpInfo.AssignedRegs.Regs.empty())
Chris Lattnerdecc2672010-04-07 05:20:54 +00005471 OpInfo.AssignedRegs.AddInlineAsmOperands(
5472 InlineAsm::Kind_RegDefEarlyClobber,
Bill Wendling46ada192010-03-02 01:55:18 +00005473 false, 0, DAG,
Bill Wendling651ad132009-12-22 01:25:10 +00005474 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005475 break;
5476 }
5477 }
5478 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005479
Chris Lattnerdecc2672010-04-07 05:20:54 +00005480 // Finish up input operands. Set the input chain and add the flag last.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005481 AsmNodeOperands[0] = Chain;
5482 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005483
Dale Johannesen66978ee2009-01-31 02:22:37 +00005484 Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005485 DAG.getVTList(MVT::Other, MVT::Flag),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005486 &AsmNodeOperands[0], AsmNodeOperands.size());
5487 Flag = Chain.getValue(1);
5488
5489 // If this asm returns a register value, copy the result from that register
5490 // and set it as the value of the call.
5491 if (!RetValRegs.Regs.empty()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005492 SDValue Val = RetValRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005493 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005494
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005495 // FIXME: Why don't we do this for inline asms with MRVs?
5496 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
Owen Andersone50ed302009-08-10 22:56:29 +00005497 EVT ResultType = TLI.getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005498
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005499 // If any of the results of the inline asm is a vector, it may have the
5500 // wrong width/num elts. This can happen for register classes that can
5501 // contain multiple different value types. The preg or vreg allocated may
5502 // not have the same VT as was expected. Convert it to the right type
5503 // with bit_convert.
5504 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005505 Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005506 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005507
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005508 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005509 ResultType.isInteger() && Val.getValueType().isInteger()) {
5510 // If a result value was tied to an input value, the computed result may
5511 // have a wider width than the expected result. Extract the relevant
5512 // portion.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005513 Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005514 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005515
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005516 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Chris Lattner0c526442008-10-17 17:52:49 +00005517 }
Dan Gohman95915732008-10-18 01:03:45 +00005518
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005519 setValue(CS.getInstruction(), Val);
Dale Johannesenec65a7d2009-04-14 00:56:56 +00005520 // Don't need to use this as a chain in this case.
5521 if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty())
5522 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005523 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005524
Dan Gohman46510a72010-04-15 01:51:59 +00005525 std::vector<std::pair<SDValue, const Value *> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005526
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005527 // Process indirect outputs, first output all of the flagged copies out of
5528 // physregs.
5529 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
5530 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
Dan Gohman46510a72010-04-15 01:51:59 +00005531 const Value *Ptr = IndirectStoresToEmit[i].second;
Dale Johannesen66978ee2009-01-31 02:22:37 +00005532 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005533 Chain, &Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005534 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
5535 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005536
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005537 // Emit the non-flagged stores from the physregs.
5538 SmallVector<SDValue, 8> OutChains;
Bill Wendling651ad132009-12-22 01:25:10 +00005539 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) {
5540 SDValue Val = DAG.getStore(Chain, getCurDebugLoc(),
5541 StoresToEmit[i].first,
5542 getValue(StoresToEmit[i].second),
David Greene1e559442010-02-15 17:00:31 +00005543 StoresToEmit[i].second, 0,
5544 false, false, 0);
Bill Wendling651ad132009-12-22 01:25:10 +00005545 OutChains.push_back(Val);
Bill Wendling651ad132009-12-22 01:25:10 +00005546 }
5547
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005548 if (!OutChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00005549 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005550 &OutChains[0], OutChains.size());
Bill Wendling651ad132009-12-22 01:25:10 +00005551
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005552 DAG.setRoot(Chain);
5553}
5554
Dan Gohman46510a72010-04-15 01:51:59 +00005555void SelectionDAGBuilder::visitVAStart(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00005556 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(),
5557 MVT::Other, getRoot(),
Eric Christopher551754c2010-04-16 23:37:20 +00005558 getValue(I.getOperand(1)),
5559 DAG.getSrcValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005560}
5561
Dan Gohman46510a72010-04-15 01:51:59 +00005562void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) {
Dale Johannesena04b7572009-02-03 23:04:43 +00005563 SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(),
5564 getRoot(), getValue(I.getOperand(0)),
5565 DAG.getSrcValue(I.getOperand(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005566 setValue(&I, V);
5567 DAG.setRoot(V.getValue(1));
5568}
5569
Dan Gohman46510a72010-04-15 01:51:59 +00005570void SelectionDAGBuilder::visitVAEnd(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00005571 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(),
5572 MVT::Other, getRoot(),
Eric Christopher551754c2010-04-16 23:37:20 +00005573 getValue(I.getOperand(1)),
5574 DAG.getSrcValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005575}
5576
Dan Gohman46510a72010-04-15 01:51:59 +00005577void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00005578 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(),
5579 MVT::Other, getRoot(),
5580 getValue(I.getOperand(1)),
Eric Christopher551754c2010-04-16 23:37:20 +00005581 getValue(I.getOperand(2)),
5582 DAG.getSrcValue(I.getOperand(1)),
5583 DAG.getSrcValue(I.getOperand(2))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005584}
5585
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005586/// TargetLowering::LowerCallTo - This is the default LowerCallTo
Dan Gohman98ca4f22009-08-05 01:29:28 +00005587/// implementation, which just calls LowerCall.
5588/// FIXME: When all targets are
5589/// migrated to using LowerCall, this hook should be integrated into SDISel.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005590std::pair<SDValue, SDValue>
5591TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy,
5592 bool RetSExt, bool RetZExt, bool isVarArg,
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00005593 bool isInreg, unsigned NumFixedArgs,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00005594 CallingConv::ID CallConv, bool isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00005595 bool isReturnValueUsed,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005596 SDValue Callee,
Dan Gohmand858e902010-04-17 15:26:15 +00005597 ArgListTy &Args, SelectionDAG &DAG,
5598 DebugLoc dl) const {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005599 // Handle all of the outgoing arguments.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005600 SmallVector<ISD::OutputArg, 32> Outs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005601 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00005602 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005603 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
5604 for (unsigned Value = 0, NumValues = ValueVTs.size();
5605 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005606 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005607 const Type *ArgTy = VT.getTypeForEVT(RetTy->getContext());
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005608 SDValue Op = SDValue(Args[i].Node.getNode(),
5609 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005610 ISD::ArgFlagsTy Flags;
5611 unsigned OriginalAlignment =
5612 getTargetData()->getABITypeAlignment(ArgTy);
5613
5614 if (Args[i].isZExt)
5615 Flags.setZExt();
5616 if (Args[i].isSExt)
5617 Flags.setSExt();
5618 if (Args[i].isInReg)
5619 Flags.setInReg();
5620 if (Args[i].isSRet)
5621 Flags.setSRet();
5622 if (Args[i].isByVal) {
5623 Flags.setByVal();
5624 const PointerType *Ty = cast<PointerType>(Args[i].Ty);
5625 const Type *ElementTy = Ty->getElementType();
5626 unsigned FrameAlign = getByValTypeAlignment(ElementTy);
Duncan Sands777d2302009-05-09 07:06:46 +00005627 unsigned FrameSize = getTargetData()->getTypeAllocSize(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005628 // For ByVal, alignment should come from FE. BE will guess if this
5629 // info is not there but there are cases it cannot get right.
5630 if (Args[i].Alignment)
5631 FrameAlign = Args[i].Alignment;
5632 Flags.setByValAlign(FrameAlign);
5633 Flags.setByValSize(FrameSize);
5634 }
5635 if (Args[i].isNest)
5636 Flags.setNest();
5637 Flags.setOrigAlign(OriginalAlignment);
5638
Owen Anderson23b9b192009-08-12 00:36:31 +00005639 EVT PartVT = getRegisterType(RetTy->getContext(), VT);
5640 unsigned NumParts = getNumRegisters(RetTy->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005641 SmallVector<SDValue, 4> Parts(NumParts);
5642 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
5643
5644 if (Args[i].isSExt)
5645 ExtendKind = ISD::SIGN_EXTEND;
5646 else if (Args[i].isZExt)
5647 ExtendKind = ISD::ZERO_EXTEND;
5648
Bill Wendling46ada192010-03-02 01:55:18 +00005649 getCopyToParts(DAG, dl, Op, &Parts[0], NumParts,
Bill Wendling3ea3c242009-12-22 02:10:19 +00005650 PartVT, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005651
Dan Gohman98ca4f22009-08-05 01:29:28 +00005652 for (unsigned j = 0; j != NumParts; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005653 // if it isn't first piece, alignment must be 1
Dan Gohman98ca4f22009-08-05 01:29:28 +00005654 ISD::OutputArg MyFlags(Flags, Parts[j], i < NumFixedArgs);
5655 if (NumParts > 1 && j == 0)
5656 MyFlags.Flags.setSplit();
5657 else if (j != 0)
5658 MyFlags.Flags.setOrigAlign(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005659
Dan Gohman98ca4f22009-08-05 01:29:28 +00005660 Outs.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005661 }
5662 }
5663 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005664
Dan Gohman98ca4f22009-08-05 01:29:28 +00005665 // Handle the incoming return values from the call.
5666 SmallVector<ISD::InputArg, 32> Ins;
Owen Andersone50ed302009-08-10 22:56:29 +00005667 SmallVector<EVT, 4> RetTys;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005668 ComputeValueVTs(*this, RetTy, RetTys);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005669 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00005670 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00005671 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
5672 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005673 for (unsigned i = 0; i != NumRegs; ++i) {
5674 ISD::InputArg MyFlags;
5675 MyFlags.VT = RegisterVT;
5676 MyFlags.Used = isReturnValueUsed;
5677 if (RetSExt)
5678 MyFlags.Flags.setSExt();
5679 if (RetZExt)
5680 MyFlags.Flags.setZExt();
5681 if (isInreg)
5682 MyFlags.Flags.setInReg();
5683 Ins.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005684 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005685 }
5686
Dan Gohman98ca4f22009-08-05 01:29:28 +00005687 SmallVector<SDValue, 4> InVals;
Evan Cheng022d9e12010-02-02 23:55:14 +00005688 Chain = LowerCall(Chain, Callee, CallConv, isVarArg, isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00005689 Outs, Ins, dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00005690
5691 // Verify that the target's LowerCall behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00005692 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00005693 "LowerCall didn't return a valid chain!");
5694 assert((!isTailCall || InVals.empty()) &&
5695 "LowerCall emitted a return value for a tail call!");
5696 assert((isTailCall || InVals.size() == Ins.size()) &&
5697 "LowerCall didn't emit the correct number of values!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00005698
5699 // For a tail call, the return value is merely live-out and there aren't
5700 // any nodes in the DAG representing it. Return a special value to
5701 // indicate that a tail call has been emitted and no more Instructions
5702 // should be processed in the current block.
5703 if (isTailCall) {
5704 DAG.setRoot(Chain);
5705 return std::make_pair(SDValue(), SDValue());
5706 }
5707
Evan Chengaf1871f2010-03-11 19:38:18 +00005708 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
5709 assert(InVals[i].getNode() &&
5710 "LowerCall emitted a null value!");
5711 assert(Ins[i].VT == InVals[i].getValueType() &&
5712 "LowerCall emitted a value with the wrong type!");
5713 });
5714
Dan Gohman98ca4f22009-08-05 01:29:28 +00005715 // Collect the legal value parts into potentially illegal values
5716 // that correspond to the original function's return values.
5717 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5718 if (RetSExt)
5719 AssertOp = ISD::AssertSext;
5720 else if (RetZExt)
5721 AssertOp = ISD::AssertZext;
5722 SmallVector<SDValue, 4> ReturnValues;
5723 unsigned CurReg = 0;
5724 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00005725 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00005726 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
5727 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005728
Bill Wendling46ada192010-03-02 01:55:18 +00005729 ReturnValues.push_back(getCopyFromParts(DAG, dl, &InVals[CurReg],
Bill Wendling4533cac2010-01-28 21:51:40 +00005730 NumRegs, RegisterVT, VT,
5731 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00005732 CurReg += NumRegs;
5733 }
5734
5735 // For a function returning void, there is no return value. We can't create
5736 // such a node, so we just return a null return value in that case. In
5737 // that case, nothing will actualy look at the value.
5738 if (ReturnValues.empty())
5739 return std::make_pair(SDValue(), Chain);
5740
5741 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
5742 DAG.getVTList(&RetTys[0], RetTys.size()),
5743 &ReturnValues[0], ReturnValues.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005744 return std::make_pair(Res, Chain);
5745}
5746
Duncan Sands9fbc7e22009-01-21 09:00:29 +00005747void TargetLowering::LowerOperationWrapper(SDNode *N,
5748 SmallVectorImpl<SDValue> &Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005749 SelectionDAG &DAG) const {
Duncan Sands9fbc7e22009-01-21 09:00:29 +00005750 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00005751 if (Res.getNode())
5752 Results.push_back(Res);
5753}
5754
Dan Gohmand858e902010-04-17 15:26:15 +00005755SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Torok Edwinc23197a2009-07-14 16:55:14 +00005756 llvm_unreachable("LowerOperation not implemented for this target!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005757 return SDValue();
5758}
5759
Dan Gohman46510a72010-04-15 01:51:59 +00005760void
5761SelectionDAGBuilder::CopyValueToVirtualRegister(const Value *V, unsigned Reg) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005762 SDValue Op = getValue(V);
5763 assert((Op.getOpcode() != ISD::CopyFromReg ||
5764 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
5765 "Copy from a reg to the same reg!");
5766 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
5767
Owen Anderson23b9b192009-08-12 00:36:31 +00005768 RegsForValue RFV(V->getContext(), TLI, Reg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005769 SDValue Chain = DAG.getEntryNode();
Bill Wendling46ada192010-03-02 01:55:18 +00005770 RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), Chain, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005771 PendingExports.push_back(Chain);
5772}
5773
5774#include "llvm/CodeGen/SelectionDAGISel.h"
5775
Dan Gohman46510a72010-04-15 01:51:59 +00005776void SelectionDAGISel::LowerArguments(const BasicBlock *LLVMBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005777 // If this is the entry block, emit arguments.
Dan Gohman46510a72010-04-15 01:51:59 +00005778 const Function &F = *LLVMBB->getParent();
Dan Gohman2048b852009-11-23 18:04:58 +00005779 SelectionDAG &DAG = SDB->DAG;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005780 SDValue OldRoot = DAG.getRoot();
Dan Gohman2048b852009-11-23 18:04:58 +00005781 DebugLoc dl = SDB->getCurDebugLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005782 const TargetData *TD = TLI.getTargetData();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005783 SmallVector<ISD::InputArg, 16> Ins;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005784
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00005785 // Check whether the function can return without sret-demotion.
5786 SmallVector<EVT, 4> OutVTs;
5787 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005788 getReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005789 OutVTs, OutsFlags, TLI);
5790 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
5791
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005792 FLI.CanLowerReturn = TLI.CanLowerReturn(F.getCallingConv(), F.isVarArg(),
Bill Wendling3ea3c242009-12-22 02:10:19 +00005793 OutVTs, OutsFlags, DAG);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005794 if (!FLI.CanLowerReturn) {
5795 // Put in an sret pointer parameter before all the other parameters.
5796 SmallVector<EVT, 1> ValueVTs;
5797 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
5798
5799 // NOTE: Assuming that a pointer will never break down to more than one VT
5800 // or one register.
5801 ISD::ArgFlagsTy Flags;
5802 Flags.setSRet();
5803 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), ValueVTs[0]);
5804 ISD::InputArg RetArg(Flags, RegisterVT, true);
5805 Ins.push_back(RetArg);
5806 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00005807
Dan Gohman98ca4f22009-08-05 01:29:28 +00005808 // Set up the incoming argument description vector.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005809 unsigned Idx = 1;
Dan Gohman46510a72010-04-15 01:51:59 +00005810 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005811 I != E; ++I, ++Idx) {
Owen Andersone50ed302009-08-10 22:56:29 +00005812 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005813 ComputeValueVTs(TLI, I->getType(), ValueVTs);
5814 bool isArgValueUsed = !I->use_empty();
5815 for (unsigned Value = 0, NumValues = ValueVTs.size();
5816 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005817 EVT VT = ValueVTs[Value];
Owen Anderson1d0be152009-08-13 21:58:54 +00005818 const Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00005819 ISD::ArgFlagsTy Flags;
5820 unsigned OriginalAlignment =
5821 TD->getABITypeAlignment(ArgTy);
5822
5823 if (F.paramHasAttr(Idx, Attribute::ZExt))
5824 Flags.setZExt();
5825 if (F.paramHasAttr(Idx, Attribute::SExt))
5826 Flags.setSExt();
5827 if (F.paramHasAttr(Idx, Attribute::InReg))
5828 Flags.setInReg();
5829 if (F.paramHasAttr(Idx, Attribute::StructRet))
5830 Flags.setSRet();
5831 if (F.paramHasAttr(Idx, Attribute::ByVal)) {
5832 Flags.setByVal();
5833 const PointerType *Ty = cast<PointerType>(I->getType());
5834 const Type *ElementTy = Ty->getElementType();
5835 unsigned FrameAlign = TLI.getByValTypeAlignment(ElementTy);
5836 unsigned FrameSize = TD->getTypeAllocSize(ElementTy);
5837 // For ByVal, alignment should be passed from FE. BE will guess if
5838 // this info is not there but there are cases it cannot get right.
5839 if (F.getParamAlignment(Idx))
5840 FrameAlign = F.getParamAlignment(Idx);
5841 Flags.setByValAlign(FrameAlign);
5842 Flags.setByValSize(FrameSize);
5843 }
5844 if (F.paramHasAttr(Idx, Attribute::Nest))
5845 Flags.setNest();
5846 Flags.setOrigAlign(OriginalAlignment);
5847
Owen Anderson23b9b192009-08-12 00:36:31 +00005848 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5849 unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005850 for (unsigned i = 0; i != NumRegs; ++i) {
5851 ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed);
5852 if (NumRegs > 1 && i == 0)
5853 MyFlags.Flags.setSplit();
5854 // if it isn't first piece, alignment must be 1
5855 else if (i > 0)
5856 MyFlags.Flags.setOrigAlign(1);
5857 Ins.push_back(MyFlags);
5858 }
5859 }
5860 }
5861
5862 // Call the target to set up the argument values.
5863 SmallVector<SDValue, 8> InVals;
5864 SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(),
5865 F.isVarArg(), Ins,
5866 dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00005867
5868 // Verify that the target's LowerFormalArguments behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00005869 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00005870 "LowerFormalArguments didn't return a valid chain!");
5871 assert(InVals.size() == Ins.size() &&
5872 "LowerFormalArguments didn't emit the correct number of values!");
Bill Wendling3ea58b62009-12-22 21:35:02 +00005873 DEBUG({
5874 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
5875 assert(InVals[i].getNode() &&
5876 "LowerFormalArguments emitted a null value!");
5877 assert(Ins[i].VT == InVals[i].getValueType() &&
5878 "LowerFormalArguments emitted a value with the wrong type!");
5879 }
5880 });
Bill Wendling3ea3c242009-12-22 02:10:19 +00005881
Dan Gohman5e866062009-08-06 15:37:27 +00005882 // Update the DAG with the new chain value resulting from argument lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005883 DAG.setRoot(NewRoot);
5884
5885 // Set up the argument values.
5886 unsigned i = 0;
5887 Idx = 1;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005888 if (!FLI.CanLowerReturn) {
5889 // Create a virtual register for the sret pointer, and put in a copy
5890 // from the sret argument into it.
5891 SmallVector<EVT, 1> ValueVTs;
5892 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
5893 EVT VT = ValueVTs[0];
5894 EVT RegVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5895 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Bill Wendling46ada192010-03-02 01:55:18 +00005896 SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1,
Bill Wendling3ea3c242009-12-22 02:10:19 +00005897 RegVT, VT, AssertOp);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005898
Dan Gohman2048b852009-11-23 18:04:58 +00005899 MachineFunction& MF = SDB->DAG.getMachineFunction();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005900 MachineRegisterInfo& RegInfo = MF.getRegInfo();
5901 unsigned SRetReg = RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT));
5902 FLI.DemoteRegister = SRetReg;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005903 NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurDebugLoc(),
5904 SRetReg, ArgValue);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005905 DAG.setRoot(NewRoot);
Bill Wendling3ea3c242009-12-22 02:10:19 +00005906
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005907 // i indexes lowered arguments. Bump it past the hidden sret argument.
5908 // Idx indexes LLVM arguments. Don't touch it.
5909 ++i;
5910 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00005911
Dan Gohman46510a72010-04-15 01:51:59 +00005912 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005913 ++I, ++Idx) {
5914 SmallVector<SDValue, 4> ArgValues;
Owen Andersone50ed302009-08-10 22:56:29 +00005915 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005916 ComputeValueVTs(TLI, I->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005917 unsigned NumValues = ValueVTs.size();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005918 for (unsigned Value = 0; Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005919 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005920 EVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5921 unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005922
5923 if (!I->use_empty()) {
5924 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5925 if (F.paramHasAttr(Idx, Attribute::SExt))
5926 AssertOp = ISD::AssertSext;
5927 else if (F.paramHasAttr(Idx, Attribute::ZExt))
5928 AssertOp = ISD::AssertZext;
5929
Bill Wendling46ada192010-03-02 01:55:18 +00005930 ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i],
Bill Wendling3ea3c242009-12-22 02:10:19 +00005931 NumParts, PartVT, VT,
5932 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00005933 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00005934
Dan Gohman98ca4f22009-08-05 01:29:28 +00005935 i += NumParts;
5936 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00005937
Dan Gohman98ca4f22009-08-05 01:29:28 +00005938 if (!I->use_empty()) {
Evan Cheng8e36a5c2010-03-29 21:27:30 +00005939 SDValue Res;
5940 if (!ArgValues.empty())
5941 Res = DAG.getMergeValues(&ArgValues[0], NumValues,
5942 SDB->getCurDebugLoc());
Bill Wendling3ea3c242009-12-22 02:10:19 +00005943 SDB->setValue(I, Res);
5944
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005945 // If this argument is live outside of the entry block, insert a copy from
5946 // whereever we got it to the vreg that other BB's will reference it as.
Dan Gohman2048b852009-11-23 18:04:58 +00005947 SDB->CopyToExportRegsIfNeeded(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005948 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005949 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00005950
Dan Gohman98ca4f22009-08-05 01:29:28 +00005951 assert(i == InVals.size() && "Argument register count mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005952
5953 // Finally, if the target has anything special to do, allow it to do so.
5954 // FIXME: this should insert code into the DAG!
Dan Gohman64652652010-04-14 20:17:22 +00005955 EmitFunctionEntryCode();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005956}
5957
5958/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
5959/// ensure constants are generated when needed. Remember the virtual registers
5960/// that need to be added to the Machine PHI nodes as input. We cannot just
5961/// directly add them, because expansion might result in multiple MBB's for one
5962/// BB. As such, the start of the BB might correspond to a different MBB than
5963/// the end.
5964///
5965void
Dan Gohman46510a72010-04-15 01:51:59 +00005966SelectionDAGISel::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
5967 const TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005968
5969 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
5970
5971 // Check successor nodes' PHI nodes that expect a constant to be available
5972 // from this block.
5973 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
Dan Gohman46510a72010-04-15 01:51:59 +00005974 const BasicBlock *SuccBB = TI->getSuccessor(succ);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005975 if (!isa<PHINode>(SuccBB->begin())) continue;
5976 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005977
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005978 // If this terminator has multiple identical successors (common for
5979 // switches), only handle each succ once.
5980 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005981
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005982 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005983
5984 // At this point we know that there is a 1-1 correspondence between LLVM PHI
5985 // nodes and Machine PHI nodes, but the incoming operands have not been
5986 // emitted yet.
Dan Gohman46510a72010-04-15 01:51:59 +00005987 for (BasicBlock::const_iterator I = SuccBB->begin();
5988 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005989 // Ignore dead phi's.
5990 if (PN->use_empty()) continue;
5991
5992 unsigned Reg;
Dan Gohman46510a72010-04-15 01:51:59 +00005993 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005994
Dan Gohman46510a72010-04-15 01:51:59 +00005995 if (const Constant *C = dyn_cast<Constant>(PHIOp)) {
Dan Gohman2048b852009-11-23 18:04:58 +00005996 unsigned &RegOut = SDB->ConstantsOut[C];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005997 if (RegOut == 0) {
5998 RegOut = FuncInfo->CreateRegForValue(C);
Dan Gohman2048b852009-11-23 18:04:58 +00005999 SDB->CopyValueToVirtualRegister(C, RegOut);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006000 }
6001 Reg = RegOut;
6002 } else {
6003 Reg = FuncInfo->ValueMap[PHIOp];
6004 if (Reg == 0) {
6005 assert(isa<AllocaInst>(PHIOp) &&
6006 FuncInfo->StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
6007 "Didn't codegen value into a register!??");
6008 Reg = FuncInfo->CreateRegForValue(PHIOp);
Dan Gohman2048b852009-11-23 18:04:58 +00006009 SDB->CopyValueToVirtualRegister(PHIOp, Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006010 }
6011 }
6012
6013 // Remember that this register needs to added to the machine PHI node as
6014 // the input for this MBB.
Owen Andersone50ed302009-08-10 22:56:29 +00006015 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006016 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
6017 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +00006018 EVT VT = ValueVTs[vti];
Owen Anderson23b9b192009-08-12 00:36:31 +00006019 unsigned NumRegisters = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006020 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
Dan Gohman2048b852009-11-23 18:04:58 +00006021 SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006022 Reg += NumRegisters;
6023 }
6024 }
6025 }
Dan Gohman2048b852009-11-23 18:04:58 +00006026 SDB->ConstantsOut.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006027}
6028
Dan Gohman3df24e62008-09-03 23:12:08 +00006029/// This is the Fast-ISel version of HandlePHINodesInSuccessorBlocks. It only
6030/// supports legal types, and it emits MachineInstrs directly instead of
6031/// creating SelectionDAG nodes.
6032///
6033bool
Dan Gohman46510a72010-04-15 01:51:59 +00006034SelectionDAGISel::HandlePHINodesInSuccessorBlocksFast(const BasicBlock *LLVMBB,
Dan Gohman3df24e62008-09-03 23:12:08 +00006035 FastISel *F) {
Dan Gohman46510a72010-04-15 01:51:59 +00006036 const TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006037
Dan Gohman3df24e62008-09-03 23:12:08 +00006038 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
Dan Gohman2048b852009-11-23 18:04:58 +00006039 unsigned OrigNumPHINodesToUpdate = SDB->PHINodesToUpdate.size();
Dan Gohman3df24e62008-09-03 23:12:08 +00006040
6041 // Check successor nodes' PHI nodes that expect a constant to be available
6042 // from this block.
6043 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
Dan Gohman46510a72010-04-15 01:51:59 +00006044 const BasicBlock *SuccBB = TI->getSuccessor(succ);
Dan Gohman3df24e62008-09-03 23:12:08 +00006045 if (!isa<PHINode>(SuccBB->begin())) continue;
6046 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006047
Dan Gohman3df24e62008-09-03 23:12:08 +00006048 // If this terminator has multiple identical successors (common for
6049 // switches), only handle each succ once.
6050 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006051
Dan Gohman3df24e62008-09-03 23:12:08 +00006052 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
Dan Gohman3df24e62008-09-03 23:12:08 +00006053
6054 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6055 // nodes and Machine PHI nodes, but the incoming operands have not been
6056 // emitted yet.
Dan Gohman46510a72010-04-15 01:51:59 +00006057 for (BasicBlock::const_iterator I = SuccBB->begin();
6058 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohman3df24e62008-09-03 23:12:08 +00006059 // Ignore dead phi's.
6060 if (PN->use_empty()) continue;
6061
6062 // Only handle legal types. Two interesting things to note here. First,
6063 // by bailing out early, we may leave behind some dead instructions,
6064 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
6065 // own moves. Second, this check is necessary becuase FastISel doesn't
6066 // use CreateRegForValue to create registers, so it always creates
6067 // exactly one register for each non-void instruction.
Owen Andersone50ed302009-08-10 22:56:29 +00006068 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00006069 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
6070 // Promote MVT::i1.
6071 if (VT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +00006072 VT = TLI.getTypeToTransformTo(*CurDAG->getContext(), VT);
Dan Gohman74321ab2008-09-10 21:01:31 +00006073 else {
Dan Gohman2048b852009-11-23 18:04:58 +00006074 SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohman74321ab2008-09-10 21:01:31 +00006075 return false;
6076 }
Dan Gohman3df24e62008-09-03 23:12:08 +00006077 }
6078
Dan Gohman46510a72010-04-15 01:51:59 +00006079 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
Dan Gohman3df24e62008-09-03 23:12:08 +00006080
6081 unsigned Reg = F->getRegForValue(PHIOp);
6082 if (Reg == 0) {
Dan Gohman2048b852009-11-23 18:04:58 +00006083 SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohman3df24e62008-09-03 23:12:08 +00006084 return false;
6085 }
Dan Gohman2048b852009-11-23 18:04:58 +00006086 SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Dan Gohman3df24e62008-09-03 23:12:08 +00006087 }
6088 }
6089
6090 return true;
6091}