aboutsummaryrefslogtreecommitdiff
path: root/lib/Target/PowerPC/PPC32ISelSimple.cpp
diff options
context:
space:
mode:
authorNate Begeman <natebegeman@mac.com>2005-08-18 23:53:15 +0000
committerNate Begeman <natebegeman@mac.com>2005-08-18 23:53:15 +0000
commit73bfa7152481620d60bf63d5397dfe35bbc9c098 (patch)
tree129c67f7b6aed90a90140376213d51644e89eb8d /lib/Target/PowerPC/PPC32ISelSimple.cpp
parentcfbf96aa9c3bd317548f72e022ba28a40353f95a (diff)
Remove the X86 and PowerPC Simple instruction selectors; their time has
passed. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@22886 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Target/PowerPC/PPC32ISelSimple.cpp')
-rw-r--r--lib/Target/PowerPC/PPC32ISelSimple.cpp3924
1 files changed, 0 insertions, 3924 deletions
diff --git a/lib/Target/PowerPC/PPC32ISelSimple.cpp b/lib/Target/PowerPC/PPC32ISelSimple.cpp
deleted file mode 100644
index 742b92016d..0000000000
--- a/lib/Target/PowerPC/PPC32ISelSimple.cpp
+++ /dev/null
@@ -1,3924 +0,0 @@
-//===-- PPC32ISelSimple.cpp - A simple instruction selector PowerPC32 -----===//
-//
-// The LLVM Compiler Infrastructure
-//
-// This file was developed by the LLVM research group and is distributed under
-// the University of Illinois Open Source License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-
-#define DEBUG_TYPE "isel"
-#include "PowerPC.h"
-#include "PowerPCInstrBuilder.h"
-#include "PowerPCInstrInfo.h"
-#include "PPC32TargetMachine.h"
-#include "llvm/Constants.h"
-#include "llvm/DerivedTypes.h"
-#include "llvm/Function.h"
-#include "llvm/Instructions.h"
-#include "llvm/Pass.h"
-#include "llvm/CodeGen/IntrinsicLowering.h"
-#include "llvm/CodeGen/MachineConstantPool.h"
-#include "llvm/CodeGen/MachineFrameInfo.h"
-#include "llvm/CodeGen/MachineFunction.h"
-#include "llvm/CodeGen/SSARegMap.h"
-#include "llvm/Target/MRegisterInfo.h"
-#include "llvm/Target/TargetMachine.h"
-#include "llvm/Support/GetElementPtrTypeIterator.h"
-#include "llvm/Support/InstVisitor.h"
-#include "llvm/Support/MathExtras.h"
-#include "llvm/Support/Debug.h"
-#include "llvm/ADT/Statistic.h"
-#include <vector>
-using namespace llvm;
-
-
-// IsRunOfOnes - returns true if Val consists of one contiguous run of 1's with
-// any number of 0's on either side. the 1's are allowed to wrap from LSB to
-// MSB. so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
-// not, since all 1's are not contiguous.
-static bool IsRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
- if (isShiftedMask_32(Val)) {
- // look for the first non-zero bit
- MB = CountLeadingZeros_32(Val);
- // look for the first zero bit after the run of ones
- ME = CountLeadingZeros_32((Val - 1) ^ Val);
- return true;
- } else if (isShiftedMask_32(Val = ~Val)) { // invert mask
- // effectively look for the first zero bit
- ME = CountLeadingZeros_32(Val) - 1;
- // effectively look for the first one bit after the run of zeros
- MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
- return true;
- }
- // no run present
- return false;
-}
-
-namespace {
- /// TypeClass - Used by the PowerPC backend to group LLVM types by their basic
- /// PPC Representation.
- ///
- enum TypeClass {
- cByte, cShort, cInt, cFP32, cFP64, cLong
- };
-}
-
-/// getClass - Turn a primitive type into a "class" number which is based on the
-/// size of the type, and whether or not it is floating point.
-///
-static inline TypeClass getClass(const Type *Ty) {
- switch (Ty->getTypeID()) {
- case Type::SByteTyID:
- case Type::UByteTyID: return cByte; // Byte operands are class #0
- case Type::ShortTyID:
- case Type::UShortTyID: return cShort; // Short operands are class #1
- case Type::IntTyID:
- case Type::UIntTyID:
- case Type::PointerTyID: return cInt; // Ints and pointers are class #2
-
- case Type::FloatTyID: return cFP32; // Single float is #3
- case Type::DoubleTyID: return cFP64; // Double Point is #4
-
- case Type::LongTyID:
- case Type::ULongTyID: return cLong; // Longs are class #5
- default:
- assert(0 && "Invalid type to getClass!");
- return cByte; // not reached
- }
-}
-
-// getClassB - Just like getClass, but treat boolean values as ints.
-static inline TypeClass getClassB(const Type *Ty) {
- if (Ty == Type::BoolTy) return cByte;
- return getClass(Ty);
-}
-
-namespace {
- struct PPC32ISel : public FunctionPass, InstVisitor<PPC32ISel> {
- PPC32TargetMachine &TM;
- MachineFunction *F; // The function we are compiling into
- MachineBasicBlock *BB; // The current MBB we are compiling
- int VarArgsFrameIndex; // FrameIndex for start of varargs area
-
- /// CollapsedGepOp - This struct is for recording the intermediate results
- /// used to calculate the base, index, and offset of a GEP instruction.
- struct CollapsedGepOp {
- ConstantSInt *offset; // the current offset into the struct/array
- Value *index; // the index of the array element
- ConstantUInt *size; // the size of each array element
- CollapsedGepOp(ConstantSInt *o, Value *i, ConstantUInt *s) :
- offset(o), index(i), size(s) {}
- };
-
- /// FoldedGEP - This struct is for recording the necessary information to
- /// emit the GEP in a load or store instruction, used by emitGEPOperation.
- struct FoldedGEP {
- unsigned base;
- unsigned index;
- ConstantSInt *offset;
- FoldedGEP() : base(0), index(0), offset(0) {}
- FoldedGEP(unsigned b, unsigned i, ConstantSInt *o) :
- base(b), index(i), offset(o) {}
- };
-
- /// RlwimiRec - This struct is for recording the arguments to a PowerPC
- /// rlwimi instruction to be output for a particular Instruction::Or when
- /// we recognize the pattern for rlwimi, starting with a shift or and.
- struct RlwimiRec {
- Value *Target, *Insert;
- unsigned Shift, MB, ME;
- RlwimiRec() : Target(0), Insert(0), Shift(0), MB(0), ME(0) {}
- RlwimiRec(Value *tgt, Value *ins, unsigned s, unsigned b, unsigned e) :
- Target(tgt), Insert(ins), Shift(s), MB(b), ME(e) {}
- };
-
- // External functions we may use in compiling the Module
- Function *fmodfFn, *fmodFn, *__cmpdi2Fn, *__moddi3Fn, *__divdi3Fn,
- *__umoddi3Fn, *__udivdi3Fn, *__fixsfdiFn, *__fixdfdiFn, *__fixunssfdiFn,
- *__fixunsdfdiFn, *__floatdisfFn, *__floatdidfFn, *mallocFn, *freeFn;
-
- // Mapping between Values and SSA Regs
- std::map<Value*, unsigned> RegMap;
-
- // MBBMap - Mapping between LLVM BB -> Machine BB
- std::map<const BasicBlock*, MachineBasicBlock*> MBBMap;
-
- // AllocaMap - Mapping from fixed sized alloca instructions to the
- // FrameIndex for the alloca.
- std::map<AllocaInst*, unsigned> AllocaMap;
-
- // GEPMap - Mapping between basic blocks and GEP definitions
- std::map<GetElementPtrInst*, FoldedGEP> GEPMap;
-
- // RlwimiMap - Mapping between BinaryOperand (Or) instructions and info
- // needed to properly emit a rlwimi instruction in its place.
- std::map<Instruction *, RlwimiRec> InsertMap;
-
- // A rlwimi instruction is the combination of at least three instructions.
- // Keep a vector of instructions to skip around so that we do not try to
- // emit instructions that were folded into a rlwimi.
- std::vector<Instruction *> SkipList;
-
- // A Reg to hold the base address used for global loads and stores, and a
- // flag to set whether or not we need to emit it for this function.
- unsigned GlobalBaseReg;
- bool GlobalBaseInitialized;
-
- PPC32ISel(TargetMachine &tm):TM(reinterpret_cast<PPC32TargetMachine&>(tm)),
- F(0), BB(0) {}
-
- bool doInitialization(Module &M) {
- // Add external functions that we may call
- Type *i = Type::IntTy;
- Type *d = Type::DoubleTy;
- Type *f = Type::FloatTy;
- Type *l = Type::LongTy;
- Type *ul = Type::ULongTy;
- Type *voidPtr = PointerType::get(Type::SByteTy);
- // float fmodf(float, float);
- fmodfFn = M.getOrInsertFunction("fmodf", f, f, f, 0);
- // double fmod(double, double);
- fmodFn = M.getOrInsertFunction("fmod", d, d, d, 0);
- // int __cmpdi2(long, long);
- __cmpdi2Fn = M.getOrInsertFunction("__cmpdi2", i, l, l, 0);
- // long __moddi3(long, long);
- __moddi3Fn = M.getOrInsertFunction("__moddi3", l, l, l, 0);
- // long __divdi3(long, long);
- __divdi3Fn = M.getOrInsertFunction("__divdi3", l, l, l, 0);
- // unsigned long __umoddi3(unsigned long, unsigned long);
- __umoddi3Fn = M.getOrInsertFunction("__umoddi3", ul, ul, ul, 0);
- // unsigned long __udivdi3(unsigned long, unsigned long);
- __udivdi3Fn = M.getOrInsertFunction("__udivdi3", ul, ul, ul, 0);
- // long __fixsfdi(float)
- __fixsfdiFn = M.getOrInsertFunction("__fixsfdi", l, f, 0);
- // long __fixdfdi(double)
- __fixdfdiFn = M.getOrInsertFunction("__fixdfdi", l, d, 0);
- // unsigned long __fixunssfdi(float)
- __fixunssfdiFn = M.getOrInsertFunction("__fixunssfdi", ul, f, 0);
- // unsigned long __fixunsdfdi(double)
- __fixunsdfdiFn = M.getOrInsertFunction("__fixunsdfdi", ul, d, 0);
- // float __floatdisf(long)
- __floatdisfFn = M.getOrInsertFunction("__floatdisf", f, l, 0);
- // double __floatdidf(long)
- __floatdidfFn = M.getOrInsertFunction("__floatdidf", d, l, 0);
- // void* malloc(size_t)
- mallocFn = M.getOrInsertFunction("malloc", voidPtr, Type::UIntTy, 0);
- // void free(void*)
- freeFn = M.getOrInsertFunction("free", Type::VoidTy, voidPtr, 0);
- return true;
- }
-
- /// runOnFunction - Top level implementation of instruction selection for
- /// the entire function.
- ///
- bool runOnFunction(Function &Fn) {
- // First pass over the function, lower any unknown intrinsic functions
- // with the IntrinsicLowering class.
- LowerUnknownIntrinsicFunctionCalls(Fn);
-
- F = &MachineFunction::construct(&Fn, TM);
-
- // Create all of the machine basic blocks for the function...
- for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
- F->getBasicBlockList().push_back(MBBMap[I] = new MachineBasicBlock(I));
-
- BB = &F->front();
-
- // Make sure we re-emit a set of the global base reg if necessary
- GlobalBaseInitialized = false;
-
- // Copy incoming arguments off of the stack...
- LoadArgumentsToVirtualRegs(Fn);
-
- // Instruction select everything except PHI nodes
- visit(Fn);
-
- // Select the PHI nodes
- SelectPHINodes();
-
- GEPMap.clear();
- RegMap.clear();
- MBBMap.clear();
- InsertMap.clear();
- AllocaMap.clear();
- SkipList.clear();
- F = 0;
- // We always build a machine code representation for the function
- return true;
- }
-
- virtual const char *getPassName() const {
- return "PowerPC Simple Instruction Selection";
- }
-
- /// visitBasicBlock - This method is called when we are visiting a new basic
- /// block. This simply creates a new MachineBasicBlock to emit code into
- /// and adds it to the current MachineFunction. Subsequent visit* for
- /// instructions will be invoked for all instructions in the basic block.
- ///
- void visitBasicBlock(BasicBlock &LLVM_BB) {
- BB = MBBMap[&LLVM_BB];
- }
-
- /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the
- /// function, lowering any calls to unknown intrinsic functions into the
- /// equivalent LLVM code.
- ///
- void LowerUnknownIntrinsicFunctionCalls(Function &F);
-
- /// LoadArgumentsToVirtualRegs - Load all of the arguments to this function
- /// from the stack into virtual registers.
- ///
- void LoadArgumentsToVirtualRegs(Function &F);
-
- /// SelectPHINodes - Insert machine code to generate phis. This is tricky
- /// because we have to generate our sources into the source basic blocks,
- /// not the current one.
- ///
- void SelectPHINodes();
-
- // Visitation methods for various instructions. These methods simply emit
- // fixed PowerPC code for each instruction.
-
- // Control flow operators.
- void visitReturnInst(ReturnInst &RI);
- void visitBranchInst(BranchInst &BI);
- void visitUnreachableInst(UnreachableInst &UI) {}
-
- struct ValueRecord {
- Value *Val;
- unsigned Reg;
- const Type *Ty;
- ValueRecord(unsigned R, const Type *T) : Val(0), Reg(R), Ty(T) {}
- ValueRecord(Value *V) : Val(V), Reg(0), Ty(V->getType()) {}
- };
-
- void doCall(const ValueRecord &Ret, MachineInstr *CallMI,
- const std::vector<ValueRecord> &Args, bool isVarArg);
- void visitCallInst(CallInst &I);
- void visitIntrinsicCall(Intrinsic::ID ID, CallInst &I);
-
- // Arithmetic operators
- void visitSimpleBinary(BinaryOperator &B, unsigned OpcodeClass);
- void visitAdd(BinaryOperator &B) { visitSimpleBinary(B, 0); }
- void visitSub(BinaryOperator &B) { visitSimpleBinary(B, 1); }
- void visitMul(BinaryOperator &B);
-
- void visitDiv(BinaryOperator &B) { visitDivRem(B); }
- void visitRem(BinaryOperator &B) { visitDivRem(B); }
- void visitDivRem(BinaryOperator &B);
-
- // Bitwise operators
- void visitAnd(BinaryOperator &B) { visitSimpleBinary(B, 2); }
- void visitOr (BinaryOperator &B) { visitSimpleBinary(B, 3); }
- void visitXor(BinaryOperator &B) { visitSimpleBinary(B, 4); }
-
- // Comparison operators...
- void visitSetCondInst(SetCondInst &I);
- void EmitComparison(unsigned OpNum, Value *Op0, Value *Op1,
- MachineBasicBlock *MBB,
- MachineBasicBlock::iterator MBBI);
- void visitSelectInst(SelectInst &SI);
-
-
- // Memory Instructions
- void visitLoadInst(LoadInst &I);
- void visitStoreInst(StoreInst &I);
- void visitGetElementPtrInst(GetElementPtrInst &I);
- void visitAllocaInst(AllocaInst &I);
- void visitMallocInst(MallocInst &I);
- void visitFreeInst(FreeInst &I);
-
- // Other operators
- void visitShiftInst(ShiftInst &I);
- void visitPHINode(PHINode &I) {} // PHI nodes handled by second pass
- void visitCastInst(CastInst &I);
- void visitVAArgInst(VAArgInst &I);
-
- void visitInstruction(Instruction &I) {
- std::cerr << "Cannot instruction select: " << I;
- abort();
- }
-
- unsigned ExtendOrClear(MachineBasicBlock *MBB,
- MachineBasicBlock::iterator IP,
- Value *Op0);
-
- /// promote32 - Make a value 32-bits wide, and put it somewhere.
- ///
- void promote32(unsigned targetReg, const ValueRecord &VR);
-
- /// emitGEPOperation - Common code shared between visitGetElementPtrInst and
- /// constant expression GEP support.
- ///
- void emitGEPOperation(MachineBasicBlock *BB, MachineBasicBlock::iterator IP,
- GetElementPtrInst *GEPI, bool foldGEP);
-
- /// emitCastOperation - Common code shared between visitCastInst and
- /// constant expression cast support.
- ///
- void emitCastOperation(MachineBasicBlock *BB,MachineBasicBlock::iterator IP,
- Value *Src, const Type *DestTy, unsigned TargetReg);
-
-
- /// emitBitfieldInsert - return true if we were able to fold the sequence of
- /// instructions into a bitfield insert (rlwimi).
- bool emitBitfieldInsert(User *OpUser, unsigned DestReg);
-
- /// emitBitfieldExtract - return true if we were able to fold the sequence
- /// of instructions into a bitfield extract (rlwinm).
- bool emitBitfieldExtract(MachineBasicBlock *MBB,
- MachineBasicBlock::iterator IP,
- User *OpUser, unsigned DestReg);
-
- /// emitBinaryConstOperation - Used by several functions to emit simple
- /// arithmetic and logical operations with constants on a register rather
- /// than a Value.
- ///
- void emitBinaryConstOperation(MachineBasicBlock *MBB,
- MachineBasicBlock::iterator IP,
- unsigned Op0Reg, ConstantInt *Op1,
- unsigned Opcode, unsigned DestReg);
-
- /// emitSimpleBinaryOperation - Implement simple binary operators for
- /// integral types. OperatorClass is one of: 0 for Add, 1 for Sub,
- /// 2 for And, 3 for Or, 4 for Xor.
- ///
- void emitSimpleBinaryOperation(MachineBasicBlock *BB,
- MachineBasicBlock::iterator IP,
- BinaryOperator *BO, Value *Op0, Value *Op1,
- unsigned OperatorClass, unsigned TargetReg);
-
- /// emitBinaryFPOperation - This method handles emission of floating point
- /// Add (0), Sub (1), Mul (2), and Div (3) operations.
- void emitBinaryFPOperation(MachineBasicBlock *BB,
- MachineBasicBlock::iterator IP,
- Value *Op0, Value *Op1,
- unsigned OperatorClass, unsigned TargetReg);
-
- void emitMultiply(MachineBasicBlock *BB, MachineBasicBlock::iterator IP,
- Value *Op0, Value *Op1, unsigned TargetReg);
-
- void doMultiply(MachineBasicBlock *MBB,
- MachineBasicBlock::iterator IP,
- unsigned DestReg, Value *Op0, Value *Op1);
-
- /// doMultiplyConst - This method will multiply the value in Op0Reg by the
- /// value of the ContantInt *CI
- void doMultiplyConst(MachineBasicBlock *MBB,
- MachineBasicBlock::iterator IP,
- unsigned DestReg, Value *Op0, ConstantInt *CI);
-
- void emitDivRemOperation(MachineBasicBlock *BB,
- MachineBasicBlock::iterator IP,
- Value *Op0, Value *Op1, bool isDiv,
- unsigned TargetReg);
-
- /// emitSetCCOperation - Common code shared between visitSetCondInst and
- /// constant expression support.
- ///
- void emitSetCCOperation(MachineBasicBlock *BB,
- MachineBasicBlock::iterator IP,
- Value *Op0, Value *Op1, unsigned Opcode,
- unsigned TargetReg);
-
- /// emitShiftOperation - Common code shared between visitShiftInst and
- /// constant expression support.
- ///
- void emitShiftOperation(MachineBasicBlock *MBB,
- MachineBasicBlock::iterator IP,
- Value *Op, Value *ShiftAmount, bool isLeftShift,
- const Type *ResultTy, ShiftInst *SI,
- unsigned DestReg);
-
- /// emitSelectOperation - Common code shared between visitSelectInst and the
- /// constant expression support.
- ///
- void emitSelectOperation(MachineBasicBlock *MBB,
- MachineBasicBlock::iterator IP,
- Value *Cond, Value *TrueVal, Value *FalseVal,
- unsigned DestReg);
-
- /// getGlobalBaseReg - Output the instructions required to put the
- /// base address to use for accessing globals into a register. Returns the
- /// register containing the base address.
- ///
- unsigned getGlobalBaseReg();
-
- /// copyConstantToRegister - Output the instructions required to put the
- /// specified constant into the specified register.
- ///
- void copyConstantToRegister(MachineBasicBlock *MBB,
- MachineBasicBlock::iterator MBBI,
- Constant *C, unsigned Reg);
-
- void emitUCOM(MachineBasicBlock *MBB, MachineBasicBlock::iterator MBBI,
- unsigned LHS, unsigned RHS);
-
- /// makeAnotherReg - This method returns the next register number we haven't
- /// yet used.
- ///
- /// Long values are handled somewhat specially. They are always allocated
- /// as pairs of 32 bit integer values. The register number returned is the
- /// high 32 bits of the long value, and the regNum+1 is the low 32 bits.
- ///
- unsigned makeAnotherReg(const Type *Ty) {
- assert(dynamic_cast<const PPC32RegisterInfo*>(TM.getRegisterInfo()) &&
- "Current target doesn't have PPC reg info??");
- const PPC32RegisterInfo *PPCRI =
- static_cast<const PPC32RegisterInfo*>(TM.getRegisterInfo());
- if (Ty == Type::LongTy || Ty == Type::ULongTy) {
- const TargetRegisterClass *RC = PPCRI->getRegClassForType(Type::IntTy);
- // Create the upper part
- F->getSSARegMap()->createVirtualRegister(RC);
- // Create the lower part.
- return F->getSSARegMap()->createVirtualRegister(RC)-1;
- }
-
- // Add the mapping of regnumber => reg class to MachineFunction
- const TargetRegisterClass *RC = PPCRI->getRegClassForType(Ty);
- return F->getSSARegMap()->createVirtualRegister(RC);
- }
-
- /// getReg - This method turns an LLVM value into a register number.
- ///
- unsigned getReg(Value &V) { return getReg(&V); } // Allow references
- unsigned getReg(Value *V) {
- // Just append to the end of the current bb.
- MachineBasicBlock::iterator It = BB->end();
- return getReg(V, BB, It);
- }
- unsigned getReg(Value *V, MachineBasicBlock *MBB,
- MachineBasicBlock::iterator IPt);
-
- /// canUseAsImmediateForOpcode - This method returns whether a ConstantInt
- /// is okay to use as an immediate argument to a certain binary operation
- bool canUseAsImmediateForOpcode(ConstantInt *CI, unsigned Opcode,
- bool Shifted);
-
- /// getFixedSizedAllocaFI - Return the frame index for a fixed sized alloca
- /// that is to be statically allocated with the initial stack frame
- /// adjustment.
- unsigned getFixedSizedAllocaFI(AllocaInst *AI);
- };
-}
-
-/// dyn_castFixedAlloca - If the specified value is a fixed size alloca
-/// instruction in the entry block, return it. Otherwise, return a null
-/// pointer.
-static AllocaInst *dyn_castFixedAlloca(Value *V) {
- if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
- BasicBlock *BB = AI->getParent();
- if (isa<ConstantUInt>(AI->getArraySize()) && BB ==&BB->getParent()->front())
- return AI;
- }
- return 0;
-}
-
-/// getReg - This method turns an LLVM value into a register number.
-///
-unsigned PPC32ISel::getReg(Value *V, MachineBasicBlock *MBB,
- MachineBasicBlock::iterator IPt) {
- if (Constant *C = dyn_cast<Constant>(V)) {
- unsigned Reg = makeAnotherReg(V->getType());
- copyConstantToRegister(MBB, IPt, C, Reg);
- return Reg;
- } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
- // Do not emit noop casts at all, unless it's a double -> float cast.
- if (getClassB(CI->getType()) == getClassB(CI->getOperand(0)->getType()))
- return getReg(CI->getOperand(0), MBB, IPt);
- } else if (AllocaInst *AI = dyn_castFixedAlloca(V)) {
- unsigned Reg = makeAnotherReg(V->getType());
- unsigned FI = getFixedSizedAllocaFI(AI);
- addFrameReference(BuildMI(*MBB, IPt, PPC::ADDI, 2, Reg), FI, 0, false);
- return Reg;
- }
-
- unsigned &Reg = RegMap[V];
- if (Reg == 0) {
- Reg = makeAnotherReg(V->getType());
- RegMap[V] = Reg;
- }
-
- return Reg;
-}
-
-/// canUseAsImmediateForOpcode - This method returns whether a ConstantInt
-/// is okay to use as an immediate argument to a certain binary operator.
-/// The shifted argument determines if the immediate is suitable to be used with
-/// the PowerPC instructions such as addis which concatenate 16 bits of the
-/// immediate with 16 bits of zeroes.
-///
-bool PPC32ISel::canUseAsImmediateForOpcode(ConstantInt *CI, unsigned Opcode,
- bool Shifted) {
- ConstantSInt *Op1Cs;
- ConstantUInt *Op1Cu;
-
- // For shifted immediates, any value with the low halfword cleared may be used
- if (Shifted) {
- if (((int32_t)CI->getRawValue() & 0x0000FFFF) == 0)
- return true;
- else
- return false;
- }
-
- // Treat subfic like addi for the purposes of constant validation
- if (Opcode == 5) Opcode = 0;
-
- // addi, subfic, compare, and non-indexed load take SIMM
- bool cond1 = (Opcode < 2)
- && ((int32_t)CI->getRawValue() <= 32767)
- && ((int32_t)CI->getRawValue() >= -32768);
-
- // ANDIo, ORI, and XORI take unsigned values
- bool cond2 = (Opcode >= 2)
- && (Op1Cs = dyn_cast<ConstantSInt>(CI))
- && (Op1Cs->getValue() >= 0)
- && (Op1Cs->getValue() <= 65535);
-
- // ANDIo, ORI, and XORI take UIMMs, so they can be larger
- bool cond3 = (Opcode >= 2)
- && (Op1Cu = dyn_cast<ConstantUInt>(CI))
- && (Op1Cu->getValue() <= 65535);
-
- if (cond1 || cond2 || cond3)
- return true;
-
- return false;
-}
-
-/// getFixedSizedAllocaFI - Return the frame index for a fixed sized alloca
-/// that is to be statically allocated with the initial stack frame
-/// adjustment.
-unsigned PPC32ISel::getFixedSizedAllocaFI(AllocaInst *AI) {
- // Already computed this?
- std::map<AllocaInst*, unsigned>::iterator I = AllocaMap.lower_bound(AI);
- if (I != AllocaMap.end() && I->first == AI) return I->second;
-
- const Type *Ty = AI->getAllocatedType();
- ConstantUInt *CUI = cast<ConstantUInt>(AI->getArraySize());
- unsigned TySize = TM.getTargetData().getTypeSize(Ty);
- TySize *= CUI->getValue(); // Get total allocated size...
- unsigned Alignment = TM.getTargetData().getTypeAlignment(Ty);
-
- // Create a new stack object using the frame manager...
- int FrameIdx = F->getFrameInfo()->CreateStackObject(TySize, Alignment);
- AllocaMap.insert(I, std::make_pair(AI, FrameIdx));
- return FrameIdx;
-}
-
-
-/// getGlobalBaseReg - Output the instructions required to put the
-/// base address to use for accessing globals into a register.
-///
-unsigned PPC32ISel::getGlobalBaseReg() {
- if (!GlobalBaseInitialized) {
- // Insert the set of GlobalBaseReg into the first MBB of the function
- MachineBasicBlock &FirstMBB = F->front();
- MachineBasicBlock::iterator MBBI = FirstMBB.begin();
- GlobalBaseReg = makeAnotherReg(Type::IntTy);
- BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
- BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg);
- GlobalBaseInitialized = true;
- }
- return GlobalBaseReg;
-}
-
-/// copyConstantToRegister - Output the instructions required to put the
-/// specified constant into the specified register.
-///
-void PPC32ISel::copyConstantToRegister(MachineBasicBlock *MBB,
- MachineBasicBlock::iterator IP,
- Constant *C, unsigned R) {
- if (isa<UndefValue>(C)) {
- BuildMI(*MBB, IP, PPC::IMPLICIT_DEF, 0, R);
- if (getClassB(C->getType()) == cLong)
- BuildMI(*MBB, IP, PPC::IMPLICIT_DEF, 0, R+1);
- return;
- }
- if (C->getType()->isIntegral()) {
- unsigned Class = getClassB(C->getType());
-
- if (Class == cLong) {
- if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(C)) {
- uint64_t uval = CUI->getValue();
- unsigned hiUVal = uval >> 32;
- unsigned loUVal = uval;
- ConstantUInt *CUHi = ConstantUInt::get(Type::UIntTy, hiUVal);
- ConstantUInt *CULo = ConstantUInt::get(Type::UIntTy, loUVal);
- copyConstantToRegister(MBB, IP, CUHi, R);
- copyConstantToRegister(MBB, IP, CULo, R+1);
- return;
- } else if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(C)) {
- int64_t sval = CSI->getValue();
- int hiSVal = sval >> 32;
- int loSVal = sval;
- ConstantSInt *CSHi = ConstantSInt::get(Type::IntTy, hiSVal);
- ConstantSInt *CSLo = ConstantSInt::get(Type::IntTy, loSVal);
- copyConstantToRegister(MBB, IP, CSHi, R);
- copyConstantToRegister(MBB, IP, CSLo, R+1);
- return;
- } else {
- std::cerr << "Unhandled long constant type!\n";
- abort();
- }
- }
-
- assert(Class <= cInt && "Type not handled yet!");
-
- // Handle bool
- if (C->getType() == Type::BoolTy) {
- BuildMI(*MBB, IP, PPC::LI, 1, R).addSImm(C == ConstantBool::True);
- return;
- }
-
- // Handle int
- if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(C)) {
- unsigned uval = CUI->getValue();
- if (uval < 32768) {
- BuildMI(*MBB, IP, PPC::LI, 1, R).addSImm(uval);
- } else {
- unsigned Temp = makeAnotherReg(Type::IntTy);
- BuildMI(*MBB, IP, PPC::LIS, 1, Temp).addSImm(uval >> 16);
- BuildMI(*MBB, IP, PPC::ORI, 2, R).addReg(Temp).addImm(uval & 0xFFFF);
- }
- return;
- } else if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(C)) {
- int sval = CSI->getValue();
- if (sval < 32768 && sval >= -32768) {
- BuildMI(*MBB, IP, PPC::LI, 1, R).addSImm(sval);
- } else {
- unsigned Temp = makeAnotherReg(Type::IntTy);
- BuildMI(*MBB, IP, PPC::LIS, 1, Temp).addSImm(sval >> 16);
- BuildMI(*MBB, IP, PPC::ORI, 2, R).addReg(Temp).addImm(sval & 0xFFFF);
- }
- return;
- }
- std::cerr << "Unhandled integer constant!\n";
- abort();
- } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
- // We need to spill the constant to memory...
- MachineConstantPool *CP = F->getConstantPool();
- unsigned CPI = CP->getConstantPoolIndex(CFP);
- const Type *Ty = CFP->getType();
-
- assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!");
-
- // Load addr of constant to reg; constant is located at base + distance
- unsigned Reg1 = makeAnotherReg(Type::IntTy);
- unsigned Opcode = (Ty == Type::FloatTy) ? PPC::LFS : PPC::LFD;
- // Move value at base + distance into return reg
- BuildMI(*MBB, IP, PPC::ADDIS, 2, Reg1)
- .addReg(getGlobalBaseReg()).addConstantPoolIndex(CPI);
- BuildMI(*MBB, IP, Opcode, 2, R).addConstantPoolIndex(CPI).addReg(Reg1);
- } else if (isa<ConstantPointerNull>(C)) {
- // Copy zero (null pointer) to the register.
- BuildMI(*MBB, IP, PPC::LI, 1, R).addSImm(0);
- } else if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) {
- // GV is located at base + distance
- unsigned TmpReg = makeAnotherReg(GV->getType());
-
- // Move value at base + distance into return reg
- BuildMI(*MBB, IP, PPC::ADDIS, 2, TmpReg)
- .addReg(getGlobalBaseReg()).addGlobalAddress(GV);
-
- if (GV->hasWeakLinkage() || GV->isExternal()) {
- BuildMI(*MBB, IP, PPC::LWZ, 2, R).addGlobalAddress(GV).addReg(TmpReg);
- } else {
- BuildMI(*MBB, IP, PPC::LA, 2, R).addReg(TmpReg).addGlobalAddress(GV);
- }
- } else {
- std::cerr << "Offending constant: " << *C << "\n";
- assert(0 && "Type not handled yet!");
- }
-}
-
-/// LoadArgumentsToVirtualRegs - Load all of the arguments to this function from
-/// the stack into virtual registers.
-void PPC32ISel::LoadArgumentsToVirtualRegs(Function &Fn) {
- unsigned ArgOffset = 24;
- unsigned GPR_remaining = 8;
- unsigned FPR_remaining = 13;
- unsigned GPR_idx = 0, FPR_idx = 0;
- static const unsigned GPR[] = {
- PPC::R3, PPC::R4, PPC::R5, PPC::R6,
- PPC::R7, PPC::R8, PPC::R9, PPC::R10,
- };
- static const unsigned FPR[] = {
- PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
- PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
- };
-
- MachineFrameInfo *MFI = F->getFrameInfo();
-
- for (Function::arg_iterator I = Fn.arg_begin(), E = Fn.arg_end();
- I != E; ++I) {
- bool ArgLive = !I->use_empty();
- unsigned Reg = ArgLive ? getReg(*I) : 0;
- int FI; // Frame object index
-
- switch (getClassB(I->getType())) {
- case cByte:
- if (ArgLive) {
- FI = MFI->CreateFixedObject(4, ArgOffset);
- if (GPR_remaining > 0) {
- F->addLiveIn(GPR[GPR_idx]);
- BuildMI(BB, PPC::OR, 2, Reg).addReg(GPR[GPR_idx])
- .addReg(GPR[GPR_idx]);
- } else {
- addFrameReference(BuildMI(BB, PPC::LBZ, 2, Reg), FI);
- }
- }
- break;
- case cShort:
- if (ArgLive) {
- FI = MFI->CreateFixedObject(4, ArgOffset);
- if (GPR_remaining > 0) {
- F->addLiveIn(GPR[GPR_idx]);
- BuildMI(BB, PPC::OR, 2, Reg).addReg(GPR[GPR_idx])
- .addReg(GPR[GPR_idx]);
- } else {
- addFrameReference(BuildMI(BB, PPC::LHZ, 2, Reg), FI);
- }
- }
- break;
- case cInt:
- if (ArgLive) {
- FI = MFI->CreateFixedObject(4, ArgOffset);
- if (GPR_remaining > 0) {
- F->addLiveIn(GPR[GPR_idx]);
- BuildMI(BB, PPC::OR, 2, Reg).addReg(GPR[GPR_idx])
- .addReg(GPR[GPR_idx]);
- } else {
- addFrameReference(BuildMI(BB, PPC::LWZ, 2, Reg), FI);
- }
- }
- break;
- case cLong:
- if (ArgLive) {
- FI = MFI->CreateFixedObject(8, ArgOffset);
- if (GPR_remaining > 1) {
- F->addLiveIn(GPR[GPR_idx]);
- F->addLiveIn(GPR[GPR_idx+1]);
- BuildMI(BB, PPC::OR, 2, Reg).addReg(GPR[GPR_idx])
- .addReg(GPR[GPR_idx]);
- BuildMI(BB, PPC::OR, 2, Reg+1).addReg(GPR[GPR_idx+1])
- .addReg(GPR[GPR_idx+1]);
- } else {
- addFrameReference(BuildMI(BB, PPC::LWZ, 2, Reg), FI);
- addFrameReference(BuildMI(BB, PPC::LWZ, 2, Reg+1), FI, 4);
- }
- }
- // longs require 4 additional bytes and use 2 GPRs
- ArgOffset += 4;
- if (GPR_remaining > 1) {
- GPR_remaining--;
- GPR_idx++;
- }
- break;
- case cFP32:
- if (ArgLive) {
- FI = MFI->CreateFixedObject(4, ArgOffset);
-
- if (FPR_remaining > 0) {
- F->addLiveIn(FPR[FPR_idx]);
- BuildMI(BB, PPC::FMR, 1, Reg).addReg(FPR[FPR_idx]);
- FPR_remaining--;
- FPR_idx++;
- } else {
- addFrameReference(BuildMI(BB, PPC::LFS, 2, Reg), FI);
- }
- }
- break;
- case cFP64:
- if (ArgLive) {
- FI = MFI->CreateFixedObject(8, ArgOffset);
-
- if (FPR_remaining > 0) {
- F->addLiveIn(FPR[FPR_idx]);
- BuildMI(BB, PPC::FMR, 1, Reg).addReg(FPR[FPR_idx]);
- FPR_remaining--;
- FPR_idx++;
- } else {
- addFrameReference(BuildMI(BB, PPC::LFD, 2, Reg), FI);
- }
- }
-
- // doubles require 4 additional bytes and use 2 GPRs of param space
- ArgOffset += 4;
- if (GPR_remaining > 0) {
- GPR_remaining--;
- GPR_idx++;
- }
- break;
- default:
- assert(0 && "Unhandled argument type!");
- }
- ArgOffset += 4; // Each argument takes at least 4 bytes on the stack...
- if (GPR_remaining > 0) {
- GPR_remaining--; // uses up 2 GPRs
- GPR_idx++;
- }
- }
-
- // If the function takes variable number of arguments, add a frame offset for
- // the start of the first vararg value... this is used to expand
- // llvm.va_start.
- if (Fn.getFunctionType()->isVarArg())
- VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
-
- if (Fn.getReturnType() != Type::VoidTy)
- switch (getClassB(Fn.getReturnType())) {
- case cByte:
- case cShort:
- case cInt:
- F->addLiveOut(PPC::R3);
- break;
- case cLong:
- F->addLiveOut(PPC::R3);
- F->addLiveOut(PPC::R4);
- break;
- case cFP32:
- case cFP64:
- F->addLiveOut(PPC::F1);
- break;
- }
-}
-
-
-/// SelectPHINodes - Insert machine code to generate phis. This is tricky
-/// because we have to generate our sources into the source basic blocks, not
-/// the current one.
-///
-void PPC32ISel::SelectPHINodes() {
- const TargetInstrInfo &TII = *TM.getInstrInfo();
- const Function &LF = *F->getFunction(); // The LLVM function...
-
- MachineBasicBlock::iterator MFLRIt = F->begin()->begin();
- if (GlobalBaseInitialized) {
- // If we emitted a MFLR for the global base reg, get an iterator to an
- // instruction after it.
- while (MFLRIt->getOpcode() != PPC::MFLR)
- ++MFLRIt;
- ++MFLRIt; // step one MI past it.
- }
-
- for (Function::const_iterator I = LF.begin(), E = LF.end(); I != E; ++I) {
- const BasicBlock *BB = I;
- MachineBasicBlock &MBB = *MBBMap[I];
-
- // Loop over all of the PHI nodes in the LLVM basic block...
- MachineBasicBlock::iterator PHIInsertPoint = MBB.begin();
- for (BasicBlock::const_iterator I = BB->begin();
- PHINode *PN = const_cast<PHINode*>(dyn_cast<PHINode>(I)); ++I) {</