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