Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1 | //===-- InstSelectSimple.cpp - A simple instruction selector for PowerPC --===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Misha Brukman | 98649d1 | 2004-06-24 21:54:47 +0000 | [diff] [blame] | 10 | #define DEBUG_TYPE "isel" |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 11 | #include "PowerPC.h" |
| 12 | #include "PowerPCInstrBuilder.h" |
| 13 | #include "PowerPCInstrInfo.h" |
Misha Brukman | 3d9a6c2 | 2004-08-11 00:09:42 +0000 | [diff] [blame] | 14 | #include "PPC32.h" |
| 15 | #include "PPC32TargetMachine.h" |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 16 | #include "llvm/Constants.h" |
| 17 | #include "llvm/DerivedTypes.h" |
| 18 | #include "llvm/Function.h" |
| 19 | #include "llvm/Instructions.h" |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 20 | #include "llvm/Pass.h" |
Misha Brukman | 8c9f520 | 2004-06-21 18:30:31 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/IntrinsicLowering.h" |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 23 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 24 | #include "llvm/CodeGen/MachineFunction.h" |
| 25 | #include "llvm/CodeGen/SSARegMap.h" |
| 26 | #include "llvm/Target/MRegisterInfo.h" |
| 27 | #include "llvm/Target/TargetMachine.h" |
| 28 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
| 29 | #include "llvm/Support/InstVisitor.h" |
Misha Brukman | 98649d1 | 2004-06-24 21:54:47 +0000 | [diff] [blame] | 30 | #include "Support/Debug.h" |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 31 | #include "Support/Statistic.h" |
Misha Brukman | 98649d1 | 2004-06-24 21:54:47 +0000 | [diff] [blame] | 32 | #include <vector> |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 33 | using namespace llvm; |
| 34 | |
| 35 | namespace { |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 36 | Statistic<> GEPFolds("ppc-codegen", "Number of GEPs folded"); |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 37 | |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 38 | /// TypeClass - Used by the PowerPC backend to group LLVM types by their basic |
| 39 | /// PPC Representation. |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 40 | /// |
| 41 | enum TypeClass { |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 42 | cByte, cShort, cInt, cFP32, cFP64, cLong |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 43 | }; |
| 44 | } |
| 45 | |
| 46 | /// getClass - Turn a primitive type into a "class" number which is based on the |
| 47 | /// size of the type, and whether or not it is floating point. |
| 48 | /// |
| 49 | static inline TypeClass getClass(const Type *Ty) { |
Misha Brukman | 358829f | 2004-06-21 17:25:55 +0000 | [diff] [blame] | 50 | switch (Ty->getTypeID()) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 51 | case Type::SByteTyID: |
| 52 | case Type::UByteTyID: return cByte; // Byte operands are class #0 |
| 53 | case Type::ShortTyID: |
| 54 | case Type::UShortTyID: return cShort; // Short operands are class #1 |
| 55 | case Type::IntTyID: |
| 56 | case Type::UIntTyID: |
Misha Brukman | 2834a4d | 2004-07-07 20:07:22 +0000 | [diff] [blame] | 57 | case Type::PointerTyID: return cInt; // Ints and pointers are class #2 |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 58 | |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 59 | case Type::FloatTyID: return cFP32; // Single float is #3 |
| 60 | case Type::DoubleTyID: return cFP64; // Double Point is #4 |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 61 | |
| 62 | case Type::LongTyID: |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 63 | case Type::ULongTyID: return cLong; // Longs are class #5 |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 64 | default: |
| 65 | assert(0 && "Invalid type to getClass!"); |
| 66 | return cByte; // not reached |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // getClassB - Just like getClass, but treat boolean values as ints. |
| 71 | static inline TypeClass getClassB(const Type *Ty) { |
Misha Brukman | 4c14f33 | 2004-07-23 01:11:19 +0000 | [diff] [blame] | 72 | if (Ty == Type::BoolTy) return cInt; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 73 | return getClass(Ty); |
| 74 | } |
| 75 | |
| 76 | namespace { |
| 77 | struct ISel : public FunctionPass, InstVisitor<ISel> { |
Misha Brukman | 3d9a6c2 | 2004-08-11 00:09:42 +0000 | [diff] [blame] | 78 | PPC32TargetMachine &TM; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 79 | MachineFunction *F; // The function we are compiling into |
| 80 | MachineBasicBlock *BB; // The current MBB we are compiling |
| 81 | int VarArgsFrameIndex; // FrameIndex for start of varargs area |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 82 | |
Misha Brukman | 313efcb | 2004-07-09 15:45:07 +0000 | [diff] [blame] | 83 | std::map<Value*, unsigned> RegMap; // Mapping between Values and SSA Regs |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 84 | |
Misha Brukman | 2834a4d | 2004-07-07 20:07:22 +0000 | [diff] [blame] | 85 | // External functions used in the Module |
Nate Begeman | b64af91 | 2004-08-10 20:42:36 +0000 | [diff] [blame] | 86 | Function *fmodfFn, *fmodFn, *__cmpdi2Fn, *__moddi3Fn, *__divdi3Fn, |
| 87 | *__umoddi3Fn, *__udivdi3Fn, *__fixsfdiFn, *__fixdfdiFn, *__fixunssfdiFn, |
| 88 | *__fixunsdfdiFn, *__floatdisfFn, *__floatdidfFn, *mallocFn, *freeFn; |
Misha Brukman | 2834a4d | 2004-07-07 20:07:22 +0000 | [diff] [blame] | 89 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 90 | // MBBMap - Mapping between LLVM BB -> Machine BB |
| 91 | std::map<const BasicBlock*, MachineBasicBlock*> MBBMap; |
| 92 | |
| 93 | // AllocaMap - Mapping from fixed sized alloca instructions to the |
| 94 | // FrameIndex for the alloca. |
| 95 | std::map<AllocaInst*, unsigned> AllocaMap; |
| 96 | |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 97 | // A Reg to hold the base address used for global loads and stores, and a |
| 98 | // flag to set whether or not we need to emit it for this function. |
| 99 | unsigned GlobalBaseReg; |
| 100 | bool GlobalBaseInitialized; |
| 101 | |
Misha Brukman | 3d9a6c2 | 2004-08-11 00:09:42 +0000 | [diff] [blame] | 102 | ISel(TargetMachine &tm) : TM(reinterpret_cast<PPC32TargetMachine&>(tm)), |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 103 | F(0), BB(0) {} |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 104 | |
Misha Brukman | 2834a4d | 2004-07-07 20:07:22 +0000 | [diff] [blame] | 105 | bool doInitialization(Module &M) { |
Misha Brukman | b093259 | 2004-07-07 15:36:18 +0000 | [diff] [blame] | 106 | // Add external functions that we may call |
Nate Begeman | b64af91 | 2004-08-10 20:42:36 +0000 | [diff] [blame] | 107 | Type *i = Type::IntTy; |
Misha Brukman | 2834a4d | 2004-07-07 20:07:22 +0000 | [diff] [blame] | 108 | Type *d = Type::DoubleTy; |
Misha Brukman | f3f6382 | 2004-07-08 19:41:16 +0000 | [diff] [blame] | 109 | Type *f = Type::FloatTy; |
Misha Brukman | 2834a4d | 2004-07-07 20:07:22 +0000 | [diff] [blame] | 110 | Type *l = Type::LongTy; |
| 111 | Type *ul = Type::ULongTy; |
Misha Brukman | 313efcb | 2004-07-09 15:45:07 +0000 | [diff] [blame] | 112 | Type *voidPtr = PointerType::get(Type::SByteTy); |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 113 | // float fmodf(float, float); |
| 114 | fmodfFn = M.getOrInsertFunction("fmodf", f, f, f, 0); |
Misha Brukman | 2834a4d | 2004-07-07 20:07:22 +0000 | [diff] [blame] | 115 | // double fmod(double, double); |
Misha Brukman | 0aa97c6 | 2004-07-08 18:27:59 +0000 | [diff] [blame] | 116 | fmodFn = M.getOrInsertFunction("fmod", d, d, d, 0); |
Nate Begeman | b64af91 | 2004-08-10 20:42:36 +0000 | [diff] [blame] | 117 | // int __cmpdi2(long, long); |
| 118 | __cmpdi2Fn = M.getOrInsertFunction("__cmpdi2", i, l, l, 0); |
Misha Brukman | 2834a4d | 2004-07-07 20:07:22 +0000 | [diff] [blame] | 119 | // long __moddi3(long, long); |
Misha Brukman | 0aa97c6 | 2004-07-08 18:27:59 +0000 | [diff] [blame] | 120 | __moddi3Fn = M.getOrInsertFunction("__moddi3", l, l, l, 0); |
Misha Brukman | 2834a4d | 2004-07-07 20:07:22 +0000 | [diff] [blame] | 121 | // long __divdi3(long, long); |
Misha Brukman | 0aa97c6 | 2004-07-08 18:27:59 +0000 | [diff] [blame] | 122 | __divdi3Fn = M.getOrInsertFunction("__divdi3", l, l, l, 0); |
Misha Brukman | 2834a4d | 2004-07-07 20:07:22 +0000 | [diff] [blame] | 123 | // unsigned long __umoddi3(unsigned long, unsigned long); |
Misha Brukman | 0aa97c6 | 2004-07-08 18:27:59 +0000 | [diff] [blame] | 124 | __umoddi3Fn = M.getOrInsertFunction("__umoddi3", ul, ul, ul, 0); |
Misha Brukman | 2834a4d | 2004-07-07 20:07:22 +0000 | [diff] [blame] | 125 | // unsigned long __udivdi3(unsigned long, unsigned long); |
Misha Brukman | 0aa97c6 | 2004-07-08 18:27:59 +0000 | [diff] [blame] | 126 | __udivdi3Fn = M.getOrInsertFunction("__udivdi3", ul, ul, ul, 0); |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 127 | // long __fixsfdi(float) |
Nate Begeman | b64af91 | 2004-08-10 20:42:36 +0000 | [diff] [blame] | 128 | __fixsfdiFn = M.getOrInsertFunction("__fixsfdi", l, f, 0); |
Misha Brukman | f3f6382 | 2004-07-08 19:41:16 +0000 | [diff] [blame] | 129 | // long __fixdfdi(double) |
| 130 | __fixdfdiFn = M.getOrInsertFunction("__fixdfdi", l, d, 0); |
Nate Begeman | b64af91 | 2004-08-10 20:42:36 +0000 | [diff] [blame] | 131 | // unsigned long __fixunssfdi(float) |
| 132 | __fixunssfdiFn = M.getOrInsertFunction("__fixunssfdi", ul, f, 0); |
| 133 | // unsigned long __fixunsdfdi(double) |
| 134 | __fixunsdfdiFn = M.getOrInsertFunction("__fixunsdfdi", ul, d, 0); |
Misha Brukman | f3f6382 | 2004-07-08 19:41:16 +0000 | [diff] [blame] | 135 | // float __floatdisf(long) |
| 136 | __floatdisfFn = M.getOrInsertFunction("__floatdisf", f, l, 0); |
| 137 | // double __floatdidf(long) |
| 138 | __floatdidfFn = M.getOrInsertFunction("__floatdidf", d, l, 0); |
Misha Brukman | 313efcb | 2004-07-09 15:45:07 +0000 | [diff] [blame] | 139 | // void* malloc(size_t) |
| 140 | mallocFn = M.getOrInsertFunction("malloc", voidPtr, Type::UIntTy, 0); |
| 141 | // void free(void*) |
| 142 | freeFn = M.getOrInsertFunction("free", Type::VoidTy, voidPtr, 0); |
Misha Brukman | 2834a4d | 2004-07-07 20:07:22 +0000 | [diff] [blame] | 143 | return false; |
| 144 | } |
Misha Brukman | d18a31d | 2004-07-06 22:51:53 +0000 | [diff] [blame] | 145 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 146 | /// runOnFunction - Top level implementation of instruction selection for |
| 147 | /// the entire function. |
| 148 | /// |
| 149 | bool runOnFunction(Function &Fn) { |
| 150 | // First pass over the function, lower any unknown intrinsic functions |
| 151 | // with the IntrinsicLowering class. |
| 152 | LowerUnknownIntrinsicFunctionCalls(Fn); |
| 153 | |
| 154 | F = &MachineFunction::construct(&Fn, TM); |
| 155 | |
| 156 | // Create all of the machine basic blocks for the function... |
| 157 | for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) |
| 158 | F->getBasicBlockList().push_back(MBBMap[I] = new MachineBasicBlock(I)); |
| 159 | |
| 160 | BB = &F->front(); |
| 161 | |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 162 | // Make sure we re-emit a set of the global base reg if necessary |
| 163 | GlobalBaseInitialized = false; |
| 164 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 165 | // Copy incoming arguments off of the stack... |
| 166 | LoadArgumentsToVirtualRegs(Fn); |
| 167 | |
| 168 | // Instruction select everything except PHI nodes |
| 169 | visit(Fn); |
| 170 | |
| 171 | // Select the PHI nodes |
| 172 | SelectPHINodes(); |
| 173 | |
| 174 | RegMap.clear(); |
| 175 | MBBMap.clear(); |
| 176 | AllocaMap.clear(); |
| 177 | F = 0; |
| 178 | // We always build a machine code representation for the function |
| 179 | return true; |
| 180 | } |
| 181 | |
| 182 | virtual const char *getPassName() const { |
| 183 | return "PowerPC Simple Instruction Selection"; |
| 184 | } |
| 185 | |
| 186 | /// visitBasicBlock - This method is called when we are visiting a new basic |
| 187 | /// block. This simply creates a new MachineBasicBlock to emit code into |
| 188 | /// and adds it to the current MachineFunction. Subsequent visit* for |
| 189 | /// instructions will be invoked for all instructions in the basic block. |
| 190 | /// |
| 191 | void visitBasicBlock(BasicBlock &LLVM_BB) { |
| 192 | BB = MBBMap[&LLVM_BB]; |
| 193 | } |
| 194 | |
| 195 | /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the |
| 196 | /// function, lowering any calls to unknown intrinsic functions into the |
| 197 | /// equivalent LLVM code. |
| 198 | /// |
| 199 | void LowerUnknownIntrinsicFunctionCalls(Function &F); |
| 200 | |
| 201 | /// LoadArgumentsToVirtualRegs - Load all of the arguments to this function |
| 202 | /// from the stack into virtual registers. |
| 203 | /// |
| 204 | void LoadArgumentsToVirtualRegs(Function &F); |
| 205 | |
| 206 | /// SelectPHINodes - Insert machine code to generate phis. This is tricky |
| 207 | /// because we have to generate our sources into the source basic blocks, |
| 208 | /// not the current one. |
| 209 | /// |
| 210 | void SelectPHINodes(); |
| 211 | |
| 212 | // Visitation methods for various instructions. These methods simply emit |
| 213 | // fixed PowerPC code for each instruction. |
| 214 | |
| 215 | // Control flow operators |
| 216 | void visitReturnInst(ReturnInst &RI); |
| 217 | void visitBranchInst(BranchInst &BI); |
| 218 | |
| 219 | struct ValueRecord { |
| 220 | Value *Val; |
| 221 | unsigned Reg; |
| 222 | const Type *Ty; |
| 223 | ValueRecord(unsigned R, const Type *T) : Val(0), Reg(R), Ty(T) {} |
| 224 | ValueRecord(Value *V) : Val(V), Reg(0), Ty(V->getType()) {} |
| 225 | }; |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 226 | |
| 227 | // This struct is for recording the necessary operations to emit the GEP |
| 228 | struct CollapsedGepOp { |
| 229 | bool isMul; |
| 230 | Value *index; |
| 231 | ConstantSInt *size; |
| 232 | CollapsedGepOp(bool mul, Value *i, ConstantSInt *s) : |
| 233 | isMul(mul), index(i), size(s) {} |
| 234 | }; |
| 235 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 236 | void doCall(const ValueRecord &Ret, MachineInstr *CallMI, |
Misha Brukman | d18a31d | 2004-07-06 22:51:53 +0000 | [diff] [blame] | 237 | const std::vector<ValueRecord> &Args, bool isVarArg); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 238 | void visitCallInst(CallInst &I); |
| 239 | void visitIntrinsicCall(Intrinsic::ID ID, CallInst &I); |
| 240 | |
| 241 | // Arithmetic operators |
| 242 | void visitSimpleBinary(BinaryOperator &B, unsigned OpcodeClass); |
| 243 | void visitAdd(BinaryOperator &B) { visitSimpleBinary(B, 0); } |
| 244 | void visitSub(BinaryOperator &B) { visitSimpleBinary(B, 1); } |
| 245 | void visitMul(BinaryOperator &B); |
| 246 | |
| 247 | void visitDiv(BinaryOperator &B) { visitDivRem(B); } |
| 248 | void visitRem(BinaryOperator &B) { visitDivRem(B); } |
| 249 | void visitDivRem(BinaryOperator &B); |
| 250 | |
| 251 | // Bitwise operators |
| 252 | void visitAnd(BinaryOperator &B) { visitSimpleBinary(B, 2); } |
| 253 | void visitOr (BinaryOperator &B) { visitSimpleBinary(B, 3); } |
| 254 | void visitXor(BinaryOperator &B) { visitSimpleBinary(B, 4); } |
| 255 | |
| 256 | // Comparison operators... |
| 257 | void visitSetCondInst(SetCondInst &I); |
| 258 | unsigned EmitComparison(unsigned OpNum, Value *Op0, Value *Op1, |
| 259 | MachineBasicBlock *MBB, |
| 260 | MachineBasicBlock::iterator MBBI); |
| 261 | void visitSelectInst(SelectInst &SI); |
| 262 | |
| 263 | |
| 264 | // Memory Instructions |
| 265 | void visitLoadInst(LoadInst &I); |
| 266 | void visitStoreInst(StoreInst &I); |
| 267 | void visitGetElementPtrInst(GetElementPtrInst &I); |
| 268 | void visitAllocaInst(AllocaInst &I); |
| 269 | void visitMallocInst(MallocInst &I); |
| 270 | void visitFreeInst(FreeInst &I); |
| 271 | |
| 272 | // Other operators |
| 273 | void visitShiftInst(ShiftInst &I); |
| 274 | void visitPHINode(PHINode &I) {} // PHI nodes handled by second pass |
| 275 | void visitCastInst(CastInst &I); |
| 276 | void visitVANextInst(VANextInst &I); |
| 277 | void visitVAArgInst(VAArgInst &I); |
| 278 | |
| 279 | void visitInstruction(Instruction &I) { |
| 280 | std::cerr << "Cannot instruction select: " << I; |
| 281 | abort(); |
| 282 | } |
| 283 | |
| 284 | /// promote32 - Make a value 32-bits wide, and put it somewhere. |
| 285 | /// |
| 286 | void promote32(unsigned targetReg, const ValueRecord &VR); |
| 287 | |
| 288 | /// emitGEPOperation - Common code shared between visitGetElementPtrInst and |
| 289 | /// constant expression GEP support. |
| 290 | /// |
| 291 | void emitGEPOperation(MachineBasicBlock *BB, MachineBasicBlock::iterator IP, |
| 292 | Value *Src, User::op_iterator IdxBegin, |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 293 | User::op_iterator IdxEnd, unsigned TargetReg, |
Nate Begeman | b64af91 | 2004-08-10 20:42:36 +0000 | [diff] [blame] | 294 | bool CollapseRemainder, ConstantSInt **Remainder, |
| 295 | unsigned *PendingAddReg); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 296 | |
| 297 | /// emitCastOperation - Common code shared between visitCastInst and |
| 298 | /// constant expression cast support. |
| 299 | /// |
| 300 | void emitCastOperation(MachineBasicBlock *BB,MachineBasicBlock::iterator IP, |
| 301 | Value *Src, const Type *DestTy, unsigned TargetReg); |
| 302 | |
| 303 | /// emitSimpleBinaryOperation - Common code shared between visitSimpleBinary |
| 304 | /// and constant expression support. |
| 305 | /// |
| 306 | void emitSimpleBinaryOperation(MachineBasicBlock *BB, |
| 307 | MachineBasicBlock::iterator IP, |
| 308 | Value *Op0, Value *Op1, |
| 309 | unsigned OperatorClass, unsigned TargetReg); |
| 310 | |
| 311 | /// emitBinaryFPOperation - This method handles emission of floating point |
| 312 | /// Add (0), Sub (1), Mul (2), and Div (3) operations. |
| 313 | void emitBinaryFPOperation(MachineBasicBlock *BB, |
| 314 | MachineBasicBlock::iterator IP, |
| 315 | Value *Op0, Value *Op1, |
| 316 | unsigned OperatorClass, unsigned TargetReg); |
| 317 | |
| 318 | void emitMultiply(MachineBasicBlock *BB, MachineBasicBlock::iterator IP, |
| 319 | Value *Op0, Value *Op1, unsigned TargetReg); |
| 320 | |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 321 | void doMultiply(MachineBasicBlock *MBB, |
| 322 | MachineBasicBlock::iterator IP, |
| 323 | unsigned DestReg, Value *Op0, Value *Op1); |
| 324 | |
| 325 | /// doMultiplyConst - This method will multiply the value in Op0Reg by the |
| 326 | /// value of the ContantInt *CI |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 327 | void doMultiplyConst(MachineBasicBlock *MBB, |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 328 | MachineBasicBlock::iterator IP, |
| 329 | unsigned DestReg, Value *Op0, ConstantInt *CI); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 330 | |
| 331 | void emitDivRemOperation(MachineBasicBlock *BB, |
| 332 | MachineBasicBlock::iterator IP, |
| 333 | Value *Op0, Value *Op1, bool isDiv, |
| 334 | unsigned TargetReg); |
| 335 | |
| 336 | /// emitSetCCOperation - Common code shared between visitSetCondInst and |
| 337 | /// constant expression support. |
| 338 | /// |
| 339 | void emitSetCCOperation(MachineBasicBlock *BB, |
| 340 | MachineBasicBlock::iterator IP, |
| 341 | Value *Op0, Value *Op1, unsigned Opcode, |
| 342 | unsigned TargetReg); |
| 343 | |
| 344 | /// emitShiftOperation - Common code shared between visitShiftInst and |
| 345 | /// constant expression support. |
| 346 | /// |
| 347 | void emitShiftOperation(MachineBasicBlock *MBB, |
| 348 | MachineBasicBlock::iterator IP, |
| 349 | Value *Op, Value *ShiftAmount, bool isLeftShift, |
| 350 | const Type *ResultTy, unsigned DestReg); |
| 351 | |
| 352 | /// emitSelectOperation - Common code shared between visitSelectInst and the |
| 353 | /// constant expression support. |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 354 | /// |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 355 | void emitSelectOperation(MachineBasicBlock *MBB, |
| 356 | MachineBasicBlock::iterator IP, |
| 357 | Value *Cond, Value *TrueVal, Value *FalseVal, |
| 358 | unsigned DestReg); |
| 359 | |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 360 | /// copyGlobalBaseToRegister - Output the instructions required to put the |
| 361 | /// base address to use for accessing globals into a register. |
| 362 | /// |
| 363 | void ISel::copyGlobalBaseToRegister(MachineBasicBlock *MBB, |
| 364 | MachineBasicBlock::iterator IP, |
| 365 | unsigned R); |
| 366 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 367 | /// copyConstantToRegister - Output the instructions required to put the |
| 368 | /// specified constant into the specified register. |
| 369 | /// |
| 370 | void copyConstantToRegister(MachineBasicBlock *MBB, |
| 371 | MachineBasicBlock::iterator MBBI, |
| 372 | Constant *C, unsigned Reg); |
| 373 | |
| 374 | void emitUCOM(MachineBasicBlock *MBB, MachineBasicBlock::iterator MBBI, |
| 375 | unsigned LHS, unsigned RHS); |
| 376 | |
| 377 | /// makeAnotherReg - This method returns the next register number we haven't |
| 378 | /// yet used. |
| 379 | /// |
| 380 | /// Long values are handled somewhat specially. They are always allocated |
| 381 | /// as pairs of 32 bit integer values. The register number returned is the |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 382 | /// high 32 bits of the long value, and the regNum+1 is the low 32 bits. |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 383 | /// |
| 384 | unsigned makeAnotherReg(const Type *Ty) { |
| 385 | assert(dynamic_cast<const PowerPCRegisterInfo*>(TM.getRegisterInfo()) && |
| 386 | "Current target doesn't have PPC reg info??"); |
Nate Begeman | b64af91 | 2004-08-10 20:42:36 +0000 | [diff] [blame] | 387 | const PowerPCRegisterInfo *PPCRI = |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 388 | static_cast<const PowerPCRegisterInfo*>(TM.getRegisterInfo()); |
| 389 | if (Ty == Type::LongTy || Ty == Type::ULongTy) { |
Nate Begeman | b64af91 | 2004-08-10 20:42:36 +0000 | [diff] [blame] | 390 | const TargetRegisterClass *RC = PPCRI->getRegClassForType(Type::IntTy); |
| 391 | // Create the upper part |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 392 | F->getSSARegMap()->createVirtualRegister(RC); |
Nate Begeman | b64af91 | 2004-08-10 20:42:36 +0000 | [diff] [blame] | 393 | // Create the lower part. |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 394 | return F->getSSARegMap()->createVirtualRegister(RC)-1; |
| 395 | } |
| 396 | |
| 397 | // Add the mapping of regnumber => reg class to MachineFunction |
Nate Begeman | b64af91 | 2004-08-10 20:42:36 +0000 | [diff] [blame] | 398 | const TargetRegisterClass *RC = PPCRI->getRegClassForType(Ty); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 399 | return F->getSSARegMap()->createVirtualRegister(RC); |
| 400 | } |
| 401 | |
| 402 | /// getReg - This method turns an LLVM value into a register number. |
| 403 | /// |
| 404 | unsigned getReg(Value &V) { return getReg(&V); } // Allow references |
| 405 | unsigned getReg(Value *V) { |
| 406 | // Just append to the end of the current bb. |
| 407 | MachineBasicBlock::iterator It = BB->end(); |
| 408 | return getReg(V, BB, It); |
| 409 | } |
| 410 | unsigned getReg(Value *V, MachineBasicBlock *MBB, |
| 411 | MachineBasicBlock::iterator IPt); |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 412 | |
| 413 | /// canUseAsImmediateForOpcode - This method returns whether a ConstantInt |
| 414 | /// is okay to use as an immediate argument to a certain binary operation |
| 415 | bool canUseAsImmediateForOpcode(ConstantInt *CI, unsigned Opcode); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 416 | |
| 417 | /// getFixedSizedAllocaFI - Return the frame index for a fixed sized alloca |
| 418 | /// that is to be statically allocated with the initial stack frame |
| 419 | /// adjustment. |
| 420 | unsigned getFixedSizedAllocaFI(AllocaInst *AI); |
| 421 | }; |
| 422 | } |
| 423 | |
| 424 | /// dyn_castFixedAlloca - If the specified value is a fixed size alloca |
| 425 | /// instruction in the entry block, return it. Otherwise, return a null |
| 426 | /// pointer. |
| 427 | static AllocaInst *dyn_castFixedAlloca(Value *V) { |
| 428 | if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) { |
| 429 | BasicBlock *BB = AI->getParent(); |
| 430 | if (isa<ConstantUInt>(AI->getArraySize()) && BB ==&BB->getParent()->front()) |
| 431 | return AI; |
| 432 | } |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | /// getReg - This method turns an LLVM value into a register number. |
| 437 | /// |
| 438 | unsigned ISel::getReg(Value *V, MachineBasicBlock *MBB, |
| 439 | MachineBasicBlock::iterator IPt) { |
Misha Brukman | ba1c1da | 2004-07-20 00:59:38 +0000 | [diff] [blame] | 440 | if (Constant *C = dyn_cast<Constant>(V)) { |
Chris Lattner | a51e4f6 | 2004-07-18 18:45:01 +0000 | [diff] [blame] | 441 | unsigned Reg = makeAnotherReg(V->getType()); |
| 442 | copyConstantToRegister(MBB, IPt, C, Reg); |
| 443 | return Reg; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 444 | } else if (AllocaInst *AI = dyn_castFixedAlloca(V)) { |
| 445 | unsigned Reg = makeAnotherReg(V->getType()); |
| 446 | unsigned FI = getFixedSizedAllocaFI(AI); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 447 | addFrameReference(BuildMI(*MBB, IPt, PPC::ADDI, 2, Reg), FI, 0, false); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 448 | return Reg; |
| 449 | } |
| 450 | |
| 451 | unsigned &Reg = RegMap[V]; |
| 452 | if (Reg == 0) { |
| 453 | Reg = makeAnotherReg(V->getType()); |
| 454 | RegMap[V] = Reg; |
| 455 | } |
| 456 | |
| 457 | return Reg; |
| 458 | } |
| 459 | |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 460 | /// canUseAsImmediateForOpcode - This method returns whether a ConstantInt |
| 461 | /// is okay to use as an immediate argument to a certain binary operator. |
| 462 | /// |
| 463 | /// Operator is one of: 0 for Add, 1 for Sub, 2 for And, 3 for Or, 4 for Xor. |
Misha Brukman | 4722544 | 2004-07-23 22:35:49 +0000 | [diff] [blame] | 464 | bool ISel::canUseAsImmediateForOpcode(ConstantInt *CI, unsigned Operator) { |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 465 | ConstantSInt *Op1Cs; |
| 466 | ConstantUInt *Op1Cu; |
| 467 | |
| 468 | // ADDI, Compare, and non-indexed Load take SIMM |
Misha Brukman | 17a9000 | 2004-07-21 20:22:06 +0000 | [diff] [blame] | 469 | bool cond1 = (Operator == 0) |
| 470 | && (Op1Cs = dyn_cast<ConstantSInt>(CI)) |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 471 | && (Op1Cs->getValue() <= 32767) |
Misha Brukman | 17a9000 | 2004-07-21 20:22:06 +0000 | [diff] [blame] | 472 | && (Op1Cs->getValue() >= -32768); |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 473 | |
| 474 | // SUBI takes -SIMM since it is a mnemonic for ADDI |
Misha Brukman | 17a9000 | 2004-07-21 20:22:06 +0000 | [diff] [blame] | 475 | bool cond2 = (Operator == 1) |
| 476 | && (Op1Cs = dyn_cast<ConstantSInt>(CI)) |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 477 | && (Op1Cs->getValue() <= 32768) |
Misha Brukman | 17a9000 | 2004-07-21 20:22:06 +0000 | [diff] [blame] | 478 | && (Op1Cs->getValue() >= -32767); |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 479 | |
| 480 | // ANDIo, ORI, and XORI take unsigned values |
Misha Brukman | 17a9000 | 2004-07-21 20:22:06 +0000 | [diff] [blame] | 481 | bool cond3 = (Operator >= 2) |
Misha Brukman | 2ed17ca | 2004-07-22 15:58:04 +0000 | [diff] [blame] | 482 | && (Op1Cs = dyn_cast<ConstantSInt>(CI)) |
| 483 | && (Op1Cs->getValue() >= 0) |
Misha Brukman | 17a9000 | 2004-07-21 20:22:06 +0000 | [diff] [blame] | 484 | && (Op1Cs->getValue() <= 32767); |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 485 | |
| 486 | // ADDI and SUBI take SIMMs, so we have to make sure the UInt would fit |
Misha Brukman | 17a9000 | 2004-07-21 20:22:06 +0000 | [diff] [blame] | 487 | bool cond4 = (Operator < 2) |
| 488 | && (Op1Cu = dyn_cast<ConstantUInt>(CI)) |
| 489 | && (Op1Cu->getValue() <= 32767); |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 490 | |
| 491 | // ANDIo, ORI, and XORI take UIMMs, so they can be larger |
Misha Brukman | 17a9000 | 2004-07-21 20:22:06 +0000 | [diff] [blame] | 492 | bool cond5 = (Operator >= 2) |
| 493 | && (Op1Cu = dyn_cast<ConstantUInt>(CI)) |
| 494 | && (Op1Cu->getValue() <= 65535); |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 495 | |
| 496 | if (cond1 || cond2 || cond3 || cond4 || cond5) |
| 497 | return true; |
| 498 | |
| 499 | return false; |
| 500 | } |
| 501 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 502 | /// getFixedSizedAllocaFI - Return the frame index for a fixed sized alloca |
| 503 | /// that is to be statically allocated with the initial stack frame |
| 504 | /// adjustment. |
| 505 | unsigned ISel::getFixedSizedAllocaFI(AllocaInst *AI) { |
| 506 | // Already computed this? |
| 507 | std::map<AllocaInst*, unsigned>::iterator I = AllocaMap.lower_bound(AI); |
| 508 | if (I != AllocaMap.end() && I->first == AI) return I->second; |
| 509 | |
| 510 | const Type *Ty = AI->getAllocatedType(); |
| 511 | ConstantUInt *CUI = cast<ConstantUInt>(AI->getArraySize()); |
| 512 | unsigned TySize = TM.getTargetData().getTypeSize(Ty); |
| 513 | TySize *= CUI->getValue(); // Get total allocated size... |
| 514 | unsigned Alignment = TM.getTargetData().getTypeAlignment(Ty); |
| 515 | |
| 516 | // Create a new stack object using the frame manager... |
| 517 | int FrameIdx = F->getFrameInfo()->CreateStackObject(TySize, Alignment); |
| 518 | AllocaMap.insert(I, std::make_pair(AI, FrameIdx)); |
| 519 | return FrameIdx; |
| 520 | } |
| 521 | |
| 522 | |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 523 | /// copyGlobalBaseToRegister - Output the instructions required to put the |
| 524 | /// base address to use for accessing globals into a register. |
| 525 | /// |
| 526 | void ISel::copyGlobalBaseToRegister(MachineBasicBlock *MBB, |
| 527 | MachineBasicBlock::iterator IP, |
| 528 | unsigned R) { |
| 529 | if (!GlobalBaseInitialized) { |
| 530 | // Insert the set of GlobalBaseReg into the first MBB of the function |
| 531 | MachineBasicBlock &FirstMBB = F->front(); |
| 532 | MachineBasicBlock::iterator MBBI = FirstMBB.begin(); |
| 533 | GlobalBaseReg = makeAnotherReg(Type::IntTy); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 534 | BuildMI(FirstMBB, MBBI, PPC::IMPLICIT_DEF, 0, PPC::LR); |
| 535 | BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, GlobalBaseReg); |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 536 | GlobalBaseInitialized = true; |
| 537 | } |
| 538 | // Emit our copy of GlobalBaseReg to the destination register in the |
| 539 | // current MBB |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 540 | BuildMI(*MBB, IP, PPC::OR, 2, R).addReg(GlobalBaseReg) |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 541 | .addReg(GlobalBaseReg); |
| 542 | } |
| 543 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 544 | /// copyConstantToRegister - Output the instructions required to put the |
| 545 | /// specified constant into the specified register. |
| 546 | /// |
| 547 | void ISel::copyConstantToRegister(MachineBasicBlock *MBB, |
| 548 | MachineBasicBlock::iterator IP, |
| 549 | Constant *C, unsigned R) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 550 | if (C->getType()->isIntegral()) { |
| 551 | unsigned Class = getClassB(C->getType()); |
| 552 | |
| 553 | if (Class == cLong) { |
Misha Brukman | a0af38c | 2004-07-28 19:13:49 +0000 | [diff] [blame] | 554 | if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(C)) { |
| 555 | uint64_t uval = CUI->getValue(); |
| 556 | unsigned hiUVal = uval >> 32; |
| 557 | unsigned loUVal = uval; |
| 558 | ConstantUInt *CUHi = ConstantUInt::get(Type::UIntTy, hiUVal); |
| 559 | ConstantUInt *CULo = ConstantUInt::get(Type::UIntTy, loUVal); |
| 560 | copyConstantToRegister(MBB, IP, CUHi, R); |
| 561 | copyConstantToRegister(MBB, IP, CULo, R+1); |
| 562 | return; |
| 563 | } else if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(C)) { |
| 564 | int64_t sval = CSI->getValue(); |
| 565 | int hiSVal = sval >> 32; |
| 566 | int loSVal = sval; |
| 567 | ConstantSInt *CSHi = ConstantSInt::get(Type::IntTy, hiSVal); |
| 568 | ConstantSInt *CSLo = ConstantSInt::get(Type::IntTy, loSVal); |
| 569 | copyConstantToRegister(MBB, IP, CSHi, R); |
| 570 | copyConstantToRegister(MBB, IP, CSLo, R+1); |
| 571 | return; |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 572 | } else { |
Misha Brukman | a0af38c | 2004-07-28 19:13:49 +0000 | [diff] [blame] | 573 | std::cerr << "Unhandled long constant type!\n"; |
| 574 | abort(); |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | assert(Class <= cInt && "Type not handled yet!"); |
| 579 | |
| 580 | // Handle bool |
| 581 | if (C->getType() == Type::BoolTy) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 582 | BuildMI(*MBB, IP, PPC::LI, 1, R).addSImm(C == ConstantBool::True); |
Misha Brukman | a0af38c | 2004-07-28 19:13:49 +0000 | [diff] [blame] | 583 | return; |
| 584 | } |
| 585 | |
| 586 | // Handle int |
| 587 | if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(C)) { |
| 588 | unsigned uval = CUI->getValue(); |
| 589 | if (uval < 32768) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 590 | BuildMI(*MBB, IP, PPC::LI, 1, R).addSImm(uval); |
Misha Brukman | a0af38c | 2004-07-28 19:13:49 +0000 | [diff] [blame] | 591 | } else { |
| 592 | unsigned Temp = makeAnotherReg(Type::IntTy); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 593 | BuildMI(*MBB, IP, PPC::LIS, 1, Temp).addSImm(uval >> 16); |
| 594 | BuildMI(*MBB, IP, PPC::ORI, 2, R).addReg(Temp).addImm(uval); |
Misha Brukman | a0af38c | 2004-07-28 19:13:49 +0000 | [diff] [blame] | 595 | } |
| 596 | return; |
| 597 | } else if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(C)) { |
| 598 | int sval = CSI->getValue(); |
| 599 | if (sval < 32768 && sval >= -32768) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 600 | BuildMI(*MBB, IP, PPC::LI, 1, R).addSImm(sval); |
Misha Brukman | a0af38c | 2004-07-28 19:13:49 +0000 | [diff] [blame] | 601 | } else { |
| 602 | unsigned Temp = makeAnotherReg(Type::IntTy); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 603 | BuildMI(*MBB, IP, PPC::LIS, 1, Temp).addSImm(sval >> 16); |
| 604 | BuildMI(*MBB, IP, PPC::ORI, 2, R).addReg(Temp).addImm(sval); |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 605 | } |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 606 | return; |
| 607 | } |
Misha Brukman | a0af38c | 2004-07-28 19:13:49 +0000 | [diff] [blame] | 608 | std::cerr << "Unhandled integer constant!\n"; |
| 609 | abort(); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 610 | } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { |
Misha Brukman | d18a31d | 2004-07-06 22:51:53 +0000 | [diff] [blame] | 611 | // We need to spill the constant to memory... |
| 612 | MachineConstantPool *CP = F->getConstantPool(); |
| 613 | unsigned CPI = CP->getConstantPoolIndex(CFP); |
| 614 | const Type *Ty = CFP->getType(); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 615 | |
Misha Brukman | d18a31d | 2004-07-06 22:51:53 +0000 | [diff] [blame] | 616 | assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!"); |
Misha Brukman | fc879c3 | 2004-07-08 18:02:38 +0000 | [diff] [blame] | 617 | |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 618 | // Load addr of constant to reg; constant is located at base + distance |
| 619 | unsigned GlobalBase = makeAnotherReg(Type::IntTy); |
Misha Brukman | fc879c3 | 2004-07-08 18:02:38 +0000 | [diff] [blame] | 620 | unsigned Reg1 = makeAnotherReg(Type::IntTy); |
| 621 | unsigned Reg2 = makeAnotherReg(Type::IntTy); |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 622 | // Move value at base + distance into return reg |
| 623 | copyGlobalBaseToRegister(MBB, IP, GlobalBase); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 624 | BuildMI(*MBB, IP, PPC::LOADHiAddr, 2, Reg1).addReg(GlobalBase) |
Misha Brukman | fc879c3 | 2004-07-08 18:02:38 +0000 | [diff] [blame] | 625 | .addConstantPoolIndex(CPI); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 626 | BuildMI(*MBB, IP, PPC::LOADLoDirect, 2, Reg2).addReg(Reg1) |
Misha Brukman | fc879c3 | 2004-07-08 18:02:38 +0000 | [diff] [blame] | 627 | .addConstantPoolIndex(CPI); |
| 628 | |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 629 | unsigned LoadOpcode = (Ty == Type::FloatTy) ? PPC::LFS : PPC::LFD; |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 630 | BuildMI(*MBB, IP, LoadOpcode, 2, R).addSImm(0).addReg(Reg2); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 631 | } else if (isa<ConstantPointerNull>(C)) { |
| 632 | // Copy zero (null pointer) to the register. |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 633 | BuildMI(*MBB, IP, PPC::LI, 1, R).addSImm(0); |
Chris Lattner | 67910e1 | 2004-07-18 07:29:35 +0000 | [diff] [blame] | 634 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) { |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 635 | // GV is located at base + distance |
| 636 | unsigned GlobalBase = makeAnotherReg(Type::IntTy); |
Misha Brukman | ba1c1da | 2004-07-20 00:59:38 +0000 | [diff] [blame] | 637 | unsigned TmpReg = makeAnotherReg(GV->getType()); |
Misha Brukman | bf417a6 | 2004-07-20 20:43:05 +0000 | [diff] [blame] | 638 | unsigned Opcode = (GV->hasWeakLinkage() || GV->isExternal()) ? |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 639 | PPC::LOADLoIndirect : PPC::LOADLoDirect; |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 640 | |
| 641 | // Move value at base + distance into return reg |
| 642 | copyGlobalBaseToRegister(MBB, IP, GlobalBase); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 643 | BuildMI(*MBB, IP, PPC::LOADHiAddr, 2, TmpReg).addReg(GlobalBase) |
Misha Brukman | ba1c1da | 2004-07-20 00:59:38 +0000 | [diff] [blame] | 644 | .addGlobalAddress(GV); |
Misha Brukman | ec6319a | 2004-07-20 15:51:37 +0000 | [diff] [blame] | 645 | BuildMI(*MBB, IP, Opcode, 2, R).addReg(TmpReg).addGlobalAddress(GV); |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 646 | |
| 647 | // Add the GV to the list of things whose addresses have been taken. |
| 648 | TM.AddressTaken.insert(GV); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 649 | } else { |
Chris Lattner | 76e2df2 | 2004-07-15 02:14:30 +0000 | [diff] [blame] | 650 | std::cerr << "Offending constant: " << *C << "\n"; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 651 | assert(0 && "Type not handled yet!"); |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | /// LoadArgumentsToVirtualRegs - Load all of the arguments to this function from |
| 656 | /// the stack into virtual registers. |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 657 | void ISel::LoadArgumentsToVirtualRegs(Function &Fn) { |
Chris Lattner | 3ea9346 | 2004-08-06 06:58:50 +0000 | [diff] [blame] | 658 | unsigned ArgOffset = 24; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 659 | unsigned GPR_remaining = 8; |
| 660 | unsigned FPR_remaining = 13; |
Misha Brukman | d18a31d | 2004-07-06 22:51:53 +0000 | [diff] [blame] | 661 | unsigned GPR_idx = 0, FPR_idx = 0; |
| 662 | static const unsigned GPR[] = { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 663 | PPC::R3, PPC::R4, PPC::R5, PPC::R6, |
| 664 | PPC::R7, PPC::R8, PPC::R9, PPC::R10, |
Misha Brukman | d18a31d | 2004-07-06 22:51:53 +0000 | [diff] [blame] | 665 | }; |
| 666 | static const unsigned FPR[] = { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 667 | PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7, |
| 668 | PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13 |
Misha Brukman | d18a31d | 2004-07-06 22:51:53 +0000 | [diff] [blame] | 669 | }; |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 670 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 671 | MachineFrameInfo *MFI = F->getFrameInfo(); |
Misha Brukman | d18a31d | 2004-07-06 22:51:53 +0000 | [diff] [blame] | 672 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 673 | for (Function::aiterator I = Fn.abegin(), E = Fn.aend(); I != E; ++I) { |
| 674 | bool ArgLive = !I->use_empty(); |
| 675 | unsigned Reg = ArgLive ? getReg(*I) : 0; |
| 676 | int FI; // Frame object index |
| 677 | |
| 678 | switch (getClassB(I->getType())) { |
| 679 | case cByte: |
| 680 | if (ArgLive) { |
Misha Brukman | ec6319a | 2004-07-20 15:51:37 +0000 | [diff] [blame] | 681 | FI = MFI->CreateFixedObject(4, ArgOffset); |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 682 | if (GPR_remaining > 0) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 683 | BuildMI(BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]); |
| 684 | BuildMI(BB, PPC::OR, 2, Reg).addReg(GPR[GPR_idx]) |
Misha Brukman | d18a31d | 2004-07-06 22:51:53 +0000 | [diff] [blame] | 685 | .addReg(GPR[GPR_idx]); |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 686 | } else { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 687 | addFrameReference(BuildMI(BB, PPC::LBZ, 2, Reg), FI); |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 688 | } |
| 689 | } |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 690 | break; |
| 691 | case cShort: |
| 692 | if (ArgLive) { |
Misha Brukman | ec6319a | 2004-07-20 15:51:37 +0000 | [diff] [blame] | 693 | FI = MFI->CreateFixedObject(4, ArgOffset); |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 694 | if (GPR_remaining > 0) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 695 | BuildMI(BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]); |
| 696 | BuildMI(BB, PPC::OR, 2, Reg).addReg(GPR[GPR_idx]) |
Misha Brukman | d18a31d | 2004-07-06 22:51:53 +0000 | [diff] [blame] | 697 | .addReg(GPR[GPR_idx]); |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 698 | } else { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 699 | addFrameReference(BuildMI(BB, PPC::LHZ, 2, Reg), FI); |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 700 | } |
| 701 | } |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 702 | break; |
| 703 | case cInt: |
| 704 | if (ArgLive) { |
| 705 | FI = MFI->CreateFixedObject(4, ArgOffset); |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 706 | if (GPR_remaining > 0) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 707 | BuildMI(BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]); |
| 708 | BuildMI(BB, PPC::OR, 2, Reg).addReg(GPR[GPR_idx]) |
Misha Brukman | d18a31d | 2004-07-06 22:51:53 +0000 | [diff] [blame] | 709 | .addReg(GPR[GPR_idx]); |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 710 | } else { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 711 | addFrameReference(BuildMI(BB, PPC::LWZ, 2, Reg), FI); |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 712 | } |
| 713 | } |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 714 | break; |
| 715 | case cLong: |
| 716 | if (ArgLive) { |
| 717 | FI = MFI->CreateFixedObject(8, ArgOffset); |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 718 | if (GPR_remaining > 1) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 719 | BuildMI(BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx]); |
| 720 | BuildMI(BB, PPC::IMPLICIT_DEF, 0, GPR[GPR_idx+1]); |
| 721 | BuildMI(BB, PPC::OR, 2, Reg).addReg(GPR[GPR_idx]) |
Misha Brukman | 313efcb | 2004-07-09 15:45:07 +0000 | [diff] [blame] | 722 | .addReg(GPR[GPR_idx]); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 723 | BuildMI(BB, PPC::OR, 2, Reg+1).addReg(GPR[GPR_idx+1]) |
Misha Brukman | 313efcb | 2004-07-09 15:45:07 +0000 | [diff] [blame] | 724 | .addReg(GPR[GPR_idx+1]); |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 725 | } else { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 726 | addFrameReference(BuildMI(BB, PPC::LWZ, 2, Reg), FI); |
| 727 | addFrameReference(BuildMI(BB, PPC::LWZ, 2, Reg+1), FI, 4); |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 728 | } |
| 729 | } |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 730 | // longs require 4 additional bytes and use 2 GPRs |
| 731 | ArgOffset += 4; |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 732 | if (GPR_remaining > 1) { |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 733 | GPR_remaining--; |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 734 | GPR_idx++; |
| 735 | } |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 736 | break; |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 737 | case cFP32: |
| 738 | if (ArgLive) { |
| 739 | FI = MFI->CreateFixedObject(4, ArgOffset); |
| 740 | |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 741 | if (FPR_remaining > 0) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 742 | BuildMI(BB, PPC::IMPLICIT_DEF, 0, FPR[FPR_idx]); |
| 743 | BuildMI(BB, PPC::FMR, 1, Reg).addReg(FPR[FPR_idx]); |
Misha Brukman | d18a31d | 2004-07-06 22:51:53 +0000 | [diff] [blame] | 744 | FPR_remaining--; |
| 745 | FPR_idx++; |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 746 | } else { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 747 | addFrameReference(BuildMI(BB, PPC::LFS, 2, Reg), FI); |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 748 | } |
| 749 | } |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 750 | break; |
| 751 | case cFP64: |
| 752 | if (ArgLive) { |
| 753 | FI = MFI->CreateFixedObject(8, ArgOffset); |
| 754 | |
| 755 | if (FPR_remaining > 0) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 756 | BuildMI(BB, PPC::IMPLICIT_DEF, 0, FPR[FPR_idx]); |
| 757 | BuildMI(BB, PPC::FMR, 1, Reg).addReg(FPR[FPR_idx]); |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 758 | FPR_remaining--; |
| 759 | FPR_idx++; |
| 760 | } else { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 761 | addFrameReference(BuildMI(BB, PPC::LFD, 2, Reg), FI); |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 762 | } |
| 763 | } |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 764 | |
| 765 | // doubles require 4 additional bytes and use 2 GPRs of param space |
| 766 | ArgOffset += 4; |
| 767 | if (GPR_remaining > 0) { |
| 768 | GPR_remaining--; |
| 769 | GPR_idx++; |
| 770 | } |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 771 | break; |
| 772 | default: |
| 773 | assert(0 && "Unhandled argument type!"); |
| 774 | } |
| 775 | ArgOffset += 4; // Each argument takes at least 4 bytes on the stack... |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 776 | if (GPR_remaining > 0) { |
Misha Brukman | d18a31d | 2004-07-06 22:51:53 +0000 | [diff] [blame] | 777 | GPR_remaining--; // uses up 2 GPRs |
| 778 | GPR_idx++; |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 779 | } |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | // If the function takes variable number of arguments, add a frame offset for |
| 783 | // the start of the first vararg value... this is used to expand |
| 784 | // llvm.va_start. |
| 785 | if (Fn.getFunctionType()->isVarArg()) |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 786 | VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | |
| 790 | /// SelectPHINodes - Insert machine code to generate phis. This is tricky |
| 791 | /// because we have to generate our sources into the source basic blocks, not |
| 792 | /// the current one. |
| 793 | /// |
| 794 | void ISel::SelectPHINodes() { |
| 795 | const TargetInstrInfo &TII = *TM.getInstrInfo(); |
| 796 | const Function &LF = *F->getFunction(); // The LLVM function... |
| 797 | for (Function::const_iterator I = LF.begin(), E = LF.end(); I != E; ++I) { |
| 798 | const BasicBlock *BB = I; |
| 799 | MachineBasicBlock &MBB = *MBBMap[I]; |
| 800 | |
| 801 | // Loop over all of the PHI nodes in the LLVM basic block... |
| 802 | MachineBasicBlock::iterator PHIInsertPoint = MBB.begin(); |
| 803 | for (BasicBlock::const_iterator I = BB->begin(); |
| 804 | PHINode *PN = const_cast<PHINode*>(dyn_cast<PHINode>(I)); ++I) { |
| 805 | |
| 806 | // Create a new machine instr PHI node, and insert it. |
| 807 | unsigned PHIReg = getReg(*PN); |
| 808 | MachineInstr *PhiMI = BuildMI(MBB, PHIInsertPoint, |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 809 | PPC::PHI, PN->getNumOperands(), PHIReg); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 810 | |
| 811 | MachineInstr *LongPhiMI = 0; |
| 812 | if (PN->getType() == Type::LongTy || PN->getType() == Type::ULongTy) |
| 813 | LongPhiMI = BuildMI(MBB, PHIInsertPoint, |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 814 | PPC::PHI, PN->getNumOperands(), PHIReg+1); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 815 | |
| 816 | // PHIValues - Map of blocks to incoming virtual registers. We use this |
| 817 | // so that we only initialize one incoming value for a particular block, |
| 818 | // even if the block has multiple entries in the PHI node. |
| 819 | // |
| 820 | std::map<MachineBasicBlock*, unsigned> PHIValues; |
| 821 | |
| 822 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
Misha Brukman | 313efcb | 2004-07-09 15:45:07 +0000 | [diff] [blame] | 823 | MachineBasicBlock *PredMBB = 0; |
| 824 | for (MachineBasicBlock::pred_iterator PI = MBB.pred_begin (), |
| 825 | PE = MBB.pred_end (); PI != PE; ++PI) |
| 826 | if (PN->getIncomingBlock(i) == (*PI)->getBasicBlock()) { |
| 827 | PredMBB = *PI; |
| 828 | break; |
| 829 | } |
| 830 | assert (PredMBB && "Couldn't find incoming machine-cfg edge for phi"); |
| 831 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 832 | unsigned ValReg; |
| 833 | std::map<MachineBasicBlock*, unsigned>::iterator EntryIt = |
| 834 | PHIValues.lower_bound(PredMBB); |
| 835 | |
| 836 | if (EntryIt != PHIValues.end() && EntryIt->first == PredMBB) { |
| 837 | // We already inserted an initialization of the register for this |
| 838 | // predecessor. Recycle it. |
| 839 | ValReg = EntryIt->second; |
Misha Brukman | 4722544 | 2004-07-23 22:35:49 +0000 | [diff] [blame] | 840 | } else { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 841 | // Get the incoming value into a virtual register. |
| 842 | // |
| 843 | Value *Val = PN->getIncomingValue(i); |
| 844 | |
| 845 | // If this is a constant or GlobalValue, we may have to insert code |
| 846 | // into the basic block to compute it into a virtual register. |
| 847 | if ((isa<Constant>(Val) && !isa<ConstantExpr>(Val)) || |
| 848 | isa<GlobalValue>(Val)) { |
| 849 | // Simple constants get emitted at the end of the basic block, |
| 850 | // before any terminator instructions. We "know" that the code to |
| 851 | // move a constant into a register will never clobber any flags. |
| 852 | ValReg = getReg(Val, PredMBB, PredMBB->getFirstTerminator()); |
| 853 | } else { |
| 854 | // Because we don't want to clobber any values which might be in |
| 855 | // physical registers with the computation of this constant (which |
| 856 | // might be arbitrarily complex if it is a constant expression), |
| 857 | // just insert the computation at the top of the basic block. |
| 858 | MachineBasicBlock::iterator PI = PredMBB->begin(); |
Misha Brukman | 4722544 | 2004-07-23 22:35:49 +0000 | [diff] [blame] | 859 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 860 | // Skip over any PHI nodes though! |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 861 | while (PI != PredMBB->end() && PI->getOpcode() == PPC::PHI) |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 862 | ++PI; |
Misha Brukman | 4722544 | 2004-07-23 22:35:49 +0000 | [diff] [blame] | 863 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 864 | ValReg = getReg(Val, PredMBB, PI); |
| 865 | } |
| 866 | |
| 867 | // Remember that we inserted a value for this PHI for this predecessor |
| 868 | PHIValues.insert(EntryIt, std::make_pair(PredMBB, ValReg)); |
| 869 | } |
| 870 | |
| 871 | PhiMI->addRegOperand(ValReg); |
| 872 | PhiMI->addMachineBasicBlockOperand(PredMBB); |
| 873 | if (LongPhiMI) { |
| 874 | LongPhiMI->addRegOperand(ValReg+1); |
| 875 | LongPhiMI->addMachineBasicBlockOperand(PredMBB); |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | // Now that we emitted all of the incoming values for the PHI node, make |
| 880 | // sure to reposition the InsertPoint after the PHI that we just added. |
| 881 | // This is needed because we might have inserted a constant into this |
| 882 | // block, right after the PHI's which is before the old insert point! |
| 883 | PHIInsertPoint = LongPhiMI ? LongPhiMI : PhiMI; |
| 884 | ++PHIInsertPoint; |
| 885 | } |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | |
| 890 | // canFoldSetCCIntoBranchOrSelect - Return the setcc instruction if we can fold |
| 891 | // it into the conditional branch or select instruction which is the only user |
| 892 | // of the cc instruction. This is the case if the conditional branch is the |
| 893 | // only user of the setcc, and if the setcc is in the same basic block as the |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 894 | // conditional branch. |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 895 | // |
| 896 | static SetCondInst *canFoldSetCCIntoBranchOrSelect(Value *V) { |
| 897 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(V)) |
| 898 | if (SCI->hasOneUse()) { |
| 899 | Instruction *User = cast<Instruction>(SCI->use_back()); |
| 900 | if ((isa<BranchInst>(User) || isa<SelectInst>(User)) && |
Misha Brukman | bebde75 | 2004-07-16 21:06:24 +0000 | [diff] [blame] | 901 | SCI->getParent() == User->getParent()) |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 902 | return SCI; |
| 903 | } |
| 904 | return 0; |
| 905 | } |
| 906 | |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 907 | |
| 908 | // canFoldGEPIntoLoadOrStore - Return the GEP instruction if we can fold it into |
| 909 | // the load or store instruction that is the only user of the GEP. |
| 910 | // |
| 911 | static GetElementPtrInst *canFoldGEPIntoLoadOrStore(Value *V) { |
| 912 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(V)) |
| 913 | if (GEPI->hasOneUse()) { |
| 914 | Instruction *User = cast<Instruction>(GEPI->use_back()); |
| 915 | if (isa<StoreInst>(User) && |
| 916 | GEPI->getParent() == User->getParent() && |
| 917 | User->getOperand(0) != GEPI && |
| 918 | User->getOperand(1) == GEPI) { |
| 919 | ++GEPFolds; |
| 920 | return GEPI; |
| 921 | } |
| 922 | if (isa<LoadInst>(User) && |
| 923 | GEPI->getParent() == User->getParent() && |
| 924 | User->getOperand(0) == GEPI) { |
| 925 | ++GEPFolds; |
| 926 | return GEPI; |
| 927 | } |
| 928 | } |
| 929 | return 0; |
| 930 | } |
| 931 | |
| 932 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 933 | // Return a fixed numbering for setcc instructions which does not depend on the |
| 934 | // order of the opcodes. |
| 935 | // |
| 936 | static unsigned getSetCCNumber(unsigned Opcode) { |
Misha Brukman | e9c6551 | 2004-07-06 15:32:44 +0000 | [diff] [blame] | 937 | switch (Opcode) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 938 | default: assert(0 && "Unknown setcc instruction!"); |
| 939 | case Instruction::SetEQ: return 0; |
| 940 | case Instruction::SetNE: return 1; |
| 941 | case Instruction::SetLT: return 2; |
| 942 | case Instruction::SetGE: return 3; |
| 943 | case Instruction::SetGT: return 4; |
| 944 | case Instruction::SetLE: return 5; |
| 945 | } |
| 946 | } |
| 947 | |
Misha Brukman | e9c6551 | 2004-07-06 15:32:44 +0000 | [diff] [blame] | 948 | static unsigned getPPCOpcodeForSetCCNumber(unsigned Opcode) { |
| 949 | switch (Opcode) { |
| 950 | default: assert(0 && "Unknown setcc instruction!"); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 951 | case Instruction::SetEQ: return PPC::BEQ; |
| 952 | case Instruction::SetNE: return PPC::BNE; |
| 953 | case Instruction::SetLT: return PPC::BLT; |
| 954 | case Instruction::SetGE: return PPC::BGE; |
| 955 | case Instruction::SetGT: return PPC::BGT; |
| 956 | case Instruction::SetLE: return PPC::BLE; |
Misha Brukman | e9c6551 | 2004-07-06 15:32:44 +0000 | [diff] [blame] | 957 | } |
| 958 | } |
| 959 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 960 | /// emitUCOM - emits an unordered FP compare. |
| 961 | void ISel::emitUCOM(MachineBasicBlock *MBB, MachineBasicBlock::iterator IP, |
| 962 | unsigned LHS, unsigned RHS) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 963 | BuildMI(*MBB, IP, PPC::FCMPU, 2, PPC::CR0).addReg(LHS).addReg(RHS); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 964 | } |
| 965 | |
Misha Brukman | bebde75 | 2004-07-16 21:06:24 +0000 | [diff] [blame] | 966 | /// EmitComparison - emits a comparison of the two operands, returning the |
| 967 | /// extended setcc code to use. The result is in CR0. |
| 968 | /// |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 969 | unsigned ISel::EmitComparison(unsigned OpNum, Value *Op0, Value *Op1, |
| 970 | MachineBasicBlock *MBB, |
| 971 | MachineBasicBlock::iterator IP) { |
| 972 | // The arguments are already supposed to be of the same type. |
| 973 | const Type *CompTy = Op0->getType(); |
| 974 | unsigned Class = getClassB(CompTy); |
| 975 | unsigned Op0r = getReg(Op0, MBB, IP); |
Misha Brukman | 4722544 | 2004-07-23 22:35:49 +0000 | [diff] [blame] | 976 | |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 977 | // Before we do a comparison, we have to make sure that we're truncating our |
| 978 | // registers appropriately. |
| 979 | if (Class == cByte) { |
| 980 | unsigned TmpReg = makeAnotherReg(CompTy); |
| 981 | if (CompTy->isSigned()) |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 982 | BuildMI(*MBB, IP, PPC::EXTSB, 1, TmpReg).addReg(Op0r); |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 983 | else |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 984 | BuildMI(*MBB, IP, PPC::RLWINM, 4, TmpReg).addReg(Op0r).addImm(0) |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 985 | .addImm(24).addImm(31); |
| 986 | Op0r = TmpReg; |
| 987 | } else if (Class == cShort) { |
| 988 | unsigned TmpReg = makeAnotherReg(CompTy); |
| 989 | if (CompTy->isSigned()) |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 990 | BuildMI(*MBB, IP, PPC::EXTSH, 1, TmpReg).addReg(Op0r); |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 991 | else |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 992 | BuildMI(*MBB, IP, PPC::RLWINM, 4, TmpReg).addReg(Op0r).addImm(0) |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 993 | .addImm(16).addImm(31); |
| 994 | Op0r = TmpReg; |
| 995 | } |
| 996 | |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 997 | // Use crand for lt, gt and crandc for le, ge |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 998 | unsigned CROpcode = (OpNum == 2 || OpNum == 4) ? PPC::CRAND : PPC::CRANDC; |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 999 | // ? cr1[lt] : cr1[gt] |
| 1000 | unsigned CR1field = (OpNum == 2 || OpNum == 3) ? 4 : 5; |
| 1001 | // ? cr0[lt] : cr0[gt] |
| 1002 | unsigned CR0field = (OpNum == 2 || OpNum == 5) ? 0 : 1; |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1003 | unsigned Opcode = CompTy->isSigned() ? PPC::CMPW : PPC::CMPLW; |
| 1004 | unsigned OpcodeImm = CompTy->isSigned() ? PPC::CMPWI : PPC::CMPLWI; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1005 | |
| 1006 | // Special case handling of: cmp R, i |
Misha Brukman | 2ed17ca | 2004-07-22 15:58:04 +0000 | [diff] [blame] | 1007 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1008 | if (Class == cByte || Class == cShort || Class == cInt) { |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 1009 | unsigned Op1v = CI->getRawValue() & 0xFFFF; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1010 | |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 1011 | // Treat compare like ADDI for the purposes of immediate suitability |
| 1012 | if (canUseAsImmediateForOpcode(CI, 0)) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1013 | BuildMI(*MBB, IP, OpcodeImm, 2, PPC::CR0).addReg(Op0r).addSImm(Op1v); |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 1014 | } else { |
| 1015 | unsigned Op1r = getReg(Op1, MBB, IP); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1016 | BuildMI(*MBB, IP, Opcode, 2, PPC::CR0).addReg(Op0r).addReg(Op1r); |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 1017 | } |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1018 | return OpNum; |
| 1019 | } else { |
| 1020 | assert(Class == cLong && "Unknown integer class!"); |
| 1021 | unsigned LowCst = CI->getRawValue(); |
| 1022 | unsigned HiCst = CI->getRawValue() >> 32; |
| 1023 | if (OpNum < 2) { // seteq, setne |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 1024 | unsigned LoLow = makeAnotherReg(Type::IntTy); |
| 1025 | unsigned LoTmp = makeAnotherReg(Type::IntTy); |
| 1026 | unsigned HiLow = makeAnotherReg(Type::IntTy); |
| 1027 | unsigned HiTmp = makeAnotherReg(Type::IntTy); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1028 | unsigned FinalTmp = makeAnotherReg(Type::IntTy); |
Misha Brukman | 4722544 | 2004-07-23 22:35:49 +0000 | [diff] [blame] | 1029 | |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1030 | BuildMI(*MBB, IP, PPC::XORI, 2, LoLow).addReg(Op0r+1) |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 1031 | .addImm(LowCst & 0xFFFF); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1032 | BuildMI(*MBB, IP, PPC::XORIS, 2, LoTmp).addReg(LoLow) |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 1033 | .addImm(LowCst >> 16); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1034 | BuildMI(*MBB, IP, PPC::XORI, 2, HiLow).addReg(Op0r) |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 1035 | .addImm(HiCst & 0xFFFF); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1036 | BuildMI(*MBB, IP, PPC::XORIS, 2, HiTmp).addReg(HiLow) |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 1037 | .addImm(HiCst >> 16); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1038 | BuildMI(*MBB, IP, PPC::ORo, 2, FinalTmp).addReg(LoTmp).addReg(HiTmp); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1039 | return OpNum; |
| 1040 | } else { |
Misha Brukman | bebde75 | 2004-07-16 21:06:24 +0000 | [diff] [blame] | 1041 | unsigned ConstReg = makeAnotherReg(CompTy); |
Misha Brukman | bebde75 | 2004-07-16 21:06:24 +0000 | [diff] [blame] | 1042 | copyConstantToRegister(MBB, IP, CI, ConstReg); |
Misha Brukman | 4722544 | 2004-07-23 22:35:49 +0000 | [diff] [blame] | 1043 | |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 1044 | // cr0 = r3 ccOpcode r5 or (r3 == r5 AND r4 ccOpcode r6) |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1045 | BuildMI(*MBB, IP, Opcode, 2, PPC::CR0).addReg(Op0r) |
Misha Brukman | bebde75 | 2004-07-16 21:06:24 +0000 | [diff] [blame] | 1046 | .addReg(ConstReg); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1047 | BuildMI(*MBB, IP, Opcode, 2, PPC::CR1).addReg(Op0r+1) |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 1048 | .addReg(ConstReg+1); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1049 | BuildMI(*MBB, IP, PPC::CRAND, 3).addImm(2).addImm(2).addImm(CR1field); |
| 1050 | BuildMI(*MBB, IP, PPC::CROR, 3).addImm(CR0field).addImm(CR0field) |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 1051 | .addImm(2); |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 1052 | return OpNum; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1053 | } |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | unsigned Op1r = getReg(Op1, MBB, IP); |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 1058 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1059 | switch (Class) { |
| 1060 | default: assert(0 && "Unknown type class!"); |
| 1061 | case cByte: |
| 1062 | case cShort: |
| 1063 | case cInt: |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1064 | BuildMI(*MBB, IP, Opcode, 2, PPC::CR0).addReg(Op0r).addReg(Op1r); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1065 | break; |
Misha Brukman | d18a31d | 2004-07-06 22:51:53 +0000 | [diff] [blame] | 1066 | |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 1067 | case cFP32: |
| 1068 | case cFP64: |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1069 | emitUCOM(MBB, IP, Op0r, Op1r); |
| 1070 | break; |
| 1071 | |
| 1072 | case cLong: |
| 1073 | if (OpNum < 2) { // seteq, setne |
| 1074 | unsigned LoTmp = makeAnotherReg(Type::IntTy); |
| 1075 | unsigned HiTmp = makeAnotherReg(Type::IntTy); |
| 1076 | unsigned FinalTmp = makeAnotherReg(Type::IntTy); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1077 | BuildMI(*MBB, IP, PPC::XOR, 2, HiTmp).addReg(Op0r).addReg(Op1r); |
| 1078 | BuildMI(*MBB, IP, PPC::XOR, 2, LoTmp).addReg(Op0r+1).addReg(Op1r+1); |
| 1079 | BuildMI(*MBB, IP, PPC::ORo, 2, FinalTmp).addReg(LoTmp).addReg(HiTmp); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1080 | break; // Allow the sete or setne to be generated from flags set by OR |
| 1081 | } else { |
Misha Brukman | bebde75 | 2004-07-16 21:06:24 +0000 | [diff] [blame] | 1082 | unsigned TmpReg1 = makeAnotherReg(Type::IntTy); |
| 1083 | unsigned TmpReg2 = makeAnotherReg(Type::IntTy); |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 1084 | |
| 1085 | // cr0 = r3 ccOpcode r5 or (r3 == r5 AND r4 ccOpcode r6) |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1086 | BuildMI(*MBB, IP, Opcode, 2, PPC::CR0).addReg(Op0r).addReg(Op1r); |
| 1087 | BuildMI(*MBB, IP, Opcode, 2, PPC::CR1).addReg(Op0r+1).addReg(Op1r+1); |
| 1088 | BuildMI(*MBB, IP, PPC::CRAND, 3).addImm(2).addImm(2).addImm(CR1field); |
| 1089 | BuildMI(*MBB, IP, PPC::CROR, 3).addImm(CR0field).addImm(CR0field) |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 1090 | .addImm(2); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1091 | return OpNum; |
| 1092 | } |
| 1093 | } |
| 1094 | return OpNum; |
| 1095 | } |
| 1096 | |
Misha Brukman | d18a31d | 2004-07-06 22:51:53 +0000 | [diff] [blame] | 1097 | /// visitSetCondInst - emit code to calculate the condition via |
| 1098 | /// EmitComparison(), and possibly store a 0 or 1 to a register as a result |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1099 | /// |
| 1100 | void ISel::visitSetCondInst(SetCondInst &I) { |
Misha Brukman | d18a31d | 2004-07-06 22:51:53 +0000 | [diff] [blame] | 1101 | if (canFoldSetCCIntoBranchOrSelect(&I)) |
Misha Brukman | e9c6551 | 2004-07-06 15:32:44 +0000 | [diff] [blame] | 1102 | return; |
Misha Brukman | bebde75 | 2004-07-16 21:06:24 +0000 | [diff] [blame] | 1103 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1104 | unsigned DestReg = getReg(I); |
Misha Brukman | 2834a4d | 2004-07-07 20:07:22 +0000 | [diff] [blame] | 1105 | unsigned OpNum = I.getOpcode(); |
Misha Brukman | 425ff24 | 2004-07-01 21:34:10 +0000 | [diff] [blame] | 1106 | const Type *Ty = I.getOperand (0)->getType(); |
Misha Brukman | 4722544 | 2004-07-23 22:35:49 +0000 | [diff] [blame] | 1107 | |
Misha Brukman | d18a31d | 2004-07-06 22:51:53 +0000 | [diff] [blame] | 1108 | EmitComparison(OpNum, I.getOperand(0), I.getOperand(1), BB, BB->end()); |
Misha Brukman | 4722544 | 2004-07-23 22:35:49 +0000 | [diff] [blame] | 1109 | |
Misha Brukman | d18a31d | 2004-07-06 22:51:53 +0000 | [diff] [blame] | 1110 | unsigned Opcode = getPPCOpcodeForSetCCNumber(OpNum); |
Misha Brukman | 425ff24 | 2004-07-01 21:34:10 +0000 | [diff] [blame] | 1111 | MachineBasicBlock *thisMBB = BB; |
| 1112 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 1113 | ilist<MachineBasicBlock>::iterator It = BB; |
| 1114 | ++It; |
| 1115 | |
Misha Brukman | 425ff24 | 2004-07-01 21:34:10 +0000 | [diff] [blame] | 1116 | // thisMBB: |
| 1117 | // ... |
| 1118 | // cmpTY cr0, r1, r2 |
| 1119 | // bCC copy1MBB |
| 1120 | // b copy0MBB |
| 1121 | |
| 1122 | // FIXME: we wouldn't need copy0MBB (we could fold it into thisMBB) |
| 1123 | // if we could insert other, non-terminator instructions after the |
| 1124 | // bCC. But MBB->getFirstTerminator() can't understand this. |
| 1125 | MachineBasicBlock *copy1MBB = new MachineBasicBlock(LLVM_BB); |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 1126 | F->getBasicBlockList().insert(It, copy1MBB); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1127 | BuildMI(BB, Opcode, 2).addReg(PPC::CR0).addMBB(copy1MBB); |
Misha Brukman | 425ff24 | 2004-07-01 21:34:10 +0000 | [diff] [blame] | 1128 | MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB); |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 1129 | F->getBasicBlockList().insert(It, copy0MBB); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1130 | BuildMI(BB, PPC::B, 1).addMBB(copy0MBB); |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 1131 | MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB); |
| 1132 | F->getBasicBlockList().insert(It, sinkMBB); |
Misha Brukman | 425ff24 | 2004-07-01 21:34:10 +0000 | [diff] [blame] | 1133 | // Update machine-CFG edges |
| 1134 | BB->addSuccessor(copy1MBB); |
| 1135 | BB->addSuccessor(copy0MBB); |
| 1136 | |
Misha Brukman | 425ff24 | 2004-07-01 21:34:10 +0000 | [diff] [blame] | 1137 | // copy1MBB: |
| 1138 | // %TrueValue = li 1 |
Misha Brukman | e9c6551 | 2004-07-06 15:32:44 +0000 | [diff] [blame] | 1139 | // b sinkMBB |
Misha Brukman | 425ff24 | 2004-07-01 21:34:10 +0000 | [diff] [blame] | 1140 | BB = copy1MBB; |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 1141 | unsigned TrueValue = makeAnotherReg(I.getType()); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1142 | BuildMI(BB, PPC::LI, 1, TrueValue).addSImm(1); |
| 1143 | BuildMI(BB, PPC::B, 1).addMBB(sinkMBB); |
Misha Brukman | 425ff24 | 2004-07-01 21:34:10 +0000 | [diff] [blame] | 1144 | // Update machine-CFG edges |
| 1145 | BB->addSuccessor(sinkMBB); |
| 1146 | |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 1147 | // copy0MBB: |
| 1148 | // %FalseValue = li 0 |
| 1149 | // fallthrough |
| 1150 | BB = copy0MBB; |
| 1151 | unsigned FalseValue = makeAnotherReg(I.getType()); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1152 | BuildMI(BB, PPC::LI, 1, FalseValue).addSImm(0); |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 1153 | // Update machine-CFG edges |
| 1154 | BB->addSuccessor(sinkMBB); |
| 1155 | |
Misha Brukman | 425ff24 | 2004-07-01 21:34:10 +0000 | [diff] [blame] | 1156 | // sinkMBB: |
| 1157 | // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, copy1MBB ] |
| 1158 | // ... |
| 1159 | BB = sinkMBB; |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1160 | BuildMI(BB, PPC::PHI, 4, DestReg).addReg(FalseValue) |
Misha Brukman | 425ff24 | 2004-07-01 21:34:10 +0000 | [diff] [blame] | 1161 | .addMBB(copy0MBB).addReg(TrueValue).addMBB(copy1MBB); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1164 | void ISel::visitSelectInst(SelectInst &SI) { |
| 1165 | unsigned DestReg = getReg(SI); |
| 1166 | MachineBasicBlock::iterator MII = BB->end(); |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 1167 | emitSelectOperation(BB, MII, SI.getCondition(), SI.getTrueValue(), |
| 1168 | SI.getFalseValue(), DestReg); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | /// emitSelect - Common code shared between visitSelectInst and the constant |
| 1172 | /// expression support. |
| 1173 | /// FIXME: this is most likely broken in one or more ways. Namely, PowerPC has |
| 1174 | /// no select instruction. FSEL only works for comparisons against zero. |
| 1175 | void ISel::emitSelectOperation(MachineBasicBlock *MBB, |
| 1176 | MachineBasicBlock::iterator IP, |
| 1177 | Value *Cond, Value *TrueVal, Value *FalseVal, |
| 1178 | unsigned DestReg) { |
| 1179 | unsigned SelectClass = getClassB(TrueVal->getType()); |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 1180 | unsigned Opcode; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1181 | |
Misha Brukman | bebde75 | 2004-07-16 21:06:24 +0000 | [diff] [blame] | 1182 | // See if we can fold the setcc into the select instruction, or if we have |
| 1183 | // to get the register of the Cond value |
Misha Brukman | bebde75 | 2004-07-16 21:06:24 +0000 | [diff] [blame] | 1184 | if (SetCondInst *SCI = canFoldSetCCIntoBranchOrSelect(Cond)) { |
| 1185 | // We successfully folded the setcc into the select instruction. |
Misha Brukman | bebde75 | 2004-07-16 21:06:24 +0000 | [diff] [blame] | 1186 | unsigned OpNum = getSetCCNumber(SCI->getOpcode()); |
Misha Brukman | 4722544 | 2004-07-23 22:35:49 +0000 | [diff] [blame] | 1187 | OpNum = EmitComparison(OpNum, SCI->getOperand(0),SCI->getOperand(1),MBB,IP); |
Misha Brukman | bebde75 | 2004-07-16 21:06:24 +0000 | [diff] [blame] | 1188 | Opcode = getPPCOpcodeForSetCCNumber(SCI->getOpcode()); |
| 1189 | } else { |
| 1190 | unsigned CondReg = getReg(Cond, MBB, IP); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1191 | BuildMI(*MBB, IP, PPC::CMPI, 2, PPC::CR0).addReg(CondReg).addSImm(0); |
Misha Brukman | bebde75 | 2004-07-16 21:06:24 +0000 | [diff] [blame] | 1192 | Opcode = getPPCOpcodeForSetCCNumber(Instruction::SetNE); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1193 | } |
Misha Brukman | bebde75 | 2004-07-16 21:06:24 +0000 | [diff] [blame] | 1194 | |
| 1195 | // thisMBB: |
| 1196 | // ... |
| 1197 | // cmpTY cr0, r1, r2 |
| 1198 | // bCC copy1MBB |
| 1199 | // b copy0MBB |
| 1200 | |
| 1201 | MachineBasicBlock *thisMBB = BB; |
| 1202 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 1203 | ilist<MachineBasicBlock>::iterator It = BB; |
| 1204 | ++It; |
Misha Brukman | bebde75 | 2004-07-16 21:06:24 +0000 | [diff] [blame] | 1205 | |
| 1206 | // FIXME: we wouldn't need copy0MBB (we could fold it into thisMBB) |
| 1207 | // if we could insert other, non-terminator instructions after the |
| 1208 | // bCC. But MBB->getFirstTerminator() can't understand this. |
| 1209 | MachineBasicBlock *copy1MBB = new MachineBasicBlock(LLVM_BB); |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 1210 | F->getBasicBlockList().insert(It, copy1MBB); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1211 | BuildMI(BB, Opcode, 2).addReg(PPC::CR0).addMBB(copy1MBB); |
Misha Brukman | bebde75 | 2004-07-16 21:06:24 +0000 | [diff] [blame] | 1212 | MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB); |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 1213 | F->getBasicBlockList().insert(It, copy0MBB); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1214 | BuildMI(BB, PPC::B, 1).addMBB(copy0MBB); |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 1215 | MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB); |
| 1216 | F->getBasicBlockList().insert(It, sinkMBB); |
Misha Brukman | bebde75 | 2004-07-16 21:06:24 +0000 | [diff] [blame] | 1217 | // Update machine-CFG edges |
| 1218 | BB->addSuccessor(copy1MBB); |
| 1219 | BB->addSuccessor(copy0MBB); |
| 1220 | |
Misha Brukman | bebde75 | 2004-07-16 21:06:24 +0000 | [diff] [blame] | 1221 | // copy1MBB: |
| 1222 | // %TrueValue = ... |
| 1223 | // b sinkMBB |
| 1224 | BB = copy1MBB; |
| 1225 | unsigned TrueValue = getReg(TrueVal, BB, BB->begin()); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1226 | BuildMI(BB, PPC::B, 1).addMBB(sinkMBB); |
Misha Brukman | bebde75 | 2004-07-16 21:06:24 +0000 | [diff] [blame] | 1227 | // Update machine-CFG edges |
| 1228 | BB->addSuccessor(sinkMBB); |
| 1229 | |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 1230 | // copy0MBB: |
| 1231 | // %FalseValue = ... |
| 1232 | // fallthrough |
| 1233 | BB = copy0MBB; |
| 1234 | unsigned FalseValue = getReg(FalseVal, BB, BB->begin()); |
| 1235 | // Update machine-CFG edges |
| 1236 | BB->addSuccessor(sinkMBB); |
| 1237 | |
Misha Brukman | bebde75 | 2004-07-16 21:06:24 +0000 | [diff] [blame] | 1238 | // sinkMBB: |
| 1239 | // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, copy1MBB ] |
| 1240 | // ... |
| 1241 | BB = sinkMBB; |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1242 | BuildMI(BB, PPC::PHI, 4, DestReg).addReg(FalseValue) |
Misha Brukman | bebde75 | 2004-07-16 21:06:24 +0000 | [diff] [blame] | 1243 | .addMBB(copy0MBB).addReg(TrueValue).addMBB(copy1MBB); |
Misha Brukman | a31f1f7 | 2004-07-21 20:30:18 +0000 | [diff] [blame] | 1244 | // For a register pair representing a long value, define the second reg |
Nate Begeman | 8d963e6 | 2004-08-11 03:30:55 +0000 | [diff] [blame] | 1245 | if (getClassB(TrueVal->getType()) == cLong) |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1246 | BuildMI(BB, PPC::LI, 1, DestReg+1).addImm(0); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1247 | return; |
| 1248 | } |
| 1249 | |
| 1250 | |
| 1251 | |
| 1252 | /// promote32 - Emit instructions to turn a narrow operand into a 32-bit-wide |
| 1253 | /// operand, in the specified target register. |
| 1254 | /// |
| 1255 | void ISel::promote32(unsigned targetReg, const ValueRecord &VR) { |
| 1256 | bool isUnsigned = VR.Ty->isUnsigned() || VR.Ty == Type::BoolTy; |
| 1257 | |
| 1258 | Value *Val = VR.Val; |
| 1259 | const Type *Ty = VR.Ty; |
| 1260 | if (Val) { |
| 1261 | if (Constant *C = dyn_cast<Constant>(Val)) { |
| 1262 | Val = ConstantExpr::getCast(C, Type::IntTy); |
| 1263 | Ty = Type::IntTy; |
| 1264 | } |
| 1265 | |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 1266 | // If this is a simple constant, just emit a load directly to avoid the copy |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1267 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) { |
| 1268 | int TheVal = CI->getRawValue() & 0xFFFFFFFF; |
| 1269 | |
| 1270 | if (TheVal < 32768 && TheVal >= -32768) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1271 | BuildMI(BB, PPC::LI, 1, targetReg).addSImm(TheVal); |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 1272 | } else { |
| 1273 | unsigned TmpReg = makeAnotherReg(Type::IntTy); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1274 | BuildMI(BB, PPC::LIS, 1, TmpReg).addSImm(TheVal >> 16); |
| 1275 | BuildMI(BB, PPC::ORI, 2, targetReg).addReg(TmpReg) |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 1276 | .addImm(TheVal & 0xFFFF); |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 1277 | } |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1278 | return; |
| 1279 | } |
| 1280 | } |
| 1281 | |
| 1282 | // Make sure we have the register number for this value... |
| 1283 | unsigned Reg = Val ? getReg(Val) : VR.Reg; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1284 | switch (getClassB(Ty)) { |
| 1285 | case cByte: |
| 1286 | // Extend value into target register (8->32) |
| 1287 | if (isUnsigned) |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1288 | BuildMI(BB, PPC::RLWINM, 4, targetReg).addReg(Reg).addZImm(0) |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 1289 | .addZImm(24).addZImm(31); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1290 | else |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1291 | BuildMI(BB, PPC::EXTSB, 1, targetReg).addReg(Reg); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1292 | break; |
| 1293 | case cShort: |
| 1294 | // Extend value into target register (16->32) |
| 1295 | if (isUnsigned) |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1296 | BuildMI(BB, PPC::RLWINM, 4, targetReg).addReg(Reg).addZImm(0) |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 1297 | .addZImm(16).addZImm(31); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1298 | else |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1299 | BuildMI(BB, PPC::EXTSH, 1, targetReg).addReg(Reg); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1300 | break; |
| 1301 | case cInt: |
| 1302 | // Move value into target register (32->32) |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1303 | BuildMI(BB, PPC::OR, 2, targetReg).addReg(Reg).addReg(Reg); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1304 | break; |
| 1305 | default: |
| 1306 | assert(0 && "Unpromotable operand class in promote32"); |
| 1307 | } |
| 1308 | } |
| 1309 | |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 1310 | /// visitReturnInst - implemented with BLR |
| 1311 | /// |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1312 | void ISel::visitReturnInst(ReturnInst &I) { |
Misha Brukman | d47bbf7 | 2004-06-25 19:04:27 +0000 | [diff] [blame] | 1313 | // Only do the processing if this is a non-void return |
| 1314 | if (I.getNumOperands() > 0) { |
| 1315 | Value *RetVal = I.getOperand(0); |
| 1316 | switch (getClassB(RetVal->getType())) { |
| 1317 | case cByte: // integral return values: extend or move into r3 and return |
| 1318 | case cShort: |
| 1319 | case cInt: |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1320 | promote32(PPC::R3, ValueRecord(RetVal)); |
Misha Brukman | d47bbf7 | 2004-06-25 19:04:27 +0000 | [diff] [blame] | 1321 | break; |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 1322 | case cFP32: |
| 1323 | case cFP64: { // Floats & Doubles: Return in f1 |
Misha Brukman | d47bbf7 | 2004-06-25 19:04:27 +0000 | [diff] [blame] | 1324 | unsigned RetReg = getReg(RetVal); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1325 | BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(RetReg); |
Misha Brukman | d47bbf7 | 2004-06-25 19:04:27 +0000 | [diff] [blame] | 1326 | break; |
| 1327 | } |
| 1328 | case cLong: { |
| 1329 | unsigned RetReg = getReg(RetVal); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1330 | BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(RetReg).addReg(RetReg); |
| 1331 | BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(RetReg+1).addReg(RetReg+1); |
Misha Brukman | d47bbf7 | 2004-06-25 19:04:27 +0000 | [diff] [blame] | 1332 | break; |
| 1333 | } |
| 1334 | default: |
| 1335 | visitInstruction(I); |
| 1336 | } |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1337 | } |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1338 | BuildMI(BB, PPC::BLR, 1).addImm(0); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | // getBlockAfter - Return the basic block which occurs lexically after the |
| 1342 | // specified one. |
| 1343 | static inline BasicBlock *getBlockAfter(BasicBlock *BB) { |
| 1344 | Function::iterator I = BB; ++I; // Get iterator to next block |
| 1345 | return I != BB->getParent()->end() ? &*I : 0; |
| 1346 | } |
| 1347 | |
| 1348 | /// visitBranchInst - Handle conditional and unconditional branches here. Note |
| 1349 | /// that since code layout is frozen at this point, that if we are trying to |
| 1350 | /// jump to a block that is the immediate successor of the current block, we can |
| 1351 | /// just make a fall-through (but we don't currently). |
| 1352 | /// |
| 1353 | void ISel::visitBranchInst(BranchInst &BI) { |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 1354 | // Update machine-CFG edges |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 1355 | BB->addSuccessor(MBBMap[BI.getSuccessor(0)]); |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 1356 | if (BI.isConditional()) |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 1357 | BB->addSuccessor(MBBMap[BI.getSuccessor(1)]); |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 1358 | |
| 1359 | BasicBlock *NextBB = getBlockAfter(BI.getParent()); // BB after current one |
Misha Brukman | e9c6551 | 2004-07-06 15:32:44 +0000 | [diff] [blame] | 1360 | |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 1361 | if (!BI.isConditional()) { // Unconditional branch? |
Misha Brukman | e9c6551 | 2004-07-06 15:32:44 +0000 | [diff] [blame] | 1362 | if (BI.getSuccessor(0) != NextBB) |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1363 | BuildMI(BB, PPC::B, 1).addMBB(MBBMap[BI.getSuccessor(0)]); |
Misha Brukman | fadb82f | 2004-06-24 22:00:15 +0000 | [diff] [blame] | 1364 | return; |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1367 | // See if we can fold the setcc into the branch itself... |
| 1368 | SetCondInst *SCI = canFoldSetCCIntoBranchOrSelect(BI.getCondition()); |
| 1369 | if (SCI == 0) { |
| 1370 | // Nope, cannot fold setcc into this branch. Emit a branch on a condition |
| 1371 | // computed some other way... |
| 1372 | unsigned condReg = getReg(BI.getCondition()); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1373 | BuildMI(BB, PPC::CMPLI, 3, PPC::CR0).addImm(0).addReg(condReg) |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 1374 | .addImm(0); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1375 | if (BI.getSuccessor(1) == NextBB) { |
| 1376 | if (BI.getSuccessor(0) != NextBB) |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1377 | BuildMI(BB, PPC::COND_BRANCH, 3).addReg(PPC::CR0).addImm(PPC::BNE) |
Misha Brukman | fa20a6d | 2004-07-27 18:35:23 +0000 | [diff] [blame] | 1378 | .addMBB(MBBMap[BI.getSuccessor(0)]) |
| 1379 | .addMBB(MBBMap[BI.getSuccessor(1)]); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1380 | } else { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1381 | BuildMI(BB, PPC::COND_BRANCH, 3).addReg(PPC::CR0).addImm(PPC::BEQ) |
Misha Brukman | fa20a6d | 2004-07-27 18:35:23 +0000 | [diff] [blame] | 1382 | .addMBB(MBBMap[BI.getSuccessor(1)]) |
| 1383 | .addMBB(MBBMap[BI.getSuccessor(0)]); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1384 | if (BI.getSuccessor(0) != NextBB) |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1385 | BuildMI(BB, PPC::B, 1).addMBB(MBBMap[BI.getSuccessor(0)]); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1386 | } |
| 1387 | return; |
| 1388 | } |
| 1389 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1390 | unsigned OpNum = getSetCCNumber(SCI->getOpcode()); |
Misha Brukman | e9c6551 | 2004-07-06 15:32:44 +0000 | [diff] [blame] | 1391 | unsigned Opcode = getPPCOpcodeForSetCCNumber(SCI->getOpcode()); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1392 | MachineBasicBlock::iterator MII = BB->end(); |
| 1393 | OpNum = EmitComparison(OpNum, SCI->getOperand(0), SCI->getOperand(1), BB,MII); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1394 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1395 | if (BI.getSuccessor(0) != NextBB) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1396 | BuildMI(BB, PPC::COND_BRANCH, 3).addReg(PPC::CR0).addImm(Opcode) |
Misha Brukman | fa20a6d | 2004-07-27 18:35:23 +0000 | [diff] [blame] | 1397 | .addMBB(MBBMap[BI.getSuccessor(0)]) |
| 1398 | .addMBB(MBBMap[BI.getSuccessor(1)]); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1399 | if (BI.getSuccessor(1) != NextBB) |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1400 | BuildMI(BB, PPC::B, 1).addMBB(MBBMap[BI.getSuccessor(1)]); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1401 | } else { |
| 1402 | // Change to the inverse condition... |
| 1403 | if (BI.getSuccessor(1) != NextBB) { |
Misha Brukman | fa20a6d | 2004-07-27 18:35:23 +0000 | [diff] [blame] | 1404 | Opcode = PowerPCInstrInfo::invertPPCBranchOpcode(Opcode); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1405 | BuildMI(BB, PPC::COND_BRANCH, 3).addReg(PPC::CR0).addImm(Opcode) |
Misha Brukman | fa20a6d | 2004-07-27 18:35:23 +0000 | [diff] [blame] | 1406 | .addMBB(MBBMap[BI.getSuccessor(1)]) |
| 1407 | .addMBB(MBBMap[BI.getSuccessor(0)]); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1408 | } |
| 1409 | } |
| 1410 | } |
| 1411 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1412 | /// doCall - This emits an abstract call instruction, setting up the arguments |
| 1413 | /// and the return value as appropriate. For the actual function call itself, |
| 1414 | /// it inserts the specified CallMI instruction into the stream. |
| 1415 | /// |
| 1416 | /// FIXME: See Documentation at the following URL for "correct" behavior |
| 1417 | /// <http://developer.apple.com/documentation/DeveloperTools/Conceptual/MachORuntime/2rt_powerpc_abi/chapter_9_section_5.html> |
| 1418 | void ISel::doCall(const ValueRecord &Ret, MachineInstr *CallMI, |
Misha Brukman | d18a31d | 2004-07-06 22:51:53 +0000 | [diff] [blame] | 1419 | const std::vector<ValueRecord> &Args, bool isVarArg) { |
Chris Lattner | 3ea9346 | 2004-08-06 06:58:50 +0000 | [diff] [blame] | 1420 | // Count how many bytes are to be pushed on the stack, including the linkage |
| 1421 | // area, and parameter passing area. |
| 1422 | unsigned NumBytes = 24; |
| 1423 | unsigned ArgOffset = 24; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1424 | |
| 1425 | if (!Args.empty()) { |
| 1426 | for (unsigned i = 0, e = Args.size(); i != e; ++i) |
| 1427 | switch (getClassB(Args[i].Ty)) { |
| 1428 | case cByte: case cShort: case cInt: |
| 1429 | NumBytes += 4; break; |
| 1430 | case cLong: |
| 1431 | NumBytes += 8; break; |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 1432 | case cFP32: |
| 1433 | NumBytes += 4; break; |
| 1434 | case cFP64: |
| 1435 | NumBytes += 8; break; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1436 | break; |
| 1437 | default: assert(0 && "Unknown class!"); |
| 1438 | } |
| 1439 | |
Chris Lattner | 3ea9346 | 2004-08-06 06:58:50 +0000 | [diff] [blame] | 1440 | // Just to be safe, we'll always reserve the full 32 bytes worth of |
| 1441 | // argument passing space in case any called code gets funky on us. |
| 1442 | if (NumBytes < 24 + 32) NumBytes = 24 + 32; |
| 1443 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1444 | // Adjust the stack pointer for the new arguments... |
Chris Lattner | 3ea9346 | 2004-08-06 06:58:50 +0000 | [diff] [blame] | 1445 | // These functions are automatically eliminated by the prolog/epilog pass |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1446 | BuildMI(BB, PPC::ADJCALLSTACKDOWN, 1).addImm(NumBytes); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1447 | |
| 1448 | // Arguments go on the stack in reverse order, as specified by the ABI. |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 1449 | // Offset to the paramater area on the stack is 24. |
Misha Brukman | d18a31d | 2004-07-06 22:51:53 +0000 | [diff] [blame] | 1450 | int GPR_remaining = 8, FPR_remaining = 13; |
Misha Brukman | fc879c3 | 2004-07-08 18:02:38 +0000 | [diff] [blame] | 1451 | unsigned GPR_idx = 0, FPR_idx = 0; |
Misha Brukman | d18a31d | 2004-07-06 22:51:53 +0000 | [diff] [blame] | 1452 | static const unsigned GPR[] = { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1453 | PPC::R3, PPC::R4, PPC::R5, PPC::R6, |
| 1454 | PPC::R7, PPC::R8, PPC::R9, PPC::R10, |
Misha Brukman | 14d8c7a | 2004-06-29 23:45:05 +0000 | [diff] [blame] | 1455 | }; |
Misha Brukman | d18a31d | 2004-07-06 22:51:53 +0000 | [diff] [blame] | 1456 | static const unsigned FPR[] = { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1457 | PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, |
| 1458 | PPC::F7, PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, |
| 1459 | PPC::F13 |
Misha Brukman | 14d8c7a | 2004-06-29 23:45:05 +0000 | [diff] [blame] | 1460 | }; |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 1461 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1462 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
| 1463 | unsigned ArgReg; |
| 1464 | switch (getClassB(Args[i].Ty)) { |
| 1465 | case cByte: |
| 1466 | case cShort: |
| 1467 | // Promote arg to 32 bits wide into a temporary register... |
| 1468 | ArgReg = makeAnotherReg(Type::UIntTy); |
| 1469 | promote32(ArgReg, Args[i]); |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 1470 | |
| 1471 | // Reg or stack? |
| 1472 | if (GPR_remaining > 0) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1473 | BuildMI(BB, PPC::OR, 2, GPR[GPR_idx]).addReg(ArgReg) |
Misha Brukman | fadb82f | 2004-06-24 22:00:15 +0000 | [diff] [blame] | 1474 | .addReg(ArgReg); |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 1475 | CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use); |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 1476 | } |
| 1477 | if (GPR_remaining <= 0 || isVarArg) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1478 | BuildMI(BB, PPC::STW, 3).addReg(ArgReg).addSImm(ArgOffset) |
| 1479 | .addReg(PPC::R1); |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 1480 | } |
| 1481 | break; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1482 | case cInt: |
| 1483 | ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg; |
| 1484 | |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 1485 | // Reg or stack? |
| 1486 | if (GPR_remaining > 0) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1487 | BuildMI(BB, PPC::OR, 2, GPR[GPR_idx]).addReg(ArgReg) |
Misha Brukman | fadb82f | 2004-06-24 22:00:15 +0000 | [diff] [blame] | 1488 | .addReg(ArgReg); |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 1489 | CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use); |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 1490 | } |
| 1491 | if (GPR_remaining <= 0 || isVarArg) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1492 | BuildMI(BB, PPC::STW, 3).addReg(ArgReg).addSImm(ArgOffset) |
| 1493 | .addReg(PPC::R1); |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 1494 | } |
| 1495 | break; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1496 | case cLong: |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 1497 | ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1498 | |
Misha Brukman | ec6319a | 2004-07-20 15:51:37 +0000 | [diff] [blame] | 1499 | // Reg or stack? Note that PPC calling conventions state that long args |
| 1500 | // are passed rN = hi, rN+1 = lo, opposite of LLVM. |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 1501 | if (GPR_remaining > 1) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1502 | BuildMI(BB, PPC::OR, 2, GPR[GPR_idx]).addReg(ArgReg) |
Misha Brukman | ec6319a | 2004-07-20 15:51:37 +0000 | [diff] [blame] | 1503 | .addReg(ArgReg); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1504 | BuildMI(BB, PPC::OR, 2, GPR[GPR_idx+1]).addReg(ArgReg+1) |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 1505 | .addReg(ArgReg+1); |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 1506 | CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use); |
| 1507 | CallMI->addRegOperand(GPR[GPR_idx+1], MachineOperand::Use); |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 1508 | } |
| 1509 | if (GPR_remaining <= 1 || isVarArg) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1510 | BuildMI(BB, PPC::STW, 3).addReg(ArgReg).addSImm(ArgOffset) |
| 1511 | .addReg(PPC::R1); |
| 1512 | BuildMI(BB, PPC::STW, 3).addReg(ArgReg+1).addSImm(ArgOffset+4) |
| 1513 | .addReg(PPC::R1); |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 1514 | } |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1515 | |
| 1516 | ArgOffset += 4; // 8 byte entry, not 4. |
Misha Brukman | 14d8c7a | 2004-06-29 23:45:05 +0000 | [diff] [blame] | 1517 | GPR_remaining -= 1; // uses up 2 GPRs |
| 1518 | GPR_idx += 1; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1519 | break; |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 1520 | case cFP32: |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1521 | ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg; |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 1522 | // Reg or stack? |
| 1523 | if (FPR_remaining > 0) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1524 | BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgReg); |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 1525 | CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use); |
| 1526 | FPR_remaining--; |
| 1527 | FPR_idx++; |
| 1528 | |
| 1529 | // If this is a vararg function, and there are GPRs left, also |
| 1530 | // pass the float in an int. Otherwise, put it on the stack. |
| 1531 | if (isVarArg) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1532 | BuildMI(BB, PPC::STFS, 3).addReg(ArgReg).addSImm(ArgOffset) |
| 1533 | .addReg(PPC::R1); |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 1534 | if (GPR_remaining > 0) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1535 | BuildMI(BB, PPC::LWZ, 2, GPR[GPR_idx]) |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 1536 | .addSImm(ArgOffset).addReg(ArgReg); |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 1537 | CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use); |
| 1538 | } |
Misha Brukman | 1916bf9 | 2004-06-24 21:56:15 +0000 | [diff] [blame] | 1539 | } |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1540 | } else { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1541 | BuildMI(BB, PPC::STFS, 3).addReg(ArgReg).addSImm(ArgOffset) |
| 1542 | .addReg(PPC::R1); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1543 | } |
| 1544 | break; |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 1545 | case cFP64: |
| 1546 | ArgReg = Args[i].Val ? getReg(Args[i].Val) : Args[i].Reg; |
| 1547 | // Reg or stack? |
| 1548 | if (FPR_remaining > 0) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1549 | BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgReg); |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 1550 | CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use); |
| 1551 | FPR_remaining--; |
| 1552 | FPR_idx++; |
| 1553 | // For vararg functions, must pass doubles via int regs as well |
| 1554 | if (isVarArg) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1555 | BuildMI(BB, PPC::STFD, 3).addReg(ArgReg).addSImm(ArgOffset) |
| 1556 | .addReg(PPC::R1); |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 1557 | |
Misha Brukman | 2ed17ca | 2004-07-22 15:58:04 +0000 | [diff] [blame] | 1558 | // Doubles can be split across reg + stack for varargs |
| 1559 | if (GPR_remaining > 0) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1560 | BuildMI(BB, PPC::LWZ, 2, GPR[GPR_idx]).addSImm(ArgOffset) |
| 1561 | .addReg(PPC::R1); |
Misha Brukman | 2ed17ca | 2004-07-22 15:58:04 +0000 | [diff] [blame] | 1562 | CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use); |
| 1563 | } |
| 1564 | if (GPR_remaining > 1) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1565 | BuildMI(BB, PPC::LWZ, 2, GPR[GPR_idx+1]) |
| 1566 | .addSImm(ArgOffset+4).addReg(PPC::R1); |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 1567 | CallMI->addRegOperand(GPR[GPR_idx+1], MachineOperand::Use); |
| 1568 | } |
| 1569 | } |
| 1570 | } else { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1571 | BuildMI(BB, PPC::STFD, 3).addReg(ArgReg).addSImm(ArgOffset) |
| 1572 | .addReg(PPC::R1); |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 1573 | } |
| 1574 | // Doubles use 8 bytes, and 2 GPRs worth of param space |
| 1575 | ArgOffset += 4; |
| 1576 | GPR_remaining--; |
| 1577 | GPR_idx++; |
| 1578 | break; |
| 1579 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1580 | default: assert(0 && "Unknown class!"); |
| 1581 | } |
| 1582 | ArgOffset += 4; |
Misha Brukman | 14d8c7a | 2004-06-29 23:45:05 +0000 | [diff] [blame] | 1583 | GPR_remaining--; |
| 1584 | GPR_idx++; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1585 | } |
| 1586 | } else { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1587 | BuildMI(BB, PPC::ADJCALLSTACKDOWN, 1).addImm(0); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1588 | } |
| 1589 | |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1590 | BuildMI(BB, PPC::IMPLICIT_DEF, 0, PPC::LR); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1591 | BB->push_back(CallMI); |
Chris Lattner | 3ea9346 | 2004-08-06 06:58:50 +0000 | [diff] [blame] | 1592 | |
| 1593 | // These functions are automatically eliminated by the prolog/epilog pass |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1594 | BuildMI(BB, PPC::ADJCALLSTACKUP, 1).addImm(NumBytes); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1595 | |
| 1596 | // If there is a return value, scavenge the result from the location the call |
| 1597 | // leaves it in... |
| 1598 | // |
| 1599 | if (Ret.Ty != Type::VoidTy) { |
| 1600 | unsigned DestClass = getClassB(Ret.Ty); |
| 1601 | switch (DestClass) { |
| 1602 | case cByte: |
| 1603 | case cShort: |
| 1604 | case cInt: |
| 1605 | // Integral results are in r3 |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1606 | BuildMI(BB, PPC::OR, 2, Ret.Reg).addReg(PPC::R3).addReg(PPC::R3); |
Misha Brukman | e327e49 | 2004-06-24 23:53:24 +0000 | [diff] [blame] | 1607 | break; |
Chris Lattner | 3ea9346 | 2004-08-06 06:58:50 +0000 | [diff] [blame] | 1608 | case cFP32: // Floating-point return values live in f1 |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 1609 | case cFP64: |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1610 | BuildMI(BB, PPC::FMR, 1, Ret.Reg).addReg(PPC::F1); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1611 | break; |
Chris Lattner | 3ea9346 | 2004-08-06 06:58:50 +0000 | [diff] [blame] | 1612 | case cLong: // Long values are in r3:r4 |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1613 | BuildMI(BB, PPC::OR, 2, Ret.Reg).addReg(PPC::R3).addReg(PPC::R3); |
| 1614 | BuildMI(BB, PPC::OR, 2, Ret.Reg+1).addReg(PPC::R4).addReg(PPC::R4); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1615 | break; |
| 1616 | default: assert(0 && "Unknown class!"); |
| 1617 | } |
| 1618 | } |
| 1619 | } |
| 1620 | |
| 1621 | |
| 1622 | /// visitCallInst - Push args on stack and do a procedure call instruction. |
| 1623 | void ISel::visitCallInst(CallInst &CI) { |
| 1624 | MachineInstr *TheCall; |
Misha Brukman | d18a31d | 2004-07-06 22:51:53 +0000 | [diff] [blame] | 1625 | Function *F = CI.getCalledFunction(); |
| 1626 | if (F) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1627 | // Is it an intrinsic function call? |
| 1628 | if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) { |
| 1629 | visitIntrinsicCall(ID, CI); // Special intrinsics are not handled here |
| 1630 | return; |
| 1631 | } |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1632 | // Emit a CALL instruction with PC-relative displacement. |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1633 | TheCall = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(F, true); |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 1634 | // Add it to the set of functions called to be used by the Printer |
| 1635 | TM.CalledFunctions.insert(F); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1636 | } else { // Emit an indirect call through the CTR |
| 1637 | unsigned Reg = getReg(CI.getCalledValue()); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1638 | BuildMI(BB, PPC::MTCTR, 1).addReg(Reg); |
| 1639 | TheCall = BuildMI(PPC::CALLindirect, 2).addZImm(20).addZImm(0); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
| 1642 | std::vector<ValueRecord> Args; |
| 1643 | for (unsigned i = 1, e = CI.getNumOperands(); i != e; ++i) |
| 1644 | Args.push_back(ValueRecord(CI.getOperand(i))); |
| 1645 | |
| 1646 | unsigned DestReg = CI.getType() != Type::VoidTy ? getReg(CI) : 0; |
Misha Brukman | d18a31d | 2004-07-06 22:51:53 +0000 | [diff] [blame] | 1647 | bool isVarArg = F ? F->getFunctionType()->isVarArg() : true; |
| 1648 | doCall(ValueRecord(DestReg, CI.getType()), TheCall, Args, isVarArg); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1649 | } |
| 1650 | |
| 1651 | |
| 1652 | /// dyncastIsNan - Return the operand of an isnan operation if this is an isnan. |
| 1653 | /// |
| 1654 | static Value *dyncastIsNan(Value *V) { |
| 1655 | if (CallInst *CI = dyn_cast<CallInst>(V)) |
| 1656 | if (Function *F = CI->getCalledFunction()) |
Misha Brukman | a2916ce | 2004-06-21 17:58:36 +0000 | [diff] [blame] | 1657 | if (F->getIntrinsicID() == Intrinsic::isunordered) |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1658 | return CI->getOperand(1); |
| 1659 | return 0; |
| 1660 | } |
| 1661 | |
| 1662 | /// isOnlyUsedByUnorderedComparisons - Return true if this value is only used by |
| 1663 | /// or's whos operands are all calls to the isnan predicate. |
| 1664 | static bool isOnlyUsedByUnorderedComparisons(Value *V) { |
| 1665 | assert(dyncastIsNan(V) && "The value isn't an isnan call!"); |
| 1666 | |
| 1667 | // Check all uses, which will be or's of isnans if this predicate is true. |
| 1668 | for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;++UI){ |
| 1669 | Instruction *I = cast<Instruction>(*UI); |
| 1670 | if (I->getOpcode() != Instruction::Or) return false; |
| 1671 | if (I->getOperand(0) != V && !dyncastIsNan(I->getOperand(0))) return false; |
| 1672 | if (I->getOperand(1) != V && !dyncastIsNan(I->getOperand(1))) return false; |
| 1673 | } |
| 1674 | |
| 1675 | return true; |
| 1676 | } |
| 1677 | |
| 1678 | /// LowerUnknownIntrinsicFunctionCalls - This performs a prepass over the |
| 1679 | /// function, lowering any calls to unknown intrinsic functions into the |
| 1680 | /// equivalent LLVM code. |
| 1681 | /// |
| 1682 | void ISel::LowerUnknownIntrinsicFunctionCalls(Function &F) { |
| 1683 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 1684 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) |
| 1685 | if (CallInst *CI = dyn_cast<CallInst>(I++)) |
| 1686 | if (Function *F = CI->getCalledFunction()) |
| 1687 | switch (F->getIntrinsicID()) { |
| 1688 | case Intrinsic::not_intrinsic: |
| 1689 | case Intrinsic::vastart: |
| 1690 | case Intrinsic::vacopy: |
| 1691 | case Intrinsic::vaend: |
| 1692 | case Intrinsic::returnaddress: |
| 1693 | case Intrinsic::frameaddress: |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 1694 | // FIXME: should lower these ourselves |
Misha Brukman | a2916ce | 2004-06-21 17:58:36 +0000 | [diff] [blame] | 1695 | // case Intrinsic::isunordered: |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 1696 | // case Intrinsic::memcpy: -> doCall(). system memcpy almost |
| 1697 | // guaranteed to be faster than anything we generate ourselves |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1698 | // We directly implement these intrinsics |
| 1699 | break; |
| 1700 | case Intrinsic::readio: { |
| 1701 | // On PPC, memory operations are in-order. Lower this intrinsic |
| 1702 | // into a volatile load. |
| 1703 | Instruction *Before = CI->getPrev(); |
| 1704 | LoadInst * LI = new LoadInst(CI->getOperand(1), "", true, CI); |
| 1705 | CI->replaceAllUsesWith(LI); |
| 1706 | BB->getInstList().erase(CI); |
| 1707 | break; |
| 1708 | } |
| 1709 | case Intrinsic::writeio: { |
| 1710 | // On PPC, memory operations are in-order. Lower this intrinsic |
| 1711 | // into a volatile store. |
| 1712 | Instruction *Before = CI->getPrev(); |
Misha Brukman | 8d442c2 | 2004-07-14 15:29:51 +0000 | [diff] [blame] | 1713 | StoreInst *SI = new StoreInst(CI->getOperand(1), |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1714 | CI->getOperand(2), true, CI); |
Misha Brukman | 8d442c2 | 2004-07-14 15:29:51 +0000 | [diff] [blame] | 1715 | CI->replaceAllUsesWith(SI); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1716 | BB->getInstList().erase(CI); |
| 1717 | break; |
| 1718 | } |
| 1719 | default: |
| 1720 | // All other intrinsic calls we must lower. |
| 1721 | Instruction *Before = CI->getPrev(); |
| 1722 | TM.getIntrinsicLowering().LowerIntrinsicCall(CI); |
| 1723 | if (Before) { // Move iterator to instruction after call |
| 1724 | I = Before; ++I; |
| 1725 | } else { |
| 1726 | I = BB->begin(); |
| 1727 | } |
| 1728 | } |
| 1729 | } |
| 1730 | |
| 1731 | void ISel::visitIntrinsicCall(Intrinsic::ID ID, CallInst &CI) { |
| 1732 | unsigned TmpReg1, TmpReg2, TmpReg3; |
| 1733 | switch (ID) { |
| 1734 | case Intrinsic::vastart: |
| 1735 | // Get the address of the first vararg value... |
| 1736 | TmpReg1 = getReg(CI); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1737 | addFrameReference(BuildMI(BB, PPC::ADDI, 2, TmpReg1), VarArgsFrameIndex, |
Misha Brukman | ec6319a | 2004-07-20 15:51:37 +0000 | [diff] [blame] | 1738 | 0, false); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1739 | return; |
| 1740 | |
| 1741 | case Intrinsic::vacopy: |
| 1742 | TmpReg1 = getReg(CI); |
| 1743 | TmpReg2 = getReg(CI.getOperand(1)); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1744 | BuildMI(BB, PPC::OR, 2, TmpReg1).addReg(TmpReg2).addReg(TmpReg2); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1745 | return; |
| 1746 | case Intrinsic::vaend: return; |
| 1747 | |
| 1748 | case Intrinsic::returnaddress: |
Misha Brukman | ec6319a | 2004-07-20 15:51:37 +0000 | [diff] [blame] | 1749 | TmpReg1 = getReg(CI); |
| 1750 | if (cast<Constant>(CI.getOperand(1))->isNullValue()) { |
| 1751 | MachineFrameInfo *MFI = F->getFrameInfo(); |
| 1752 | unsigned NumBytes = MFI->getStackSize(); |
| 1753 | |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1754 | BuildMI(BB, PPC::LWZ, 2, TmpReg1).addSImm(NumBytes+8) |
| 1755 | .addReg(PPC::R1); |
Misha Brukman | ec6319a | 2004-07-20 15:51:37 +0000 | [diff] [blame] | 1756 | } else { |
| 1757 | // Values other than zero are not implemented yet. |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1758 | BuildMI(BB, PPC::LI, 1, TmpReg1).addSImm(0); |
Misha Brukman | ec6319a | 2004-07-20 15:51:37 +0000 | [diff] [blame] | 1759 | } |
| 1760 | return; |
| 1761 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1762 | case Intrinsic::frameaddress: |
| 1763 | TmpReg1 = getReg(CI); |
| 1764 | if (cast<Constant>(CI.getOperand(1))->isNullValue()) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1765 | BuildMI(BB, PPC::OR, 2, TmpReg1).addReg(PPC::R1).addReg(PPC::R1); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1766 | } else { |
| 1767 | // Values other than zero are not implemented yet. |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1768 | BuildMI(BB, PPC::LI, 1, TmpReg1).addSImm(0); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1769 | } |
| 1770 | return; |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 1771 | |
Misha Brukman | a2916ce | 2004-06-21 17:58:36 +0000 | [diff] [blame] | 1772 | #if 0 |
| 1773 | // This may be useful for supporting isunordered |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1774 | case Intrinsic::isnan: |
| 1775 | // If this is only used by 'isunordered' style comparisons, don't emit it. |
| 1776 | if (isOnlyUsedByUnorderedComparisons(&CI)) return; |
| 1777 | TmpReg1 = getReg(CI.getOperand(1)); |
| 1778 | emitUCOM(BB, BB->end(), TmpReg1, TmpReg1); |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 1779 | TmpReg2 = makeAnotherReg(Type::IntTy); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1780 | BuildMI(BB, PPC::MFCR, TmpReg2); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1781 | TmpReg3 = getReg(CI); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1782 | BuildMI(BB, PPC::RLWINM, 4, TmpReg3).addReg(TmpReg2).addImm(4).addImm(31).addImm(31); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1783 | return; |
Misha Brukman | a2916ce | 2004-06-21 17:58:36 +0000 | [diff] [blame] | 1784 | #endif |
| 1785 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1786 | default: assert(0 && "Error: unknown intrinsics should have been lowered!"); |
| 1787 | } |
| 1788 | } |
| 1789 | |
| 1790 | /// visitSimpleBinary - Implement simple binary operators for integral types... |
| 1791 | /// OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for Or, 4 for |
| 1792 | /// Xor. |
| 1793 | /// |
| 1794 | void ISel::visitSimpleBinary(BinaryOperator &B, unsigned OperatorClass) { |
| 1795 | unsigned DestReg = getReg(B); |
| 1796 | MachineBasicBlock::iterator MI = BB->end(); |
| 1797 | Value *Op0 = B.getOperand(0), *Op1 = B.getOperand(1); |
| 1798 | unsigned Class = getClassB(B.getType()); |
| 1799 | |
| 1800 | emitSimpleBinaryOperation(BB, MI, Op0, Op1, OperatorClass, DestReg); |
| 1801 | } |
| 1802 | |
| 1803 | /// emitBinaryFPOperation - This method handles emission of floating point |
| 1804 | /// Add (0), Sub (1), Mul (2), and Div (3) operations. |
| 1805 | void ISel::emitBinaryFPOperation(MachineBasicBlock *BB, |
| 1806 | MachineBasicBlock::iterator IP, |
| 1807 | Value *Op0, Value *Op1, |
| 1808 | unsigned OperatorClass, unsigned DestReg) { |
| 1809 | |
| 1810 | // Special case: op Reg, <const fp> |
| 1811 | if (ConstantFP *Op1C = dyn_cast<ConstantFP>(Op1)) { |
Misha Brukman | fadb82f | 2004-06-24 22:00:15 +0000 | [diff] [blame] | 1812 | // Create a constant pool entry for this constant. |
| 1813 | MachineConstantPool *CP = F->getConstantPool(); |
| 1814 | unsigned CPI = CP->getConstantPoolIndex(Op1C); |
| 1815 | const Type *Ty = Op1->getType(); |
Misha Brukman | d9aa783 | 2004-07-12 23:49:47 +0000 | [diff] [blame] | 1816 | assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!"); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1817 | |
Misha Brukman | fadb82f | 2004-06-24 22:00:15 +0000 | [diff] [blame] | 1818 | static const unsigned OpcodeTab[][4] = { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1819 | { PPC::FADDS, PPC::FSUBS, PPC::FMULS, PPC::FDIVS }, // Float |
| 1820 | { PPC::FADD, PPC::FSUB, PPC::FMUL, PPC::FDIV }, // Double |
Misha Brukman | fadb82f | 2004-06-24 22:00:15 +0000 | [diff] [blame] | 1821 | }; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1822 | |
Misha Brukman | fadb82f | 2004-06-24 22:00:15 +0000 | [diff] [blame] | 1823 | unsigned Opcode = OpcodeTab[Ty != Type::FloatTy][OperatorClass]; |
Misha Brukman | a596f8c | 2004-07-13 15:35:45 +0000 | [diff] [blame] | 1824 | unsigned Op1Reg = getReg(Op1C, BB, IP); |
Misha Brukman | fadb82f | 2004-06-24 22:00:15 +0000 | [diff] [blame] | 1825 | unsigned Op0r = getReg(Op0, BB, IP); |
Misha Brukman | a596f8c | 2004-07-13 15:35:45 +0000 | [diff] [blame] | 1826 | BuildMI(*BB, IP, Opcode, 2, DestReg).addReg(Op0r).addReg(Op1Reg); |
Misha Brukman | fadb82f | 2004-06-24 22:00:15 +0000 | [diff] [blame] | 1827 | return; |
| 1828 | } |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1829 | |
| 1830 | // Special case: R1 = op <const fp>, R2 |
Misha Brukman | a596f8c | 2004-07-13 15:35:45 +0000 | [diff] [blame] | 1831 | if (ConstantFP *Op0C = dyn_cast<ConstantFP>(Op0)) |
| 1832 | if (Op0C->isExactlyValue(-0.0) && OperatorClass == 1) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1833 | // -0.0 - X === -X |
| 1834 | unsigned op1Reg = getReg(Op1, BB, IP); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1835 | BuildMI(*BB, IP, PPC::FNEG, 1, DestReg).addReg(op1Reg); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1836 | return; |
| 1837 | } else { |
| 1838 | // R1 = op CST, R2 --> R1 = opr R2, CST |
| 1839 | |
| 1840 | // Create a constant pool entry for this constant. |
| 1841 | MachineConstantPool *CP = F->getConstantPool(); |
Misha Brukman | a596f8c | 2004-07-13 15:35:45 +0000 | [diff] [blame] | 1842 | unsigned CPI = CP->getConstantPoolIndex(Op0C); |
| 1843 | const Type *Ty = Op0C->getType(); |
| 1844 | assert(Ty == Type::FloatTy || Ty == Type::DoubleTy && "Unknown FP type!"); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1845 | |
| 1846 | static const unsigned OpcodeTab[][4] = { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1847 | { PPC::FADDS, PPC::FSUBS, PPC::FMULS, PPC::FDIVS }, // Float |
| 1848 | { PPC::FADD, PPC::FSUB, PPC::FMUL, PPC::FDIV }, // Double |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1849 | }; |
| 1850 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1851 | unsigned Opcode = OpcodeTab[Ty != Type::FloatTy][OperatorClass]; |
Misha Brukman | a596f8c | 2004-07-13 15:35:45 +0000 | [diff] [blame] | 1852 | unsigned Op0Reg = getReg(Op0C, BB, IP); |
| 1853 | unsigned Op1Reg = getReg(Op1, BB, IP); |
| 1854 | BuildMI(*BB, IP, Opcode, 2, DestReg).addReg(Op0Reg).addReg(Op1Reg); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1855 | return; |
| 1856 | } |
| 1857 | |
| 1858 | // General case. |
Misha Brukman | 911afde | 2004-06-25 14:50:41 +0000 | [diff] [blame] | 1859 | static const unsigned OpcodeTab[] = { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1860 | PPC::FADD, PPC::FSUB, PPC::FMUL, PPC::FDIV |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1861 | }; |
| 1862 | |
| 1863 | unsigned Opcode = OpcodeTab[OperatorClass]; |
| 1864 | unsigned Op0r = getReg(Op0, BB, IP); |
| 1865 | unsigned Op1r = getReg(Op1, BB, IP); |
| 1866 | BuildMI(*BB, IP, Opcode, 2, DestReg).addReg(Op0r).addReg(Op1r); |
| 1867 | } |
| 1868 | |
| 1869 | /// emitSimpleBinaryOperation - Implement simple binary operators for integral |
| 1870 | /// types... OperatorClass is one of: 0 for Add, 1 for Sub, 2 for And, 3 for |
| 1871 | /// Or, 4 for Xor. |
| 1872 | /// |
| 1873 | /// emitSimpleBinaryOperation - Common code shared between visitSimpleBinary |
| 1874 | /// and constant expression support. |
| 1875 | /// |
| 1876 | void ISel::emitSimpleBinaryOperation(MachineBasicBlock *MBB, |
| 1877 | MachineBasicBlock::iterator IP, |
| 1878 | Value *Op0, Value *Op1, |
| 1879 | unsigned OperatorClass, unsigned DestReg) { |
| 1880 | unsigned Class = getClassB(Op0->getType()); |
| 1881 | |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 1882 | // Arithmetic and Bitwise operators |
Misha Brukman | 911afde | 2004-06-25 14:50:41 +0000 | [diff] [blame] | 1883 | static const unsigned OpcodeTab[] = { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1884 | PPC::ADD, PPC::SUB, PPC::AND, PPC::OR, PPC::XOR |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 1885 | }; |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 1886 | static const unsigned ImmOpcodeTab[] = { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1887 | PPC::ADDI, PPC::SUBI, PPC::ANDIo, PPC::ORI, PPC::XORI |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 1888 | }; |
Misha Brukman | 2ed17ca | 2004-07-22 15:58:04 +0000 | [diff] [blame] | 1889 | static const unsigned RImmOpcodeTab[] = { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1890 | PPC::ADDI, PPC::SUBFIC, PPC::ANDIo, PPC::ORI, PPC::XORI |
Misha Brukman | 2ed17ca | 2004-07-22 15:58:04 +0000 | [diff] [blame] | 1891 | }; |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 1892 | |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 1893 | // Otherwise, code generate the full operation with a constant. |
| 1894 | static const unsigned BottomTab[] = { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1895 | PPC::ADDC, PPC::SUBC, PPC::AND, PPC::OR, PPC::XOR |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 1896 | }; |
| 1897 | static const unsigned TopTab[] = { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1898 | PPC::ADDE, PPC::SUBFE, PPC::AND, PPC::OR, PPC::XOR |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 1899 | }; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1900 | |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 1901 | if (Class == cFP32 || Class == cFP64) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1902 | assert(OperatorClass < 2 && "No logical ops for FP!"); |
| 1903 | emitBinaryFPOperation(MBB, IP, Op0, Op1, OperatorClass, DestReg); |
| 1904 | return; |
| 1905 | } |
| 1906 | |
| 1907 | if (Op0->getType() == Type::BoolTy) { |
| 1908 | if (OperatorClass == 3) |
| 1909 | // If this is an or of two isnan's, emit an FP comparison directly instead |
| 1910 | // of or'ing two isnan's together. |
| 1911 | if (Value *LHS = dyncastIsNan(Op0)) |
| 1912 | if (Value *RHS = dyncastIsNan(Op1)) { |
| 1913 | unsigned Op0Reg = getReg(RHS, MBB, IP), Op1Reg = getReg(LHS, MBB, IP); |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 1914 | unsigned TmpReg = makeAnotherReg(Type::IntTy); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1915 | emitUCOM(MBB, IP, Op0Reg, Op1Reg); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1916 | BuildMI(*MBB, IP, PPC::MFCR, TmpReg); |
| 1917 | BuildMI(*MBB, IP, PPC::RLWINM, 4, DestReg).addReg(TmpReg).addImm(4) |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 1918 | .addImm(31).addImm(31); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1919 | return; |
| 1920 | } |
| 1921 | } |
| 1922 | |
Misha Brukman | 2ed17ca | 2004-07-22 15:58:04 +0000 | [diff] [blame] | 1923 | // Special case: op <const int>, Reg |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 1924 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op0)) { |
Misha Brukman | 2ed17ca | 2004-07-22 15:58:04 +0000 | [diff] [blame] | 1925 | // sub 0, X -> subfic |
| 1926 | if (OperatorClass == 1 && canUseAsImmediateForOpcode(CI, 0)) { |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 1927 | unsigned Op1r = getReg(Op1, MBB, IP); |
Misha Brukman | 2ed17ca | 2004-07-22 15:58:04 +0000 | [diff] [blame] | 1928 | int imm = CI->getRawValue() & 0xFFFF; |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 1929 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1930 | if (Class == cLong) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1931 | BuildMI(*MBB, IP, PPC::SUBFIC, 2, DestReg+1).addReg(Op1r+1) |
Misha Brukman | 2ed17ca | 2004-07-22 15:58:04 +0000 | [diff] [blame] | 1932 | .addSImm(imm); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1933 | BuildMI(*MBB, IP, PPC::SUBFZE, 1, DestReg).addReg(Op1r); |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 1934 | } else { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1935 | BuildMI(*MBB, IP, PPC::SUBFIC, 2, DestReg).addReg(Op1r).addSImm(imm); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1936 | } |
| 1937 | return; |
| 1938 | } |
Misha Brukman | 2ed17ca | 2004-07-22 15:58:04 +0000 | [diff] [blame] | 1939 | |
| 1940 | // If it is easy to do, swap the operands and emit an immediate op |
| 1941 | if (Class != cLong && OperatorClass != 1 && |
| 1942 | canUseAsImmediateForOpcode(CI, OperatorClass)) { |
| 1943 | unsigned Op1r = getReg(Op1, MBB, IP); |
| 1944 | int imm = CI->getRawValue() & 0xFFFF; |
| 1945 | |
| 1946 | if (OperatorClass < 2) |
| 1947 | BuildMI(*MBB, IP, RImmOpcodeTab[OperatorClass], 2, DestReg).addReg(Op1r) |
| 1948 | .addSImm(imm); |
| 1949 | else |
| 1950 | BuildMI(*MBB, IP, RImmOpcodeTab[OperatorClass], 2, DestReg).addReg(Op1r) |
| 1951 | .addZImm(imm); |
| 1952 | return; |
| 1953 | } |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 1954 | } |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1955 | |
| 1956 | // Special case: op Reg, <const int> |
| 1957 | if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) { |
| 1958 | unsigned Op0r = getReg(Op0, MBB, IP); |
| 1959 | |
| 1960 | // xor X, -1 -> not X |
| 1961 | if (OperatorClass == 4 && Op1C->isAllOnesValue()) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1962 | BuildMI(*MBB, IP, PPC::NOR, 2, DestReg).addReg(Op0r).addReg(Op0r); |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 1963 | if (Class == cLong) // Invert the low part too |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 1964 | BuildMI(*MBB, IP, PPC::NOR, 2, DestReg+1).addReg(Op0r+1) |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 1965 | .addReg(Op0r+1); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1966 | return; |
| 1967 | } |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 1968 | |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 1969 | if (Class != cLong) { |
| 1970 | if (canUseAsImmediateForOpcode(Op1C, OperatorClass)) { |
| 1971 | int immediate = Op1C->getRawValue() & 0xFFFF; |
| 1972 | |
| 1973 | if (OperatorClass < 2) |
Misha Brukman | 2ed17ca | 2004-07-22 15:58:04 +0000 | [diff] [blame] | 1974 | BuildMI(*MBB, IP, ImmOpcodeTab[OperatorClass], 2,DestReg).addReg(Op0r) |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 1975 | .addSImm(immediate); |
| 1976 | else |
Misha Brukman | 2ed17ca | 2004-07-22 15:58:04 +0000 | [diff] [blame] | 1977 | BuildMI(*MBB, IP, ImmOpcodeTab[OperatorClass], 2,DestReg).addReg(Op0r) |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 1978 | .addZImm(immediate); |
| 1979 | } else { |
| 1980 | unsigned Op1r = getReg(Op1, MBB, IP); |
| 1981 | BuildMI(*MBB, IP, OpcodeTab[OperatorClass], 2, DestReg).addReg(Op0r) |
| 1982 | .addReg(Op1r); |
| 1983 | } |
| 1984 | return; |
| 1985 | } |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1986 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1987 | unsigned Op1r = getReg(Op1, MBB, IP); |
| 1988 | |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 1989 | BuildMI(*MBB, IP, BottomTab[OperatorClass], 2, DestReg+1).addReg(Op0r+1) |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 1990 | .addReg(Op1r+1); |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 1991 | BuildMI(*MBB, IP, TopTab[OperatorClass], 2, DestReg).addReg(Op0r) |
| 1992 | .addReg(Op1r); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1993 | return; |
| 1994 | } |
Misha Brukman | 2ed17ca | 2004-07-22 15:58:04 +0000 | [diff] [blame] | 1995 | |
| 1996 | // We couldn't generate an immediate variant of the op, load both halves into |
| 1997 | // registers and emit the appropriate opcode. |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 1998 | unsigned Op0r = getReg(Op0, MBB, IP); |
| 1999 | unsigned Op1r = getReg(Op1, MBB, IP); |
| 2000 | |
| 2001 | if (Class != cLong) { |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 2002 | unsigned Opcode = OpcodeTab[OperatorClass]; |
| 2003 | BuildMI(*MBB, IP, Opcode, 2, DestReg).addReg(Op0r).addReg(Op1r); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2004 | } else { |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 2005 | BuildMI(*MBB, IP, BottomTab[OperatorClass], 2, DestReg+1).addReg(Op0r+1) |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 2006 | .addReg(Op1r+1); |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 2007 | BuildMI(*MBB, IP, TopTab[OperatorClass], 2, DestReg).addReg(Op0r) |
| 2008 | .addReg(Op1r); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2009 | } |
| 2010 | return; |
| 2011 | } |
| 2012 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2013 | // ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It |
| 2014 | // returns zero when the input is not exactly a power of two. |
| 2015 | static unsigned ExactLog2(unsigned Val) { |
| 2016 | if (Val == 0 || (Val & (Val-1))) return 0; |
| 2017 | unsigned Count = 0; |
| 2018 | while (Val != 1) { |
| 2019 | Val >>= 1; |
| 2020 | ++Count; |
| 2021 | } |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 2022 | return Count; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2023 | } |
| 2024 | |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 2025 | /// doMultiply - Emit appropriate instructions to multiply together the |
| 2026 | /// Values Op0 and Op1, and put the result in DestReg. |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 2027 | /// |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 2028 | void ISel::doMultiply(MachineBasicBlock *MBB, |
| 2029 | MachineBasicBlock::iterator IP, |
| 2030 | unsigned DestReg, Value *Op0, Value *Op1) { |
| 2031 | unsigned Class0 = getClass(Op0->getType()); |
| 2032 | unsigned Class1 = getClass(Op1->getType()); |
| 2033 | |
| 2034 | unsigned Op0r = getReg(Op0, MBB, IP); |
| 2035 | unsigned Op1r = getReg(Op1, MBB, IP); |
| 2036 | |
| 2037 | // 64 x 64 -> 64 |
| 2038 | if (Class0 == cLong && Class1 == cLong) { |
| 2039 | unsigned Tmp1 = makeAnotherReg(Type::IntTy); |
| 2040 | unsigned Tmp2 = makeAnotherReg(Type::IntTy); |
| 2041 | unsigned Tmp3 = makeAnotherReg(Type::IntTy); |
| 2042 | unsigned Tmp4 = makeAnotherReg(Type::IntTy); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2043 | BuildMI(*MBB, IP, PPC::MULHWU, 2, Tmp1).addReg(Op0r+1).addReg(Op1r+1); |
| 2044 | BuildMI(*MBB, IP, PPC::MULLW, 2, DestReg+1).addReg(Op0r+1).addReg(Op1r+1); |
| 2045 | BuildMI(*MBB, IP, PPC::MULLW, 2, Tmp2).addReg(Op0r+1).addReg(Op1r); |
| 2046 | BuildMI(*MBB, IP, PPC::ADD, 2, Tmp3).addReg(Tmp1).addReg(Tmp2); |
| 2047 | BuildMI(*MBB, IP, PPC::MULLW, 2, Tmp4).addReg(Op0r).addReg(Op1r+1); |
| 2048 | BuildMI(*MBB, IP, PPC::ADD, 2, DestReg).addReg(Tmp3).addReg(Tmp4); |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 2049 | return; |
| 2050 | } |
| 2051 | |
| 2052 | // 64 x 32 or less, promote 32 to 64 and do a 64 x 64 |
| 2053 | if (Class0 == cLong && Class1 <= cInt) { |
| 2054 | unsigned Tmp0 = makeAnotherReg(Type::IntTy); |
| 2055 | unsigned Tmp1 = makeAnotherReg(Type::IntTy); |
| 2056 | unsigned Tmp2 = makeAnotherReg(Type::IntTy); |
| 2057 | unsigned Tmp3 = makeAnotherReg(Type::IntTy); |
| 2058 | unsigned Tmp4 = makeAnotherReg(Type::IntTy); |
| 2059 | if (Op1->getType()->isSigned()) |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2060 | BuildMI(*MBB, IP, PPC::SRAWI, 2, Tmp0).addReg(Op1r).addImm(31); |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 2061 | else |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2062 | BuildMI(*MBB, IP, PPC::LI, 2, Tmp0).addSImm(0); |
| 2063 | BuildMI(*MBB, IP, PPC::MULHWU, 2, Tmp1).addReg(Op0r+1).addReg(Op1r); |
| 2064 | BuildMI(*MBB, IP, PPC::MULLW, 2, DestReg+1).addReg(Op0r+1).addReg(Op1r); |
| 2065 | BuildMI(*MBB, IP, PPC::MULLW, 2, Tmp2).addReg(Op0r+1).addReg(Tmp0); |
| 2066 | BuildMI(*MBB, IP, PPC::ADD, 2, Tmp3).addReg(Tmp1).addReg(Tmp2); |
| 2067 | BuildMI(*MBB, IP, PPC::MULLW, 2, Tmp4).addReg(Op0r).addReg(Op1r); |
| 2068 | BuildMI(*MBB, IP, PPC::ADD, 2, DestReg).addReg(Tmp3).addReg(Tmp4); |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 2069 | return; |
| 2070 | } |
| 2071 | |
| 2072 | // 32 x 32 -> 32 |
| 2073 | if (Class0 <= cInt && Class1 <= cInt) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2074 | BuildMI(*MBB, IP, PPC::MULLW, 2, DestReg).addReg(Op0r).addReg(Op1r); |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 2075 | return; |
| 2076 | } |
| 2077 | |
| 2078 | assert(0 && "doMultiply cannot operate on unknown type!"); |
| 2079 | } |
| 2080 | |
| 2081 | /// doMultiplyConst - This method will multiply the value in Op0 by the |
| 2082 | /// value of the ContantInt *CI |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2083 | void ISel::doMultiplyConst(MachineBasicBlock *MBB, |
| 2084 | MachineBasicBlock::iterator IP, |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 2085 | unsigned DestReg, Value *Op0, ConstantInt *CI) { |
| 2086 | unsigned Class = getClass(Op0->getType()); |
| 2087 | |
| 2088 | // Mul op0, 0 ==> 0 |
| 2089 | if (CI->isNullValue()) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2090 | BuildMI(*MBB, IP, PPC::LI, 1, DestReg).addSImm(0); |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 2091 | if (Class == cLong) |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2092 | BuildMI(*MBB, IP, PPC::LI, 1, DestReg+1).addSImm(0); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2093 | return; |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 2094 | } |
| 2095 | |
| 2096 | // Mul op0, 1 ==> op0 |
| 2097 | if (CI->equalsInt(1)) { |
| 2098 | unsigned Op0r = getReg(Op0, MBB, IP); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2099 | BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(Op0r).addReg(Op0r); |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 2100 | if (Class == cLong) |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2101 | BuildMI(*MBB, IP, PPC::OR, 2, DestReg+1).addReg(Op0r+1).addReg(Op0r+1); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2102 | return; |
| 2103 | } |
| 2104 | |
| 2105 | // If the element size is exactly a power of 2, use a shift to get it. |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 2106 | if (unsigned Shift = ExactLog2(CI->getRawValue())) { |
| 2107 | ConstantUInt *ShiftCI = ConstantUInt::get(Type::UByteTy, Shift); |
| 2108 | emitShiftOperation(MBB, IP, Op0, ShiftCI, true, Op0->getType(), DestReg); |
| 2109 | return; |
| 2110 | } |
| 2111 | |
| 2112 | // If 32 bits or less and immediate is in right range, emit mul by immediate |
Misha Brukman | 2ed17ca | 2004-07-22 15:58:04 +0000 | [diff] [blame] | 2113 | if (Class == cByte || Class == cShort || Class == cInt) { |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 2114 | if (canUseAsImmediateForOpcode(CI, 0)) { |
| 2115 | unsigned Op0r = getReg(Op0, MBB, IP); |
| 2116 | unsigned imm = CI->getRawValue() & 0xFFFF; |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2117 | BuildMI(*MBB, IP, PPC::MULLI, 2, DestReg).addReg(Op0r).addSImm(imm); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2118 | return; |
| 2119 | } |
| 2120 | } |
| 2121 | |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 2122 | doMultiply(MBB, IP, DestReg, Op0, CI); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2123 | } |
| 2124 | |
| 2125 | void ISel::visitMul(BinaryOperator &I) { |
| 2126 | unsigned ResultReg = getReg(I); |
| 2127 | |
| 2128 | Value *Op0 = I.getOperand(0); |
| 2129 | Value *Op1 = I.getOperand(1); |
| 2130 | |
| 2131 | MachineBasicBlock::iterator IP = BB->end(); |
| 2132 | emitMultiply(BB, IP, Op0, Op1, ResultReg); |
| 2133 | } |
| 2134 | |
| 2135 | void ISel::emitMultiply(MachineBasicBlock *MBB, MachineBasicBlock::iterator IP, |
| 2136 | Value *Op0, Value *Op1, unsigned DestReg) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2137 | TypeClass Class = getClass(Op0->getType()); |
| 2138 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2139 | switch (Class) { |
| 2140 | case cByte: |
| 2141 | case cShort: |
| 2142 | case cInt: |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 2143 | case cLong: |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2144 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) { |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 2145 | doMultiplyConst(MBB, IP, DestReg, Op0, CI); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2146 | } else { |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 2147 | doMultiply(MBB, IP, DestReg, Op0, Op1); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2148 | } |
| 2149 | return; |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 2150 | case cFP32: |
| 2151 | case cFP64: |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2152 | emitBinaryFPOperation(MBB, IP, Op0, Op1, 2, DestReg); |
| 2153 | return; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2154 | break; |
| 2155 | } |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2156 | } |
| 2157 | |
| 2158 | |
| 2159 | /// visitDivRem - Handle division and remainder instructions... these |
| 2160 | /// instruction both require the same instructions to be generated, they just |
| 2161 | /// select the result from a different register. Note that both of these |
| 2162 | /// instructions work differently for signed and unsigned operands. |
| 2163 | /// |
| 2164 | void ISel::visitDivRem(BinaryOperator &I) { |
| 2165 | unsigned ResultReg = getReg(I); |
| 2166 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 2167 | |
| 2168 | MachineBasicBlock::iterator IP = BB->end(); |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 2169 | emitDivRemOperation(BB, IP, Op0, Op1, I.getOpcode() == Instruction::Div, |
| 2170 | ResultReg); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2171 | } |
| 2172 | |
| 2173 | void ISel::emitDivRemOperation(MachineBasicBlock *BB, |
| 2174 | MachineBasicBlock::iterator IP, |
| 2175 | Value *Op0, Value *Op1, bool isDiv, |
| 2176 | unsigned ResultReg) { |
| 2177 | const Type *Ty = Op0->getType(); |
| 2178 | unsigned Class = getClass(Ty); |
| 2179 | switch (Class) { |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 2180 | case cFP32: |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2181 | if (isDiv) { |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 2182 | // Floating point divide... |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2183 | emitBinaryFPOperation(BB, IP, Op0, Op1, 3, ResultReg); |
| 2184 | return; |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 2185 | } else { |
| 2186 | // Floating point remainder via fmodf(float x, float y); |
| 2187 | unsigned Op0Reg = getReg(Op0, BB, IP); |
| 2188 | unsigned Op1Reg = getReg(Op1, BB, IP); |
| 2189 | MachineInstr *TheCall = |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2190 | BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(fmodfFn, true); |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 2191 | std::vector<ValueRecord> Args; |
| 2192 | Args.push_back(ValueRecord(Op0Reg, Type::FloatTy)); |
| 2193 | Args.push_back(ValueRecord(Op1Reg, Type::FloatTy)); |
| 2194 | doCall(ValueRecord(ResultReg, Type::FloatTy), TheCall, Args, false); |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 2195 | TM.CalledFunctions.insert(fmodfFn); |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 2196 | } |
| 2197 | return; |
| 2198 | case cFP64: |
| 2199 | if (isDiv) { |
| 2200 | // Floating point divide... |
| 2201 | emitBinaryFPOperation(BB, IP, Op0, Op1, 3, ResultReg); |
| 2202 | return; |
| 2203 | } else { |
| 2204 | // Floating point remainder via fmod(double x, double y); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2205 | unsigned Op0Reg = getReg(Op0, BB, IP); |
| 2206 | unsigned Op1Reg = getReg(Op1, BB, IP); |
| 2207 | MachineInstr *TheCall = |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2208 | BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(fmodFn, true); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2209 | std::vector<ValueRecord> Args; |
| 2210 | Args.push_back(ValueRecord(Op0Reg, Type::DoubleTy)); |
| 2211 | Args.push_back(ValueRecord(Op1Reg, Type::DoubleTy)); |
Misha Brukman | d18a31d | 2004-07-06 22:51:53 +0000 | [diff] [blame] | 2212 | doCall(ValueRecord(ResultReg, Type::DoubleTy), TheCall, Args, false); |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 2213 | TM.CalledFunctions.insert(fmodFn); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2214 | } |
| 2215 | return; |
| 2216 | case cLong: { |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 2217 | static Function* const Funcs[] = |
Misha Brukman | 0aa97c6 | 2004-07-08 18:27:59 +0000 | [diff] [blame] | 2218 | { __moddi3Fn, __divdi3Fn, __umoddi3Fn, __udivdi3Fn }; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2219 | unsigned Op0Reg = getReg(Op0, BB, IP); |
| 2220 | unsigned Op1Reg = getReg(Op1, BB, IP); |
| 2221 | unsigned NameIdx = Ty->isUnsigned()*2 + isDiv; |
| 2222 | MachineInstr *TheCall = |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2223 | BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(Funcs[NameIdx], true); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2224 | |
| 2225 | std::vector<ValueRecord> Args; |
| 2226 | Args.push_back(ValueRecord(Op0Reg, Type::LongTy)); |
| 2227 | Args.push_back(ValueRecord(Op1Reg, Type::LongTy)); |
Misha Brukman | d18a31d | 2004-07-06 22:51:53 +0000 | [diff] [blame] | 2228 | doCall(ValueRecord(ResultReg, Type::LongTy), TheCall, Args, false); |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 2229 | TM.CalledFunctions.insert(Funcs[NameIdx]); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2230 | return; |
| 2231 | } |
| 2232 | case cByte: case cShort: case cInt: |
| 2233 | break; // Small integrals, handled below... |
| 2234 | default: assert(0 && "Unknown class!"); |
| 2235 | } |
| 2236 | |
| 2237 | // Special case signed division by power of 2. |
| 2238 | if (isDiv) |
| 2239 | if (ConstantSInt *CI = dyn_cast<ConstantSInt>(Op1)) { |
| 2240 | assert(Class != cLong && "This doesn't handle 64-bit divides!"); |
| 2241 | int V = CI->getValue(); |
| 2242 | |
| 2243 | if (V == 1) { // X /s 1 => X |
| 2244 | unsigned Op0Reg = getReg(Op0, BB, IP); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2245 | BuildMI(*BB, IP, PPC::OR, 2, ResultReg).addReg(Op0Reg).addReg(Op0Reg); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2246 | return; |
| 2247 | } |
| 2248 | |
| 2249 | if (V == -1) { // X /s -1 => -X |
| 2250 | unsigned Op0Reg = getReg(Op0, BB, IP); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2251 | BuildMI(*BB, IP, PPC::NEG, 1, ResultReg).addReg(Op0Reg); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2252 | return; |
| 2253 | } |
| 2254 | |
Misha Brukman | ec6319a | 2004-07-20 15:51:37 +0000 | [diff] [blame] | 2255 | unsigned log2V = ExactLog2(V); |
| 2256 | if (log2V != 0 && Ty->isSigned()) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2257 | unsigned Op0Reg = getReg(Op0, BB, IP); |
| 2258 | unsigned TmpReg = makeAnotherReg(Op0->getType()); |
Misha Brukman | ec6319a | 2004-07-20 15:51:37 +0000 | [diff] [blame] | 2259 | |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2260 | BuildMI(*BB, IP, PPC::SRAWI, 2, TmpReg).addReg(Op0Reg).addImm(log2V); |
| 2261 | BuildMI(*BB, IP, PPC::ADDZE, 1, ResultReg).addReg(TmpReg); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2262 | return; |
| 2263 | } |
| 2264 | } |
| 2265 | |
| 2266 | unsigned Op0Reg = getReg(Op0, BB, IP); |
| 2267 | unsigned Op1Reg = getReg(Op1, BB, IP); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2268 | unsigned Opcode = Ty->isSigned() ? PPC::DIVW : PPC::DIVWU; |
Misha Brukman | ec6319a | 2004-07-20 15:51:37 +0000 | [diff] [blame] | 2269 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2270 | if (isDiv) { |
Misha Brukman | ec6319a | 2004-07-20 15:51:37 +0000 | [diff] [blame] | 2271 | BuildMI(*BB, IP, Opcode, 2, ResultReg).addReg(Op0Reg).addReg(Op1Reg); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2272 | } else { // Remainder |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 2273 | unsigned TmpReg1 = makeAnotherReg(Op0->getType()); |
| 2274 | unsigned TmpReg2 = makeAnotherReg(Op0->getType()); |
| 2275 | |
Misha Brukman | ec6319a | 2004-07-20 15:51:37 +0000 | [diff] [blame] | 2276 | BuildMI(*BB, IP, Opcode, 2, TmpReg1).addReg(Op0Reg).addReg(Op1Reg); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2277 | BuildMI(*BB, IP, PPC::MULLW, 2, TmpReg2).addReg(TmpReg1).addReg(Op1Reg); |
| 2278 | BuildMI(*BB, IP, PPC::SUBF, 2, ResultReg).addReg(TmpReg2).addReg(Op0Reg); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2279 | } |
| 2280 | } |
| 2281 | |
| 2282 | |
| 2283 | /// Shift instructions: 'shl', 'sar', 'shr' - Some special cases here |
| 2284 | /// for constant immediate shift values, and for constant immediate |
| 2285 | /// shift values equal to 1. Even the general case is sort of special, |
| 2286 | /// because the shift amount has to be in CL, not just any old register. |
| 2287 | /// |
| 2288 | void ISel::visitShiftInst(ShiftInst &I) { |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 2289 | MachineBasicBlock::iterator IP = BB->end(); |
| 2290 | emitShiftOperation(BB, IP, I.getOperand(0), I.getOperand(1), |
| 2291 | I.getOpcode() == Instruction::Shl, I.getType(), |
| 2292 | getReg(I)); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2293 | } |
| 2294 | |
| 2295 | /// emitShiftOperation - Common code shared between visitShiftInst and |
| 2296 | /// constant expression support. |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 2297 | /// |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2298 | void ISel::emitShiftOperation(MachineBasicBlock *MBB, |
| 2299 | MachineBasicBlock::iterator IP, |
| 2300 | Value *Op, Value *ShiftAmount, bool isLeftShift, |
| 2301 | const Type *ResultTy, unsigned DestReg) { |
| 2302 | unsigned SrcReg = getReg (Op, MBB, IP); |
| 2303 | bool isSigned = ResultTy->isSigned (); |
| 2304 | unsigned Class = getClass (ResultTy); |
| 2305 | |
| 2306 | // Longs, as usual, are handled specially... |
| 2307 | if (Class == cLong) { |
| 2308 | // If we have a constant shift, we can generate much more efficient code |
| 2309 | // than otherwise... |
| 2310 | // |
| 2311 | if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(ShiftAmount)) { |
| 2312 | unsigned Amount = CUI->getValue(); |
| 2313 | if (Amount < 32) { |
| 2314 | if (isLeftShift) { |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 2315 | // FIXME: RLWIMI is a use-and-def of DestReg+1, but that violates SSA |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2316 | BuildMI(*MBB, IP, PPC::RLWINM, 4, DestReg).addReg(SrcReg) |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 2317 | .addImm(Amount).addImm(0).addImm(31-Amount); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2318 | BuildMI(*MBB, IP, PPC::RLWIMI, 5).addReg(DestReg).addReg(SrcReg+1) |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 2319 | .addImm(Amount).addImm(32-Amount).addImm(31); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2320 | BuildMI(*MBB, IP, PPC::RLWINM, 4, DestReg+1).addReg(SrcReg+1) |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 2321 | .addImm(Amount).addImm(0).addImm(31-Amount); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2322 | } else { |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 2323 | // FIXME: RLWIMI is a use-and-def of DestReg, but that violates SSA |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2324 | BuildMI(*MBB, IP, PPC::RLWINM, 4, DestReg+1).addReg(SrcReg+1) |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 2325 | .addImm(32-Amount).addImm(Amount).addImm(31); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2326 | BuildMI(*MBB, IP, PPC::RLWIMI, 5).addReg(DestReg+1).addReg(SrcReg) |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 2327 | .addImm(32-Amount).addImm(0).addImm(Amount-1); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2328 | BuildMI(*MBB, IP, PPC::RLWINM, 4, DestReg).addReg(SrcReg) |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 2329 | .addImm(32-Amount).addImm(Amount).addImm(31); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2330 | } |
| 2331 | } else { // Shifting more than 32 bits |
| 2332 | Amount -= 32; |
| 2333 | if (isLeftShift) { |
| 2334 | if (Amount != 0) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2335 | BuildMI(*MBB, IP, PPC::RLWINM, 4, DestReg).addReg(SrcReg+1) |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 2336 | .addImm(Amount).addImm(0).addImm(31-Amount); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2337 | } else { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2338 | BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(SrcReg+1) |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 2339 | .addReg(SrcReg+1); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2340 | } |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2341 | BuildMI(*MBB, IP, PPC::LI, 1, DestReg+1).addSImm(0); |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 2342 | } else { |
| 2343 | if (Amount != 0) { |
| 2344 | if (isSigned) |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2345 | BuildMI(*MBB, IP, PPC::SRAWI, 2, DestReg+1).addReg(SrcReg) |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 2346 | .addImm(Amount); |
| 2347 | else |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2348 | BuildMI(*MBB, IP, PPC::RLWINM, 4, DestReg+1).addReg(SrcReg) |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 2349 | .addImm(32-Amount).addImm(Amount).addImm(31); |
| 2350 | } else { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2351 | BuildMI(*MBB, IP, PPC::OR, 2, DestReg+1).addReg(SrcReg) |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 2352 | .addReg(SrcReg); |
| 2353 | } |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2354 | BuildMI(*MBB, IP,PPC::LI, 1, DestReg).addSImm(0); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2355 | } |
| 2356 | } |
| 2357 | } else { |
| 2358 | unsigned TmpReg1 = makeAnotherReg(Type::IntTy); |
| 2359 | unsigned TmpReg2 = makeAnotherReg(Type::IntTy); |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 2360 | unsigned TmpReg3 = makeAnotherReg(Type::IntTy); |
| 2361 | unsigned TmpReg4 = makeAnotherReg(Type::IntTy); |
| 2362 | unsigned TmpReg5 = makeAnotherReg(Type::IntTy); |
| 2363 | unsigned TmpReg6 = makeAnotherReg(Type::IntTy); |
| 2364 | unsigned ShiftAmountReg = getReg (ShiftAmount, MBB, IP); |
| 2365 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2366 | if (isLeftShift) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2367 | BuildMI(*MBB, IP, PPC::SUBFIC, 2, TmpReg1).addReg(ShiftAmountReg) |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 2368 | .addSImm(32); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2369 | BuildMI(*MBB, IP, PPC::SLW, 2, TmpReg2).addReg(SrcReg) |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 2370 | .addReg(ShiftAmountReg); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2371 | BuildMI(*MBB, IP, PPC::SRW, 2, TmpReg3).addReg(SrcReg+1) |
Misha Brukman | 2ed17ca | 2004-07-22 15:58:04 +0000 | [diff] [blame] | 2372 | .addReg(TmpReg1); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2373 | BuildMI(*MBB, IP, PPC::OR, 2,TmpReg4).addReg(TmpReg2).addReg(TmpReg3); |
| 2374 | BuildMI(*MBB, IP, PPC::ADDI, 2, TmpReg5).addReg(ShiftAmountReg) |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 2375 | .addSImm(-32); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2376 | BuildMI(*MBB, IP, PPC::SLW, 2, TmpReg6).addReg(SrcReg+1) |
Misha Brukman | 2ed17ca | 2004-07-22 15:58:04 +0000 | [diff] [blame] | 2377 | .addReg(TmpReg5); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2378 | BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(TmpReg4) |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 2379 | .addReg(TmpReg6); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2380 | BuildMI(*MBB, IP, PPC::SLW, 2, DestReg+1).addReg(SrcReg+1) |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 2381 | .addReg(ShiftAmountReg); |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 2382 | } else { |
| 2383 | if (isSigned) { |
Misha Brukman | 14d8c7a | 2004-06-29 23:45:05 +0000 | [diff] [blame] | 2384 | // FIXME: Unimplemented |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 2385 | // Page C-3 of the PowerPC 32bit Programming Environments Manual |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2386 | std::cerr << "ERROR: Unimplemented: signed right shift of long\n"; |
Misha Brukman | 14d8c7a | 2004-06-29 23:45:05 +0000 | [diff] [blame] | 2387 | abort(); |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 2388 | } else { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2389 | BuildMI(*MBB, IP, PPC::SUBFIC, 2, TmpReg1).addReg(ShiftAmountReg) |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 2390 | .addSImm(32); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2391 | BuildMI(*MBB, IP, PPC::SRW, 2, TmpReg2).addReg(SrcReg+1) |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 2392 | .addReg(ShiftAmountReg); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2393 | BuildMI(*MBB, IP, PPC::SLW, 2, TmpReg3).addReg(SrcReg) |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 2394 | .addReg(TmpReg1); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2395 | BuildMI(*MBB, IP, PPC::OR, 2, TmpReg4).addReg(TmpReg2) |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 2396 | .addReg(TmpReg3); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2397 | BuildMI(*MBB, IP, PPC::ADDI, 2, TmpReg5).addReg(ShiftAmountReg) |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 2398 | .addSImm(-32); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2399 | BuildMI(*MBB, IP, PPC::SRW, 2, TmpReg6).addReg(SrcReg) |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 2400 | .addReg(TmpReg5); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2401 | BuildMI(*MBB, IP, PPC::OR, 2, DestReg+1).addReg(TmpReg4) |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 2402 | .addReg(TmpReg6); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2403 | BuildMI(*MBB, IP, PPC::SRW, 2, DestReg).addReg(SrcReg) |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 2404 | .addReg(ShiftAmountReg); |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 2405 | } |
| 2406 | } |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2407 | } |
| 2408 | return; |
| 2409 | } |
| 2410 | |
| 2411 | if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(ShiftAmount)) { |
| 2412 | // The shift amount is constant, guaranteed to be a ubyte. Get its value. |
| 2413 | assert(CUI->getType() == Type::UByteTy && "Shift amount not a ubyte?"); |
| 2414 | unsigned Amount = CUI->getValue(); |
| 2415 | |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 2416 | if (isLeftShift) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2417 | BuildMI(*MBB, IP, PPC::RLWINM, 4, DestReg).addReg(SrcReg) |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 2418 | .addImm(Amount).addImm(0).addImm(31-Amount); |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 2419 | } else { |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 2420 | if (isSigned) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2421 | BuildMI(*MBB, IP, PPC::SRAWI,2,DestReg).addReg(SrcReg).addImm(Amount); |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 2422 | } else { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2423 | BuildMI(*MBB, IP, PPC::RLWINM, 4, DestReg).addReg(SrcReg) |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 2424 | .addImm(32-Amount).addImm(Amount).addImm(31); |
| 2425 | } |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 2426 | } |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2427 | } else { // The shift amount is non-constant. |
| 2428 | unsigned ShiftAmountReg = getReg (ShiftAmount, MBB, IP); |
| 2429 | |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 2430 | if (isLeftShift) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2431 | BuildMI(*MBB, IP, PPC::SLW, 2, DestReg).addReg(SrcReg) |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 2432 | .addReg(ShiftAmountReg); |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 2433 | } else { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2434 | BuildMI(*MBB, IP, isSigned ? PPC::SRAW : PPC::SRW, 2, DestReg) |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 2435 | .addReg(SrcReg).addReg(ShiftAmountReg); |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 2436 | } |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2437 | } |
| 2438 | } |
| 2439 | |
| 2440 | |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2441 | /// visitLoadInst - Implement LLVM load instructions. Pretty straightforward |
| 2442 | /// mapping of LLVM classes to PPC load instructions, with the exception of |
| 2443 | /// signed byte loads, which need a sign extension following them. |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2444 | /// |
| 2445 | void ISel::visitLoadInst(LoadInst &I) { |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2446 | // Immediate opcodes, for reg+imm addressing |
| 2447 | static const unsigned ImmOpcodes[] = { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2448 | PPC::LBZ, PPC::LHZ, PPC::LWZ, |
| 2449 | PPC::LFS, PPC::LFD, PPC::LWZ |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2450 | }; |
| 2451 | // Indexed opcodes, for reg+reg addressing |
| 2452 | static const unsigned IdxOpcodes[] = { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2453 | PPC::LBZX, PPC::LHZX, PPC::LWZX, |
| 2454 | PPC::LFSX, PPC::LFDX, PPC::LWZX |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 2455 | }; |
Misha Brukman | 2ed17ca | 2004-07-22 15:58:04 +0000 | [diff] [blame] | 2456 | |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2457 | unsigned Class = getClassB(I.getType()); |
| 2458 | unsigned ImmOpcode = ImmOpcodes[Class]; |
| 2459 | unsigned IdxOpcode = IdxOpcodes[Class]; |
| 2460 | unsigned DestReg = getReg(I); |
| 2461 | Value *SourceAddr = I.getOperand(0); |
| 2462 | |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2463 | if (Class == cShort && I.getType()->isSigned()) ImmOpcode = PPC::LHA; |
| 2464 | if (Class == cShort && I.getType()->isSigned()) IdxOpcode = PPC::LHAX; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2465 | |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2466 | if (AllocaInst *AI = dyn_castFixedAlloca(SourceAddr)) { |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 2467 | unsigned FI = getFixedSizedAllocaFI(AI); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2468 | if (Class == cLong) { |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2469 | addFrameReference(BuildMI(BB, ImmOpcode, 2, DestReg), FI); |
| 2470 | addFrameReference(BuildMI(BB, ImmOpcode, 2, DestReg+1), FI, 4); |
Misha Brukman | 2ed17ca | 2004-07-22 15:58:04 +0000 | [diff] [blame] | 2471 | } else if (Class == cByte && I.getType()->isSigned()) { |
| 2472 | unsigned TmpReg = makeAnotherReg(I.getType()); |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2473 | addFrameReference(BuildMI(BB, ImmOpcode, 2, TmpReg), FI); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2474 | BuildMI(BB, PPC::EXTSB, 1, DestReg).addReg(TmpReg); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2475 | } else { |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2476 | addFrameReference(BuildMI(BB, ImmOpcode, 2, DestReg), FI); |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 2477 | } |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2478 | return; |
| 2479 | } |
| 2480 | |
| 2481 | // If this load is the only use of the GEP instruction that is its address, |
| 2482 | // then we can fold the GEP directly into the load instruction. |
| 2483 | // emitGEPOperation with a second to last arg of 'true' will place the |
| 2484 | // base register for the GEP into baseReg, and the constant offset from that |
| 2485 | // into offset. If the offset fits in 16 bits, we can emit a reg+imm store |
| 2486 | // otherwise, we copy the offset into another reg, and use reg+reg addressing. |
| 2487 | if (GetElementPtrInst *GEPI = canFoldGEPIntoLoadOrStore(SourceAddr)) { |
| 2488 | unsigned baseReg = getReg(GEPI); |
Nate Begeman | b64af91 | 2004-08-10 20:42:36 +0000 | [diff] [blame] | 2489 | unsigned pendingAdd; |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2490 | ConstantSInt *offset; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2491 | |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2492 | emitGEPOperation(BB, BB->end(), GEPI->getOperand(0), GEPI->op_begin()+1, |
Nate Begeman | b64af91 | 2004-08-10 20:42:36 +0000 | [diff] [blame] | 2493 | GEPI->op_end(), baseReg, true, &offset, &pendingAdd); |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2494 | |
Nate Begeman | b64af91 | 2004-08-10 20:42:36 +0000 | [diff] [blame] | 2495 | if (pendingAdd == 0 && Class != cLong && |
| 2496 | canUseAsImmediateForOpcode(offset, 0)) { |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2497 | if (Class == cByte && I.getType()->isSigned()) { |
| 2498 | unsigned TmpReg = makeAnotherReg(I.getType()); |
| 2499 | BuildMI(BB, ImmOpcode, 2, TmpReg).addSImm(offset->getValue()) |
| 2500 | .addReg(baseReg); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2501 | BuildMI(BB, PPC::EXTSB, 1, DestReg).addReg(TmpReg); |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2502 | } else { |
| 2503 | BuildMI(BB, ImmOpcode, 2, DestReg).addSImm(offset->getValue()) |
| 2504 | .addReg(baseReg); |
| 2505 | } |
| 2506 | return; |
| 2507 | } |
| 2508 | |
Nate Begeman | b64af91 | 2004-08-10 20:42:36 +0000 | [diff] [blame] | 2509 | unsigned indexReg = (pendingAdd != 0) ? pendingAdd : getReg(offset); |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2510 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2511 | if (Class == cLong) { |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2512 | unsigned indexPlus4 = makeAnotherReg(Type::IntTy); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2513 | BuildMI(BB, PPC::ADDI, 2, indexPlus4).addReg(indexReg).addSImm(4); |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2514 | BuildMI(BB, IdxOpcode, 2, DestReg).addReg(indexReg).addReg(baseReg); |
| 2515 | BuildMI(BB, IdxOpcode, 2, DestReg+1).addReg(indexPlus4).addReg(baseReg); |
Misha Brukman | 2ed17ca | 2004-07-22 15:58:04 +0000 | [diff] [blame] | 2516 | } else if (Class == cByte && I.getType()->isSigned()) { |
| 2517 | unsigned TmpReg = makeAnotherReg(I.getType()); |
Nate Begeman | b64af91 | 2004-08-10 20:42:36 +0000 | [diff] [blame] | 2518 | BuildMI(BB, IdxOpcode, 2, TmpReg).addReg(indexReg).addReg(baseReg); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2519 | BuildMI(BB, PPC::EXTSB, 1, DestReg).addReg(TmpReg); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2520 | } else { |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2521 | BuildMI(BB, IdxOpcode, 2, DestReg).addReg(indexReg).addReg(baseReg); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2522 | } |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2523 | return; |
| 2524 | } |
| 2525 | |
| 2526 | // The fallback case, where the load was from a source that could not be |
| 2527 | // folded into the load instruction. |
| 2528 | unsigned SrcAddrReg = getReg(SourceAddr); |
| 2529 | |
| 2530 | if (Class == cLong) { |
| 2531 | BuildMI(BB, ImmOpcode, 2, DestReg).addSImm(0).addReg(SrcAddrReg); |
| 2532 | BuildMI(BB, ImmOpcode, 2, DestReg+1).addSImm(4).addReg(SrcAddrReg); |
| 2533 | } else if (Class == cByte && I.getType()->isSigned()) { |
| 2534 | unsigned TmpReg = makeAnotherReg(I.getType()); |
| 2535 | BuildMI(BB, ImmOpcode, 2, TmpReg).addSImm(0).addReg(SrcAddrReg); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2536 | BuildMI(BB, PPC::EXTSB, 1, DestReg).addReg(TmpReg); |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2537 | } else { |
| 2538 | BuildMI(BB, ImmOpcode, 2, DestReg).addSImm(0).addReg(SrcAddrReg); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2539 | } |
| 2540 | } |
| 2541 | |
| 2542 | /// visitStoreInst - Implement LLVM store instructions |
| 2543 | /// |
| 2544 | void ISel::visitStoreInst(StoreInst &I) { |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2545 | // Immediate opcodes, for reg+imm addressing |
| 2546 | static const unsigned ImmOpcodes[] = { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2547 | PPC::STB, PPC::STH, PPC::STW, |
| 2548 | PPC::STFS, PPC::STFD, PPC::STW |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2549 | }; |
| 2550 | // Indexed opcodes, for reg+reg addressing |
| 2551 | static const unsigned IdxOpcodes[] = { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2552 | PPC::STBX, PPC::STHX, PPC::STWX, |
| 2553 | PPC::STFSX, PPC::STFDX, PPC::STWX |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2554 | }; |
| 2555 | |
| 2556 | Value *SourceAddr = I.getOperand(1); |
| 2557 | const Type *ValTy = I.getOperand(0)->getType(); |
| 2558 | unsigned Class = getClassB(ValTy); |
| 2559 | unsigned ImmOpcode = ImmOpcodes[Class]; |
| 2560 | unsigned IdxOpcode = IdxOpcodes[Class]; |
| 2561 | unsigned ValReg = getReg(I.getOperand(0)); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2562 | |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2563 | // If this store is the only use of the GEP instruction that is its address, |
| 2564 | // then we can fold the GEP directly into the store instruction. |
| 2565 | // emitGEPOperation with a second to last arg of 'true' will place the |
| 2566 | // base register for the GEP into baseReg, and the constant offset from that |
| 2567 | // into offset. If the offset fits in 16 bits, we can emit a reg+imm store |
| 2568 | // otherwise, we copy the offset into another reg, and use reg+reg addressing. |
| 2569 | if (GetElementPtrInst *GEPI = canFoldGEPIntoLoadOrStore(SourceAddr)) { |
| 2570 | unsigned baseReg = getReg(GEPI); |
Nate Begeman | b64af91 | 2004-08-10 20:42:36 +0000 | [diff] [blame] | 2571 | unsigned pendingAdd; |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2572 | ConstantSInt *offset; |
| 2573 | |
| 2574 | emitGEPOperation(BB, BB->end(), GEPI->getOperand(0), GEPI->op_begin()+1, |
Nate Begeman | b64af91 | 2004-08-10 20:42:36 +0000 | [diff] [blame] | 2575 | GEPI->op_end(), baseReg, true, &offset, &pendingAdd); |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2576 | |
Nate Begeman | b64af91 | 2004-08-10 20:42:36 +0000 | [diff] [blame] | 2577 | if (0 == pendingAdd && Class != cLong && |
| 2578 | canUseAsImmediateForOpcode(offset, 0)) { |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2579 | BuildMI(BB, ImmOpcode, 3).addReg(ValReg).addSImm(offset->getValue()) |
| 2580 | .addReg(baseReg); |
| 2581 | return; |
| 2582 | } |
| 2583 | |
Nate Begeman | b64af91 | 2004-08-10 20:42:36 +0000 | [diff] [blame] | 2584 | unsigned indexReg = (pendingAdd != 0) ? pendingAdd : getReg(offset); |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2585 | |
| 2586 | if (Class == cLong) { |
| 2587 | unsigned indexPlus4 = makeAnotherReg(Type::IntTy); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2588 | BuildMI(BB, PPC::ADDI, 2, indexPlus4).addReg(indexReg).addSImm(4); |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2589 | BuildMI(BB, IdxOpcode, 3).addReg(ValReg).addReg(indexReg).addReg(baseReg); |
| 2590 | BuildMI(BB, IdxOpcode, 3).addReg(ValReg+1).addReg(indexPlus4) |
| 2591 | .addReg(baseReg); |
| 2592 | return; |
| 2593 | } |
| 2594 | BuildMI(BB, IdxOpcode, 3).addReg(ValReg).addReg(indexReg).addReg(baseReg); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2595 | return; |
| 2596 | } |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2597 | |
| 2598 | // If the store address wasn't the only use of a GEP, we fall back to the |
| 2599 | // standard path: store the ValReg at the value in AddressReg. |
| 2600 | unsigned AddressReg = getReg(I.getOperand(1)); |
| 2601 | if (Class == cLong) { |
| 2602 | BuildMI(BB, ImmOpcode, 3).addReg(ValReg).addSImm(0).addReg(AddressReg); |
| 2603 | BuildMI(BB, ImmOpcode, 3).addReg(ValReg+1).addSImm(4).addReg(AddressReg); |
| 2604 | return; |
| 2605 | } |
| 2606 | BuildMI(BB, ImmOpcode, 3).addReg(ValReg).addSImm(0).addReg(AddressReg); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2607 | } |
| 2608 | |
| 2609 | |
| 2610 | /// visitCastInst - Here we have various kinds of copying with or without sign |
| 2611 | /// extension going on. |
| 2612 | /// |
| 2613 | void ISel::visitCastInst(CastInst &CI) { |
| 2614 | Value *Op = CI.getOperand(0); |
| 2615 | |
| 2616 | unsigned SrcClass = getClassB(Op->getType()); |
| 2617 | unsigned DestClass = getClassB(CI.getType()); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2618 | |
| 2619 | // If this is a cast from a 32-bit integer to a Long type, and the only uses |
| 2620 | // of the case are GEP instructions, then the cast does not need to be |
| 2621 | // generated explicitly, it will be folded into the GEP. |
| 2622 | if (DestClass == cLong && SrcClass == cInt) { |
| 2623 | bool AllUsesAreGEPs = true; |
| 2624 | for (Value::use_iterator I = CI.use_begin(), E = CI.use_end(); I != E; ++I) |
| 2625 | if (!isa<GetElementPtrInst>(*I)) { |
| 2626 | AllUsesAreGEPs = false; |
| 2627 | break; |
| 2628 | } |
| 2629 | |
| 2630 | // No need to codegen this cast if all users are getelementptr instrs... |
| 2631 | if (AllUsesAreGEPs) return; |
| 2632 | } |
| 2633 | |
| 2634 | unsigned DestReg = getReg(CI); |
| 2635 | MachineBasicBlock::iterator MI = BB->end(); |
| 2636 | emitCastOperation(BB, MI, Op, CI.getType(), DestReg); |
| 2637 | } |
| 2638 | |
| 2639 | /// emitCastOperation - Common code shared between visitCastInst and constant |
| 2640 | /// expression cast support. |
| 2641 | /// |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 2642 | void ISel::emitCastOperation(MachineBasicBlock *MBB, |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2643 | MachineBasicBlock::iterator IP, |
| 2644 | Value *Src, const Type *DestTy, |
| 2645 | unsigned DestReg) { |
| 2646 | const Type *SrcTy = Src->getType(); |
| 2647 | unsigned SrcClass = getClassB(SrcTy); |
| 2648 | unsigned DestClass = getClassB(DestTy); |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 2649 | unsigned SrcReg = getReg(Src, MBB, IP); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2650 | |
| 2651 | // Implement casts to bool by using compare on the operand followed by set if |
| 2652 | // not zero on the result. |
| 2653 | if (DestTy == Type::BoolTy) { |
| 2654 | switch (SrcClass) { |
| 2655 | case cByte: |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 2656 | case cShort: |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2657 | case cInt: { |
| 2658 | unsigned TmpReg = makeAnotherReg(Type::IntTy); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2659 | BuildMI(*MBB, IP, PPC::ADDIC, 2, TmpReg).addReg(SrcReg).addSImm(-1); |
| 2660 | BuildMI(*MBB, IP, PPC::SUBFE, 2, DestReg).addReg(TmpReg).addReg(SrcReg); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2661 | break; |
| 2662 | } |
| 2663 | case cLong: { |
| 2664 | unsigned TmpReg = makeAnotherReg(Type::IntTy); |
| 2665 | unsigned SrcReg2 = makeAnotherReg(Type::IntTy); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2666 | BuildMI(*MBB, IP, PPC::OR, 2, SrcReg2).addReg(SrcReg).addReg(SrcReg+1); |
| 2667 | BuildMI(*MBB, IP, PPC::ADDIC, 2, TmpReg).addReg(SrcReg2).addSImm(-1); |
| 2668 | BuildMI(*MBB, IP, PPC::SUBFE, 2, DestReg).addReg(TmpReg) |
Misha Brukman | bf417a6 | 2004-07-20 20:43:05 +0000 | [diff] [blame] | 2669 | .addReg(SrcReg2); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2670 | break; |
| 2671 | } |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 2672 | case cFP32: |
| 2673 | case cFP64: |
| 2674 | // FSEL perhaps? |
Misha Brukman | 2ed17ca | 2004-07-22 15:58:04 +0000 | [diff] [blame] | 2675 | std::cerr << "ERROR: Cast fp-to-bool not implemented!\n"; |
Misha Brukman | d18a31d | 2004-07-06 22:51:53 +0000 | [diff] [blame] | 2676 | abort(); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2677 | } |
| 2678 | return; |
| 2679 | } |
| 2680 | |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 2681 | // Handle cast of Float -> Double |
| 2682 | if (SrcClass == cFP32 && DestClass == cFP64) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2683 | BuildMI(*MBB, IP, PPC::FMR, 1, DestReg).addReg(SrcReg); |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 2684 | return; |
| 2685 | } |
| 2686 | |
| 2687 | // Handle cast of Double -> Float |
| 2688 | if (SrcClass == cFP64 && DestClass == cFP32) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2689 | BuildMI(*MBB, IP, PPC::FRSP, 1, DestReg).addReg(SrcReg); |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 2690 | return; |
| 2691 | } |
| 2692 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2693 | // Handle casts from integer to floating point now... |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 2694 | if (DestClass == cFP32 || DestClass == cFP64) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2695 | |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 2696 | // Emit a library call for long to float conversion |
| 2697 | if (SrcClass == cLong) { |
| 2698 | std::vector<ValueRecord> Args; |
| 2699 | Args.push_back(ValueRecord(SrcReg, SrcTy)); |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 2700 | Function *floatFn = (DestClass == cFP32) ? __floatdisfFn : __floatdidfFn; |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 2701 | MachineInstr *TheCall = |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2702 | BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(floatFn, true); |
Misha Brukman | d18a31d | 2004-07-06 22:51:53 +0000 | [diff] [blame] | 2703 | doCall(ValueRecord(DestReg, DestTy), TheCall, Args, false); |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 2704 | TM.CalledFunctions.insert(floatFn); |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 2705 | return; |
| 2706 | } |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2707 | |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 2708 | // Make sure we're dealing with a full 32 bits |
| 2709 | unsigned TmpReg = makeAnotherReg(Type::IntTy); |
| 2710 | promote32(TmpReg, ValueRecord(SrcReg, SrcTy)); |
| 2711 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2712 | SrcReg = TmpReg; |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 2713 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2714 | // Spill the integer to memory and reload it from there. |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 2715 | // Also spill room for a special conversion constant |
| 2716 | int ConstantFrameIndex = |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2717 | F->getFrameInfo()->CreateStackObject(Type::DoubleTy, TM.getTargetData()); |
| 2718 | int ValueFrameIdx = |
| 2719 | F->getFrameInfo()->CreateStackObject(Type::DoubleTy, TM.getTargetData()); |
| 2720 | |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 2721 | unsigned constantHi = makeAnotherReg(Type::IntTy); |
| 2722 | unsigned constantLo = makeAnotherReg(Type::IntTy); |
| 2723 | unsigned ConstF = makeAnotherReg(Type::DoubleTy); |
| 2724 | unsigned TempF = makeAnotherReg(Type::DoubleTy); |
| 2725 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2726 | if (!SrcTy->isSigned()) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2727 | BuildMI(*BB, IP, PPC::LIS, 1, constantHi).addSImm(0x4330); |
| 2728 | BuildMI(*BB, IP, PPC::LI, 1, constantLo).addSImm(0); |
| 2729 | addFrameReference(BuildMI(*BB, IP, PPC::STW, 3).addReg(constantHi), |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 2730 | ConstantFrameIndex); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2731 | addFrameReference(BuildMI(*BB, IP, PPC::STW, 3).addReg(constantLo), |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 2732 | ConstantFrameIndex, 4); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2733 | addFrameReference(BuildMI(*BB, IP, PPC::STW, 3).addReg(constantHi), |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 2734 | ValueFrameIdx); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2735 | addFrameReference(BuildMI(*BB, IP, PPC::STW, 3).addReg(SrcReg), |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 2736 | ValueFrameIdx, 4); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2737 | addFrameReference(BuildMI(*BB, IP, PPC::LFD, 2, ConstF), |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 2738 | ConstantFrameIndex); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2739 | addFrameReference(BuildMI(*BB, IP, PPC::LFD, 2, TempF), ValueFrameIdx); |
| 2740 | BuildMI(*BB, IP, PPC::FSUB, 2, DestReg).addReg(TempF).addReg(ConstF); |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 2741 | } else { |
| 2742 | unsigned TempLo = makeAnotherReg(Type::IntTy); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2743 | BuildMI(*BB, IP, PPC::LIS, 1, constantHi).addSImm(0x4330); |
| 2744 | BuildMI(*BB, IP, PPC::LIS, 1, constantLo).addSImm(0x8000); |
| 2745 | addFrameReference(BuildMI(*BB, IP, PPC::STW, 3).addReg(constantHi), |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 2746 | ConstantFrameIndex); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2747 | addFrameReference(BuildMI(*BB, IP, PPC::STW, 3).addReg(constantLo), |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 2748 | ConstantFrameIndex, 4); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2749 | addFrameReference(BuildMI(*BB, IP, PPC::STW, 3).addReg(constantHi), |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 2750 | ValueFrameIdx); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2751 | BuildMI(*BB, IP, PPC::XORIS, 2, TempLo).addReg(SrcReg).addImm(0x8000); |
| 2752 | addFrameReference(BuildMI(*BB, IP, PPC::STW, 3).addReg(TempLo), |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 2753 | ValueFrameIdx, 4); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2754 | addFrameReference(BuildMI(*BB, IP, PPC::LFD, 2, ConstF), |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 2755 | ConstantFrameIndex); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2756 | addFrameReference(BuildMI(*BB, IP, PPC::LFD, 2, TempF), ValueFrameIdx); |
| 2757 | BuildMI(*BB, IP, PPC::FSUB, 2, DestReg).addReg(TempF).addReg(ConstF); |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 2758 | } |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2759 | return; |
| 2760 | } |
| 2761 | |
| 2762 | // Handle casts from floating point to integer now... |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 2763 | if (SrcClass == cFP32 || SrcClass == cFP64) { |
Nate Begeman | b64af91 | 2004-08-10 20:42:36 +0000 | [diff] [blame] | 2764 | static Function* const Funcs[] = |
| 2765 | { __fixsfdiFn, __fixdfdiFn, __fixunssfdiFn, __fixunsdfdiFn }; |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 2766 | // emit library call |
| 2767 | if (DestClass == cLong) { |
Nate Begeman | b64af91 | 2004-08-10 20:42:36 +0000 | [diff] [blame] | 2768 | bool isDouble = SrcClass == cFP64; |
| 2769 | unsigned nameIndex = 2 * DestTy->isSigned() + isDouble; |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 2770 | std::vector<ValueRecord> Args; |
| 2771 | Args.push_back(ValueRecord(SrcReg, SrcTy)); |
Nate Begeman | b64af91 | 2004-08-10 20:42:36 +0000 | [diff] [blame] | 2772 | Function *floatFn = Funcs[nameIndex]; |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 2773 | MachineInstr *TheCall = |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2774 | BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(floatFn, true); |
Misha Brukman | d18a31d | 2004-07-06 22:51:53 +0000 | [diff] [blame] | 2775 | doCall(ValueRecord(DestReg, DestTy), TheCall, Args, false); |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 2776 | TM.CalledFunctions.insert(floatFn); |
Misha Brukman | 422791f | 2004-06-21 17:41:12 +0000 | [diff] [blame] | 2777 | return; |
| 2778 | } |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2779 | |
| 2780 | int ValueFrameIdx = |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 2781 | F->getFrameInfo()->CreateStackObject(SrcTy, TM.getTargetData()); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2782 | |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 2783 | if (DestTy->isSigned()) { |
Misha Brukman | 4c14f33 | 2004-07-23 01:11:19 +0000 | [diff] [blame] | 2784 | unsigned TempReg = makeAnotherReg(Type::DoubleTy); |
| 2785 | |
| 2786 | // Convert to integer in the FP reg and store it to a stack slot |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2787 | BuildMI(*BB, IP, PPC::FCTIWZ, 1, TempReg).addReg(SrcReg); |
| 2788 | addFrameReference(BuildMI(*BB, IP, PPC::STFD, 3) |
Misha Brukman | 4c14f33 | 2004-07-23 01:11:19 +0000 | [diff] [blame] | 2789 | .addReg(TempReg), ValueFrameIdx); |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2790 | |
| 2791 | // There is no load signed byte opcode, so we must emit a sign extend for |
| 2792 | // that particular size. Make sure to source the new integer from the |
| 2793 | // correct offset. |
Misha Brukman | 4c14f33 | 2004-07-23 01:11:19 +0000 | [diff] [blame] | 2794 | if (DestClass == cByte) { |
| 2795 | unsigned TempReg2 = makeAnotherReg(DestTy); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2796 | addFrameReference(BuildMI(*BB, IP, PPC::LBZ, 2, TempReg2), |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2797 | ValueFrameIdx, 7); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2798 | BuildMI(*MBB, IP, PPC::EXTSB, DestReg).addReg(TempReg2); |
Misha Brukman | 4c14f33 | 2004-07-23 01:11:19 +0000 | [diff] [blame] | 2799 | } else { |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2800 | int offset = (DestClass == cShort) ? 6 : 4; |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2801 | unsigned LoadOp = (DestClass == cShort) ? PPC::LHA : PPC::LWZ; |
Misha Brukman | 4c14f33 | 2004-07-23 01:11:19 +0000 | [diff] [blame] | 2802 | addFrameReference(BuildMI(*BB, IP, LoadOp, 2, DestReg), |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2803 | ValueFrameIdx, offset); |
Misha Brukman | 4c14f33 | 2004-07-23 01:11:19 +0000 | [diff] [blame] | 2804 | } |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 2805 | } else { |
Misha Brukman | b160d1f | 2004-07-23 20:32:59 +0000 | [diff] [blame] | 2806 | unsigned Zero = getReg(ConstantFP::get(Type::DoubleTy, 0.0f)); |
| 2807 | double maxInt = (1LL << 32) - 1; |
| 2808 | unsigned MaxInt = getReg(ConstantFP::get(Type::DoubleTy, maxInt)); |
| 2809 | double border = 1LL << 31; |
| 2810 | unsigned Border = getReg(ConstantFP::get(Type::DoubleTy, border)); |
| 2811 | unsigned UseZero = makeAnotherReg(Type::DoubleTy); |
| 2812 | unsigned UseMaxInt = makeAnotherReg(Type::DoubleTy); |
| 2813 | unsigned UseChoice = makeAnotherReg(Type::DoubleTy); |
| 2814 | unsigned TmpReg = makeAnotherReg(Type::DoubleTy); |
| 2815 | unsigned TmpReg2 = makeAnotherReg(Type::DoubleTy); |
| 2816 | unsigned ConvReg = makeAnotherReg(Type::DoubleTy); |
| 2817 | unsigned IntTmp = makeAnotherReg(Type::IntTy); |
| 2818 | unsigned XorReg = makeAnotherReg(Type::IntTy); |
| 2819 | int FrameIdx = |
| 2820 | F->getFrameInfo()->CreateStackObject(SrcTy, TM.getTargetData()); |
| 2821 | // Update machine-CFG edges |
| 2822 | MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock()); |
| 2823 | MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock()); |
| 2824 | MachineBasicBlock *OldMBB = BB; |
| 2825 | ilist<MachineBasicBlock>::iterator It = BB; ++It; |
| 2826 | F->getBasicBlockList().insert(It, XorMBB); |
| 2827 | F->getBasicBlockList().insert(It, PhiMBB); |
| 2828 | BB->addSuccessor(XorMBB); |
| 2829 | BB->addSuccessor(PhiMBB); |
| 2830 | |
| 2831 | // Convert from floating point to unsigned 32-bit value |
| 2832 | // Use 0 if incoming value is < 0.0 |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2833 | BuildMI(*BB, IP, PPC::FSEL, 3, UseZero).addReg(SrcReg).addReg(SrcReg) |
Misha Brukman | b160d1f | 2004-07-23 20:32:59 +0000 | [diff] [blame] | 2834 | .addReg(Zero); |
| 2835 | // Use 2**32 - 1 if incoming value is >= 2**32 |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2836 | BuildMI(*BB, IP, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(SrcReg); |
| 2837 | BuildMI(*BB, IP, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt) |
Misha Brukman | b160d1f | 2004-07-23 20:32:59 +0000 | [diff] [blame] | 2838 | .addReg(UseZero).addReg(MaxInt); |
| 2839 | // Subtract 2**31 |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2840 | BuildMI(*BB, IP, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border); |
Misha Brukman | b160d1f | 2004-07-23 20:32:59 +0000 | [diff] [blame] | 2841 | // Use difference if >= 2**31 |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2842 | BuildMI(*BB, IP, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice) |
Misha Brukman | b160d1f | 2004-07-23 20:32:59 +0000 | [diff] [blame] | 2843 | .addReg(Border); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2844 | BuildMI(*BB, IP, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg) |
Misha Brukman | b160d1f | 2004-07-23 20:32:59 +0000 | [diff] [blame] | 2845 | .addReg(UseChoice); |
| 2846 | // Convert to integer |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2847 | BuildMI(*BB, IP, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2); |
| 2848 | addFrameReference(BuildMI(*BB, IP, PPC::STFD, 3).addReg(ConvReg), |
Misha Brukman | b160d1f | 2004-07-23 20:32:59 +0000 | [diff] [blame] | 2849 | FrameIdx); |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2850 | if (DestClass == cByte) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2851 | addFrameReference(BuildMI(*BB, IP, PPC::LBZ, 2, DestReg), |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2852 | FrameIdx, 7); |
| 2853 | } else if (DestClass == cShort) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2854 | addFrameReference(BuildMI(*BB, IP, PPC::LHZ, 2, DestReg), |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2855 | FrameIdx, 6); |
| 2856 | } if (DestClass == cInt) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2857 | addFrameReference(BuildMI(*BB, IP, PPC::LWZ, 2, IntTmp), |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2858 | FrameIdx, 4); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2859 | BuildMI(*BB, IP, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB); |
| 2860 | BuildMI(*BB, IP, PPC::B, 1).addMBB(XorMBB); |
Misha Brukman | b160d1f | 2004-07-23 20:32:59 +0000 | [diff] [blame] | 2861 | |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2862 | // XorMBB: |
| 2863 | // add 2**31 if input was >= 2**31 |
| 2864 | BB = XorMBB; |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2865 | BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000); |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2866 | XorMBB->addSuccessor(PhiMBB); |
Misha Brukman | b160d1f | 2004-07-23 20:32:59 +0000 | [diff] [blame] | 2867 | |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2868 | // PhiMBB: |
| 2869 | // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ] |
| 2870 | BB = PhiMBB; |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2871 | BuildMI(BB, PPC::PHI, 2, DestReg).addReg(IntTmp).addMBB(OldMBB) |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2872 | .addReg(XorReg).addMBB(XorMBB); |
| 2873 | } |
| 2874 | } |
| 2875 | return; |
| 2876 | } |
| 2877 | |
| 2878 | // Check our invariants |
| 2879 | assert((SrcClass <= cInt || SrcClass == cLong) && |
| 2880 | "Unhandled source class for cast operation!"); |
| 2881 | assert((DestClass <= cInt || DestClass == cLong) && |
| 2882 | "Unhandled destination class for cast operation!"); |
| 2883 | |
| 2884 | bool sourceUnsigned = SrcTy->isUnsigned() || SrcTy == Type::BoolTy; |
| 2885 | bool destUnsigned = DestTy->isUnsigned(); |
| 2886 | |
| 2887 | // Unsigned -> Unsigned, clear if larger, |
| 2888 | if (sourceUnsigned && destUnsigned) { |
| 2889 | // handle long dest class now to keep switch clean |
| 2890 | if (DestClass == cLong) { |
| 2891 | if (SrcClass == cLong) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2892 | BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg); |
| 2893 | BuildMI(*MBB, IP, PPC::OR, 2, DestReg+1).addReg(SrcReg+1) |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2894 | .addReg(SrcReg+1); |
| 2895 | } else { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2896 | BuildMI(*MBB, IP, PPC::LI, 1, DestReg).addSImm(0); |
| 2897 | BuildMI(*MBB, IP, PPC::OR, 2, DestReg+1).addReg(SrcReg) |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2898 | .addReg(SrcReg); |
| 2899 | } |
| 2900 | return; |
| 2901 | } |
| 2902 | |
| 2903 | // handle u{ byte, short, int } x u{ byte, short, int } |
| 2904 | unsigned clearBits = (SrcClass == cByte || DestClass == cByte) ? 24 : 16; |
| 2905 | switch (SrcClass) { |
| 2906 | case cByte: |
| 2907 | case cShort: |
| 2908 | if (SrcClass == DestClass) |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2909 | BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg); |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2910 | else |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2911 | BuildMI(*MBB, IP, PPC::RLWINM, 4, DestReg).addReg(SrcReg) |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2912 | .addImm(0).addImm(clearBits).addImm(31); |
| 2913 | break; |
| 2914 | case cLong: |
| 2915 | ++SrcReg; |
| 2916 | // Fall through |
| 2917 | case cInt: |
| 2918 | if (DestClass == cInt) |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2919 | BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg); |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2920 | else |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2921 | BuildMI(*MBB, IP, PPC::RLWINM, 4, DestReg).addReg(SrcReg) |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2922 | .addImm(0).addImm(clearBits).addImm(31); |
| 2923 | break; |
| 2924 | } |
| 2925 | return; |
| 2926 | } |
| 2927 | |
| 2928 | // Signed -> Signed |
| 2929 | if (!sourceUnsigned && !destUnsigned) { |
| 2930 | // handle long dest class now to keep switch clean |
| 2931 | if (DestClass == cLong) { |
| 2932 | if (SrcClass == cLong) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2933 | BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg); |
| 2934 | BuildMI(*MBB, IP, PPC::OR, 2, DestReg+1).addReg(SrcReg+1) |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2935 | .addReg(SrcReg+1); |
| 2936 | } else { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2937 | BuildMI(*MBB, IP, PPC::SRAWI, 2, DestReg).addReg(SrcReg).addImm(31); |
| 2938 | BuildMI(*MBB, IP, PPC::OR, 2, DestReg+1).addReg(SrcReg) |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2939 | .addReg(SrcReg); |
| 2940 | } |
| 2941 | return; |
| 2942 | } |
| 2943 | |
| 2944 | // handle { byte, short, int } x { byte, short, int } |
| 2945 | switch (SrcClass) { |
| 2946 | case cByte: |
| 2947 | if (DestClass == cByte) |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2948 | BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg); |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2949 | else |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2950 | BuildMI(*MBB, IP, PPC::EXTSB, 1, DestReg).addReg(SrcReg); |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2951 | break; |
| 2952 | case cShort: |
| 2953 | if (DestClass == cByte) |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2954 | BuildMI(*MBB, IP, PPC::EXTSB, 1, DestReg).addReg(SrcReg); |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2955 | else if (DestClass == cShort) |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2956 | BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg); |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2957 | else |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2958 | BuildMI(*MBB, IP, PPC::EXTSH, 1, DestReg).addReg(SrcReg); |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2959 | break; |
| 2960 | case cLong: |
| 2961 | ++SrcReg; |
| 2962 | // Fall through |
| 2963 | case cInt: |
| 2964 | if (DestClass == cByte) |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2965 | BuildMI(*MBB, IP, PPC::EXTSB, 1, DestReg).addReg(SrcReg); |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2966 | else if (DestClass == cShort) |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2967 | BuildMI(*MBB, IP, PPC::EXTSH, 1, DestReg).addReg(SrcReg); |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2968 | else |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2969 | BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg); |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2970 | break; |
| 2971 | } |
| 2972 | return; |
| 2973 | } |
| 2974 | |
| 2975 | // Unsigned -> Signed |
| 2976 | if (sourceUnsigned && !destUnsigned) { |
| 2977 | // handle long dest class now to keep switch clean |
| 2978 | if (DestClass == cLong) { |
| 2979 | if (SrcClass == cLong) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2980 | BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg); |
| 2981 | BuildMI(*MBB, IP, PPC::OR, 2, DestReg+1).addReg(SrcReg+1). |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2982 | addReg(SrcReg+1); |
| 2983 | } else { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2984 | BuildMI(*MBB, IP, PPC::LI, 1, DestReg).addSImm(0); |
| 2985 | BuildMI(*MBB, IP, PPC::OR, 2, DestReg+1).addReg(SrcReg) |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2986 | .addReg(SrcReg); |
| 2987 | } |
| 2988 | return; |
| 2989 | } |
| 2990 | |
| 2991 | // handle u{ byte, short, int } -> { byte, short, int } |
| 2992 | switch (SrcClass) { |
| 2993 | case cByte: |
| 2994 | if (DestClass == cByte) |
| 2995 | // uByte 255 -> signed byte == -1 |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2996 | BuildMI(*MBB, IP, PPC::EXTSB, 1, DestReg).addReg(SrcReg); |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 2997 | else |
| 2998 | // uByte 255 -> signed short/int == 255 |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 2999 | BuildMI(*MBB, IP, PPC::RLWINM, 4, DestReg).addReg(SrcReg).addImm(0) |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 3000 | .addImm(24).addImm(31); |
| 3001 | break; |
| 3002 | case cShort: |
| 3003 | if (DestClass == cByte) |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 3004 | BuildMI(*MBB, IP, PPC::EXTSB, 1, DestReg).addReg(SrcReg); |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 3005 | else if (DestClass == cShort) |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 3006 | BuildMI(*MBB, IP, PPC::EXTSH, 1, DestReg).addReg(SrcReg); |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 3007 | else |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 3008 | BuildMI(*MBB, IP, PPC::RLWINM, 4, DestReg).addReg(SrcReg).addImm(0) |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 3009 | .addImm(16).addImm(31); |
| 3010 | break; |
| 3011 | case cLong: |
| 3012 | ++SrcReg; |
| 3013 | // Fall through |
| 3014 | case cInt: |
| 3015 | if (DestClass == cByte) |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 3016 | BuildMI(*MBB, IP, PPC::EXTSB, 1, DestReg).addReg(SrcReg); |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 3017 | else if (DestClass == cShort) |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 3018 | BuildMI(*MBB, IP, PPC::EXTSH, 1, DestReg).addReg(SrcReg); |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 3019 | else |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 3020 | BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg); |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 3021 | break; |
| 3022 | } |
| 3023 | return; |
| 3024 | } |
| 3025 | |
| 3026 | // Signed -> Unsigned |
| 3027 | if (!sourceUnsigned && destUnsigned) { |
| 3028 | // handle long dest class now to keep switch clean |
| 3029 | if (DestClass == cLong) { |
| 3030 | if (SrcClass == cLong) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 3031 | BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg); |
| 3032 | BuildMI(*MBB, IP, PPC::OR, 2, DestReg+1).addReg(SrcReg+1) |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 3033 | .addReg(SrcReg+1); |
| 3034 | } else { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 3035 | BuildMI(*MBB, IP, PPC::SRAWI, 2, DestReg).addReg(SrcReg).addImm(31); |
| 3036 | BuildMI(*MBB, IP, PPC::OR, 2, DestReg+1).addReg(SrcReg) |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 3037 | .addReg(SrcReg); |
| 3038 | } |
| 3039 | return; |
| 3040 | } |
| 3041 | |
| 3042 | // handle { byte, short, int } -> u{ byte, short, int } |
| 3043 | unsigned clearBits = (DestClass == cByte) ? 24 : 16; |
| 3044 | switch (SrcClass) { |
| 3045 | case cByte: |
| 3046 | case cShort: |
| 3047 | if (DestClass == cByte || DestClass == cShort) |
| 3048 | // sbyte -1 -> ubyte 0x000000FF |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 3049 | BuildMI(*MBB, IP, PPC::RLWINM, 4, DestReg).addReg(SrcReg) |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 3050 | .addImm(0).addImm(clearBits).addImm(31); |
| 3051 | else |
| 3052 | // sbyte -1 -> ubyte 0xFFFFFFFF |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 3053 | BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg); |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 3054 | break; |
| 3055 | case cLong: |
| 3056 | ++SrcReg; |
| 3057 | // Fall through |
| 3058 | case cInt: |
| 3059 | if (DestClass == cInt) |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 3060 | BuildMI(*MBB, IP, PPC::OR, 2, DestReg).addReg(SrcReg).addReg(SrcReg); |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 3061 | else |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 3062 | BuildMI(*MBB, IP, PPC::RLWINM, 4, DestReg).addReg(SrcReg) |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 3063 | .addImm(0).addImm(clearBits).addImm(31); |
| 3064 | break; |
Misha Brukman | 7e898c3 | 2004-07-20 00:41:46 +0000 | [diff] [blame] | 3065 | } |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 3066 | return; |
| 3067 | } |
| 3068 | |
| 3069 | // Anything we haven't handled already, we can't (yet) handle at all. |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 3070 | std::cerr << "Unhandled cast from " << SrcTy->getDescription() |
| 3071 | << "to " << DestTy->getDescription() << '\n'; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 3072 | abort(); |
| 3073 | } |
| 3074 | |
| 3075 | /// visitVANextInst - Implement the va_next instruction... |
| 3076 | /// |
| 3077 | void ISel::visitVANextInst(VANextInst &I) { |
| 3078 | unsigned VAList = getReg(I.getOperand(0)); |
| 3079 | unsigned DestReg = getReg(I); |
| 3080 | |
| 3081 | unsigned Size; |
Misha Brukman | 358829f | 2004-06-21 17:25:55 +0000 | [diff] [blame] | 3082 | switch (I.getArgType()->getTypeID()) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 3083 | default: |
| 3084 | std::cerr << I; |
| 3085 | assert(0 && "Error: bad type for va_next instruction!"); |
| 3086 | return; |
| 3087 | case Type::PointerTyID: |
| 3088 | case Type::UIntTyID: |
| 3089 | case Type::IntTyID: |
| 3090 | Size = 4; |
| 3091 | break; |
| 3092 | case Type::ULongTyID: |
| 3093 | case Type::LongTyID: |
| 3094 | case Type::DoubleTyID: |
| 3095 | Size = 8; |
| 3096 | break; |
| 3097 | } |
| 3098 | |
| 3099 | // Increment the VAList pointer... |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 3100 | BuildMI(BB, PPC::ADDI, 2, DestReg).addReg(VAList).addSImm(Size); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 3101 | } |
| 3102 | |
| 3103 | void ISel::visitVAArgInst(VAArgInst &I) { |
| 3104 | unsigned VAList = getReg(I.getOperand(0)); |
| 3105 | unsigned DestReg = getReg(I); |
| 3106 | |
Misha Brukman | 358829f | 2004-06-21 17:25:55 +0000 | [diff] [blame] | 3107 | switch (I.getType()->getTypeID()) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 3108 | default: |
| 3109 | std::cerr << I; |
| 3110 | assert(0 && "Error: bad type for va_next instruction!"); |
| 3111 | return; |
| 3112 | case Type::PointerTyID: |
| 3113 | case Type::UIntTyID: |
| 3114 | case Type::IntTyID: |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 3115 | BuildMI(BB, PPC::LWZ, 2, DestReg).addSImm(0).addReg(VAList); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 3116 | break; |
| 3117 | case Type::ULongTyID: |
| 3118 | case Type::LongTyID: |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 3119 | BuildMI(BB, PPC::LWZ, 2, DestReg).addSImm(0).addReg(VAList); |
| 3120 | BuildMI(BB, PPC::LWZ, 2, DestReg+1).addSImm(4).addReg(VAList); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 3121 | break; |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 3122 | case Type::FloatTyID: |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 3123 | BuildMI(BB, PPC::LFS, 2, DestReg).addSImm(0).addReg(VAList); |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 3124 | break; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 3125 | case Type::DoubleTyID: |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 3126 | BuildMI(BB, PPC::LFD, 2, DestReg).addSImm(0).addReg(VAList); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 3127 | break; |
| 3128 | } |
| 3129 | } |
| 3130 | |
| 3131 | /// visitGetElementPtrInst - instruction-select GEP instructions |
| 3132 | /// |
| 3133 | void ISel::visitGetElementPtrInst(GetElementPtrInst &I) { |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 3134 | if (canFoldGEPIntoLoadOrStore(&I)) |
| 3135 | return; |
| 3136 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 3137 | unsigned outputReg = getReg(I); |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 3138 | emitGEPOperation(BB, BB->end(), I.getOperand(0), I.op_begin()+1, I.op_end(), |
Nate Begeman | b64af91 | 2004-08-10 20:42:36 +0000 | [diff] [blame] | 3139 | outputReg, false, 0, 0); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 3140 | } |
| 3141 | |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 3142 | /// emitGEPOperation - Common code shared between visitGetElementPtrInst and |
| 3143 | /// constant expression GEP support. |
| 3144 | /// |
Misha Brukman | 17a9000 | 2004-07-21 20:22:06 +0000 | [diff] [blame] | 3145 | void ISel::emitGEPOperation(MachineBasicBlock *MBB, |
| 3146 | MachineBasicBlock::iterator IP, |
| 3147 | Value *Src, User::op_iterator IdxBegin, |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 3148 | User::op_iterator IdxEnd, unsigned TargetReg, |
Nate Begeman | b64af91 | 2004-08-10 20:42:36 +0000 | [diff] [blame] | 3149 | bool GEPIsFolded, ConstantSInt **RemainderPtr, |
| 3150 | unsigned *PendingAddReg) { |
Misha Brukman | 2ed17ca | 2004-07-22 15:58:04 +0000 | [diff] [blame] | 3151 | const TargetData &TD = TM.getTargetData(); |
| 3152 | const Type *Ty = Src->getType(); |
| 3153 | unsigned basePtrReg = getReg(Src, MBB, IP); |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 3154 | int64_t constValue = 0; |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 3155 | |
| 3156 | // Record the operations to emit the GEP in a vector so that we can emit them |
| 3157 | // after having analyzed the entire instruction. |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 3158 | std::vector<CollapsedGepOp> ops; |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 3159 | |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 3160 | // GEPs have zero or more indices; we must perform a struct access |
| 3161 | // or array access for each one. |
| 3162 | for (GetElementPtrInst::op_iterator oi = IdxBegin, oe = IdxEnd; oi != oe; |
| 3163 | ++oi) { |
| 3164 | Value *idx = *oi; |
Misha Brukman | 2ed17ca | 2004-07-22 15:58:04 +0000 | [diff] [blame] | 3165 | if (const StructType *StTy = dyn_cast<StructType>(Ty)) { |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 3166 | // It's a struct access. idx is the index into the structure, |
| 3167 | // which names the field. Use the TargetData structure to |
| 3168 | // pick out what the layout of the structure is in memory. |
| 3169 | // Use the (constant) structure index's value to find the |
| 3170 | // right byte offset from the StructLayout class's list of |
| 3171 | // structure member offsets. |
Misha Brukman | 2ed17ca | 2004-07-22 15:58:04 +0000 | [diff] [blame] | 3172 | unsigned fieldIndex = cast<ConstantUInt>(idx)->getValue(); |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 3173 | unsigned memberOffset = |
Misha Brukman | 2ed17ca | 2004-07-22 15:58:04 +0000 | [diff] [blame] | 3174 | TD.getStructLayout(StTy)->MemberOffsets[fieldIndex]; |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 3175 | |
| 3176 | // StructType member offsets are always constant values. Add it to the |
| 3177 | // running total. |
| 3178 | constValue += memberOffset; |
| 3179 | |
| 3180 | // The next type is the member of the structure selected by the |
| 3181 | // index. |
| 3182 | Ty = StTy->getElementType (fieldIndex); |
| 3183 | } else if (const SequentialType *SqTy = dyn_cast<SequentialType> (Ty)) { |
Misha Brukman | 313efcb | 2004-07-09 15:45:07 +0000 | [diff] [blame] | 3184 | // Many GEP instructions use a [cast (int/uint) to LongTy] as their |
| 3185 | // operand. Handle this case directly now... |
| 3186 | if (CastInst *CI = dyn_cast<CastInst>(idx)) |
| 3187 | if (CI->getOperand(0)->getType() == Type::IntTy || |
| 3188 | CI->getOperand(0)->getType() == Type::UIntTy) |
| 3189 | idx = CI->getOperand(0); |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 3190 | |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 3191 | // It's an array or pointer access: [ArraySize x ElementType]. |
| 3192 | // We want to add basePtrReg to (idxReg * sizeof ElementType). First, we |
| 3193 | // must find the size of the pointed-to type (Not coincidentally, the next |
| 3194 | // type is the type of the elements in the array). |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 3195 | Ty = SqTy->getElementType(); |
Misha Brukman | 2ed17ca | 2004-07-22 15:58:04 +0000 | [diff] [blame] | 3196 | unsigned elementSize = TD.getTypeSize(Ty); |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 3197 | |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 3198 | if (ConstantInt *C = dyn_cast<ConstantInt>(idx)) { |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 3199 | if (ConstantSInt *CS = dyn_cast<ConstantSInt>(C)) |
| 3200 | constValue += CS->getValue() * elementSize; |
| 3201 | else if (ConstantUInt *CU = dyn_cast<ConstantUInt>(C)) |
| 3202 | constValue += CU->getValue() * elementSize; |
| 3203 | else |
| 3204 | assert(0 && "Invalid ConstantInt GEP index type!"); |
| 3205 | } else { |
| 3206 | // Push current gep state to this point as an add |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 3207 | ops.push_back(CollapsedGepOp(false, 0, |
| 3208 | ConstantSInt::get(Type::IntTy,constValue))); |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 3209 | |
| 3210 | // Push multiply gep op and reset constant value |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 3211 | ops.push_back(CollapsedGepOp(true, idx, |
| 3212 | ConstantSInt::get(Type::IntTy, elementSize))); |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 3213 | |
| 3214 | constValue = 0; |
Misha Brukman | 313efcb | 2004-07-09 15:45:07 +0000 | [diff] [blame] | 3215 | } |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 3216 | } |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 3217 | } |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 3218 | // Emit instructions for all the collapsed ops |
Nate Begeman | b64af91 | 2004-08-10 20:42:36 +0000 | [diff] [blame] | 3219 | bool pendingAdd = false; |
| 3220 | unsigned pendingAddReg = 0; |
| 3221 | |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 3222 | for(std::vector<CollapsedGepOp>::iterator cgo_i = ops.begin(), |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 3223 | cgo_e = ops.end(); cgo_i != cgo_e; ++cgo_i) { |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 3224 | CollapsedGepOp& cgo = *cgo_i; |
Nate Begeman | b64af91 | 2004-08-10 20:42:36 +0000 | [diff] [blame] | 3225 | unsigned nextBasePtrReg = makeAnotherReg(Type::IntTy); |
| 3226 | |
| 3227 | // If we didn't emit an add last time through the loop, we need to now so |
| 3228 | // that the base reg is updated appropriately. |
| 3229 | if (pendingAdd) { |
| 3230 | assert(pendingAddReg != 0 && "Uninitialized register in pending add!"); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 3231 | BuildMI(*MBB, IP, PPC::ADD, 2, nextBasePtrReg).addReg(basePtrReg) |
Nate Begeman | b64af91 | 2004-08-10 20:42:36 +0000 | [diff] [blame] | 3232 | .addReg(pendingAddReg); |
| 3233 | basePtrReg = nextBasePtrReg; |
| 3234 | nextBasePtrReg = makeAnotherReg(Type::IntTy); |
| 3235 | pendingAddReg = 0; |
| 3236 | pendingAdd = false; |
| 3237 | } |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 3238 | |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 3239 | if (cgo.isMul) { |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 3240 | // We know the elementSize is a constant, so we can emit a constant mul |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 3241 | unsigned TmpReg = makeAnotherReg(Type::IntTy); |
Nate Begeman | b64af91 | 2004-08-10 20:42:36 +0000 | [diff] [blame] | 3242 | doMultiplyConst(MBB, IP, nextBasePtrReg, cgo.index, cgo.size); |
| 3243 | pendingAddReg = basePtrReg; |
| 3244 | pendingAdd = true; |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 3245 | } else { |
| 3246 | // Try and generate an immediate addition if possible |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 3247 | if (cgo.size->isNullValue()) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 3248 | BuildMI(*MBB, IP, PPC::OR, 2, nextBasePtrReg).addReg(basePtrReg) |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 3249 | .addReg(basePtrReg); |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 3250 | } else if (canUseAsImmediateForOpcode(cgo.size, 0)) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 3251 | BuildMI(*MBB, IP, PPC::ADDI, 2, nextBasePtrReg).addReg(basePtrReg) |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 3252 | .addSImm(cgo.size->getValue()); |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 3253 | } else { |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 3254 | unsigned Op1r = getReg(cgo.size, MBB, IP); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 3255 | BuildMI(*MBB, IP, PPC::ADD, 2, nextBasePtrReg).addReg(basePtrReg) |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 3256 | .addReg(Op1r); |
| 3257 | } |
| 3258 | } |
| 3259 | |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 3260 | basePtrReg = nextBasePtrReg; |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 3261 | } |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 3262 | // Add the current base register plus any accumulated constant value |
| 3263 | ConstantSInt *remainder = ConstantSInt::get(Type::IntTy, constValue); |
| 3264 | |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 3265 | // If we are emitting this during a fold, copy the current base register to |
| 3266 | // the target, and save the current constant offset so the folding load or |
| 3267 | // store can try and use it as an immediate. |
| 3268 | if (GEPIsFolded) { |
Nate Begeman | b64af91 | 2004-08-10 20:42:36 +0000 | [diff] [blame] | 3269 | // If this is a folded GEP and the last element was an index, then we need |
| 3270 | // to do some extra work to turn a shift/add/stw into a shift/stwx |
| 3271 | if (pendingAdd && 0 == remainder->getValue()) { |
| 3272 | assert(pendingAddReg != 0 && "Uninitialized register in pending add!"); |
| 3273 | *PendingAddReg = pendingAddReg; |
| 3274 | } else { |
| 3275 | *PendingAddReg = 0; |
| 3276 | if (pendingAdd) { |
| 3277 | unsigned nextBasePtrReg = makeAnotherReg(Type::IntTy); |
| 3278 | assert(pendingAddReg != 0 && "Uninitialized register in pending add!"); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 3279 | BuildMI(*MBB, IP, PPC::ADD, 2, nextBasePtrReg).addReg(basePtrReg) |
Nate Begeman | b64af91 | 2004-08-10 20:42:36 +0000 | [diff] [blame] | 3280 | .addReg(pendingAddReg); |
| 3281 | basePtrReg = nextBasePtrReg; |
| 3282 | } |
| 3283 | } |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 3284 | BuildMI (*MBB, IP, PPC::OR, 2, TargetReg).addReg(basePtrReg) |
Nate Begeman | b64af91 | 2004-08-10 20:42:36 +0000 | [diff] [blame] | 3285 | .addReg(basePtrReg); |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 3286 | *RemainderPtr = remainder; |
| 3287 | return; |
| 3288 | } |
Nate Begeman | b64af91 | 2004-08-10 20:42:36 +0000 | [diff] [blame] | 3289 | |
| 3290 | // If we still have a pending add at this point, emit it now |
| 3291 | if (pendingAdd) { |
| 3292 | unsigned TmpReg = makeAnotherReg(Type::IntTy); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 3293 | BuildMI(*MBB, IP, PPC::ADD, 2, TmpReg).addReg(pendingAddReg) |
Nate Begeman | b64af91 | 2004-08-10 20:42:36 +0000 | [diff] [blame] | 3294 | .addReg(basePtrReg); |
| 3295 | basePtrReg = TmpReg; |
| 3296 | } |
Misha Brukman | b097f21 | 2004-07-26 18:13:24 +0000 | [diff] [blame] | 3297 | |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 3298 | // After we have processed all the indices, the result is left in |
| 3299 | // basePtrReg. Move it to the register where we were expected to |
| 3300 | // put the answer. |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 3301 | if (remainder->isNullValue()) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 3302 | BuildMI (*MBB, IP, PPC::OR, 2, TargetReg).addReg(basePtrReg) |
Nate Begeman | b64af91 | 2004-08-10 20:42:36 +0000 | [diff] [blame] | 3303 | .addReg(basePtrReg); |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 3304 | } else if (canUseAsImmediateForOpcode(remainder, 0)) { |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 3305 | BuildMI(*MBB, IP, PPC::ADDI, 2, TargetReg).addReg(basePtrReg) |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 3306 | .addSImm(remainder->getValue()); |
| 3307 | } else { |
| 3308 | unsigned Op1r = getReg(remainder, MBB, IP); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 3309 | BuildMI(*MBB, IP, PPC::ADD, 2, TargetReg).addReg(basePtrReg).addReg(Op1r); |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 3310 | } |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 3311 | } |
| 3312 | |
| 3313 | /// visitAllocaInst - If this is a fixed size alloca, allocate space from the |
| 3314 | /// frame manager, otherwise do it the hard way. |
| 3315 | /// |
| 3316 | void ISel::visitAllocaInst(AllocaInst &I) { |
| 3317 | // If this is a fixed size alloca in the entry block for the function, we |
| 3318 | // statically stack allocate the space, so we don't need to do anything here. |
| 3319 | // |
| 3320 | if (dyn_castFixedAlloca(&I)) return; |
| 3321 | |
| 3322 | // Find the data size of the alloca inst's getAllocatedType. |
| 3323 | const Type *Ty = I.getAllocatedType(); |
| 3324 | unsigned TySize = TM.getTargetData().getTypeSize(Ty); |
| 3325 | |
| 3326 | // Create a register to hold the temporary result of multiplying the type size |
| 3327 | // constant by the variable amount. |
| 3328 | unsigned TotalSizeReg = makeAnotherReg(Type::UIntTy); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 3329 | |
| 3330 | // TotalSizeReg = mul <numelements>, <TypeSize> |
| 3331 | MachineBasicBlock::iterator MBBI = BB->end(); |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 3332 | ConstantUInt *CUI = ConstantUInt::get(Type::UIntTy, TySize); |
| 3333 | doMultiplyConst(BB, MBBI, TotalSizeReg, I.getArraySize(), CUI); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 3334 | |
| 3335 | // AddedSize = add <TotalSizeReg>, 15 |
| 3336 | unsigned AddedSizeReg = makeAnotherReg(Type::UIntTy); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 3337 | BuildMI(BB, PPC::ADDI, 2, AddedSizeReg).addReg(TotalSizeReg).addSImm(15); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 3338 | |
| 3339 | // AlignedSize = and <AddedSize>, ~15 |
| 3340 | unsigned AlignedSize = makeAnotherReg(Type::UIntTy); |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 3341 | BuildMI(BB, PPC::RLWINM, 4, AlignedSize).addReg(AddedSizeReg).addImm(0) |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 3342 | .addImm(0).addImm(27); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 3343 | |
| 3344 | // Subtract size from stack pointer, thereby allocating some space. |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 3345 | BuildMI(BB, PPC::SUB, 2, PPC::R1).addReg(PPC::R1).addReg(AlignedSize); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 3346 | |
| 3347 | // Put a pointer to the space into the result register, by copying |
| 3348 | // the stack pointer. |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 3349 | BuildMI(BB, PPC::OR, 2, getReg(I)).addReg(PPC::R1).addReg(PPC::R1); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 3350 | |
| 3351 | // Inform the Frame Information that we have just allocated a variable-sized |
| 3352 | // object. |
| 3353 | F->getFrameInfo()->CreateVariableSizedObject(); |
| 3354 | } |
| 3355 | |
| 3356 | /// visitMallocInst - Malloc instructions are code generated into direct calls |
| 3357 | /// to the library malloc. |
| 3358 | /// |
| 3359 | void ISel::visitMallocInst(MallocInst &I) { |
| 3360 | unsigned AllocSize = TM.getTargetData().getTypeSize(I.getAllocatedType()); |
| 3361 | unsigned Arg; |
| 3362 | |
| 3363 | if (ConstantUInt *C = dyn_cast<ConstantUInt>(I.getOperand(0))) { |
| 3364 | Arg = getReg(ConstantUInt::get(Type::UIntTy, C->getValue() * AllocSize)); |
| 3365 | } else { |
| 3366 | Arg = makeAnotherReg(Type::UIntTy); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 3367 | MachineBasicBlock::iterator MBBI = BB->end(); |
Misha Brukman | 1013ef5 | 2004-07-21 20:09:08 +0000 | [diff] [blame] | 3368 | ConstantUInt *CUI = ConstantUInt::get(Type::UIntTy, AllocSize); |
| 3369 | doMultiplyConst(BB, MBBI, Arg, I.getOperand(0), CUI); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 3370 | } |
| 3371 | |
| 3372 | std::vector<ValueRecord> Args; |
| 3373 | Args.push_back(ValueRecord(Arg, Type::UIntTy)); |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 3374 | MachineInstr *TheCall = |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 3375 | BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(mallocFn, true); |
Misha Brukman | d18a31d | 2004-07-06 22:51:53 +0000 | [diff] [blame] | 3376 | doCall(ValueRecord(getReg(I), I.getType()), TheCall, Args, false); |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 3377 | TM.CalledFunctions.insert(mallocFn); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 3378 | } |
| 3379 | |
| 3380 | |
| 3381 | /// visitFreeInst - Free instructions are code gen'd to call the free libc |
| 3382 | /// function. |
| 3383 | /// |
| 3384 | void ISel::visitFreeInst(FreeInst &I) { |
| 3385 | std::vector<ValueRecord> Args; |
| 3386 | Args.push_back(ValueRecord(I.getOperand(0))); |
Misha Brukman | 2fec990 | 2004-06-21 20:22:03 +0000 | [diff] [blame] | 3387 | MachineInstr *TheCall = |
Misha Brukman | 5b57081 | 2004-08-10 22:47:03 +0000 | [diff] [blame] | 3388 | BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(freeFn, true); |
Misha Brukman | d18a31d | 2004-07-06 22:51:53 +0000 | [diff] [blame] | 3389 | doCall(ValueRecord(0, Type::VoidTy), TheCall, Args, false); |
Misha Brukman | e2eceb5 | 2004-07-23 16:08:20 +0000 | [diff] [blame] | 3390 | TM.CalledFunctions.insert(freeFn); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 3391 | } |
| 3392 | |
Misha Brukman | 3d9a6c2 | 2004-08-11 00:09:42 +0000 | [diff] [blame] | 3393 | /// createPPC32ISelSimple - This pass converts an LLVM function into a machine |
| 3394 | /// code representation is a very simple peep-hole fashion. |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 3395 | /// |
Misha Brukman | 3d9a6c2 | 2004-08-11 00:09:42 +0000 | [diff] [blame] | 3396 | FunctionPass *llvm::createPPC32ISelSimple(TargetMachine &TM) { |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 3397 | return new ISel(TM); |
| 3398 | } |