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