1 //===-- PPC64ISelSimple.cpp - A simple instruction selector for PowerPC ---===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 #define DEBUG_TYPE "isel"
12 #include "PowerPCInstrBuilder.h"
13 #include "PowerPCInstrInfo.h"
14 #include "PPC64TargetMachine.h"
15 #include "llvm/Constants.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Function.h"
18 #include "llvm/Instructions.h"
19 #include "llvm/Pass.h"
20 #include "llvm/CodeGen/IntrinsicLowering.h"
21 #include "llvm/CodeGen/MachineConstantPool.h"
22 #include "llvm/CodeGen/MachineFrameInfo.h"
23 #include "llvm/CodeGen/MachineFunction.h"
24 #include "llvm/CodeGen/SSARegMap.h"
25 #include "llvm/Target/MRegisterInfo.h"
26 #include "llvm/Target/TargetMachine.h"
27 #include "llvm/Support/GetElementPtrTypeIterator.h"
28 #include "llvm/Support/InstVisitor.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/ADT/Statistic.h"
35 Statistic<> GEPFolds("ppc64-codegen", "Number of GEPs folded");
37 /// TypeClass - Used by the PowerPC backend to group LLVM types by their basic
38 /// PPC Representation.
41 cByte, cShort, cInt, cFP32, cFP64, cLong
45 /// getClass - Turn a primitive type into a "class" number which is based on the
46 /// size of the type, and whether or not it is floating point.
48 static inline TypeClass getClass(const Type *Ty) {
49 switch (Ty->getTypeID()) {
51 case Type::UByteTyID: return cByte; // Byte operands are class #0
53 case Type::UShortTyID: return cShort; // Short operands are class #1
55 case Type::UIntTyID: return cInt; // Ints are class #2
57 case Type::FloatTyID: return cFP32; // Single float is #3
58 case Type::DoubleTyID: return cFP64; // Double Point is #4
60 case Type::PointerTyID:
62 case Type::ULongTyID: return cLong; // Longs and pointers are class #5
64 assert(0 && "Invalid type to getClass!");
65 return cByte; // not reached
69 // getClassB - Just like getClass, but treat boolean values as ints.
70 static inline TypeClass getClassB(const Type *Ty) {
71 if (Ty == Type::BoolTy) return cInt;
76 struct PPC64ISel : public FunctionPass, InstVisitor<PPC64ISel> {
77 PPC64TargetMachine &TM;
78 MachineFunction *F; // The function we are compiling into
79 MachineBasicBlock *BB; // The current MBB we are compiling
80 int VarArgsFrameIndex; // FrameIndex for start of varargs area
82 std::map<Value*, unsigned> RegMap; // Mapping between Values and SSA Regs
84 // External functions used in the Module
85 Function *fmodfFn, *fmodFn, *__cmpdi2Fn, *__fixsfdiFn, *__fixdfdiFn,
86 *__fixunssfdiFn, *__fixunsdfdiFn, *mallocFn, *freeFn;
88 // MBBMap - Mapping between LLVM BB -> Machine BB
89 std::map<const BasicBlock*, MachineBasicBlock*> MBBMap;
91 // AllocaMap - Mapping from fixed sized alloca instructions to the
92 // FrameIndex for the alloca.
93 std::map<AllocaInst*, unsigned> AllocaMap;
95 // Target configuration data
96 const unsigned ParameterSaveAreaOffset, MaxArgumentStackSpace;
98 PPC64ISel(TargetMachine &tm):TM(reinterpret_cast<PPC64TargetMachine&>(tm)),
99 F(0), BB(0), ParameterSaveAreaOffset(24), MaxArgumentStackSpace(32) {}
101 bool doInitialization(Module &M) {
102 // Add external functions that we may call
103 Type *i = Type::IntTy;
104 Type *d = Type::DoubleTy;
105 Type *f = Type::FloatTy;
106 Type *l = Type::LongTy;
107 Type *ul = Type::ULongTy;
108 Type *voidPtr = PointerType::get(Type::SByteTy);
109 // float fmodf(float, float);
110 fmodfFn = M.getOrInsertFunction("fmodf", f, f, f, 0);
111 // double fmod(double, double);
112 fmodFn = M.getOrInsertFunction("fmod", d, d, d, 0);
113 // int __cmpdi2(long, long);
114 __cmpdi2Fn = M.getOrInsertFunction("__cmpdi2", i, l, l, 0);
115 // long __fixsfdi(float)
116 __fixsfdiFn = M.getOrInsertFunction("__fixsfdi", l, f, 0);
117 // long __fixdfdi(double)
118 __fixdfdiFn = M.getOrInsertFunction("__fixdfdi", l, d, 0);
119 // unsigned long __fixunssfdi(float)
120 __fixunssfdiFn = M.getOrInsertFunction("__fixunssfdi", ul, f, 0);
121 // unsigned long __fixunsdfdi(double)
122 __fixunsdfdiFn = M.getOrInsertFunction("__fixunsdfdi", ul, d, 0);
123 // void* malloc(size_t)
124 mallocFn = M.getOrInsertFunction("malloc", voidPtr, Type::UIntTy, 0);
126 freeFn = M.getOrInsertFunction("free", Type::VoidTy, voidPtr, 0);
130 /// runOnFunction - Top level implementation of instruction selection for
131 /// the entire function.
133 bool runOnFunction(Function &Fn) {
134 // First pass over the function, lower any unknown intrinsic functions
135 // with the IntrinsicLowering class.
136 LowerUnknownIntrinsicFunctionCalls(Fn);
138 F = &MachineFunction::construct(&Fn, TM);
140 // Create all of the machine basic blocks for the function...
141 for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
142 F->getBasicBlockList().push_back(MBBMap[I] = new MachineBasicBlock(I));
146 // Copy incoming arguments off of the stack...
147 LoadArgumentsToVirtualRegs(Fn);
149 // Instruction select everything except PHI nodes
152 // Select the PHI nodes
159 // We always build a machine code representation for the function
163 virtual const char *getPassName() const {
164 return "PowerPC Simple Instruction Selection";
167 /// visitBasicBlock - This method is called when we are visiting a new basic
168 /// block. This simply creates a new MachineBasicBlock to emit code into
169 /// and adds it to the current MachineFunction. Subsequent visit* for
170 /// instructions will be invoked for all instructions in the basic block.
172 void visitBasicBlock(BasicBlock &LLVM_BB) {
173 BB = MBBMap[&LLVM_BB];
176 /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
177 /// function, lowering any calls to unknown intrinsic functions into the
178 /// equivalent LLVM code.
180 void LowerUnknownIntrinsicFunctionCalls(Function &F);
182 /// LoadArgumentsToVirtualRegs - Load all of the arguments to this function
183 /// from the stack into virtual registers.
185 void LoadArgumentsToVirtualRegs(Function &F);
187 /// SelectPHINodes - Insert machine code to generate phis. This is tricky
188 /// because we have to generate our sources into the source basic blocks,
189 /// not the current one.
191 void SelectPHINodes();
193 // Visitation methods for various instructions. These methods simply emit
194 // fixed PowerPC code for each instruction.
196 // Control flow operators
197 void visitReturnInst(ReturnInst &RI);
198 void visitBranchInst(BranchInst &BI);
204 ValueRecord(unsigned R, const Type *T) : Val(0), Reg(R), Ty(T) {}
205 ValueRecord(Value *V) : Val(V), Reg(0), Ty(V->getType()) {}
208 // This struct is for recording the necessary operations to emit the GEP
209 struct CollapsedGepOp {
213 CollapsedGepOp(bool mul, Value *i, ConstantSInt *s) :
214 isMul(mul), index(i), size(s) {}
217 void doCall(const ValueRecord &Ret, MachineInstr *CallMI,
218 const std::vector<ValueRecord> &Args, bool isVarArg);
219 void visitCallInst(CallInst &I);
220 void visitIntrinsicCall(Intrinsic::ID ID, CallInst &I);
222 // Arithmetic operators
223 void visitSimpleBinary(BinaryOperator &B, unsigned OpcodeClass);
224 void visitAdd(BinaryOperator &B) { visitSimpleBinary(B, 0); }
225 void visitSub(BinaryOperator &B) { visitSimpleBinary(B, 1); }
226 void visitMul(BinaryOperator &B);
228 void visitDiv(BinaryOperator &B) { visitDivRem(B); }
229 void visitRem(BinaryOperator &B) { visitDivRem(B); }
230 void visitDivRem(BinaryOperator &B);
233 void visitAnd(BinaryOperator &B) { visitSimpleBinary(B, 2); }
234 void visitOr (BinaryOperator &B) { visitSimpleBinary(B, 3); }
235 void visitXor(BinaryOperator &B) { visitSimpleBinary(B, 4); }
237 // Comparison operators...
238 void visitSetCondInst(SetCondInst &I);
239 unsigned EmitComparison(unsigned OpNum, Value *Op0, Value *Op1,
240 MachineBasicBlock *MBB,
241 MachineBasicBlock::iterator MBBI);
242 void visitSelectInst(SelectInst &SI);
245 // Memory Instructions
246 void visitLoadInst(LoadInst &I);
247 void visitStoreInst(StoreInst &I);
248 void visitGetElementPtrInst(GetElementPtrInst &I);
249 void visitAllocaInst(AllocaInst &I);
250 void visitMallocInst(MallocInst &I);
251 void visitFreeInst(FreeInst &I);
254 void visitShiftInst(ShiftInst &I);
255 void visitPHINode(PHINode &I) {} // PHI nodes handled by second pass
256 void visitCastInst(CastInst &I);
257 void visitVANextInst(VANextInst &I);
258 void visitVAArgInst(VAArgInst &I);
260 void visitInstruction(Instruction &I) {
261 std::cerr << "Cannot instruction select: " << I;
265 /// promote32 - Make a value 32-bits wide, and put it somewhere.
267 void promote32(unsigned targetReg, const ValueRecord &VR);
269 /// emitGEPOperation - Common code shared between visitGetElementPtrInst and
270 /// constant expression GEP support.
272 void emitGEPOperation(MachineBasicBlock *BB, MachineBasicBlock::iterator IP,
273 Value *Src, User::op_iterator IdxBegin,
274 User::op_iterator IdxEnd, unsigned TargetReg,
275 bool CollapseRemainder, ConstantSInt **Remainder,
276 unsigned *PendingAddReg);
278 /// emitCastOperation - Common code shared between visitCastInst and
279 /// constant expression cast support.
281 void emitCastOperation(MachineBasicBlock *BB,MachineBasicBlock::iterator IP,
282 Value *Src, const Type *DestTy, unsigned TargetReg);
284 /// emitSimpleBinaryOperation - Common code shared between visitSimpleBinary
285 /// and constant expression support.
287 void emitSimpleBinaryOperation(MachineBasicBlock *BB,
288 MachineBasicBlock::iterator IP,
289 Value *Op0, Value *Op1,
290 unsigned OperatorClass, unsigned TargetReg);
292 /// emitBinaryFPOperation - This method handles emission of floating point
293 /// Add (0), Sub (1), Mul (2), and Div (3) operations.
294 void emitBinaryFPOperation(MachineBasicBlock *BB,
295 MachineBasicBlock::iterator IP,
296 Value *Op0, Value *Op1,
297 unsigned OperatorClass, unsigned TargetReg);
299 void emitMultiply(MachineBasicBlock *BB, MachineBasicBlock::iterator IP,
300 Value *Op0, Value *Op1, unsigned TargetReg);
302 void doMultiply(MachineBasicBlock *MBB,
303 MachineBasicBlock::iterator IP,
304 unsigned DestReg, Value *Op0, Value *Op1);
306 /// doMultiplyConst - This method will multiply the value in Op0Reg by the
307 /// value of the ContantInt *CI
308 void doMultiplyConst(MachineBasicBlock *MBB,
309 MachineBasicBlock::iterator IP,
310 unsigned DestReg, Value *Op0, ConstantInt *CI);
312 void emitDivRemOperation(MachineBasicBlock *BB,
313 MachineBasicBlock::iterator IP,
314 Value *Op0, Value *Op1, bool isDiv,
317 /// emitSetCCOperation - Common code shared between visitSetCondInst and
318 /// constant expression support.
320 void emitSetCCOperation(MachineBasicBlock *BB,
321 MachineBasicBlock::iterator IP,
322 Value *Op0, Value *Op1, unsigned Opcode,
325 /// emitShiftOperation - Common code shared between visitShiftInst and
326 /// constant expression support.
328 void emitShiftOperation(MachineBasicBlock *MBB,
329 MachineBasicBlock::iterator IP,
330 Value *Op, Value *ShiftAmount, bool isLeftShift,
331 const Type *ResultTy, unsigned DestReg);
333 /// emitSelectOperation - Common code shared between visitSelectInst and the
334 /// constant expression support.
336 void emitSelectOperation(MachineBasicBlock *MBB,
337 MachineBasicBlock::iterator IP,
338 Value *Cond, Value *TrueVal, Value *FalseVal,
341 /// copyConstantToRegister - Output the instructions required to put the
342 /// specified constant into the specified register.
344 void copyConstantToRegister(MachineBasicBlock *MBB,
345 MachineBasicBlock::iterator MBBI,
346 Constant *C, unsigned Reg);
348 void emitUCOM(MachineBasicBlock *MBB, MachineBasicBlock::iterator MBBI,
349 unsigned LHS, unsigned RHS);
351 /// makeAnotherReg - This method returns the next register number we haven't
354 unsigned makeAnotherReg(const Type *Ty) {
355 assert(dynamic_cast<const PPC64RegisterInfo*>(TM.getRegisterInfo()) &&
356 "Current target doesn't have PPC reg info??");
357 const PPC64RegisterInfo *PPCRI =
358 static_cast<const PPC64RegisterInfo*>(TM.getRegisterInfo());
359 // Add the mapping of regnumber => reg class to MachineFunction
360 const TargetRegisterClass *RC = PPCRI->getRegClassForType(Ty);
361 return F->getSSARegMap()->createVirtualRegister(RC);
364 /// getReg - This method turns an LLVM value into a register number.
366 unsigned getReg(Value &V) { return getReg(&V); } // Allow references
367 unsigned getReg(Value *V) {
368 // Just append to the end of the current bb.
369 MachineBasicBlock::iterator It = BB->end();
370 return getReg(V, BB, It);
372 unsigned getReg(Value *V, MachineBasicBlock *MBB,
373 MachineBasicBlock::iterator IPt);
375 /// canUseAsImmediateForOpcode - This method returns whether a ConstantInt
376 /// is okay to use as an immediate argument to a certain binary operation
377 bool canUseAsImmediateForOpcode(ConstantInt *CI, unsigned Opcode);
379 /// getFixedSizedAllocaFI - Return the frame index for a fixed sized alloca
380 /// that is to be statically allocated with the initial stack frame
382 unsigned getFixedSizedAllocaFI(AllocaInst *AI);
386 /// dyn_castFixedAlloca - If the specified value is a fixed size alloca
387 /// instruction in the entry block, return it. Otherwise, return a null
389 static AllocaInst *dyn_castFixedAlloca(Value *V) {
390 if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
391 BasicBlock *BB = AI->getParent();
392 if (isa<ConstantUInt>(AI->getArraySize()) && BB ==&BB->getParent()->front())
398 /// getReg - This method turns an LLVM value into a register number.
400 unsigned PPC64ISel::getReg(Value *V, MachineBasicBlock *MBB,
401 MachineBasicBlock::iterator IPt) {
402 if (Constant *C = dyn_cast<Constant>(V)) {
403 unsigned Reg = makeAnotherReg(V->getType());
404 copyConstantToRegister(MBB, IPt, C, Reg);
406 } else if (AllocaInst *AI = dyn_castFixedAlloca(V)) {
407 unsigned Reg = makeAnotherReg(V->getType());
408 unsigned FI = getFixedSizedAllocaFI(AI);
409 addFrameReference(BuildMI(*MBB, IPt, PPC::ADDI, 2, Reg), FI, 0, false);
413 unsigned &Reg = RegMap[V];
415 Reg = makeAnotherReg(V->getType());
422 /// canUseAsImmediateForOpcode - This method returns whether a ConstantInt
423 /// is okay to use as an immediate argument to a certain binary operator.
425 /// Operator is one of: 0 for Add, 1 for Sub, 2 for And, 3 for Or, 4 for Xor.
426 bool PPC64ISel::canUseAsImmediateForOpcode(ConstantInt *CI, unsigned Operator) {
430 // ADDI, Compare, and non-indexed Load take SIMM
431 bool cond1 = (Operator == 0)
432 && (Op1Cs = dyn_cast<ConstantSInt>(CI))
433 && (Op1Cs->getValue() <= 32767)
434 && (Op1Cs->getValue() >= -32768);
436 // SUBI takes -SIMM since it is a mnemonic for ADDI
437 bool cond2 = (Operator == 1)
438 && (Op1Cs = dyn_cast<ConstantSInt>(CI))
439 && (Op1Cs->getValue() <= 32768)
440 && (Op1Cs->getValue() >= -32767);
442 // ANDIo, ORI, and XORI take unsigned values
443 bool cond3 = (Operator >= 2)
444 && (Op1Cs = dyn_cast<ConstantSInt>(CI))
445 && (Op1Cs->getValue() >= 0)
446 && (Op1Cs->getValue() <= 32767);
448 // ADDI and SUBI take SIMMs, so we have to make sure the UInt would fit
449 bool cond4 = (Operator < 2)
450 && (Op1Cu = dyn_cast<ConstantUInt>(CI))
451 && (Op1Cu->getValue() <= 32767);
453 // ANDIo, ORI, and XORI take UIMMs, so they can be larger
454 bool cond5 = (Operator >= 2)
455 && (Op1Cu = dyn_cast<ConstantUInt>(CI))
456 && (Op1Cu->getValue() <= 65535);
458 if (cond1 || cond2 || cond3 || cond4 || cond5)
464 /// getFixedSizedAllocaFI - Return the frame index for a fixed sized alloca
465 /// that is to be statically allocated with the initial stack frame
467 unsigned PPC64ISel::getFixedSizedAllocaFI(AllocaInst *AI) {
468 // Already computed this?
469 std::map<AllocaInst*, unsigned>::iterator I = AllocaMap.lower_bound(AI);
470 if (I != AllocaMap.end() && I->first == AI) return I->second;
472 const Type *Ty = AI->getAllocatedType();
473 ConstantUInt *CUI = cast<ConstantUInt>(AI->getArraySize());
474 unsigned TySize = TM.getTargetData().getTypeSize(Ty);
475 TySize *= CUI->getValue(); // Get total allocated size...
476 unsigned Alignment = TM.getTargetData().getTypeAlignment(Ty);
478 // Create a new stack object using the frame manager...
479 int FrameIdx = F->getFrameInfo()->CreateStackObject(TySize, Alignment);
480 AllocaMap.insert(I, std::make_pair(AI, FrameIdx));
485 /// copyConstantToRegister - Output the instructions required to put the
486 /// specified constant into the specified register.
488 void PPC64ISel::copyConstantToRegister(MachineBasicBlock *MBB,
489 MachineBasicBlock::iterator IP,
490 Constant *C, unsigned R) {
491 if (C->getType()->isIntegral()) {
492 unsigned Class = getClassB(C->getType());
494 if (Class == cLong) {
495 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(C)) {
496 uint64_t uval = CUI->getValue();
497 if (uval < (1LL << 32)) {
498 ConstantUInt *CU = ConstantUInt::get(Type::UIntTy, uval);
499 copyConstantToRegister(MBB, IP, CU, R);
502 } else if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(C)) {
503 int64_t val = CUI->getValue();
504 if (val < (1LL << 31)) {
505 ConstantUInt *CU = ConstantUInt::get(Type::UIntTy, val);
506 copyConstantToRegister(MBB, IP, CU, R);
510 std::cerr << "Unhandled long constant type!\n";
513 // Spill long to the constant pool and load it
514 MachineConstantPool *CP = F->getConstantPool();
515 unsigned CPI = CP->getConstantPoolIndex(C);
516 BuildMI(*MBB, IP, PPC::LD, 1, R)
517 .addReg(PPC::R2).addConstantPoolIndex(CPI);
521 assert(Class <= cInt && "Type not handled yet!");
524 if (C->getType() == Type::BoolTy) {
525 BuildMI(*MBB, IP, PPC::LI, 1, R).addSImm(C == ConstantBool::True);
530 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(C)) {
531 unsigned uval = CUI->getValue();
533 BuildMI(*MBB, IP, PPC::LI, 1, R).addSImm(uval);
535 unsigned Temp = makeAnotherReg(Type::IntTy);
536 BuildMI(*MBB, IP, PPC::LIS, 1, Temp).addSImm(uval >> 16);
537 BuildMI(*MBB, IP, PPC::ORI, 2, R).addReg(Temp).addImm(uval);
540 } else if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(C)) {
541 int sval = CSI->getValue();
542 if (sval < 32768 && sval >= -32768) {
543 BuildMI(*MBB, IP, PPC::LI, 1, R).addSImm(sval);
545 unsigned Temp = makeAnotherReg(Type::IntTy);
546 BuildMI(*MBB, IP, PPC::LIS, 1, Temp).addSImm(sval >> 16);
547 BuildMI(*MBB, IP, PPC::ORI, 2, R).addReg(Temp).addImm(sval);
551 std::cerr << "Unhandled integer constant!\n";
553 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
554 // We need to spill the constant to memory...
555 MachineConstantPool *CP = F->getConstantPool();
556 unsigned CPI = CP->getConstantPoolIndex(CFP);
557 const Type *Ty = CFP->getType();
558 unsigned LoadOpcode = (Ty == Type::FloatTy) ? PPC::LFS : PPC::LFD;
559 BuildMI(*MBB,IP,LoadOpcode,2,R).addConstantPoolIndex(CPI).addReg(PPC::R2);
560 } else if (isa<ConstantPointerNull>(C)) {
561 // Copy zero (null pointer) to the register.
562 BuildMI(*MBB, IP, PPC::LI, 1, R).addSImm(0);
563 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) {
564 static unsigned OpcodeTable[] = {
565 PPC::LBZ, PPC::LHZ, PPC::LWZ, PPC::LFS, PPC::LFD, PPC::LD
567 unsigned Opcode = OpcodeTable[getClassB(GV->getType())];
568 BuildMI(*MBB, IP, Opcode, 2, R).addGlobalAddress(GV).addReg(PPC::R2);
570 std::cerr << "Offending constant: " << *C << "\n";
571 assert(0 && "Type not handled yet!");
575 /// LoadArgumentsToVirtualRegs - Load all of the arguments to this function from
576 /// the stack into virtual registers.
577 void PPC64ISel::LoadArgumentsToVirtualRegs(Function &Fn) {
578 unsigned ArgOffset = ParameterSaveAreaOffset;
579 unsigned GPR_remaining = 8;
580 unsigned FPR_remaining = 13;
581 unsigned GPR_idx = 0, FPR_idx = 0;
582 static const unsigned GPR[] = {
583 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
584 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
586 static const unsigned FPR[] = {
587 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
588 PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
591 MachineFrameInfo *MFI = F->getFrameInfo();
593 for (Function::arg_iterator I = Fn.arg_begin(), E = Fn.arg_end(); I != E; ++I) {
594 bool ArgLive = !I->use_empty();
595 unsigned Reg = ArgLive ? getReg(*I) : 0;
596 int FI; // Frame object index
598 switch (getClassB(I->getType())) {
601 FI = MFI->CreateFixedObject(4, ArgOffset);
602 if (GPR_remaining > 0) {
603 BuildMI(BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
604 BuildMI(BB, PPC::OR, 2, Reg).addReg(GPR[GPR_idx])
605 .addReg(GPR[GPR_idx]);
607 addFrameReference(BuildMI(BB, PPC::LBZ, 2, Reg), FI);
613 FI = MFI->CreateFixedObject(4, ArgOffset);
614 if (GPR_remaining > 0) {
615 BuildMI(BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
616 BuildMI(BB, PPC::OR, 2, Reg).addReg(GPR[GPR_idx])
617 .addReg(GPR[GPR_idx]);
619 addFrameReference(BuildMI(BB, PPC::LHZ, 2, Reg), FI);
625 FI = MFI->CreateFixedObject(4, ArgOffset);
626 if (GPR_remaining > 0) {
627 BuildMI(BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
628 BuildMI(BB, PPC::OR, 2, Reg).addReg(GPR[GPR_idx])
629 .addReg(GPR[GPR_idx]);
631 addFrameReference(BuildMI(BB, PPC::LWZ, 2, Reg), FI);
637 FI = MFI->CreateFixedObject(8, ArgOffset);
638 if (GPR_remaining > 1) {
639 BuildMI(BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]);
640 BuildMI(BB, PPC::OR, 2, Reg).addReg(GPR[GPR_idx])
641 .addReg(GPR[GPR_idx]);
643 addFrameReference(BuildMI(BB, PPC::LD, 2, Reg), FI);
646 // longs require 4 additional bytes
651 FI = MFI->CreateFixedObject(4, ArgOffset);
653 if (FPR_remaining > 0) {
654 BuildMI(BB, PPC::IMPLICIT_DEF, 0, FPR[FPR_idx]);
655 BuildMI(BB, PPC::FMR, 1, Reg).addReg(FPR[FPR_idx]);
659 addFrameReference(BuildMI(BB, PPC::LFS, 2, Reg), FI);
665 FI = MFI->CreateFixedObject(8, ArgOffset);
667 if (FPR_remaining > 0) {
668 BuildMI(BB, PPC::IMPLICIT_DEF, 0, FPR[FPR_idx]);
669 BuildMI(BB, PPC::FMR, 1, Reg).addReg(FPR[FPR_idx]);
673 addFrameReference(BuildMI(BB, PPC::LFD, 2, Reg), FI);
677 // doubles require 4 additional bytes and use 2 GPRs of param space
679 if (GPR_remaining > 0) {
685 assert(0 && "Unhandled argument type!");
687 ArgOffset += 4; // Each argument takes at least 4 bytes on the stack...
688 if (GPR_remaining > 0) {
689 GPR_remaining--; // uses up 2 GPRs
694 // If the function takes variable number of arguments, add a frame offset for
695 // the start of the first vararg value... this is used to expand
697 if (Fn.getFunctionType()->isVarArg())
698 VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
702 /// SelectPHINodes - Insert machine code to generate phis. This is tricky
703 /// because we have to generate our sources into the source basic blocks, not
706 void PPC64ISel::SelectPHINodes() {
707 const TargetInstrInfo &TII = *TM.getInstrInfo();
708 const Function &LF = *F->getFunction(); // The LLVM function...
709 for (Function::const_iterator I = LF.begin(), E = LF.end(); I != E; ++I) {
710 const BasicBlock *BB = I;
711 MachineBasicBlock &MBB = *MBBMap[I];
713 // Loop over all of the PHI nodes in the LLVM basic block...
714 MachineBasicBlock::iterator PHIInsertPoint = MBB.begin();
715 for (BasicBlock::const_iterator I = BB->begin();
716 PHINode *PN = const_cast<PHINode*>(dyn_cast<PHINode>(I)); ++I) {
718 // Create a new machine instr PHI node, and insert it.
719 unsigned PHIReg = getReg(*PN);
720 MachineInstr *PhiMI = BuildMI(MBB, PHIInsertPoint,
721 PPC::PHI, PN->getNumOperands(), PHIReg);
723 // PHIValues - Map of blocks to incoming virtual registers. We use this
724 // so that we only initialize one incoming value for a particular block,
725 // even if the block has multiple entries in the PHI node.
727 std::map<MachineBasicBlock*, unsigned> PHIValues;
729 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
730 MachineBasicBlock *PredMBB = 0;
731 for (MachineBasicBlock::pred_iterator PI = MBB.pred_begin (),
732 PE = MBB.pred_end (); PI != PE; ++PI)
733 if (PN->getIncomingBlock(i) == (*PI)->getBasicBlock()) {
737 assert (PredMBB && "Couldn't find incoming machine-cfg edge for phi");
740 std::map<MachineBasicBlock*, unsigned>::iterator EntryIt =
741 PHIValues.lower_bound(PredMBB);
743 if (EntryIt != PHIValues.end() && EntryIt->first == PredMBB) {
744 // We already inserted an initialization of the register for this
745 // predecessor. Recycle it.
746 ValReg = EntryIt->second;
748 // Get the incoming value into a virtual register.
750 Value *Val = PN->getIncomingValue(i);
752 // If this is a constant or GlobalValue, we may have to insert code
753 // into the basic block to compute it into a virtual register.
754 if ((isa<Constant>(Val) && !isa<ConstantExpr>(Val)) ||
755 isa<GlobalValue>(Val)) {
756 // Simple constants get emitted at the end of the basic block,
757 // before any terminator instructions. We "know" that the code to
758 // move a constant into a register will never clobber any flags.
759 ValReg = getReg(Val, PredMBB, PredMBB->getFirstTerminator());
761 // Because we don't want to clobber any values which might be in
762 // physical registers with the computation of this constant (which
763 // might be arbitrarily complex if it is a constant expression),
764 // just insert the computation at the top of the basic block.
765 MachineBasicBlock::iterator PI = PredMBB->begin();
767 // Skip over any PHI nodes though!
768 while (PI != PredMBB->end() && PI->getOpcode() == PPC::PHI)
771 ValReg = getReg(Val, PredMBB, PI);
774 // Remember that we inserted a value for this PHI for this predecessor
775 PHIValues.insert(EntryIt, std::make_pair(PredMBB, ValReg));
778 PhiMI->addRegOperand(ValReg);
779 PhiMI->addMachineBasicBlockOperand(PredMBB);
782 // Now that we emitted all of the incoming values for the PHI node, make
783 // sure to reposition the InsertPoint after the PHI that we just added.
784 // This is needed because we might have inserted a constant into this
785 // block, right after the PHI's which is before the old insert point!
786 PHIInsertPoint = PhiMI;
793 // canFoldSetCCIntoBranchOrSelect - Return the setcc instruction if we can fold
794 // it into the conditional branch or select instruction which is the only user
795 // of the cc instruction. This is the case if the conditional branch is the
796 // only user of the setcc, and if the setcc is in the same basic block as the
797 // conditional branch.
799 static SetCondInst *canFoldSetCCIntoBranchOrSelect(Value *V) {
800 if (SetCondInst *SCI = dyn_cast<SetCondInst>(V))
801 if (SCI->hasOneUse()) {
802 Instruction *User = cast<Instruction>(SCI->use_back());
803 if ((isa<BranchInst>(User) || isa<SelectInst>(User)) &&
804 SCI->getParent() == User->getParent())
811 // canFoldGEPIntoLoadOrStore - Return the GEP instruction if we can fold it into
812 // the load or store instruction that is the only user of the GEP.
814 static GetElementPtrInst *canFoldGEPIntoLoadOrStore(Value *V) {
815 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(V))
816 if (GEPI->hasOneUse()) {
817 Instruction *User = cast<Instruction>(GEPI->use_back());
818 if (isa<StoreInst>(User) &&
819 GEPI->getParent() == User->getParent() &&
820 User->getOperand(0) != GEPI &&
821 User->getOperand(1) == GEPI) {
825 if (isa<LoadInst>(User) &&
826 GEPI->getParent() == User->getParent() &&
827 User->getOperand(0) == GEPI) {
836 // Return a fixed numbering for setcc instructions which does not depend on the
837 // order of the opcodes.
839 static unsigned getSetCCNumber(unsigned Opcode) {
841 default: assert(0 && "Unknown setcc instruction!");
842 case Instruction::SetEQ: return 0;
843 case Instruction::SetNE: return 1;
844 case Instruction::SetLT: return 2;
845 case Instruction::SetGE: return 3;
846 case Instruction::SetGT: return 4;
847 case Instruction::SetLE: return 5;
851 static unsigned getPPCOpcodeForSetCCNumber(unsigned Opcode) {
853 default: assert(0 && "Unknown setcc instruction!");
854 case Instruction::SetEQ: return PPC::BEQ;
855 case Instruction::SetNE: return PPC::BNE;
856 case Instruction::SetLT: return PPC::BLT;
857 case Instruction::SetGE: return PPC::BGE;
858 case Instruction::SetGT: return PPC::BGT;
859 case Instruction::SetLE: return PPC::BLE;
863 /// emitUCOM - emits an unordered FP compare.
864 void PPC64ISel::emitUCOM(MachineBasicBlock *MBB, MachineBasicBlock::iterator IP,
865 unsigned LHS, unsigned RHS) {
866 BuildMI(*MBB, IP, PPC::FCMPU, 2, PPC::CR0).addReg(LHS).addReg(RHS);
869 /// EmitComparison - emits a comparison of the two operands, returning the
870 /// extended setcc code to use. The result is in CR0.
872 unsigned PPC64ISel::EmitComparison(unsigned OpNum, Value *Op0, Value *Op1,
873 MachineBasicBlock *MBB,
874 MachineBasicBlock::iterator IP) {
875 // The arguments are already supposed to be of the same type.
876 const Type *CompTy = Op0->getType();
877 unsigned Class = getClassB(CompTy);
878 unsigned Op0r = getReg(Op0, MBB, IP);
880 // Before we do a comparison, we have to make sure that we're truncating our
881 // registers appropriately.
882 if (Class == cByte) {
883 unsigned TmpReg = makeAnotherReg(CompTy);
884 if (CompTy->isSigned())
885 BuildMI(*MBB, IP, PPC::EXTSB, 1, TmpReg).addReg(Op0r);
887 BuildMI(*MBB, IP, PPC::RLWINM, 4, TmpReg).addReg(Op0r).addImm(0)
888 .addImm(24).addImm(31);
890 } else if (Class == cShort) {
891 unsigned TmpReg = makeAnotherReg(CompTy);
892 if (CompTy->isSigned())
893 BuildMI(*MBB, IP, PPC::EXTSH, 1, TmpReg).addReg(Op0r);
895 BuildMI(*MBB, IP, PPC::RLWINM, 4, TmpReg).addReg(Op0r).addImm(0)
896 .addImm(16).addImm(31);
900 // Use crand for lt, gt and crandc for le, ge
901 unsigned CROpcode = (OpNum == 2 || OpNum == 4) ? PPC::CRAND : PPC::CRANDC;
902 unsigned Opcode = CompTy->isSigned() ? PPC::CMPW : PPC::CMPLW;
903 unsigned OpcodeImm = CompTy->isSigned() ? PPC::CMPWI : PPC::CMPLWI;
904 if (Class == cLong) {
905 Opcode = CompTy->isSigned() ? PPC::CMPD : PPC::CMPLD;
906 OpcodeImm = CompTy->isSigned() ? PPC::CMPDI : PPC::CMPLDI;
909 // Special case handling of: cmp R, i
910 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
911 unsigned Op1v = CI->getRawValue() & 0xFFFF;
913 // Treat compare like ADDI for the purposes of immediate suitability
914 if (canUseAsImmediateForOpcode(CI, 0)) {
915 BuildMI(*MBB, IP, OpcodeImm, 2, PPC::CR0).addReg(Op0r).addSImm(Op1v);
917 unsigned Op1r = getReg(Op1, MBB, IP);
918 BuildMI(*MBB, IP, Opcode, 2, PPC::CR0).addReg(Op0r).addReg(Op1r);
923 unsigned Op1r = getReg(Op1, MBB, IP);
926 default: assert(0 && "Unknown type class!");
931 BuildMI(*MBB, IP, Opcode, 2, PPC::CR0).addReg(Op0r).addReg(Op1r);
936 emitUCOM(MBB, IP, Op0r, Op1r);
943 /// visitSetCondInst - emit code to calculate the condition via
944 /// EmitComparison(), and possibly store a 0 or 1 to a register as a result
946 void PPC64ISel::visitSetCondInst(SetCondInst &I) {
947 if (canFoldSetCCIntoBranchOrSelect(&I))
950 unsigned DestReg = getReg(I);
951 unsigned OpNum = I.getOpcode();
952 const Type *Ty = I.getOperand (0)->getType();
954 EmitComparison(OpNum, I.getOperand(0), I.getOperand(1), BB, BB->end());
956 unsigned Opcode = getPPCOpcodeForSetCCNumber(OpNum);
957 MachineBasicBlock *thisMBB = BB;
958 const BasicBlock *LLVM_BB = BB->getBasicBlock();
959 ilist<MachineBasicBlock>::iterator It = BB;
968 // FIXME: we wouldn't need copy0MBB (we could fold it into thisMBB)
969 // if we could insert other, non-terminator instructions after the
970 // bCC. But MBB->getFirstTerminator() can't understand this.
971 MachineBasicBlock *copy1MBB = new MachineBasicBlock(LLVM_BB);
972 F->getBasicBlockList().insert(It, copy1MBB);
973 BuildMI(BB, Opcode, 2).addReg(PPC::CR0).addMBB(copy1MBB);
974 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
975 F->getBasicBlockList().insert(It, copy0MBB);
976 BuildMI(BB, PPC::B, 1).addMBB(copy0MBB);
977 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
978 F->getBasicBlockList().insert(It, sinkMBB);
979 // Update machine-CFG edges
980 BB->addSuccessor(copy1MBB);
981 BB->addSuccessor(copy0MBB);
987 unsigned TrueValue = makeAnotherReg(I.getType());
988 BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1);
989 BuildMI(BB, PPC::B, 1).addMBB(sinkMBB);
990 // Update machine-CFG edges
991 BB->addSuccessor(sinkMBB);
994 // %FalseValue = li 0
997 unsigned FalseValue = makeAnotherReg(I.getType());
998 BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0);
999 // Update machine-CFG edges
1000 BB->addSuccessor(sinkMBB);
1003 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, copy1MBB ]
1006 BuildMI(BB, PPC::PHI, 4, DestReg).addReg(FalseValue)
1007 .addMBB(copy0MBB).addReg(TrueValue).addMBB(copy1MBB);
1010 void PPC64ISel::visitSelectInst(SelectInst &SI) {
1011 unsigned DestReg = getReg(SI);
1012 MachineBasicBlock::iterator MII = BB->end();
1013 emitSelectOperation(BB, MII, SI.getCondition(), SI.getTrueValue(),
1014 SI.getFalseValue(), DestReg);
1017 /// emitSelect - Common code shared between visitSelectInst and the constant
1018 /// expression support.
1019 /// FIXME: this is most likely broken in one or more ways. Namely, PowerPC has
1020 /// no select instruction. FSEL only works for comparisons against zero.
1021 void PPC64ISel::emitSelectOperation(MachineBasicBlock *MBB,
1022 MachineBasicBlock::iterator IP,
1023 Value *Cond, Value *TrueVal,
1024 Value *FalseVal, unsigned DestReg) {
1025 unsigned SelectClass = getClassB(TrueVal->getType());
1028 // See if we can fold the setcc into the select instruction, or if we have
1029 // to get the register of the Cond value
1030 if (SetCondInst *SCI = canFoldSetCCIntoBranchOrSelect(Cond)) {
1031 // We successfully folded the setcc into the select instruction.
1032 unsigned OpNum = getSetCCNumber(SCI->getOpcode());
1033 OpNum = EmitComparison(OpNum, SCI->getOperand(0),SCI->getOperand(1),MBB,IP);
1034 Opcode = getPPCOpcodeForSetCCNumber(SCI->getOpcode());
1036 unsigned CondReg = getReg(Cond, MBB, IP);
1037 BuildMI(*MBB, IP, PPC::CMPI, 2, PPC::CR0).addReg(CondReg).addSImm(0);
1038 Opcode = getPPCOpcodeForSetCCNumber(Instruction::SetNE);
1043 // cmpTY cr0, r1, r2
1047 MachineBasicBlock *thisMBB = BB;
1048 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1049 ilist<MachineBasicBlock>::iterator It = BB;
1052 // FIXME: we wouldn't need copy0MBB (we could fold it into thisMBB)
1053 // if we could insert other, non-terminator instructions after the
1054 // bCC. But MBB->getFirstTerminator() can't understand this.
1055 MachineBasicBlock *copy1MBB = new MachineBasicBlock(LLVM_BB);
1056 F->getBasicBlockList().insert(It, copy1MBB);
1057 BuildMI(BB, Opcode, 2).addReg(PPC::CR0).addMBB(copy1MBB);
1058 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1059 F->getBasicBlockList().insert(It, copy0MBB);
1060 BuildMI(BB, PPC::B, 1).addMBB(copy0MBB);
1061 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1062 F->getBasicBlockList().insert(It, sinkMBB);
1063 // Update machine-CFG edges
1064 BB->addSuccessor(copy1MBB);
1065 BB->addSuccessor(copy0MBB);
1071 unsigned TrueValue = getReg(TrueVal, BB, BB->begin());
1072 BuildMI(BB, PPC::B, 1).addMBB(sinkMBB);
1073 // Update machine-CFG edges
1074 BB->addSuccessor(sinkMBB);
1077 // %FalseValue = ...
1080 unsigned FalseValue = getReg(FalseVal, BB, BB->begin());
1081 // Update machine-CFG edges
1082 BB->addSuccessor(sinkMBB);
1085 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, copy1MBB ]
1088 BuildMI(BB, PPC::PHI, 4, DestReg).addReg(FalseValue)
1089 .addMBB(copy0MBB).addReg(TrueValue).addMBB(copy1MBB);
1095 /// promote32 - Emit instructions to turn a narrow operand into a 32-bit-wide
1096 /// operand, in the specified target register.
1098 void PPC64ISel::promote32(unsigned targetReg, const ValueRecord &VR) {
1099 bool isUnsigned = VR.Ty->isUnsigned() || VR.Ty == Type::BoolTy;
1101 Value *Val = VR.Val;
1102 const Type *Ty = VR.Ty;
1104 if (Constant *C = dyn_cast<Constant>(Val)) {
1105 Val = ConstantExpr::getCast(C, Type::IntTy);
1106 if (isa<ConstantExpr>(Val)) // Could not fold
1109 Ty = Type::IntTy; // Folded!
1112 // If this is a simple constant, just emit a load directly to avoid the copy
1113 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
1114 int TheVal = CI->getRawValue() & 0xFFFFFFFF;
1116 if (TheVal < 32768 && TheVal >= -32768) {
1117 BuildMI(BB, PPC::LI, 1, targetReg).addSImm(TheVal);
1119 unsigned TmpReg = makeAnotherReg(Type::IntTy);
1120 BuildMI(BB, PPC::LIS, 1, TmpReg).addSImm(TheVal >> 16);
1121 BuildMI(BB, PPC::ORI, 2, targetReg).addReg(TmpReg)
1122 .addImm(TheVal & 0xFFFF);
1128 // Make sure we have the register number for this value...
1129 unsigned Reg = Val ? getReg(Val) : VR.Reg;
1130 switch (getClassB(Ty)) {
1132 // Extend value into target register (8->32)
1134 BuildMI(BB, PPC::RLWINM, 4, targetReg).addReg(Reg).addZImm(0)
1135 .addZImm(24).addZImm(31);
1137 BuildMI(BB, PPC::EXTSB, 1, targetReg).addReg(Reg);
1140 // Extend value into target register (16->32)
1142 BuildMI(BB, PPC::RLWINM, 4, targetReg).addReg(Reg).addZImm(0)
1143 .addZImm(16).addZImm(31);
1145 BuildMI(BB, PPC::EXTSH, 1, targetReg).addReg(Reg);
1149 // Move value into target register (32->32)
1150 BuildMI(BB, PPC::OR, 2, targetReg).addReg(Reg).addReg(Reg);
1153 assert(0 && "Unpromotable operand class in promote32");
1157 /// visitReturnInst - implemented with BLR
1159 void PPC64ISel::visitReturnInst(ReturnInst &I) {
1160 // Only do the processing if this is a non-void return
1161 if (I.getNumOperands() > 0) {
1162 Value *RetVal = I.getOperand(0);
1163 switch (getClassB(RetVal->getType())) {
1164 case cByte: // integral return values: extend or move into r3 and return
1168 promote32(PPC::R3, ValueRecord(RetVal));
1171 case cFP64: { // Floats & Doubles: Return in f1
1172 unsigned RetReg = getReg(RetVal);
1173 BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(RetReg);
1177 visitInstruction(I);
1180 BuildMI(BB, PPC::BLR, 1).addImm(1);
1183 // getBlockAfter - Return the basic block which occurs lexically after the
1185 static inline BasicBlock *getBlockAfter(BasicBlock *BB) {
1186 Function::iterator I = BB; ++I; // Get iterator to next block
1187 return I != BB->getParent()->end() ? &*I : 0;
1190 /// visitBranchInst - Handle conditional and unconditional branches here. Note
1191 /// that since code layout is frozen at this point, that if we are trying to
1192 /// jump to a block that is the immediate successor of the current block, we can
1193 /// just make a fall-through (but we don't currently).
1195 void PPC64ISel::visitBranchInst(BranchInst &BI) {
1196 // Update machine-CFG edges
1197 BB->addSuccessor(MBBMap[BI.getSuccessor(0)]);
1198 if (BI.isConditional())
1199 BB->addSuccessor(MBBMap[BI.getSuccessor(1)]);
1201 BasicBlock *NextBB = getBlockAfter(BI.getParent()); // BB after current one
1203 if (!BI.isConditional()) { // Unconditional branch?
1204 if (BI.getSuccessor(0) != NextBB)
1205 BuildMI(BB, PPC::B, 1).addMBB(MBBMap[BI.getSuccessor(0)]);
1209 // See if we can fold the setcc into the branch itself...
1210 SetCondInst *SCI = canFoldSetCCIntoBranchOrSelect(BI.getCondition());
1212 // Nope, cannot fold setcc into this branch. Emit a branch on a condition
1213 // computed some other way...
1214 unsigned condReg = getReg(BI.getCondition());
1215 BuildMI(BB, PPC::CMPLI, 3, PPC::CR0).addImm(0).addReg(condReg)
1217 if (BI.getSuccessor(1) == NextBB) {
1218 if (BI.getSuccessor(0) != NextBB)
1219 BuildMI(BB, PPC::COND_BRANCH, 3).addReg(PPC::CR0).addImm(PPC::BNE)
1220 .addMBB(MBBMap[BI.getSuccessor(0)])
1221 .addMBB(MBBMap[BI.getSuccessor(1)]);
1223 BuildMI(BB, PPC::COND_BRANCH, 3).addReg(PPC::CR0).addImm(PPC::BEQ)
1224 .addMBB(MBBMap[BI.getSuccessor(1)])
1225 .addMBB(MBBMap[BI.getSuccessor(0)]);
1226 if (BI.getSuccessor(0) != NextBB)
1227 BuildMI(BB, PPC::B, 1).addMBB(MBBMap[BI.getSuccessor(0)]);
1232 unsigned OpNum = getSetCCNumber(SCI->getOpcode());
1233 unsigned Opcode = getPPCOpcodeForSetCCNumber(SCI->getOpcode());
1234 MachineBasicBlock::iterator MII = BB->end();
1235 OpNum = EmitComparison(OpNum, SCI->getOperand(0), SCI->getOperand(1), BB,MII);
1237 if (BI.getSuccessor(0) != NextBB) {
1238 BuildMI(BB, PPC::COND_BRANCH, 3).addReg(PPC::CR0).addImm(Opcode)
1239 .addMBB(MBBMap[BI.getSuccessor(0)])
1240 .addMBB(MBBMap[BI.getSuccessor(1)]);
1241 if (BI.getSuccessor(1) != NextBB)
1242 BuildMI(BB, PPC::B, 1).addMBB(MBBMap[BI.getSuccessor(1)]);
1244 // Change to the inverse condition...
1245 if (BI.getSuccessor(1) != NextBB) {
1246 Opcode = PPC64InstrInfo::invertPPCBranchOpcode(Opcode);
1247 BuildMI(BB, PPC::COND_BRANCH, 3).addReg(PPC::CR0).addImm(Opcode)
1248 .addMBB(MBBMap[BI.getSuccessor(1)])
1249 .addMBB(MBBMap[BI.getSuccessor(0)]);
1254 /// doCall - This emits an abstract call instruction, setting up the arguments
1255 /// and the return value as appropriate. For the actual function call itself,
1256 /// it inserts the specified CallMI instruction into the stream.
1258 void PPC64ISel::doCall(const ValueRecord &Ret, MachineInstr *CallMI,
1259 const std::vector<ValueRecord> &Args, bool isVarArg) {
1260 // Count how many bytes are to be pushed on the stack, including the linkage
1261 // area, and parameter passing area.
1262 unsigned NumBytes = ParameterSaveAreaOffset;
1263 unsigned ArgOffset = ParameterSaveAreaOffset;
1265 if (!Args.empty()) {
1266 for (unsigned i = 0, e = Args.size(); i != e; ++i)
1267 switch (getClassB(Args[i].Ty)) {
1268 case cByte: case cShort: case cInt:
1269 NumBytes += 4; break;
1271 NumBytes += 8; break;
1273 NumBytes += 4; break;
1275 NumBytes += 8; break;
1277 default: assert(0 && "Unknown class!");
1280 // Just to be safe, we'll always reserve the full argument passing space in
1281 // case any called code gets funky on us.
1282 if (NumBytes < ParameterSaveAreaOffset + MaxArgumentStackSpace)
1283 NumBytes = ParameterSaveAreaOffset + MaxArgumentStackSpace;
1285 // Adjust the stack pointer for the new arguments...
1286 // These functions are automatically eliminated by the prolog/epilog pass
1287 BuildMI(BB, PPC::ADJCALLSTACKDOWN, 1).addImm(NumBytes);
1289 // Arguments go on the stack in reverse order, as specified by the ABI.
1290 int GPR_remaining = 8, FPR_remaining = 13;
1291 unsigned GPR_idx = 0, FPR_idx = 0;
1292 static const unsigned GPR[] = {
1293 PPC::R3, PPC::R4, PPC::R5, PPC::R6,
1294 PPC::R7, PPC::R8, PPC::R9, PPC::R10,
1296 static const unsigned FPR[] = {
1297 PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6,
1298 PPC::F7, PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12,
1302 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
1304 switch (getClassB(Args[i].Ty)) {
1307 // Promote arg to 32 bits wide into a temporary register...
1308 ArgReg = makeAnotherReg(Type::UIntTy);
1309 promote32(ArgReg, Args[i]);
1312 if (GPR_remaining > 0) {
1313 BuildMI(BB, PPC::OR, 2, GPR[GPR_idx]).addReg(ArgReg)
1315 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1317 if (GPR_remaining <= 0 || isVarArg) {
1318 BuildMI(BB, PPC::STW, 3).addReg(ArgReg).addSImm(ArgOffset)
1323 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
1326 if (GPR_remaining > 0) {
1327 BuildMI(BB, PPC::OR, 2, GPR[GPR_idx]).addReg(ArgReg)
1329 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1331 if (GPR_remaining <= 0 || isVarArg) {
1332 BuildMI(BB, PPC::STW, 3).addReg(ArgReg).addSImm(ArgOffset)
1337 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
1340 if (GPR_remaining > 0) {
1341 BuildMI(BB, PPC::OR, 2, GPR[GPR_idx]).addReg(ArgReg)
1343 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1345 if (GPR_remaining <= 0 || isVarArg) {
1346 BuildMI(BB, PPC::STD, 3).addReg(ArgReg).addSImm(ArgOffset)
1349 ArgOffset += 4; // 8 byte entry, not 4.
1352 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
1354 if (FPR_remaining > 0) {
1355 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgReg);
1356 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
1360 // If this is a vararg function, and there are GPRs left, also
1361 // pass the float in an int. Otherwise, put it on the stack.
1363 BuildMI(BB, PPC::STFS, 3).addReg(ArgReg).addSImm(ArgOffset)
1365 if (GPR_remaining > 0) {
1366 BuildMI(BB, PPC::LWZ, 2, GPR[GPR_idx])
1367 .addSImm(ArgOffset).addReg(ArgReg);
1368 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1372 BuildMI(BB, PPC::STFS, 3).addReg(ArgReg).addSImm(ArgOffset)
1377 ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg;
1379 if (FPR_remaining > 0) {
1380 BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgReg);
1381 CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use);
1384 // For vararg functions, must pass doubles via int regs as well
1386 BuildMI(BB, PPC::STFD, 3).addReg(ArgReg).addSImm(ArgOffset)
1389 if (GPR_remaining > 0) {
1390 BuildMI(BB, PPC::LD, 2, GPR[GPR_idx]).addSImm(ArgOffset)
1392 CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use);
1396 BuildMI(BB, PPC::STFD, 3).addReg(ArgReg).addSImm(ArgOffset)
1399 // Doubles use 8 bytes
1403 default: assert(0 && "Unknown class!");
1410 BuildMI(BB, PPC::ADJCALLSTACKDOWN, 1).addImm(0);
1413 BuildMI(BB, PPC::IMPLICIT_DEF, 0, PPC::LR);
1414 BB->push_back(CallMI);
1415 BuildMI(BB, PPC::NOP, 0);
1417 // These functions are automatically eliminated by the prolog/epilog pass
1418 BuildMI(BB, PPC::ADJCALLSTACKUP, 1).addImm(NumBytes);
1420 // If there is a return value, scavenge the result from the location the call
1423 if (Ret.Ty != Type::VoidTy) {
1424 unsigned DestClass = getClassB(Ret.Ty);
1425 switch (DestClass) {
1430 // Integral results are in r3
1431 BuildMI(BB, PPC::OR, 2, Ret.Reg).addReg(PPC::R3).addReg(PPC::R3);
1433 case cFP32: // Floating-point return values live in f1
1435 BuildMI(BB, PPC::FMR, 1, Ret.Reg).addReg(PPC::F1);
1437 default: assert(0 && "Unknown class!");
1443 /// visitCallInst - Push args on stack and do a procedure call instruction.
1444 void PPC64ISel::visitCallInst(CallInst &CI) {
1445 MachineInstr *TheCall;
1446 Function *F = CI.getCalledFunction();
1448 // Is it an intrinsic function call?
1449 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) {
1450 visitIntrinsicCall(ID, CI); // Special intrinsics are not handled here
1453 // Emit a CALL instruction with PC-relative displacement.
1454 TheCall = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(F, true);
1455 } else { // Emit an indirect call through the CTR
1456 unsigned Reg = getReg(CI.getCalledValue());
1457 BuildMI(BB, PPC::MTCTR, 1).addReg(Reg);
1458 TheCall = BuildMI(PPC::CALLindirect, 2).addZImm(20).addZImm(0);
1461 std::vector<ValueRecord> Args;
1462 for (unsigned i = 1, e = CI.getNumOperands(); i != e; ++i)
1463 Args.push_back(ValueRecord(CI.getOperand(i)));
1465 unsigned DestReg = CI.getType() != Type::VoidTy ? getReg(CI) : 0;
1466 bool isVarArg = F ? F->getFunctionType()->isVarArg() : true;
1467 doCall(ValueRecord(DestReg, CI.getType()), TheCall, Args, isVarArg);
1471 /// dyncastIsNan - Return the operand of an isnan operation if this is an isnan.
1473 static Value *dyncastIsNan(Value *V) {
1474 if (CallInst *CI = dyn_cast<CallInst>(V))
1475 if (Function *F = CI->getCalledFunction())
1476 if (F->getIntrinsicID() == Intrinsic::isunordered)
1477 return CI->getOperand(1);
1481 /// isOnlyUsedByUnorderedComparisons - Return true if this value is only used by
1482 /// or's whos operands are all calls to the isnan predicate.
1483 static bool isOnlyUsedByUnorderedComparisons(Value *V) {
1484 assert(dyncastIsNan(V) && "The value isn't an isnan call!");
1486 // Check all uses, which will be or's of isnans if this predicate is true.
1487 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;++UI){
1488 Instruction *I = cast<Instruction>(*UI);
1489 if (I->getOpcode() != Instruction::Or) return false;
1490 if (I->getOperand(0) != V && !dyncastIsNan(I->getOperand(0))) return false;
1491 if (I->getOperand(1) != V && !dyncastIsNan(I->getOperand(1))) return false;
1497 /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
1498 /// function, lowering any calls to unknown intrinsic functions into the
1499 /// equivalent LLVM code.
1501 void PPC64ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) {
1502 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
1503 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
1504 if (CallInst *CI = dyn_cast<CallInst>(I++))
1505 if (Function *F = CI->getCalledFunction())
1506 switch (F->getIntrinsicID()) {
1507 case Intrinsic::not_intrinsic:
1508 case Intrinsic::vastart:
1509 case Intrinsic::vacopy:
1510 case Intrinsic::vaend:
1511 case Intrinsic::returnaddress:
1512 case Intrinsic::frameaddress:
1513 // FIXME: should lower these ourselves
1514 // case Intrinsic::isunordered:
1515 // case Intrinsic::memcpy: -> doCall(). system memcpy almost
1516 // guaranteed to be faster than anything we generate ourselves
1517 // We directly implement these intrinsics
1519 case Intrinsic::readio: {
1520 // On PPC, memory operations are in-order. Lower this intrinsic
1521 // into a volatile load.
1522 LoadInst * LI = new LoadInst(CI->getOperand(1), "", true, CI);
1523 CI->replaceAllUsesWith(LI);
1524 BB->getInstList().erase(CI);
1527 case Intrinsic::writeio: {
1528 // On PPC, memory operations are in-order. Lower this intrinsic
1529 // into a volatile store.
1530 StoreInst *SI = new StoreInst(CI->getOperand(1),
1531 CI->getOperand(2), true, CI);
1532 CI->replaceAllUsesWith(SI);
1533 BB->getInstList().erase(CI);
1537 // All other intrinsic calls we must lower.
1538 Instruction *Before = CI->getPrev();
1539 TM.getIntrinsicLowering().LowerIntrinsicCall(CI);
1540 if (Before) { // Move iterator to instruction after call
1548 void PPC64ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) {
1549 unsigned TmpReg1, TmpReg2, TmpReg3;
1551 case Intrinsic::vastart:
1552 // Get the address of the first vararg value...
1553 TmpReg1 = getReg(CI);
1554 addFrameReference(BuildMI(BB, PPC::ADDI, 2, TmpReg1), VarArgsFrameIndex,
1558 case Intrinsic::vacopy:
1559 TmpReg1 = getReg(CI);
1560 TmpReg2 = getReg(CI.getOperand(1));
1561 BuildMI(BB, PPC::OR, 2, TmpReg1).addReg(TmpReg2).addReg(TmpReg2);
1563 case Intrinsic::vaend: return;
1565 case Intrinsic::returnaddress:
1566 TmpReg1 = getReg(CI);
1567 if (cast<Constant>(CI.getOperand(1))->isNullValue()) {
1568 MachineFrameInfo *MFI = F->getFrameInfo();
1569 unsigned NumBytes = MFI->getStackSize();
1571 BuildMI(BB, PPC::LWZ, 2, TmpReg1).addSImm(NumBytes+8)
1574 // Values other than zero are not implemented yet.
1575 BuildMI(BB, PPC::LI, 1, TmpReg1).addSImm(0);
1579 case Intrinsic::frameaddress:
1580 TmpReg1 = getReg(CI);
1581 if (cast<Constant>(CI.getOperand(1))->isNullValue()) {
1582 BuildMI(BB, PPC::OR, 2, TmpReg1).addReg(PPC::R1).addReg(PPC::R1);
1584 // Values other than zero are not implemented yet.
1585 BuildMI(BB, PPC::LI, 1, TmpReg1).addSImm(0);
1590 // This may be useful for supporting isunordered
1591 case Intrinsic::isnan:
1592 // If this is only used by 'isunordered' style comparisons, don't emit it.
1593 if (isOnlyUsedByUnorderedComparisons(&CI)) return;
1594 TmpReg1 = getReg(CI.getOperand(1));
1595 emitUCOM(BB, BB->end(), TmpReg1, TmpReg1);
1596 TmpReg2 = makeAnotherReg(Type::IntTy);
1597 BuildMI(BB, PPC::MFCR, TmpReg2);
1598 TmpReg3 = getReg(CI);
1599 BuildMI(BB, PPC::RLWINM, 4, TmpReg3).addReg(TmpReg2).addImm(4).addImm(31).addImm(31);
1603 default: assert(0 && "Error: unknown intrinsics should have been lowered!");
1607 /// visitSimpleBinary - Implement simple binary operators for integral types...
1608 /// OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for Or, 4 for
1611 void PPC64ISel::visitSimpleBinary(BinaryOperator &B, unsigned OperatorClass) {
1612 unsigned DestReg = getReg(B);
1613 MachineBasicBlock::iterator MI = BB->end();
1614 Value *Op0 = B.getOperand(0), *Op1 = B.getOperand(1);
1615 unsigned Class = getClassB(B.getType());
1617 emitSimpleBinaryOperation(BB, MI, Op0, Op1, OperatorClass, DestReg);
1620 /// emitBinaryFPOperation - This method handles emission of floating point
1621 /// Add (0), Sub (1), Mul (2), and Div (3) operations.
1622 void PPC64ISel::emitBinaryFPOperation(MachineBasicBlock *BB,
1623 MachineBasicBlock::iterator IP,
1624 Value *Op0, Value *Op1,
1625 unsigned OperatorClass, unsigned DestReg){
1627 static const unsigned OpcodeTab[][4] = {
1628 { PPC::FADDS, PPC::FSUBS, PPC::FMULS, PPC::FDIVS }, // Float
1629 { PPC::FADD, PPC::FSUB, PPC::FMUL, PPC::FDIV }, // Double
1632 // Special case: R1 = op <const fp>, R2
1633 if (ConstantFP *Op0C = dyn_cast<ConstantFP>(Op0))
1634 if (Op0C->isExactlyValue(-0.0) && OperatorClass == 1) {
1636 unsigned op1Reg = getReg(Op1, BB, IP);
1637 BuildMI(*BB, IP, PPC::FNEG, 1, DestReg).addReg(op1Reg);
1641 unsigned Opcode = OpcodeTab[Op0->getType() == Type::DoubleTy][OperatorClass];
1642 unsigned Op0r = getReg(Op0, BB, IP);
1643 unsigned Op1r = getReg(Op1, BB, IP);
1644 BuildMI(*BB, IP, Opcode, 2, DestReg).addReg(Op0r).addReg(Op1r);
1647 /// emitSimpleBinaryOperation - Implement simple binary operators for integral
1648 /// types... OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for
1651 /// emitSimpleBinaryOperation - Common code shared between visitSimpleBinary
1652 /// and constant expression support.
1654 void PPC64ISel::emitSimpleBinaryOperation(MachineBasicBlock *MBB,
1655 MachineBasicBlock::iterator IP,
1656 Value *Op0, Value *Op1,
1657 unsigned OperatorClass,
1659 unsigned Class = getClassB(Op0->getType());
1661 // Arithmetic and Bitwise operators
1662 static const unsigned OpcodeTab[] = {
1663 PPC::ADD, PPC::SUB, PPC::AND, PPC::OR, PPC::XOR
1665 // FIXME: Convert this to the version from PPC32ISel
1666 static const unsigned ImmOpcodeTab[] = {
1667 PPC::ADDI, PPC::ADDI, PPC::ANDIo, PPC::ORI, PPC::XORI
1669 static const unsigned RImmOpcodeTab[] = {
1670 PPC::ADDI, PPC::SUBFIC, PPC::ANDIo, PPC::ORI, PPC::XORI
1673 if (Class == cFP32 || Class == cFP64) {
1674 assert(OperatorClass < 2 && "No logical ops for FP!");
1675 emitBinaryFPOperation(MBB, IP, Op0, Op1, OperatorClass, DestReg);
1679 if (Op0->getType() == Type::BoolTy) {
1680 if (OperatorClass == 3)
1681 // If this is an or of two isnan's, emit an FP comparison directly instead
1682 // of or'ing two isnan's together.
1683 if (Value *LHS = dyncastIsNan(Op0))
1684 if (Value *RHS = dyncastIsNan(Op1)) {
1685 unsigned Op0Reg = getReg(RHS, MBB, IP), Op1Reg = getReg(LHS, MBB, IP);
1686 unsigned TmpReg = makeAnotherReg(Type::IntTy);
1687 emitUCOM(MBB, IP, Op0Reg, Op1Reg);
1688 BuildMI(*MBB, IP, PPC::MFCR, TmpReg);
1689 BuildMI(*MBB, IP, PPC::RLWINM, 4, DestReg).addReg(TmpReg).addImm(4)
1690 .addImm(31).addImm(31);
1695 // Special case: op <const int>, Reg
1696 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op0)) {
1697 // sub 0, X -> subfic
1698 if (OperatorClass == 1 && canUseAsImmediateForOpcode(CI, 0)) {
1699 unsigned Op1r = getReg(Op1, MBB, IP);
1700 int imm = CI->getRawValue() & 0xFFFF;
1701 BuildMI(*MBB, IP, PPC::SUBFIC, 2, DestReg).addReg(Op1r).addSImm(imm);
1705 // If it is easy to do, swap the operands and emit an immediate op
1706 if (Class != cLong && OperatorClass != 1 &&
1707 canUseAsImmediateForOpcode(CI, OperatorClass)) {
1708 unsigned Op1r = getReg(Op1, MBB, IP);
1709 int imm = CI->getRawValue() & 0xFFFF;
1711 if (OperatorClass < 2)
1712 BuildMI(*MBB, IP, RImmOpcodeTab[OperatorClass], 2, DestReg).addReg(Op1r)
1715 BuildMI(*MBB, IP, RImmOpcodeTab[OperatorClass], 2, DestReg).addReg(Op1r)
1721 // Special case: op Reg, <const int>
1722 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
1723 unsigned Op0r = getReg(Op0, MBB, IP);
1725 // xor X, -1 -> not X
1726 if (OperatorClass == 4 && Op1C->isAllOnesValue()) {
1727 BuildMI(*MBB, IP, PPC::NOR, 2, DestReg).addReg(Op0r).addReg(Op0r);
1731 if (canUseAsImmediateForOpcode(Op1C, OperatorClass)) {
1732 int immediate = Op1C->getRawValue() & 0xFFFF;
1734 if (OperatorClass < 2)
1735 BuildMI(*MBB, IP, ImmOpcodeTab[OperatorClass], 2,DestReg).addReg(Op0r)
1736 .addSImm(immediate);
1738 BuildMI(*MBB, IP, ImmOpcodeTab[OperatorClass], 2,DestReg).addReg(Op0r)
1739 .addZImm(immediate);
1741 unsigned Op1r = getReg(Op1, MBB, IP);
1742 BuildMI(*MBB, IP, OpcodeTab[OperatorClass], 2, DestReg).addReg(Op0r)
1748 // We couldn't generate an immediate variant of the op, load both halves into
1749 // registers and emit the appropriate opcode.
1750 unsigned Op0r = getReg(Op0, MBB, IP);
1751 unsigned Op1r = getReg(Op1, MBB, IP);
1752 unsigned Opcode = OpcodeTab[OperatorClass];
1753 BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addReg(Op1r);
1756 // ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
1757 // returns zero when the input is not exactly a power of two.
1758 static unsigned ExactLog2(unsigned Val) {
1759 if (Val == 0 || (Val & (Val-1))) return 0;
1768 /// doMultiply - Emit appropriate instructions to multiply together the
1769 /// Values Op0 and Op1, and put the result in DestReg.
1771 void PPC64ISel::doMultiply(MachineBasicBlock *MBB,
1772 MachineBasicBlock::iterator IP,
1773 unsigned DestReg, Value *Op0, Value *Op1) {
1774 unsigned Class0 = getClass(Op0->getType());
1775 unsigned Class1 = getClass(Op1->getType());
1777 unsigned Op0r = getReg(Op0, MBB, IP);
1778 unsigned Op1r = getReg(Op1, MBB, IP);
1781 if (Class0 == cLong && Class1 == cLong) {
1782 BuildMI(*MBB, IP, PPC::MULLD, 2, DestReg).addReg(Op0r).addReg(Op1r);
1786 // 64 x 32 or less, promote 32 to 64 and do a 64 x 64
1787 if (Class0 == cLong && Class1 <= cInt) {
1788 // FIXME: CLEAR or SIGN EXTEND Op1
1789 BuildMI(*MBB, IP, PPC::MULLD, 2, DestReg).addReg(Op0r).addReg(Op1r);
1794 if (Class0 <= cInt && Class1 <= cInt) {
1795 BuildMI(*MBB, IP, PPC::MULLW, 2, DestReg).addReg(Op0r).addReg(Op1r);
1799 assert(0 && "doMultiply cannot operate on unknown type!");
1802 /// doMultiplyConst - This method will multiply the value in Op0 by the
1803 /// value of the ContantInt *CI
1804 void PPC64ISel::doMultiplyConst(MachineBasicBlock *MBB,
1805 MachineBasicBlock::iterator IP,
1806 unsigned DestReg, Value *Op0, ConstantInt *CI) {
1807 unsigned Class = getClass(Op0->getType());
1810 if (CI->isNullValue()) {
1811 BuildMI(*MBB, IP, PPC::LI, 1, DestReg).addSImm(0);
1815 // Mul op0, 1 ==> op0
1816 if (CI->equalsInt(1)) {
1817 unsigned Op0r = getReg(Op0, MBB, IP);
1818 BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(Op0r).addReg(Op0r);
1822 // If the element size is exactly a power of 2, use a shift to get it.
1823 if (unsigned Shift = ExactLog2(CI->getRawValue())) {
1824 ConstantUInt *ShiftCI = ConstantUInt::get(Type::UByteTy, Shift);
1825 emitShiftOperation(MBB, IP, Op0, ShiftCI, true, Op0->getType(), DestReg);
1829 // If 32 bits or less and immediate is in right range, emit mul by immediate
1830 if (Class == cByte || Class == cShort || Class == cInt) {
1831 if (canUseAsImmediateForOpcode(CI, 0)) {
1832 unsigned Op0r = getReg(Op0, MBB, IP);
1833 unsigned imm = CI->getRawValue() & 0xFFFF;
1834 BuildMI(*MBB, IP, PPC::MULLI, 2, DestReg).addReg(Op0r).addSImm(imm);
1839 doMultiply(MBB, IP, DestReg, Op0, CI);
1842 void PPC64ISel::visitMul(BinaryOperator &I) {
1843 unsigned ResultReg = getReg(I);
1845 Value *Op0 = I.getOperand(0);
1846 Value *Op1 = I.getOperand(1);
1848 MachineBasicBlock::iterator IP = BB->end();
1849 emitMultiply(BB, IP, Op0, Op1, ResultReg);
1852 void PPC64ISel::emitMultiply(MachineBasicBlock *MBB,
1853 MachineBasicBlock::iterator IP,
1854 Value *Op0, Value *Op1, unsigned DestReg) {
1855 TypeClass Class = getClass(Op0->getType());
1862 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
1863 doMultiplyConst(MBB, IP, DestReg, Op0, CI);
1865 doMultiply(MBB, IP, DestReg, Op0, Op1);
1870 emitBinaryFPOperation(MBB, IP, Op0, Op1, 2, DestReg);
1877 /// visitDivRem - Handle division and remainder instructions... these
1878 /// instruction both require the same instructions to be generated, they just
1879 /// select the result from a different register. Note that both of these
1880 /// instructions work differently for signed and unsigned operands.
1882 void PPC64ISel::visitDivRem(BinaryOperator &I) {
1883 unsigned ResultReg = getReg(I);
1884 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1886 MachineBasicBlock::iterator IP = BB->end();
1887 emitDivRemOperation(BB, IP, Op0, Op1, I.getOpcode() == Instruction::Div,
1891 void PPC64ISel::emitDivRemOperation(MachineBasicBlock *BB,
1892 MachineBasicBlock::iterator IP,
1893 Value *Op0, Value *Op1, bool isDiv,
1894 unsigned ResultReg) {
1895 const Type *Ty = Op0->getType();
1896 unsigned Class = getClass(Ty);
1900 // Floating point divide...
1901 emitBinaryFPOperation(BB, IP, Op0, Op1, 3, ResultReg);
1904 // Floating point remainder via fmodf(float x, float y);
1905 unsigned Op0Reg = getReg(Op0, BB, IP);
1906 unsigned Op1Reg = getReg(Op1, BB, IP);
1907 MachineInstr *TheCall =
1908 BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(fmodfFn, true);
1909 std::vector<ValueRecord> Args;
1910 Args.push_back(ValueRecord(Op0Reg, Type::FloatTy));
1911 Args.push_back(ValueRecord(Op1Reg, Type::FloatTy));
1912 doCall(ValueRecord(ResultReg, Type::FloatTy), TheCall, Args, false);
1917 // Floating point divide...
1918 emitBinaryFPOperation(BB, IP, Op0, Op1, 3, ResultReg);
1921 // Floating point remainder via fmod(double x, double y);
1922 unsigned Op0Reg = getReg(Op0, BB, IP);
1923 unsigned Op1Reg = getReg(Op1, BB, IP);
1924 MachineInstr *TheCall =
1925 BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(fmodFn, true);
1926 std::vector<ValueRecord> Args;
1927 Args.push_back(ValueRecord(Op0Reg, Type::DoubleTy));
1928 Args.push_back(ValueRecord(Op1Reg, Type::DoubleTy));
1929 doCall(ValueRecord(ResultReg, Type::DoubleTy), TheCall, Args, false);
1932 case cLong: case cByte: case cShort: case cInt:
1933 break; // Small integrals, handled below...
1934 default: assert(0 && "Unknown class!");
1937 // Special case signed division by power of 2.
1939 if (ConstantSInt *CI = dyn_cast<ConstantSInt>(Op1)) {
1940 assert(Class != cLong && "This doesn't handle 64-bit divides!");
1941 int V = CI->getValue();
1943 if (V == 1) { // X /s 1 => X
1944 unsigned Op0Reg = getReg(Op0, BB, IP);
1945 BuildMI(*BB, IP, PPC::OR, 2, ResultReg).addReg(Op0Reg).addReg(Op0Reg);
1949 if (V == -1) { // X /s -1 => -X
1950 unsigned Op0Reg = getReg(Op0, BB, IP);
1951 BuildMI(*BB, IP, PPC::NEG, 1, ResultReg).addReg(Op0Reg);
1955 unsigned log2V = ExactLog2(V);
1956 if (log2V != 0 && Ty->isSigned()) {
1957 unsigned Op0Reg = getReg(Op0, BB, IP);
1958 unsigned TmpReg = makeAnotherReg(Op0->getType());
1959 unsigned Opcode = Class == cLong ? PPC::SRADI : PPC::SRAWI;
1961 BuildMI(*BB, IP, Opcode, 2, TmpReg).addReg(Op0Reg).addImm(log2V);
1962 BuildMI(*BB, IP, PPC::ADDZE, 1, ResultReg).addReg(TmpReg);
1967 static const unsigned DivOpcodes[] =
1968 { PPC::DIVWU, PPC::DIVW, PPC::DIVDU, PPC::DIVD };
1970 unsigned Op0Reg = getReg(Op0, BB, IP);
1971 unsigned Op1Reg = getReg(Op1, BB, IP);
1972 unsigned Opcode = DivOpcodes[2*(Class == cLong) + Ty->isSigned()];
1975 BuildMI(*BB, IP, Opcode, 2, ResultReg).addReg(Op0Reg).addReg(Op1Reg);
1976 } else { // Remainder
1977 unsigned TmpReg1 = makeAnotherReg(Op0->getType());
1978 unsigned TmpReg2 = makeAnotherReg(Op0->getType());
1979 unsigned MulOpcode = Class == cLong ? PPC::MULLD : PPC::MULLW;
1981 BuildMI(*BB, IP, Opcode, 2, TmpReg1).addReg(Op0Reg).addReg(Op1Reg);
1982 BuildMI(*BB, IP, MulOpcode, 2, TmpReg2).addReg(TmpReg1).addReg(Op1Reg);
1983 BuildMI(*BB, IP, PPC::SUBF, 2, ResultReg).addReg(TmpReg2).addReg(Op0Reg);
1988 /// Shift instructions: 'shl', 'sar', 'shr' - Some special cases here
1989 /// for constant immediate shift values, and for constant immediate
1990 /// shift values equal to 1. Even the general case is sort of special,
1991 /// because the shift amount has to be in CL, not just any old register.
1993 void PPC64ISel::visitShiftInst(ShiftInst &I) {
1994 MachineBasicBlock::iterator IP = BB->end();
1995 emitShiftOperation(BB, IP, I.getOperand(0), I.getOperand(1),
1996 I.getOpcode() == Instruction::Shl, I.getType(),
2000 /// emitShiftOperation - Common code shared between visitShiftInst and
2001 /// constant expression support.
2003 void PPC64ISel::emitShiftOperation(MachineBasicBlock *MBB,
2004 MachineBasicBlock::iterator IP,
2005 Value *Op, Value *ShiftAmount,
2006 bool isLeftShift, const Type *ResultTy,
2008 unsigned SrcReg = getReg (Op, MBB, IP);
2009 bool isSigned = ResultTy->isSigned ();
2010 unsigned Class = getClass (ResultTy);
2012 // Longs, as usual, are handled specially...
2013 if (Class == cLong) {
2014 // If we have a constant shift, we can generate much more efficient code
2015 // than otherwise...
2017 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(ShiftAmount)) {
2018 unsigned Amount = CUI->getValue();
2019 assert(Amount < 64 && "Invalid immediate shift amount!");
2021 BuildMI(*MBB, IP, PPC::RLDICR, 3, DestReg).addReg(SrcReg).addImm(Amount)
2025 BuildMI(*MBB, IP, PPC::SRADI, 2, DestReg).addReg(SrcReg)
2028 BuildMI(*MBB, IP, PPC::RLDICL, 3, DestReg).addReg(SrcReg)
2029 .addImm(64-Amount).addImm(Amount);
2033 unsigned ShiftReg = getReg (ShiftAmount, MBB, IP);
2036 BuildMI(*MBB, IP, PPC::SLD, 2, DestReg).addReg(SrcReg).addReg(ShiftReg);
2038 unsigned Opcode = (isSigned) ? PPC::SRAD : PPC::SRD;
2039 BuildMI(*MBB, IP, Opcode, DestReg).addReg(SrcReg).addReg(ShiftReg);
2045 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(ShiftAmount)) {
2046 // The shift amount is constant, guaranteed to be a ubyte. Get its value.
2047 assert(CUI->getType() == Type::UByteTy && "Shift amount not a ubyte?");
2048 unsigned Amount = CUI->getValue();
2051 BuildMI(*MBB, IP, PPC::RLWINM, 4, DestReg).addReg(SrcReg)
2052 .addImm(Amount).addImm(0).addImm(31-Amount);
2055 BuildMI(*MBB, IP, PPC::SRAWI,2,DestReg).addReg(SrcReg).addImm(Amount);
2057 BuildMI(*MBB, IP, PPC::RLWINM, 4, DestReg).addReg(SrcReg)
2058 .addImm(32-Amount).addImm(Amount).addImm(31);
2061 } else { // The shift amount is non-constant.
2062 unsigned ShiftAmountReg = getReg(ShiftAmount, MBB, IP);
2065 BuildMI(*MBB, IP, PPC::SLW, 2, DestReg).addReg(SrcReg)
2066 .addReg(ShiftAmountReg);
2068 BuildMI(*MBB, IP, isSigned ? PPC::SRAW : PPC::SRW, 2, DestReg)
2069 .addReg(SrcReg).addReg(ShiftAmountReg);
2075 /// visitLoadInst - Implement LLVM load instructions. Pretty straightforward
2076 /// mapping of LLVM classes to PPC load instructions, with the exception of
2077 /// signed byte loads, which need a sign extension following them.
2079 void PPC64ISel::visitLoadInst(LoadInst &I) {
2080 // Immediate opcodes, for reg+imm addressing
2081 static const unsigned ImmOpcodes[] = {
2082 PPC::LBZ, PPC::LHZ, PPC::LWZ,
2083 PPC::LFS, PPC::LFD, PPC::LWZ
2085 // Indexed opcodes, for reg+reg addressing
2086 static const unsigned IdxOpcodes[] = {
2087 PPC::LBZX, PPC::LHZX, PPC::LWZX,
2088 PPC::LFSX, PPC::LFDX, PPC::LWZX
2091 unsigned Class = getClassB(I.getType());
2092 unsigned ImmOpcode = ImmOpcodes[Class];
2093 unsigned IdxOpcode = IdxOpcodes[Class];
2094 unsigned DestReg = getReg(I);
2095 Value *SourceAddr = I.getOperand(0);
2097 if (Class == cShort && I.getType()->isSigned()) ImmOpcode = PPC::LHA;
2098 if (Class == cShort && I.getType()->isSigned()) IdxOpcode = PPC::LHAX;
2100 if (AllocaInst *AI = dyn_castFixedAlloca(SourceAddr)) {
2101 unsigned FI = getFixedSizedAllocaFI(AI);
2102 if (Class == cByte && I.getType()->isSigned()) {
2103 unsigned TmpReg = makeAnotherReg(I.getType());
2104 addFrameReference(BuildMI(BB, ImmOpcode, 2, TmpReg), FI);
2105 BuildMI(BB, PPC::EXTSB, 1, DestReg).addReg(TmpReg);
2107 addFrameReference(BuildMI(BB, ImmOpcode, 2, DestReg), FI);
2112 // If this load is the only use of the GEP instruction that is its address,
2113 // then we can fold the GEP directly into the load instruction.
2114 // emitGEPOperation with a second to last arg of 'true' will place the
2115 // base register for the GEP into baseReg, and the constant offset from that
2116 // into offset. If the offset fits in 16 bits, we can emit a reg+imm store
2117 // otherwise, we copy the offset into another reg, and use reg+reg addressing.
2118 if (GetElementPtrInst *GEPI = canFoldGEPIntoLoadOrStore(SourceAddr)) {
2119 unsigned baseReg = getReg(GEPI);
2120 unsigned pendingAdd;
2121 ConstantSInt *offset;
2123 emitGEPOperation(BB, BB->end(), GEPI->getOperand(0), GEPI->op_begin()+1,
2124 GEPI->op_end(), baseReg, true, &offset, &pendingAdd);
2126 if (pendingAdd == 0 && Class != cLong &&
2127 canUseAsImmediateForOpcode(offset, 0)) {
2128 if (Class == cByte && I.getType()->isSigned()) {
2129 unsigned TmpReg = makeAnotherReg(I.getType());
2130 BuildMI(BB, ImmOpcode, 2, TmpReg).addSImm(offset->getValue())
2132 BuildMI(BB, PPC::EXTSB, 1, DestReg).addReg(TmpReg);
2134 BuildMI(BB, ImmOpcode, 2, DestReg).addSImm(offset->getValue())
2140 unsigned indexReg = (pendingAdd != 0) ? pendingAdd : getReg(offset);
2142 if (Class == cByte && I.getType()->isSigned()) {
2143 unsigned TmpReg = makeAnotherReg(I.getType());
2144 BuildMI(BB, IdxOpcode, 2, TmpReg).addReg(indexReg).addReg(baseReg);
2145 BuildMI(BB, PPC::EXTSB, 1, DestReg).addReg(TmpReg);
2147 BuildMI(BB, IdxOpcode, 2, DestReg).addReg(indexReg).addReg(baseReg);
2152 // The fallback case, where the load was from a source that could not be
2153 // folded into the load instruction.
2154 unsigned SrcAddrReg = getReg(SourceAddr);
2156 if (Class == cByte && I.getType()->isSigned()) {
2157 unsigned TmpReg = makeAnotherReg(I.getType());
2158 BuildMI(BB, ImmOpcode, 2, TmpReg).addSImm(0).addReg(SrcAddrReg);
2159 BuildMI(BB, PPC::EXTSB, 1, DestReg).addReg(TmpReg);
2161 BuildMI(BB, ImmOpcode, 2, DestReg).addSImm(0).addReg(SrcAddrReg);
2165 /// visitStoreInst - Implement LLVM store instructions
2167 void PPC64ISel::visitStoreInst(StoreInst &I) {
2168 // Immediate opcodes, for reg+imm addressing
2169 static const unsigned ImmOpcodes[] = {
2170 PPC::STB, PPC::STH, PPC::STW,
2171 PPC::STFS, PPC::STFD, PPC::STW
2173 // Indexed opcodes, for reg+reg addressing
2174 static const unsigned IdxOpcodes[] = {
2175 PPC::STBX, PPC::STHX, PPC::STWX,
2176 PPC::STFSX, PPC::STFDX, PPC::STWX
2179 Value *SourceAddr = I.getOperand(1);
2180 const Type *ValTy = I.getOperand(0)->getType();
2181 unsigned Class = getClassB(ValTy);
2182 unsigned ImmOpcode = ImmOpcodes[Class];
2183 unsigned IdxOpcode = IdxOpcodes[Class];
2184 unsigned ValReg = getReg(I.getOperand(0));
2186 // If this store is the only use of the GEP instruction that is its address,
2187 // then we can fold the GEP directly into the store instruction.
2188 // emitGEPOperation with a second to last arg of 'true' will place the
2189 // base register for the GEP into baseReg, and the constant offset from that
2190 // into offset. If the offset fits in 16 bits, we can emit a reg+imm store
2191 // otherwise, we copy the offset into another reg, and use reg+reg addressing.
2192 if (GetElementPtrInst *GEPI = canFoldGEPIntoLoadOrStore(SourceAddr)) {
2193 unsigned baseReg = getReg(GEPI);
2194 unsigned pendingAdd;
2195 ConstantSInt *offset;
2197 emitGEPOperation(BB, BB->end(), GEPI->getOperand(0), GEPI->op_begin()+1,
2198 GEPI->op_end(), baseReg, true, &offset, &pendingAdd);
2200 if (0 == pendingAdd && Class != cLong &&
2201 canUseAsImmediateForOpcode(offset, 0)) {
2202 BuildMI(BB, ImmOpcode, 3).addReg(ValReg).addSImm(offset->getValue())
2207 unsigned indexReg = (pendingAdd != 0) ? pendingAdd : getReg(offset);
2208 BuildMI(BB, IdxOpcode, 3).addReg(ValReg).addReg(indexReg).addReg(baseReg);
2212 // If the store address wasn't the only use of a GEP, we fall back to the
2213 // standard path: store the ValReg at the value in AddressReg.
2214 unsigned AddressReg = getReg(I.getOperand(1));
2215 BuildMI(BB, ImmOpcode, 3).addReg(ValReg).addSImm(0).addReg(AddressReg);
2219 /// visitCastInst - Here we have various kinds of copying with or without sign
2220 /// extension going on.
2222 void PPC64ISel::visitCastInst(CastInst &CI) {
2223 Value *Op = CI.getOperand(0);
2225 unsigned SrcClass = getClassB(Op->getType());
2226 unsigned DestClass = getClassB(CI.getType());
2228 // If this is a cast from a 32-bit integer to a Long type, and the only uses
2229 // of the case are GEP instructions, then the cast does not need to be
2230 // generated explicitly, it will be folded into the GEP.
2231 if (DestClass == cLong && SrcClass == cInt) {
2232 bool AllUsesAreGEPs = true;
2233 for (Value::use_iterator I = CI.use_begin(), E = CI.use_end(); I != E; ++I)
2234 if (!isa<GetElementPtrInst>(*I)) {
2235 AllUsesAreGEPs = false;
2239 // No need to codegen this cast if all users are getelementptr instrs...
2240 if (AllUsesAreGEPs) return;
2243 unsigned DestReg = getReg(CI);
2244 MachineBasicBlock::iterator MI = BB->end();
2245 emitCastOperation(BB, MI, Op, CI.getType(), DestReg);
2248 /// emitCastOperation - Common code shared between visitCastInst and constant
2249 /// expression cast support.
2251 void PPC64ISel::emitCastOperation(MachineBasicBlock *MBB,
2252 MachineBasicBlock::iterator IP,
2253 Value *Src, const Type *DestTy,
2255 const Type *SrcTy = Src->getType();
2256 unsigned SrcClass = getClassB(SrcTy);
2257 unsigned DestClass = getClassB(DestTy);
2258 unsigned SrcReg = getReg(Src, MBB, IP);
2260 // Implement casts to bool by using compare on the operand followed by set if
2261 // not zero on the result.
2262 if (DestTy == Type::BoolTy) {
2268 unsigned TmpReg = makeAnotherReg(Type::IntTy);
2269 BuildMI(*MBB, IP, PPC::ADDIC, 2, TmpReg).addReg(SrcReg).addSImm(-1);
2270 BuildMI(*MBB, IP, PPC::SUBFE, 2, DestReg).addReg(TmpReg).addReg(SrcReg);
2276 std::cerr << "ERROR: Cast fp-to-bool not implemented!\n";
2282 // Handle cast of Float -> Double
2283 if (SrcClass == cFP32 && DestClass == cFP64) {
2284 BuildMI(*MBB, IP, PPC::FMR, 1, DestReg).addReg(SrcReg);
2288 // Handle cast of Double -> Float
2289 if (SrcClass == cFP64 && DestClass == cFP32) {
2290 BuildMI(*MBB, IP, PPC::FRSP, 1, DestReg).addReg(SrcReg);
2294 // Handle casts from integer to floating point now...
2295 if (DestClass == cFP32 || DestClass == cFP64) {
2297 // Spill the integer to memory and reload it from there.
2298 unsigned TmpReg = makeAnotherReg(Type::DoubleTy);
2300 F->getFrameInfo()->CreateStackObject(Type::DoubleTy, TM.getTargetData());
2302 if (SrcClass == cLong) {
2303 if (SrcTy->isSigned()) {
2304 addFrameReference(BuildMI(*MBB, IP, PPC::STD, 3).addReg(SrcReg),
2306 addFrameReference(BuildMI(*MBB, IP, PPC::LFD, 2, TmpReg),
2308 BuildMI(*MBB, IP, PPC::FCFID, 1, DestReg).addReg(TmpReg);
2310 unsigned Scale = getReg(ConstantFP::get(Type::DoubleTy, 0x1p32));
2311 unsigned TmpHi = makeAnotherReg(Type::IntTy);
2312 unsigned TmpLo = makeAnotherReg(Type::IntTy);
2313 unsigned FPLow = makeAnotherReg(Type::DoubleTy);
2314 unsigned FPTmpHi = makeAnotherReg(Type::DoubleTy);
2315 unsigned FPTmpLo = makeAnotherReg(Type::DoubleTy);
2316 int OtherFrameIdx = F->getFrameInfo()->CreateStackObject(Type::DoubleTy,
2317 TM.getTargetData());
2318 BuildMI(*MBB, IP, PPC::RLDICL, 3, TmpHi).addReg(SrcReg).addImm(32)
2320 BuildMI(*MBB, IP, PPC::RLDICL, 3, TmpLo).addReg(SrcReg).addImm(0)
2322 addFrameReference(BuildMI(*MBB, IP, PPC::STD, 3).addReg(TmpHi),
2324 addFrameReference(BuildMI(*MBB, IP, PPC::STD, 3).addReg(TmpLo),
2326 addFrameReference(BuildMI(*MBB, IP, PPC::LFD, 2, TmpReg),
2328 addFrameReference(BuildMI(*MBB, IP, PPC::LFD, 2, FPLow),
2330 BuildMI(*MBB, IP, PPC::FCFID, 1, FPTmpHi).addReg(TmpReg);
2331 BuildMI(*MBB, IP, PPC::FCFID, 1, FPTmpLo).addReg(FPLow);
2332 BuildMI(*MBB, IP, PPC::FMADD, 3, DestReg).addReg(Scale).addReg(FPTmpHi)
2338 // FIXME: really want a promote64
2339 unsigned IntTmp = makeAnotherReg(Type::IntTy);
2341 if (SrcTy->isSigned())
2342 BuildMI(*MBB, IP, PPC::EXTSW, 1, IntTmp).addReg(SrcReg);
2344 BuildMI(*MBB, IP, PPC::RLDICL, 3, IntTmp).addReg(SrcReg).addImm(0)
2346 addFrameReference(BuildMI(*MBB, IP, PPC::STD, 3).addReg(IntTmp),
2348 addFrameReference(BuildMI(*MBB, IP, PPC::LFD, 2, TmpReg),
2350 BuildMI(*MBB, IP, PPC::FCFID, 1, DestReg).addReg(TmpReg);
2354 // Handle casts from floating point to integer now...
2355 if (SrcClass == cFP32 || SrcClass == cFP64) {
2356 static Function* const Funcs[] =
2357 { __fixsfdiFn, __fixdfdiFn, __fixunssfdiFn, __fixunsdfdiFn };
2358 // emit library call
2359 if (DestClass == cLong) {
2360 bool isDouble = SrcClass == cFP64;
2361 unsigned nameIndex = 2 * DestTy->isSigned() + isDouble;
2362 std::vector<ValueRecord> Args;
2363 Args.push_back(ValueRecord(SrcReg, SrcTy));
2364 Function *floatFn = Funcs[nameIndex];
2365 MachineInstr *TheCall =
2366 BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(floatFn, true);
2367 doCall(ValueRecord(DestReg, DestTy), TheCall, Args, false);
2372 F->getFrameInfo()->CreateStackObject(SrcTy, TM.getTargetData());
2374 if (DestTy->isSigned()) {
2375 unsigned TempReg = makeAnotherReg(Type::DoubleTy);
2377 // Convert to integer in the FP reg and store it to a stack slot
2378 BuildMI(*BB, IP, PPC::FCTIWZ, 1, TempReg).addReg(SrcReg);
2379 addFrameReference(BuildMI(*BB, IP, PPC::STFD, 3)
2380 .addReg(TempReg), ValueFrameIdx);
2382 // There is no load signed byte opcode, so we must emit a sign extend for
2383 // that particular size. Make sure to source the new integer from the
2385 if (DestClass == cByte) {
2386 unsigned TempReg2 = makeAnotherReg(DestTy);
2387 addFrameReference(BuildMI(*BB, IP, PPC::LBZ, 2, TempReg2),
2389 BuildMI(*MBB, IP, PPC::EXTSB, DestReg).addReg(TempReg2);
2391 int offset = (DestClass == cShort) ? 6 : 4;
2392 unsigned LoadOp = (DestClass == cShort) ? PPC::LHA : PPC::LWZ;
2393 addFrameReference(BuildMI(*BB, IP, LoadOp, 2, DestReg),
2394 ValueFrameIdx, offset);
2397 unsigned Zero = getReg(ConstantFP::get(Type::DoubleTy, 0.0f));
2398 double maxInt = (1LL << 32) - 1;
2399 unsigned MaxInt = getReg(ConstantFP::get(Type::DoubleTy, maxInt));
2400 double border = 1LL << 31;
2401 unsigned Border = getReg(ConstantFP::get(Type::DoubleTy, border));
2402 unsigned UseZero = makeAnotherReg(Type::DoubleTy);
2403 unsigned UseMaxInt = makeAnotherReg(Type::DoubleTy);
2404 unsigned UseChoice = makeAnotherReg(Type::DoubleTy);
2405 unsigned TmpReg = makeAnotherReg(Type::DoubleTy);
2406 unsigned TmpReg2 = makeAnotherReg(Type::DoubleTy);
2407 unsigned ConvReg = makeAnotherReg(Type::DoubleTy);
2408 unsigned IntTmp = makeAnotherReg(Type::IntTy);
2409 unsigned XorReg = makeAnotherReg(Type::IntTy);
2411 F->getFrameInfo()->CreateStackObject(SrcTy, TM.getTargetData());
2412 // Update machine-CFG edges
2413 MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock());
2414 MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock());
2415 MachineBasicBlock *OldMBB = BB;
2416 ilist<MachineBasicBlock>::iterator It = BB; ++It;
2417 F->getBasicBlockList().insert(It, XorMBB);
2418 F->getBasicBlockList().insert(It, PhiMBB);
2419 BB->addSuccessor(XorMBB);
2420 BB->addSuccessor(PhiMBB);
2422 // Convert from floating point to unsigned 32-bit value
2423 // Use 0 if incoming value is < 0.0
2424 BuildMI(*BB, IP, PPC::FSEL, 3, UseZero).addReg(SrcReg).addReg(SrcReg)
2426 // Use 2**32 - 1 if incoming value is >= 2**32
2427 BuildMI(*BB, IP, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(SrcReg);
2428 BuildMI(*BB, IP, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt)
2429 .addReg(UseZero).addReg(MaxInt);
2431 BuildMI(*BB, IP, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border);
2432 // Use difference if >= 2**31
2433 BuildMI(*BB, IP, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice)
2435 BuildMI(*BB, IP, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg)
2437 // Convert to integer
2438 BuildMI(*BB, IP, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2);
2439 addFrameReference(BuildMI(*BB, IP, PPC::STFD, 3).addReg(ConvReg),
2441 if (DestClass == cByte) {
2442 addFrameReference(BuildMI(*BB, IP, PPC::LBZ, 2, DestReg),
2444 } else if (DestClass == cShort) {
2445 addFrameReference(BuildMI(*BB, IP, PPC::LHZ, 2, DestReg),
2447 } if (DestClass == cInt) {
2448 addFrameReference(BuildMI(*BB, IP, PPC::LWZ, 2, IntTmp),
2450 BuildMI(*BB, IP, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB);
2451 BuildMI(*BB, IP, PPC::B, 1).addMBB(XorMBB);
2454 // add 2**31 if input was >= 2**31
2456 BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000);
2457 XorMBB->addSuccessor(PhiMBB);
2460 // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ]
2462 BuildMI(BB, PPC::PHI, 4, DestReg).addReg(IntTmp).addMBB(OldMBB)
2463 .addReg(XorReg).addMBB(XorMBB);
2469 // Check our invariants
2470 assert((SrcClass <= cInt || SrcClass == cLong) &&
2471 "Unhandled source class for cast operation!");
2472 assert((DestClass <= cInt || DestClass == cLong) &&
2473 "Unhandled destination class for cast operation!");
2475 bool sourceUnsigned = SrcTy->isUnsigned() || SrcTy == Type::BoolTy;
2476 bool destUnsigned = DestTy->isUnsigned();
2478 // Unsigned -> Unsigned, clear if larger
2479 if (sourceUnsigned && destUnsigned) {
2480 // handle long dest class now to keep switch clean
2481 if (DestClass == cLong) {
2482 BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
2486 // handle u{ byte, short, int } x u{ byte, short, int }
2487 unsigned clearBits = (SrcClass == cByte || DestClass == cByte) ? 24 : 16;
2491 if (SrcClass == DestClass)
2492 BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
2494 BuildMI(*MBB, IP, PPC::RLWINM, 4, DestReg).addReg(SrcReg)
2495 .addImm(0).addImm(clearBits).addImm(31);
2499 if (DestClass == cInt)
2500 BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
2502 BuildMI(*MBB, IP, PPC::RLWINM, 4, DestReg).addReg(SrcReg)
2503 .addImm(0).addImm(clearBits).addImm(31);
2510 if (!sourceUnsigned && !destUnsigned) {
2511 // handle long dest class now to keep switch clean
2512 if (DestClass == cLong) {
2513 BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
2517 // handle { byte, short, int } x { byte, short, int }
2520 if (DestClass == cByte)
2521 BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
2523 BuildMI(*MBB, IP, PPC::EXTSB, 1, DestReg).addReg(SrcReg);
2526 if (DestClass == cByte)
2527 BuildMI(*MBB, IP, PPC::EXTSB, 1, DestReg).addReg(SrcReg);
2528 else if (DestClass == cShort)
2529 BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
2531 BuildMI(*MBB, IP, PPC::EXTSH, 1, DestReg).addReg(SrcReg);
2535 if (DestClass == cByte)
2536 BuildMI(*MBB, IP, PPC::EXTSB, 1, DestReg).addReg(SrcReg);
2537 else if (DestClass == cShort)
2538 BuildMI(*MBB, IP, PPC::EXTSH, 1, DestReg).addReg(SrcReg);
2540 BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
2546 // Unsigned -> Signed
2547 if (sourceUnsigned && !destUnsigned) {
2548 // handle long dest class now to keep switch clean
2549 if (DestClass == cLong) {
2550 BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
2554 // handle u{ byte, short, int } -> { byte, short, int }
2557 if (DestClass == cByte)
2558 // uByte 255 -> signed byte == -1
2559 BuildMI(*MBB, IP, PPC::EXTSB, 1, DestReg).addReg(SrcReg);
2561 // uByte 255 -> signed short/int == 255
2562 BuildMI(*MBB, IP, PPC::RLWINM, 4, DestReg).addReg(SrcReg).addImm(0)
2563 .addImm(24).addImm(31);
2566 if (DestClass == cByte)
2567 BuildMI(*MBB, IP, PPC::EXTSB, 1, DestReg).addReg(SrcReg);
2568 else if (DestClass == cShort)
2569 BuildMI(*MBB, IP, PPC::EXTSH, 1, DestReg).addReg(SrcReg);
2571 BuildMI(*MBB, IP, PPC::RLWINM, 4, DestReg).addReg(SrcReg).addImm(0)
2572 .addImm(16).addImm(31);
2576 if (DestClass == cByte)
2577 BuildMI(*MBB, IP, PPC::EXTSB, 1, DestReg).addReg(SrcReg);
2578 else if (DestClass == cShort)
2579 BuildMI(*MBB, IP, PPC::EXTSH, 1, DestReg).addReg(SrcReg);
2581 BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
2587 // Signed -> Unsigned
2588 if (!sourceUnsigned && destUnsigned) {
2589 // handle long dest class now to keep switch clean
2590 if (DestClass == cLong) {
2591 BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
2595 // handle { byte, short, int } -> u{ byte, short, int }
2596 unsigned clearBits = (DestClass == cByte) ? 24 : 16;
2600 if (DestClass == cByte || DestClass == cShort)
2601 // sbyte -1 -> ubyte 0x000000FF
2602 BuildMI(*MBB, IP, PPC::RLWINM, 4, DestReg).addReg(SrcReg)
2603 .addImm(0).addImm(clearBits).addImm(31);
2605 // sbyte -1 -> ubyte 0xFFFFFFFF
2606 BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
2610 if (DestClass == cInt)
2611 BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg);
2613 BuildMI(*MBB, IP, PPC::RLWINM, 4, DestReg).addReg(SrcReg)
2614 .addImm(0).addImm(clearBits).addImm(31);
2620 // Anything we haven't handled already, we can't (yet) handle at all.
2621 std::cerr << "Unhandled cast from " << SrcTy->getDescription()
2622 << "to " << DestTy->getDescription() << '\n';
2626 /// visitVANextInst - Implement the va_next instruction...
2628 void PPC64ISel::visitVANextInst(VANextInst &I) {
2629 unsigned VAList = getReg(I.getOperand(0));
2630 unsigned DestReg = getReg(I);
2633 switch (I.getArgType()->getTypeID()) {
2636 assert(0 && "Error: bad type for va_next instruction!");
2638 case Type::PointerTyID:
2639 case Type::UIntTyID:
2643 case Type::ULongTyID:
2644 case Type::LongTyID:
2645 case Type::DoubleTyID:
2650 // Increment the VAList pointer...
2651 BuildMI(BB, PPC::ADDI, 2, DestReg).addReg(VAList).addSImm(Size);
2654 void PPC64ISel::visitVAArgInst(VAArgInst &I) {
2655 unsigned VAList = getReg(I.getOperand(0));
2656 unsigned DestReg = getReg(I);
2658 switch (I.getType()->getTypeID()) {
2661 assert(0 && "Error: bad type for va_next instruction!");
2663 case Type::PointerTyID:
2664 case Type::UIntTyID:
2666 BuildMI(BB, PPC::LWZ, 2, DestReg).addSImm(0).addReg(VAList);
2668 case Type::ULongTyID:
2669 case Type::LongTyID:
2670 BuildMI(BB, PPC::LD, 2, DestReg).addSImm(0).addReg(VAList);
2672 case Type::FloatTyID:
2673 BuildMI(BB, PPC::LFS, 2, DestReg).addSImm(0).addReg(VAList);
2675 case Type::DoubleTyID:
2676 BuildMI(BB, PPC::LFD, 2, DestReg).addSImm(0).addReg(VAList);
2681 /// visitGetElementPtrInst - instruction-select GEP instructions
2683 void PPC64ISel::visitGetElementPtrInst(GetElementPtrInst &I) {
2684 if (canFoldGEPIntoLoadOrStore(&I))
2687 unsigned outputReg = getReg(I);
2688 emitGEPOperation(BB, BB->end(), I.getOperand(0), I.op_begin()+1, I.op_end(),
2689 outputReg, false, 0, 0);
2692 /// emitGEPOperation - Common code shared between visitGetElementPtrInst and
2693 /// constant expression GEP support.
2695 void PPC64ISel::emitGEPOperation(MachineBasicBlock *MBB,
2696 MachineBasicBlock::iterator IP,
2697 Value *Src, User::op_iterator IdxBegin,
2698 User::op_iterator IdxEnd, unsigned TargetReg,
2699 bool GEPIsFolded, ConstantSInt **RemainderPtr,
2700 unsigned *PendingAddReg) {
2701 const TargetData &TD = TM.getTargetData();
2702 const Type *Ty = Src->getType();
2703 unsigned basePtrReg = getReg(Src, MBB, IP);
2704 int64_t constValue = 0;
2706 // Record the operations to emit the GEP in a vector so that we can emit them
2707 // after having analyzed the entire instruction.
2708 std::vector<CollapsedGepOp> ops;
2710 // GEPs have zero or more indices; we must perform a struct access
2711 // or array access for each one.
2712 for (GetElementPtrInst::op_iterator oi = IdxBegin, oe = IdxEnd; oi != oe;
2715 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
2716 // It's a struct access. idx is the index into the structure,
2717 // which names the field. Use the TargetData structure to
2718 // pick out what the layout of the structure is in memory.
2719 // Use the (constant) structure index's value to find the
2720 // right byte offset from the StructLayout class's list of
2721 // structure member offsets.
2722 unsigned fieldIndex = cast<ConstantUInt>(idx)->getValue();
2723 unsigned memberOffset =
2724 TD.getStructLayout(StTy)->MemberOffsets[fieldIndex];
2726 // StructType member offsets are always constant values. Add it to the
2728 constValue += memberOffset;
2730 // The next type is the member of the structure selected by the
2732 Ty = StTy->getElementType (fieldIndex);
2733 } else if (const SequentialType *SqTy = dyn_cast<SequentialType> (Ty)) {
2734 // Many GEP instructions use a [cast (int/uint) to LongTy] as their
2735 // operand. Handle this case directly now...
2736 if (CastInst *CI = dyn_cast<CastInst>(idx))
2737 if (CI->getOperand(0)->getType() == Type::IntTy ||
2738 CI->getOperand(0)->getType() == Type::UIntTy)
2739 idx = CI->getOperand(0);
2741 // It's an array or pointer access: [ArraySize x ElementType].
2742 // We want to add basePtrReg to (idxReg * sizeof ElementType). First, we
2743 // must find the size of the pointed-to type (Not coincidentally, the next
2744 // type is the type of the elements in the array).
2745 Ty = SqTy->getElementType();
2746 unsigned elementSize = TD.getTypeSize(Ty);
2748 if (ConstantInt *C = dyn_cast<ConstantInt>(idx)) {
2749 if (ConstantSInt *CS = dyn_cast<ConstantSInt>(C))
2750 constValue += CS->getValue() * elementSize;
2751 else if (ConstantUInt *CU = dyn_cast<ConstantUInt>(C))
2752 constValue += CU->getValue() * elementSize;
2754 assert(0 && "Invalid ConstantInt GEP index type!");
2756 // Push current gep state to this point as an add
2757 ops.push_back(CollapsedGepOp(false, 0,
2758 ConstantSInt::get(Type::IntTy,constValue)));
2760 // Push multiply gep op and reset constant value
2761 ops.push_back(CollapsedGepOp(true, idx,
2762 ConstantSInt::get(Type::IntTy, elementSize)));
2768 // Emit instructions for all the collapsed ops
2769 bool pendingAdd = false;
2770 unsigned pendingAddReg = 0;
2772 for(std::vector<CollapsedGepOp>::iterator cgo_i = ops.begin(),
2773 cgo_e = ops.end(); cgo_i != cgo_e; ++cgo_i) {
2774 CollapsedGepOp& cgo = *cgo_i;
2775 unsigned nextBasePtrReg = makeAnotherReg(Type::IntTy);
2777 // If we didn't emit an add last time through the loop, we need to now so
2778 // that the base reg is updated appropriately.
2780 assert(pendingAddReg != 0 && "Uninitialized register in pending add!");
2781 BuildMI(*MBB, IP, PPC::ADD, 2, nextBasePtrReg).addReg(basePtrReg)
2782 .addReg(pendingAddReg);
2783 basePtrReg = nextBasePtrReg;
2784 nextBasePtrReg = makeAnotherReg(Type::IntTy);
2790 // We know the elementSize is a constant, so we can emit a constant mul
2791 unsigned TmpReg = makeAnotherReg(Type::IntTy);
2792 doMultiplyConst(MBB, IP, nextBasePtrReg, cgo.index, cgo.size);
2793 pendingAddReg = basePtrReg;
2796 // Try and generate an immediate addition if possible
2797 if (cgo.size->isNullValue()) {
2798 BuildMI(*MBB, IP, PPC::OR, 2, nextBasePtrReg).addReg(basePtrReg)
2799 .addReg(basePtrReg);
2800 } else if (canUseAsImmediateForOpcode(cgo.size, 0)) {
2801 BuildMI(*MBB, IP, PPC::ADDI, 2, nextBasePtrReg).addReg(basePtrReg)
2802 .addSImm(cgo.size->getValue());
2804 unsigned Op1r = getReg(cgo.size, MBB, IP);
2805 BuildMI(*MBB, IP, PPC::ADD, 2, nextBasePtrReg).addReg(basePtrReg)
2810 basePtrReg = nextBasePtrReg;
2812 // Add the current base register plus any accumulated constant value
2813 ConstantSInt *remainder = ConstantSInt::get(Type::IntTy, constValue);
2815 // If we are emitting this during a fold, copy the current base register to
2816 // the target, and save the current constant offset so the folding load or
2817 // store can try and use it as an immediate.
2819 // If this is a folded GEP and the last element was an index, then we need
2820 // to do some extra work to turn a shift/add/stw into a shift/stwx
2821 if (pendingAdd && 0 == remainder->getValue()) {
2822 assert(pendingAddReg != 0 && "Uninitialized register in pending add!");
2823 *PendingAddReg = pendingAddReg;
2827 unsigned nextBasePtrReg = makeAnotherReg(Type::IntTy);
2828 assert(pendingAddReg != 0 && "Uninitialized register in pending add!");
2829 BuildMI(*MBB, IP, PPC::ADD, 2, nextBasePtrReg).addReg(basePtrReg)
2830 .addReg(pendingAddReg);
2831 basePtrReg = nextBasePtrReg;
2834 BuildMI (*MBB, IP, PPC::OR, 2, TargetReg).addReg(basePtrReg)
2835 .addReg(basePtrReg);
2836 *RemainderPtr = remainder;
2840 // If we still have a pending add at this point, emit it now
2842 unsigned TmpReg = makeAnotherReg(Type::IntTy);
2843 BuildMI(*MBB, IP, PPC::ADD, 2, TmpReg).addReg(pendingAddReg)
2844 .addReg(basePtrReg);
2845 basePtrReg = TmpReg;
2848 // After we have processed all the indices, the result is left in
2849 // basePtrReg. Move it to the register where we were expected to
2851 if (remainder->isNullValue()) {
2852 BuildMI (*MBB, IP, PPC::OR, 2, TargetReg).addReg(basePtrReg)
2853 .addReg(basePtrReg);
2854 } else if (canUseAsImmediateForOpcode(remainder, 0)) {
2855 BuildMI(*MBB, IP, PPC::ADDI, 2, TargetReg).addReg(basePtrReg)
2856 .addSImm(remainder->getValue());
2858 unsigned Op1r = getReg(remainder, MBB, IP);
2859 BuildMI(*MBB, IP, PPC::ADD, 2, TargetReg).addReg(basePtrReg).addReg(Op1r);
2863 /// visitAllocaInst - If this is a fixed size alloca, allocate space from the
2864 /// frame manager, otherwise do it the hard way.
2866 void PPC64ISel::visitAllocaInst(AllocaInst &I) {
2867 // If this is a fixed size alloca in the entry block for the function, we
2868 // statically stack allocate the space, so we don't need to do anything here.
2870 if (dyn_castFixedAlloca(&I)) return;
2872 // Find the data size of the alloca inst's getAllocatedType.
2873 const Type *Ty = I.getAllocatedType();
2874 unsigned TySize = TM.getTargetData().getTypeSize(Ty);
2876 // Create a register to hold the temporary result of multiplying the type size
2877 // constant by the variable amount.
2878 unsigned TotalSizeReg = makeAnotherReg(Type::UIntTy);
2880 // TotalSizeReg = mul <numelements>, <TypeSize>
2881 MachineBasicBlock::iterator MBBI = BB->end();
2882 ConstantUInt *CUI = ConstantUInt::get(Type::UIntTy, TySize);
2883 doMultiplyConst(BB, MBBI, TotalSizeReg, I.getArraySize(), CUI);
2885 // AddedSize = add <TotalSizeReg>, 15
2886 unsigned AddedSizeReg = makeAnotherReg(Type::UIntTy);
2887 BuildMI(BB, PPC::ADDI, 2, AddedSizeReg).addReg(TotalSizeReg).addSImm(15);
2889 // AlignedSize = and <AddedSize>, ~15
2890 unsigned AlignedSize = makeAnotherReg(Type::UIntTy);
2891 BuildMI(BB, PPC::RLWINM, 4, AlignedSize).addReg(AddedSizeReg).addImm(0)
2892 .addImm(0).addImm(27);
2894 // Subtract size from stack pointer, thereby allocating some space.
2895 BuildMI(BB, PPC::SUB, 2, PPC::R1).addReg(PPC::R1).addReg(AlignedSize);
2897 // Put a pointer to the space into the result register, by copying
2898 // the stack pointer.
2899 BuildMI(BB, PPC::OR, 2, getReg(I)).addReg(PPC::R1).addReg(PPC::R1);
2901 // Inform the Frame Information that we have just allocated a variable-sized
2903 F->getFrameInfo()->CreateVariableSizedObject();
2906 /// visitMallocInst - Malloc instructions are code generated into direct calls
2907 /// to the library malloc.
2909 void PPC64ISel::visitMallocInst(MallocInst &I) {
2910 unsigned AllocSize = TM.getTargetData().getTypeSize(I.getAllocatedType());
2913 if (ConstantUInt *C = dyn_cast<ConstantUInt>(I.getOperand(0))) {
2914 Arg = getReg(ConstantUInt::get(Type::UIntTy, C->getValue() * AllocSize));
2916 Arg = makeAnotherReg(Type::UIntTy);
2917 MachineBasicBlock::iterator MBBI = BB->end();
2918 ConstantUInt *CUI = ConstantUInt::get(Type::UIntTy, AllocSize);
2919 doMultiplyConst(BB, MBBI, Arg, I.getOperand(0), CUI);
2922 std::vector<ValueRecord> Args;
2923 Args.push_back(ValueRecord(Arg, Type::UIntTy));
2924 MachineInstr *TheCall =
2925 BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(mallocFn, true);
2926 doCall(ValueRecord(getReg(I), I.getType()), TheCall, Args, false);
2930 /// visitFreeInst - Free instructions are code gen'd to call the free libc
2933 void PPC64ISel::visitFreeInst(FreeInst &I) {
2934 std::vector<ValueRecord> Args;
2935 Args.push_back(ValueRecord(I.getOperand(0)));
2936 MachineInstr *TheCall =
2937 BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(freeFn, true);
2938 doCall(ValueRecord(0, Type::VoidTy), TheCall, Args, false);
2941 /// createPPC64ISelSimple - This pass converts an LLVM function into a machine
2942 /// code representation is a very simple peep-hole fashion.
2944 FunctionPass *llvm::createPPC64ISelSimple(TargetMachine &TM) {
2945 return new PPC64ISel(TM);