blob: 8f0a916d68dc127a8c6ae2a6a5fd25e1f61162d6 [file] [log] [blame]
Chris Lattner1c08c712005-01-07 07:47:53 +00001//===-- SelectionDAGISel.cpp - Implement the SelectionDAGISel class -------===//
2//
3// 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.
7//
8//===----------------------------------------------------------------------===//
9//
10// This implements the SelectionDAGISel class.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "isel"
15#include "llvm/CodeGen/SelectionDAGISel.h"
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/Instructions.h"
20#include "llvm/Intrinsics.h"
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/MachineInstrBuilder.h"
24#include "llvm/CodeGen/SelectionDAG.h"
25#include "llvm/CodeGen/SSARegMap.h"
26#include "llvm/Target/TargetData.h"
27#include "llvm/Target/TargetFrameInfo.h"
28#include "llvm/Target/TargetInstrInfo.h"
29#include "llvm/Target/TargetLowering.h"
30#include "llvm/Target/TargetMachine.h"
Chris Lattner7944d9d2005-01-12 03:41:21 +000031#include "llvm/Support/CommandLine.h"
Chris Lattner1c08c712005-01-07 07:47:53 +000032#include "llvm/Support/Debug.h"
33#include <map>
34#include <iostream>
35using namespace llvm;
36
Chris Lattner7944d9d2005-01-12 03:41:21 +000037#ifndef _NDEBUG
38static cl::opt<bool>
39ViewDAGs("view-isel-dags", cl::Hidden,
40 cl::desc("Pop up a window to show isel dags as they are selected"));
41#else
42static const bool ViewDAGS = 0;
43#endif
44
Chris Lattner1c08c712005-01-07 07:47:53 +000045namespace llvm {
46 //===--------------------------------------------------------------------===//
47 /// FunctionLoweringInfo - This contains information that is global to a
48 /// function that is used when lowering a region of the function.
Chris Lattnerf26bc8e2005-01-08 19:52:31 +000049 class FunctionLoweringInfo {
50 public:
Chris Lattner1c08c712005-01-07 07:47:53 +000051 TargetLowering &TLI;
52 Function &Fn;
53 MachineFunction &MF;
54 SSARegMap *RegMap;
55
56 FunctionLoweringInfo(TargetLowering &TLI, Function &Fn,MachineFunction &MF);
57
58 /// MBBMap - A mapping from LLVM basic blocks to their machine code entry.
59 std::map<const BasicBlock*, MachineBasicBlock *> MBBMap;
60
61 /// ValueMap - Since we emit code for the function a basic block at a time,
62 /// we must remember which virtual registers hold the values for
63 /// cross-basic-block values.
64 std::map<const Value*, unsigned> ValueMap;
65
66 /// StaticAllocaMap - Keep track of frame indices for fixed sized allocas in
67 /// the entry block. This allows the allocas to be efficiently referenced
68 /// anywhere in the function.
69 std::map<const AllocaInst*, int> StaticAllocaMap;
70
71 unsigned MakeReg(MVT::ValueType VT) {
72 return RegMap->createVirtualRegister(TLI.getRegClassFor(VT));
73 }
74
75 unsigned CreateRegForValue(const Value *V) {
76 MVT::ValueType VT = TLI.getValueType(V->getType());
77 // The common case is that we will only create one register for this
78 // value. If we have that case, create and return the virtual register.
79 unsigned NV = TLI.getNumElements(VT);
80 if (NV == 1) return MakeReg(VT);
81
82 // If this value is represented with multiple target registers, make sure
83 // to create enough consequtive registers of the right (smaller) type.
84 unsigned NT = VT-1; // Find the type to use.
85 while (TLI.getNumElements((MVT::ValueType)NT) != 1)
86 --NT;
87
88 unsigned R = MakeReg((MVT::ValueType)NT);
89 for (unsigned i = 1; i != NV; ++i)
90 MakeReg((MVT::ValueType)NT);
91 return R;
92 }
93
94 unsigned InitializeRegForValue(const Value *V) {
95 unsigned &R = ValueMap[V];
96 assert(R == 0 && "Already initialized this value register!");
97 return R = CreateRegForValue(V);
98 }
99 };
100}
101
102/// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by
103/// PHI nodes or outside of the basic block that defines it.
104static bool isUsedOutsideOfDefiningBlock(Instruction *I) {
105 if (isa<PHINode>(I)) return true;
106 BasicBlock *BB = I->getParent();
107 for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; ++UI)
108 if (cast<Instruction>(*UI)->getParent() != BB || isa<PHINode>(*UI))
109 return true;
110 return false;
111}
112
113FunctionLoweringInfo::FunctionLoweringInfo(TargetLowering &tli,
114 Function &fn, MachineFunction &mf)
115 : TLI(tli), Fn(fn), MF(mf), RegMap(MF.getSSARegMap()) {
116
117 // Initialize the mapping of values to registers. This is only set up for
118 // instruction values that are used outside of the block that defines
119 // them.
120 for (Function::aiterator AI = Fn.abegin(), E = Fn.aend(); AI != E; ++AI)
121 InitializeRegForValue(AI);
122
123 Function::iterator BB = Fn.begin(), E = Fn.end();
124 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
125 if (AllocaInst *AI = dyn_cast<AllocaInst>(I))
126 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(AI->getArraySize())) {
127 const Type *Ty = AI->getAllocatedType();
128 uint64_t TySize = TLI.getTargetData().getTypeSize(Ty);
129 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
130 TySize *= CUI->getValue(); // Get total allocated size.
131 StaticAllocaMap[AI] =
Chris Lattnerf26bc8e2005-01-08 19:52:31 +0000132 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
Chris Lattner1c08c712005-01-07 07:47:53 +0000133 }
134
135 for (; BB != E; ++BB)
Chris Lattnerf26bc8e2005-01-08 19:52:31 +0000136 for (BasicBlock::iterator I = BB->begin(), e = BB->end(); I != e; ++I)
Chris Lattner1c08c712005-01-07 07:47:53 +0000137 if (!I->use_empty() && isUsedOutsideOfDefiningBlock(I))
138 if (!isa<AllocaInst>(I) ||
139 !StaticAllocaMap.count(cast<AllocaInst>(I)))
140 InitializeRegForValue(I);
141
142 // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This
143 // also creates the initial PHI MachineInstrs, though none of the input
144 // operands are populated.
145 for (Function::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
146 MachineBasicBlock *MBB = new MachineBasicBlock(BB);
147 MBBMap[BB] = MBB;
148 MF.getBasicBlockList().push_back(MBB);
149
150 // Create Machine PHI nodes for LLVM PHI nodes, lowering them as
151 // appropriate.
152 PHINode *PN;
153 for (BasicBlock::iterator I = BB->begin();
Chris Lattnerf44fd882005-01-07 21:34:19 +0000154 (PN = dyn_cast<PHINode>(I)); ++I)
155 if (!PN->use_empty()) {
156 unsigned NumElements =
157 TLI.getNumElements(TLI.getValueType(PN->getType()));
158 unsigned PHIReg = ValueMap[PN];
159 assert(PHIReg &&"PHI node does not have an assigned virtual register!");
160 for (unsigned i = 0; i != NumElements; ++i)
161 BuildMI(MBB, TargetInstrInfo::PHI, PN->getNumOperands(), PHIReg+i);
162 }
Chris Lattner1c08c712005-01-07 07:47:53 +0000163 }
164}
165
166
167
168//===----------------------------------------------------------------------===//
169/// SelectionDAGLowering - This is the common target-independent lowering
170/// implementation that is parameterized by a TargetLowering object.
171/// Also, targets can overload any lowering method.
172///
173namespace llvm {
174class SelectionDAGLowering {
175 MachineBasicBlock *CurMBB;
176
177 std::map<const Value*, SDOperand> NodeMap;
178
179public:
180 // TLI - This is information that describes the available target features we
181 // need for lowering. This indicates when operations are unavailable,
182 // implemented with a libcall, etc.
183 TargetLowering &TLI;
184 SelectionDAG &DAG;
185 const TargetData &TD;
186
187 /// FuncInfo - Information about the function as a whole.
188 ///
189 FunctionLoweringInfo &FuncInfo;
190
191 SelectionDAGLowering(SelectionDAG &dag, TargetLowering &tli,
192 FunctionLoweringInfo &funcinfo)
193 : TLI(tli), DAG(dag), TD(DAG.getTarget().getTargetData()),
194 FuncInfo(funcinfo) {
195 }
196
197 void visit(Instruction &I) { visit(I.getOpcode(), I); }
198
199 void visit(unsigned Opcode, User &I) {
200 switch (Opcode) {
201 default: assert(0 && "Unknown instruction type encountered!");
202 abort();
203 // Build the switch statement using the Instruction.def file.
204#define HANDLE_INST(NUM, OPCODE, CLASS) \
205 case Instruction::OPCODE:return visit##OPCODE((CLASS&)I);
206#include "llvm/Instruction.def"
207 }
208 }
209
210 void setCurrentBasicBlock(MachineBasicBlock *MBB) { CurMBB = MBB; }
211
212
213 SDOperand getIntPtrConstant(uint64_t Val) {
214 return DAG.getConstant(Val, TLI.getPointerTy());
215 }
216
217 SDOperand getValue(const Value *V) {
218 SDOperand &N = NodeMap[V];
219 if (N.Val) return N;
220
221 MVT::ValueType VT = TLI.getValueType(V->getType());
222 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V)))
223 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
224 visit(CE->getOpcode(), *CE);
225 assert(N.Val && "visit didn't populate the ValueMap!");
226 return N;
227 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) {
228 return N = DAG.getGlobalAddress(GV, VT);
229 } else if (isa<ConstantPointerNull>(C)) {
230 return N = DAG.getConstant(0, TLI.getPointerTy());
231 } else if (isa<UndefValue>(C)) {
232 /// FIXME: Implement UNDEFVALUE better.
233 if (MVT::isInteger(VT))
234 return N = DAG.getConstant(0, VT);
235 else if (MVT::isFloatingPoint(VT))
236 return N = DAG.getConstantFP(0, VT);
237 else
238 assert(0 && "Unknown value type!");
239
240 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
241 return N = DAG.getConstantFP(CFP->getValue(), VT);
242 } else {
243 // Canonicalize all constant ints to be unsigned.
244 return N = DAG.getConstant(cast<ConstantIntegral>(C)->getRawValue(),VT);
245 }
246
247 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
248 std::map<const AllocaInst*, int>::iterator SI =
249 FuncInfo.StaticAllocaMap.find(AI);
250 if (SI != FuncInfo.StaticAllocaMap.end())
251 return DAG.getFrameIndex(SI->second, TLI.getPointerTy());
252 }
253
254 std::map<const Value*, unsigned>::const_iterator VMI =
255 FuncInfo.ValueMap.find(V);
256 assert(VMI != FuncInfo.ValueMap.end() && "Value not in map!");
257 return N = DAG.getCopyFromReg(VMI->second, VT);
258 }
259
260 const SDOperand &setValue(const Value *V, SDOperand NewN) {
261 SDOperand &N = NodeMap[V];
262 assert(N.Val == 0 && "Already set a value for this node!");
263 return N = NewN;
264 }
265
266 // Terminator instructions.
267 void visitRet(ReturnInst &I);
268 void visitBr(BranchInst &I);
269 void visitUnreachable(UnreachableInst &I) { /* noop */ }
270
271 // These all get lowered before this pass.
272 void visitSwitch(SwitchInst &I) { assert(0 && "TODO"); }
273 void visitInvoke(InvokeInst &I) { assert(0 && "TODO"); }
274 void visitUnwind(UnwindInst &I) { assert(0 && "TODO"); }
275
276 //
277 void visitBinary(User &I, unsigned Opcode);
278 void visitAdd(User &I) { visitBinary(I, ISD::ADD); }
279 void visitSub(User &I) { visitBinary(I, ISD::SUB); }
280 void visitMul(User &I) { visitBinary(I, ISD::MUL); }
281 void visitDiv(User &I) {
282 visitBinary(I, I.getType()->isUnsigned() ? ISD::UDIV : ISD::SDIV);
283 }
284 void visitRem(User &I) {
285 visitBinary(I, I.getType()->isUnsigned() ? ISD::UREM : ISD::SREM);
286 }
287 void visitAnd(User &I) { visitBinary(I, ISD::AND); }
288 void visitOr (User &I) { visitBinary(I, ISD::OR); }
289 void visitXor(User &I) { visitBinary(I, ISD::XOR); }
290 void visitShl(User &I) { visitBinary(I, ISD::SHL); }
291 void visitShr(User &I) {
292 visitBinary(I, I.getType()->isUnsigned() ? ISD::SRL : ISD::SRA);
293 }
294
295 void visitSetCC(User &I, ISD::CondCode SignedOpc, ISD::CondCode UnsignedOpc);
296 void visitSetEQ(User &I) { visitSetCC(I, ISD::SETEQ, ISD::SETEQ); }
297 void visitSetNE(User &I) { visitSetCC(I, ISD::SETNE, ISD::SETNE); }
298 void visitSetLE(User &I) { visitSetCC(I, ISD::SETLE, ISD::SETULE); }
299 void visitSetGE(User &I) { visitSetCC(I, ISD::SETGE, ISD::SETUGE); }
300 void visitSetLT(User &I) { visitSetCC(I, ISD::SETLT, ISD::SETULT); }
301 void visitSetGT(User &I) { visitSetCC(I, ISD::SETGT, ISD::SETUGT); }
302
303 void visitGetElementPtr(User &I);
304 void visitCast(User &I);
305 void visitSelect(User &I);
306 //
307
308 void visitMalloc(MallocInst &I);
309 void visitFree(FreeInst &I);
310 void visitAlloca(AllocaInst &I);
311 void visitLoad(LoadInst &I);
312 void visitStore(StoreInst &I);
313 void visitPHI(PHINode &I) { } // PHI nodes are handled specially.
314 void visitCall(CallInst &I);
315
Chris Lattner1c08c712005-01-07 07:47:53 +0000316 void visitVAStart(CallInst &I);
317 void visitVANext(VANextInst &I);
318 void visitVAArg(VAArgInst &I);
319 void visitVAEnd(CallInst &I);
320 void visitVACopy(CallInst &I);
Chris Lattner39ae3622005-01-09 00:00:49 +0000321 void visitFrameReturnAddress(CallInst &I, bool isFrameAddress);
Chris Lattner1c08c712005-01-07 07:47:53 +0000322
Chris Lattner7041ee32005-01-11 05:56:49 +0000323 void visitMemIntrinsic(CallInst &I, unsigned Op);
Chris Lattner1c08c712005-01-07 07:47:53 +0000324
325 void visitUserOp1(Instruction &I) {
326 assert(0 && "UserOp1 should not exist at instruction selection time!");
327 abort();
328 }
329 void visitUserOp2(Instruction &I) {
330 assert(0 && "UserOp2 should not exist at instruction selection time!");
331 abort();
332 }
333};
334} // end namespace llvm
335
336void SelectionDAGLowering::visitRet(ReturnInst &I) {
337 if (I.getNumOperands() == 0) {
338 DAG.setRoot(DAG.getNode(ISD::RET, MVT::Other, DAG.getRoot()));
339 return;
340 }
341
342 SDOperand Op1 = getValue(I.getOperand(0));
343 switch (Op1.getValueType()) {
344 default: assert(0 && "Unknown value type!");
345 case MVT::i1:
346 case MVT::i8:
347 case MVT::i16:
348 // Extend integer types to 32-bits.
349 if (I.getOperand(0)->getType()->isSigned())
350 Op1 = DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Op1);
351 else
352 Op1 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op1);
353 break;
354 case MVT::f32:
355 // Extend float to double.
356 Op1 = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Op1);
357 break;
358 case MVT::i32:
359 case MVT::i64:
360 case MVT::f64:
361 break; // No extension needed!
362 }
363
364 DAG.setRoot(DAG.getNode(ISD::RET, MVT::Other, DAG.getRoot(), Op1));
365}
366
367void SelectionDAGLowering::visitBr(BranchInst &I) {
368 // Update machine-CFG edges.
369 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
370 CurMBB->addSuccessor(Succ0MBB);
371
372 // Figure out which block is immediately after the current one.
373 MachineBasicBlock *NextBlock = 0;
374 MachineFunction::iterator BBI = CurMBB;
375 if (++BBI != CurMBB->getParent()->end())
376 NextBlock = BBI;
377
378 if (I.isUnconditional()) {
379 // If this is not a fall-through branch, emit the branch.
380 if (Succ0MBB != NextBlock)
381 DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, DAG.getRoot(),
382 DAG.getBasicBlock(Succ0MBB)));
383 } else {
384 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
385 CurMBB->addSuccessor(Succ1MBB);
386
387 SDOperand Cond = getValue(I.getCondition());
388
389 if (Succ1MBB == NextBlock) {
390 // If the condition is false, fall through. This means we should branch
391 // if the condition is true to Succ #0.
392 DAG.setRoot(DAG.getNode(ISD::BRCOND, MVT::Other, DAG.getRoot(),
393 Cond, DAG.getBasicBlock(Succ0MBB)));
394 } else if (Succ0MBB == NextBlock) {
395 // If the condition is true, fall through. This means we should branch if
396 // the condition is false to Succ #1. Invert the condition first.
397 SDOperand True = DAG.getConstant(1, Cond.getValueType());
398 Cond = DAG.getNode(ISD::XOR, Cond.getValueType(), Cond, True);
399 DAG.setRoot(DAG.getNode(ISD::BRCOND, MVT::Other, DAG.getRoot(),
400 Cond, DAG.getBasicBlock(Succ1MBB)));
401 } else {
402 // Neither edge is a fall through. If the comparison is true, jump to
403 // Succ#0, otherwise branch unconditionally to succ #1.
404 DAG.setRoot(DAG.getNode(ISD::BRCOND, MVT::Other, DAG.getRoot(),
405 Cond, DAG.getBasicBlock(Succ0MBB)));
406 DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, DAG.getRoot(),
407 DAG.getBasicBlock(Succ1MBB)));
408 }
409 }
410}
411
412void SelectionDAGLowering::visitBinary(User &I, unsigned Opcode) {
413 SDOperand Op1 = getValue(I.getOperand(0));
414 SDOperand Op2 = getValue(I.getOperand(1));
415 setValue(&I, DAG.getNode(Opcode, Op1.getValueType(), Op1, Op2));
416}
417
418void SelectionDAGLowering::visitSetCC(User &I,ISD::CondCode SignedOpcode,
419 ISD::CondCode UnsignedOpcode) {
420 SDOperand Op1 = getValue(I.getOperand(0));
421 SDOperand Op2 = getValue(I.getOperand(1));
422 ISD::CondCode Opcode = SignedOpcode;
423 if (I.getOperand(0)->getType()->isUnsigned())
424 Opcode = UnsignedOpcode;
425 setValue(&I, DAG.getSetCC(Opcode, Op1, Op2));
426}
427
428void SelectionDAGLowering::visitSelect(User &I) {
429 SDOperand Cond = getValue(I.getOperand(0));
430 SDOperand TrueVal = getValue(I.getOperand(1));
431 SDOperand FalseVal = getValue(I.getOperand(2));
432 setValue(&I, DAG.getNode(ISD::SELECT, TrueVal.getValueType(), Cond,
433 TrueVal, FalseVal));
434}
435
436void SelectionDAGLowering::visitCast(User &I) {
437 SDOperand N = getValue(I.getOperand(0));
438 MVT::ValueType SrcTy = TLI.getValueType(I.getOperand(0)->getType());
439 MVT::ValueType DestTy = TLI.getValueType(I.getType());
440
441 if (N.getValueType() == DestTy) {
442 setValue(&I, N); // noop cast.
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000443 } else if (isInteger(SrcTy)) {
444 if (isInteger(DestTy)) { // Int -> Int cast
445 if (DestTy < SrcTy) // Truncating cast?
446 setValue(&I, DAG.getNode(ISD::TRUNCATE, DestTy, N));
447 else if (I.getOperand(0)->getType()->isSigned())
448 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, DestTy, N));
449 else
450 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, DestTy, N));
451 } else { // Int -> FP cast
452 if (I.getOperand(0)->getType()->isSigned())
453 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, DestTy, N));
454 else
455 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, DestTy, N));
456 }
Chris Lattner1c08c712005-01-07 07:47:53 +0000457 } else {
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000458 assert(isFloatingPoint(SrcTy) && "Unknown value type!");
459 if (isFloatingPoint(DestTy)) { // FP -> FP cast
460 if (DestTy < SrcTy) // Rounding cast?
461 setValue(&I, DAG.getNode(ISD::FP_ROUND, DestTy, N));
462 else
463 setValue(&I, DAG.getNode(ISD::FP_EXTEND, DestTy, N));
464 } else { // FP -> Int cast.
465 if (I.getType()->isSigned())
466 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, DestTy, N));
467 else
468 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, DestTy, N));
469 }
Chris Lattner1c08c712005-01-07 07:47:53 +0000470 }
471}
472
473void SelectionDAGLowering::visitGetElementPtr(User &I) {
474 SDOperand N = getValue(I.getOperand(0));
475 const Type *Ty = I.getOperand(0)->getType();
476 const Type *UIntPtrTy = TD.getIntPtrType();
477
478 for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end();
479 OI != E; ++OI) {
480 Value *Idx = *OI;
481 if (const StructType *StTy = dyn_cast<StructType> (Ty)) {
482 unsigned Field = cast<ConstantUInt>(Idx)->getValue();
483 if (Field) {
484 // N = N + Offset
485 uint64_t Offset = TD.getStructLayout(StTy)->MemberOffsets[Field];
486 N = DAG.getNode(ISD::ADD, N.getValueType(), N,
487 getIntPtrConstant(Offset));
488 }
489 Ty = StTy->getElementType(Field);
490 } else {
491 Ty = cast<SequentialType>(Ty)->getElementType();
492 if (!isa<Constant>(Idx) || !cast<Constant>(Idx)->isNullValue()) {
493 // N = N + Idx * ElementSize;
494 uint64_t ElementSize = TD.getTypeSize(Ty);
Chris Lattner7cc47772005-01-07 21:56:57 +0000495 SDOperand IdxN = getValue(Idx), Scale = getIntPtrConstant(ElementSize);
496
497 // If the index is smaller or larger than intptr_t, truncate or extend
498 // it.
499 if (IdxN.getValueType() < Scale.getValueType()) {
500 if (Idx->getType()->isSigned())
501 IdxN = DAG.getNode(ISD::SIGN_EXTEND, Scale.getValueType(), IdxN);
502 else
503 IdxN = DAG.getNode(ISD::ZERO_EXTEND, Scale.getValueType(), IdxN);
504 } else if (IdxN.getValueType() > Scale.getValueType())
505 IdxN = DAG.getNode(ISD::TRUNCATE, Scale.getValueType(), IdxN);
506
507 IdxN = DAG.getNode(ISD::MUL, N.getValueType(), IdxN, Scale);
508
Chris Lattner1c08c712005-01-07 07:47:53 +0000509 N = DAG.getNode(ISD::ADD, N.getValueType(), N, IdxN);
510 }
511 }
512 }
513 setValue(&I, N);
514}
515
516void SelectionDAGLowering::visitAlloca(AllocaInst &I) {
517 // If this is a fixed sized alloca in the entry block of the function,
518 // allocate it statically on the stack.
519 if (FuncInfo.StaticAllocaMap.count(&I))
520 return; // getValue will auto-populate this.
521
522 const Type *Ty = I.getAllocatedType();
523 uint64_t TySize = TLI.getTargetData().getTypeSize(Ty);
524 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
525
526 SDOperand AllocSize = getValue(I.getArraySize());
527
528 assert(AllocSize.getValueType() == TLI.getPointerTy() &&
529 "FIXME: should extend or truncate to pointer size!");
530
531 AllocSize = DAG.getNode(ISD::MUL, TLI.getPointerTy(), AllocSize,
532 getIntPtrConstant(TySize));
533
534 // Handle alignment. If the requested alignment is less than or equal to the
535 // stack alignment, ignore it and round the size of the allocation up to the
536 // stack alignment size. If the size is greater than the stack alignment, we
537 // note this in the DYNAMIC_STACKALLOC node.
538 unsigned StackAlign =
539 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
540 if (Align <= StackAlign) {
541 Align = 0;
542 // Add SA-1 to the size.
543 AllocSize = DAG.getNode(ISD::ADD, AllocSize.getValueType(), AllocSize,
544 getIntPtrConstant(StackAlign-1));
545 // Mask out the low bits for alignment purposes.
546 AllocSize = DAG.getNode(ISD::AND, AllocSize.getValueType(), AllocSize,
547 getIntPtrConstant(~(uint64_t)(StackAlign-1)));
548 }
549
550 SDOperand DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, AllocSize.getValueType(),
551 DAG.getRoot(), AllocSize,
552 getIntPtrConstant(Align));
553 DAG.setRoot(setValue(&I, DSA).getValue(1));
554
555 // Inform the Frame Information that we have just allocated a variable-sized
556 // object.
557 CurMBB->getParent()->getFrameInfo()->CreateVariableSizedObject();
558}
559
560
561void SelectionDAGLowering::visitLoad(LoadInst &I) {
562 SDOperand Ptr = getValue(I.getOperand(0));
563 SDOperand L = DAG.getLoad(TLI.getValueType(I.getType()), DAG.getRoot(), Ptr);
564 DAG.setRoot(setValue(&I, L).getValue(1));
565}
566
567
568void SelectionDAGLowering::visitStore(StoreInst &I) {
569 Value *SrcV = I.getOperand(0);
570 SDOperand Src = getValue(SrcV);
571 SDOperand Ptr = getValue(I.getOperand(1));
572 DAG.setRoot(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), Src, Ptr));
573 return;
574}
575
576void SelectionDAGLowering::visitCall(CallInst &I) {
Chris Lattner64e14b12005-01-08 22:48:57 +0000577 const char *RenameFn = 0;
Chris Lattner1c08c712005-01-07 07:47:53 +0000578 if (Function *F = I.getCalledFunction())
579 switch (F->getIntrinsicID()) {
580 case 0: break; // Not an intrinsic.
581 case Intrinsic::vastart: visitVAStart(I); return;
582 case Intrinsic::vaend: visitVAEnd(I); return;
583 case Intrinsic::vacopy: visitVACopy(I); return;
Chris Lattner39ae3622005-01-09 00:00:49 +0000584 case Intrinsic::returnaddress: visitFrameReturnAddress(I, false); return;
585 case Intrinsic::frameaddress: visitFrameReturnAddress(I, true); return;
Chris Lattner1c08c712005-01-07 07:47:53 +0000586 default:
587 // FIXME: IMPLEMENT THESE.
588 // readport, writeport, readio, writeio
589 assert(0 && "This intrinsic is not implemented yet!");
590 return;
Chris Lattner64e14b12005-01-08 22:48:57 +0000591 case Intrinsic::setjmp: RenameFn = "setjmp"; break;
592 case Intrinsic::longjmp: RenameFn = "longjmp"; break;
Chris Lattner7041ee32005-01-11 05:56:49 +0000593 case Intrinsic::memcpy: visitMemIntrinsic(I, ISD::MEMCPY); return;
594 case Intrinsic::memset: visitMemIntrinsic(I, ISD::MEMSET); return;
595 case Intrinsic::memmove: visitMemIntrinsic(I, ISD::MEMMOVE); return;
Chris Lattner1c08c712005-01-07 07:47:53 +0000596
597 case Intrinsic::isunordered:
598 setValue(&I, DAG.getSetCC(ISD::SETUO, getValue(I.getOperand(1)),
599 getValue(I.getOperand(2))));
600 return;
601 }
602
Chris Lattner64e14b12005-01-08 22:48:57 +0000603 SDOperand Callee;
604 if (!RenameFn)
605 Callee = getValue(I.getOperand(0));
606 else
607 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Chris Lattner1c08c712005-01-07 07:47:53 +0000608 std::vector<std::pair<SDOperand, const Type*> > Args;
609
610 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
611 Value *Arg = I.getOperand(i);
612 SDOperand ArgNode = getValue(Arg);
613 Args.push_back(std::make_pair(ArgNode, Arg->getType()));
614 }
615
Chris Lattnercf5734d2005-01-08 19:26:18 +0000616 std::pair<SDOperand,SDOperand> Result =
617 TLI.LowerCallTo(DAG.getRoot(), I.getType(), Callee, Args, DAG);
Chris Lattner1c08c712005-01-07 07:47:53 +0000618 if (I.getType() != Type::VoidTy)
Chris Lattnercf5734d2005-01-08 19:26:18 +0000619 setValue(&I, Result.first);
620 DAG.setRoot(Result.second);
Chris Lattner1c08c712005-01-07 07:47:53 +0000621}
622
623void SelectionDAGLowering::visitMalloc(MallocInst &I) {
624 SDOperand Src = getValue(I.getOperand(0));
625
626 MVT::ValueType IntPtr = TLI.getPointerTy();
627 // FIXME: Extend or truncate to the intptr size.
628 assert(Src.getValueType() == IntPtr && "Need to adjust the amount!");
629
630 // Scale the source by the type size.
631 uint64_t ElementSize = TD.getTypeSize(I.getType()->getElementType());
632 Src = DAG.getNode(ISD::MUL, Src.getValueType(),
633 Src, getIntPtrConstant(ElementSize));
634
635 std::vector<std::pair<SDOperand, const Type*> > Args;
636 Args.push_back(std::make_pair(Src, TLI.getTargetData().getIntPtrType()));
Chris Lattnercf5734d2005-01-08 19:26:18 +0000637
638 std::pair<SDOperand,SDOperand> Result =
639 TLI.LowerCallTo(DAG.getRoot(), I.getType(),
640 DAG.getExternalSymbol("malloc", IntPtr),
641 Args, DAG);
642 setValue(&I, Result.first); // Pointers always fit in registers
643 DAG.setRoot(Result.second);
Chris Lattner1c08c712005-01-07 07:47:53 +0000644}
645
646void SelectionDAGLowering::visitFree(FreeInst &I) {
647 std::vector<std::pair<SDOperand, const Type*> > Args;
648 Args.push_back(std::make_pair(getValue(I.getOperand(0)),
649 TLI.getTargetData().getIntPtrType()));
650 MVT::ValueType IntPtr = TLI.getPointerTy();
Chris Lattnercf5734d2005-01-08 19:26:18 +0000651 std::pair<SDOperand,SDOperand> Result =
652 TLI.LowerCallTo(DAG.getRoot(), Type::VoidTy,
653 DAG.getExternalSymbol("free", IntPtr), Args, DAG);
654 DAG.setRoot(Result.second);
Chris Lattner1c08c712005-01-07 07:47:53 +0000655}
656
Chris Lattner39ae3622005-01-09 00:00:49 +0000657std::pair<SDOperand, SDOperand>
658TargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
Chris Lattner1c08c712005-01-07 07:47:53 +0000659 // We have no sane default behavior, just emit a useful error message and bail
660 // out.
Chris Lattner39ae3622005-01-09 00:00:49 +0000661 std::cerr << "Variable arguments handling not implemented on this target!\n";
Chris Lattner1c08c712005-01-07 07:47:53 +0000662 abort();
663}
664
Chris Lattner39ae3622005-01-09 00:00:49 +0000665SDOperand TargetLowering::LowerVAEnd(SDOperand Chain, SDOperand L,
666 SelectionDAG &DAG) {
667 // Default to a noop.
668 return Chain;
669}
670
671std::pair<SDOperand,SDOperand>
672TargetLowering::LowerVACopy(SDOperand Chain, SDOperand L, SelectionDAG &DAG) {
673 // Default to returning the input list.
674 return std::make_pair(L, Chain);
675}
676
677std::pair<SDOperand,SDOperand>
678TargetLowering::LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
679 const Type *ArgTy, SelectionDAG &DAG) {
680 // We have no sane default behavior, just emit a useful error message and bail
681 // out.
682 std::cerr << "Variable arguments handling not implemented on this target!\n";
683 abort();
684}
685
686
687void SelectionDAGLowering::visitVAStart(CallInst &I) {
688 std::pair<SDOperand,SDOperand> Result = TLI.LowerVAStart(DAG.getRoot(), DAG);
689 setValue(&I, Result.first);
690 DAG.setRoot(Result.second);
691}
692
693void SelectionDAGLowering::visitVAArg(VAArgInst &I) {
694 std::pair<SDOperand,SDOperand> Result =
695 TLI.LowerVAArgNext(false, DAG.getRoot(), getValue(I.getOperand(0)),
696 I.getType(), DAG);
697 setValue(&I, Result.first);
698 DAG.setRoot(Result.second);
699}
700
Chris Lattner1c08c712005-01-07 07:47:53 +0000701void SelectionDAGLowering::visitVANext(VANextInst &I) {
Chris Lattner39ae3622005-01-09 00:00:49 +0000702 std::pair<SDOperand,SDOperand> Result =
703 TLI.LowerVAArgNext(true, DAG.getRoot(), getValue(I.getOperand(0)),
704 I.getArgType(), DAG);
705 setValue(&I, Result.first);
706 DAG.setRoot(Result.second);
Chris Lattner1c08c712005-01-07 07:47:53 +0000707}
708
709void SelectionDAGLowering::visitVAEnd(CallInst &I) {
Chris Lattner39ae3622005-01-09 00:00:49 +0000710 DAG.setRoot(TLI.LowerVAEnd(DAG.getRoot(), getValue(I.getOperand(1)), DAG));
Chris Lattner1c08c712005-01-07 07:47:53 +0000711}
712
713void SelectionDAGLowering::visitVACopy(CallInst &I) {
Chris Lattner39ae3622005-01-09 00:00:49 +0000714 std::pair<SDOperand,SDOperand> Result =
715 TLI.LowerVACopy(DAG.getRoot(), getValue(I.getOperand(1)), DAG);
716 setValue(&I, Result.first);
717 DAG.setRoot(Result.second);
Chris Lattner1c08c712005-01-07 07:47:53 +0000718}
719
Chris Lattner39ae3622005-01-09 00:00:49 +0000720
721// It is always conservatively correct for llvm.returnaddress and
722// llvm.frameaddress to return 0.
723std::pair<SDOperand, SDOperand>
724TargetLowering::LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain,
725 unsigned Depth, SelectionDAG &DAG) {
726 return std::make_pair(DAG.getConstant(0, getPointerTy()), Chain);
Chris Lattner1c08c712005-01-07 07:47:53 +0000727}
728
Chris Lattner39ae3622005-01-09 00:00:49 +0000729void SelectionDAGLowering::visitFrameReturnAddress(CallInst &I, bool isFrame) {
730 unsigned Depth = (unsigned)cast<ConstantUInt>(I.getOperand(1))->getValue();
731 std::pair<SDOperand,SDOperand> Result =
732 TLI.LowerFrameReturnAddress(isFrame, DAG.getRoot(), Depth, DAG);
733 setValue(&I, Result.first);
734 DAG.setRoot(Result.second);
Chris Lattner1c08c712005-01-07 07:47:53 +0000735}
736
Chris Lattner7041ee32005-01-11 05:56:49 +0000737void SelectionDAGLowering::visitMemIntrinsic(CallInst &I, unsigned Op) {
738 std::vector<SDOperand> Ops;
739 Ops.push_back(DAG.getRoot());
740 Ops.push_back(getValue(I.getOperand(1)));
741 Ops.push_back(getValue(I.getOperand(2)));
742 Ops.push_back(getValue(I.getOperand(3)));
743 Ops.push_back(getValue(I.getOperand(4)));
744 DAG.setRoot(DAG.getNode(Op, MVT::Other, Ops));
Chris Lattner1c08c712005-01-07 07:47:53 +0000745}
746
Chris Lattner7041ee32005-01-11 05:56:49 +0000747//===----------------------------------------------------------------------===//
748// SelectionDAGISel code
749//===----------------------------------------------------------------------===//
Chris Lattner1c08c712005-01-07 07:47:53 +0000750
751unsigned SelectionDAGISel::MakeReg(MVT::ValueType VT) {
752 return RegMap->createVirtualRegister(TLI.getRegClassFor(VT));
753}
754
755
756
757bool SelectionDAGISel::runOnFunction(Function &Fn) {
758 MachineFunction &MF = MachineFunction::construct(&Fn, TLI.getTargetMachine());
759 RegMap = MF.getSSARegMap();
760 DEBUG(std::cerr << "\n\n\n=== " << Fn.getName() << "\n");
761
762 FunctionLoweringInfo FuncInfo(TLI, Fn, MF);
763
764 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
765 SelectBasicBlock(I, MF, FuncInfo);
766
767 return true;
768}
769
770
Chris Lattnerddb870b2005-01-13 17:59:43 +0000771SDOperand SelectionDAGISel::
772CopyValueToVirtualRegister(SelectionDAGLowering &SDL, Value *V, unsigned Reg) {
Chris Lattner1c08c712005-01-07 07:47:53 +0000773 SelectionDAG &DAG = SDL.DAG;
Chris Lattnerf1fdaca2005-01-11 22:03:46 +0000774 SDOperand Op = SDL.getValue(V);
775 if (CopyRegSDNode *CR = dyn_cast<CopyRegSDNode>(Op))
776 assert(CR->getReg() != Reg && "Copy from a reg to the same reg!");
Chris Lattnerddb870b2005-01-13 17:59:43 +0000777 return DAG.getCopyToReg(DAG.getRoot(), Op, Reg);
Chris Lattner1c08c712005-01-07 07:47:53 +0000778}
779
780void SelectionDAGISel::BuildSelectionDAG(SelectionDAG &DAG, BasicBlock *LLVMBB,
781 std::vector<std::pair<MachineInstr*, unsigned> > &PHINodesToUpdate,
782 FunctionLoweringInfo &FuncInfo) {
783 SelectionDAGLowering SDL(DAG, TLI, FuncInfo);
Chris Lattnerddb870b2005-01-13 17:59:43 +0000784
785 std::vector<SDOperand> UnorderedChains;
Chris Lattner1c08c712005-01-07 07:47:53 +0000786
787 // If this is the entry block, emit arguments.
788 Function *F = LLVMBB->getParent();
789 if (LLVMBB == &F->front()) {
790 // FIXME: If an argument is only used in one basic block, we could directly
791 // emit it (ONLY) into that block, not emitting the COPY_TO_VREG node. This
792 // would improve codegen in several cases on X86 by allowing the loads to be
793 // folded into the user operation.
794 std::vector<SDOperand> Args = TLI.LowerArguments(*LLVMBB->getParent(), DAG);
795
796 unsigned a = 0;
797 for (Function::aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI,++a)
798 if (!AI->use_empty()) {
799 SDL.setValue(AI, Args[a]);
Chris Lattnerddb870b2005-01-13 17:59:43 +0000800 UnorderedChains.push_back(
801 CopyValueToVirtualRegister(SDL, AI, FuncInfo.ValueMap[AI]));
Chris Lattner1c08c712005-01-07 07:47:53 +0000802 }
803 }
804
805 BB = FuncInfo.MBBMap[LLVMBB];
806 SDL.setCurrentBasicBlock(BB);
807
808 // Lower all of the non-terminator instructions.
809 for (BasicBlock::iterator I = LLVMBB->begin(), E = --LLVMBB->end();
810 I != E; ++I)
811 SDL.visit(*I);
812
813 // Ensure that all instructions which are used outside of their defining
814 // blocks are available as virtual registers.
815 for (BasicBlock::iterator I = LLVMBB->begin(), E = LLVMBB->end(); I != E;++I)
Chris Lattnerf1fdaca2005-01-11 22:03:46 +0000816 if (!I->use_empty() && !isa<PHINode>(I)) {
Chris Lattneree749d72005-01-09 01:16:24 +0000817 std::map<const Value*, unsigned>::iterator VMI =FuncInfo.ValueMap.find(I);
Chris Lattner1c08c712005-01-07 07:47:53 +0000818 if (VMI != FuncInfo.ValueMap.end())
Chris Lattnerddb870b2005-01-13 17:59:43 +0000819 UnorderedChains.push_back(
820 CopyValueToVirtualRegister(SDL, I, VMI->second));
Chris Lattner1c08c712005-01-07 07:47:53 +0000821 }
822
823 // Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
824 // ensure constants are generated when needed. Remember the virtual registers
825 // that need to be added to the Machine PHI nodes as input. We cannot just
826 // directly add them, because expansion might result in multiple MBB's for one
827 // BB. As such, the start of the BB might correspond to a different MBB than
828 // the end.
829 //
830
831 // Emit constants only once even if used by multiple PHI nodes.
832 std::map<Constant*, unsigned> ConstantsOut;
833
834 // Check successor nodes PHI nodes that expect a constant to be available from
835 // this block.
836 TerminatorInst *TI = LLVMBB->getTerminator();
837 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
838 BasicBlock *SuccBB = TI->getSuccessor(succ);
839 MachineBasicBlock::iterator MBBI = FuncInfo.MBBMap[SuccBB]->begin();
840 PHINode *PN;
841
842 // At this point we know that there is a 1-1 correspondence between LLVM PHI
843 // nodes and Machine PHI nodes, but the incoming operands have not been
844 // emitted yet.
845 for (BasicBlock::iterator I = SuccBB->begin();
Chris Lattnerf44fd882005-01-07 21:34:19 +0000846 (PN = dyn_cast<PHINode>(I)); ++I)
847 if (!PN->use_empty()) {
848 unsigned Reg;
849 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
850 if (Constant *C = dyn_cast<Constant>(PHIOp)) {
851 unsigned &RegOut = ConstantsOut[C];
852 if (RegOut == 0) {
853 RegOut = FuncInfo.CreateRegForValue(C);
Chris Lattnerddb870b2005-01-13 17:59:43 +0000854 UnorderedChains.push_back(
855 CopyValueToVirtualRegister(SDL, C, RegOut));
Chris Lattnerf44fd882005-01-07 21:34:19 +0000856 }
857 Reg = RegOut;
858 } else {
859 Reg = FuncInfo.ValueMap[PHIOp];
Chris Lattneree749d72005-01-09 01:16:24 +0000860 if (Reg == 0) {
861 assert(isa<AllocaInst>(PHIOp) &&
862 FuncInfo.StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
863 "Didn't codegen value into a register!??");
864 Reg = FuncInfo.CreateRegForValue(PHIOp);
Chris Lattnerddb870b2005-01-13 17:59:43 +0000865 UnorderedChains.push_back(
866 CopyValueToVirtualRegister(SDL, PHIOp, Reg));
Chris Lattneree749d72005-01-09 01:16:24 +0000867 }
Chris Lattner1c08c712005-01-07 07:47:53 +0000868 }
Chris Lattnerf44fd882005-01-07 21:34:19 +0000869
870 // Remember that this register needs to added to the machine PHI node as
871 // the input for this MBB.
872 unsigned NumElements =
873 TLI.getNumElements(TLI.getValueType(PN->getType()));
874 for (unsigned i = 0, e = NumElements; i != e; ++i)
875 PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
Chris Lattner1c08c712005-01-07 07:47:53 +0000876 }
Chris Lattner1c08c712005-01-07 07:47:53 +0000877 }
878 ConstantsOut.clear();
879
Chris Lattnerddb870b2005-01-13 17:59:43 +0000880 // Turn all of the unordered chains into one factored node.
881 switch (UnorderedChains.size()) {
882 case 0: break;
883 case 1: DAG.setRoot(UnorderedChains[0]); break;
884 default:
885 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, UnorderedChains));
886 }
887
Chris Lattner1c08c712005-01-07 07:47:53 +0000888 // Lower the terminator after the copies are emitted.
889 SDL.visit(*LLVMBB->getTerminator());
890}
891
892void SelectionDAGISel::SelectBasicBlock(BasicBlock *LLVMBB, MachineFunction &MF,
893 FunctionLoweringInfo &FuncInfo) {
894 SelectionDAG DAG(TLI.getTargetMachine(), MF);
895 CurDAG = &DAG;
896 std::vector<std::pair<MachineInstr*, unsigned> > PHINodesToUpdate;
897
898 // First step, lower LLVM code to some DAG. This DAG may use operations and
899 // types that are not supported by the target.
900 BuildSelectionDAG(DAG, LLVMBB, PHINodesToUpdate, FuncInfo);
901
902 DEBUG(std::cerr << "Lowered selection DAG:\n");
903 DEBUG(DAG.dump());
904
905 // Second step, hack on the DAG until it only uses operations and types that
906 // the target supports.
907 DAG.Legalize(TLI);
908
909 DEBUG(std::cerr << "Legalized selection DAG:\n");
910 DEBUG(DAG.dump());
911
912 // Finally, instruction select all of the operations to machine code, adding
913 // the code to the MachineBasicBlock.
914 InstructionSelectBasicBlock(DAG);
915
Chris Lattner7944d9d2005-01-12 03:41:21 +0000916 if (ViewDAGs) DAG.viewGraph();
917
Chris Lattner1c08c712005-01-07 07:47:53 +0000918 DEBUG(std::cerr << "Selected machine code:\n");
919 DEBUG(BB->dump());
920
921 // Finally, now that we know what the last MBB the LLVM BB expanded is, update
922 // PHI nodes in successors.
923 for (unsigned i = 0, e = PHINodesToUpdate.size(); i != e; ++i) {
924 MachineInstr *PHI = PHINodesToUpdate[i].first;
925 assert(PHI->getOpcode() == TargetInstrInfo::PHI &&
926 "This is not a machine PHI node that we are updating!");
927 PHI->addRegOperand(PHINodesToUpdate[i].second);
928 PHI->addMachineBasicBlockOperand(BB);
929 }
930}