blob: f6fb089789ca802f79c78cee11a8c26406f45fb9 [file] [log] [blame]
Chris Lattner7a60d912005-01-07 07:47:53 +00001//===-- SelectionDAGISel.cpp - Implement the SelectionDAGISel class -------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
Chris Lattner7a60d912005-01-07 07:47:53 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman835702a2005-04-21 22:36:52 +00007//
Chris Lattner7a60d912005-01-07 07:47:53 +00008//===----------------------------------------------------------------------===//
9//
10// This implements the SelectionDAGISel class.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "isel"
15#include "llvm/CodeGen/SelectionDAGISel.h"
Evan Cheng739a6a42006-01-21 02:32:06 +000016#include "llvm/CodeGen/ScheduleDAG.h"
Chris Lattner2e77db62005-05-13 18:50:42 +000017#include "llvm/CallingConv.h"
Chris Lattner7a60d912005-01-07 07:47:53 +000018#include "llvm/Constants.h"
19#include "llvm/DerivedTypes.h"
20#include "llvm/Function.h"
Chris Lattner435b4022005-11-29 06:21:05 +000021#include "llvm/GlobalVariable.h"
Chris Lattner476e67b2006-01-26 22:24:51 +000022#include "llvm/InlineAsm.h"
Chris Lattner7a60d912005-01-07 07:47:53 +000023#include "llvm/Instructions.h"
24#include "llvm/Intrinsics.h"
Jim Laskeya8bdac82006-03-23 18:06:46 +000025#include "llvm/IntrinsicInst.h"
Chris Lattnerf2b62f32005-11-16 07:22:30 +000026#include "llvm/CodeGen/IntrinsicLowering.h"
Jim Laskey219d5592006-01-04 22:28:25 +000027#include "llvm/CodeGen/MachineDebugInfo.h"
Chris Lattner7a60d912005-01-07 07:47:53 +000028#include "llvm/CodeGen/MachineFunction.h"
29#include "llvm/CodeGen/MachineFrameInfo.h"
30#include "llvm/CodeGen/MachineInstrBuilder.h"
31#include "llvm/CodeGen/SelectionDAG.h"
32#include "llvm/CodeGen/SSARegMap.h"
Chris Lattnerd4382f02005-09-13 19:30:54 +000033#include "llvm/Target/MRegisterInfo.h"
Chris Lattner7a60d912005-01-07 07:47:53 +000034#include "llvm/Target/TargetData.h"
35#include "llvm/Target/TargetFrameInfo.h"
36#include "llvm/Target/TargetInstrInfo.h"
37#include "llvm/Target/TargetLowering.h"
38#include "llvm/Target/TargetMachine.h"
Chris Lattnerc9950c12005-08-17 06:37:43 +000039#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Chris Lattnere05a4612005-01-12 03:41:21 +000040#include "llvm/Support/CommandLine.h"
Chris Lattner43535a12005-11-09 04:45:33 +000041#include "llvm/Support/MathExtras.h"
Chris Lattner7a60d912005-01-07 07:47:53 +000042#include "llvm/Support/Debug.h"
43#include <map>
Chris Lattner1558fc62006-02-01 18:59:47 +000044#include <set>
Chris Lattner7a60d912005-01-07 07:47:53 +000045#include <iostream>
Jeff Cohen83c22e02006-02-24 02:52:40 +000046#include <algorithm>
Chris Lattner7a60d912005-01-07 07:47:53 +000047using namespace llvm;
48
Chris Lattner975f5c92005-09-01 18:44:10 +000049#ifndef NDEBUG
Chris Lattnere05a4612005-01-12 03:41:21 +000050static cl::opt<bool>
Evan Cheng739a6a42006-01-21 02:32:06 +000051ViewISelDAGs("view-isel-dags", cl::Hidden,
52 cl::desc("Pop up a window to show isel dags as they are selected"));
53static cl::opt<bool>
54ViewSchedDAGs("view-sched-dags", cl::Hidden,
55 cl::desc("Pop up a window to show sched dags as they are processed"));
Chris Lattnere05a4612005-01-12 03:41:21 +000056#else
Evan Cheng739a6a42006-01-21 02:32:06 +000057static const bool ViewISelDAGs = 0;
58static const bool ViewSchedDAGs = 0;
Chris Lattnere05a4612005-01-12 03:41:21 +000059#endif
60
Chris Lattner5255d042006-03-10 07:49:12 +000061// Scheduling heuristics
62enum SchedHeuristics {
63 defaultScheduling, // Let the target specify its preference.
64 noScheduling, // No scheduling, emit breadth first sequence.
65 simpleScheduling, // Two pass, min. critical path, max. utilization.
66 simpleNoItinScheduling, // Same as above exact using generic latency.
67 listSchedulingBURR, // Bottom up reg reduction list scheduling.
68 listSchedulingTD // Top-down list scheduler.
69};
70
Evan Chengc1e1d972006-01-23 07:01:07 +000071namespace {
72 cl::opt<SchedHeuristics>
73 ISHeuristic(
74 "sched",
75 cl::desc("Choose scheduling style"),
Evan Chenga6eff8a2006-01-25 09:12:57 +000076 cl::init(defaultScheduling),
Evan Chengc1e1d972006-01-23 07:01:07 +000077 cl::values(
Evan Chenga6eff8a2006-01-25 09:12:57 +000078 clEnumValN(defaultScheduling, "default",
79 "Target preferred scheduling style"),
Evan Chengc1e1d972006-01-23 07:01:07 +000080 clEnumValN(noScheduling, "none",
Jim Laskeyb8566fa2006-01-23 13:34:04 +000081 "No scheduling: breadth first sequencing"),
Evan Chengc1e1d972006-01-23 07:01:07 +000082 clEnumValN(simpleScheduling, "simple",
83 "Simple two pass scheduling: minimize critical path "
84 "and maximize processor utilization"),
85 clEnumValN(simpleNoItinScheduling, "simple-noitin",
86 "Simple two pass scheduling: Same as simple "
87 "except using generic latency"),
Evan Chenga6eff8a2006-01-25 09:12:57 +000088 clEnumValN(listSchedulingBURR, "list-burr",
Evan Cheng31272342006-01-23 08:26:10 +000089 "Bottom up register reduction list scheduling"),
Chris Lattner47639db2006-03-06 00:22:00 +000090 clEnumValN(listSchedulingTD, "list-td",
91 "Top-down list scheduler"),
Evan Chengc1e1d972006-01-23 07:01:07 +000092 clEnumValEnd));
93} // namespace
94
Chris Lattner6f87d182006-02-22 22:37:12 +000095namespace {
96 /// RegsForValue - This struct represents the physical registers that a
97 /// particular value is assigned and the type information about the value.
98 /// This is needed because values can be promoted into larger registers and
99 /// expanded into multiple smaller registers than the value.
100 struct RegsForValue {
101 /// Regs - This list hold the register (for legal and promoted values)
102 /// or register set (for expanded values) that the value should be assigned
103 /// to.
104 std::vector<unsigned> Regs;
105
106 /// RegVT - The value type of each register.
107 ///
108 MVT::ValueType RegVT;
109
110 /// ValueVT - The value type of the LLVM value, which may be promoted from
111 /// RegVT or made from merging the two expanded parts.
112 MVT::ValueType ValueVT;
113
114 RegsForValue() : RegVT(MVT::Other), ValueVT(MVT::Other) {}
115
116 RegsForValue(unsigned Reg, MVT::ValueType regvt, MVT::ValueType valuevt)
117 : RegVT(regvt), ValueVT(valuevt) {
118 Regs.push_back(Reg);
119 }
120 RegsForValue(const std::vector<unsigned> &regs,
121 MVT::ValueType regvt, MVT::ValueType valuevt)
122 : Regs(regs), RegVT(regvt), ValueVT(valuevt) {
123 }
124
125 /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
126 /// this value and returns the result as a ValueVT value. This uses
127 /// Chain/Flag as the input and updates them for the output Chain/Flag.
128 SDOperand getCopyFromRegs(SelectionDAG &DAG,
Chris Lattnere7c0ffb2006-02-23 20:06:57 +0000129 SDOperand &Chain, SDOperand &Flag) const;
Chris Lattner571d9642006-02-23 19:21:04 +0000130
131 /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
132 /// specified value into the registers specified by this object. This uses
133 /// Chain/Flag as the input and updates them for the output Chain/Flag.
134 void getCopyToRegs(SDOperand Val, SelectionDAG &DAG,
Chris Lattnere7c0ffb2006-02-23 20:06:57 +0000135 SDOperand &Chain, SDOperand &Flag) const;
Chris Lattner571d9642006-02-23 19:21:04 +0000136
137 /// AddInlineAsmOperands - Add this value to the specified inlineasm node
138 /// operand list. This adds the code marker and includes the number of
139 /// values added into it.
140 void AddInlineAsmOperands(unsigned Code, SelectionDAG &DAG,
Chris Lattnere7c0ffb2006-02-23 20:06:57 +0000141 std::vector<SDOperand> &Ops) const;
Chris Lattner6f87d182006-02-22 22:37:12 +0000142 };
143}
Evan Chengc1e1d972006-01-23 07:01:07 +0000144
Chris Lattner7a60d912005-01-07 07:47:53 +0000145namespace llvm {
146 //===--------------------------------------------------------------------===//
147 /// FunctionLoweringInfo - This contains information that is global to a
148 /// function that is used when lowering a region of the function.
Chris Lattnerd0061952005-01-08 19:52:31 +0000149 class FunctionLoweringInfo {
150 public:
Chris Lattner7a60d912005-01-07 07:47:53 +0000151 TargetLowering &TLI;
152 Function &Fn;
153 MachineFunction &MF;
154 SSARegMap *RegMap;
155
156 FunctionLoweringInfo(TargetLowering &TLI, Function &Fn,MachineFunction &MF);
157
158 /// MBBMap - A mapping from LLVM basic blocks to their machine code entry.
159 std::map<const BasicBlock*, MachineBasicBlock *> MBBMap;
160
161 /// ValueMap - Since we emit code for the function a basic block at a time,
162 /// we must remember which virtual registers hold the values for
163 /// cross-basic-block values.
164 std::map<const Value*, unsigned> ValueMap;
165
166 /// StaticAllocaMap - Keep track of frame indices for fixed sized allocas in
167 /// the entry block. This allows the allocas to be efficiently referenced
168 /// anywhere in the function.
169 std::map<const AllocaInst*, int> StaticAllocaMap;
170
171 unsigned MakeReg(MVT::ValueType VT) {
172 return RegMap->createVirtualRegister(TLI.getRegClassFor(VT));
173 }
Misha Brukman835702a2005-04-21 22:36:52 +0000174
Chris Lattner49409cb2006-03-16 19:51:18 +0000175 unsigned CreateRegForValue(const Value *V);
176
Chris Lattner7a60d912005-01-07 07:47:53 +0000177 unsigned InitializeRegForValue(const Value *V) {
178 unsigned &R = ValueMap[V];
179 assert(R == 0 && "Already initialized this value register!");
180 return R = CreateRegForValue(V);
181 }
182 };
183}
184
185/// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by
186/// PHI nodes or outside of the basic block that defines it.
187static bool isUsedOutsideOfDefiningBlock(Instruction *I) {
188 if (isa<PHINode>(I)) return true;
189 BasicBlock *BB = I->getParent();
190 for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; ++UI)
191 if (cast<Instruction>(*UI)->getParent() != BB || isa<PHINode>(*UI))
192 return true;
193 return false;
194}
195
Chris Lattner6871b232005-10-30 19:42:35 +0000196/// isOnlyUsedInEntryBlock - If the specified argument is only used in the
197/// entry block, return true.
198static bool isOnlyUsedInEntryBlock(Argument *A) {
199 BasicBlock *Entry = A->getParent()->begin();
200 for (Value::use_iterator UI = A->use_begin(), E = A->use_end(); UI != E; ++UI)
201 if (cast<Instruction>(*UI)->getParent() != Entry)
202 return false; // Use not in entry block.
203 return true;
204}
205
Chris Lattner7a60d912005-01-07 07:47:53 +0000206FunctionLoweringInfo::FunctionLoweringInfo(TargetLowering &tli,
Misha Brukman835702a2005-04-21 22:36:52 +0000207 Function &fn, MachineFunction &mf)
Chris Lattner7a60d912005-01-07 07:47:53 +0000208 : TLI(tli), Fn(fn), MF(mf), RegMap(MF.getSSARegMap()) {
209
Chris Lattner6871b232005-10-30 19:42:35 +0000210 // Create a vreg for each argument register that is not dead and is used
211 // outside of the entry block for the function.
212 for (Function::arg_iterator AI = Fn.arg_begin(), E = Fn.arg_end();
213 AI != E; ++AI)
214 if (!isOnlyUsedInEntryBlock(AI))
215 InitializeRegForValue(AI);
216
Chris Lattner7a60d912005-01-07 07:47:53 +0000217 // Initialize the mapping of values to registers. This is only set up for
218 // instruction values that are used outside of the block that defines
219 // them.
Jeff Cohenf8a5e5ae2005-10-01 03:57:14 +0000220 Function::iterator BB = Fn.begin(), EB = Fn.end();
Chris Lattner7a60d912005-01-07 07:47:53 +0000221 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
222 if (AllocaInst *AI = dyn_cast<AllocaInst>(I))
223 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(AI->getArraySize())) {
224 const Type *Ty = AI->getAllocatedType();
225 uint64_t TySize = TLI.getTargetData().getTypeSize(Ty);
Nate Begeman3ee3e692005-11-06 09:00:38 +0000226 unsigned Align =
227 std::max((unsigned)TLI.getTargetData().getTypeAlignment(Ty),
228 AI->getAlignment());
Chris Lattnercbefe722005-05-13 23:14:17 +0000229
230 // If the alignment of the value is smaller than the size of the value,
231 // and if the size of the value is particularly small (<= 8 bytes),
232 // round up to the size of the value for potentially better performance.
233 //
234 // FIXME: This could be made better with a preferred alignment hook in
235 // TargetData. It serves primarily to 8-byte align doubles for X86.
236 if (Align < TySize && TySize <= 8) Align = TySize;
Chris Lattner8396a302005-10-18 22:11:42 +0000237 TySize *= CUI->getValue(); // Get total allocated size.
Chris Lattner0a71a9a2005-10-18 22:14:06 +0000238 if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects.
Chris Lattner7a60d912005-01-07 07:47:53 +0000239 StaticAllocaMap[AI] =
Chris Lattnerd0061952005-01-08 19:52:31 +0000240 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
Chris Lattner7a60d912005-01-07 07:47:53 +0000241 }
242
Jeff Cohenf8a5e5ae2005-10-01 03:57:14 +0000243 for (; BB != EB; ++BB)
244 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
Chris Lattner7a60d912005-01-07 07:47:53 +0000245 if (!I->use_empty() && isUsedOutsideOfDefiningBlock(I))
246 if (!isa<AllocaInst>(I) ||
247 !StaticAllocaMap.count(cast<AllocaInst>(I)))
248 InitializeRegForValue(I);
249
250 // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This
251 // also creates the initial PHI MachineInstrs, though none of the input
252 // operands are populated.
Jeff Cohenf8a5e5ae2005-10-01 03:57:14 +0000253 for (BB = Fn.begin(), EB = Fn.end(); BB != EB; ++BB) {
Chris Lattner7a60d912005-01-07 07:47:53 +0000254 MachineBasicBlock *MBB = new MachineBasicBlock(BB);
255 MBBMap[BB] = MBB;
256 MF.getBasicBlockList().push_back(MBB);
257
258 // Create Machine PHI nodes for LLVM PHI nodes, lowering them as
259 // appropriate.
260 PHINode *PN;
261 for (BasicBlock::iterator I = BB->begin();
Chris Lattner8ea875f2005-01-07 21:34:19 +0000262 (PN = dyn_cast<PHINode>(I)); ++I)
263 if (!PN->use_empty()) {
264 unsigned NumElements =
265 TLI.getNumElements(TLI.getValueType(PN->getType()));
266 unsigned PHIReg = ValueMap[PN];
267 assert(PHIReg &&"PHI node does not have an assigned virtual register!");
268 for (unsigned i = 0; i != NumElements; ++i)
269 BuildMI(MBB, TargetInstrInfo::PHI, PN->getNumOperands(), PHIReg+i);
270 }
Chris Lattner7a60d912005-01-07 07:47:53 +0000271 }
272}
273
Chris Lattner49409cb2006-03-16 19:51:18 +0000274/// CreateRegForValue - Allocate the appropriate number of virtual registers of
275/// the correctly promoted or expanded types. Assign these registers
276/// consecutive vreg numbers and return the first assigned number.
277unsigned FunctionLoweringInfo::CreateRegForValue(const Value *V) {
278 MVT::ValueType VT = TLI.getValueType(V->getType());
279
280 // The number of multiples of registers that we need, to, e.g., split up
281 // a <2 x int64> -> 4 x i32 registers.
282 unsigned NumVectorRegs = 1;
283
284 // If this is a packed type, figure out what type it will decompose into
285 // and how many of the elements it will use.
286 if (VT == MVT::Vector) {
287 const PackedType *PTy = cast<PackedType>(V->getType());
288 unsigned NumElts = PTy->getNumElements();
289 MVT::ValueType EltTy = TLI.getValueType(PTy->getElementType());
290
291 // Divide the input until we get to a supported size. This will always
292 // end with a scalar if the target doesn't support vectors.
293 while (NumElts > 1 && !TLI.isTypeLegal(getVectorType(EltTy, NumElts))) {
294 NumElts >>= 1;
295 NumVectorRegs <<= 1;
296 }
Chris Lattner7ececaa2006-03-16 23:05:19 +0000297 if (NumElts == 1)
298 VT = EltTy;
299 else
300 VT = getVectorType(EltTy, NumElts);
Chris Lattner49409cb2006-03-16 19:51:18 +0000301 }
302
303 // The common case is that we will only create one register for this
304 // value. If we have that case, create and return the virtual register.
305 unsigned NV = TLI.getNumElements(VT);
306 if (NV == 1) {
307 // If we are promoting this value, pick the next largest supported type.
308 MVT::ValueType PromotedType = TLI.getTypeToTransformTo(VT);
309 unsigned Reg = MakeReg(PromotedType);
310 // If this is a vector of supported or promoted types (e.g. 4 x i16),
311 // create all of the registers.
312 for (unsigned i = 1; i != NumVectorRegs; ++i)
313 MakeReg(PromotedType);
314 return Reg;
315 }
316
317 // If this value is represented with multiple target registers, make sure
318 // to create enough consecutive registers of the right (smaller) type.
319 unsigned NT = VT-1; // Find the type to use.
320 while (TLI.getNumElements((MVT::ValueType)NT) != 1)
321 --NT;
322
323 unsigned R = MakeReg((MVT::ValueType)NT);
324 for (unsigned i = 1; i != NV*NumVectorRegs; ++i)
325 MakeReg((MVT::ValueType)NT);
326 return R;
327}
Chris Lattner7a60d912005-01-07 07:47:53 +0000328
329//===----------------------------------------------------------------------===//
330/// SelectionDAGLowering - This is the common target-independent lowering
331/// implementation that is parameterized by a TargetLowering object.
332/// Also, targets can overload any lowering method.
333///
334namespace llvm {
335class SelectionDAGLowering {
336 MachineBasicBlock *CurMBB;
337
338 std::map<const Value*, SDOperand> NodeMap;
339
Chris Lattner4d9651c2005-01-17 22:19:26 +0000340 /// PendingLoads - Loads are not emitted to the program immediately. We bunch
341 /// them up and then emit token factor nodes when possible. This allows us to
342 /// get simple disambiguation between loads without worrying about alias
343 /// analysis.
344 std::vector<SDOperand> PendingLoads;
345
Chris Lattner7a60d912005-01-07 07:47:53 +0000346public:
347 // TLI - This is information that describes the available target features we
348 // need for lowering. This indicates when operations are unavailable,
349 // implemented with a libcall, etc.
350 TargetLowering &TLI;
351 SelectionDAG &DAG;
352 const TargetData &TD;
353
354 /// FuncInfo - Information about the function as a whole.
355 ///
356 FunctionLoweringInfo &FuncInfo;
357
358 SelectionDAGLowering(SelectionDAG &dag, TargetLowering &tli,
Misha Brukman835702a2005-04-21 22:36:52 +0000359 FunctionLoweringInfo &funcinfo)
Chris Lattner7a60d912005-01-07 07:47:53 +0000360 : TLI(tli), DAG(dag), TD(DAG.getTarget().getTargetData()),
361 FuncInfo(funcinfo) {
362 }
363
Chris Lattner4108bb02005-01-17 19:43:36 +0000364 /// getRoot - Return the current virtual root of the Selection DAG.
365 ///
366 SDOperand getRoot() {
Chris Lattner4d9651c2005-01-17 22:19:26 +0000367 if (PendingLoads.empty())
368 return DAG.getRoot();
Misha Brukman835702a2005-04-21 22:36:52 +0000369
Chris Lattner4d9651c2005-01-17 22:19:26 +0000370 if (PendingLoads.size() == 1) {
371 SDOperand Root = PendingLoads[0];
372 DAG.setRoot(Root);
373 PendingLoads.clear();
374 return Root;
375 }
376
377 // Otherwise, we have to make a token factor node.
378 SDOperand Root = DAG.getNode(ISD::TokenFactor, MVT::Other, PendingLoads);
379 PendingLoads.clear();
380 DAG.setRoot(Root);
381 return Root;
Chris Lattner4108bb02005-01-17 19:43:36 +0000382 }
383
Chris Lattner7a60d912005-01-07 07:47:53 +0000384 void visit(Instruction &I) { visit(I.getOpcode(), I); }
385
386 void visit(unsigned Opcode, User &I) {
387 switch (Opcode) {
388 default: assert(0 && "Unknown instruction type encountered!");
389 abort();
390 // Build the switch statement using the Instruction.def file.
391#define HANDLE_INST(NUM, OPCODE, CLASS) \
392 case Instruction::OPCODE:return visit##OPCODE((CLASS&)I);
393#include "llvm/Instruction.def"
394 }
395 }
396
397 void setCurrentBasicBlock(MachineBasicBlock *MBB) { CurMBB = MBB; }
398
Chris Lattner4024c002006-03-15 22:19:46 +0000399 SDOperand getLoadFrom(const Type *Ty, SDOperand Ptr,
400 SDOperand SrcValue, SDOperand Root,
401 bool isVolatile);
Chris Lattner7a60d912005-01-07 07:47:53 +0000402
403 SDOperand getIntPtrConstant(uint64_t Val) {
404 return DAG.getConstant(Val, TLI.getPointerTy());
405 }
406
Chris Lattner8471b152006-03-16 19:57:50 +0000407 SDOperand getValue(const Value *V);
Chris Lattner7a60d912005-01-07 07:47:53 +0000408
409 const SDOperand &setValue(const Value *V, SDOperand NewN) {
410 SDOperand &N = NodeMap[V];
411 assert(N.Val == 0 && "Already set a value for this node!");
412 return N = NewN;
413 }
Chris Lattner1558fc62006-02-01 18:59:47 +0000414
Chris Lattner6f87d182006-02-22 22:37:12 +0000415 RegsForValue GetRegistersForValue(const std::string &ConstrCode,
416 MVT::ValueType VT,
417 bool OutReg, bool InReg,
418 std::set<unsigned> &OutputRegs,
419 std::set<unsigned> &InputRegs);
420
Chris Lattner7a60d912005-01-07 07:47:53 +0000421 // Terminator instructions.
422 void visitRet(ReturnInst &I);
423 void visitBr(BranchInst &I);
424 void visitUnreachable(UnreachableInst &I) { /* noop */ }
425
426 // These all get lowered before this pass.
427 void visitSwitch(SwitchInst &I) { assert(0 && "TODO"); }
428 void visitInvoke(InvokeInst &I) { assert(0 && "TODO"); }
429 void visitUnwind(UnwindInst &I) { assert(0 && "TODO"); }
430
431 //
Nate Begemanb2e089c2005-11-19 00:36:38 +0000432 void visitBinary(User &I, unsigned IntOp, unsigned FPOp, unsigned VecOp);
Nate Begeman127321b2005-11-18 07:42:56 +0000433 void visitShift(User &I, unsigned Opcode);
Nate Begemanb2e089c2005-11-19 00:36:38 +0000434 void visitAdd(User &I) {
435 visitBinary(I, ISD::ADD, ISD::FADD, ISD::VADD);
Chris Lattner6f3b5772005-09-28 22:28:18 +0000436 }
Chris Lattnerf68fd0b2005-04-02 05:04:50 +0000437 void visitSub(User &I);
Nate Begemanb2e089c2005-11-19 00:36:38 +0000438 void visitMul(User &I) {
439 visitBinary(I, ISD::MUL, ISD::FMUL, ISD::VMUL);
Chris Lattner6f3b5772005-09-28 22:28:18 +0000440 }
Chris Lattner7a60d912005-01-07 07:47:53 +0000441 void visitDiv(User &I) {
Chris Lattner6f3b5772005-09-28 22:28:18 +0000442 const Type *Ty = I.getType();
Evan Cheng3bf916d2006-03-03 07:01:07 +0000443 visitBinary(I,
444 Ty->isSigned() ? ISD::SDIV : ISD::UDIV, ISD::FDIV,
445 Ty->isSigned() ? ISD::VSDIV : ISD::VUDIV);
Chris Lattner7a60d912005-01-07 07:47:53 +0000446 }
447 void visitRem(User &I) {
Chris Lattner6f3b5772005-09-28 22:28:18 +0000448 const Type *Ty = I.getType();
Nate Begemanb2e089c2005-11-19 00:36:38 +0000449 visitBinary(I, Ty->isSigned() ? ISD::SREM : ISD::UREM, ISD::FREM, 0);
Chris Lattner7a60d912005-01-07 07:47:53 +0000450 }
Evan Cheng3bf916d2006-03-03 07:01:07 +0000451 void visitAnd(User &I) { visitBinary(I, ISD::AND, 0, ISD::VAND); }
452 void visitOr (User &I) { visitBinary(I, ISD::OR, 0, ISD::VOR); }
453 void visitXor(User &I) { visitBinary(I, ISD::XOR, 0, ISD::VXOR); }
Nate Begeman127321b2005-11-18 07:42:56 +0000454 void visitShl(User &I) { visitShift(I, ISD::SHL); }
455 void visitShr(User &I) {
456 visitShift(I, I.getType()->isUnsigned() ? ISD::SRL : ISD::SRA);
Chris Lattner7a60d912005-01-07 07:47:53 +0000457 }
458
459 void visitSetCC(User &I, ISD::CondCode SignedOpc, ISD::CondCode UnsignedOpc);
460 void visitSetEQ(User &I) { visitSetCC(I, ISD::SETEQ, ISD::SETEQ); }
461 void visitSetNE(User &I) { visitSetCC(I, ISD::SETNE, ISD::SETNE); }
462 void visitSetLE(User &I) { visitSetCC(I, ISD::SETLE, ISD::SETULE); }
463 void visitSetGE(User &I) { visitSetCC(I, ISD::SETGE, ISD::SETUGE); }
464 void visitSetLT(User &I) { visitSetCC(I, ISD::SETLT, ISD::SETULT); }
465 void visitSetGT(User &I) { visitSetCC(I, ISD::SETGT, ISD::SETUGT); }
466
Chris Lattner7c0cd8c2006-03-21 20:44:12 +0000467 void visitExtractElement(ExtractElementInst &I);
Chris Lattner32206f52006-03-18 01:44:44 +0000468 void visitInsertElement(InsertElementInst &I);
469
Chris Lattner7a60d912005-01-07 07:47:53 +0000470 void visitGetElementPtr(User &I);
471 void visitCast(User &I);
472 void visitSelect(User &I);
473 //
474
475 void visitMalloc(MallocInst &I);
476 void visitFree(FreeInst &I);
477 void visitAlloca(AllocaInst &I);
478 void visitLoad(LoadInst &I);
479 void visitStore(StoreInst &I);
480 void visitPHI(PHINode &I) { } // PHI nodes are handled specially.
481 void visitCall(CallInst &I);
Chris Lattner476e67b2006-01-26 22:24:51 +0000482 void visitInlineAsm(CallInst &I);
Chris Lattnercd6f0f42005-11-09 19:44:01 +0000483 const char *visitIntrinsicCall(CallInst &I, unsigned Intrinsic);
Chris Lattnerd96b09a2006-03-24 02:22:33 +0000484 void visitTargetIntrinsic(CallInst &I, unsigned Intrinsic);
Chris Lattner7a60d912005-01-07 07:47:53 +0000485
Chris Lattner7a60d912005-01-07 07:47:53 +0000486 void visitVAStart(CallInst &I);
Chris Lattner7a60d912005-01-07 07:47:53 +0000487 void visitVAArg(VAArgInst &I);
488 void visitVAEnd(CallInst &I);
489 void visitVACopy(CallInst &I);
Chris Lattner58cfd792005-01-09 00:00:49 +0000490 void visitFrameReturnAddress(CallInst &I, bool isFrameAddress);
Chris Lattner7a60d912005-01-07 07:47:53 +0000491
Chris Lattner875def92005-01-11 05:56:49 +0000492 void visitMemIntrinsic(CallInst &I, unsigned Op);
Chris Lattner7a60d912005-01-07 07:47:53 +0000493
494 void visitUserOp1(Instruction &I) {
495 assert(0 && "UserOp1 should not exist at instruction selection time!");
496 abort();
497 }
498 void visitUserOp2(Instruction &I) {
499 assert(0 && "UserOp2 should not exist at instruction selection time!");
500 abort();
501 }
502};
503} // end namespace llvm
504
Chris Lattner8471b152006-03-16 19:57:50 +0000505SDOperand SelectionDAGLowering::getValue(const Value *V) {
506 SDOperand &N = NodeMap[V];
507 if (N.Val) return N;
508
509 const Type *VTy = V->getType();
510 MVT::ValueType VT = TLI.getValueType(VTy);
511 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
512 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
513 visit(CE->getOpcode(), *CE);
514 assert(N.Val && "visit didn't populate the ValueMap!");
515 return N;
516 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) {
517 return N = DAG.getGlobalAddress(GV, VT);
518 } else if (isa<ConstantPointerNull>(C)) {
519 return N = DAG.getConstant(0, TLI.getPointerTy());
520 } else if (isa<UndefValue>(C)) {
Chris Lattnerc16b05e2006-03-19 00:20:20 +0000521 if (!isa<PackedType>(VTy))
522 return N = DAG.getNode(ISD::UNDEF, VT);
523
Chris Lattnerf4e1a532006-03-19 00:52:58 +0000524 // Create a VBUILD_VECTOR of undef nodes.
Chris Lattnerc16b05e2006-03-19 00:20:20 +0000525 const PackedType *PTy = cast<PackedType>(VTy);
526 unsigned NumElements = PTy->getNumElements();
527 MVT::ValueType PVT = TLI.getValueType(PTy->getElementType());
528
529 std::vector<SDOperand> Ops;
530 Ops.assign(NumElements, DAG.getNode(ISD::UNDEF, PVT));
531
532 // Create a VConstant node with generic Vector type.
533 Ops.push_back(DAG.getConstant(NumElements, MVT::i32));
534 Ops.push_back(DAG.getValueType(PVT));
Chris Lattnerf4e1a532006-03-19 00:52:58 +0000535 return N = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, Ops);
Chris Lattner8471b152006-03-16 19:57:50 +0000536 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
537 return N = DAG.getConstantFP(CFP->getValue(), VT);
538 } else if (const PackedType *PTy = dyn_cast<PackedType>(VTy)) {
539 unsigned NumElements = PTy->getNumElements();
540 MVT::ValueType PVT = TLI.getValueType(PTy->getElementType());
Chris Lattner8471b152006-03-16 19:57:50 +0000541
542 // Now that we know the number and type of the elements, push a
543 // Constant or ConstantFP node onto the ops list for each element of
544 // the packed constant.
545 std::vector<SDOperand> Ops;
546 if (ConstantPacked *CP = dyn_cast<ConstantPacked>(C)) {
547 if (MVT::isFloatingPoint(PVT)) {
548 for (unsigned i = 0; i != NumElements; ++i) {
549 const ConstantFP *El = cast<ConstantFP>(CP->getOperand(i));
550 Ops.push_back(DAG.getConstantFP(El->getValue(), PVT));
551 }
552 } else {
553 for (unsigned i = 0; i != NumElements; ++i) {
554 const ConstantIntegral *El =
555 cast<ConstantIntegral>(CP->getOperand(i));
556 Ops.push_back(DAG.getConstant(El->getRawValue(), PVT));
557 }
558 }
559 } else {
560 assert(isa<ConstantAggregateZero>(C) && "Unknown packed constant!");
561 SDOperand Op;
562 if (MVT::isFloatingPoint(PVT))
563 Op = DAG.getConstantFP(0, PVT);
564 else
565 Op = DAG.getConstant(0, PVT);
566 Ops.assign(NumElements, Op);
567 }
568
Chris Lattnerf4e1a532006-03-19 00:52:58 +0000569 // Create a VBUILD_VECTOR node with generic Vector type.
Chris Lattnerc16b05e2006-03-19 00:20:20 +0000570 Ops.push_back(DAG.getConstant(NumElements, MVT::i32));
571 Ops.push_back(DAG.getValueType(PVT));
Chris Lattnerf4e1a532006-03-19 00:52:58 +0000572 return N = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, Ops);
Chris Lattner8471b152006-03-16 19:57:50 +0000573 } else {
574 // Canonicalize all constant ints to be unsigned.
575 return N = DAG.getConstant(cast<ConstantIntegral>(C)->getRawValue(),VT);
576 }
577 }
578
579 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
580 std::map<const AllocaInst*, int>::iterator SI =
581 FuncInfo.StaticAllocaMap.find(AI);
582 if (SI != FuncInfo.StaticAllocaMap.end())
583 return DAG.getFrameIndex(SI->second, TLI.getPointerTy());
584 }
585
586 std::map<const Value*, unsigned>::const_iterator VMI =
587 FuncInfo.ValueMap.find(V);
588 assert(VMI != FuncInfo.ValueMap.end() && "Value not in map!");
589
590 unsigned InReg = VMI->second;
591
592 // If this type is not legal, make it so now.
593 if (VT == MVT::Vector) {
594 // FIXME: We only handle legal vectors right now. We need a VBUILD_VECTOR
595 const PackedType *PTy = cast<PackedType>(VTy);
596 unsigned NumElements = PTy->getNumElements();
597 MVT::ValueType PVT = TLI.getValueType(PTy->getElementType());
598 MVT::ValueType TVT = MVT::getVectorType(PVT, NumElements);
599 assert(TLI.isTypeLegal(TVT) &&
600 "FIXME: Cannot handle illegal vector types here yet!");
601 VT = TVT;
Chris Lattner313229c2006-03-24 22:49:42 +0000602 }
Chris Lattner8471b152006-03-16 19:57:50 +0000603
604 MVT::ValueType DestVT = TLI.getTypeToTransformTo(VT);
605
606 N = DAG.getCopyFromReg(DAG.getEntryNode(), InReg, DestVT);
607 if (DestVT < VT) {
608 // Source must be expanded. This input value is actually coming from the
609 // register pair VMI->second and VMI->second+1.
610 N = DAG.getNode(ISD::BUILD_PAIR, VT, N,
611 DAG.getCopyFromReg(DAG.getEntryNode(), InReg+1, DestVT));
612 } else {
613 if (DestVT > VT) { // Promotion case
614 if (MVT::isFloatingPoint(VT))
615 N = DAG.getNode(ISD::FP_ROUND, VT, N);
616 else
617 N = DAG.getNode(ISD::TRUNCATE, VT, N);
618 }
619 }
620
621 return N;
622}
623
624
Chris Lattner7a60d912005-01-07 07:47:53 +0000625void SelectionDAGLowering::visitRet(ReturnInst &I) {
626 if (I.getNumOperands() == 0) {
Chris Lattner4108bb02005-01-17 19:43:36 +0000627 DAG.setRoot(DAG.getNode(ISD::RET, MVT::Other, getRoot()));
Chris Lattner7a60d912005-01-07 07:47:53 +0000628 return;
629 }
Nate Begeman8c47c3a2006-01-27 21:09:22 +0000630 std::vector<SDOperand> NewValues;
631 NewValues.push_back(getRoot());
632 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
633 SDOperand RetOp = getValue(I.getOperand(i));
634
635 // If this is an integer return value, we need to promote it ourselves to
636 // the full width of a register, since LegalizeOp will use ANY_EXTEND rather
637 // than sign/zero.
638 if (MVT::isInteger(RetOp.getValueType()) &&
639 RetOp.getValueType() < MVT::i64) {
640 MVT::ValueType TmpVT;
641 if (TLI.getTypeAction(MVT::i32) == TargetLowering::Promote)
642 TmpVT = TLI.getTypeToTransformTo(MVT::i32);
643 else
644 TmpVT = MVT::i32;
Chris Lattner7a60d912005-01-07 07:47:53 +0000645
Nate Begeman8c47c3a2006-01-27 21:09:22 +0000646 if (I.getOperand(i)->getType()->isSigned())
647 RetOp = DAG.getNode(ISD::SIGN_EXTEND, TmpVT, RetOp);
648 else
649 RetOp = DAG.getNode(ISD::ZERO_EXTEND, TmpVT, RetOp);
650 }
651 NewValues.push_back(RetOp);
Chris Lattner7a60d912005-01-07 07:47:53 +0000652 }
Nate Begeman8c47c3a2006-01-27 21:09:22 +0000653 DAG.setRoot(DAG.getNode(ISD::RET, MVT::Other, NewValues));
Chris Lattner7a60d912005-01-07 07:47:53 +0000654}
655
656void SelectionDAGLowering::visitBr(BranchInst &I) {
657 // Update machine-CFG edges.
658 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
Chris Lattner7a60d912005-01-07 07:47:53 +0000659
660 // Figure out which block is immediately after the current one.
661 MachineBasicBlock *NextBlock = 0;
662 MachineFunction::iterator BBI = CurMBB;
663 if (++BBI != CurMBB->getParent()->end())
664 NextBlock = BBI;
665
666 if (I.isUnconditional()) {
667 // If this is not a fall-through branch, emit the branch.
668 if (Succ0MBB != NextBlock)
Chris Lattner4108bb02005-01-17 19:43:36 +0000669 DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, getRoot(),
Misha Brukman77451162005-04-22 04:01:18 +0000670 DAG.getBasicBlock(Succ0MBB)));
Chris Lattner7a60d912005-01-07 07:47:53 +0000671 } else {
672 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
Chris Lattner7a60d912005-01-07 07:47:53 +0000673
674 SDOperand Cond = getValue(I.getCondition());
Chris Lattner7a60d912005-01-07 07:47:53 +0000675 if (Succ1MBB == NextBlock) {
676 // If the condition is false, fall through. This means we should branch
677 // if the condition is true to Succ #0.
Chris Lattner4108bb02005-01-17 19:43:36 +0000678 DAG.setRoot(DAG.getNode(ISD::BRCOND, MVT::Other, getRoot(),
Misha Brukman77451162005-04-22 04:01:18 +0000679 Cond, DAG.getBasicBlock(Succ0MBB)));
Chris Lattner7a60d912005-01-07 07:47:53 +0000680 } else if (Succ0MBB == NextBlock) {
681 // If the condition is true, fall through. This means we should branch if
682 // the condition is false to Succ #1. Invert the condition first.
683 SDOperand True = DAG.getConstant(1, Cond.getValueType());
684 Cond = DAG.getNode(ISD::XOR, Cond.getValueType(), Cond, True);
Chris Lattner4108bb02005-01-17 19:43:36 +0000685 DAG.setRoot(DAG.getNode(ISD::BRCOND, MVT::Other, getRoot(),
Misha Brukman77451162005-04-22 04:01:18 +0000686 Cond, DAG.getBasicBlock(Succ1MBB)));
Chris Lattner7a60d912005-01-07 07:47:53 +0000687 } else {
Chris Lattner8a98c7f2005-04-09 03:30:29 +0000688 std::vector<SDOperand> Ops;
689 Ops.push_back(getRoot());
Evan Cheng42c01c82006-02-16 08:27:56 +0000690 // If the false case is the current basic block, then this is a self
691 // loop. We do not want to emit "Loop: ... brcond Out; br Loop", as it
692 // adds an extra instruction in the loop. Instead, invert the
693 // condition and emit "Loop: ... br!cond Loop; br Out.
694 if (CurMBB == Succ1MBB) {
695 std::swap(Succ0MBB, Succ1MBB);
696 SDOperand True = DAG.getConstant(1, Cond.getValueType());
697 Cond = DAG.getNode(ISD::XOR, Cond.getValueType(), Cond, True);
698 }
Nate Begemanbb01d4f2006-03-17 01:40:33 +0000699 SDOperand True = DAG.getNode(ISD::BRCOND, MVT::Other, getRoot(), Cond,
700 DAG.getBasicBlock(Succ0MBB));
701 DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, True,
702 DAG.getBasicBlock(Succ1MBB)));
Chris Lattner7a60d912005-01-07 07:47:53 +0000703 }
704 }
705}
706
Chris Lattnerf68fd0b2005-04-02 05:04:50 +0000707void SelectionDAGLowering::visitSub(User &I) {
708 // -0.0 - X --> fneg
Chris Lattner6f3b5772005-09-28 22:28:18 +0000709 if (I.getType()->isFloatingPoint()) {
710 if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
711 if (CFP->isExactlyValue(-0.0)) {
712 SDOperand Op2 = getValue(I.getOperand(1));
713 setValue(&I, DAG.getNode(ISD::FNEG, Op2.getValueType(), Op2));
714 return;
715 }
Chris Lattner6f3b5772005-09-28 22:28:18 +0000716 }
Nate Begemanb2e089c2005-11-19 00:36:38 +0000717 visitBinary(I, ISD::SUB, ISD::FSUB, ISD::VSUB);
Chris Lattnerf68fd0b2005-04-02 05:04:50 +0000718}
719
Nate Begemanb2e089c2005-11-19 00:36:38 +0000720void SelectionDAGLowering::visitBinary(User &I, unsigned IntOp, unsigned FPOp,
721 unsigned VecOp) {
722 const Type *Ty = I.getType();
Chris Lattner7a60d912005-01-07 07:47:53 +0000723 SDOperand Op1 = getValue(I.getOperand(0));
724 SDOperand Op2 = getValue(I.getOperand(1));
Chris Lattner96c26752005-01-19 22:31:21 +0000725
Chris Lattner19baba62005-11-19 18:40:42 +0000726 if (Ty->isIntegral()) {
Nate Begemanb2e089c2005-11-19 00:36:38 +0000727 setValue(&I, DAG.getNode(IntOp, Op1.getValueType(), Op1, Op2));
728 } else if (Ty->isFloatingPoint()) {
729 setValue(&I, DAG.getNode(FPOp, Op1.getValueType(), Op1, Op2));
730 } else {
731 const PackedType *PTy = cast<PackedType>(Ty);
Chris Lattner32206f52006-03-18 01:44:44 +0000732 SDOperand Num = DAG.getConstant(PTy->getNumElements(), MVT::i32);
733 SDOperand Typ = DAG.getValueType(TLI.getValueType(PTy->getElementType()));
734 setValue(&I, DAG.getNode(VecOp, MVT::Vector, Op1, Op2, Num, Typ));
Nate Begemanb2e089c2005-11-19 00:36:38 +0000735 }
Nate Begeman127321b2005-11-18 07:42:56 +0000736}
Chris Lattner96c26752005-01-19 22:31:21 +0000737
Nate Begeman127321b2005-11-18 07:42:56 +0000738void SelectionDAGLowering::visitShift(User &I, unsigned Opcode) {
739 SDOperand Op1 = getValue(I.getOperand(0));
740 SDOperand Op2 = getValue(I.getOperand(1));
741
742 Op2 = DAG.getNode(ISD::ANY_EXTEND, TLI.getShiftAmountTy(), Op2);
743
Chris Lattner7a60d912005-01-07 07:47:53 +0000744 setValue(&I, DAG.getNode(Opcode, Op1.getValueType(), Op1, Op2));
745}
746
747void SelectionDAGLowering::visitSetCC(User &I,ISD::CondCode SignedOpcode,
748 ISD::CondCode UnsignedOpcode) {
749 SDOperand Op1 = getValue(I.getOperand(0));
750 SDOperand Op2 = getValue(I.getOperand(1));
751 ISD::CondCode Opcode = SignedOpcode;
752 if (I.getOperand(0)->getType()->isUnsigned())
753 Opcode = UnsignedOpcode;
Chris Lattnerd47675e2005-08-09 20:20:18 +0000754 setValue(&I, DAG.getSetCC(MVT::i1, Op1, Op2, Opcode));
Chris Lattner7a60d912005-01-07 07:47:53 +0000755}
756
757void SelectionDAGLowering::visitSelect(User &I) {
758 SDOperand Cond = getValue(I.getOperand(0));
759 SDOperand TrueVal = getValue(I.getOperand(1));
760 SDOperand FalseVal = getValue(I.getOperand(2));
761 setValue(&I, DAG.getNode(ISD::SELECT, TrueVal.getValueType(), Cond,
762 TrueVal, FalseVal));
763}
764
765void SelectionDAGLowering::visitCast(User &I) {
766 SDOperand N = getValue(I.getOperand(0));
Chris Lattner2f4119a2006-03-22 20:09:35 +0000767 MVT::ValueType SrcVT = N.getValueType();
Chris Lattner4024c002006-03-15 22:19:46 +0000768 MVT::ValueType DestVT = TLI.getValueType(I.getType());
Chris Lattner7a60d912005-01-07 07:47:53 +0000769
Chris Lattner2f4119a2006-03-22 20:09:35 +0000770 if (DestVT == MVT::Vector) {
771 // This is a cast to a vector from something else. This is always a bit
772 // convert. Get information about the input vector.
773 const PackedType *DestTy = cast<PackedType>(I.getType());
774 MVT::ValueType EltVT = TLI.getValueType(DestTy->getElementType());
775 setValue(&I, DAG.getNode(ISD::VBIT_CONVERT, DestVT, N,
776 DAG.getConstant(DestTy->getNumElements(),MVT::i32),
777 DAG.getValueType(EltVT)));
778 } else if (SrcVT == DestVT) {
Chris Lattner7a60d912005-01-07 07:47:53 +0000779 setValue(&I, N); // noop cast.
Chris Lattner4024c002006-03-15 22:19:46 +0000780 } else if (DestVT == MVT::i1) {
Chris Lattner2d8b55c2005-05-09 22:17:13 +0000781 // Cast to bool is a comparison against zero, not truncation to zero.
Chris Lattner4024c002006-03-15 22:19:46 +0000782 SDOperand Zero = isInteger(SrcVT) ? DAG.getConstant(0, N.getValueType()) :
Chris Lattner2d8b55c2005-05-09 22:17:13 +0000783 DAG.getConstantFP(0.0, N.getValueType());
Chris Lattnerd47675e2005-08-09 20:20:18 +0000784 setValue(&I, DAG.getSetCC(MVT::i1, N, Zero, ISD::SETNE));
Chris Lattner4024c002006-03-15 22:19:46 +0000785 } else if (isInteger(SrcVT)) {
786 if (isInteger(DestVT)) { // Int -> Int cast
787 if (DestVT < SrcVT) // Truncating cast?
788 setValue(&I, DAG.getNode(ISD::TRUNCATE, DestVT, N));
Chris Lattner2a6db3c2005-01-08 08:08:56 +0000789 else if (I.getOperand(0)->getType()->isSigned())
Chris Lattner4024c002006-03-15 22:19:46 +0000790 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, DestVT, N));
Chris Lattner2a6db3c2005-01-08 08:08:56 +0000791 else
Chris Lattner4024c002006-03-15 22:19:46 +0000792 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, DestVT, N));
Chris Lattnerb893d042006-03-22 22:20:49 +0000793 } else if (isFloatingPoint(DestVT)) { // Int -> FP cast
Chris Lattner2a6db3c2005-01-08 08:08:56 +0000794 if (I.getOperand(0)->getType()->isSigned())
Chris Lattner4024c002006-03-15 22:19:46 +0000795 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, DestVT, N));
Chris Lattner2a6db3c2005-01-08 08:08:56 +0000796 else
Chris Lattner4024c002006-03-15 22:19:46 +0000797 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, DestVT, N));
Chris Lattner2f4119a2006-03-22 20:09:35 +0000798 } else {
799 assert(0 && "Unknown cast!");
Chris Lattner2a6db3c2005-01-08 08:08:56 +0000800 }
Chris Lattner4024c002006-03-15 22:19:46 +0000801 } else if (isFloatingPoint(SrcVT)) {
802 if (isFloatingPoint(DestVT)) { // FP -> FP cast
803 if (DestVT < SrcVT) // Rounding cast?
804 setValue(&I, DAG.getNode(ISD::FP_ROUND, DestVT, N));
Chris Lattner2a6db3c2005-01-08 08:08:56 +0000805 else
Chris Lattner4024c002006-03-15 22:19:46 +0000806 setValue(&I, DAG.getNode(ISD::FP_EXTEND, DestVT, N));
Chris Lattner2f4119a2006-03-22 20:09:35 +0000807 } else if (isInteger(DestVT)) { // FP -> Int cast.
Chris Lattner2a6db3c2005-01-08 08:08:56 +0000808 if (I.getType()->isSigned())
Chris Lattner4024c002006-03-15 22:19:46 +0000809 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, DestVT, N));
Chris Lattner2a6db3c2005-01-08 08:08:56 +0000810 else
Chris Lattner4024c002006-03-15 22:19:46 +0000811 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, DestVT, N));
Chris Lattner2f4119a2006-03-22 20:09:35 +0000812 } else {
813 assert(0 && "Unknown cast!");
Chris Lattner4024c002006-03-15 22:19:46 +0000814 }
815 } else {
Chris Lattner2f4119a2006-03-22 20:09:35 +0000816 assert(SrcVT == MVT::Vector && "Unknown cast!");
817 assert(DestVT != MVT::Vector && "Casts to vector already handled!");
818 // This is a cast from a vector to something else. This is always a bit
819 // convert. Get information about the input vector.
820 setValue(&I, DAG.getNode(ISD::VBIT_CONVERT, DestVT, N));
Chris Lattner7a60d912005-01-07 07:47:53 +0000821 }
822}
823
Chris Lattner32206f52006-03-18 01:44:44 +0000824void SelectionDAGLowering::visitInsertElement(InsertElementInst &I) {
Chris Lattner32206f52006-03-18 01:44:44 +0000825 SDOperand InVec = getValue(I.getOperand(0));
826 SDOperand InVal = getValue(I.getOperand(1));
827 SDOperand InIdx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(),
828 getValue(I.getOperand(2)));
829
Chris Lattner29b23012006-03-19 01:17:20 +0000830 SDOperand Num = *(InVec.Val->op_end()-2);
831 SDOperand Typ = *(InVec.Val->op_end()-1);
832 setValue(&I, DAG.getNode(ISD::VINSERT_VECTOR_ELT, MVT::Vector,
833 InVec, InVal, InIdx, Num, Typ));
Chris Lattner32206f52006-03-18 01:44:44 +0000834}
835
Chris Lattner7c0cd8c2006-03-21 20:44:12 +0000836void SelectionDAGLowering::visitExtractElement(ExtractElementInst &I) {
837 SDOperand InVec = getValue(I.getOperand(0));
838 SDOperand InIdx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(),
839 getValue(I.getOperand(1)));
840 SDOperand Typ = *(InVec.Val->op_end()-1);
841 setValue(&I, DAG.getNode(ISD::VEXTRACT_VECTOR_ELT,
842 TLI.getValueType(I.getType()), InVec, InIdx));
843}
Chris Lattner32206f52006-03-18 01:44:44 +0000844
Chris Lattner7a60d912005-01-07 07:47:53 +0000845void SelectionDAGLowering::visitGetElementPtr(User &I) {
846 SDOperand N = getValue(I.getOperand(0));
847 const Type *Ty = I.getOperand(0)->getType();
848 const Type *UIntPtrTy = TD.getIntPtrType();
849
850 for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end();
851 OI != E; ++OI) {
852 Value *Idx = *OI;
Chris Lattner35397782005-12-05 07:10:48 +0000853 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
Chris Lattner7a60d912005-01-07 07:47:53 +0000854 unsigned Field = cast<ConstantUInt>(Idx)->getValue();
855 if (Field) {
856 // N = N + Offset
857 uint64_t Offset = TD.getStructLayout(StTy)->MemberOffsets[Field];
858 N = DAG.getNode(ISD::ADD, N.getValueType(), N,
Misha Brukman77451162005-04-22 04:01:18 +0000859 getIntPtrConstant(Offset));
Chris Lattner7a60d912005-01-07 07:47:53 +0000860 }
861 Ty = StTy->getElementType(Field);
862 } else {
863 Ty = cast<SequentialType>(Ty)->getElementType();
Chris Lattner19a83992005-01-07 21:56:57 +0000864
Chris Lattner43535a12005-11-09 04:45:33 +0000865 // If this is a constant subscript, handle it quickly.
866 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
867 if (CI->getRawValue() == 0) continue;
Chris Lattner19a83992005-01-07 21:56:57 +0000868
Chris Lattner43535a12005-11-09 04:45:33 +0000869 uint64_t Offs;
870 if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(CI))
871 Offs = (int64_t)TD.getTypeSize(Ty)*CSI->getValue();
872 else
873 Offs = TD.getTypeSize(Ty)*cast<ConstantUInt>(CI)->getValue();
874 N = DAG.getNode(ISD::ADD, N.getValueType(), N, getIntPtrConstant(Offs));
875 continue;
Chris Lattner7a60d912005-01-07 07:47:53 +0000876 }
Chris Lattner43535a12005-11-09 04:45:33 +0000877
878 // N = N + Idx * ElementSize;
879 uint64_t ElementSize = TD.getTypeSize(Ty);
880 SDOperand IdxN = getValue(Idx);
881
882 // If the index is smaller or larger than intptr_t, truncate or extend
883 // it.
884 if (IdxN.getValueType() < N.getValueType()) {
885 if (Idx->getType()->isSigned())
886 IdxN = DAG.getNode(ISD::SIGN_EXTEND, N.getValueType(), IdxN);
887 else
888 IdxN = DAG.getNode(ISD::ZERO_EXTEND, N.getValueType(), IdxN);
889 } else if (IdxN.getValueType() > N.getValueType())
890 IdxN = DAG.getNode(ISD::TRUNCATE, N.getValueType(), IdxN);
891
892 // If this is a multiply by a power of two, turn it into a shl
893 // immediately. This is a very common case.
894 if (isPowerOf2_64(ElementSize)) {
895 unsigned Amt = Log2_64(ElementSize);
896 IdxN = DAG.getNode(ISD::SHL, N.getValueType(), IdxN,
Chris Lattner41fd6d52005-11-09 16:50:40 +0000897 DAG.getConstant(Amt, TLI.getShiftAmountTy()));
Chris Lattner43535a12005-11-09 04:45:33 +0000898 N = DAG.getNode(ISD::ADD, N.getValueType(), N, IdxN);
899 continue;
900 }
901
902 SDOperand Scale = getIntPtrConstant(ElementSize);
903 IdxN = DAG.getNode(ISD::MUL, N.getValueType(), IdxN, Scale);
904 N = DAG.getNode(ISD::ADD, N.getValueType(), N, IdxN);
Chris Lattner7a60d912005-01-07 07:47:53 +0000905 }
906 }
907 setValue(&I, N);
908}
909
910void SelectionDAGLowering::visitAlloca(AllocaInst &I) {
911 // If this is a fixed sized alloca in the entry block of the function,
912 // allocate it statically on the stack.
913 if (FuncInfo.StaticAllocaMap.count(&I))
914 return; // getValue will auto-populate this.
915
916 const Type *Ty = I.getAllocatedType();
917 uint64_t TySize = TLI.getTargetData().getTypeSize(Ty);
Nate Begeman3ee3e692005-11-06 09:00:38 +0000918 unsigned Align = std::max((unsigned)TLI.getTargetData().getTypeAlignment(Ty),
919 I.getAlignment());
Chris Lattner7a60d912005-01-07 07:47:53 +0000920
921 SDOperand AllocSize = getValue(I.getArraySize());
Chris Lattnereccb73d2005-01-22 23:04:37 +0000922 MVT::ValueType IntPtr = TLI.getPointerTy();
923 if (IntPtr < AllocSize.getValueType())
924 AllocSize = DAG.getNode(ISD::TRUNCATE, IntPtr, AllocSize);
925 else if (IntPtr > AllocSize.getValueType())
926 AllocSize = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, AllocSize);
Chris Lattner7a60d912005-01-07 07:47:53 +0000927
Chris Lattnereccb73d2005-01-22 23:04:37 +0000928 AllocSize = DAG.getNode(ISD::MUL, IntPtr, AllocSize,
Chris Lattner7a60d912005-01-07 07:47:53 +0000929 getIntPtrConstant(TySize));
930
931 // Handle alignment. If the requested alignment is less than or equal to the
932 // stack alignment, ignore it and round the size of the allocation up to the
933 // stack alignment size. If the size is greater than the stack alignment, we
934 // note this in the DYNAMIC_STACKALLOC node.
935 unsigned StackAlign =
936 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
937 if (Align <= StackAlign) {
938 Align = 0;
939 // Add SA-1 to the size.
940 AllocSize = DAG.getNode(ISD::ADD, AllocSize.getValueType(), AllocSize,
941 getIntPtrConstant(StackAlign-1));
942 // Mask out the low bits for alignment purposes.
943 AllocSize = DAG.getNode(ISD::AND, AllocSize.getValueType(), AllocSize,
944 getIntPtrConstant(~(uint64_t)(StackAlign-1)));
945 }
946
Chris Lattner96c262e2005-05-14 07:29:57 +0000947 std::vector<MVT::ValueType> VTs;
948 VTs.push_back(AllocSize.getValueType());
949 VTs.push_back(MVT::Other);
950 std::vector<SDOperand> Ops;
951 Ops.push_back(getRoot());
952 Ops.push_back(AllocSize);
953 Ops.push_back(getIntPtrConstant(Align));
954 SDOperand DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
Chris Lattner7a60d912005-01-07 07:47:53 +0000955 DAG.setRoot(setValue(&I, DSA).getValue(1));
956
957 // Inform the Frame Information that we have just allocated a variable-sized
958 // object.
959 CurMBB->getParent()->getFrameInfo()->CreateVariableSizedObject();
960}
961
Chris Lattner7a60d912005-01-07 07:47:53 +0000962void SelectionDAGLowering::visitLoad(LoadInst &I) {
963 SDOperand Ptr = getValue(I.getOperand(0));
Misha Brukman835702a2005-04-21 22:36:52 +0000964
Chris Lattner4d9651c2005-01-17 22:19:26 +0000965 SDOperand Root;
966 if (I.isVolatile())
967 Root = getRoot();
968 else {
969 // Do not serialize non-volatile loads against each other.
970 Root = DAG.getRoot();
971 }
Chris Lattner4024c002006-03-15 22:19:46 +0000972
973 setValue(&I, getLoadFrom(I.getType(), Ptr, DAG.getSrcValue(I.getOperand(0)),
974 Root, I.isVolatile()));
975}
976
977SDOperand SelectionDAGLowering::getLoadFrom(const Type *Ty, SDOperand Ptr,
978 SDOperand SrcValue, SDOperand Root,
979 bool isVolatile) {
Nate Begemanb2e089c2005-11-19 00:36:38 +0000980 SDOperand L;
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000981 if (const PackedType *PTy = dyn_cast<PackedType>(Ty)) {
Nate Begeman07890bb2005-11-22 01:29:36 +0000982 MVT::ValueType PVT = TLI.getValueType(PTy->getElementType());
Chris Lattner32206f52006-03-18 01:44:44 +0000983 L = DAG.getVecLoad(PTy->getNumElements(), PVT, Root, Ptr, SrcValue);
Nate Begemanb2e089c2005-11-19 00:36:38 +0000984 } else {
Chris Lattner4024c002006-03-15 22:19:46 +0000985 L = DAG.getLoad(TLI.getValueType(Ty), Root, Ptr, SrcValue);
Nate Begemanb2e089c2005-11-19 00:36:38 +0000986 }
Chris Lattner4d9651c2005-01-17 22:19:26 +0000987
Chris Lattner4024c002006-03-15 22:19:46 +0000988 if (isVolatile)
Chris Lattner4d9651c2005-01-17 22:19:26 +0000989 DAG.setRoot(L.getValue(1));
990 else
991 PendingLoads.push_back(L.getValue(1));
Chris Lattner4024c002006-03-15 22:19:46 +0000992
993 return L;
Chris Lattner7a60d912005-01-07 07:47:53 +0000994}
995
996
997void SelectionDAGLowering::visitStore(StoreInst &I) {
998 Value *SrcV = I.getOperand(0);
999 SDOperand Src = getValue(SrcV);
1000 SDOperand Ptr = getValue(I.getOperand(1));
Chris Lattnerf5675a02005-05-09 04:08:33 +00001001 DAG.setRoot(DAG.getNode(ISD::STORE, MVT::Other, getRoot(), Src, Ptr,
Andrew Lenharth2edc1882005-06-29 18:54:02 +00001002 DAG.getSrcValue(I.getOperand(1))));
Chris Lattner7a60d912005-01-07 07:47:53 +00001003}
1004
Chris Lattnerd96b09a2006-03-24 02:22:33 +00001005/// IntrinsicCannotAccessMemory - Return true if the specified intrinsic cannot
1006/// access memory and has no other side effects at all.
1007static bool IntrinsicCannotAccessMemory(unsigned IntrinsicID) {
1008#define GET_NO_MEMORY_INTRINSICS
1009#include "llvm/Intrinsics.gen"
1010#undef GET_NO_MEMORY_INTRINSICS
1011 return false;
1012}
1013
1014/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
1015/// node.
1016void SelectionDAGLowering::visitTargetIntrinsic(CallInst &I,
1017 unsigned Intrinsic) {
Chris Lattner313229c2006-03-24 22:49:42 +00001018 bool HasChain = !IntrinsicCannotAccessMemory(Intrinsic);
Chris Lattnerd96b09a2006-03-24 02:22:33 +00001019
1020 // Build the operand list.
1021 std::vector<SDOperand> Ops;
1022 if (HasChain) // If this intrinsic has side-effects, chainify it.
1023 Ops.push_back(getRoot());
1024
1025 // Add the intrinsic ID as an integer operand.
1026 Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
1027
1028 // Add all operands of the call to the operand list.
1029 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
1030 SDOperand Op = getValue(I.getOperand(i));
1031
1032 // If this is a vector type, force it to the right packed type.
1033 if (Op.getValueType() == MVT::Vector) {
1034 const PackedType *OpTy = cast<PackedType>(I.getOperand(i)->getType());
1035 MVT::ValueType EltVT = TLI.getValueType(OpTy->getElementType());
1036
1037 MVT::ValueType VVT = MVT::getVectorType(EltVT, OpTy->getNumElements());
1038 assert(VVT != MVT::Other && "Intrinsic uses a non-legal type?");
1039 Op = DAG.getNode(ISD::VBIT_CONVERT, VVT, Op);
1040 }
1041
1042 assert(TLI.isTypeLegal(Op.getValueType()) &&
1043 "Intrinsic uses a non-legal type?");
1044 Ops.push_back(Op);
1045 }
1046
1047 std::vector<MVT::ValueType> VTs;
1048 if (I.getType() != Type::VoidTy) {
1049 MVT::ValueType VT = TLI.getValueType(I.getType());
1050 if (VT == MVT::Vector) {
1051 const PackedType *DestTy = cast<PackedType>(I.getType());
1052 MVT::ValueType EltVT = TLI.getValueType(DestTy->getElementType());
1053
1054 VT = MVT::getVectorType(EltVT, DestTy->getNumElements());
1055 assert(VT != MVT::Other && "Intrinsic uses a non-legal type?");
1056 }
1057
1058 assert(TLI.isTypeLegal(VT) && "Intrinsic uses a non-legal type?");
1059 VTs.push_back(VT);
1060 }
1061 if (HasChain)
1062 VTs.push_back(MVT::Other);
1063
1064 // Create the node.
1065 SDOperand Result = DAG.getNode(ISD::INTRINSIC, VTs, Ops);
1066
1067 if (HasChain)
1068 DAG.setRoot(Result.getValue(Result.Val->getNumValues()-1));
1069 if (I.getType() != Type::VoidTy) {
1070 if (const PackedType *PTy = dyn_cast<PackedType>(I.getType())) {
1071 MVT::ValueType EVT = TLI.getValueType(PTy->getElementType());
1072 Result = DAG.getNode(ISD::VBIT_CONVERT, MVT::Vector, Result,
1073 DAG.getConstant(PTy->getNumElements(), MVT::i32),
1074 DAG.getValueType(EVT));
1075 }
1076 setValue(&I, Result);
1077 }
1078}
1079
Chris Lattnercd6f0f42005-11-09 19:44:01 +00001080/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
1081/// we want to emit this as a call to a named external function, return the name
1082/// otherwise lower it and return null.
1083const char *
1084SelectionDAGLowering::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) {
1085 switch (Intrinsic) {
Chris Lattnerd96b09a2006-03-24 02:22:33 +00001086 default:
1087 // By default, turn this into a target intrinsic node.
1088 visitTargetIntrinsic(I, Intrinsic);
1089 return 0;
Chris Lattnercd6f0f42005-11-09 19:44:01 +00001090 case Intrinsic::vastart: visitVAStart(I); return 0;
1091 case Intrinsic::vaend: visitVAEnd(I); return 0;
1092 case Intrinsic::vacopy: visitVACopy(I); return 0;
1093 case Intrinsic::returnaddress: visitFrameReturnAddress(I, false); return 0;
1094 case Intrinsic::frameaddress: visitFrameReturnAddress(I, true); return 0;
1095 case Intrinsic::setjmp:
1096 return "_setjmp"+!TLI.usesUnderscoreSetJmpLongJmp();
1097 break;
1098 case Intrinsic::longjmp:
1099 return "_longjmp"+!TLI.usesUnderscoreSetJmpLongJmp();
1100 break;
Chris Lattner093c1592006-03-03 00:00:25 +00001101 case Intrinsic::memcpy_i32:
1102 case Intrinsic::memcpy_i64:
1103 visitMemIntrinsic(I, ISD::MEMCPY);
1104 return 0;
1105 case Intrinsic::memset_i32:
1106 case Intrinsic::memset_i64:
1107 visitMemIntrinsic(I, ISD::MEMSET);
1108 return 0;
1109 case Intrinsic::memmove_i32:
1110 case Intrinsic::memmove_i64:
1111 visitMemIntrinsic(I, ISD::MEMMOVE);
1112 return 0;
Chris Lattnercd6f0f42005-11-09 19:44:01 +00001113
Chris Lattner5d4e61d2005-12-13 17:40:33 +00001114 case Intrinsic::dbg_stoppoint: {
Jim Laskey5995d012006-02-11 01:01:30 +00001115 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
Jim Laskeya8bdac82006-03-23 18:06:46 +00001116 DbgStopPointInst &SPI = cast<DbgStopPointInst>(I);
Jim Laskey70928882006-03-26 22:46:27 +00001117 if (DebugInfo && SPI.getContext() && DebugInfo->Verify(SPI.getContext())) {
Jim Laskey5995d012006-02-11 01:01:30 +00001118 std::vector<SDOperand> Ops;
Chris Lattner435b4022005-11-29 06:21:05 +00001119
Jim Laskey5995d012006-02-11 01:01:30 +00001120 Ops.push_back(getRoot());
Jim Laskeya8bdac82006-03-23 18:06:46 +00001121 Ops.push_back(getValue(SPI.getLineValue()));
1122 Ops.push_back(getValue(SPI.getColumnValue()));
Chris Lattner435b4022005-11-29 06:21:05 +00001123
Jim Laskeya8bdac82006-03-23 18:06:46 +00001124 DebugInfoDesc *DD = DebugInfo->getDescFor(SPI.getContext());
Jim Laskey5995d012006-02-11 01:01:30 +00001125 assert(DD && "Not a debug information descriptor");
Jim Laskeya8bdac82006-03-23 18:06:46 +00001126 CompileUnitDesc *CompileUnit = cast<CompileUnitDesc>(DD);
1127
Jim Laskey5995d012006-02-11 01:01:30 +00001128 Ops.push_back(DAG.getString(CompileUnit->getFileName()));
1129 Ops.push_back(DAG.getString(CompileUnit->getDirectory()));
1130
Jim Laskeya8bdac82006-03-23 18:06:46 +00001131 DAG.setRoot(DAG.getNode(ISD::LOCATION, MVT::Other, Ops));
Chris Lattner5d4e61d2005-12-13 17:40:33 +00001132 }
Jim Laskeya8bdac82006-03-23 18:06:46 +00001133
Chris Lattnerf2b62f32005-11-16 07:22:30 +00001134 return 0;
Chris Lattner435b4022005-11-29 06:21:05 +00001135 }
Jim Laskeya8bdac82006-03-23 18:06:46 +00001136 case Intrinsic::dbg_region_start: {
1137 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
1138 DbgRegionStartInst &RSI = cast<DbgRegionStartInst>(I);
Jim Laskey70928882006-03-26 22:46:27 +00001139 if (DebugInfo && RSI.getContext() && DebugInfo->Verify(RSI.getContext())) {
Jim Laskeya8bdac82006-03-23 18:06:46 +00001140 std::vector<SDOperand> Ops;
1141
1142 unsigned LabelID = DebugInfo->RecordRegionStart(RSI.getContext());
1143
1144 Ops.push_back(getRoot());
1145 Ops.push_back(DAG.getConstant(LabelID, MVT::i32));
1146
1147 DAG.setRoot(DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Ops));
1148 }
1149
Chris Lattnerf2b62f32005-11-16 07:22:30 +00001150 return 0;
Jim Laskeya8bdac82006-03-23 18:06:46 +00001151 }
1152 case Intrinsic::dbg_region_end: {
1153 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
1154 DbgRegionEndInst &REI = cast<DbgRegionEndInst>(I);
Jim Laskey70928882006-03-26 22:46:27 +00001155 if (DebugInfo && REI.getContext() && DebugInfo->Verify(REI.getContext())) {
Jim Laskeya8bdac82006-03-23 18:06:46 +00001156 std::vector<SDOperand> Ops;
1157
1158 unsigned LabelID = DebugInfo->RecordRegionEnd(REI.getContext());
1159
1160 Ops.push_back(getRoot());
1161 Ops.push_back(DAG.getConstant(LabelID, MVT::i32));
1162
1163 DAG.setRoot(DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Ops));
1164 }
1165
Chris Lattnerf2b62f32005-11-16 07:22:30 +00001166 return 0;
Jim Laskeya8bdac82006-03-23 18:06:46 +00001167 }
1168 case Intrinsic::dbg_func_start: {
1169 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
1170 DbgFuncStartInst &FSI = cast<DbgFuncStartInst>(I);
Jim Laskey70928882006-03-26 22:46:27 +00001171 if (DebugInfo && FSI.getSubprogram() &&
1172 DebugInfo->Verify(FSI.getSubprogram())) {
Jim Laskeya8bdac82006-03-23 18:06:46 +00001173 std::vector<SDOperand> Ops;
1174
1175 unsigned LabelID = DebugInfo->RecordRegionStart(FSI.getSubprogram());
1176
1177 Ops.push_back(getRoot());
1178 Ops.push_back(DAG.getConstant(LabelID, MVT::i32));
1179
1180 DAG.setRoot(DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Ops));
1181 }
1182
Chris Lattnerf2b62f32005-11-16 07:22:30 +00001183 return 0;
Jim Laskeya8bdac82006-03-23 18:06:46 +00001184 }
1185 case Intrinsic::dbg_declare: {
1186 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
1187 DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
1188 if (DebugInfo && DebugInfo->Verify(DI.getVariable())) {
1189 std::vector<SDOperand> Ops;
1190
Jim Laskey53f1ecc2006-03-24 09:50:27 +00001191 SDOperand AddressOp = getValue(DI.getAddress());
1192 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(AddressOp)) {
Jim Laskeya8bdac82006-03-23 18:06:46 +00001193 DebugInfo->RecordVariable(DI.getVariable(), FI->getIndex());
1194 }
1195 }
1196
1197 return 0;
1198 }
Chris Lattnercd6f0f42005-11-09 19:44:01 +00001199
Reid Spencerb4f9a6f2006-01-16 21:12:35 +00001200 case Intrinsic::isunordered_f32:
1201 case Intrinsic::isunordered_f64:
Chris Lattnercd6f0f42005-11-09 19:44:01 +00001202 setValue(&I, DAG.getSetCC(MVT::i1,getValue(I.getOperand(1)),
1203 getValue(I.getOperand(2)), ISD::SETUO));
1204 return 0;
1205
Reid Spencerb4f9a6f2006-01-16 21:12:35 +00001206 case Intrinsic::sqrt_f32:
1207 case Intrinsic::sqrt_f64:
Chris Lattnercd6f0f42005-11-09 19:44:01 +00001208 setValue(&I, DAG.getNode(ISD::FSQRT,
1209 getValue(I.getOperand(1)).getValueType(),
1210 getValue(I.getOperand(1))));
1211 return 0;
1212 case Intrinsic::pcmarker: {
1213 SDOperand Tmp = getValue(I.getOperand(1));
1214 DAG.setRoot(DAG.getNode(ISD::PCMARKER, MVT::Other, getRoot(), Tmp));
1215 return 0;
1216 }
Andrew Lenharthde1b5d62005-11-11 22:48:54 +00001217 case Intrinsic::readcyclecounter: {
1218 std::vector<MVT::ValueType> VTs;
1219 VTs.push_back(MVT::i64);
1220 VTs.push_back(MVT::Other);
1221 std::vector<SDOperand> Ops;
1222 Ops.push_back(getRoot());
1223 SDOperand Tmp = DAG.getNode(ISD::READCYCLECOUNTER, VTs, Ops);
1224 setValue(&I, Tmp);
1225 DAG.setRoot(Tmp.getValue(1));
Andrew Lenharth01aa5632005-11-11 16:47:30 +00001226 return 0;
Andrew Lenharthde1b5d62005-11-11 22:48:54 +00001227 }
Nate Begeman2fba8a32006-01-14 03:14:10 +00001228 case Intrinsic::bswap_i16:
Nate Begeman2fba8a32006-01-14 03:14:10 +00001229 case Intrinsic::bswap_i32:
Nate Begeman2fba8a32006-01-14 03:14:10 +00001230 case Intrinsic::bswap_i64:
1231 setValue(&I, DAG.getNode(ISD::BSWAP,
1232 getValue(I.getOperand(1)).getValueType(),
1233 getValue(I.getOperand(1))));
1234 return 0;
Reid Spencerb4f9a6f2006-01-16 21:12:35 +00001235 case Intrinsic::cttz_i8:
1236 case Intrinsic::cttz_i16:
1237 case Intrinsic::cttz_i32:
1238 case Intrinsic::cttz_i64:
Chris Lattnercd6f0f42005-11-09 19:44:01 +00001239 setValue(&I, DAG.getNode(ISD::CTTZ,
1240 getValue(I.getOperand(1)).getValueType(),
1241 getValue(I.getOperand(1))));
1242 return 0;
Reid Spencerb4f9a6f2006-01-16 21:12:35 +00001243 case Intrinsic::ctlz_i8:
1244 case Intrinsic::ctlz_i16:
1245 case Intrinsic::ctlz_i32:
1246 case Intrinsic::ctlz_i64:
Chris Lattnercd6f0f42005-11-09 19:44:01 +00001247 setValue(&I, DAG.getNode(ISD::CTLZ,
1248 getValue(I.getOperand(1)).getValueType(),
1249 getValue(I.getOperand(1))));
1250 return 0;
Reid Spencerb4f9a6f2006-01-16 21:12:35 +00001251 case Intrinsic::ctpop_i8:
1252 case Intrinsic::ctpop_i16:
1253 case Intrinsic::ctpop_i32:
1254 case Intrinsic::ctpop_i64:
Chris Lattnercd6f0f42005-11-09 19:44:01 +00001255 setValue(&I, DAG.getNode(ISD::CTPOP,
1256 getValue(I.getOperand(1)).getValueType(),
1257 getValue(I.getOperand(1))));
1258 return 0;
Chris Lattnerb3266452006-01-13 02:50:02 +00001259 case Intrinsic::stacksave: {
1260 std::vector<MVT::ValueType> VTs;
1261 VTs.push_back(TLI.getPointerTy());
1262 VTs.push_back(MVT::Other);
1263 std::vector<SDOperand> Ops;
1264 Ops.push_back(getRoot());
1265 SDOperand Tmp = DAG.getNode(ISD::STACKSAVE, VTs, Ops);
1266 setValue(&I, Tmp);
1267 DAG.setRoot(Tmp.getValue(1));
1268 return 0;
1269 }
Chris Lattnerdeda32a2006-01-23 05:22:07 +00001270 case Intrinsic::stackrestore: {
1271 SDOperand Tmp = getValue(I.getOperand(1));
1272 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, MVT::Other, getRoot(), Tmp));
Chris Lattnerb3266452006-01-13 02:50:02 +00001273 return 0;
Chris Lattnerdeda32a2006-01-23 05:22:07 +00001274 }
Chris Lattner9e8b6332005-12-12 22:51:16 +00001275 case Intrinsic::prefetch:
1276 // FIXME: Currently discarding prefetches.
1277 return 0;
Chris Lattnercd6f0f42005-11-09 19:44:01 +00001278 }
1279}
1280
1281
Chris Lattner7a60d912005-01-07 07:47:53 +00001282void SelectionDAGLowering::visitCall(CallInst &I) {
Chris Lattner18d2b342005-01-08 22:48:57 +00001283 const char *RenameFn = 0;
Chris Lattnercd6f0f42005-11-09 19:44:01 +00001284 if (Function *F = I.getCalledFunction()) {
Chris Lattner0c140002005-04-02 05:26:53 +00001285 if (F->isExternal())
Chris Lattnercd6f0f42005-11-09 19:44:01 +00001286 if (unsigned IID = F->getIntrinsicID()) {
1287 RenameFn = visitIntrinsicCall(I, IID);
1288 if (!RenameFn)
1289 return;
1290 } else { // Not an LLVM intrinsic.
1291 const std::string &Name = F->getName();
Chris Lattner5c1ba2a2006-03-05 05:09:38 +00001292 if (Name[0] == 'c' && (Name == "copysign" || Name == "copysignf")) {
1293 if (I.getNumOperands() == 3 && // Basic sanity checks.
1294 I.getOperand(1)->getType()->isFloatingPoint() &&
1295 I.getType() == I.getOperand(1)->getType() &&
1296 I.getType() == I.getOperand(2)->getType()) {
1297 SDOperand LHS = getValue(I.getOperand(1));
1298 SDOperand RHS = getValue(I.getOperand(2));
1299 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, LHS.getValueType(),
1300 LHS, RHS));
1301 return;
1302 }
1303 } else if (Name[0] == 'f' && (Name == "fabs" || Name == "fabsf")) {
Chris Lattner0c140002005-04-02 05:26:53 +00001304 if (I.getNumOperands() == 2 && // Basic sanity checks.
1305 I.getOperand(1)->getType()->isFloatingPoint() &&
1306 I.getType() == I.getOperand(1)->getType()) {
Chris Lattnercd6f0f42005-11-09 19:44:01 +00001307 SDOperand Tmp = getValue(I.getOperand(1));
Chris Lattner0c140002005-04-02 05:26:53 +00001308 setValue(&I, DAG.getNode(ISD::FABS, Tmp.getValueType(), Tmp));
1309 return;
1310 }
Chris Lattnercd6f0f42005-11-09 19:44:01 +00001311 } else if (Name[0] == 's' && (Name == "sin" || Name == "sinf")) {
Chris Lattner80026402005-04-30 04:43:14 +00001312 if (I.getNumOperands() == 2 && // Basic sanity checks.
1313 I.getOperand(1)->getType()->isFloatingPoint() &&
Chris Lattner1784a9d22006-02-14 05:39:35 +00001314 I.getType() == I.getOperand(1)->getType()) {
Chris Lattnercd6f0f42005-11-09 19:44:01 +00001315 SDOperand Tmp = getValue(I.getOperand(1));
Chris Lattner80026402005-04-30 04:43:14 +00001316 setValue(&I, DAG.getNode(ISD::FSIN, Tmp.getValueType(), Tmp));
1317 return;
1318 }
Chris Lattnercd6f0f42005-11-09 19:44:01 +00001319 } else if (Name[0] == 'c' && (Name == "cos" || Name == "cosf")) {
Chris Lattner80026402005-04-30 04:43:14 +00001320 if (I.getNumOperands() == 2 && // Basic sanity checks.
1321 I.getOperand(1)->getType()->isFloatingPoint() &&
Chris Lattner1784a9d22006-02-14 05:39:35 +00001322 I.getType() == I.getOperand(1)->getType()) {
Chris Lattnercd6f0f42005-11-09 19:44:01 +00001323 SDOperand Tmp = getValue(I.getOperand(1));
Chris Lattner80026402005-04-30 04:43:14 +00001324 setValue(&I, DAG.getNode(ISD::FCOS, Tmp.getValueType(), Tmp));
1325 return;
1326 }
1327 }
Chris Lattnere4f71d02005-05-14 13:56:55 +00001328 }
Chris Lattner476e67b2006-01-26 22:24:51 +00001329 } else if (isa<InlineAsm>(I.getOperand(0))) {
1330 visitInlineAsm(I);
1331 return;
Chris Lattnercd6f0f42005-11-09 19:44:01 +00001332 }
Misha Brukman835702a2005-04-21 22:36:52 +00001333
Chris Lattner18d2b342005-01-08 22:48:57 +00001334 SDOperand Callee;
1335 if (!RenameFn)
1336 Callee = getValue(I.getOperand(0));
1337 else
1338 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Chris Lattner7a60d912005-01-07 07:47:53 +00001339 std::vector<std::pair<SDOperand, const Type*> > Args;
Chris Lattnercd6f0f42005-11-09 19:44:01 +00001340 Args.reserve(I.getNumOperands());
Chris Lattner7a60d912005-01-07 07:47:53 +00001341 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
1342 Value *Arg = I.getOperand(i);
1343 SDOperand ArgNode = getValue(Arg);
1344 Args.push_back(std::make_pair(ArgNode, Arg->getType()));
1345 }
Misha Brukman835702a2005-04-21 22:36:52 +00001346
Nate Begemanf6565252005-03-26 01:29:23 +00001347 const PointerType *PT = cast<PointerType>(I.getCalledValue()->getType());
1348 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Misha Brukman835702a2005-04-21 22:36:52 +00001349
Chris Lattner1f45cd72005-01-08 19:26:18 +00001350 std::pair<SDOperand,SDOperand> Result =
Chris Lattner111778e2005-05-12 19:56:57 +00001351 TLI.LowerCallTo(getRoot(), I.getType(), FTy->isVarArg(), I.getCallingConv(),
Chris Lattner2e77db62005-05-13 18:50:42 +00001352 I.isTailCall(), Callee, Args, DAG);
Chris Lattner7a60d912005-01-07 07:47:53 +00001353 if (I.getType() != Type::VoidTy)
Chris Lattner1f45cd72005-01-08 19:26:18 +00001354 setValue(&I, Result.first);
1355 DAG.setRoot(Result.second);
Chris Lattner7a60d912005-01-07 07:47:53 +00001356}
1357
Chris Lattner6f87d182006-02-22 22:37:12 +00001358SDOperand RegsForValue::getCopyFromRegs(SelectionDAG &DAG,
Chris Lattnere7c0ffb2006-02-23 20:06:57 +00001359 SDOperand &Chain, SDOperand &Flag)const{
Chris Lattner6f87d182006-02-22 22:37:12 +00001360 SDOperand Val = DAG.getCopyFromReg(Chain, Regs[0], RegVT, Flag);
1361 Chain = Val.getValue(1);
1362 Flag = Val.getValue(2);
1363
1364 // If the result was expanded, copy from the top part.
1365 if (Regs.size() > 1) {
1366 assert(Regs.size() == 2 &&
1367 "Cannot expand to more than 2 elts yet!");
1368 SDOperand Hi = DAG.getCopyFromReg(Chain, Regs[1], RegVT, Flag);
1369 Chain = Val.getValue(1);
1370 Flag = Val.getValue(2);
Chris Lattnere7c0ffb2006-02-23 20:06:57 +00001371 if (DAG.getTargetLoweringInfo().isLittleEndian())
1372 return DAG.getNode(ISD::BUILD_PAIR, ValueVT, Val, Hi);
1373 else
1374 return DAG.getNode(ISD::BUILD_PAIR, ValueVT, Hi, Val);
Chris Lattner6f87d182006-02-22 22:37:12 +00001375 }
Chris Lattner1558fc62006-02-01 18:59:47 +00001376
Chris Lattner6f87d182006-02-22 22:37:12 +00001377 // Otherwise, if the return value was promoted, truncate it to the
1378 // appropriate type.
1379 if (RegVT == ValueVT)
1380 return Val;
1381
1382 if (MVT::isInteger(RegVT))
1383 return DAG.getNode(ISD::TRUNCATE, ValueVT, Val);
1384 else
1385 return DAG.getNode(ISD::FP_ROUND, ValueVT, Val);
1386}
1387
Chris Lattner571d9642006-02-23 19:21:04 +00001388/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
1389/// specified value into the registers specified by this object. This uses
1390/// Chain/Flag as the input and updates them for the output Chain/Flag.
1391void RegsForValue::getCopyToRegs(SDOperand Val, SelectionDAG &DAG,
Chris Lattnere7c0ffb2006-02-23 20:06:57 +00001392 SDOperand &Chain, SDOperand &Flag) const {
Chris Lattner571d9642006-02-23 19:21:04 +00001393 if (Regs.size() == 1) {
1394 // If there is a single register and the types differ, this must be
1395 // a promotion.
1396 if (RegVT != ValueVT) {
1397 if (MVT::isInteger(RegVT))
1398 Val = DAG.getNode(ISD::ANY_EXTEND, RegVT, Val);
1399 else
1400 Val = DAG.getNode(ISD::FP_EXTEND, RegVT, Val);
1401 }
1402 Chain = DAG.getCopyToReg(Chain, Regs[0], Val, Flag);
1403 Flag = Chain.getValue(1);
1404 } else {
Chris Lattnere7c0ffb2006-02-23 20:06:57 +00001405 std::vector<unsigned> R(Regs);
1406 if (!DAG.getTargetLoweringInfo().isLittleEndian())
1407 std::reverse(R.begin(), R.end());
1408
1409 for (unsigned i = 0, e = R.size(); i != e; ++i) {
Chris Lattner571d9642006-02-23 19:21:04 +00001410 SDOperand Part = DAG.getNode(ISD::EXTRACT_ELEMENT, RegVT, Val,
1411 DAG.getConstant(i, MVT::i32));
Chris Lattnere7c0ffb2006-02-23 20:06:57 +00001412 Chain = DAG.getCopyToReg(Chain, R[i], Part, Flag);
Chris Lattner571d9642006-02-23 19:21:04 +00001413 Flag = Chain.getValue(1);
1414 }
1415 }
1416}
Chris Lattner6f87d182006-02-22 22:37:12 +00001417
Chris Lattner571d9642006-02-23 19:21:04 +00001418/// AddInlineAsmOperands - Add this value to the specified inlineasm node
1419/// operand list. This adds the code marker and includes the number of
1420/// values added into it.
1421void RegsForValue::AddInlineAsmOperands(unsigned Code, SelectionDAG &DAG,
Chris Lattnere7c0ffb2006-02-23 20:06:57 +00001422 std::vector<SDOperand> &Ops) const {
Chris Lattner571d9642006-02-23 19:21:04 +00001423 Ops.push_back(DAG.getConstant(Code | (Regs.size() << 3), MVT::i32));
1424 for (unsigned i = 0, e = Regs.size(); i != e; ++i)
1425 Ops.push_back(DAG.getRegister(Regs[i], RegVT));
1426}
Chris Lattner6f87d182006-02-22 22:37:12 +00001427
1428/// isAllocatableRegister - If the specified register is safe to allocate,
1429/// i.e. it isn't a stack pointer or some other special register, return the
1430/// register class for the register. Otherwise, return null.
1431static const TargetRegisterClass *
Chris Lattnerb1124f32006-02-22 23:09:03 +00001432isAllocatableRegister(unsigned Reg, MachineFunction &MF,
1433 const TargetLowering &TLI, const MRegisterInfo *MRI) {
1434 for (MRegisterInfo::regclass_iterator RCI = MRI->regclass_begin(),
1435 E = MRI->regclass_end(); RCI != E; ++RCI) {
1436 const TargetRegisterClass *RC = *RCI;
1437 // If none of the the value types for this register class are valid, we
1438 // can't use it. For example, 64-bit reg classes on 32-bit targets.
1439 bool isLegal = false;
1440 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
1441 I != E; ++I) {
1442 if (TLI.isTypeLegal(*I)) {
1443 isLegal = true;
1444 break;
1445 }
1446 }
1447
1448 if (!isLegal) continue;
1449
Chris Lattner6f87d182006-02-22 22:37:12 +00001450 // NOTE: This isn't ideal. In particular, this might allocate the
1451 // frame pointer in functions that need it (due to them not being taken
1452 // out of allocation, because a variable sized allocation hasn't been seen
1453 // yet). This is a slight code pessimization, but should still work.
Chris Lattnerb1124f32006-02-22 23:09:03 +00001454 for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
1455 E = RC->allocation_order_end(MF); I != E; ++I)
Chris Lattner6f87d182006-02-22 22:37:12 +00001456 if (*I == Reg)
Chris Lattnerb1124f32006-02-22 23:09:03 +00001457 return RC;
Chris Lattner1558fc62006-02-01 18:59:47 +00001458 }
1459 return 0;
Chris Lattner6f87d182006-02-22 22:37:12 +00001460}
1461
1462RegsForValue SelectionDAGLowering::
1463GetRegistersForValue(const std::string &ConstrCode,
1464 MVT::ValueType VT, bool isOutReg, bool isInReg,
1465 std::set<unsigned> &OutputRegs,
1466 std::set<unsigned> &InputRegs) {
1467 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
1468 TLI.getRegForInlineAsmConstraint(ConstrCode, VT);
1469 std::vector<unsigned> Regs;
1470
1471 unsigned NumRegs = VT != MVT::Other ? TLI.getNumElements(VT) : 1;
1472 MVT::ValueType RegVT;
1473 MVT::ValueType ValueVT = VT;
1474
1475 if (PhysReg.first) {
1476 if (VT == MVT::Other)
1477 ValueVT = *PhysReg.second->vt_begin();
1478 RegVT = VT;
1479
1480 // This is a explicit reference to a physical register.
1481 Regs.push_back(PhysReg.first);
1482
1483 // If this is an expanded reference, add the rest of the regs to Regs.
1484 if (NumRegs != 1) {
1485 RegVT = *PhysReg.second->vt_begin();
1486 TargetRegisterClass::iterator I = PhysReg.second->begin();
1487 TargetRegisterClass::iterator E = PhysReg.second->end();
1488 for (; *I != PhysReg.first; ++I)
1489 assert(I != E && "Didn't find reg!");
1490
1491 // Already added the first reg.
1492 --NumRegs; ++I;
1493 for (; NumRegs; --NumRegs, ++I) {
1494 assert(I != E && "Ran out of registers to allocate!");
1495 Regs.push_back(*I);
1496 }
1497 }
1498 return RegsForValue(Regs, RegVT, ValueVT);
1499 }
1500
1501 // This is a reference to a register class. Allocate NumRegs consecutive,
1502 // available, registers from the class.
1503 std::vector<unsigned> RegClassRegs =
1504 TLI.getRegClassForInlineAsmConstraint(ConstrCode, VT);
1505
1506 const MRegisterInfo *MRI = DAG.getTarget().getRegisterInfo();
1507 MachineFunction &MF = *CurMBB->getParent();
1508 unsigned NumAllocated = 0;
1509 for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
1510 unsigned Reg = RegClassRegs[i];
1511 // See if this register is available.
1512 if ((isOutReg && OutputRegs.count(Reg)) || // Already used.
1513 (isInReg && InputRegs.count(Reg))) { // Already used.
1514 // Make sure we find consecutive registers.
1515 NumAllocated = 0;
1516 continue;
1517 }
1518
1519 // Check to see if this register is allocatable (i.e. don't give out the
1520 // stack pointer).
Chris Lattnerb1124f32006-02-22 23:09:03 +00001521 const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, MRI);
Chris Lattner6f87d182006-02-22 22:37:12 +00001522 if (!RC) {
1523 // Make sure we find consecutive registers.
1524 NumAllocated = 0;
1525 continue;
1526 }
1527
1528 // Okay, this register is good, we can use it.
1529 ++NumAllocated;
1530
1531 // If we allocated enough consecutive
1532 if (NumAllocated == NumRegs) {
1533 unsigned RegStart = (i-NumAllocated)+1;
1534 unsigned RegEnd = i+1;
1535 // Mark all of the allocated registers used.
1536 for (unsigned i = RegStart; i != RegEnd; ++i) {
1537 unsigned Reg = RegClassRegs[i];
1538 Regs.push_back(Reg);
1539 if (isOutReg) OutputRegs.insert(Reg); // Mark reg used.
1540 if (isInReg) InputRegs.insert(Reg); // Mark reg used.
1541 }
1542
1543 return RegsForValue(Regs, *RC->vt_begin(), VT);
1544 }
1545 }
1546
1547 // Otherwise, we couldn't allocate enough registers for this.
1548 return RegsForValue();
Chris Lattner1558fc62006-02-01 18:59:47 +00001549}
1550
Chris Lattner6f87d182006-02-22 22:37:12 +00001551
Chris Lattner476e67b2006-01-26 22:24:51 +00001552/// visitInlineAsm - Handle a call to an InlineAsm object.
1553///
1554void SelectionDAGLowering::visitInlineAsm(CallInst &I) {
1555 InlineAsm *IA = cast<InlineAsm>(I.getOperand(0));
1556
1557 SDOperand AsmStr = DAG.getTargetExternalSymbol(IA->getAsmString().c_str(),
1558 MVT::Other);
1559
1560 // Note, we treat inline asms both with and without side-effects as the same.
1561 // If an inline asm doesn't have side effects and doesn't access memory, we
1562 // could not choose to not chain it.
1563 bool hasSideEffects = IA->hasSideEffects();
1564
Chris Lattner3a5ed552006-02-01 01:28:23 +00001565 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
Chris Lattner7ad77df2006-02-22 00:56:39 +00001566 std::vector<MVT::ValueType> ConstraintVTs;
Chris Lattner476e67b2006-01-26 22:24:51 +00001567
1568 /// AsmNodeOperands - A list of pairs. The first element is a register, the
1569 /// second is a bitfield where bit #0 is set if it is a use and bit #1 is set
1570 /// if it is a def of that register.
1571 std::vector<SDOperand> AsmNodeOperands;
1572 AsmNodeOperands.push_back(SDOperand()); // reserve space for input chain
1573 AsmNodeOperands.push_back(AsmStr);
1574
1575 SDOperand Chain = getRoot();
1576 SDOperand Flag;
1577
Chris Lattner1558fc62006-02-01 18:59:47 +00001578 // We fully assign registers here at isel time. This is not optimal, but
1579 // should work. For register classes that correspond to LLVM classes, we
1580 // could let the LLVM RA do its thing, but we currently don't. Do a prepass
1581 // over the constraints, collecting fixed registers that we know we can't use.
1582 std::set<unsigned> OutputRegs, InputRegs;
Chris Lattner7ad77df2006-02-22 00:56:39 +00001583 unsigned OpNum = 1;
Chris Lattner1558fc62006-02-01 18:59:47 +00001584 for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
1585 assert(Constraints[i].Codes.size() == 1 && "Only handles one code so far!");
1586 std::string &ConstraintCode = Constraints[i].Codes[0];
Chris Lattner7f5880b2006-02-02 00:25:23 +00001587
Chris Lattner7ad77df2006-02-22 00:56:39 +00001588 MVT::ValueType OpVT;
1589
1590 // Compute the value type for each operand and add it to ConstraintVTs.
1591 switch (Constraints[i].Type) {
1592 case InlineAsm::isOutput:
1593 if (!Constraints[i].isIndirectOutput) {
1594 assert(I.getType() != Type::VoidTy && "Bad inline asm!");
1595 OpVT = TLI.getValueType(I.getType());
1596 } else {
Chris Lattner9fed5b62006-02-27 23:45:39 +00001597 const Type *OpTy = I.getOperand(OpNum)->getType();
Chris Lattner7ad77df2006-02-22 00:56:39 +00001598 OpVT = TLI.getValueType(cast<PointerType>(OpTy)->getElementType());
1599 OpNum++; // Consumes a call operand.
1600 }
1601 break;
1602 case InlineAsm::isInput:
1603 OpVT = TLI.getValueType(I.getOperand(OpNum)->getType());
1604 OpNum++; // Consumes a call operand.
1605 break;
1606 case InlineAsm::isClobber:
1607 OpVT = MVT::Other;
1608 break;
1609 }
1610
1611 ConstraintVTs.push_back(OpVT);
1612
Chris Lattner6f87d182006-02-22 22:37:12 +00001613 if (TLI.getRegForInlineAsmConstraint(ConstraintCode, OpVT).first == 0)
1614 continue; // Not assigned a fixed reg.
Chris Lattner7ad77df2006-02-22 00:56:39 +00001615
Chris Lattner6f87d182006-02-22 22:37:12 +00001616 // Build a list of regs that this operand uses. This always has a single
1617 // element for promoted/expanded operands.
1618 RegsForValue Regs = GetRegistersForValue(ConstraintCode, OpVT,
1619 false, false,
1620 OutputRegs, InputRegs);
Chris Lattner1558fc62006-02-01 18:59:47 +00001621
1622 switch (Constraints[i].Type) {
1623 case InlineAsm::isOutput:
1624 // We can't assign any other output to this register.
Chris Lattner6f87d182006-02-22 22:37:12 +00001625 OutputRegs.insert(Regs.Regs.begin(), Regs.Regs.end());
Chris Lattner1558fc62006-02-01 18:59:47 +00001626 // If this is an early-clobber output, it cannot be assigned to the same
1627 // value as the input reg.
Chris Lattner7f5880b2006-02-02 00:25:23 +00001628 if (Constraints[i].isEarlyClobber || Constraints[i].hasMatchingInput)
Chris Lattner6f87d182006-02-22 22:37:12 +00001629 InputRegs.insert(Regs.Regs.begin(), Regs.Regs.end());
Chris Lattner1558fc62006-02-01 18:59:47 +00001630 break;
Chris Lattner7ad77df2006-02-22 00:56:39 +00001631 case InlineAsm::isInput:
1632 // We can't assign any other input to this register.
Chris Lattner6f87d182006-02-22 22:37:12 +00001633 InputRegs.insert(Regs.Regs.begin(), Regs.Regs.end());
Chris Lattner7ad77df2006-02-22 00:56:39 +00001634 break;
Chris Lattner1558fc62006-02-01 18:59:47 +00001635 case InlineAsm::isClobber:
1636 // Clobbered regs cannot be used as inputs or outputs.
Chris Lattner6f87d182006-02-22 22:37:12 +00001637 InputRegs.insert(Regs.Regs.begin(), Regs.Regs.end());
1638 OutputRegs.insert(Regs.Regs.begin(), Regs.Regs.end());
Chris Lattner1558fc62006-02-01 18:59:47 +00001639 break;
Chris Lattner1558fc62006-02-01 18:59:47 +00001640 }
1641 }
Chris Lattner3a5ed552006-02-01 01:28:23 +00001642
Chris Lattner5c79f982006-02-21 23:12:12 +00001643 // Loop over all of the inputs, copying the operand values into the
1644 // appropriate registers and processing the output regs.
Chris Lattner6f87d182006-02-22 22:37:12 +00001645 RegsForValue RetValRegs;
1646 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Chris Lattner7ad77df2006-02-22 00:56:39 +00001647 OpNum = 1;
Chris Lattner5c79f982006-02-21 23:12:12 +00001648
Chris Lattner2e56e892006-01-31 02:03:41 +00001649 for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
Chris Lattner3a5ed552006-02-01 01:28:23 +00001650 assert(Constraints[i].Codes.size() == 1 && "Only handles one code so far!");
1651 std::string &ConstraintCode = Constraints[i].Codes[0];
Chris Lattner7ad77df2006-02-22 00:56:39 +00001652
Chris Lattner3a5ed552006-02-01 01:28:23 +00001653 switch (Constraints[i].Type) {
1654 case InlineAsm::isOutput: {
Chris Lattner9fed5b62006-02-27 23:45:39 +00001655 TargetLowering::ConstraintType CTy = TargetLowering::C_RegisterClass;
1656 if (ConstraintCode.size() == 1) // not a physreg name.
1657 CTy = TLI.getConstraintType(ConstraintCode[0]);
1658
1659 if (CTy == TargetLowering::C_Memory) {
1660 // Memory output.
1661 SDOperand InOperandVal = getValue(I.getOperand(OpNum));
1662
1663 // Check that the operand (the address to store to) isn't a float.
1664 if (!MVT::isInteger(InOperandVal.getValueType()))
1665 assert(0 && "MATCH FAIL!");
1666
1667 if (!Constraints[i].isIndirectOutput)
1668 assert(0 && "MATCH FAIL!");
1669
1670 OpNum++; // Consumes a call operand.
1671
1672 // Extend/truncate to the right pointer type if needed.
1673 MVT::ValueType PtrType = TLI.getPointerTy();
1674 if (InOperandVal.getValueType() < PtrType)
1675 InOperandVal = DAG.getNode(ISD::ZERO_EXTEND, PtrType, InOperandVal);
1676 else if (InOperandVal.getValueType() > PtrType)
1677 InOperandVal = DAG.getNode(ISD::TRUNCATE, PtrType, InOperandVal);
1678
1679 // Add information to the INLINEASM node to know about this output.
1680 unsigned ResOpType = 4/*MEM*/ | (1 << 3);
1681 AsmNodeOperands.push_back(DAG.getConstant(ResOpType, MVT::i32));
1682 AsmNodeOperands.push_back(InOperandVal);
1683 break;
1684 }
1685
1686 // Otherwise, this is a register output.
1687 assert(CTy == TargetLowering::C_RegisterClass && "Unknown op type!");
1688
Chris Lattner6f87d182006-02-22 22:37:12 +00001689 // If this is an early-clobber output, or if there is an input
1690 // constraint that matches this, we need to reserve the input register
1691 // so no other inputs allocate to it.
1692 bool UsesInputRegister = false;
1693 if (Constraints[i].isEarlyClobber || Constraints[i].hasMatchingInput)
1694 UsesInputRegister = true;
1695
1696 // Copy the output from the appropriate register. Find a register that
Chris Lattner7ad77df2006-02-22 00:56:39 +00001697 // we can use.
Chris Lattner6f87d182006-02-22 22:37:12 +00001698 RegsForValue Regs =
1699 GetRegistersForValue(ConstraintCode, ConstraintVTs[i],
1700 true, UsesInputRegister,
1701 OutputRegs, InputRegs);
1702 assert(!Regs.Regs.empty() && "Couldn't allocate output reg!");
Chris Lattner7ad77df2006-02-22 00:56:39 +00001703
Chris Lattner3a5ed552006-02-01 01:28:23 +00001704 if (!Constraints[i].isIndirectOutput) {
Chris Lattner6f87d182006-02-22 22:37:12 +00001705 assert(RetValRegs.Regs.empty() &&
Chris Lattner3a5ed552006-02-01 01:28:23 +00001706 "Cannot have multiple output constraints yet!");
Chris Lattner3a5ed552006-02-01 01:28:23 +00001707 assert(I.getType() != Type::VoidTy && "Bad inline asm!");
Chris Lattner6f87d182006-02-22 22:37:12 +00001708 RetValRegs = Regs;
Chris Lattner3a5ed552006-02-01 01:28:23 +00001709 } else {
Chris Lattner9fed5b62006-02-27 23:45:39 +00001710 IndirectStoresToEmit.push_back(std::make_pair(Regs,
1711 I.getOperand(OpNum)));
Chris Lattner3a5ed552006-02-01 01:28:23 +00001712 OpNum++; // Consumes a call operand.
1713 }
Chris Lattner2e56e892006-01-31 02:03:41 +00001714
1715 // Add information to the INLINEASM node to know that this register is
1716 // set.
Chris Lattner571d9642006-02-23 19:21:04 +00001717 Regs.AddInlineAsmOperands(2 /*REGDEF*/, DAG, AsmNodeOperands);
Chris Lattner2e56e892006-01-31 02:03:41 +00001718 break;
1719 }
1720 case InlineAsm::isInput: {
Chris Lattner9fed5b62006-02-27 23:45:39 +00001721 SDOperand InOperandVal = getValue(I.getOperand(OpNum));
Chris Lattner1558fc62006-02-01 18:59:47 +00001722 OpNum++; // Consumes a call operand.
Chris Lattner65ad53f2006-02-04 02:16:44 +00001723
Chris Lattner7f5880b2006-02-02 00:25:23 +00001724 if (isdigit(ConstraintCode[0])) { // Matching constraint?
1725 // If this is required to match an output register we have already set,
1726 // just use its register.
1727 unsigned OperandNo = atoi(ConstraintCode.c_str());
Chris Lattner65ad53f2006-02-04 02:16:44 +00001728
Chris Lattner571d9642006-02-23 19:21:04 +00001729 // Scan until we find the definition we already emitted of this operand.
1730 // When we find it, create a RegsForValue operand.
1731 unsigned CurOp = 2; // The first operand.
1732 for (; OperandNo; --OperandNo) {
1733 // Advance to the next operand.
1734 unsigned NumOps =
1735 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getValue();
1736 assert((NumOps & 7) == 2 /*REGDEF*/ &&
1737 "Skipped past definitions?");
1738 CurOp += (NumOps>>3)+1;
1739 }
1740
1741 unsigned NumOps =
1742 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getValue();
1743 assert((NumOps & 7) == 2 /*REGDEF*/ &&
1744 "Skipped past definitions?");
1745
1746 // Add NumOps>>3 registers to MatchedRegs.
1747 RegsForValue MatchedRegs;
1748 MatchedRegs.ValueVT = InOperandVal.getValueType();
1749 MatchedRegs.RegVT = AsmNodeOperands[CurOp+1].getValueType();
1750 for (unsigned i = 0, e = NumOps>>3; i != e; ++i) {
1751 unsigned Reg=cast<RegisterSDNode>(AsmNodeOperands[++CurOp])->getReg();
1752 MatchedRegs.Regs.push_back(Reg);
1753 }
1754
1755 // Use the produced MatchedRegs object to
1756 MatchedRegs.getCopyToRegs(InOperandVal, DAG, Chain, Flag);
1757 MatchedRegs.AddInlineAsmOperands(1 /*REGUSE*/, DAG, AsmNodeOperands);
Chris Lattner571d9642006-02-23 19:21:04 +00001758 break;
Chris Lattner7f5880b2006-02-02 00:25:23 +00001759 }
Chris Lattner7ef7a642006-02-24 01:11:24 +00001760
1761 TargetLowering::ConstraintType CTy = TargetLowering::C_RegisterClass;
1762 if (ConstraintCode.size() == 1) // not a physreg name.
1763 CTy = TLI.getConstraintType(ConstraintCode[0]);
1764
1765 if (CTy == TargetLowering::C_Other) {
1766 if (!TLI.isOperandValidForConstraint(InOperandVal, ConstraintCode[0]))
1767 assert(0 && "MATCH FAIL!");
1768
1769 // Add information to the INLINEASM node to know about this input.
1770 unsigned ResOpType = 3 /*IMM*/ | (1 << 3);
1771 AsmNodeOperands.push_back(DAG.getConstant(ResOpType, MVT::i32));
1772 AsmNodeOperands.push_back(InOperandVal);
1773 break;
1774 } else if (CTy == TargetLowering::C_Memory) {
1775 // Memory input.
1776
1777 // Check that the operand isn't a float.
1778 if (!MVT::isInteger(InOperandVal.getValueType()))
1779 assert(0 && "MATCH FAIL!");
1780
1781 // Extend/truncate to the right pointer type if needed.
1782 MVT::ValueType PtrType = TLI.getPointerTy();
1783 if (InOperandVal.getValueType() < PtrType)
1784 InOperandVal = DAG.getNode(ISD::ZERO_EXTEND, PtrType, InOperandVal);
1785 else if (InOperandVal.getValueType() > PtrType)
1786 InOperandVal = DAG.getNode(ISD::TRUNCATE, PtrType, InOperandVal);
1787
1788 // Add information to the INLINEASM node to know about this input.
1789 unsigned ResOpType = 4/*MEM*/ | (1 << 3);
1790 AsmNodeOperands.push_back(DAG.getConstant(ResOpType, MVT::i32));
1791 AsmNodeOperands.push_back(InOperandVal);
1792 break;
1793 }
1794
1795 assert(CTy == TargetLowering::C_RegisterClass && "Unknown op type!");
1796
1797 // Copy the input into the appropriate registers.
1798 RegsForValue InRegs =
1799 GetRegistersForValue(ConstraintCode, ConstraintVTs[i],
1800 false, true, OutputRegs, InputRegs);
1801 // FIXME: should be match fail.
1802 assert(!InRegs.Regs.empty() && "Couldn't allocate input reg!");
1803
1804 InRegs.getCopyToRegs(InOperandVal, DAG, Chain, Flag);
1805
1806 InRegs.AddInlineAsmOperands(1/*REGUSE*/, DAG, AsmNodeOperands);
Chris Lattner2e56e892006-01-31 02:03:41 +00001807 break;
1808 }
Chris Lattner571d9642006-02-23 19:21:04 +00001809 case InlineAsm::isClobber: {
1810 RegsForValue ClobberedRegs =
1811 GetRegistersForValue(ConstraintCode, MVT::Other, false, false,
1812 OutputRegs, InputRegs);
1813 // Add the clobbered value to the operand list, so that the register
1814 // allocator is aware that the physreg got clobbered.
1815 if (!ClobberedRegs.Regs.empty())
1816 ClobberedRegs.AddInlineAsmOperands(2/*REGDEF*/, DAG, AsmNodeOperands);
Chris Lattner2e56e892006-01-31 02:03:41 +00001817 break;
1818 }
Chris Lattner571d9642006-02-23 19:21:04 +00001819 }
Chris Lattner2e56e892006-01-31 02:03:41 +00001820 }
Chris Lattner476e67b2006-01-26 22:24:51 +00001821
1822 // Finish up input operands.
1823 AsmNodeOperands[0] = Chain;
1824 if (Flag.Val) AsmNodeOperands.push_back(Flag);
1825
1826 std::vector<MVT::ValueType> VTs;
1827 VTs.push_back(MVT::Other);
1828 VTs.push_back(MVT::Flag);
1829 Chain = DAG.getNode(ISD::INLINEASM, VTs, AsmNodeOperands);
1830 Flag = Chain.getValue(1);
1831
Chris Lattner2e56e892006-01-31 02:03:41 +00001832 // If this asm returns a register value, copy the result from that register
1833 // and set it as the value of the call.
Chris Lattner6f87d182006-02-22 22:37:12 +00001834 if (!RetValRegs.Regs.empty())
1835 setValue(&I, RetValRegs.getCopyFromRegs(DAG, Chain, Flag));
Chris Lattner476e67b2006-01-26 22:24:51 +00001836
Chris Lattner2e56e892006-01-31 02:03:41 +00001837 std::vector<std::pair<SDOperand, Value*> > StoresToEmit;
1838
1839 // Process indirect outputs, first output all of the flagged copies out of
1840 // physregs.
1841 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
Chris Lattner6f87d182006-02-22 22:37:12 +00001842 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
Chris Lattner2e56e892006-01-31 02:03:41 +00001843 Value *Ptr = IndirectStoresToEmit[i].second;
Chris Lattner6f87d182006-02-22 22:37:12 +00001844 SDOperand OutVal = OutRegs.getCopyFromRegs(DAG, Chain, Flag);
1845 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
Chris Lattner2e56e892006-01-31 02:03:41 +00001846 }
1847
1848 // Emit the non-flagged stores from the physregs.
1849 std::vector<SDOperand> OutChains;
1850 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i)
1851 OutChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1852 StoresToEmit[i].first,
1853 getValue(StoresToEmit[i].second),
1854 DAG.getSrcValue(StoresToEmit[i].second)));
1855 if (!OutChains.empty())
1856 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains);
Chris Lattner476e67b2006-01-26 22:24:51 +00001857 DAG.setRoot(Chain);
1858}
1859
1860
Chris Lattner7a60d912005-01-07 07:47:53 +00001861void SelectionDAGLowering::visitMalloc(MallocInst &I) {
1862 SDOperand Src = getValue(I.getOperand(0));
1863
1864 MVT::ValueType IntPtr = TLI.getPointerTy();
Chris Lattnereccb73d2005-01-22 23:04:37 +00001865
1866 if (IntPtr < Src.getValueType())
1867 Src = DAG.getNode(ISD::TRUNCATE, IntPtr, Src);
1868 else if (IntPtr > Src.getValueType())
1869 Src = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, Src);
Chris Lattner7a60d912005-01-07 07:47:53 +00001870
1871 // Scale the source by the type size.
1872 uint64_t ElementSize = TD.getTypeSize(I.getType()->getElementType());
1873 Src = DAG.getNode(ISD::MUL, Src.getValueType(),
1874 Src, getIntPtrConstant(ElementSize));
1875
1876 std::vector<std::pair<SDOperand, const Type*> > Args;
1877 Args.push_back(std::make_pair(Src, TLI.getTargetData().getIntPtrType()));
Chris Lattner1f45cd72005-01-08 19:26:18 +00001878
1879 std::pair<SDOperand,SDOperand> Result =
Chris Lattner2e77db62005-05-13 18:50:42 +00001880 TLI.LowerCallTo(getRoot(), I.getType(), false, CallingConv::C, true,
Chris Lattner1f45cd72005-01-08 19:26:18 +00001881 DAG.getExternalSymbol("malloc", IntPtr),
1882 Args, DAG);
1883 setValue(&I, Result.first); // Pointers always fit in registers
1884 DAG.setRoot(Result.second);
Chris Lattner7a60d912005-01-07 07:47:53 +00001885}
1886
1887void SelectionDAGLowering::visitFree(FreeInst &I) {
1888 std::vector<std::pair<SDOperand, const Type*> > Args;
1889 Args.push_back(std::make_pair(getValue(I.getOperand(0)),
1890 TLI.getTargetData().getIntPtrType()));
1891 MVT::ValueType IntPtr = TLI.getPointerTy();
Chris Lattner1f45cd72005-01-08 19:26:18 +00001892 std::pair<SDOperand,SDOperand> Result =
Chris Lattner2e77db62005-05-13 18:50:42 +00001893 TLI.LowerCallTo(getRoot(), Type::VoidTy, false, CallingConv::C, true,
Chris Lattner1f45cd72005-01-08 19:26:18 +00001894 DAG.getExternalSymbol("free", IntPtr), Args, DAG);
1895 DAG.setRoot(Result.second);
Chris Lattner7a60d912005-01-07 07:47:53 +00001896}
1897
Chris Lattner13d7c252005-08-26 20:54:47 +00001898// InsertAtEndOfBasicBlock - This method should be implemented by targets that
1899// mark instructions with the 'usesCustomDAGSchedInserter' flag. These
1900// instructions are special in various ways, which require special support to
1901// insert. The specified MachineInstr is created but not inserted into any
1902// basic blocks, and the scheduler passes ownership of it to this method.
1903MachineBasicBlock *TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1904 MachineBasicBlock *MBB) {
1905 std::cerr << "If a target marks an instruction with "
1906 "'usesCustomDAGSchedInserter', it must implement "
1907 "TargetLowering::InsertAtEndOfBasicBlock!\n";
1908 abort();
1909 return 0;
1910}
1911
Chris Lattner58cfd792005-01-09 00:00:49 +00001912void SelectionDAGLowering::visitVAStart(CallInst &I) {
Nate Begemane74795c2006-01-25 18:21:52 +00001913 DAG.setRoot(DAG.getNode(ISD::VASTART, MVT::Other, getRoot(),
1914 getValue(I.getOperand(1)),
1915 DAG.getSrcValue(I.getOperand(1))));
Chris Lattner58cfd792005-01-09 00:00:49 +00001916}
1917
1918void SelectionDAGLowering::visitVAArg(VAArgInst &I) {
Nate Begemane74795c2006-01-25 18:21:52 +00001919 SDOperand V = DAG.getVAArg(TLI.getValueType(I.getType()), getRoot(),
1920 getValue(I.getOperand(0)),
1921 DAG.getSrcValue(I.getOperand(0)));
1922 setValue(&I, V);
1923 DAG.setRoot(V.getValue(1));
Chris Lattner7a60d912005-01-07 07:47:53 +00001924}
1925
1926void SelectionDAGLowering::visitVAEnd(CallInst &I) {
Nate Begemane74795c2006-01-25 18:21:52 +00001927 DAG.setRoot(DAG.getNode(ISD::VAEND, MVT::Other, getRoot(),
1928 getValue(I.getOperand(1)),
1929 DAG.getSrcValue(I.getOperand(1))));
Chris Lattner7a60d912005-01-07 07:47:53 +00001930}
1931
1932void SelectionDAGLowering::visitVACopy(CallInst &I) {
Nate Begemane74795c2006-01-25 18:21:52 +00001933 DAG.setRoot(DAG.getNode(ISD::VACOPY, MVT::Other, getRoot(),
1934 getValue(I.getOperand(1)),
1935 getValue(I.getOperand(2)),
1936 DAG.getSrcValue(I.getOperand(1)),
1937 DAG.getSrcValue(I.getOperand(2))));
Chris Lattner7a60d912005-01-07 07:47:53 +00001938}
1939
Chris Lattner58cfd792005-01-09 00:00:49 +00001940// It is always conservatively correct for llvm.returnaddress and
1941// llvm.frameaddress to return 0.
1942std::pair<SDOperand, SDOperand>
1943TargetLowering::LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain,
1944 unsigned Depth, SelectionDAG &DAG) {
1945 return std::make_pair(DAG.getConstant(0, getPointerTy()), Chain);
Chris Lattner7a60d912005-01-07 07:47:53 +00001946}
1947
Chris Lattner29dcc712005-05-14 05:50:48 +00001948SDOperand TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner897cd7d2005-01-16 07:28:41 +00001949 assert(0 && "LowerOperation not implemented for this target!");
1950 abort();
Misha Brukman73e929f2005-02-17 21:39:27 +00001951 return SDOperand();
Chris Lattner897cd7d2005-01-16 07:28:41 +00001952}
1953
Nate Begeman595ec732006-01-28 03:14:31 +00001954SDOperand TargetLowering::CustomPromoteOperation(SDOperand Op,
1955 SelectionDAG &DAG) {
1956 assert(0 && "CustomPromoteOperation not implemented for this target!");
1957 abort();
1958 return SDOperand();
1959}
1960
Chris Lattner58cfd792005-01-09 00:00:49 +00001961void SelectionDAGLowering::visitFrameReturnAddress(CallInst &I, bool isFrame) {
1962 unsigned Depth = (unsigned)cast<ConstantUInt>(I.getOperand(1))->getValue();
1963 std::pair<SDOperand,SDOperand> Result =
Chris Lattner4108bb02005-01-17 19:43:36 +00001964 TLI.LowerFrameReturnAddress(isFrame, getRoot(), Depth, DAG);
Chris Lattner58cfd792005-01-09 00:00:49 +00001965 setValue(&I, Result.first);
1966 DAG.setRoot(Result.second);
Chris Lattner7a60d912005-01-07 07:47:53 +00001967}
1968
Evan Cheng6781b6e2006-02-15 21:59:04 +00001969/// getMemsetValue - Vectorized representation of the memset value
Evan Cheng81fcea82006-02-14 08:22:34 +00001970/// operand.
1971static SDOperand getMemsetValue(SDOperand Value, MVT::ValueType VT,
Evan Cheng93e48652006-02-15 22:12:35 +00001972 SelectionDAG &DAG) {
Evan Cheng81fcea82006-02-14 08:22:34 +00001973 MVT::ValueType CurVT = VT;
1974 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
1975 uint64_t Val = C->getValue() & 255;
1976 unsigned Shift = 8;
1977 while (CurVT != MVT::i8) {
1978 Val = (Val << Shift) | Val;
1979 Shift <<= 1;
1980 CurVT = (MVT::ValueType)((unsigned)CurVT - 1);
Evan Cheng81fcea82006-02-14 08:22:34 +00001981 }
1982 return DAG.getConstant(Val, VT);
1983 } else {
1984 Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value);
1985 unsigned Shift = 8;
1986 while (CurVT != MVT::i8) {
1987 Value =
1988 DAG.getNode(ISD::OR, VT,
1989 DAG.getNode(ISD::SHL, VT, Value,
1990 DAG.getConstant(Shift, MVT::i8)), Value);
1991 Shift <<= 1;
1992 CurVT = (MVT::ValueType)((unsigned)CurVT - 1);
Evan Cheng81fcea82006-02-14 08:22:34 +00001993 }
1994
1995 return Value;
1996 }
1997}
1998
Evan Cheng6781b6e2006-02-15 21:59:04 +00001999/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
2000/// used when a memcpy is turned into a memset when the source is a constant
2001/// string ptr.
2002static SDOperand getMemsetStringVal(MVT::ValueType VT,
2003 SelectionDAG &DAG, TargetLowering &TLI,
2004 std::string &Str, unsigned Offset) {
2005 MVT::ValueType CurVT = VT;
2006 uint64_t Val = 0;
2007 unsigned MSB = getSizeInBits(VT) / 8;
2008 if (TLI.isLittleEndian())
2009 Offset = Offset + MSB - 1;
2010 for (unsigned i = 0; i != MSB; ++i) {
2011 Val = (Val << 8) | Str[Offset];
2012 Offset += TLI.isLittleEndian() ? -1 : 1;
2013 }
2014 return DAG.getConstant(Val, VT);
2015}
2016
Evan Cheng81fcea82006-02-14 08:22:34 +00002017/// getMemBasePlusOffset - Returns base and offset node for the
2018static SDOperand getMemBasePlusOffset(SDOperand Base, unsigned Offset,
2019 SelectionDAG &DAG, TargetLowering &TLI) {
2020 MVT::ValueType VT = Base.getValueType();
2021 return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT));
2022}
2023
Evan Chengdb2a7a72006-02-14 20:12:38 +00002024/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
Evan Chengd5026102006-02-14 09:11:59 +00002025/// to replace the memset / memcpy is below the threshold. It also returns the
2026/// types of the sequence of memory ops to perform memset / memcpy.
Evan Chengdb2a7a72006-02-14 20:12:38 +00002027static bool MeetsMaxMemopRequirement(std::vector<MVT::ValueType> &MemOps,
2028 unsigned Limit, uint64_t Size,
2029 unsigned Align, TargetLowering &TLI) {
Evan Cheng81fcea82006-02-14 08:22:34 +00002030 MVT::ValueType VT;
2031
2032 if (TLI.allowsUnalignedMemoryAccesses()) {
2033 VT = MVT::i64;
2034 } else {
2035 switch (Align & 7) {
2036 case 0:
2037 VT = MVT::i64;
2038 break;
2039 case 4:
2040 VT = MVT::i32;
2041 break;
2042 case 2:
2043 VT = MVT::i16;
2044 break;
2045 default:
2046 VT = MVT::i8;
2047 break;
2048 }
2049 }
2050
Evan Chengd5026102006-02-14 09:11:59 +00002051 MVT::ValueType LVT = MVT::i64;
2052 while (!TLI.isTypeLegal(LVT))
2053 LVT = (MVT::ValueType)((unsigned)LVT - 1);
2054 assert(MVT::isInteger(LVT));
Evan Cheng81fcea82006-02-14 08:22:34 +00002055
Evan Chengd5026102006-02-14 09:11:59 +00002056 if (VT > LVT)
2057 VT = LVT;
2058
Evan Cheng04514992006-02-14 23:05:54 +00002059 unsigned NumMemOps = 0;
Evan Cheng81fcea82006-02-14 08:22:34 +00002060 while (Size != 0) {
2061 unsigned VTSize = getSizeInBits(VT) / 8;
2062 while (VTSize > Size) {
2063 VT = (MVT::ValueType)((unsigned)VT - 1);
Evan Cheng81fcea82006-02-14 08:22:34 +00002064 VTSize >>= 1;
2065 }
Evan Chengd5026102006-02-14 09:11:59 +00002066 assert(MVT::isInteger(VT));
2067
2068 if (++NumMemOps > Limit)
2069 return false;
Evan Cheng81fcea82006-02-14 08:22:34 +00002070 MemOps.push_back(VT);
2071 Size -= VTSize;
2072 }
Evan Chengd5026102006-02-14 09:11:59 +00002073
2074 return true;
Evan Cheng81fcea82006-02-14 08:22:34 +00002075}
2076
Chris Lattner875def92005-01-11 05:56:49 +00002077void SelectionDAGLowering::visitMemIntrinsic(CallInst &I, unsigned Op) {
Evan Cheng81fcea82006-02-14 08:22:34 +00002078 SDOperand Op1 = getValue(I.getOperand(1));
2079 SDOperand Op2 = getValue(I.getOperand(2));
2080 SDOperand Op3 = getValue(I.getOperand(3));
2081 SDOperand Op4 = getValue(I.getOperand(4));
2082 unsigned Align = (unsigned)cast<ConstantSDNode>(Op4)->getValue();
2083 if (Align == 0) Align = 1;
2084
2085 if (ConstantSDNode *Size = dyn_cast<ConstantSDNode>(Op3)) {
2086 std::vector<MVT::ValueType> MemOps;
Evan Cheng81fcea82006-02-14 08:22:34 +00002087
2088 // Expand memset / memcpy to a series of load / store ops
2089 // if the size operand falls below a certain threshold.
2090 std::vector<SDOperand> OutChains;
2091 switch (Op) {
Evan Cheng038521e2006-02-14 19:45:56 +00002092 default: break; // Do nothing for now.
Evan Cheng81fcea82006-02-14 08:22:34 +00002093 case ISD::MEMSET: {
Evan Chengdb2a7a72006-02-14 20:12:38 +00002094 if (MeetsMaxMemopRequirement(MemOps, TLI.getMaxStoresPerMemset(),
2095 Size->getValue(), Align, TLI)) {
Evan Chengd5026102006-02-14 09:11:59 +00002096 unsigned NumMemOps = MemOps.size();
Evan Cheng81fcea82006-02-14 08:22:34 +00002097 unsigned Offset = 0;
2098 for (unsigned i = 0; i < NumMemOps; i++) {
2099 MVT::ValueType VT = MemOps[i];
2100 unsigned VTSize = getSizeInBits(VT) / 8;
Evan Cheng93e48652006-02-15 22:12:35 +00002101 SDOperand Value = getMemsetValue(Op2, VT, DAG);
Evan Chenge2038bd2006-02-15 01:54:51 +00002102 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, getRoot(),
2103 Value,
Chris Lattner6f87d182006-02-22 22:37:12 +00002104 getMemBasePlusOffset(Op1, Offset, DAG, TLI),
2105 DAG.getSrcValue(I.getOperand(1), Offset));
Evan Chenge2038bd2006-02-15 01:54:51 +00002106 OutChains.push_back(Store);
Evan Cheng81fcea82006-02-14 08:22:34 +00002107 Offset += VTSize;
2108 }
Evan Cheng81fcea82006-02-14 08:22:34 +00002109 }
Evan Chenge2038bd2006-02-15 01:54:51 +00002110 break;
Evan Cheng81fcea82006-02-14 08:22:34 +00002111 }
Evan Chenge2038bd2006-02-15 01:54:51 +00002112 case ISD::MEMCPY: {
2113 if (MeetsMaxMemopRequirement(MemOps, TLI.getMaxStoresPerMemcpy(),
2114 Size->getValue(), Align, TLI)) {
2115 unsigned NumMemOps = MemOps.size();
Evan Chengc3dcf5a2006-02-16 23:11:42 +00002116 unsigned SrcOff = 0, DstOff = 0, SrcDelta = 0;
Evan Cheng6781b6e2006-02-15 21:59:04 +00002117 GlobalAddressSDNode *G = NULL;
2118 std::string Str;
Evan Chengc3dcf5a2006-02-16 23:11:42 +00002119 bool CopyFromStr = false;
Evan Cheng6781b6e2006-02-15 21:59:04 +00002120
2121 if (Op2.getOpcode() == ISD::GlobalAddress)
2122 G = cast<GlobalAddressSDNode>(Op2);
2123 else if (Op2.getOpcode() == ISD::ADD &&
2124 Op2.getOperand(0).getOpcode() == ISD::GlobalAddress &&
2125 Op2.getOperand(1).getOpcode() == ISD::Constant) {
2126 G = cast<GlobalAddressSDNode>(Op2.getOperand(0));
Evan Chengc3dcf5a2006-02-16 23:11:42 +00002127 SrcDelta = cast<ConstantSDNode>(Op2.getOperand(1))->getValue();
Evan Cheng6781b6e2006-02-15 21:59:04 +00002128 }
2129 if (G) {
2130 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
Evan Chengc3dcf5a2006-02-16 23:11:42 +00002131 if (GV) {
Evan Cheng38280c02006-03-10 23:52:03 +00002132 Str = GV->getStringValue(false);
Evan Chengc3dcf5a2006-02-16 23:11:42 +00002133 if (!Str.empty()) {
2134 CopyFromStr = true;
2135 SrcOff += SrcDelta;
2136 }
2137 }
Evan Cheng6781b6e2006-02-15 21:59:04 +00002138 }
2139
Evan Chenge2038bd2006-02-15 01:54:51 +00002140 for (unsigned i = 0; i < NumMemOps; i++) {
2141 MVT::ValueType VT = MemOps[i];
2142 unsigned VTSize = getSizeInBits(VT) / 8;
Evan Cheng6781b6e2006-02-15 21:59:04 +00002143 SDOperand Value, Chain, Store;
2144
Evan Chengc3dcf5a2006-02-16 23:11:42 +00002145 if (CopyFromStr) {
Evan Cheng6781b6e2006-02-15 21:59:04 +00002146 Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff);
2147 Chain = getRoot();
2148 Store =
2149 DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2150 getMemBasePlusOffset(Op1, DstOff, DAG, TLI),
2151 DAG.getSrcValue(I.getOperand(1), DstOff));
2152 } else {
2153 Value = DAG.getLoad(VT, getRoot(),
2154 getMemBasePlusOffset(Op2, SrcOff, DAG, TLI),
2155 DAG.getSrcValue(I.getOperand(2), SrcOff));
2156 Chain = Value.getValue(1);
2157 Store =
2158 DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2159 getMemBasePlusOffset(Op1, DstOff, DAG, TLI),
2160 DAG.getSrcValue(I.getOperand(1), DstOff));
2161 }
Evan Chenge2038bd2006-02-15 01:54:51 +00002162 OutChains.push_back(Store);
Evan Cheng6781b6e2006-02-15 21:59:04 +00002163 SrcOff += VTSize;
2164 DstOff += VTSize;
Evan Chenge2038bd2006-02-15 01:54:51 +00002165 }
2166 }
2167 break;
2168 }
2169 }
2170
2171 if (!OutChains.empty()) {
2172 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains));
2173 return;
Evan Cheng81fcea82006-02-14 08:22:34 +00002174 }
2175 }
2176
Chris Lattner875def92005-01-11 05:56:49 +00002177 std::vector<SDOperand> Ops;
Chris Lattner4108bb02005-01-17 19:43:36 +00002178 Ops.push_back(getRoot());
Evan Cheng81fcea82006-02-14 08:22:34 +00002179 Ops.push_back(Op1);
2180 Ops.push_back(Op2);
2181 Ops.push_back(Op3);
2182 Ops.push_back(Op4);
Chris Lattner875def92005-01-11 05:56:49 +00002183 DAG.setRoot(DAG.getNode(Op, MVT::Other, Ops));
Chris Lattner7a60d912005-01-07 07:47:53 +00002184}
2185
Chris Lattner875def92005-01-11 05:56:49 +00002186//===----------------------------------------------------------------------===//
2187// SelectionDAGISel code
2188//===----------------------------------------------------------------------===//
Chris Lattner7a60d912005-01-07 07:47:53 +00002189
2190unsigned SelectionDAGISel::MakeReg(MVT::ValueType VT) {
2191 return RegMap->createVirtualRegister(TLI.getRegClassFor(VT));
2192}
2193
Chris Lattnerc9950c12005-08-17 06:37:43 +00002194void SelectionDAGISel::getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner1a908c82005-08-18 17:35:14 +00002195 // FIXME: we only modify the CFG to split critical edges. This
2196 // updates dom and loop info.
Chris Lattnerc9950c12005-08-17 06:37:43 +00002197}
Chris Lattner7a60d912005-01-07 07:47:53 +00002198
Chris Lattner35397782005-12-05 07:10:48 +00002199
2200/// InsertGEPComputeCode - Insert code into BB to compute Ptr+PtrOffset,
2201/// casting to the type of GEPI.
2202static Value *InsertGEPComputeCode(Value *&V, BasicBlock *BB, Instruction *GEPI,
2203 Value *Ptr, Value *PtrOffset) {
2204 if (V) return V; // Already computed.
2205
2206 BasicBlock::iterator InsertPt;
2207 if (BB == GEPI->getParent()) {
2208 // If insert into the GEP's block, insert right after the GEP.
2209 InsertPt = GEPI;
2210 ++InsertPt;
2211 } else {
2212 // Otherwise, insert at the top of BB, after any PHI nodes
2213 InsertPt = BB->begin();
2214 while (isa<PHINode>(InsertPt)) ++InsertPt;
2215 }
2216
Chris Lattnerbe73d6e2005-12-08 08:00:12 +00002217 // If Ptr is itself a cast, but in some other BB, emit a copy of the cast into
2218 // BB so that there is only one value live across basic blocks (the cast
2219 // operand).
2220 if (CastInst *CI = dyn_cast<CastInst>(Ptr))
2221 if (CI->getParent() != BB && isa<PointerType>(CI->getOperand(0)->getType()))
2222 Ptr = new CastInst(CI->getOperand(0), CI->getType(), "", InsertPt);
2223
Chris Lattner35397782005-12-05 07:10:48 +00002224 // Add the offset, cast it to the right type.
2225 Ptr = BinaryOperator::createAdd(Ptr, PtrOffset, "", InsertPt);
2226 Ptr = new CastInst(Ptr, GEPI->getType(), "", InsertPt);
2227 return V = Ptr;
2228}
2229
2230
2231/// OptimizeGEPExpression - Since we are doing basic-block-at-a-time instruction
2232/// selection, we want to be a bit careful about some things. In particular, if
2233/// we have a GEP instruction that is used in a different block than it is
2234/// defined, the addressing expression of the GEP cannot be folded into loads or
2235/// stores that use it. In this case, decompose the GEP and move constant
2236/// indices into blocks that use it.
2237static void OptimizeGEPExpression(GetElementPtrInst *GEPI,
2238 const TargetData &TD) {
Chris Lattner35397782005-12-05 07:10:48 +00002239 // If this GEP is only used inside the block it is defined in, there is no
2240 // need to rewrite it.
2241 bool isUsedOutsideDefBB = false;
2242 BasicBlock *DefBB = GEPI->getParent();
2243 for (Value::use_iterator UI = GEPI->use_begin(), E = GEPI->use_end();
2244 UI != E; ++UI) {
2245 if (cast<Instruction>(*UI)->getParent() != DefBB) {
2246 isUsedOutsideDefBB = true;
2247 break;
2248 }
2249 }
2250 if (!isUsedOutsideDefBB) return;
2251
2252 // If this GEP has no non-zero constant indices, there is nothing we can do,
2253 // ignore it.
2254 bool hasConstantIndex = false;
2255 for (GetElementPtrInst::op_iterator OI = GEPI->op_begin()+1,
2256 E = GEPI->op_end(); OI != E; ++OI) {
2257 if (ConstantInt *CI = dyn_cast<ConstantInt>(*OI))
2258 if (CI->getRawValue()) {
2259 hasConstantIndex = true;
2260 break;
2261 }
2262 }
Chris Lattnerf1a54c02005-12-11 09:05:13 +00002263 // If this is a GEP &Alloca, 0, 0, forward subst the frame index into uses.
2264 if (!hasConstantIndex && !isa<AllocaInst>(GEPI->getOperand(0))) return;
Chris Lattner35397782005-12-05 07:10:48 +00002265
2266 // Otherwise, decompose the GEP instruction into multiplies and adds. Sum the
2267 // constant offset (which we now know is non-zero) and deal with it later.
2268 uint64_t ConstantOffset = 0;
2269 const Type *UIntPtrTy = TD.getIntPtrType();
2270 Value *Ptr = new CastInst(GEPI->getOperand(0), UIntPtrTy, "", GEPI);
2271 const Type *Ty = GEPI->getOperand(0)->getType();
2272
2273 for (GetElementPtrInst::op_iterator OI = GEPI->op_begin()+1,
2274 E = GEPI->op_end(); OI != E; ++OI) {
2275 Value *Idx = *OI;
2276 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
2277 unsigned Field = cast<ConstantUInt>(Idx)->getValue();
2278 if (Field)
2279 ConstantOffset += TD.getStructLayout(StTy)->MemberOffsets[Field];
2280 Ty = StTy->getElementType(Field);
2281 } else {
2282 Ty = cast<SequentialType>(Ty)->getElementType();
2283
2284 // Handle constant subscripts.
2285 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
2286 if (CI->getRawValue() == 0) continue;
2287
2288 if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(CI))
2289 ConstantOffset += (int64_t)TD.getTypeSize(Ty)*CSI->getValue();
2290 else
2291 ConstantOffset+=TD.getTypeSize(Ty)*cast<ConstantUInt>(CI)->getValue();
2292 continue;
2293 }
2294
2295 // Ptr = Ptr + Idx * ElementSize;
2296
2297 // Cast Idx to UIntPtrTy if needed.
2298 Idx = new CastInst(Idx, UIntPtrTy, "", GEPI);
2299
2300 uint64_t ElementSize = TD.getTypeSize(Ty);
2301 // Mask off bits that should not be set.
2302 ElementSize &= ~0ULL >> (64-UIntPtrTy->getPrimitiveSizeInBits());
2303 Constant *SizeCst = ConstantUInt::get(UIntPtrTy, ElementSize);
2304
2305 // Multiply by the element size and add to the base.
2306 Idx = BinaryOperator::createMul(Idx, SizeCst, "", GEPI);
2307 Ptr = BinaryOperator::createAdd(Ptr, Idx, "", GEPI);
2308 }
2309 }
2310
2311 // Make sure that the offset fits in uintptr_t.
2312 ConstantOffset &= ~0ULL >> (64-UIntPtrTy->getPrimitiveSizeInBits());
2313 Constant *PtrOffset = ConstantUInt::get(UIntPtrTy, ConstantOffset);
2314
2315 // Okay, we have now emitted all of the variable index parts to the BB that
2316 // the GEP is defined in. Loop over all of the using instructions, inserting
2317 // an "add Ptr, ConstantOffset" into each block that uses it and update the
Chris Lattnerbe73d6e2005-12-08 08:00:12 +00002318 // instruction to use the newly computed value, making GEPI dead. When the
2319 // user is a load or store instruction address, we emit the add into the user
2320 // block, otherwise we use a canonical version right next to the gep (these
2321 // won't be foldable as addresses, so we might as well share the computation).
2322
Chris Lattner35397782005-12-05 07:10:48 +00002323 std::map<BasicBlock*,Value*> InsertedExprs;
2324 while (!GEPI->use_empty()) {
2325 Instruction *User = cast<Instruction>(GEPI->use_back());
Chris Lattnerbe73d6e2005-12-08 08:00:12 +00002326
2327 // If this use is not foldable into the addressing mode, use a version
2328 // emitted in the GEP block.
2329 Value *NewVal;
2330 if (!isa<LoadInst>(User) &&
2331 (!isa<StoreInst>(User) || User->getOperand(0) == GEPI)) {
2332 NewVal = InsertGEPComputeCode(InsertedExprs[DefBB], DefBB, GEPI,
2333 Ptr, PtrOffset);
2334 } else {
2335 // Otherwise, insert the code in the User's block so it can be folded into
2336 // any users in that block.
2337 NewVal = InsertGEPComputeCode(InsertedExprs[User->getParent()],
Chris Lattner35397782005-12-05 07:10:48 +00002338 User->getParent(), GEPI,
2339 Ptr, PtrOffset);
Chris Lattner35397782005-12-05 07:10:48 +00002340 }
Chris Lattnerbe73d6e2005-12-08 08:00:12 +00002341 User->replaceUsesOfWith(GEPI, NewVal);
2342 }
Chris Lattner35397782005-12-05 07:10:48 +00002343
2344 // Finally, the GEP is dead, remove it.
2345 GEPI->eraseFromParent();
2346}
2347
Chris Lattner7a60d912005-01-07 07:47:53 +00002348bool SelectionDAGISel::runOnFunction(Function &Fn) {
2349 MachineFunction &MF = MachineFunction::construct(&Fn, TLI.getTargetMachine());
2350 RegMap = MF.getSSARegMap();
2351 DEBUG(std::cerr << "\n\n\n=== " << Fn.getName() << "\n");
2352
Chris Lattner35397782005-12-05 07:10:48 +00002353 // First, split all critical edges for PHI nodes with incoming values that are
2354 // constants, this way the load of the constant into a vreg will not be placed
2355 // into MBBs that are used some other way.
2356 //
2357 // In this pass we also look for GEP instructions that are used across basic
2358 // blocks and rewrites them to improve basic-block-at-a-time selection.
2359 //
Chris Lattner1a908c82005-08-18 17:35:14 +00002360 for (Function::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
2361 PHINode *PN;
Chris Lattner35397782005-12-05 07:10:48 +00002362 BasicBlock::iterator BBI;
2363 for (BBI = BB->begin(); (PN = dyn_cast<PHINode>(BBI)); ++BBI)
Chris Lattner1a908c82005-08-18 17:35:14 +00002364 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
2365 if (isa<Constant>(PN->getIncomingValue(i)))
2366 SplitCriticalEdge(PN->getIncomingBlock(i), BB);
Chris Lattner35397782005-12-05 07:10:48 +00002367
2368 for (BasicBlock::iterator E = BB->end(); BBI != E; )
2369 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(BBI++))
2370 OptimizeGEPExpression(GEPI, TLI.getTargetData());
Chris Lattner1a908c82005-08-18 17:35:14 +00002371 }
Chris Lattnercd6f0f42005-11-09 19:44:01 +00002372
Chris Lattner7a60d912005-01-07 07:47:53 +00002373 FunctionLoweringInfo FuncInfo(TLI, Fn, MF);
2374
2375 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
2376 SelectBasicBlock(I, MF, FuncInfo);
Misha Brukman835702a2005-04-21 22:36:52 +00002377
Chris Lattner7a60d912005-01-07 07:47:53 +00002378 return true;
2379}
2380
2381
Chris Lattner718b5c22005-01-13 17:59:43 +00002382SDOperand SelectionDAGISel::
2383CopyValueToVirtualRegister(SelectionDAGLowering &SDL, Value *V, unsigned Reg) {
Chris Lattner613f79f2005-01-11 22:03:46 +00002384 SDOperand Op = SDL.getValue(V);
Chris Lattnere727af02005-01-13 20:50:02 +00002385 assert((Op.getOpcode() != ISD::CopyFromReg ||
Chris Lattner33182322005-08-16 21:55:35 +00002386 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
Chris Lattnere727af02005-01-13 20:50:02 +00002387 "Copy from a reg to the same reg!");
Chris Lattner33182322005-08-16 21:55:35 +00002388
2389 // If this type is not legal, we must make sure to not create an invalid
2390 // register use.
2391 MVT::ValueType SrcVT = Op.getValueType();
2392 MVT::ValueType DestVT = TLI.getTypeToTransformTo(SrcVT);
2393 SelectionDAG &DAG = SDL.DAG;
2394 if (SrcVT == DestVT) {
2395 return DAG.getCopyToReg(SDL.getRoot(), Reg, Op);
Chris Lattner672a42d2006-03-21 19:20:37 +00002396 } else if (SrcVT == MVT::Vector) {
2397 // FIXME: THIS DOES NOT SUPPORT PROMOTED/EXPANDED ELEMENTS!
2398
2399 // Figure out the right, legal destination reg to copy into.
2400 const PackedType *PTy = cast<PackedType>(V->getType());
2401 unsigned NumElts = PTy->getNumElements();
2402 MVT::ValueType EltTy = TLI.getValueType(PTy->getElementType());
2403
2404 unsigned NumVectorRegs = 1;
2405
2406 // Divide the input until we get to a supported size. This will always
2407 // end with a scalar if the target doesn't support vectors.
2408 while (NumElts > 1 && !TLI.isTypeLegal(getVectorType(EltTy, NumElts))) {
2409 NumElts >>= 1;
2410 NumVectorRegs <<= 1;
2411 }
2412
2413 MVT::ValueType VT;
2414 if (NumElts == 1)
2415 VT = EltTy;
2416 else
2417 VT = getVectorType(EltTy, NumElts);
2418
2419 // FIXME: THIS ASSUMES THAT THE INPUT VECTOR WILL BE LEGAL!
2420 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
2421 return DAG.getCopyToReg(SDL.getRoot(), Reg, Op);
Chris Lattner33182322005-08-16 21:55:35 +00002422 } else if (SrcVT < DestVT) {
2423 // The src value is promoted to the register.
Chris Lattnerba28c272005-08-17 06:06:25 +00002424 if (MVT::isFloatingPoint(SrcVT))
2425 Op = DAG.getNode(ISD::FP_EXTEND, DestVT, Op);
2426 else
Chris Lattnera66403d2005-09-02 00:19:37 +00002427 Op = DAG.getNode(ISD::ANY_EXTEND, DestVT, Op);
Chris Lattner33182322005-08-16 21:55:35 +00002428 return DAG.getCopyToReg(SDL.getRoot(), Reg, Op);
2429 } else {
2430 // The src value is expanded into multiple registers.
2431 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DestVT,
2432 Op, DAG.getConstant(0, MVT::i32));
2433 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DestVT,
2434 Op, DAG.getConstant(1, MVT::i32));
2435 Op = DAG.getCopyToReg(SDL.getRoot(), Reg, Lo);
2436 return DAG.getCopyToReg(Op, Reg+1, Hi);
2437 }
Chris Lattner7a60d912005-01-07 07:47:53 +00002438}
2439
Chris Lattner16f64df2005-01-17 17:15:02 +00002440void SelectionDAGISel::
2441LowerArguments(BasicBlock *BB, SelectionDAGLowering &SDL,
2442 std::vector<SDOperand> &UnorderedChains) {
2443 // If this is the entry block, emit arguments.
2444 Function &F = *BB->getParent();
Chris Lattnere3c2cf42005-01-17 17:55:19 +00002445 FunctionLoweringInfo &FuncInfo = SDL.FuncInfo;
Chris Lattner6871b232005-10-30 19:42:35 +00002446 SDOperand OldRoot = SDL.DAG.getRoot();
2447 std::vector<SDOperand> Args = TLI.LowerArguments(F, SDL.DAG);
Chris Lattner16f64df2005-01-17 17:15:02 +00002448
Chris Lattner6871b232005-10-30 19:42:35 +00002449 unsigned a = 0;
2450 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
2451 AI != E; ++AI, ++a)
2452 if (!AI->use_empty()) {
2453 SDL.setValue(AI, Args[a]);
Chris Lattnerd4382f02005-09-13 19:30:54 +00002454
Chris Lattner6871b232005-10-30 19:42:35 +00002455 // If this argument is live outside of the entry block, insert a copy from
2456 // whereever we got it to the vreg that other BB's will reference it as.
2457 if (FuncInfo.ValueMap.count(AI)) {
2458 SDOperand Copy =
2459 CopyValueToVirtualRegister(SDL, AI, FuncInfo.ValueMap[AI]);
2460 UnorderedChains.push_back(Copy);
2461 }
Chris Lattnere3c2cf42005-01-17 17:55:19 +00002462 }
Chris Lattner6871b232005-10-30 19:42:35 +00002463
2464 // Next, if the function has live ins that need to be copied into vregs,
2465 // emit the copies now, into the top of the block.
2466 MachineFunction &MF = SDL.DAG.getMachineFunction();
2467 if (MF.livein_begin() != MF.livein_end()) {
2468 SSARegMap *RegMap = MF.getSSARegMap();
2469 const MRegisterInfo &MRI = *MF.getTarget().getRegisterInfo();
2470 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
2471 E = MF.livein_end(); LI != E; ++LI)
2472 if (LI->second)
2473 MRI.copyRegToReg(*MF.begin(), MF.begin()->end(), LI->second,
2474 LI->first, RegMap->getRegClass(LI->second));
Chris Lattner16f64df2005-01-17 17:15:02 +00002475 }
Chris Lattner6871b232005-10-30 19:42:35 +00002476
2477 // Finally, if the target has anything special to do, allow it to do so.
2478 EmitFunctionEntryCode(F, SDL.DAG.getMachineFunction());
Chris Lattner16f64df2005-01-17 17:15:02 +00002479}
2480
2481
Chris Lattner7a60d912005-01-07 07:47:53 +00002482void SelectionDAGISel::BuildSelectionDAG(SelectionDAG &DAG, BasicBlock *LLVMBB,
2483 std::vector<std::pair<MachineInstr*, unsigned> > &PHINodesToUpdate,
2484 FunctionLoweringInfo &FuncInfo) {
2485 SelectionDAGLowering SDL(DAG, TLI, FuncInfo);
Chris Lattner718b5c22005-01-13 17:59:43 +00002486
2487 std::vector<SDOperand> UnorderedChains;
Misha Brukman835702a2005-04-21 22:36:52 +00002488
Chris Lattner6871b232005-10-30 19:42:35 +00002489 // Lower any arguments needed in this block if this is the entry block.
2490 if (LLVMBB == &LLVMBB->getParent()->front())
2491 LowerArguments(LLVMBB, SDL, UnorderedChains);
Chris Lattner7a60d912005-01-07 07:47:53 +00002492
2493 BB = FuncInfo.MBBMap[LLVMBB];
2494 SDL.setCurrentBasicBlock(BB);
2495
2496 // Lower all of the non-terminator instructions.
2497 for (BasicBlock::iterator I = LLVMBB->begin(), E = --LLVMBB->end();
2498 I != E; ++I)
2499 SDL.visit(*I);
2500
2501 // Ensure that all instructions which are used outside of their defining
2502 // blocks are available as virtual registers.
2503 for (BasicBlock::iterator I = LLVMBB->begin(), E = LLVMBB->end(); I != E;++I)
Chris Lattner613f79f2005-01-11 22:03:46 +00002504 if (!I->use_empty() && !isa<PHINode>(I)) {
Chris Lattnera2c5d912005-01-09 01:16:24 +00002505 std::map<const Value*, unsigned>::iterator VMI =FuncInfo.ValueMap.find(I);
Chris Lattner7a60d912005-01-07 07:47:53 +00002506 if (VMI != FuncInfo.ValueMap.end())
Chris Lattner718b5c22005-01-13 17:59:43 +00002507 UnorderedChains.push_back(
2508 CopyValueToVirtualRegister(SDL, I, VMI->second));
Chris Lattner7a60d912005-01-07 07:47:53 +00002509 }
2510
2511 // Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
2512 // ensure constants are generated when needed. Remember the virtual registers
2513 // that need to be added to the Machine PHI nodes as input. We cannot just
2514 // directly add them, because expansion might result in multiple MBB's for one
2515 // BB. As such, the start of the BB might correspond to a different MBB than
2516 // the end.
Misha Brukman835702a2005-04-21 22:36:52 +00002517 //
Chris Lattner7a60d912005-01-07 07:47:53 +00002518
2519 // Emit constants only once even if used by multiple PHI nodes.
2520 std::map<Constant*, unsigned> ConstantsOut;
2521
2522 // Check successor nodes PHI nodes that expect a constant to be available from
2523 // this block.
2524 TerminatorInst *TI = LLVMBB->getTerminator();
2525 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
2526 BasicBlock *SuccBB = TI->getSuccessor(succ);
2527 MachineBasicBlock::iterator MBBI = FuncInfo.MBBMap[SuccBB]->begin();
2528 PHINode *PN;
2529
2530 // At this point we know that there is a 1-1 correspondence between LLVM PHI
2531 // nodes and Machine PHI nodes, but the incoming operands have not been
2532 // emitted yet.
2533 for (BasicBlock::iterator I = SuccBB->begin();
Chris Lattner8ea875f2005-01-07 21:34:19 +00002534 (PN = dyn_cast<PHINode>(I)); ++I)
2535 if (!PN->use_empty()) {
2536 unsigned Reg;
2537 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
2538 if (Constant *C = dyn_cast<Constant>(PHIOp)) {
2539 unsigned &RegOut = ConstantsOut[C];
2540 if (RegOut == 0) {
2541 RegOut = FuncInfo.CreateRegForValue(C);
Chris Lattner718b5c22005-01-13 17:59:43 +00002542 UnorderedChains.push_back(
2543 CopyValueToVirtualRegister(SDL, C, RegOut));
Chris Lattner8ea875f2005-01-07 21:34:19 +00002544 }
2545 Reg = RegOut;
2546 } else {
2547 Reg = FuncInfo.ValueMap[PHIOp];
Chris Lattnera2c5d912005-01-09 01:16:24 +00002548 if (Reg == 0) {
Misha Brukman835702a2005-04-21 22:36:52 +00002549 assert(isa<AllocaInst>(PHIOp) &&
Chris Lattnera2c5d912005-01-09 01:16:24 +00002550 FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
2551 "Didn't codegen value into a register!??");
2552 Reg = FuncInfo.CreateRegForValue(PHIOp);
Chris Lattner718b5c22005-01-13 17:59:43 +00002553 UnorderedChains.push_back(
2554 CopyValueToVirtualRegister(SDL, PHIOp, Reg));
Chris Lattnera2c5d912005-01-09 01:16:24 +00002555 }
Chris Lattner7a60d912005-01-07 07:47:53 +00002556 }
Misha Brukman835702a2005-04-21 22:36:52 +00002557
Chris Lattner8ea875f2005-01-07 21:34:19 +00002558 // Remember that this register needs to added to the machine PHI node as
2559 // the input for this MBB.
2560 unsigned NumElements =
2561 TLI.getNumElements(TLI.getValueType(PN->getType()));
2562 for (unsigned i = 0, e = NumElements; i != e; ++i)
2563 PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
Chris Lattner7a60d912005-01-07 07:47:53 +00002564 }
Chris Lattner7a60d912005-01-07 07:47:53 +00002565 }
2566 ConstantsOut.clear();
2567
Chris Lattner718b5c22005-01-13 17:59:43 +00002568 // Turn all of the unordered chains into one factored node.
Chris Lattner24516842005-01-13 19:53:14 +00002569 if (!UnorderedChains.empty()) {
Chris Lattnerb7cad902005-11-09 05:03:03 +00002570 SDOperand Root = SDL.getRoot();
2571 if (Root.getOpcode() != ISD::EntryToken) {
2572 unsigned i = 0, e = UnorderedChains.size();
2573 for (; i != e; ++i) {
2574 assert(UnorderedChains[i].Val->getNumOperands() > 1);
2575 if (UnorderedChains[i].Val->getOperand(0) == Root)
2576 break; // Don't add the root if we already indirectly depend on it.
2577 }
2578
2579 if (i == e)
2580 UnorderedChains.push_back(Root);
2581 }
Chris Lattner718b5c22005-01-13 17:59:43 +00002582 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, UnorderedChains));
2583 }
2584
Chris Lattner7a60d912005-01-07 07:47:53 +00002585 // Lower the terminator after the copies are emitted.
2586 SDL.visit(*LLVMBB->getTerminator());
Chris Lattner4108bb02005-01-17 19:43:36 +00002587
2588 // Make sure the root of the DAG is up-to-date.
2589 DAG.setRoot(SDL.getRoot());
Chris Lattner7a60d912005-01-07 07:47:53 +00002590}
2591
2592void SelectionDAGISel::SelectBasicBlock(BasicBlock *LLVMBB, MachineFunction &MF,
2593 FunctionLoweringInfo &FuncInfo) {
Jim Laskey219d5592006-01-04 22:28:25 +00002594 SelectionDAG DAG(TLI, MF, getAnalysisToUpdate<MachineDebugInfo>());
Chris Lattner7a60d912005-01-07 07:47:53 +00002595 CurDAG = &DAG;
2596 std::vector<std::pair<MachineInstr*, unsigned> > PHINodesToUpdate;
2597
2598 // First step, lower LLVM code to some DAG. This DAG may use operations and
2599 // types that are not supported by the target.
2600 BuildSelectionDAG(DAG, LLVMBB, PHINodesToUpdate, FuncInfo);
2601
Chris Lattnerbcfebeb2005-10-10 16:47:10 +00002602 // Run the DAG combiner in pre-legalize mode.
2603 DAG.Combine(false);
Nate Begeman007c6502005-09-07 00:15:36 +00002604
Chris Lattner7a60d912005-01-07 07:47:53 +00002605 DEBUG(std::cerr << "Lowered selection DAG:\n");
2606 DEBUG(DAG.dump());
2607
2608 // Second step, hack on the DAG until it only uses operations and types that
2609 // the target supports.
Chris Lattnerffcb0ae2005-01-23 04:36:26 +00002610 DAG.Legalize();
Chris Lattner7a60d912005-01-07 07:47:53 +00002611
2612 DEBUG(std::cerr << "Legalized selection DAG:\n");
2613 DEBUG(DAG.dump());
2614
Chris Lattnerbcfebeb2005-10-10 16:47:10 +00002615 // Run the DAG combiner in post-legalize mode.
2616 DAG.Combine(true);
Nate Begeman007c6502005-09-07 00:15:36 +00002617
Evan Cheng739a6a42006-01-21 02:32:06 +00002618 if (ViewISelDAGs) DAG.viewGraph();
Chris Lattner6bd8fd02005-10-05 06:09:10 +00002619
Chris Lattner5ca31d92005-03-30 01:10:47 +00002620 // Third, instruction select all of the operations to machine code, adding the
2621 // code to the MachineBasicBlock.
Chris Lattner7a60d912005-01-07 07:47:53 +00002622 InstructionSelectBasicBlock(DAG);
2623
Chris Lattner7a60d912005-01-07 07:47:53 +00002624 DEBUG(std::cerr << "Selected machine code:\n");
2625 DEBUG(BB->dump());
2626
Chris Lattner5ca31d92005-03-30 01:10:47 +00002627 // Next, now that we know what the last MBB the LLVM BB expanded is, update
Chris Lattner7a60d912005-01-07 07:47:53 +00002628 // PHI nodes in successors.
2629 for (unsigned i = 0, e = PHINodesToUpdate.size(); i != e; ++i) {
2630 MachineInstr *PHI = PHINodesToUpdate[i].first;
2631 assert(PHI->getOpcode() == TargetInstrInfo::PHI &&
2632 "This is not a machine PHI node that we are updating!");
2633 PHI->addRegOperand(PHINodesToUpdate[i].second);
2634 PHI->addMachineBasicBlockOperand(BB);
2635 }
Chris Lattner5ca31d92005-03-30 01:10:47 +00002636
2637 // Finally, add the CFG edges from the last selected MBB to the successor
2638 // MBBs.
2639 TerminatorInst *TI = LLVMBB->getTerminator();
2640 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) {
2641 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[TI->getSuccessor(i)];
2642 BB->addSuccessor(Succ0MBB);
2643 }
Chris Lattner7a60d912005-01-07 07:47:53 +00002644}
Evan Cheng739a6a42006-01-21 02:32:06 +00002645
2646//===----------------------------------------------------------------------===//
2647/// ScheduleAndEmitDAG - Pick a safe ordering and emit instructions for each
2648/// target node in the graph.
2649void SelectionDAGISel::ScheduleAndEmitDAG(SelectionDAG &DAG) {
2650 if (ViewSchedDAGs) DAG.viewGraph();
Evan Chengc1e1d972006-01-23 07:01:07 +00002651 ScheduleDAG *SL = NULL;
2652
2653 switch (ISHeuristic) {
2654 default: assert(0 && "Unrecognized scheduling heuristic");
Evan Chenga6eff8a2006-01-25 09:12:57 +00002655 case defaultScheduling:
2656 if (TLI.getSchedulingPreference() == TargetLowering::SchedulingForLatency)
2657 SL = createSimpleDAGScheduler(noScheduling, DAG, BB);
2658 else /* TargetLowering::SchedulingForRegPressure */
2659 SL = createBURRListDAGScheduler(DAG, BB);
2660 break;
Evan Chengc1e1d972006-01-23 07:01:07 +00002661 case noScheduling:
Chris Lattner5255d042006-03-10 07:49:12 +00002662 SL = createBFS_DAGScheduler(DAG, BB);
2663 break;
Evan Chengc1e1d972006-01-23 07:01:07 +00002664 case simpleScheduling:
Chris Lattner5255d042006-03-10 07:49:12 +00002665 SL = createSimpleDAGScheduler(false, DAG, BB);
2666 break;
Evan Chengc1e1d972006-01-23 07:01:07 +00002667 case simpleNoItinScheduling:
Chris Lattner5255d042006-03-10 07:49:12 +00002668 SL = createSimpleDAGScheduler(true, DAG, BB);
Evan Chengc1e1d972006-01-23 07:01:07 +00002669 break;
Evan Cheng31272342006-01-23 08:26:10 +00002670 case listSchedulingBURR:
2671 SL = createBURRListDAGScheduler(DAG, BB);
Chris Lattner98ecb8e2006-03-05 21:10:33 +00002672 break;
Chris Lattner47639db2006-03-06 00:22:00 +00002673 case listSchedulingTD:
Chris Lattner543832d2006-03-08 04:25:59 +00002674 SL = createTDListDAGScheduler(DAG, BB, CreateTargetHazardRecognizer());
Chris Lattner98ecb8e2006-03-05 21:10:33 +00002675 break;
Evan Chengc1e1d972006-01-23 07:01:07 +00002676 }
Chris Lattnere23928c2006-01-21 19:12:11 +00002677 BB = SL->Run();
Evan Chengf9adce92006-02-04 06:49:00 +00002678 delete SL;
Evan Cheng739a6a42006-01-21 02:32:06 +00002679}
Chris Lattnerdcf785b2006-02-24 02:13:54 +00002680
Chris Lattner543832d2006-03-08 04:25:59 +00002681HazardRecognizer *SelectionDAGISel::CreateTargetHazardRecognizer() {
2682 return new HazardRecognizer();
Chris Lattner47639db2006-03-06 00:22:00 +00002683}
2684
Chris Lattnerdcf785b2006-02-24 02:13:54 +00002685/// SelectInlineAsmMemoryOperands - Calls to this are automatically generated
2686/// by tblgen. Others should not call it.
2687void SelectionDAGISel::
2688SelectInlineAsmMemoryOperands(std::vector<SDOperand> &Ops, SelectionDAG &DAG) {
2689 std::vector<SDOperand> InOps;
2690 std::swap(InOps, Ops);
2691
2692 Ops.push_back(InOps[0]); // input chain.
2693 Ops.push_back(InOps[1]); // input asm string.
2694
2695 const char *AsmStr = cast<ExternalSymbolSDNode>(InOps[1])->getSymbol();
2696 unsigned i = 2, e = InOps.size();
2697 if (InOps[e-1].getValueType() == MVT::Flag)
2698 --e; // Don't process a flag operand if it is here.
2699
2700 while (i != e) {
2701 unsigned Flags = cast<ConstantSDNode>(InOps[i])->getValue();
2702 if ((Flags & 7) != 4 /*MEM*/) {
2703 // Just skip over this operand, copying the operands verbatim.
2704 Ops.insert(Ops.end(), InOps.begin()+i, InOps.begin()+i+(Flags >> 3) + 1);
2705 i += (Flags >> 3) + 1;
2706 } else {
2707 assert((Flags >> 3) == 1 && "Memory operand with multiple values?");
2708 // Otherwise, this is a memory operand. Ask the target to select it.
2709 std::vector<SDOperand> SelOps;
2710 if (SelectInlineAsmMemoryOperand(InOps[i+1], 'm', SelOps, DAG)) {
2711 std::cerr << "Could not match memory address. Inline asm failure!\n";
2712 exit(1);
2713 }
2714
2715 // Add this to the output node.
2716 Ops.push_back(DAG.getConstant(4/*MEM*/ | (SelOps.size() << 3), MVT::i32));
2717 Ops.insert(Ops.end(), SelOps.begin(), SelOps.end());
2718 i += 2;
2719 }
2720 }
2721
2722 // Add the flag input back if present.
2723 if (e != InOps.size())
2724 Ops.push_back(InOps.back());
2725}