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