blob: 30871dd4e114091c2ab4239118f96294f57a1f01 [file] [log] [blame]
Chris Lattner1c08c712005-01-07 07:47:53 +00001//===-- SelectionDAGISel.cpp - Implement the SelectionDAGISel class -------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner1c08c712005-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 Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner1c08c712005-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 Chenga9c20912006-01-21 02:32:06 +000016#include "llvm/CodeGen/ScheduleDAG.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000017#include "llvm/CallingConv.h"
Chris Lattner1c08c712005-01-07 07:47:53 +000018#include "llvm/Constants.h"
19#include "llvm/DerivedTypes.h"
20#include "llvm/Function.h"
Chris Lattner36ce6912005-11-29 06:21:05 +000021#include "llvm/GlobalVariable.h"
Chris Lattnerce7518c2006-01-26 22:24:51 +000022#include "llvm/InlineAsm.h"
Chris Lattner1c08c712005-01-07 07:47:53 +000023#include "llvm/Instructions.h"
24#include "llvm/Intrinsics.h"
Jim Laskey43970fe2006-03-23 18:06:46 +000025#include "llvm/IntrinsicInst.h"
Chris Lattnerb1a5a5c2005-11-16 07:22:30 +000026#include "llvm/CodeGen/IntrinsicLowering.h"
Jim Laskeyb2efb852006-01-04 22:28:25 +000027#include "llvm/CodeGen/MachineDebugInfo.h"
Chris Lattner1c08c712005-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 Lattnerfa577022005-09-13 19:30:54 +000033#include "llvm/Target/MRegisterInfo.h"
Chris Lattner1c08c712005-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 Lattner495a0b52005-08-17 06:37:43 +000039#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Chris Lattner7944d9d2005-01-12 03:41:21 +000040#include "llvm/Support/CommandLine.h"
Chris Lattner7c0104b2005-11-09 04:45:33 +000041#include "llvm/Support/MathExtras.h"
Chris Lattner1c08c712005-01-07 07:47:53 +000042#include "llvm/Support/Debug.h"
43#include <map>
Chris Lattner4e4b5762006-02-01 18:59:47 +000044#include <set>
Chris Lattner1c08c712005-01-07 07:47:53 +000045#include <iostream>
Jeff Cohen7e881032006-02-24 02:52:40 +000046#include <algorithm>
Chris Lattner1c08c712005-01-07 07:47:53 +000047using namespace llvm;
48
Chris Lattnerda8abb02005-09-01 18:44:10 +000049#ifndef NDEBUG
Chris Lattner7944d9d2005-01-12 03:41:21 +000050static cl::opt<bool>
Evan Chenga9c20912006-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 Lattner7944d9d2005-01-12 03:41:21 +000056#else
Evan Chenga9c20912006-01-21 02:32:06 +000057static const bool ViewISelDAGs = 0;
58static const bool ViewSchedDAGs = 0;
Chris Lattner7944d9d2005-01-12 03:41:21 +000059#endif
60
Chris Lattner20a49212006-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 Cheng4ef10862006-01-23 07:01:07 +000071namespace {
72 cl::opt<SchedHeuristics>
73 ISHeuristic(
74 "sched",
75 cl::desc("Choose scheduling style"),
Evan Cheng3f239522006-01-25 09:12:57 +000076 cl::init(defaultScheduling),
Evan Cheng4ef10862006-01-23 07:01:07 +000077 cl::values(
Evan Cheng3f239522006-01-25 09:12:57 +000078 clEnumValN(defaultScheduling, "default",
79 "Target preferred scheduling style"),
Evan Cheng4ef10862006-01-23 07:01:07 +000080 clEnumValN(noScheduling, "none",
Jim Laskey17d52f72006-01-23 13:34:04 +000081 "No scheduling: breadth first sequencing"),
Evan Cheng4ef10862006-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 Cheng3f239522006-01-25 09:12:57 +000088 clEnumValN(listSchedulingBURR, "list-burr",
Evan Chengf0f9c902006-01-23 08:26:10 +000089 "Bottom up register reduction list scheduling"),
Chris Lattner03fc53c2006-03-06 00:22:00 +000090 clEnumValN(listSchedulingTD, "list-td",
91 "Top-down list scheduler"),
Evan Cheng4ef10862006-01-23 07:01:07 +000092 clEnumValEnd));
93} // namespace
94
Chris Lattner864635a2006-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 Lattner9f6637d2006-02-23 20:06:57 +0000129 SDOperand &Chain, SDOperand &Flag) const;
Chris Lattnerc3a9f8d2006-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 Lattner9f6637d2006-02-23 20:06:57 +0000135 SDOperand &Chain, SDOperand &Flag) const;
Chris Lattnerc3a9f8d2006-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 Lattner9f6637d2006-02-23 20:06:57 +0000141 std::vector<SDOperand> &Ops) const;
Chris Lattner864635a2006-02-22 22:37:12 +0000142 };
143}
Evan Cheng4ef10862006-01-23 07:01:07 +0000144
Chris Lattner1c08c712005-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 Lattnerf26bc8e2005-01-08 19:52:31 +0000149 class FunctionLoweringInfo {
150 public:
Chris Lattner1c08c712005-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 Brukmanedf128a2005-04-21 22:36:52 +0000174
Chris Lattner3c384492006-03-16 19:51:18 +0000175 unsigned CreateRegForValue(const Value *V);
176
Chris Lattner1c08c712005-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 Lattnerbf209482005-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 Lattner1c08c712005-01-07 07:47:53 +0000206FunctionLoweringInfo::FunctionLoweringInfo(TargetLowering &tli,
Misha Brukmanedf128a2005-04-21 22:36:52 +0000207 Function &fn, MachineFunction &mf)
Chris Lattner1c08c712005-01-07 07:47:53 +0000208 : TLI(tli), Fn(fn), MF(mf), RegMap(MF.getSSARegMap()) {
209
Chris Lattnerbf209482005-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 Lattner1c08c712005-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 Cohen2aeaf4e2005-10-01 03:57:14 +0000220 Function::iterator BB = Fn.begin(), EB = Fn.end();
Chris Lattner1c08c712005-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 Begemanae232e72005-11-06 09:00:38 +0000226 unsigned Align =
227 std::max((unsigned)TLI.getTargetData().getTypeAlignment(Ty),
228 AI->getAlignment());
Chris Lattnera8217e32005-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 Lattner2dfa8192005-10-18 22:11:42 +0000237 TySize *= CUI->getValue(); // Get total allocated size.
Chris Lattnerd222f6a2005-10-18 22:14:06 +0000238 if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects.
Chris Lattner1c08c712005-01-07 07:47:53 +0000239 StaticAllocaMap[AI] =
Chris Lattnerf26bc8e2005-01-08 19:52:31 +0000240 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
Chris Lattner1c08c712005-01-07 07:47:53 +0000241 }
242
Jeff Cohen2aeaf4e2005-10-01 03:57:14 +0000243 for (; BB != EB; ++BB)
244 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
Chris Lattner1c08c712005-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 Cohen2aeaf4e2005-10-01 03:57:14 +0000253 for (BB = Fn.begin(), EB = Fn.end(); BB != EB; ++BB) {
Chris Lattner1c08c712005-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 Lattnerf44fd882005-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 Lattner1c08c712005-01-07 07:47:53 +0000271 }
272}
273
Chris Lattner3c384492006-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 Lattner6cb70042006-03-16 23:05:19 +0000297 if (NumElts == 1)
298 VT = EltTy;
299 else
300 VT = getVectorType(EltTy, NumElts);
Chris Lattner3c384492006-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 Lattner1c08c712005-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 Lattnerd3948112005-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 Lattner1c08c712005-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 Brukmanedf128a2005-04-21 22:36:52 +0000359 FunctionLoweringInfo &funcinfo)
Chris Lattner1c08c712005-01-07 07:47:53 +0000360 : TLI(tli), DAG(dag), TD(DAG.getTarget().getTargetData()),
361 FuncInfo(funcinfo) {
362 }
363
Chris Lattnera651cf62005-01-17 19:43:36 +0000364 /// getRoot - Return the current virtual root of the Selection DAG.
365 ///
366 SDOperand getRoot() {
Chris Lattnerd3948112005-01-17 22:19:26 +0000367 if (PendingLoads.empty())
368 return DAG.getRoot();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000369
Chris Lattnerd3948112005-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 Lattnera651cf62005-01-17 19:43:36 +0000382 }
383
Chris Lattner1c08c712005-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 Lattner28b5b1c2006-03-15 22:19:46 +0000399 SDOperand getLoadFrom(const Type *Ty, SDOperand Ptr,
400 SDOperand SrcValue, SDOperand Root,
401 bool isVolatile);
Chris Lattner1c08c712005-01-07 07:47:53 +0000402
403 SDOperand getIntPtrConstant(uint64_t Val) {
404 return DAG.getConstant(Val, TLI.getPointerTy());
405 }
406
Chris Lattner199862b2006-03-16 19:57:50 +0000407 SDOperand getValue(const Value *V);
Chris Lattner1c08c712005-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 Lattner4e4b5762006-02-01 18:59:47 +0000414
Chris Lattner864635a2006-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 Lattner1c08c712005-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 Begeman5fbb5d22005-11-19 00:36:38 +0000432 void visitBinary(User &I, unsigned IntOp, unsigned FPOp, unsigned VecOp);
Nate Begemane21ea612005-11-18 07:42:56 +0000433 void visitShift(User &I, unsigned Opcode);
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000434 void visitAdd(User &I) {
435 visitBinary(I, ISD::ADD, ISD::FADD, ISD::VADD);
Chris Lattner01b3d732005-09-28 22:28:18 +0000436 }
Chris Lattnerb9fccc42005-04-02 05:04:50 +0000437 void visitSub(User &I);
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000438 void visitMul(User &I) {
439 visitBinary(I, ISD::MUL, ISD::FMUL, ISD::VMUL);
Chris Lattner01b3d732005-09-28 22:28:18 +0000440 }
Chris Lattner1c08c712005-01-07 07:47:53 +0000441 void visitDiv(User &I) {
Chris Lattner01b3d732005-09-28 22:28:18 +0000442 const Type *Ty = I.getType();
Evan Cheng3e1ce5a2006-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 Lattner1c08c712005-01-07 07:47:53 +0000446 }
447 void visitRem(User &I) {
Chris Lattner01b3d732005-09-28 22:28:18 +0000448 const Type *Ty = I.getType();
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000449 visitBinary(I, Ty->isSigned() ? ISD::SREM : ISD::UREM, ISD::FREM, 0);
Chris Lattner1c08c712005-01-07 07:47:53 +0000450 }
Evan Cheng3e1ce5a2006-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 Begemane21ea612005-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 Lattner1c08c712005-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 Lattner384504c2006-03-21 20:44:12 +0000467 void visitExtractElement(ExtractElementInst &I);
Chris Lattnerc7029802006-03-18 01:44:44 +0000468 void visitInsertElement(InsertElementInst &I);
469
Chris Lattner1c08c712005-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 Lattnerce7518c2006-01-26 22:24:51 +0000482 void visitInlineAsm(CallInst &I);
Chris Lattnerc9ea6fd2005-11-09 19:44:01 +0000483 const char *visitIntrinsicCall(CallInst &I, unsigned Intrinsic);
Chris Lattner0eade312006-03-24 02:22:33 +0000484 void visitTargetIntrinsic(CallInst &I, unsigned Intrinsic);
Chris Lattner1c08c712005-01-07 07:47:53 +0000485
Chris Lattner1c08c712005-01-07 07:47:53 +0000486 void visitVAStart(CallInst &I);
Chris Lattner1c08c712005-01-07 07:47:53 +0000487 void visitVAArg(VAArgInst &I);
488 void visitVAEnd(CallInst &I);
489 void visitVACopy(CallInst &I);
Chris Lattner39ae3622005-01-09 00:00:49 +0000490 void visitFrameReturnAddress(CallInst &I, bool isFrameAddress);
Chris Lattner1c08c712005-01-07 07:47:53 +0000491
Chris Lattner7041ee32005-01-11 05:56:49 +0000492 void visitMemIntrinsic(CallInst &I, unsigned Op);
Chris Lattner1c08c712005-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 Lattner199862b2006-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 Lattner23d564c2006-03-19 00:20:20 +0000521 if (!isa<PackedType>(VTy))
522 return N = DAG.getNode(ISD::UNDEF, VT);
523
Chris Lattnerb2827b02006-03-19 00:52:58 +0000524 // Create a VBUILD_VECTOR of undef nodes.
Chris Lattner23d564c2006-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 Lattnerb2827b02006-03-19 00:52:58 +0000535 return N = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, Ops);
Chris Lattner199862b2006-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 Lattner199862b2006-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 Lattnerb2827b02006-03-19 00:52:58 +0000569 // Create a VBUILD_VECTOR node with generic Vector type.
Chris Lattner23d564c2006-03-19 00:20:20 +0000570 Ops.push_back(DAG.getConstant(NumElements, MVT::i32));
571 Ops.push_back(DAG.getValueType(PVT));
Chris Lattnerb2827b02006-03-19 00:52:58 +0000572 return N = DAG.getNode(ISD::VBUILD_VECTOR, MVT::Vector, Ops);
Chris Lattner199862b2006-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;
602 }
603
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 Lattner1c08c712005-01-07 07:47:53 +0000625void SelectionDAGLowering::visitRet(ReturnInst &I) {
626 if (I.getNumOperands() == 0) {
Chris Lattnera651cf62005-01-17 19:43:36 +0000627 DAG.setRoot(DAG.getNode(ISD::RET, MVT::Other, getRoot()));
Chris Lattner1c08c712005-01-07 07:47:53 +0000628 return;
629 }
Nate Begemanee625572006-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 Lattner1c08c712005-01-07 07:47:53 +0000645
Nate Begemanee625572006-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 Lattner1c08c712005-01-07 07:47:53 +0000652 }
Nate Begemanee625572006-01-27 21:09:22 +0000653 DAG.setRoot(DAG.getNode(ISD::RET, MVT::Other, NewValues));
Chris Lattner1c08c712005-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 Lattner1c08c712005-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 Lattnera651cf62005-01-17 19:43:36 +0000669 DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, getRoot(),
Misha Brukmandedf2bd2005-04-22 04:01:18 +0000670 DAG.getBasicBlock(Succ0MBB)));
Chris Lattner1c08c712005-01-07 07:47:53 +0000671 } else {
672 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
Chris Lattner1c08c712005-01-07 07:47:53 +0000673
674 SDOperand Cond = getValue(I.getCondition());
Chris Lattner1c08c712005-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 Lattnera651cf62005-01-17 19:43:36 +0000678 DAG.setRoot(DAG.getNode(ISD::BRCOND, MVT::Other, getRoot(),
Misha Brukmandedf2bd2005-04-22 04:01:18 +0000679 Cond, DAG.getBasicBlock(Succ0MBB)));
Chris Lattner1c08c712005-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 Lattnera651cf62005-01-17 19:43:36 +0000685 DAG.setRoot(DAG.getNode(ISD::BRCOND, MVT::Other, getRoot(),
Misha Brukmandedf2bd2005-04-22 04:01:18 +0000686 Cond, DAG.getBasicBlock(Succ1MBB)));
Chris Lattner1c08c712005-01-07 07:47:53 +0000687 } else {
Chris Lattnere7ccd4a2005-04-09 03:30:29 +0000688 std::vector<SDOperand> Ops;
689 Ops.push_back(getRoot());
Evan Cheng298ebf22006-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 Begeman81e80972006-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 Lattner1c08c712005-01-07 07:47:53 +0000703 }
704 }
705}
706
Chris Lattnerb9fccc42005-04-02 05:04:50 +0000707void SelectionDAGLowering::visitSub(User &I) {
708 // -0.0 - X --> fneg
Chris Lattner01b3d732005-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 Lattner01b3d732005-09-28 22:28:18 +0000716 }
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000717 visitBinary(I, ISD::SUB, ISD::FSUB, ISD::VSUB);
Chris Lattnerb9fccc42005-04-02 05:04:50 +0000718}
719
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000720void SelectionDAGLowering::visitBinary(User &I, unsigned IntOp, unsigned FPOp,
721 unsigned VecOp) {
722 const Type *Ty = I.getType();
Chris Lattner1c08c712005-01-07 07:47:53 +0000723 SDOperand Op1 = getValue(I.getOperand(0));
724 SDOperand Op2 = getValue(I.getOperand(1));
Chris Lattner2c49f272005-01-19 22:31:21 +0000725
Chris Lattnerb67eb912005-11-19 18:40:42 +0000726 if (Ty->isIntegral()) {
Nate Begeman5fbb5d22005-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 Lattnerc7029802006-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 Begeman5fbb5d22005-11-19 00:36:38 +0000735 }
Nate Begemane21ea612005-11-18 07:42:56 +0000736}
Chris Lattner2c49f272005-01-19 22:31:21 +0000737
Nate Begemane21ea612005-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 Lattner1c08c712005-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 Lattner7cf7e3f2005-08-09 20:20:18 +0000754 setValue(&I, DAG.getSetCC(MVT::i1, Op1, Op2, Opcode));
Chris Lattner1c08c712005-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 Lattnere25ca692006-03-22 20:09:35 +0000767 MVT::ValueType SrcVT = N.getValueType();
Chris Lattner28b5b1c2006-03-15 22:19:46 +0000768 MVT::ValueType DestVT = TLI.getValueType(I.getType());
Chris Lattner1c08c712005-01-07 07:47:53 +0000769
Chris Lattnere25ca692006-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 Lattner1c08c712005-01-07 07:47:53 +0000779 setValue(&I, N); // noop cast.
Chris Lattner28b5b1c2006-03-15 22:19:46 +0000780 } else if (DestVT == MVT::i1) {
Chris Lattneref311aa2005-05-09 22:17:13 +0000781 // Cast to bool is a comparison against zero, not truncation to zero.
Chris Lattner28b5b1c2006-03-15 22:19:46 +0000782 SDOperand Zero = isInteger(SrcVT) ? DAG.getConstant(0, N.getValueType()) :
Chris Lattneref311aa2005-05-09 22:17:13 +0000783 DAG.getConstantFP(0.0, N.getValueType());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000784 setValue(&I, DAG.getSetCC(MVT::i1, N, Zero, ISD::SETNE));
Chris Lattner28b5b1c2006-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 Lattnerae0aacb2005-01-08 08:08:56 +0000789 else if (I.getOperand(0)->getType()->isSigned())
Chris Lattner28b5b1c2006-03-15 22:19:46 +0000790 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, DestVT, N));
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000791 else
Chris Lattner28b5b1c2006-03-15 22:19:46 +0000792 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, DestVT, N));
Chris Lattner7e358902006-03-22 22:20:49 +0000793 } else if (isFloatingPoint(DestVT)) { // Int -> FP cast
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000794 if (I.getOperand(0)->getType()->isSigned())
Chris Lattner28b5b1c2006-03-15 22:19:46 +0000795 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, DestVT, N));
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000796 else
Chris Lattner28b5b1c2006-03-15 22:19:46 +0000797 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, DestVT, N));
Chris Lattnere25ca692006-03-22 20:09:35 +0000798 } else {
799 assert(0 && "Unknown cast!");
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000800 }
Chris Lattner28b5b1c2006-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 Lattnerae0aacb2005-01-08 08:08:56 +0000805 else
Chris Lattner28b5b1c2006-03-15 22:19:46 +0000806 setValue(&I, DAG.getNode(ISD::FP_EXTEND, DestVT, N));
Chris Lattnere25ca692006-03-22 20:09:35 +0000807 } else if (isInteger(DestVT)) { // FP -> Int cast.
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000808 if (I.getType()->isSigned())
Chris Lattner28b5b1c2006-03-15 22:19:46 +0000809 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, DestVT, N));
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000810 else
Chris Lattner28b5b1c2006-03-15 22:19:46 +0000811 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, DestVT, N));
Chris Lattnere25ca692006-03-22 20:09:35 +0000812 } else {
813 assert(0 && "Unknown cast!");
Chris Lattner28b5b1c2006-03-15 22:19:46 +0000814 }
815 } else {
Chris Lattnere25ca692006-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 Lattner1c08c712005-01-07 07:47:53 +0000821 }
822}
823
Chris Lattnerc7029802006-03-18 01:44:44 +0000824void SelectionDAGLowering::visitInsertElement(InsertElementInst &I) {
Chris Lattnerc7029802006-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 Lattner2332b9f2006-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 Lattnerc7029802006-03-18 01:44:44 +0000834}
835
Chris Lattner384504c2006-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 Lattnerc7029802006-03-18 01:44:44 +0000844
Chris Lattner1c08c712005-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 Lattnerc88d8e92005-12-05 07:10:48 +0000853 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
Chris Lattner1c08c712005-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 Brukmandedf2bd2005-04-22 04:01:18 +0000859 getIntPtrConstant(Offset));
Chris Lattner1c08c712005-01-07 07:47:53 +0000860 }
861 Ty = StTy->getElementType(Field);
862 } else {
863 Ty = cast<SequentialType>(Ty)->getElementType();
Chris Lattner7cc47772005-01-07 21:56:57 +0000864
Chris Lattner7c0104b2005-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 Lattner7cc47772005-01-07 21:56:57 +0000868
Chris Lattner7c0104b2005-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 Lattner1c08c712005-01-07 07:47:53 +0000876 }
Chris Lattner7c0104b2005-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 Lattner6b2d6962005-11-09 16:50:40 +0000897 DAG.getConstant(Amt, TLI.getShiftAmountTy()));
Chris Lattner7c0104b2005-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 Lattner1c08c712005-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 Begemanae232e72005-11-06 09:00:38 +0000918 unsigned Align = std::max((unsigned)TLI.getTargetData().getTypeAlignment(Ty),
919 I.getAlignment());
Chris Lattner1c08c712005-01-07 07:47:53 +0000920
921 SDOperand AllocSize = getValue(I.getArraySize());
Chris Lattner68cd65e2005-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 Lattner1c08c712005-01-07 07:47:53 +0000927
Chris Lattner68cd65e2005-01-22 23:04:37 +0000928 AllocSize = DAG.getNode(ISD::MUL, IntPtr, AllocSize,
Chris Lattner1c08c712005-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 Lattneradf6c2a2005-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 Lattner1c08c712005-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 Lattner1c08c712005-01-07 07:47:53 +0000962void SelectionDAGLowering::visitLoad(LoadInst &I) {
963 SDOperand Ptr = getValue(I.getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +0000964
Chris Lattnerd3948112005-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 Lattner28b5b1c2006-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 Begeman5fbb5d22005-11-19 00:36:38 +0000980 SDOperand L;
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000981 if (const PackedType *PTy = dyn_cast<PackedType>(Ty)) {
Nate Begeman4ef3b812005-11-22 01:29:36 +0000982 MVT::ValueType PVT = TLI.getValueType(PTy->getElementType());
Chris Lattnerc7029802006-03-18 01:44:44 +0000983 L = DAG.getVecLoad(PTy->getNumElements(), PVT, Root, Ptr, SrcValue);
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000984 } else {
Chris Lattner28b5b1c2006-03-15 22:19:46 +0000985 L = DAG.getLoad(TLI.getValueType(Ty), Root, Ptr, SrcValue);
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000986 }
Chris Lattnerd3948112005-01-17 22:19:26 +0000987
Chris Lattner28b5b1c2006-03-15 22:19:46 +0000988 if (isVolatile)
Chris Lattnerd3948112005-01-17 22:19:26 +0000989 DAG.setRoot(L.getValue(1));
990 else
991 PendingLoads.push_back(L.getValue(1));
Chris Lattner28b5b1c2006-03-15 22:19:46 +0000992
993 return L;
Chris Lattner1c08c712005-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 Lattner369e6db2005-05-09 04:08:33 +00001001 DAG.setRoot(DAG.getNode(ISD::STORE, MVT::Other, getRoot(), Src, Ptr,
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001002 DAG.getSrcValue(I.getOperand(1))));
Chris Lattner1c08c712005-01-07 07:47:53 +00001003}
1004
Chris Lattner0eade312006-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) {
1018 bool HasChain = IntrinsicCannotAccessMemory(Intrinsic);
1019
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 Lattnerc9ea6fd2005-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 Lattner0eade312006-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 Lattnerc9ea6fd2005-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 Lattner03dd4652006-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 Lattnerc9ea6fd2005-11-09 19:44:01 +00001113
Chris Lattner86cb6432005-12-13 17:40:33 +00001114 case Intrinsic::dbg_stoppoint: {
Jim Laskeyce72b172006-02-11 01:01:30 +00001115 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
Jim Laskey43970fe2006-03-23 18:06:46 +00001116 DbgStopPointInst &SPI = cast<DbgStopPointInst>(I);
1117 if (DebugInfo && DebugInfo->Verify(SPI.getContext())) {
Jim Laskeyce72b172006-02-11 01:01:30 +00001118 std::vector<SDOperand> Ops;
Chris Lattner36ce6912005-11-29 06:21:05 +00001119
Jim Laskeyce72b172006-02-11 01:01:30 +00001120 Ops.push_back(getRoot());
Jim Laskey43970fe2006-03-23 18:06:46 +00001121 Ops.push_back(getValue(SPI.getLineValue()));
1122 Ops.push_back(getValue(SPI.getColumnValue()));
Chris Lattner36ce6912005-11-29 06:21:05 +00001123
Jim Laskey43970fe2006-03-23 18:06:46 +00001124 DebugInfoDesc *DD = DebugInfo->getDescFor(SPI.getContext());
Jim Laskeyce72b172006-02-11 01:01:30 +00001125 assert(DD && "Not a debug information descriptor");
Jim Laskey43970fe2006-03-23 18:06:46 +00001126 CompileUnitDesc *CompileUnit = cast<CompileUnitDesc>(DD);
1127
Jim Laskeyce72b172006-02-11 01:01:30 +00001128 Ops.push_back(DAG.getString(CompileUnit->getFileName()));
1129 Ops.push_back(DAG.getString(CompileUnit->getDirectory()));
1130
Jim Laskey43970fe2006-03-23 18:06:46 +00001131 DAG.setRoot(DAG.getNode(ISD::LOCATION, MVT::Other, Ops));
Chris Lattner86cb6432005-12-13 17:40:33 +00001132 }
Jim Laskey43970fe2006-03-23 18:06:46 +00001133
Chris Lattnerb1a5a5c2005-11-16 07:22:30 +00001134 return 0;
Chris Lattner36ce6912005-11-29 06:21:05 +00001135 }
Jim Laskey43970fe2006-03-23 18:06:46 +00001136 case Intrinsic::dbg_region_start: {
1137 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
1138 DbgRegionStartInst &RSI = cast<DbgRegionStartInst>(I);
1139 if (DebugInfo && DebugInfo->Verify(RSI.getContext())) {
1140 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 Lattnerb1a5a5c2005-11-16 07:22:30 +00001150 return 0;
Jim Laskey43970fe2006-03-23 18:06:46 +00001151 }
1152 case Intrinsic::dbg_region_end: {
1153 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
1154 DbgRegionEndInst &REI = cast<DbgRegionEndInst>(I);
1155 if (DebugInfo && DebugInfo->Verify(REI.getContext())) {
1156 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 Lattnerb1a5a5c2005-11-16 07:22:30 +00001166 return 0;
Jim Laskey43970fe2006-03-23 18:06:46 +00001167 }
1168 case Intrinsic::dbg_func_start: {
1169 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
1170 DbgFuncStartInst &FSI = cast<DbgFuncStartInst>(I);
1171 if (DebugInfo && DebugInfo->Verify(FSI.getSubprogram())) {
1172 std::vector<SDOperand> Ops;
1173
1174 unsigned LabelID = DebugInfo->RecordRegionStart(FSI.getSubprogram());
1175
1176 Ops.push_back(getRoot());
1177 Ops.push_back(DAG.getConstant(LabelID, MVT::i32));
1178
1179 DAG.setRoot(DAG.getNode(ISD::DEBUG_LABEL, MVT::Other, Ops));
1180 }
1181
Chris Lattnerb1a5a5c2005-11-16 07:22:30 +00001182 return 0;
Jim Laskey43970fe2006-03-23 18:06:46 +00001183 }
1184 case Intrinsic::dbg_declare: {
1185 MachineDebugInfo *DebugInfo = DAG.getMachineDebugInfo();
1186 DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
1187 if (DebugInfo && DebugInfo->Verify(DI.getVariable())) {
1188 std::vector<SDOperand> Ops;
1189
1190 SDOperand AllocaOp = getValue(I.getOperand(1));
1191 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(AllocaOp)) {
1192 DebugInfo->RecordVariable(DI.getVariable(), FI->getIndex());
1193 }
1194 }
1195
1196 return 0;
1197 }
Chris Lattnerc9ea6fd2005-11-09 19:44:01 +00001198
Reid Spencer0b118202006-01-16 21:12:35 +00001199 case Intrinsic::isunordered_f32:
1200 case Intrinsic::isunordered_f64:
Chris Lattnerc9ea6fd2005-11-09 19:44:01 +00001201 setValue(&I, DAG.getSetCC(MVT::i1,getValue(I.getOperand(1)),
1202 getValue(I.getOperand(2)), ISD::SETUO));
1203 return 0;
1204
Reid Spencer0b118202006-01-16 21:12:35 +00001205 case Intrinsic::sqrt_f32:
1206 case Intrinsic::sqrt_f64:
Chris Lattnerc9ea6fd2005-11-09 19:44:01 +00001207 setValue(&I, DAG.getNode(ISD::FSQRT,
1208 getValue(I.getOperand(1)).getValueType(),
1209 getValue(I.getOperand(1))));
1210 return 0;
1211 case Intrinsic::pcmarker: {
1212 SDOperand Tmp = getValue(I.getOperand(1));
1213 DAG.setRoot(DAG.getNode(ISD::PCMARKER, MVT::Other, getRoot(), Tmp));
1214 return 0;
1215 }
Andrew Lenharth8b91c772005-11-11 22:48:54 +00001216 case Intrinsic::readcyclecounter: {
1217 std::vector<MVT::ValueType> VTs;
1218 VTs.push_back(MVT::i64);
1219 VTs.push_back(MVT::Other);
1220 std::vector<SDOperand> Ops;
1221 Ops.push_back(getRoot());
1222 SDOperand Tmp = DAG.getNode(ISD::READCYCLECOUNTER, VTs, Ops);
1223 setValue(&I, Tmp);
1224 DAG.setRoot(Tmp.getValue(1));
Andrew Lenharth51b8d542005-11-11 16:47:30 +00001225 return 0;
Andrew Lenharth8b91c772005-11-11 22:48:54 +00001226 }
Nate Begemand88fc032006-01-14 03:14:10 +00001227 case Intrinsic::bswap_i16:
Nate Begemand88fc032006-01-14 03:14:10 +00001228 case Intrinsic::bswap_i32:
Nate Begemand88fc032006-01-14 03:14:10 +00001229 case Intrinsic::bswap_i64:
1230 setValue(&I, DAG.getNode(ISD::BSWAP,
1231 getValue(I.getOperand(1)).getValueType(),
1232 getValue(I.getOperand(1))));
1233 return 0;
Reid Spencer0b118202006-01-16 21:12:35 +00001234 case Intrinsic::cttz_i8:
1235 case Intrinsic::cttz_i16:
1236 case Intrinsic::cttz_i32:
1237 case Intrinsic::cttz_i64:
Chris Lattnerc9ea6fd2005-11-09 19:44:01 +00001238 setValue(&I, DAG.getNode(ISD::CTTZ,
1239 getValue(I.getOperand(1)).getValueType(),
1240 getValue(I.getOperand(1))));
1241 return 0;
Reid Spencer0b118202006-01-16 21:12:35 +00001242 case Intrinsic::ctlz_i8:
1243 case Intrinsic::ctlz_i16:
1244 case Intrinsic::ctlz_i32:
1245 case Intrinsic::ctlz_i64:
Chris Lattnerc9ea6fd2005-11-09 19:44:01 +00001246 setValue(&I, DAG.getNode(ISD::CTLZ,
1247 getValue(I.getOperand(1)).getValueType(),
1248 getValue(I.getOperand(1))));
1249 return 0;
Reid Spencer0b118202006-01-16 21:12:35 +00001250 case Intrinsic::ctpop_i8:
1251 case Intrinsic::ctpop_i16:
1252 case Intrinsic::ctpop_i32:
1253 case Intrinsic::ctpop_i64:
Chris Lattnerc9ea6fd2005-11-09 19:44:01 +00001254 setValue(&I, DAG.getNode(ISD::CTPOP,
1255 getValue(I.getOperand(1)).getValueType(),
1256 getValue(I.getOperand(1))));
1257 return 0;
Chris Lattner140d53c2006-01-13 02:50:02 +00001258 case Intrinsic::stacksave: {
1259 std::vector<MVT::ValueType> VTs;
1260 VTs.push_back(TLI.getPointerTy());
1261 VTs.push_back(MVT::Other);
1262 std::vector<SDOperand> Ops;
1263 Ops.push_back(getRoot());
1264 SDOperand Tmp = DAG.getNode(ISD::STACKSAVE, VTs, Ops);
1265 setValue(&I, Tmp);
1266 DAG.setRoot(Tmp.getValue(1));
1267 return 0;
1268 }
Chris Lattner39a17dd2006-01-23 05:22:07 +00001269 case Intrinsic::stackrestore: {
1270 SDOperand Tmp = getValue(I.getOperand(1));
1271 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, MVT::Other, getRoot(), Tmp));
Chris Lattner140d53c2006-01-13 02:50:02 +00001272 return 0;
Chris Lattner39a17dd2006-01-23 05:22:07 +00001273 }
Chris Lattnerac22c832005-12-12 22:51:16 +00001274 case Intrinsic::prefetch:
1275 // FIXME: Currently discarding prefetches.
1276 return 0;
Chris Lattnerc9ea6fd2005-11-09 19:44:01 +00001277 }
1278}
1279
1280
Chris Lattner1c08c712005-01-07 07:47:53 +00001281void SelectionDAGLowering::visitCall(CallInst &I) {
Chris Lattner64e14b12005-01-08 22:48:57 +00001282 const char *RenameFn = 0;
Chris Lattnerc9ea6fd2005-11-09 19:44:01 +00001283 if (Function *F = I.getCalledFunction()) {
Chris Lattnerc0f18152005-04-02 05:26:53 +00001284 if (F->isExternal())
Chris Lattnerc9ea6fd2005-11-09 19:44:01 +00001285 if (unsigned IID = F->getIntrinsicID()) {
1286 RenameFn = visitIntrinsicCall(I, IID);
1287 if (!RenameFn)
1288 return;
1289 } else { // Not an LLVM intrinsic.
1290 const std::string &Name = F->getName();
Chris Lattnera09f8482006-03-05 05:09:38 +00001291 if (Name[0] == 'c' && (Name == "copysign" || Name == "copysignf")) {
1292 if (I.getNumOperands() == 3 && // Basic sanity checks.
1293 I.getOperand(1)->getType()->isFloatingPoint() &&
1294 I.getType() == I.getOperand(1)->getType() &&
1295 I.getType() == I.getOperand(2)->getType()) {
1296 SDOperand LHS = getValue(I.getOperand(1));
1297 SDOperand RHS = getValue(I.getOperand(2));
1298 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, LHS.getValueType(),
1299 LHS, RHS));
1300 return;
1301 }
1302 } else if (Name[0] == 'f' && (Name == "fabs" || Name == "fabsf")) {
Chris Lattnerc0f18152005-04-02 05:26:53 +00001303 if (I.getNumOperands() == 2 && // Basic sanity checks.
1304 I.getOperand(1)->getType()->isFloatingPoint() &&
1305 I.getType() == I.getOperand(1)->getType()) {
Chris Lattnerc9ea6fd2005-11-09 19:44:01 +00001306 SDOperand Tmp = getValue(I.getOperand(1));
Chris Lattnerc0f18152005-04-02 05:26:53 +00001307 setValue(&I, DAG.getNode(ISD::FABS, Tmp.getValueType(), Tmp));
1308 return;
1309 }
Chris Lattnerc9ea6fd2005-11-09 19:44:01 +00001310 } else if (Name[0] == 's' && (Name == "sin" || Name == "sinf")) {
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001311 if (I.getNumOperands() == 2 && // Basic sanity checks.
1312 I.getOperand(1)->getType()->isFloatingPoint() &&
Chris Lattner06a248c92006-02-14 05:39:35 +00001313 I.getType() == I.getOperand(1)->getType()) {
Chris Lattnerc9ea6fd2005-11-09 19:44:01 +00001314 SDOperand Tmp = getValue(I.getOperand(1));
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001315 setValue(&I, DAG.getNode(ISD::FSIN, Tmp.getValueType(), Tmp));
1316 return;
1317 }
Chris Lattnerc9ea6fd2005-11-09 19:44:01 +00001318 } else if (Name[0] == 'c' && (Name == "cos" || Name == "cosf")) {
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001319 if (I.getNumOperands() == 2 && // Basic sanity checks.
1320 I.getOperand(1)->getType()->isFloatingPoint() &&
Chris Lattner06a248c92006-02-14 05:39:35 +00001321 I.getType() == I.getOperand(1)->getType()) {
Chris Lattnerc9ea6fd2005-11-09 19:44:01 +00001322 SDOperand Tmp = getValue(I.getOperand(1));
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001323 setValue(&I, DAG.getNode(ISD::FCOS, Tmp.getValueType(), Tmp));
1324 return;
1325 }
1326 }
Chris Lattner1ca85d52005-05-14 13:56:55 +00001327 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001328 } else if (isa<InlineAsm>(I.getOperand(0))) {
1329 visitInlineAsm(I);
1330 return;
Chris Lattnerc9ea6fd2005-11-09 19:44:01 +00001331 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001332
Chris Lattner64e14b12005-01-08 22:48:57 +00001333 SDOperand Callee;
1334 if (!RenameFn)
1335 Callee = getValue(I.getOperand(0));
1336 else
1337 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Chris Lattner1c08c712005-01-07 07:47:53 +00001338 std::vector<std::pair<SDOperand, const Type*> > Args;
Chris Lattnerc9ea6fd2005-11-09 19:44:01 +00001339 Args.reserve(I.getNumOperands());
Chris Lattner1c08c712005-01-07 07:47:53 +00001340 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
1341 Value *Arg = I.getOperand(i);
1342 SDOperand ArgNode = getValue(Arg);
1343 Args.push_back(std::make_pair(ArgNode, Arg->getType()));
1344 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001345
Nate Begeman8e21e712005-03-26 01:29:23 +00001346 const PointerType *PT = cast<PointerType>(I.getCalledValue()->getType());
1347 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Misha Brukmanedf128a2005-04-21 22:36:52 +00001348
Chris Lattnercf5734d2005-01-08 19:26:18 +00001349 std::pair<SDOperand,SDOperand> Result =
Chris Lattner9092fa32005-05-12 19:56:57 +00001350 TLI.LowerCallTo(getRoot(), I.getType(), FTy->isVarArg(), I.getCallingConv(),
Chris Lattneradf6a962005-05-13 18:50:42 +00001351 I.isTailCall(), Callee, Args, DAG);
Chris Lattner1c08c712005-01-07 07:47:53 +00001352 if (I.getType() != Type::VoidTy)
Chris Lattnercf5734d2005-01-08 19:26:18 +00001353 setValue(&I, Result.first);
1354 DAG.setRoot(Result.second);
Chris Lattner1c08c712005-01-07 07:47:53 +00001355}
1356
Chris Lattner864635a2006-02-22 22:37:12 +00001357SDOperand RegsForValue::getCopyFromRegs(SelectionDAG &DAG,
Chris Lattner9f6637d2006-02-23 20:06:57 +00001358 SDOperand &Chain, SDOperand &Flag)const{
Chris Lattner864635a2006-02-22 22:37:12 +00001359 SDOperand Val = DAG.getCopyFromReg(Chain, Regs[0], RegVT, Flag);
1360 Chain = Val.getValue(1);
1361 Flag = Val.getValue(2);
1362
1363 // If the result was expanded, copy from the top part.
1364 if (Regs.size() > 1) {
1365 assert(Regs.size() == 2 &&
1366 "Cannot expand to more than 2 elts yet!");
1367 SDOperand Hi = DAG.getCopyFromReg(Chain, Regs[1], RegVT, Flag);
1368 Chain = Val.getValue(1);
1369 Flag = Val.getValue(2);
Chris Lattner9f6637d2006-02-23 20:06:57 +00001370 if (DAG.getTargetLoweringInfo().isLittleEndian())
1371 return DAG.getNode(ISD::BUILD_PAIR, ValueVT, Val, Hi);
1372 else
1373 return DAG.getNode(ISD::BUILD_PAIR, ValueVT, Hi, Val);
Chris Lattner864635a2006-02-22 22:37:12 +00001374 }
Chris Lattner4e4b5762006-02-01 18:59:47 +00001375
Chris Lattner864635a2006-02-22 22:37:12 +00001376 // Otherwise, if the return value was promoted, truncate it to the
1377 // appropriate type.
1378 if (RegVT == ValueVT)
1379 return Val;
1380
1381 if (MVT::isInteger(RegVT))
1382 return DAG.getNode(ISD::TRUNCATE, ValueVT, Val);
1383 else
1384 return DAG.getNode(ISD::FP_ROUND, ValueVT, Val);
1385}
1386
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001387/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
1388/// specified value into the registers specified by this object. This uses
1389/// Chain/Flag as the input and updates them for the output Chain/Flag.
1390void RegsForValue::getCopyToRegs(SDOperand Val, SelectionDAG &DAG,
Chris Lattner9f6637d2006-02-23 20:06:57 +00001391 SDOperand &Chain, SDOperand &Flag) const {
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001392 if (Regs.size() == 1) {
1393 // If there is a single register and the types differ, this must be
1394 // a promotion.
1395 if (RegVT != ValueVT) {
1396 if (MVT::isInteger(RegVT))
1397 Val = DAG.getNode(ISD::ANY_EXTEND, RegVT, Val);
1398 else
1399 Val = DAG.getNode(ISD::FP_EXTEND, RegVT, Val);
1400 }
1401 Chain = DAG.getCopyToReg(Chain, Regs[0], Val, Flag);
1402 Flag = Chain.getValue(1);
1403 } else {
Chris Lattner9f6637d2006-02-23 20:06:57 +00001404 std::vector<unsigned> R(Regs);
1405 if (!DAG.getTargetLoweringInfo().isLittleEndian())
1406 std::reverse(R.begin(), R.end());
1407
1408 for (unsigned i = 0, e = R.size(); i != e; ++i) {
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001409 SDOperand Part = DAG.getNode(ISD::EXTRACT_ELEMENT, RegVT, Val,
1410 DAG.getConstant(i, MVT::i32));
Chris Lattner9f6637d2006-02-23 20:06:57 +00001411 Chain = DAG.getCopyToReg(Chain, R[i], Part, Flag);
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001412 Flag = Chain.getValue(1);
1413 }
1414 }
1415}
Chris Lattner864635a2006-02-22 22:37:12 +00001416
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001417/// AddInlineAsmOperands - Add this value to the specified inlineasm node
1418/// operand list. This adds the code marker and includes the number of
1419/// values added into it.
1420void RegsForValue::AddInlineAsmOperands(unsigned Code, SelectionDAG &DAG,
Chris Lattner9f6637d2006-02-23 20:06:57 +00001421 std::vector<SDOperand> &Ops) const {
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001422 Ops.push_back(DAG.getConstant(Code | (Regs.size() << 3), MVT::i32));
1423 for (unsigned i = 0, e = Regs.size(); i != e; ++i)
1424 Ops.push_back(DAG.getRegister(Regs[i], RegVT));
1425}
Chris Lattner864635a2006-02-22 22:37:12 +00001426
1427/// isAllocatableRegister - If the specified register is safe to allocate,
1428/// i.e. it isn't a stack pointer or some other special register, return the
1429/// register class for the register. Otherwise, return null.
1430static const TargetRegisterClass *
Chris Lattner9b6fb5d2006-02-22 23:09:03 +00001431isAllocatableRegister(unsigned Reg, MachineFunction &MF,
1432 const TargetLowering &TLI, const MRegisterInfo *MRI) {
1433 for (MRegisterInfo::regclass_iterator RCI = MRI->regclass_begin(),
1434 E = MRI->regclass_end(); RCI != E; ++RCI) {
1435 const TargetRegisterClass *RC = *RCI;
1436 // If none of the the value types for this register class are valid, we
1437 // can't use it. For example, 64-bit reg classes on 32-bit targets.
1438 bool isLegal = false;
1439 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
1440 I != E; ++I) {
1441 if (TLI.isTypeLegal(*I)) {
1442 isLegal = true;
1443 break;
1444 }
1445 }
1446
1447 if (!isLegal) continue;
1448
Chris Lattner864635a2006-02-22 22:37:12 +00001449 // NOTE: This isn't ideal. In particular, this might allocate the
1450 // frame pointer in functions that need it (due to them not being taken
1451 // out of allocation, because a variable sized allocation hasn't been seen
1452 // yet). This is a slight code pessimization, but should still work.
Chris Lattner9b6fb5d2006-02-22 23:09:03 +00001453 for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
1454 E = RC->allocation_order_end(MF); I != E; ++I)
Chris Lattner864635a2006-02-22 22:37:12 +00001455 if (*I == Reg)
Chris Lattner9b6fb5d2006-02-22 23:09:03 +00001456 return RC;
Chris Lattner4e4b5762006-02-01 18:59:47 +00001457 }
1458 return 0;
Chris Lattner864635a2006-02-22 22:37:12 +00001459}
1460
1461RegsForValue SelectionDAGLowering::
1462GetRegistersForValue(const std::string &ConstrCode,
1463 MVT::ValueType VT, bool isOutReg, bool isInReg,
1464 std::set<unsigned> &OutputRegs,
1465 std::set<unsigned> &InputRegs) {
1466 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
1467 TLI.getRegForInlineAsmConstraint(ConstrCode, VT);
1468 std::vector<unsigned> Regs;
1469
1470 unsigned NumRegs = VT != MVT::Other ? TLI.getNumElements(VT) : 1;
1471 MVT::ValueType RegVT;
1472 MVT::ValueType ValueVT = VT;
1473
1474 if (PhysReg.first) {
1475 if (VT == MVT::Other)
1476 ValueVT = *PhysReg.second->vt_begin();
1477 RegVT = VT;
1478
1479 // This is a explicit reference to a physical register.
1480 Regs.push_back(PhysReg.first);
1481
1482 // If this is an expanded reference, add the rest of the regs to Regs.
1483 if (NumRegs != 1) {
1484 RegVT = *PhysReg.second->vt_begin();
1485 TargetRegisterClass::iterator I = PhysReg.second->begin();
1486 TargetRegisterClass::iterator E = PhysReg.second->end();
1487 for (; *I != PhysReg.first; ++I)
1488 assert(I != E && "Didn't find reg!");
1489
1490 // Already added the first reg.
1491 --NumRegs; ++I;
1492 for (; NumRegs; --NumRegs, ++I) {
1493 assert(I != E && "Ran out of registers to allocate!");
1494 Regs.push_back(*I);
1495 }
1496 }
1497 return RegsForValue(Regs, RegVT, ValueVT);
1498 }
1499
1500 // This is a reference to a register class. Allocate NumRegs consecutive,
1501 // available, registers from the class.
1502 std::vector<unsigned> RegClassRegs =
1503 TLI.getRegClassForInlineAsmConstraint(ConstrCode, VT);
1504
1505 const MRegisterInfo *MRI = DAG.getTarget().getRegisterInfo();
1506 MachineFunction &MF = *CurMBB->getParent();
1507 unsigned NumAllocated = 0;
1508 for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
1509 unsigned Reg = RegClassRegs[i];
1510 // See if this register is available.
1511 if ((isOutReg && OutputRegs.count(Reg)) || // Already used.
1512 (isInReg && InputRegs.count(Reg))) { // Already used.
1513 // Make sure we find consecutive registers.
1514 NumAllocated = 0;
1515 continue;
1516 }
1517
1518 // Check to see if this register is allocatable (i.e. don't give out the
1519 // stack pointer).
Chris Lattner9b6fb5d2006-02-22 23:09:03 +00001520 const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, MRI);
Chris Lattner864635a2006-02-22 22:37:12 +00001521 if (!RC) {
1522 // Make sure we find consecutive registers.
1523 NumAllocated = 0;
1524 continue;
1525 }
1526
1527 // Okay, this register is good, we can use it.
1528 ++NumAllocated;
1529
1530 // If we allocated enough consecutive
1531 if (NumAllocated == NumRegs) {
1532 unsigned RegStart = (i-NumAllocated)+1;
1533 unsigned RegEnd = i+1;
1534 // Mark all of the allocated registers used.
1535 for (unsigned i = RegStart; i != RegEnd; ++i) {
1536 unsigned Reg = RegClassRegs[i];
1537 Regs.push_back(Reg);
1538 if (isOutReg) OutputRegs.insert(Reg); // Mark reg used.
1539 if (isInReg) InputRegs.insert(Reg); // Mark reg used.
1540 }
1541
1542 return RegsForValue(Regs, *RC->vt_begin(), VT);
1543 }
1544 }
1545
1546 // Otherwise, we couldn't allocate enough registers for this.
1547 return RegsForValue();
Chris Lattner4e4b5762006-02-01 18:59:47 +00001548}
1549
Chris Lattner864635a2006-02-22 22:37:12 +00001550
Chris Lattnerce7518c2006-01-26 22:24:51 +00001551/// visitInlineAsm - Handle a call to an InlineAsm object.
1552///
1553void SelectionDAGLowering::visitInlineAsm(CallInst &I) {
1554 InlineAsm *IA = cast<InlineAsm>(I.getOperand(0));
1555
1556 SDOperand AsmStr = DAG.getTargetExternalSymbol(IA->getAsmString().c_str(),
1557 MVT::Other);
1558
1559 // Note, we treat inline asms both with and without side-effects as the same.
1560 // If an inline asm doesn't have side effects and doesn't access memory, we
1561 // could not choose to not chain it.
1562 bool hasSideEffects = IA->hasSideEffects();
1563
Chris Lattner2cc2f662006-02-01 01:28:23 +00001564 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
Chris Lattner1efa40f2006-02-22 00:56:39 +00001565 std::vector<MVT::ValueType> ConstraintVTs;
Chris Lattnerce7518c2006-01-26 22:24:51 +00001566
1567 /// AsmNodeOperands - A list of pairs. The first element is a register, the
1568 /// second is a bitfield where bit #0 is set if it is a use and bit #1 is set
1569 /// if it is a def of that register.
1570 std::vector<SDOperand> AsmNodeOperands;
1571 AsmNodeOperands.push_back(SDOperand()); // reserve space for input chain
1572 AsmNodeOperands.push_back(AsmStr);
1573
1574 SDOperand Chain = getRoot();
1575 SDOperand Flag;
1576
Chris Lattner4e4b5762006-02-01 18:59:47 +00001577 // We fully assign registers here at isel time. This is not optimal, but
1578 // should work. For register classes that correspond to LLVM classes, we
1579 // could let the LLVM RA do its thing, but we currently don't. Do a prepass
1580 // over the constraints, collecting fixed registers that we know we can't use.
1581 std::set<unsigned> OutputRegs, InputRegs;
Chris Lattner1efa40f2006-02-22 00:56:39 +00001582 unsigned OpNum = 1;
Chris Lattner4e4b5762006-02-01 18:59:47 +00001583 for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
1584 assert(Constraints[i].Codes.size() == 1 && "Only handles one code so far!");
1585 std::string &ConstraintCode = Constraints[i].Codes[0];
Chris Lattner2223aea2006-02-02 00:25:23 +00001586
Chris Lattner1efa40f2006-02-22 00:56:39 +00001587 MVT::ValueType OpVT;
1588
1589 // Compute the value type for each operand and add it to ConstraintVTs.
1590 switch (Constraints[i].Type) {
1591 case InlineAsm::isOutput:
1592 if (!Constraints[i].isIndirectOutput) {
1593 assert(I.getType() != Type::VoidTy && "Bad inline asm!");
1594 OpVT = TLI.getValueType(I.getType());
1595 } else {
Chris Lattner22873462006-02-27 23:45:39 +00001596 const Type *OpTy = I.getOperand(OpNum)->getType();
Chris Lattner1efa40f2006-02-22 00:56:39 +00001597 OpVT = TLI.getValueType(cast<PointerType>(OpTy)->getElementType());
1598 OpNum++; // Consumes a call operand.
1599 }
1600 break;
1601 case InlineAsm::isInput:
1602 OpVT = TLI.getValueType(I.getOperand(OpNum)->getType());
1603 OpNum++; // Consumes a call operand.
1604 break;
1605 case InlineAsm::isClobber:
1606 OpVT = MVT::Other;
1607 break;
1608 }
1609
1610 ConstraintVTs.push_back(OpVT);
1611
Chris Lattner864635a2006-02-22 22:37:12 +00001612 if (TLI.getRegForInlineAsmConstraint(ConstraintCode, OpVT).first == 0)
1613 continue; // Not assigned a fixed reg.
Chris Lattner1efa40f2006-02-22 00:56:39 +00001614
Chris Lattner864635a2006-02-22 22:37:12 +00001615 // Build a list of regs that this operand uses. This always has a single
1616 // element for promoted/expanded operands.
1617 RegsForValue Regs = GetRegistersForValue(ConstraintCode, OpVT,
1618 false, false,
1619 OutputRegs, InputRegs);
Chris Lattner4e4b5762006-02-01 18:59:47 +00001620
1621 switch (Constraints[i].Type) {
1622 case InlineAsm::isOutput:
1623 // We can't assign any other output to this register.
Chris Lattner864635a2006-02-22 22:37:12 +00001624 OutputRegs.insert(Regs.Regs.begin(), Regs.Regs.end());
Chris Lattner4e4b5762006-02-01 18:59:47 +00001625 // If this is an early-clobber output, it cannot be assigned to the same
1626 // value as the input reg.
Chris Lattner2223aea2006-02-02 00:25:23 +00001627 if (Constraints[i].isEarlyClobber || Constraints[i].hasMatchingInput)
Chris Lattner864635a2006-02-22 22:37:12 +00001628 InputRegs.insert(Regs.Regs.begin(), Regs.Regs.end());
Chris Lattner4e4b5762006-02-01 18:59:47 +00001629 break;
Chris Lattner1efa40f2006-02-22 00:56:39 +00001630 case InlineAsm::isInput:
1631 // We can't assign any other input to this register.
Chris Lattner864635a2006-02-22 22:37:12 +00001632 InputRegs.insert(Regs.Regs.begin(), Regs.Regs.end());
Chris Lattner1efa40f2006-02-22 00:56:39 +00001633 break;
Chris Lattner4e4b5762006-02-01 18:59:47 +00001634 case InlineAsm::isClobber:
1635 // Clobbered regs cannot be used as inputs or outputs.
Chris Lattner864635a2006-02-22 22:37:12 +00001636 InputRegs.insert(Regs.Regs.begin(), Regs.Regs.end());
1637 OutputRegs.insert(Regs.Regs.begin(), Regs.Regs.end());
Chris Lattner4e4b5762006-02-01 18:59:47 +00001638 break;
Chris Lattner4e4b5762006-02-01 18:59:47 +00001639 }
1640 }
Chris Lattner2cc2f662006-02-01 01:28:23 +00001641
Chris Lattner0f0b7d42006-02-21 23:12:12 +00001642 // Loop over all of the inputs, copying the operand values into the
1643 // appropriate registers and processing the output regs.
Chris Lattner864635a2006-02-22 22:37:12 +00001644 RegsForValue RetValRegs;
1645 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Chris Lattner1efa40f2006-02-22 00:56:39 +00001646 OpNum = 1;
Chris Lattner0f0b7d42006-02-21 23:12:12 +00001647
Chris Lattner6656dd12006-01-31 02:03:41 +00001648 for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
Chris Lattner2cc2f662006-02-01 01:28:23 +00001649 assert(Constraints[i].Codes.size() == 1 && "Only handles one code so far!");
1650 std::string &ConstraintCode = Constraints[i].Codes[0];
Chris Lattner1efa40f2006-02-22 00:56:39 +00001651
Chris Lattner2cc2f662006-02-01 01:28:23 +00001652 switch (Constraints[i].Type) {
1653 case InlineAsm::isOutput: {
Chris Lattner22873462006-02-27 23:45:39 +00001654 TargetLowering::ConstraintType CTy = TargetLowering::C_RegisterClass;
1655 if (ConstraintCode.size() == 1) // not a physreg name.
1656 CTy = TLI.getConstraintType(ConstraintCode[0]);
1657
1658 if (CTy == TargetLowering::C_Memory) {
1659 // Memory output.
1660 SDOperand InOperandVal = getValue(I.getOperand(OpNum));
1661
1662 // Check that the operand (the address to store to) isn't a float.
1663 if (!MVT::isInteger(InOperandVal.getValueType()))
1664 assert(0 && "MATCH FAIL!");
1665
1666 if (!Constraints[i].isIndirectOutput)
1667 assert(0 && "MATCH FAIL!");
1668
1669 OpNum++; // Consumes a call operand.
1670
1671 // Extend/truncate to the right pointer type if needed.
1672 MVT::ValueType PtrType = TLI.getPointerTy();
1673 if (InOperandVal.getValueType() < PtrType)
1674 InOperandVal = DAG.getNode(ISD::ZERO_EXTEND, PtrType, InOperandVal);
1675 else if (InOperandVal.getValueType() > PtrType)
1676 InOperandVal = DAG.getNode(ISD::TRUNCATE, PtrType, InOperandVal);
1677
1678 // Add information to the INLINEASM node to know about this output.
1679 unsigned ResOpType = 4/*MEM*/ | (1 << 3);
1680 AsmNodeOperands.push_back(DAG.getConstant(ResOpType, MVT::i32));
1681 AsmNodeOperands.push_back(InOperandVal);
1682 break;
1683 }
1684
1685 // Otherwise, this is a register output.
1686 assert(CTy == TargetLowering::C_RegisterClass && "Unknown op type!");
1687
Chris Lattner864635a2006-02-22 22:37:12 +00001688 // If this is an early-clobber output, or if there is an input
1689 // constraint that matches this, we need to reserve the input register
1690 // so no other inputs allocate to it.
1691 bool UsesInputRegister = false;
1692 if (Constraints[i].isEarlyClobber || Constraints[i].hasMatchingInput)
1693 UsesInputRegister = true;
1694
1695 // Copy the output from the appropriate register. Find a register that
Chris Lattner1efa40f2006-02-22 00:56:39 +00001696 // we can use.
Chris Lattner864635a2006-02-22 22:37:12 +00001697 RegsForValue Regs =
1698 GetRegistersForValue(ConstraintCode, ConstraintVTs[i],
1699 true, UsesInputRegister,
1700 OutputRegs, InputRegs);
1701 assert(!Regs.Regs.empty() && "Couldn't allocate output reg!");
Chris Lattner1efa40f2006-02-22 00:56:39 +00001702
Chris Lattner2cc2f662006-02-01 01:28:23 +00001703 if (!Constraints[i].isIndirectOutput) {
Chris Lattner864635a2006-02-22 22:37:12 +00001704 assert(RetValRegs.Regs.empty() &&
Chris Lattner2cc2f662006-02-01 01:28:23 +00001705 "Cannot have multiple output constraints yet!");
Chris Lattner2cc2f662006-02-01 01:28:23 +00001706 assert(I.getType() != Type::VoidTy && "Bad inline asm!");
Chris Lattner864635a2006-02-22 22:37:12 +00001707 RetValRegs = Regs;
Chris Lattner2cc2f662006-02-01 01:28:23 +00001708 } else {
Chris Lattner22873462006-02-27 23:45:39 +00001709 IndirectStoresToEmit.push_back(std::make_pair(Regs,
1710 I.getOperand(OpNum)));
Chris Lattner2cc2f662006-02-01 01:28:23 +00001711 OpNum++; // Consumes a call operand.
1712 }
Chris Lattner6656dd12006-01-31 02:03:41 +00001713
1714 // Add information to the INLINEASM node to know that this register is
1715 // set.
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001716 Regs.AddInlineAsmOperands(2 /*REGDEF*/, DAG, AsmNodeOperands);
Chris Lattner6656dd12006-01-31 02:03:41 +00001717 break;
1718 }
1719 case InlineAsm::isInput: {
Chris Lattner22873462006-02-27 23:45:39 +00001720 SDOperand InOperandVal = getValue(I.getOperand(OpNum));
Chris Lattner4e4b5762006-02-01 18:59:47 +00001721 OpNum++; // Consumes a call operand.
Chris Lattner3d81fee2006-02-04 02:16:44 +00001722
Chris Lattner2223aea2006-02-02 00:25:23 +00001723 if (isdigit(ConstraintCode[0])) { // Matching constraint?
1724 // If this is required to match an output register we have already set,
1725 // just use its register.
1726 unsigned OperandNo = atoi(ConstraintCode.c_str());
Chris Lattner3d81fee2006-02-04 02:16:44 +00001727
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001728 // Scan until we find the definition we already emitted of this operand.
1729 // When we find it, create a RegsForValue operand.
1730 unsigned CurOp = 2; // The first operand.
1731 for (; OperandNo; --OperandNo) {
1732 // Advance to the next operand.
1733 unsigned NumOps =
1734 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getValue();
1735 assert((NumOps & 7) == 2 /*REGDEF*/ &&
1736 "Skipped past definitions?");
1737 CurOp += (NumOps>>3)+1;
1738 }
1739
1740 unsigned NumOps =
1741 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getValue();
1742 assert((NumOps & 7) == 2 /*REGDEF*/ &&
1743 "Skipped past definitions?");
1744
1745 // Add NumOps>>3 registers to MatchedRegs.
1746 RegsForValue MatchedRegs;
1747 MatchedRegs.ValueVT = InOperandVal.getValueType();
1748 MatchedRegs.RegVT = AsmNodeOperands[CurOp+1].getValueType();
1749 for (unsigned i = 0, e = NumOps>>3; i != e; ++i) {
1750 unsigned Reg=cast<RegisterSDNode>(AsmNodeOperands[++CurOp])->getReg();
1751 MatchedRegs.Regs.push_back(Reg);
1752 }
1753
1754 // Use the produced MatchedRegs object to
1755 MatchedRegs.getCopyToRegs(InOperandVal, DAG, Chain, Flag);
1756 MatchedRegs.AddInlineAsmOperands(1 /*REGUSE*/, DAG, AsmNodeOperands);
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001757 break;
Chris Lattner2223aea2006-02-02 00:25:23 +00001758 }
Chris Lattner87bc3bd2006-02-24 01:11:24 +00001759
1760 TargetLowering::ConstraintType CTy = TargetLowering::C_RegisterClass;
1761 if (ConstraintCode.size() == 1) // not a physreg name.
1762 CTy = TLI.getConstraintType(ConstraintCode[0]);
1763
1764 if (CTy == TargetLowering::C_Other) {
1765 if (!TLI.isOperandValidForConstraint(InOperandVal, ConstraintCode[0]))
1766 assert(0 && "MATCH FAIL!");
1767
1768 // Add information to the INLINEASM node to know about this input.
1769 unsigned ResOpType = 3 /*IMM*/ | (1 << 3);
1770 AsmNodeOperands.push_back(DAG.getConstant(ResOpType, MVT::i32));
1771 AsmNodeOperands.push_back(InOperandVal);
1772 break;
1773 } else if (CTy == TargetLowering::C_Memory) {
1774 // Memory input.
1775
1776 // Check that the operand isn't a float.
1777 if (!MVT::isInteger(InOperandVal.getValueType()))
1778 assert(0 && "MATCH FAIL!");
1779
1780 // Extend/truncate to the right pointer type if needed.
1781 MVT::ValueType PtrType = TLI.getPointerTy();
1782 if (InOperandVal.getValueType() < PtrType)
1783 InOperandVal = DAG.getNode(ISD::ZERO_EXTEND, PtrType, InOperandVal);
1784 else if (InOperandVal.getValueType() > PtrType)
1785 InOperandVal = DAG.getNode(ISD::TRUNCATE, PtrType, InOperandVal);
1786
1787 // Add information to the INLINEASM node to know about this input.
1788 unsigned ResOpType = 4/*MEM*/ | (1 << 3);
1789 AsmNodeOperands.push_back(DAG.getConstant(ResOpType, MVT::i32));
1790 AsmNodeOperands.push_back(InOperandVal);
1791 break;
1792 }
1793
1794 assert(CTy == TargetLowering::C_RegisterClass && "Unknown op type!");
1795
1796 // Copy the input into the appropriate registers.
1797 RegsForValue InRegs =
1798 GetRegistersForValue(ConstraintCode, ConstraintVTs[i],
1799 false, true, OutputRegs, InputRegs);
1800 // FIXME: should be match fail.
1801 assert(!InRegs.Regs.empty() && "Couldn't allocate input reg!");
1802
1803 InRegs.getCopyToRegs(InOperandVal, DAG, Chain, Flag);
1804
1805 InRegs.AddInlineAsmOperands(1/*REGUSE*/, DAG, AsmNodeOperands);
Chris Lattner6656dd12006-01-31 02:03:41 +00001806 break;
1807 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001808 case InlineAsm::isClobber: {
1809 RegsForValue ClobberedRegs =
1810 GetRegistersForValue(ConstraintCode, MVT::Other, false, false,
1811 OutputRegs, InputRegs);
1812 // Add the clobbered value to the operand list, so that the register
1813 // allocator is aware that the physreg got clobbered.
1814 if (!ClobberedRegs.Regs.empty())
1815 ClobberedRegs.AddInlineAsmOperands(2/*REGDEF*/, DAG, AsmNodeOperands);
Chris Lattner6656dd12006-01-31 02:03:41 +00001816 break;
1817 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001818 }
Chris Lattner6656dd12006-01-31 02:03:41 +00001819 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001820
1821 // Finish up input operands.
1822 AsmNodeOperands[0] = Chain;
1823 if (Flag.Val) AsmNodeOperands.push_back(Flag);
1824
1825 std::vector<MVT::ValueType> VTs;
1826 VTs.push_back(MVT::Other);
1827 VTs.push_back(MVT::Flag);
1828 Chain = DAG.getNode(ISD::INLINEASM, VTs, AsmNodeOperands);
1829 Flag = Chain.getValue(1);
1830
Chris Lattner6656dd12006-01-31 02:03:41 +00001831 // If this asm returns a register value, copy the result from that register
1832 // and set it as the value of the call.
Chris Lattner864635a2006-02-22 22:37:12 +00001833 if (!RetValRegs.Regs.empty())
1834 setValue(&I, RetValRegs.getCopyFromRegs(DAG, Chain, Flag));
Chris Lattnerce7518c2006-01-26 22:24:51 +00001835
Chris Lattner6656dd12006-01-31 02:03:41 +00001836 std::vector<std::pair<SDOperand, Value*> > StoresToEmit;
1837
1838 // Process indirect outputs, first output all of the flagged copies out of
1839 // physregs.
1840 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
Chris Lattner864635a2006-02-22 22:37:12 +00001841 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
Chris Lattner6656dd12006-01-31 02:03:41 +00001842 Value *Ptr = IndirectStoresToEmit[i].second;
Chris Lattner864635a2006-02-22 22:37:12 +00001843 SDOperand OutVal = OutRegs.getCopyFromRegs(DAG, Chain, Flag);
1844 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
Chris Lattner6656dd12006-01-31 02:03:41 +00001845 }
1846
1847 // Emit the non-flagged stores from the physregs.
1848 std::vector<SDOperand> OutChains;
1849 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i)
1850 OutChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1851 StoresToEmit[i].first,
1852 getValue(StoresToEmit[i].second),
1853 DAG.getSrcValue(StoresToEmit[i].second)));
1854 if (!OutChains.empty())
1855 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains);
Chris Lattnerce7518c2006-01-26 22:24:51 +00001856 DAG.setRoot(Chain);
1857}
1858
1859
Chris Lattner1c08c712005-01-07 07:47:53 +00001860void SelectionDAGLowering::visitMalloc(MallocInst &I) {
1861 SDOperand Src = getValue(I.getOperand(0));
1862
1863 MVT::ValueType IntPtr = TLI.getPointerTy();
Chris Lattner68cd65e2005-01-22 23:04:37 +00001864
1865 if (IntPtr < Src.getValueType())
1866 Src = DAG.getNode(ISD::TRUNCATE, IntPtr, Src);
1867 else if (IntPtr > Src.getValueType())
1868 Src = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, Src);
Chris Lattner1c08c712005-01-07 07:47:53 +00001869
1870 // Scale the source by the type size.
1871 uint64_t ElementSize = TD.getTypeSize(I.getType()->getElementType());
1872 Src = DAG.getNode(ISD::MUL, Src.getValueType(),
1873 Src, getIntPtrConstant(ElementSize));
1874
1875 std::vector<std::pair<SDOperand, const Type*> > Args;
1876 Args.push_back(std::make_pair(Src, TLI.getTargetData().getIntPtrType()));
Chris Lattnercf5734d2005-01-08 19:26:18 +00001877
1878 std::pair<SDOperand,SDOperand> Result =
Chris Lattneradf6a962005-05-13 18:50:42 +00001879 TLI.LowerCallTo(getRoot(), I.getType(), false, CallingConv::C, true,
Chris Lattnercf5734d2005-01-08 19:26:18 +00001880 DAG.getExternalSymbol("malloc", IntPtr),
1881 Args, DAG);
1882 setValue(&I, Result.first); // Pointers always fit in registers
1883 DAG.setRoot(Result.second);
Chris Lattner1c08c712005-01-07 07:47:53 +00001884}
1885
1886void SelectionDAGLowering::visitFree(FreeInst &I) {
1887 std::vector<std::pair<SDOperand, const Type*> > Args;
1888 Args.push_back(std::make_pair(getValue(I.getOperand(0)),
1889 TLI.getTargetData().getIntPtrType()));
1890 MVT::ValueType IntPtr = TLI.getPointerTy();
Chris Lattnercf5734d2005-01-08 19:26:18 +00001891 std::pair<SDOperand,SDOperand> Result =
Chris Lattneradf6a962005-05-13 18:50:42 +00001892 TLI.LowerCallTo(getRoot(), Type::VoidTy, false, CallingConv::C, true,
Chris Lattnercf5734d2005-01-08 19:26:18 +00001893 DAG.getExternalSymbol("free", IntPtr), Args, DAG);
1894 DAG.setRoot(Result.second);
Chris Lattner1c08c712005-01-07 07:47:53 +00001895}
1896
Chris Lattner025c39b2005-08-26 20:54:47 +00001897// InsertAtEndOfBasicBlock - This method should be implemented by targets that
1898// mark instructions with the 'usesCustomDAGSchedInserter' flag. These
1899// instructions are special in various ways, which require special support to
1900// insert. The specified MachineInstr is created but not inserted into any
1901// basic blocks, and the scheduler passes ownership of it to this method.
1902MachineBasicBlock *TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1903 MachineBasicBlock *MBB) {
1904 std::cerr << "If a target marks an instruction with "
1905 "'usesCustomDAGSchedInserter', it must implement "
1906 "TargetLowering::InsertAtEndOfBasicBlock!\n";
1907 abort();
1908 return 0;
1909}
1910
Chris Lattner39ae3622005-01-09 00:00:49 +00001911void SelectionDAGLowering::visitVAStart(CallInst &I) {
Nate Begemanacc398c2006-01-25 18:21:52 +00001912 DAG.setRoot(DAG.getNode(ISD::VASTART, MVT::Other, getRoot(),
1913 getValue(I.getOperand(1)),
1914 DAG.getSrcValue(I.getOperand(1))));
Chris Lattner39ae3622005-01-09 00:00:49 +00001915}
1916
1917void SelectionDAGLowering::visitVAArg(VAArgInst &I) {
Nate Begemanacc398c2006-01-25 18:21:52 +00001918 SDOperand V = DAG.getVAArg(TLI.getValueType(I.getType()), getRoot(),
1919 getValue(I.getOperand(0)),
1920 DAG.getSrcValue(I.getOperand(0)));
1921 setValue(&I, V);
1922 DAG.setRoot(V.getValue(1));
Chris Lattner1c08c712005-01-07 07:47:53 +00001923}
1924
1925void SelectionDAGLowering::visitVAEnd(CallInst &I) {
Nate Begemanacc398c2006-01-25 18:21:52 +00001926 DAG.setRoot(DAG.getNode(ISD::VAEND, MVT::Other, getRoot(),
1927 getValue(I.getOperand(1)),
1928 DAG.getSrcValue(I.getOperand(1))));
Chris Lattner1c08c712005-01-07 07:47:53 +00001929}
1930
1931void SelectionDAGLowering::visitVACopy(CallInst &I) {
Nate Begemanacc398c2006-01-25 18:21:52 +00001932 DAG.setRoot(DAG.getNode(ISD::VACOPY, MVT::Other, getRoot(),
1933 getValue(I.getOperand(1)),
1934 getValue(I.getOperand(2)),
1935 DAG.getSrcValue(I.getOperand(1)),
1936 DAG.getSrcValue(I.getOperand(2))));
Chris Lattner1c08c712005-01-07 07:47:53 +00001937}
1938
Chris Lattner39ae3622005-01-09 00:00:49 +00001939// It is always conservatively correct for llvm.returnaddress and
1940// llvm.frameaddress to return 0.
1941std::pair<SDOperand, SDOperand>
1942TargetLowering::LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain,
1943 unsigned Depth, SelectionDAG &DAG) {
1944 return std::make_pair(DAG.getConstant(0, getPointerTy()), Chain);
Chris Lattner1c08c712005-01-07 07:47:53 +00001945}
1946
Chris Lattner50381b62005-05-14 05:50:48 +00001947SDOperand TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner171453a2005-01-16 07:28:41 +00001948 assert(0 && "LowerOperation not implemented for this target!");
1949 abort();
Misha Brukmand3f03e42005-02-17 21:39:27 +00001950 return SDOperand();
Chris Lattner171453a2005-01-16 07:28:41 +00001951}
1952
Nate Begeman0aed7842006-01-28 03:14:31 +00001953SDOperand TargetLowering::CustomPromoteOperation(SDOperand Op,
1954 SelectionDAG &DAG) {
1955 assert(0 && "CustomPromoteOperation not implemented for this target!");
1956 abort();
1957 return SDOperand();
1958}
1959
Chris Lattner39ae3622005-01-09 00:00:49 +00001960void SelectionDAGLowering::visitFrameReturnAddress(CallInst &I, bool isFrame) {
1961 unsigned Depth = (unsigned)cast<ConstantUInt>(I.getOperand(1))->getValue();
1962 std::pair<SDOperand,SDOperand> Result =
Chris Lattnera651cf62005-01-17 19:43:36 +00001963 TLI.LowerFrameReturnAddress(isFrame, getRoot(), Depth, DAG);
Chris Lattner39ae3622005-01-09 00:00:49 +00001964 setValue(&I, Result.first);
1965 DAG.setRoot(Result.second);
Chris Lattner1c08c712005-01-07 07:47:53 +00001966}
1967
Evan Cheng74d0aa92006-02-15 21:59:04 +00001968/// getMemsetValue - Vectorized representation of the memset value
Evan Cheng1db92f92006-02-14 08:22:34 +00001969/// operand.
1970static SDOperand getMemsetValue(SDOperand Value, MVT::ValueType VT,
Evan Chenga47876d2006-02-15 22:12:35 +00001971 SelectionDAG &DAG) {
Evan Cheng1db92f92006-02-14 08:22:34 +00001972 MVT::ValueType CurVT = VT;
1973 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
1974 uint64_t Val = C->getValue() & 255;
1975 unsigned Shift = 8;
1976 while (CurVT != MVT::i8) {
1977 Val = (Val << Shift) | Val;
1978 Shift <<= 1;
1979 CurVT = (MVT::ValueType)((unsigned)CurVT - 1);
Evan Cheng1db92f92006-02-14 08:22:34 +00001980 }
1981 return DAG.getConstant(Val, VT);
1982 } else {
1983 Value = DAG.getNode(ISD::ZERO_EXTEND, VT, Value);
1984 unsigned Shift = 8;
1985 while (CurVT != MVT::i8) {
1986 Value =
1987 DAG.getNode(ISD::OR, VT,
1988 DAG.getNode(ISD::SHL, VT, Value,
1989 DAG.getConstant(Shift, MVT::i8)), Value);
1990 Shift <<= 1;
1991 CurVT = (MVT::ValueType)((unsigned)CurVT - 1);
Evan Cheng1db92f92006-02-14 08:22:34 +00001992 }
1993
1994 return Value;
1995 }
1996}
1997
Evan Cheng74d0aa92006-02-15 21:59:04 +00001998/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
1999/// used when a memcpy is turned into a memset when the source is a constant
2000/// string ptr.
2001static SDOperand getMemsetStringVal(MVT::ValueType VT,
2002 SelectionDAG &DAG, TargetLowering &TLI,
2003 std::string &Str, unsigned Offset) {
2004 MVT::ValueType CurVT = VT;
2005 uint64_t Val = 0;
2006 unsigned MSB = getSizeInBits(VT) / 8;
2007 if (TLI.isLittleEndian())
2008 Offset = Offset + MSB - 1;
2009 for (unsigned i = 0; i != MSB; ++i) {
2010 Val = (Val << 8) | Str[Offset];
2011 Offset += TLI.isLittleEndian() ? -1 : 1;
2012 }
2013 return DAG.getConstant(Val, VT);
2014}
2015
Evan Cheng1db92f92006-02-14 08:22:34 +00002016/// getMemBasePlusOffset - Returns base and offset node for the
2017static SDOperand getMemBasePlusOffset(SDOperand Base, unsigned Offset,
2018 SelectionDAG &DAG, TargetLowering &TLI) {
2019 MVT::ValueType VT = Base.getValueType();
2020 return DAG.getNode(ISD::ADD, VT, Base, DAG.getConstant(Offset, VT));
2021}
2022
Evan Chengc4f8eee2006-02-14 20:12:38 +00002023/// MeetsMaxMemopRequirement - Determines if the number of memory ops required
Evan Cheng80e89d72006-02-14 09:11:59 +00002024/// to replace the memset / memcpy is below the threshold. It also returns the
2025/// types of the sequence of memory ops to perform memset / memcpy.
Evan Chengc4f8eee2006-02-14 20:12:38 +00002026static bool MeetsMaxMemopRequirement(std::vector<MVT::ValueType> &MemOps,
2027 unsigned Limit, uint64_t Size,
2028 unsigned Align, TargetLowering &TLI) {
Evan Cheng1db92f92006-02-14 08:22:34 +00002029 MVT::ValueType VT;
2030
2031 if (TLI.allowsUnalignedMemoryAccesses()) {
2032 VT = MVT::i64;
2033 } else {
2034 switch (Align & 7) {
2035 case 0:
2036 VT = MVT::i64;
2037 break;
2038 case 4:
2039 VT = MVT::i32;
2040 break;
2041 case 2:
2042 VT = MVT::i16;
2043 break;
2044 default:
2045 VT = MVT::i8;
2046 break;
2047 }
2048 }
2049
Evan Cheng80e89d72006-02-14 09:11:59 +00002050 MVT::ValueType LVT = MVT::i64;
2051 while (!TLI.isTypeLegal(LVT))
2052 LVT = (MVT::ValueType)((unsigned)LVT - 1);
2053 assert(MVT::isInteger(LVT));
Evan Cheng1db92f92006-02-14 08:22:34 +00002054
Evan Cheng80e89d72006-02-14 09:11:59 +00002055 if (VT > LVT)
2056 VT = LVT;
2057
Evan Chengdea72452006-02-14 23:05:54 +00002058 unsigned NumMemOps = 0;
Evan Cheng1db92f92006-02-14 08:22:34 +00002059 while (Size != 0) {
2060 unsigned VTSize = getSizeInBits(VT) / 8;
2061 while (VTSize > Size) {
2062 VT = (MVT::ValueType)((unsigned)VT - 1);
Evan Cheng1db92f92006-02-14 08:22:34 +00002063 VTSize >>= 1;
2064 }
Evan Cheng80e89d72006-02-14 09:11:59 +00002065 assert(MVT::isInteger(VT));
2066
2067 if (++NumMemOps > Limit)
2068 return false;
Evan Cheng1db92f92006-02-14 08:22:34 +00002069 MemOps.push_back(VT);
2070 Size -= VTSize;
2071 }
Evan Cheng80e89d72006-02-14 09:11:59 +00002072
2073 return true;
Evan Cheng1db92f92006-02-14 08:22:34 +00002074}
2075
Chris Lattner7041ee32005-01-11 05:56:49 +00002076void SelectionDAGLowering::visitMemIntrinsic(CallInst &I, unsigned Op) {
Evan Cheng1db92f92006-02-14 08:22:34 +00002077 SDOperand Op1 = getValue(I.getOperand(1));
2078 SDOperand Op2 = getValue(I.getOperand(2));
2079 SDOperand Op3 = getValue(I.getOperand(3));
2080 SDOperand Op4 = getValue(I.getOperand(4));
2081 unsigned Align = (unsigned)cast<ConstantSDNode>(Op4)->getValue();
2082 if (Align == 0) Align = 1;
2083
2084 if (ConstantSDNode *Size = dyn_cast<ConstantSDNode>(Op3)) {
2085 std::vector<MVT::ValueType> MemOps;
Evan Cheng1db92f92006-02-14 08:22:34 +00002086
2087 // Expand memset / memcpy to a series of load / store ops
2088 // if the size operand falls below a certain threshold.
2089 std::vector<SDOperand> OutChains;
2090 switch (Op) {
Evan Chengac940ab2006-02-14 19:45:56 +00002091 default: break; // Do nothing for now.
Evan Cheng1db92f92006-02-14 08:22:34 +00002092 case ISD::MEMSET: {
Evan Chengc4f8eee2006-02-14 20:12:38 +00002093 if (MeetsMaxMemopRequirement(MemOps, TLI.getMaxStoresPerMemset(),
2094 Size->getValue(), Align, TLI)) {
Evan Cheng80e89d72006-02-14 09:11:59 +00002095 unsigned NumMemOps = MemOps.size();
Evan Cheng1db92f92006-02-14 08:22:34 +00002096 unsigned Offset = 0;
2097 for (unsigned i = 0; i < NumMemOps; i++) {
2098 MVT::ValueType VT = MemOps[i];
2099 unsigned VTSize = getSizeInBits(VT) / 8;
Evan Chenga47876d2006-02-15 22:12:35 +00002100 SDOperand Value = getMemsetValue(Op2, VT, DAG);
Evan Chengc080d6f2006-02-15 01:54:51 +00002101 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, getRoot(),
2102 Value,
Chris Lattner864635a2006-02-22 22:37:12 +00002103 getMemBasePlusOffset(Op1, Offset, DAG, TLI),
2104 DAG.getSrcValue(I.getOperand(1), Offset));
Evan Chengc080d6f2006-02-15 01:54:51 +00002105 OutChains.push_back(Store);
Evan Cheng1db92f92006-02-14 08:22:34 +00002106 Offset += VTSize;
2107 }
Evan Cheng1db92f92006-02-14 08:22:34 +00002108 }
Evan Chengc080d6f2006-02-15 01:54:51 +00002109 break;
Evan Cheng1db92f92006-02-14 08:22:34 +00002110 }
Evan Chengc080d6f2006-02-15 01:54:51 +00002111 case ISD::MEMCPY: {
2112 if (MeetsMaxMemopRequirement(MemOps, TLI.getMaxStoresPerMemcpy(),
2113 Size->getValue(), Align, TLI)) {
2114 unsigned NumMemOps = MemOps.size();
Evan Chengcffbb512006-02-16 23:11:42 +00002115 unsigned SrcOff = 0, DstOff = 0, SrcDelta = 0;
Evan Cheng74d0aa92006-02-15 21:59:04 +00002116 GlobalAddressSDNode *G = NULL;
2117 std::string Str;
Evan Chengcffbb512006-02-16 23:11:42 +00002118 bool CopyFromStr = false;
Evan Cheng74d0aa92006-02-15 21:59:04 +00002119
2120 if (Op2.getOpcode() == ISD::GlobalAddress)
2121 G = cast<GlobalAddressSDNode>(Op2);
2122 else if (Op2.getOpcode() == ISD::ADD &&
2123 Op2.getOperand(0).getOpcode() == ISD::GlobalAddress &&
2124 Op2.getOperand(1).getOpcode() == ISD::Constant) {
2125 G = cast<GlobalAddressSDNode>(Op2.getOperand(0));
Evan Chengcffbb512006-02-16 23:11:42 +00002126 SrcDelta = cast<ConstantSDNode>(Op2.getOperand(1))->getValue();
Evan Cheng74d0aa92006-02-15 21:59:04 +00002127 }
2128 if (G) {
2129 GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getGlobal());
Evan Chengcffbb512006-02-16 23:11:42 +00002130 if (GV) {
Evan Cheng09371032006-03-10 23:52:03 +00002131 Str = GV->getStringValue(false);
Evan Chengcffbb512006-02-16 23:11:42 +00002132 if (!Str.empty()) {
2133 CopyFromStr = true;
2134 SrcOff += SrcDelta;
2135 }
2136 }
Evan Cheng74d0aa92006-02-15 21:59:04 +00002137 }
2138
Evan Chengc080d6f2006-02-15 01:54:51 +00002139 for (unsigned i = 0; i < NumMemOps; i++) {
2140 MVT::ValueType VT = MemOps[i];
2141 unsigned VTSize = getSizeInBits(VT) / 8;
Evan Cheng74d0aa92006-02-15 21:59:04 +00002142 SDOperand Value, Chain, Store;
2143
Evan Chengcffbb512006-02-16 23:11:42 +00002144 if (CopyFromStr) {
Evan Cheng74d0aa92006-02-15 21:59:04 +00002145 Value = getMemsetStringVal(VT, DAG, TLI, Str, SrcOff);
2146 Chain = getRoot();
2147 Store =
2148 DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2149 getMemBasePlusOffset(Op1, DstOff, DAG, TLI),
2150 DAG.getSrcValue(I.getOperand(1), DstOff));
2151 } else {
2152 Value = DAG.getLoad(VT, getRoot(),
2153 getMemBasePlusOffset(Op2, SrcOff, DAG, TLI),
2154 DAG.getSrcValue(I.getOperand(2), SrcOff));
2155 Chain = Value.getValue(1);
2156 Store =
2157 DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2158 getMemBasePlusOffset(Op1, DstOff, DAG, TLI),
2159 DAG.getSrcValue(I.getOperand(1), DstOff));
2160 }
Evan Chengc080d6f2006-02-15 01:54:51 +00002161 OutChains.push_back(Store);
Evan Cheng74d0aa92006-02-15 21:59:04 +00002162 SrcOff += VTSize;
2163 DstOff += VTSize;
Evan Chengc080d6f2006-02-15 01:54:51 +00002164 }
2165 }
2166 break;
2167 }
2168 }
2169
2170 if (!OutChains.empty()) {
2171 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains));
2172 return;
Evan Cheng1db92f92006-02-14 08:22:34 +00002173 }
2174 }
2175
Chris Lattner7041ee32005-01-11 05:56:49 +00002176 std::vector<SDOperand> Ops;
Chris Lattnera651cf62005-01-17 19:43:36 +00002177 Ops.push_back(getRoot());
Evan Cheng1db92f92006-02-14 08:22:34 +00002178 Ops.push_back(Op1);
2179 Ops.push_back(Op2);
2180 Ops.push_back(Op3);
2181 Ops.push_back(Op4);
Chris Lattner7041ee32005-01-11 05:56:49 +00002182 DAG.setRoot(DAG.getNode(Op, MVT::Other, Ops));
Chris Lattner1c08c712005-01-07 07:47:53 +00002183}
2184
Chris Lattner7041ee32005-01-11 05:56:49 +00002185//===----------------------------------------------------------------------===//
2186// SelectionDAGISel code
2187//===----------------------------------------------------------------------===//
Chris Lattner1c08c712005-01-07 07:47:53 +00002188
2189unsigned SelectionDAGISel::MakeReg(MVT::ValueType VT) {
2190 return RegMap->createVirtualRegister(TLI.getRegClassFor(VT));
2191}
2192
Chris Lattner495a0b52005-08-17 06:37:43 +00002193void SelectionDAGISel::getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner36b708f2005-08-18 17:35:14 +00002194 // FIXME: we only modify the CFG to split critical edges. This
2195 // updates dom and loop info.
Chris Lattner495a0b52005-08-17 06:37:43 +00002196}
Chris Lattner1c08c712005-01-07 07:47:53 +00002197
Chris Lattnerc88d8e92005-12-05 07:10:48 +00002198
2199/// InsertGEPComputeCode - Insert code into BB to compute Ptr+PtrOffset,
2200/// casting to the type of GEPI.
2201static Value *InsertGEPComputeCode(Value *&V, BasicBlock *BB, Instruction *GEPI,
2202 Value *Ptr, Value *PtrOffset) {
2203 if (V) return V; // Already computed.
2204
2205 BasicBlock::iterator InsertPt;
2206 if (BB == GEPI->getParent()) {
2207 // If insert into the GEP's block, insert right after the GEP.
2208 InsertPt = GEPI;
2209 ++InsertPt;
2210 } else {
2211 // Otherwise, insert at the top of BB, after any PHI nodes
2212 InsertPt = BB->begin();
2213 while (isa<PHINode>(InsertPt)) ++InsertPt;
2214 }
2215
Chris Lattnerc78b0b72005-12-08 08:00:12 +00002216 // If Ptr is itself a cast, but in some other BB, emit a copy of the cast into
2217 // BB so that there is only one value live across basic blocks (the cast
2218 // operand).
2219 if (CastInst *CI = dyn_cast<CastInst>(Ptr))
2220 if (CI->getParent() != BB && isa<PointerType>(CI->getOperand(0)->getType()))
2221 Ptr = new CastInst(CI->getOperand(0), CI->getType(), "", InsertPt);
2222
Chris Lattnerc88d8e92005-12-05 07:10:48 +00002223 // Add the offset, cast it to the right type.
2224 Ptr = BinaryOperator::createAdd(Ptr, PtrOffset, "", InsertPt);
2225 Ptr = new CastInst(Ptr, GEPI->getType(), "", InsertPt);
2226 return V = Ptr;
2227}
2228
2229
2230/// OptimizeGEPExpression - Since we are doing basic-block-at-a-time instruction
2231/// selection, we want to be a bit careful about some things. In particular, if
2232/// we have a GEP instruction that is used in a different block than it is
2233/// defined, the addressing expression of the GEP cannot be folded into loads or
2234/// stores that use it. In this case, decompose the GEP and move constant
2235/// indices into blocks that use it.
2236static void OptimizeGEPExpression(GetElementPtrInst *GEPI,
2237 const TargetData &TD) {
Chris Lattnerc88d8e92005-12-05 07:10:48 +00002238 // If this GEP is only used inside the block it is defined in, there is no
2239 // need to rewrite it.
2240 bool isUsedOutsideDefBB = false;
2241 BasicBlock *DefBB = GEPI->getParent();
2242 for (Value::use_iterator UI = GEPI->use_begin(), E = GEPI->use_end();
2243 UI != E; ++UI) {
2244 if (cast<Instruction>(*UI)->getParent() != DefBB) {
2245 isUsedOutsideDefBB = true;
2246 break;
2247 }
2248 }
2249 if (!isUsedOutsideDefBB) return;
2250
2251 // If this GEP has no non-zero constant indices, there is nothing we can do,
2252 // ignore it.
2253 bool hasConstantIndex = false;
2254 for (GetElementPtrInst::op_iterator OI = GEPI->op_begin()+1,
2255 E = GEPI->op_end(); OI != E; ++OI) {
2256 if (ConstantInt *CI = dyn_cast<ConstantInt>(*OI))
2257 if (CI->getRawValue()) {
2258 hasConstantIndex = true;
2259 break;
2260 }
2261 }
Chris Lattner3802c252005-12-11 09:05:13 +00002262 // If this is a GEP &Alloca, 0, 0, forward subst the frame index into uses.
2263 if (!hasConstantIndex && !isa<AllocaInst>(GEPI->getOperand(0))) return;
Chris Lattnerc88d8e92005-12-05 07:10:48 +00002264
2265 // Otherwise, decompose the GEP instruction into multiplies and adds. Sum the
2266 // constant offset (which we now know is non-zero) and deal with it later.
2267 uint64_t ConstantOffset = 0;
2268 const Type *UIntPtrTy = TD.getIntPtrType();
2269 Value *Ptr = new CastInst(GEPI->getOperand(0), UIntPtrTy, "", GEPI);
2270 const Type *Ty = GEPI->getOperand(0)->getType();
2271
2272 for (GetElementPtrInst::op_iterator OI = GEPI->op_begin()+1,
2273 E = GEPI->op_end(); OI != E; ++OI) {
2274 Value *Idx = *OI;
2275 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
2276 unsigned Field = cast<ConstantUInt>(Idx)->getValue();
2277 if (Field)
2278 ConstantOffset += TD.getStructLayout(StTy)->MemberOffsets[Field];
2279 Ty = StTy->getElementType(Field);
2280 } else {
2281 Ty = cast<SequentialType>(Ty)->getElementType();
2282
2283 // Handle constant subscripts.
2284 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
2285 if (CI->getRawValue() == 0) continue;
2286
2287 if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(CI))
2288 ConstantOffset += (int64_t)TD.getTypeSize(Ty)*CSI->getValue();
2289 else
2290 ConstantOffset+=TD.getTypeSize(Ty)*cast<ConstantUInt>(CI)->getValue();
2291 continue;
2292 }
2293
2294 // Ptr = Ptr + Idx * ElementSize;
2295
2296 // Cast Idx to UIntPtrTy if needed.
2297 Idx = new CastInst(Idx, UIntPtrTy, "", GEPI);
2298
2299 uint64_t ElementSize = TD.getTypeSize(Ty);
2300 // Mask off bits that should not be set.
2301 ElementSize &= ~0ULL >> (64-UIntPtrTy->getPrimitiveSizeInBits());
2302 Constant *SizeCst = ConstantUInt::get(UIntPtrTy, ElementSize);
2303
2304 // Multiply by the element size and add to the base.
2305 Idx = BinaryOperator::createMul(Idx, SizeCst, "", GEPI);
2306 Ptr = BinaryOperator::createAdd(Ptr, Idx, "", GEPI);
2307 }
2308 }
2309
2310 // Make sure that the offset fits in uintptr_t.
2311 ConstantOffset &= ~0ULL >> (64-UIntPtrTy->getPrimitiveSizeInBits());
2312 Constant *PtrOffset = ConstantUInt::get(UIntPtrTy, ConstantOffset);
2313
2314 // Okay, we have now emitted all of the variable index parts to the BB that
2315 // the GEP is defined in. Loop over all of the using instructions, inserting
2316 // an "add Ptr, ConstantOffset" into each block that uses it and update the
Chris Lattnerc78b0b72005-12-08 08:00:12 +00002317 // instruction to use the newly computed value, making GEPI dead. When the
2318 // user is a load or store instruction address, we emit the add into the user
2319 // block, otherwise we use a canonical version right next to the gep (these
2320 // won't be foldable as addresses, so we might as well share the computation).
2321
Chris Lattnerc88d8e92005-12-05 07:10:48 +00002322 std::map<BasicBlock*,Value*> InsertedExprs;
2323 while (!GEPI->use_empty()) {
2324 Instruction *User = cast<Instruction>(GEPI->use_back());
Chris Lattnerc78b0b72005-12-08 08:00:12 +00002325
2326 // If this use is not foldable into the addressing mode, use a version
2327 // emitted in the GEP block.
2328 Value *NewVal;
2329 if (!isa<LoadInst>(User) &&
2330 (!isa<StoreInst>(User) || User->getOperand(0) == GEPI)) {
2331 NewVal = InsertGEPComputeCode(InsertedExprs[DefBB], DefBB, GEPI,
2332 Ptr, PtrOffset);
2333 } else {
2334 // Otherwise, insert the code in the User's block so it can be folded into
2335 // any users in that block.
2336 NewVal = InsertGEPComputeCode(InsertedExprs[User->getParent()],
Chris Lattnerc88d8e92005-12-05 07:10:48 +00002337 User->getParent(), GEPI,
2338 Ptr, PtrOffset);
Chris Lattnerc88d8e92005-12-05 07:10:48 +00002339 }
Chris Lattnerc78b0b72005-12-08 08:00:12 +00002340 User->replaceUsesOfWith(GEPI, NewVal);
2341 }
Chris Lattnerc88d8e92005-12-05 07:10:48 +00002342
2343 // Finally, the GEP is dead, remove it.
2344 GEPI->eraseFromParent();
2345}
2346
Chris Lattner1c08c712005-01-07 07:47:53 +00002347bool SelectionDAGISel::runOnFunction(Function &Fn) {
2348 MachineFunction &MF = MachineFunction::construct(&Fn, TLI.getTargetMachine());
2349 RegMap = MF.getSSARegMap();
2350 DEBUG(std::cerr << "\n\n\n=== " << Fn.getName() << "\n");
2351
Chris Lattnerc88d8e92005-12-05 07:10:48 +00002352 // First, split all critical edges for PHI nodes with incoming values that are
2353 // constants, this way the load of the constant into a vreg will not be placed
2354 // into MBBs that are used some other way.
2355 //
2356 // In this pass we also look for GEP instructions that are used across basic
2357 // blocks and rewrites them to improve basic-block-at-a-time selection.
2358 //
Chris Lattner36b708f2005-08-18 17:35:14 +00002359 for (Function::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
2360 PHINode *PN;
Chris Lattnerc88d8e92005-12-05 07:10:48 +00002361 BasicBlock::iterator BBI;
2362 for (BBI = BB->begin(); (PN = dyn_cast<PHINode>(BBI)); ++BBI)
Chris Lattner36b708f2005-08-18 17:35:14 +00002363 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
2364 if (isa<Constant>(PN->getIncomingValue(i)))
2365 SplitCriticalEdge(PN->getIncomingBlock(i), BB);
Chris Lattnerc88d8e92005-12-05 07:10:48 +00002366
2367 for (BasicBlock::iterator E = BB->end(); BBI != E; )
2368 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(BBI++))
2369 OptimizeGEPExpression(GEPI, TLI.getTargetData());
Chris Lattner36b708f2005-08-18 17:35:14 +00002370 }
Chris Lattnerc9ea6fd2005-11-09 19:44:01 +00002371
Chris Lattner1c08c712005-01-07 07:47:53 +00002372 FunctionLoweringInfo FuncInfo(TLI, Fn, MF);
2373
2374 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
2375 SelectBasicBlock(I, MF, FuncInfo);
Misha Brukmanedf128a2005-04-21 22:36:52 +00002376
Chris Lattner1c08c712005-01-07 07:47:53 +00002377 return true;
2378}
2379
2380
Chris Lattnerddb870b2005-01-13 17:59:43 +00002381SDOperand SelectionDAGISel::
2382CopyValueToVirtualRegister(SelectionDAGLowering &SDL, Value *V, unsigned Reg) {
Chris Lattnerf1fdaca2005-01-11 22:03:46 +00002383 SDOperand Op = SDL.getValue(V);
Chris Lattner18c2f132005-01-13 20:50:02 +00002384 assert((Op.getOpcode() != ISD::CopyFromReg ||
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002385 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
Chris Lattner18c2f132005-01-13 20:50:02 +00002386 "Copy from a reg to the same reg!");
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002387
2388 // If this type is not legal, we must make sure to not create an invalid
2389 // register use.
2390 MVT::ValueType SrcVT = Op.getValueType();
2391 MVT::ValueType DestVT = TLI.getTypeToTransformTo(SrcVT);
2392 SelectionDAG &DAG = SDL.DAG;
2393 if (SrcVT == DestVT) {
2394 return DAG.getCopyToReg(SDL.getRoot(), Reg, Op);
Chris Lattner1c6191f2006-03-21 19:20:37 +00002395 } else if (SrcVT == MVT::Vector) {
2396 // FIXME: THIS DOES NOT SUPPORT PROMOTED/EXPANDED ELEMENTS!
2397
2398 // Figure out the right, legal destination reg to copy into.
2399 const PackedType *PTy = cast<PackedType>(V->getType());
2400 unsigned NumElts = PTy->getNumElements();
2401 MVT::ValueType EltTy = TLI.getValueType(PTy->getElementType());
2402
2403 unsigned NumVectorRegs = 1;
2404
2405 // Divide the input until we get to a supported size. This will always
2406 // end with a scalar if the target doesn't support vectors.
2407 while (NumElts > 1 && !TLI.isTypeLegal(getVectorType(EltTy, NumElts))) {
2408 NumElts >>= 1;
2409 NumVectorRegs <<= 1;
2410 }
2411
2412 MVT::ValueType VT;
2413 if (NumElts == 1)
2414 VT = EltTy;
2415 else
2416 VT = getVectorType(EltTy, NumElts);
2417
2418 // FIXME: THIS ASSUMES THAT THE INPUT VECTOR WILL BE LEGAL!
2419 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
2420 return DAG.getCopyToReg(SDL.getRoot(), Reg, Op);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002421 } else if (SrcVT < DestVT) {
2422 // The src value is promoted to the register.
Chris Lattnerfae59b92005-08-17 06:06:25 +00002423 if (MVT::isFloatingPoint(SrcVT))
2424 Op = DAG.getNode(ISD::FP_EXTEND, DestVT, Op);
2425 else
Chris Lattnerfab08872005-09-02 00:19:37 +00002426 Op = DAG.getNode(ISD::ANY_EXTEND, DestVT, Op);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002427 return DAG.getCopyToReg(SDL.getRoot(), Reg, Op);
2428 } else {
2429 // The src value is expanded into multiple registers.
2430 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DestVT,
2431 Op, DAG.getConstant(0, MVT::i32));
2432 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DestVT,
2433 Op, DAG.getConstant(1, MVT::i32));
2434 Op = DAG.getCopyToReg(SDL.getRoot(), Reg, Lo);
2435 return DAG.getCopyToReg(Op, Reg+1, Hi);
2436 }
Chris Lattner1c08c712005-01-07 07:47:53 +00002437}
2438
Chris Lattner068a81e2005-01-17 17:15:02 +00002439void SelectionDAGISel::
2440LowerArguments(BasicBlock *BB, SelectionDAGLowering &SDL,
2441 std::vector<SDOperand> &UnorderedChains) {
2442 // If this is the entry block, emit arguments.
2443 Function &F = *BB->getParent();
Chris Lattner0afa8e32005-01-17 17:55:19 +00002444 FunctionLoweringInfo &FuncInfo = SDL.FuncInfo;
Chris Lattnerbf209482005-10-30 19:42:35 +00002445 SDOperand OldRoot = SDL.DAG.getRoot();
2446 std::vector<SDOperand> Args = TLI.LowerArguments(F, SDL.DAG);
Chris Lattner068a81e2005-01-17 17:15:02 +00002447
Chris Lattnerbf209482005-10-30 19:42:35 +00002448 unsigned a = 0;
2449 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
2450 AI != E; ++AI, ++a)
2451 if (!AI->use_empty()) {
2452 SDL.setValue(AI, Args[a]);
Chris Lattnerfa577022005-09-13 19:30:54 +00002453
Chris Lattnerbf209482005-10-30 19:42:35 +00002454 // If this argument is live outside of the entry block, insert a copy from
2455 // whereever we got it to the vreg that other BB's will reference it as.
2456 if (FuncInfo.ValueMap.count(AI)) {
2457 SDOperand Copy =
2458 CopyValueToVirtualRegister(SDL, AI, FuncInfo.ValueMap[AI]);
2459 UnorderedChains.push_back(Copy);
2460 }
Chris Lattner0afa8e32005-01-17 17:55:19 +00002461 }
Chris Lattnerbf209482005-10-30 19:42:35 +00002462
2463 // Next, if the function has live ins that need to be copied into vregs,
2464 // emit the copies now, into the top of the block.
2465 MachineFunction &MF = SDL.DAG.getMachineFunction();
2466 if (MF.livein_begin() != MF.livein_end()) {
2467 SSARegMap *RegMap = MF.getSSARegMap();
2468 const MRegisterInfo &MRI = *MF.getTarget().getRegisterInfo();
2469 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
2470 E = MF.livein_end(); LI != E; ++LI)
2471 if (LI->second)
2472 MRI.copyRegToReg(*MF.begin(), MF.begin()->end(), LI->second,
2473 LI->first, RegMap->getRegClass(LI->second));
Chris Lattner068a81e2005-01-17 17:15:02 +00002474 }
Chris Lattnerbf209482005-10-30 19:42:35 +00002475
2476 // Finally, if the target has anything special to do, allow it to do so.
2477 EmitFunctionEntryCode(F, SDL.DAG.getMachineFunction());
Chris Lattner068a81e2005-01-17 17:15:02 +00002478}
2479
2480
Chris Lattner1c08c712005-01-07 07:47:53 +00002481void SelectionDAGISel::BuildSelectionDAG(SelectionDAG &DAG, BasicBlock *LLVMBB,
2482 std::vector<std::pair<MachineInstr*, unsigned> > &PHINodesToUpdate,
2483 FunctionLoweringInfo &FuncInfo) {
2484 SelectionDAGLowering SDL(DAG, TLI, FuncInfo);
Chris Lattnerddb870b2005-01-13 17:59:43 +00002485
2486 std::vector<SDOperand> UnorderedChains;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002487
Chris Lattnerbf209482005-10-30 19:42:35 +00002488 // Lower any arguments needed in this block if this is the entry block.
2489 if (LLVMBB == &LLVMBB->getParent()->front())
2490 LowerArguments(LLVMBB, SDL, UnorderedChains);
Chris Lattner1c08c712005-01-07 07:47:53 +00002491
2492 BB = FuncInfo.MBBMap[LLVMBB];
2493 SDL.setCurrentBasicBlock(BB);
2494
2495 // Lower all of the non-terminator instructions.
2496 for (BasicBlock::iterator I = LLVMBB->begin(), E = --LLVMBB->end();
2497 I != E; ++I)
2498 SDL.visit(*I);
2499
2500 // Ensure that all instructions which are used outside of their defining
2501 // blocks are available as virtual registers.
2502 for (BasicBlock::iterator I = LLVMBB->begin(), E = LLVMBB->end(); I != E;++I)
Chris Lattnerf1fdaca2005-01-11 22:03:46 +00002503 if (!I->use_empty() && !isa<PHINode>(I)) {
Chris Lattneree749d72005-01-09 01:16:24 +00002504 std::map<const Value*, unsigned>::iterator VMI =FuncInfo.ValueMap.find(I);
Chris Lattner1c08c712005-01-07 07:47:53 +00002505 if (VMI != FuncInfo.ValueMap.end())
Chris Lattnerddb870b2005-01-13 17:59:43 +00002506 UnorderedChains.push_back(
2507 CopyValueToVirtualRegister(SDL, I, VMI->second));
Chris Lattner1c08c712005-01-07 07:47:53 +00002508 }
2509
2510 // Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
2511 // ensure constants are generated when needed. Remember the virtual registers
2512 // that need to be added to the Machine PHI nodes as input. We cannot just
2513 // directly add them, because expansion might result in multiple MBB's for one
2514 // BB. As such, the start of the BB might correspond to a different MBB than
2515 // the end.
Misha Brukmanedf128a2005-04-21 22:36:52 +00002516 //
Chris Lattner1c08c712005-01-07 07:47:53 +00002517
2518 // Emit constants only once even if used by multiple PHI nodes.
2519 std::map<Constant*, unsigned> ConstantsOut;
2520
2521 // Check successor nodes PHI nodes that expect a constant to be available from
2522 // this block.
2523 TerminatorInst *TI = LLVMBB->getTerminator();
2524 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
2525 BasicBlock *SuccBB = TI->getSuccessor(succ);
2526 MachineBasicBlock::iterator MBBI = FuncInfo.MBBMap[SuccBB]->begin();
2527 PHINode *PN;
2528
2529 // At this point we know that there is a 1-1 correspondence between LLVM PHI
2530 // nodes and Machine PHI nodes, but the incoming operands have not been
2531 // emitted yet.
2532 for (BasicBlock::iterator I = SuccBB->begin();
Chris Lattnerf44fd882005-01-07 21:34:19 +00002533 (PN = dyn_cast<PHINode>(I)); ++I)
2534 if (!PN->use_empty()) {
2535 unsigned Reg;
2536 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
2537 if (Constant *C = dyn_cast<Constant>(PHIOp)) {
2538 unsigned &RegOut = ConstantsOut[C];
2539 if (RegOut == 0) {
2540 RegOut = FuncInfo.CreateRegForValue(C);
Chris Lattnerddb870b2005-01-13 17:59:43 +00002541 UnorderedChains.push_back(
2542 CopyValueToVirtualRegister(SDL, C, RegOut));
Chris Lattnerf44fd882005-01-07 21:34:19 +00002543 }
2544 Reg = RegOut;
2545 } else {
2546 Reg = FuncInfo.ValueMap[PHIOp];
Chris Lattneree749d72005-01-09 01:16:24 +00002547 if (Reg == 0) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002548 assert(isa<AllocaInst>(PHIOp) &&
Chris Lattneree749d72005-01-09 01:16:24 +00002549 FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
2550 "Didn't codegen value into a register!??");
2551 Reg = FuncInfo.CreateRegForValue(PHIOp);
Chris Lattnerddb870b2005-01-13 17:59:43 +00002552 UnorderedChains.push_back(
2553 CopyValueToVirtualRegister(SDL, PHIOp, Reg));
Chris Lattneree749d72005-01-09 01:16:24 +00002554 }
Chris Lattner1c08c712005-01-07 07:47:53 +00002555 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002556
Chris Lattnerf44fd882005-01-07 21:34:19 +00002557 // Remember that this register needs to added to the machine PHI node as
2558 // the input for this MBB.
2559 unsigned NumElements =
2560 TLI.getNumElements(TLI.getValueType(PN->getType()));
2561 for (unsigned i = 0, e = NumElements; i != e; ++i)
2562 PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
Chris Lattner1c08c712005-01-07 07:47:53 +00002563 }
Chris Lattner1c08c712005-01-07 07:47:53 +00002564 }
2565 ConstantsOut.clear();
2566
Chris Lattnerddb870b2005-01-13 17:59:43 +00002567 // Turn all of the unordered chains into one factored node.
Chris Lattner5a6c6d92005-01-13 19:53:14 +00002568 if (!UnorderedChains.empty()) {
Chris Lattner7436b572005-11-09 05:03:03 +00002569 SDOperand Root = SDL.getRoot();
2570 if (Root.getOpcode() != ISD::EntryToken) {
2571 unsigned i = 0, e = UnorderedChains.size();
2572 for (; i != e; ++i) {
2573 assert(UnorderedChains[i].Val->getNumOperands() > 1);
2574 if (UnorderedChains[i].Val->getOperand(0) == Root)
2575 break; // Don't add the root if we already indirectly depend on it.
2576 }
2577
2578 if (i == e)
2579 UnorderedChains.push_back(Root);
2580 }
Chris Lattnerddb870b2005-01-13 17:59:43 +00002581 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, UnorderedChains));
2582 }
2583
Chris Lattner1c08c712005-01-07 07:47:53 +00002584 // Lower the terminator after the copies are emitted.
2585 SDL.visit(*LLVMBB->getTerminator());
Chris Lattnera651cf62005-01-17 19:43:36 +00002586
2587 // Make sure the root of the DAG is up-to-date.
2588 DAG.setRoot(SDL.getRoot());
Chris Lattner1c08c712005-01-07 07:47:53 +00002589}
2590
2591void SelectionDAGISel::SelectBasicBlock(BasicBlock *LLVMBB, MachineFunction &MF,
2592 FunctionLoweringInfo &FuncInfo) {
Jim Laskeyb2efb852006-01-04 22:28:25 +00002593 SelectionDAG DAG(TLI, MF, getAnalysisToUpdate<MachineDebugInfo>());
Chris Lattner1c08c712005-01-07 07:47:53 +00002594 CurDAG = &DAG;
2595 std::vector<std::pair<MachineInstr*, unsigned> > PHINodesToUpdate;
2596
2597 // First step, lower LLVM code to some DAG. This DAG may use operations and
2598 // types that are not supported by the target.
2599 BuildSelectionDAG(DAG, LLVMBB, PHINodesToUpdate, FuncInfo);
2600
Chris Lattneraf21d552005-10-10 16:47:10 +00002601 // Run the DAG combiner in pre-legalize mode.
2602 DAG.Combine(false);
Nate Begeman2300f552005-09-07 00:15:36 +00002603
Chris Lattner1c08c712005-01-07 07:47:53 +00002604 DEBUG(std::cerr << "Lowered selection DAG:\n");
2605 DEBUG(DAG.dump());
2606
2607 // Second step, hack on the DAG until it only uses operations and types that
2608 // the target supports.
Chris Lattnerac9dc082005-01-23 04:36:26 +00002609 DAG.Legalize();
Chris Lattner1c08c712005-01-07 07:47:53 +00002610
2611 DEBUG(std::cerr << "Legalized selection DAG:\n");
2612 DEBUG(DAG.dump());
2613
Chris Lattneraf21d552005-10-10 16:47:10 +00002614 // Run the DAG combiner in post-legalize mode.
2615 DAG.Combine(true);
Nate Begeman2300f552005-09-07 00:15:36 +00002616
Evan Chenga9c20912006-01-21 02:32:06 +00002617 if (ViewISelDAGs) DAG.viewGraph();
Chris Lattnerd48050a2005-10-05 06:09:10 +00002618
Chris Lattnera33ef482005-03-30 01:10:47 +00002619 // Third, instruction select all of the operations to machine code, adding the
2620 // code to the MachineBasicBlock.
Chris Lattner1c08c712005-01-07 07:47:53 +00002621 InstructionSelectBasicBlock(DAG);
2622
Chris Lattner1c08c712005-01-07 07:47:53 +00002623 DEBUG(std::cerr << "Selected machine code:\n");
2624 DEBUG(BB->dump());
2625
Chris Lattnera33ef482005-03-30 01:10:47 +00002626 // Next, now that we know what the last MBB the LLVM BB expanded is, update
Chris Lattner1c08c712005-01-07 07:47:53 +00002627 // PHI nodes in successors.
2628 for (unsigned i = 0, e = PHINodesToUpdate.size(); i != e; ++i) {
2629 MachineInstr *PHI = PHINodesToUpdate[i].first;
2630 assert(PHI->getOpcode() == TargetInstrInfo::PHI &&
2631 "This is not a machine PHI node that we are updating!");
2632 PHI->addRegOperand(PHINodesToUpdate[i].second);
2633 PHI->addMachineBasicBlockOperand(BB);
2634 }
Chris Lattnera33ef482005-03-30 01:10:47 +00002635
2636 // Finally, add the CFG edges from the last selected MBB to the successor
2637 // MBBs.
2638 TerminatorInst *TI = LLVMBB->getTerminator();
2639 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) {
2640 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[TI->getSuccessor(i)];
2641 BB->addSuccessor(Succ0MBB);
2642 }
Chris Lattner1c08c712005-01-07 07:47:53 +00002643}
Evan Chenga9c20912006-01-21 02:32:06 +00002644
2645//===----------------------------------------------------------------------===//
2646/// ScheduleAndEmitDAG - Pick a safe ordering and emit instructions for each
2647/// target node in the graph.
2648void SelectionDAGISel::ScheduleAndEmitDAG(SelectionDAG &DAG) {
2649 if (ViewSchedDAGs) DAG.viewGraph();
Evan Cheng4ef10862006-01-23 07:01:07 +00002650 ScheduleDAG *SL = NULL;
2651
2652 switch (ISHeuristic) {
2653 default: assert(0 && "Unrecognized scheduling heuristic");
Evan Cheng3f239522006-01-25 09:12:57 +00002654 case defaultScheduling:
2655 if (TLI.getSchedulingPreference() == TargetLowering::SchedulingForLatency)
2656 SL = createSimpleDAGScheduler(noScheduling, DAG, BB);
2657 else /* TargetLowering::SchedulingForRegPressure */
2658 SL = createBURRListDAGScheduler(DAG, BB);
2659 break;
Evan Cheng4ef10862006-01-23 07:01:07 +00002660 case noScheduling:
Chris Lattner20a49212006-03-10 07:49:12 +00002661 SL = createBFS_DAGScheduler(DAG, BB);
2662 break;
Evan Cheng4ef10862006-01-23 07:01:07 +00002663 case simpleScheduling:
Chris Lattner20a49212006-03-10 07:49:12 +00002664 SL = createSimpleDAGScheduler(false, DAG, BB);
2665 break;
Evan Cheng4ef10862006-01-23 07:01:07 +00002666 case simpleNoItinScheduling:
Chris Lattner20a49212006-03-10 07:49:12 +00002667 SL = createSimpleDAGScheduler(true, DAG, BB);
Evan Cheng4ef10862006-01-23 07:01:07 +00002668 break;
Evan Chengf0f9c902006-01-23 08:26:10 +00002669 case listSchedulingBURR:
2670 SL = createBURRListDAGScheduler(DAG, BB);
Chris Lattnera5de4842006-03-05 21:10:33 +00002671 break;
Chris Lattner03fc53c2006-03-06 00:22:00 +00002672 case listSchedulingTD:
Chris Lattnerb0d21ef2006-03-08 04:25:59 +00002673 SL = createTDListDAGScheduler(DAG, BB, CreateTargetHazardRecognizer());
Chris Lattnera5de4842006-03-05 21:10:33 +00002674 break;
Evan Cheng4ef10862006-01-23 07:01:07 +00002675 }
Chris Lattnera3818e62006-01-21 19:12:11 +00002676 BB = SL->Run();
Evan Chengcccf1232006-02-04 06:49:00 +00002677 delete SL;
Evan Chenga9c20912006-01-21 02:32:06 +00002678}
Chris Lattner0e43f2b2006-02-24 02:13:54 +00002679
Chris Lattnerb0d21ef2006-03-08 04:25:59 +00002680HazardRecognizer *SelectionDAGISel::CreateTargetHazardRecognizer() {
2681 return new HazardRecognizer();
Chris Lattner03fc53c2006-03-06 00:22:00 +00002682}
2683
Chris Lattner0e43f2b2006-02-24 02:13:54 +00002684/// SelectInlineAsmMemoryOperands - Calls to this are automatically generated
2685/// by tblgen. Others should not call it.
2686void SelectionDAGISel::
2687SelectInlineAsmMemoryOperands(std::vector<SDOperand> &Ops, SelectionDAG &DAG) {
2688 std::vector<SDOperand> InOps;
2689 std::swap(InOps, Ops);
2690
2691 Ops.push_back(InOps[0]); // input chain.
2692 Ops.push_back(InOps[1]); // input asm string.
2693
2694 const char *AsmStr = cast<ExternalSymbolSDNode>(InOps[1])->getSymbol();
2695 unsigned i = 2, e = InOps.size();
2696 if (InOps[e-1].getValueType() == MVT::Flag)
2697 --e; // Don't process a flag operand if it is here.
2698
2699 while (i != e) {
2700 unsigned Flags = cast<ConstantSDNode>(InOps[i])->getValue();
2701 if ((Flags & 7) != 4 /*MEM*/) {
2702 // Just skip over this operand, copying the operands verbatim.
2703 Ops.insert(Ops.end(), InOps.begin()+i, InOps.begin()+i+(Flags >> 3) + 1);
2704 i += (Flags >> 3) + 1;
2705 } else {
2706 assert((Flags >> 3) == 1 && "Memory operand with multiple values?");
2707 // Otherwise, this is a memory operand. Ask the target to select it.
2708 std::vector<SDOperand> SelOps;
2709 if (SelectInlineAsmMemoryOperand(InOps[i+1], 'm', SelOps, DAG)) {
2710 std::cerr << "Could not match memory address. Inline asm failure!\n";
2711 exit(1);
2712 }
2713
2714 // Add this to the output node.
2715 Ops.push_back(DAG.getConstant(4/*MEM*/ | (SelOps.size() << 3), MVT::i32));
2716 Ops.insert(Ops.end(), SelOps.begin(), SelOps.end());
2717 i += 2;
2718 }
2719 }
2720
2721 // Add the flag input back if present.
2722 if (e != InOps.size())
2723 Ops.push_back(InOps.back());
2724}