Chris Lattner | 035dfbe | 2002-08-09 20:08:06 +0000 | [diff] [blame] | 1 | //===-- SparcInstrInfo.cpp ------------------------------------------------===// |
| 2 | // |
| 3 | //===----------------------------------------------------------------------===// |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 4 | |
| 5 | #include "SparcInternals.h" |
| 6 | #include "SparcInstrSelectionSupport.h" |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 7 | #include "llvm/CodeGen/InstrSelection.h" |
| 8 | #include "llvm/CodeGen/InstrSelectionSupport.h" |
Chris Lattner | cb0a120 | 2002-02-03 07:49:49 +0000 | [diff] [blame] | 9 | #include "llvm/CodeGen/MachineCodeForMethod.h" |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 10 | #include "llvm/CodeGen/MachineCodeForInstruction.h" |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 11 | #include "llvm/Function.h" |
Chris Lattner | 31bcdb8 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 12 | #include "llvm/Constants.h" |
Vikram S. Adve | b9c3863 | 2001-11-08 04:57:53 +0000 | [diff] [blame] | 13 | #include "llvm/DerivedTypes.h" |
Vikram S. Adve | 4900116 | 2002-09-16 15:56:01 +0000 | [diff] [blame] | 14 | #include <stdlib.h> |
Anand Shukla | cfb22d3 | 2002-06-25 20:55:50 +0000 | [diff] [blame] | 15 | using std::vector; |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 16 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 17 | static const uint32_t MAXLO = (1 << 10) - 1; // set bits set by %lo(*) |
| 18 | static const uint32_t MAXSIMM = (1 << 12) - 1; // set bits in simm13 field of OR |
| 19 | |
| 20 | |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 21 | //---------------------------------------------------------------------------- |
| 22 | // Function: CreateSETUWConst |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 23 | // |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 24 | // Set a 32-bit unsigned constant in the register `dest', using |
| 25 | // SETHI, OR in the worst case. This function correctly emulates |
| 26 | // the SETUW pseudo-op for SPARC v9 (if argument isSigned == false). |
| 27 | // |
| 28 | // The isSigned=true case is used to implement SETSW without duplicating code. |
| 29 | // |
| 30 | // Optimize some common cases: |
| 31 | // (1) Small value that fits in simm13 field of OR: don't need SETHI. |
| 32 | // (2) isSigned = true and C is a small negative signed value, i.e., |
| 33 | // high bits are 1, and the remaining bits fit in simm13(OR). |
| 34 | //---------------------------------------------------------------------------- |
| 35 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 36 | static inline void |
| 37 | CreateSETUWConst(const TargetMachine& target, uint32_t C, |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 38 | Instruction* dest, vector<MachineInstr*>& mvec, |
| 39 | bool isSigned = false) |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 40 | { |
| 41 | MachineInstr *miSETHI = NULL, *miOR = NULL; |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 42 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 43 | // In order to get efficient code, we should not generate the SETHI if |
| 44 | // all high bits are 1 (i.e., this is a small signed value that fits in |
| 45 | // the simm13 field of OR). So we check for and handle that case specially. |
| 46 | // NOTE: The value C = 0x80000000 is bad: sC < 0 *and* -sC < 0. |
| 47 | // In fact, sC == -sC, so we have to check for this explicitly. |
| 48 | int32_t sC = (int32_t) C; |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 49 | bool smallNegValue =isSigned && sC < 0 && sC != -sC && -sC < (int32_t)MAXSIMM; |
| 50 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 51 | // Set the high 22 bits in dest if non-zero and simm13 field of OR not enough |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 52 | if (!smallNegValue && (C & ~MAXLO) && C > MAXSIMM) |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 53 | { |
| 54 | miSETHI = Create2OperandInstr_UImmed(SETHI, C, dest); |
| 55 | miSETHI->setOperandHi32(0); |
| 56 | mvec.push_back(miSETHI); |
| 57 | } |
| 58 | |
| 59 | // Set the low 10 or 12 bits in dest. This is necessary if no SETHI |
| 60 | // was generated, or if the low 10 bits are non-zero. |
| 61 | if (miSETHI==NULL || C & MAXLO) |
| 62 | { |
| 63 | if (miSETHI) |
| 64 | { // unsigned value with high-order bits set using SETHI |
| 65 | miOR = Create3OperandInstr_UImmed(OR, dest, C, dest); |
| 66 | miOR->setOperandLo32(1); |
| 67 | } |
| 68 | else |
| 69 | { // unsigned or small signed value that fits in simm13 field of OR |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 70 | assert(smallNegValue || (C & ~MAXSIMM) == 0); |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 71 | miOR = new MachineInstr(OR); |
| 72 | miOR->SetMachineOperandReg(0, target.getRegInfo().getZeroRegNum()); |
| 73 | miOR->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed, |
| 74 | sC); |
| 75 | miOR->SetMachineOperandVal(2,MachineOperand::MO_VirtualRegister,dest); |
| 76 | } |
| 77 | mvec.push_back(miOR); |
| 78 | } |
| 79 | |
| 80 | assert((miSETHI || miOR) && "Oops, no code was generated!"); |
| 81 | } |
| 82 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 83 | |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 84 | //---------------------------------------------------------------------------- |
| 85 | // Function: CreateSETSWConst |
| 86 | // |
| 87 | // Set a 32-bit signed constant in the register `dest', with sign-extension |
| 88 | // to 64 bits. This uses SETHI, OR, SRA in the worst case. |
| 89 | // This function correctly emulates the SETSW pseudo-op for SPARC v9. |
| 90 | // |
| 91 | // Optimize the same cases as SETUWConst, plus: |
| 92 | // (1) SRA is not needed for positive or small negative values. |
| 93 | //---------------------------------------------------------------------------- |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 94 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 95 | static inline void |
| 96 | CreateSETSWConst(const TargetMachine& target, int32_t C, |
Chris Lattner | 035dfbe | 2002-08-09 20:08:06 +0000 | [diff] [blame] | 97 | Instruction* dest, vector<MachineInstr*>& mvec) |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 98 | { |
| 99 | MachineInstr* MI; |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 100 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 101 | // Set the low 32 bits of dest |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 102 | CreateSETUWConst(target, (uint32_t) C, dest, mvec, /*isSigned*/true); |
| 103 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 104 | // Sign-extend to the high 32 bits if needed |
| 105 | if (C < 0 && (-C) > (int32_t) MAXSIMM) |
| 106 | { |
| 107 | MI = Create3OperandInstr_UImmed(SRA, dest, 0, dest); |
| 108 | mvec.push_back(MI); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 113 | //---------------------------------------------------------------------------- |
| 114 | // Function: CreateSETXConst |
| 115 | // |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 116 | // Set a 64-bit signed or unsigned constant in the register `dest'. |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 117 | // Use SETUWConst for each 32 bit word, plus a left-shift-by-32 in between. |
| 118 | // This function correctly emulates the SETX pseudo-op for SPARC v9. |
| 119 | // |
| 120 | // Optimize the same cases as SETUWConst for each 32 bit word. |
| 121 | //---------------------------------------------------------------------------- |
| 122 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 123 | static inline void |
| 124 | CreateSETXConst(const TargetMachine& target, uint64_t C, |
| 125 | Instruction* tmpReg, Instruction* dest, |
Chris Lattner | 035dfbe | 2002-08-09 20:08:06 +0000 | [diff] [blame] | 126 | vector<MachineInstr*>& mvec) |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 127 | { |
| 128 | assert(C > (unsigned int) ~0 && "Use SETUW/SETSW for 32-bit values!"); |
| 129 | |
| 130 | MachineInstr* MI; |
| 131 | |
| 132 | // Code to set the upper 32 bits of the value in register `tmpReg' |
| 133 | CreateSETUWConst(target, (C >> 32), tmpReg, mvec); |
| 134 | |
| 135 | // Shift tmpReg left by 32 bits |
| 136 | MI = Create3OperandInstr_UImmed(SLLX, tmpReg, 32, tmpReg); |
| 137 | mvec.push_back(MI); |
| 138 | |
| 139 | // Code to set the low 32 bits of the value in register `dest' |
| 140 | CreateSETUWConst(target, C, dest, mvec); |
| 141 | |
| 142 | // dest = OR(tmpReg, dest) |
| 143 | MI = Create3OperandInstr(OR, dest, tmpReg, dest); |
| 144 | mvec.push_back(MI); |
| 145 | } |
| 146 | |
| 147 | |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 148 | //---------------------------------------------------------------------------- |
| 149 | // Function: CreateSETUWLabel |
| 150 | // |
| 151 | // Set a 32-bit constant (given by a symbolic label) in the register `dest'. |
| 152 | //---------------------------------------------------------------------------- |
| 153 | |
| 154 | static inline void |
| 155 | CreateSETUWLabel(const TargetMachine& target, Value* val, |
| 156 | Instruction* dest, vector<MachineInstr*>& mvec) |
| 157 | { |
| 158 | MachineInstr* MI; |
| 159 | |
| 160 | // Set the high 22 bits in dest |
| 161 | MI = Create2OperandInstr(SETHI, val, dest); |
| 162 | MI->setOperandHi32(0); |
| 163 | mvec.push_back(MI); |
| 164 | |
| 165 | // Set the low 10 bits in dest |
| 166 | MI = Create3OperandInstr(OR, dest, val, dest); |
| 167 | MI->setOperandLo32(1); |
| 168 | mvec.push_back(MI); |
| 169 | } |
| 170 | |
| 171 | |
| 172 | //---------------------------------------------------------------------------- |
| 173 | // Function: CreateSETXLabel |
| 174 | // |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 175 | // Set a 64-bit constant (given by a symbolic label) in the register `dest'. |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 176 | //---------------------------------------------------------------------------- |
| 177 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 178 | static inline void |
| 179 | CreateSETXLabel(const TargetMachine& target, |
| 180 | Value* val, Instruction* tmpReg, Instruction* dest, |
Chris Lattner | 035dfbe | 2002-08-09 20:08:06 +0000 | [diff] [blame] | 181 | vector<MachineInstr*>& mvec) |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 182 | { |
| 183 | assert(isa<Constant>(val) || isa<GlobalValue>(val) && |
| 184 | "I only know about constant values and global addresses"); |
| 185 | |
| 186 | MachineInstr* MI; |
| 187 | |
| 188 | MI = Create2OperandInstr_Addr(SETHI, val, tmpReg); |
| 189 | MI->setOperandHi64(0); |
| 190 | mvec.push_back(MI); |
| 191 | |
| 192 | MI = Create3OperandInstr_Addr(OR, tmpReg, val, tmpReg); |
| 193 | MI->setOperandLo64(1); |
| 194 | mvec.push_back(MI); |
| 195 | |
| 196 | MI = Create3OperandInstr_UImmed(SLLX, tmpReg, 32, tmpReg); |
| 197 | mvec.push_back(MI); |
| 198 | |
| 199 | MI = Create2OperandInstr_Addr(SETHI, val, dest); |
| 200 | MI->setOperandHi32(0); |
| 201 | mvec.push_back(MI); |
| 202 | |
| 203 | MI = Create3OperandInstr(OR, dest, tmpReg, dest); |
| 204 | mvec.push_back(MI); |
| 205 | |
| 206 | MI = Create3OperandInstr_Addr(OR, dest, val, dest); |
| 207 | MI->setOperandLo32(1); |
| 208 | mvec.push_back(MI); |
| 209 | } |
| 210 | |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 211 | |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 212 | //---------------------------------------------------------------------------- |
| 213 | // Function: CreateUIntSetInstruction |
| 214 | // |
| 215 | // Create code to Set an unsigned constant in the register `dest'. |
| 216 | // Uses CreateSETUWConst, CreateSETSWConst or CreateSETXConst as needed. |
| 217 | // CreateSETSWConst is an optimization for the case that the unsigned value |
| 218 | // has all ones in the 33 high bits (so that sign-extension sets them all). |
| 219 | //---------------------------------------------------------------------------- |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 220 | |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 221 | static inline void |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 222 | CreateUIntSetInstruction(const TargetMachine& target, |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 223 | uint64_t C, Instruction* dest, |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 224 | std::vector<MachineInstr*>& mvec, |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 225 | MachineCodeForInstruction& mcfi) |
Vikram S. Adve | cee9d1c | 2001-12-15 00:33:36 +0000 | [diff] [blame] | 226 | { |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 227 | static const uint64_t lo32 = (uint32_t) ~0; |
| 228 | if (C <= lo32) // High 32 bits are 0. Set low 32 bits. |
| 229 | CreateSETUWConst(target, (uint32_t) C, dest, mvec); |
| 230 | else if ((C & ~lo32) == ~lo32 && (C & (1 << 31))) |
| 231 | { // All high 33 (not 32) bits are 1s: sign-extension will take care |
| 232 | // of high 32 bits, so use the sequence for signed int |
| 233 | CreateSETSWConst(target, (int32_t) C, dest, mvec); |
| 234 | } |
| 235 | else if (C > lo32) |
Vikram S. Adve | cee9d1c | 2001-12-15 00:33:36 +0000 | [diff] [blame] | 236 | { // C does not fit in 32 bits |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 237 | TmpInstruction* tmpReg = new TmpInstruction(Type::IntTy); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 238 | mcfi.addTemp(tmpReg); |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 239 | CreateSETXConst(target, C, tmpReg, dest, mvec); |
Vikram S. Adve | cee9d1c | 2001-12-15 00:33:36 +0000 | [diff] [blame] | 240 | } |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 243 | |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 244 | //---------------------------------------------------------------------------- |
| 245 | // Function: CreateIntSetInstruction |
| 246 | // |
| 247 | // Create code to Set a signed constant in the register `dest'. |
| 248 | // Really the same as CreateUIntSetInstruction. |
| 249 | //---------------------------------------------------------------------------- |
| 250 | |
| 251 | static inline void |
| 252 | CreateIntSetInstruction(const TargetMachine& target, |
| 253 | int64_t C, Instruction* dest, |
| 254 | std::vector<MachineInstr*>& mvec, |
| 255 | MachineCodeForInstruction& mcfi) |
| 256 | { |
| 257 | CreateUIntSetInstruction(target, (uint64_t) C, dest, mvec, mcfi); |
| 258 | } |
Chris Lattner | 035dfbe | 2002-08-09 20:08:06 +0000 | [diff] [blame] | 259 | |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 260 | |
| 261 | //--------------------------------------------------------------------------- |
Vikram S. Adve | 4900116 | 2002-09-16 15:56:01 +0000 | [diff] [blame] | 262 | // Create a table of LLVM opcode -> max. immediate constant likely to |
| 263 | // be usable for that operation. |
| 264 | //--------------------------------------------------------------------------- |
| 265 | |
| 266 | // Entry == 0 ==> no immediate constant field exists at all. |
| 267 | // Entry > 0 ==> abs(immediate constant) <= Entry |
| 268 | // |
| 269 | vector<unsigned int> MaxConstantsTable(Instruction::NumOtherOps); |
| 270 | |
| 271 | static int |
| 272 | MaxConstantForInstr(unsigned llvmOpCode) |
| 273 | { |
| 274 | int modelOpCode = -1; |
| 275 | |
| 276 | if (llvmOpCode >= Instruction::FirstBinaryOp && |
| 277 | llvmOpCode < Instruction::NumBinaryOps) |
| 278 | modelOpCode = ADD; |
| 279 | else |
| 280 | switch(llvmOpCode) { |
| 281 | case Instruction::Ret: modelOpCode = JMPLCALL; break; |
| 282 | |
| 283 | case Instruction::Malloc: |
| 284 | case Instruction::Alloca: |
| 285 | case Instruction::GetElementPtr: |
| 286 | case Instruction::PHINode: |
| 287 | case Instruction::Cast: |
| 288 | case Instruction::Call: modelOpCode = ADD; break; |
| 289 | |
| 290 | case Instruction::Shl: |
| 291 | case Instruction::Shr: modelOpCode = SLLX; break; |
| 292 | |
| 293 | default: break; |
| 294 | }; |
| 295 | |
| 296 | return (modelOpCode < 0)? 0: SparcMachineInstrDesc[modelOpCode].maxImmedConst; |
| 297 | } |
| 298 | |
| 299 | static void |
| 300 | InitializeMaxConstantsTable() |
| 301 | { |
| 302 | unsigned op; |
| 303 | assert(MaxConstantsTable.size() == Instruction::NumOtherOps && |
| 304 | "assignments below will be illegal!"); |
| 305 | for (op = Instruction::FirstTermOp; op < Instruction::NumTermOps; ++op) |
| 306 | MaxConstantsTable[op] = MaxConstantForInstr(op); |
| 307 | for (op = Instruction::FirstBinaryOp; op < Instruction::NumBinaryOps; ++op) |
| 308 | MaxConstantsTable[op] = MaxConstantForInstr(op); |
| 309 | for (op = Instruction::FirstMemoryOp; op < Instruction::NumMemoryOps; ++op) |
| 310 | MaxConstantsTable[op] = MaxConstantForInstr(op); |
| 311 | for (op = Instruction::FirstOtherOp; op < Instruction::NumOtherOps; ++op) |
| 312 | MaxConstantsTable[op] = MaxConstantForInstr(op); |
| 313 | } |
| 314 | |
| 315 | |
| 316 | //--------------------------------------------------------------------------- |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 317 | // class UltraSparcInstrInfo |
| 318 | // |
| 319 | // Purpose: |
| 320 | // Information about individual instructions. |
| 321 | // Most information is stored in the SparcMachineInstrDesc array above. |
| 322 | // Other information is computed on demand, and most such functions |
| 323 | // default to member functions in base class MachineInstrInfo. |
| 324 | //--------------------------------------------------------------------------- |
| 325 | |
| 326 | /*ctor*/ |
Vikram S. Adve | b9c3863 | 2001-11-08 04:57:53 +0000 | [diff] [blame] | 327 | UltraSparcInstrInfo::UltraSparcInstrInfo(const TargetMachine& tgt) |
| 328 | : MachineInstrInfo(tgt, SparcMachineInstrDesc, |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 329 | /*descSize = */ NUM_TOTAL_OPCODES, |
| 330 | /*numRealOpCodes = */ NUM_REAL_OPCODES) |
| 331 | { |
Vikram S. Adve | 4900116 | 2002-09-16 15:56:01 +0000 | [diff] [blame] | 332 | InitializeMaxConstantsTable(); |
| 333 | } |
| 334 | |
| 335 | bool |
| 336 | UltraSparcInstrInfo::ConstantMayNotFitInImmedField(const Constant* CV, |
| 337 | const Instruction* I) const |
| 338 | { |
| 339 | if (I->getOpcode() >= MaxConstantsTable.size()) // user-defined op (or bug!) |
| 340 | return true; |
| 341 | |
| 342 | if (isa<ConstantPointerNull>(CV)) // can always use %g0 |
| 343 | return false; |
| 344 | |
| 345 | if (const ConstantUInt* U = dyn_cast<ConstantUInt>(CV)) |
| 346 | return (U->getValue() > MaxConstantsTable[I->getOpcode()]); |
| 347 | |
| 348 | if (const ConstantSInt* S = dyn_cast<ConstantSInt>(CV)) |
| 349 | return (labs(S->getValue()) > (int) MaxConstantsTable[I->getOpcode()]); |
| 350 | |
| 351 | if (isa<ConstantBool>(CV)) |
| 352 | return (1U > MaxConstantsTable[I->getOpcode()]); |
| 353 | |
| 354 | return true; |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Vikram S. Adve | e76af29 | 2002-03-18 03:09:15 +0000 | [diff] [blame] | 357 | // |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 358 | // Create an instruction sequence to put the constant `val' into |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 359 | // the virtual register `dest'. `val' may be a Constant or a |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 360 | // GlobalValue, viz., the constant address of a global variable or function. |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 361 | // The generated instructions are returned in `mvec'. |
| 362 | // Any temp. registers (TmpInstruction) created are recorded in mcfi. |
| 363 | // Any stack space required is allocated via MachineCodeForMethod. |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 364 | // |
| 365 | void |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 366 | UltraSparcInstrInfo::CreateCodeToLoadConst(const TargetMachine& target, |
| 367 | Function* F, |
| 368 | Value* val, |
Vikram S. Adve | e76af29 | 2002-03-18 03:09:15 +0000 | [diff] [blame] | 369 | Instruction* dest, |
Chris Lattner | 035dfbe | 2002-08-09 20:08:06 +0000 | [diff] [blame] | 370 | vector<MachineInstr*>& mvec, |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 371 | MachineCodeForInstruction& mcfi) const |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 372 | { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 373 | assert(isa<Constant>(val) || isa<GlobalValue>(val) && |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 374 | "I only know about constant values and global addresses"); |
| 375 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 376 | // Use a "set" instruction for known constants or symbolic constants (labels) |
| 377 | // that can go in an integer reg. |
| 378 | // We have to use a "load" instruction for all other constants, |
| 379 | // in particular, floating point constants. |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 380 | // |
| 381 | const Type* valType = val->getType(); |
| 382 | |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 383 | if (isa<GlobalValue>(val)) |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 384 | { |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 385 | TmpInstruction* tmpReg = |
| 386 | new TmpInstruction(PointerType::get(val->getType()), val); |
| 387 | mcfi.addTemp(tmpReg); |
| 388 | CreateSETXLabel(target, val, tmpReg, dest, mvec); |
| 389 | } |
Chris Lattner | 0c4e886 | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 390 | else if (valType->isIntegral()) |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 391 | { |
| 392 | bool isValidConstant; |
| 393 | unsigned opSize = target.DataLayout.getTypeSize(val->getType()); |
| 394 | unsigned destSize = target.DataLayout.getTypeSize(dest->getType()); |
| 395 | |
| 396 | if (! dest->getType()->isSigned()) |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 397 | { |
Vikram S. Adve | a40cbb3 | 2002-08-04 20:55:37 +0000 | [diff] [blame] | 398 | uint64_t C = GetConstantValueAsUnsignedInt(val, isValidConstant); |
| 399 | assert(isValidConstant && "Unrecognized constant"); |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 400 | |
| 401 | if (opSize > destSize || |
| 402 | (val->getType()->isSigned() |
| 403 | && destSize < target.DataLayout.getIntegerRegize())) |
| 404 | { // operand is larger than dest, |
| 405 | // OR both are equal but smaller than the full register size |
| 406 | // AND operand is signed, so it may have extra sign bits: |
| 407 | // mask high bits |
| 408 | C = C & ((1U << 8*destSize) - 1); |
| 409 | } |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 410 | CreateUIntSetInstruction(target, C, dest, mvec, mcfi); |
Vikram S. Adve | cee9d1c | 2001-12-15 00:33:36 +0000 | [diff] [blame] | 411 | } |
| 412 | else |
| 413 | { |
Vikram S. Adve | cee9d1c | 2001-12-15 00:33:36 +0000 | [diff] [blame] | 414 | int64_t C = GetConstantValueAsSignedInt(val, isValidConstant); |
| 415 | assert(isValidConstant && "Unrecognized constant"); |
Vikram S. Adve | 6c0c301 | 2002-08-13 18:04:08 +0000 | [diff] [blame] | 416 | |
| 417 | if (opSize > destSize) |
| 418 | // operand is larger than dest: mask high bits |
| 419 | C = C & ((1U << 8*destSize) - 1); |
| 420 | |
| 421 | if (opSize > destSize || |
| 422 | (opSize == destSize && !val->getType()->isSigned())) |
| 423 | // sign-extend from destSize to 64 bits |
| 424 | C = ((C & (1U << (8*destSize - 1))) |
| 425 | ? C | ~((1U << 8*destSize) - 1) |
| 426 | : C); |
| 427 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 428 | CreateIntSetInstruction(target, C, dest, mvec, mcfi); |
Vikram S. Adve | cee9d1c | 2001-12-15 00:33:36 +0000 | [diff] [blame] | 429 | } |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 430 | } |
| 431 | else |
| 432 | { |
| 433 | // Make an instruction sequence to load the constant, viz: |
Vikram S. Adve | a2a7094 | 2001-10-28 21:41:46 +0000 | [diff] [blame] | 434 | // SETX <addr-of-constant>, tmpReg, addrReg |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 435 | // LOAD /*addr*/ addrReg, /*offset*/ 0, dest |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 436 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 437 | // First, create a tmp register to be used by the SETX sequence. |
Vikram S. Adve | a2a7094 | 2001-10-28 21:41:46 +0000 | [diff] [blame] | 438 | TmpInstruction* tmpReg = |
Chris Lattner | cb0a120 | 2002-02-03 07:49:49 +0000 | [diff] [blame] | 439 | new TmpInstruction(PointerType::get(val->getType()), val); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 440 | mcfi.addTemp(tmpReg); |
Vikram S. Adve | a2a7094 | 2001-10-28 21:41:46 +0000 | [diff] [blame] | 441 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 442 | // Create another TmpInstruction for the address register |
| 443 | TmpInstruction* addrReg = |
Chris Lattner | cb0a120 | 2002-02-03 07:49:49 +0000 | [diff] [blame] | 444 | new TmpInstruction(PointerType::get(val->getType()), val); |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 445 | mcfi.addTemp(addrReg); |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 446 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 447 | // Put the address (a symbolic name) into a register |
| 448 | CreateSETXLabel(target, val, tmpReg, addrReg, mvec); |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 449 | |
Vikram S. Adve | 53fd400 | 2002-07-10 21:39:50 +0000 | [diff] [blame] | 450 | // Generate the load instruction |
| 451 | int64_t zeroOffset = 0; // to avoid ambiguity with (Value*) 0 |
| 452 | MachineInstr* MI = |
| 453 | Create3OperandInstr_SImmed(ChooseLoadInstruction(val->getType()), |
| 454 | addrReg, zeroOffset, dest); |
| 455 | mvec.push_back(MI); |
| 456 | |
| 457 | // Make sure constant is emitted to constant pool in assembly code. |
| 458 | MachineCodeForMethod::get(F).addToConstantPool(cast<Constant>(val)); |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | |
| 462 | |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 463 | // Create an instruction sequence to copy an integer register `val' |
| 464 | // to a floating point register `dest' by copying to memory and back. |
Vikram S. Adve | 5b6082e | 2001-11-09 02:16:40 +0000 | [diff] [blame] | 465 | // val must be an integral type. dest must be a Float or Double. |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 466 | // The generated instructions are returned in `mvec'. |
| 467 | // Any temp. registers (TmpInstruction) created are recorded in mcfi. |
| 468 | // Any stack space required is allocated via MachineCodeForMethod. |
Vikram S. Adve | b9c3863 | 2001-11-08 04:57:53 +0000 | [diff] [blame] | 469 | // |
| 470 | void |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 471 | UltraSparcInstrInfo::CreateCodeToCopyIntToFloat(const TargetMachine& target, |
| 472 | Function* F, |
| 473 | Value* val, |
| 474 | Instruction* dest, |
Chris Lattner | 035dfbe | 2002-08-09 20:08:06 +0000 | [diff] [blame] | 475 | vector<MachineInstr*>& mvec, |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 476 | MachineCodeForInstruction& mcfi) const |
Vikram S. Adve | b9c3863 | 2001-11-08 04:57:53 +0000 | [diff] [blame] | 477 | { |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 478 | assert((val->getType()->isIntegral() || isa<PointerType>(val->getType())) |
| 479 | && "Source type must be integral (integer or bool) or pointer"); |
Chris Lattner | 9b62503 | 2002-05-06 16:15:30 +0000 | [diff] [blame] | 480 | assert(dest->getType()->isFloatingPoint() |
Vikram S. Adve | b9c3863 | 2001-11-08 04:57:53 +0000 | [diff] [blame] | 481 | && "Dest type must be float/double"); |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 482 | |
| 483 | // Get a stack slot to use for the copy |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 484 | int offset = MachineCodeForMethod::get(F).allocateLocalVar(target, val); |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 485 | |
| 486 | // Get the size of the source value being copied. |
| 487 | size_t srcSize = target.DataLayout.getTypeSize(val->getType()); |
| 488 | |
Vikram S. Adve | b9c3863 | 2001-11-08 04:57:53 +0000 | [diff] [blame] | 489 | // Store instruction stores `val' to [%fp+offset]. |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 490 | // The store and load opCodes are based on the size of the source value. |
| 491 | // If the value is smaller than 32 bits, we must sign- or zero-extend it |
| 492 | // to 32 bits since the load-float will load 32 bits. |
Vikram S. Adve | c190c01 | 2002-07-31 21:13:31 +0000 | [diff] [blame] | 493 | // Note that the store instruction is the same for signed and unsigned ints. |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 494 | const Type* storeType = (srcSize <= 4)? Type::IntTy : Type::LongTy; |
| 495 | Value* storeVal = val; |
| 496 | if (srcSize < target.DataLayout.getTypeSize(Type::FloatTy)) |
| 497 | { // sign- or zero-extend respectively |
| 498 | storeVal = new TmpInstruction(storeType, val); |
| 499 | if (val->getType()->isSigned()) |
Vikram S. Adve | 5cedede | 2002-09-27 14:29:45 +0000 | [diff] [blame^] | 500 | CreateSignExtensionInstructions(target, F, val, storeVal, 8*srcSize, |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 501 | mvec, mcfi); |
| 502 | else |
Vikram S. Adve | 5cedede | 2002-09-27 14:29:45 +0000 | [diff] [blame^] | 503 | CreateZeroExtensionInstructions(target, F, val, storeVal, 8*srcSize, |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 504 | mvec, mcfi); |
| 505 | } |
| 506 | MachineInstr* store=new MachineInstr(ChooseStoreInstruction(storeType)); |
| 507 | store->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, storeVal); |
Vikram S. Adve | e76af29 | 2002-03-18 03:09:15 +0000 | [diff] [blame] | 508 | store->SetMachineOperandReg(1, target.getRegInfo().getFramePointer()); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 509 | store->SetMachineOperandConst(2,MachineOperand::MO_SignExtendedImmed,offset); |
| 510 | mvec.push_back(store); |
Vikram S. Adve | 30764b8 | 2001-10-18 00:01:48 +0000 | [diff] [blame] | 511 | |
Vikram S. Adve | b9c3863 | 2001-11-08 04:57:53 +0000 | [diff] [blame] | 512 | // Load instruction loads [%fp+offset] to `dest'. |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 513 | // The type of the load opCode is the floating point type that matches the |
| 514 | // stored type in size: |
| 515 | // On SparcV9: float for int or smaller, double for long. |
Vikram S. Adve | 5b6082e | 2001-11-09 02:16:40 +0000 | [diff] [blame] | 516 | // |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 517 | const Type* loadType = (srcSize <= 4)? Type::FloatTy : Type::DoubleTy; |
| 518 | MachineInstr* load = new MachineInstr(ChooseLoadInstruction(loadType)); |
Vikram S. Adve | e76af29 | 2002-03-18 03:09:15 +0000 | [diff] [blame] | 519 | load->SetMachineOperandReg(0, target.getRegInfo().getFramePointer()); |
| 520 | load->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,offset); |
| 521 | load->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, dest); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 522 | mvec.push_back(load); |
Vikram S. Adve | 5b6082e | 2001-11-09 02:16:40 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 525 | // Similarly, create an instruction sequence to copy an FP register |
| 526 | // `val' to an integer register `dest' by copying to memory and back. |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 527 | // The generated instructions are returned in `mvec'. |
| 528 | // Any temp. registers (TmpInstruction) created are recorded in mcfi. |
| 529 | // Any stack space required is allocated via MachineCodeForMethod. |
Vikram S. Adve | 5b6082e | 2001-11-09 02:16:40 +0000 | [diff] [blame] | 530 | // |
| 531 | void |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 532 | UltraSparcInstrInfo::CreateCodeToCopyFloatToInt(const TargetMachine& target, |
| 533 | Function* F, |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 534 | Value* val, |
| 535 | Instruction* dest, |
Chris Lattner | 035dfbe | 2002-08-09 20:08:06 +0000 | [diff] [blame] | 536 | vector<MachineInstr*>& mvec, |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 537 | MachineCodeForInstruction& mcfi) const |
Vikram S. Adve | 5b6082e | 2001-11-09 02:16:40 +0000 | [diff] [blame] | 538 | { |
Vikram S. Adve | c190c01 | 2002-07-31 21:13:31 +0000 | [diff] [blame] | 539 | const Type* opTy = val->getType(); |
| 540 | const Type* destTy = dest->getType(); |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 541 | |
Vikram S. Adve | c190c01 | 2002-07-31 21:13:31 +0000 | [diff] [blame] | 542 | assert(opTy->isFloatingPoint() && "Source type must be float/double"); |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 543 | assert((destTy->isIntegral() || isa<PointerType>(destTy)) |
| 544 | && "Dest type must be integer, bool or pointer"); |
Vikram S. Adve | c190c01 | 2002-07-31 21:13:31 +0000 | [diff] [blame] | 545 | |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 546 | int offset = MachineCodeForMethod::get(F).allocateLocalVar(target, val); |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 547 | |
Vikram S. Adve | 5b6082e | 2001-11-09 02:16:40 +0000 | [diff] [blame] | 548 | // Store instruction stores `val' to [%fp+offset]. |
Vikram S. Adve | c190c01 | 2002-07-31 21:13:31 +0000 | [diff] [blame] | 549 | // The store opCode is based only the source value being copied. |
Vikram S. Adve | 5b6082e | 2001-11-09 02:16:40 +0000 | [diff] [blame] | 550 | // |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 551 | MachineInstr* store=new MachineInstr(ChooseStoreInstruction(opTy)); |
Vikram S. Adve | e76af29 | 2002-03-18 03:09:15 +0000 | [diff] [blame] | 552 | store->SetMachineOperandVal(0, MachineOperand::MO_VirtualRegister, val); |
| 553 | store->SetMachineOperandReg(1, target.getRegInfo().getFramePointer()); |
| 554 | store->SetMachineOperandConst(2,MachineOperand::MO_SignExtendedImmed,offset); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 555 | mvec.push_back(store); |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 556 | |
Vikram S. Adve | 5b6082e | 2001-11-09 02:16:40 +0000 | [diff] [blame] | 557 | // Load instruction loads [%fp+offset] to `dest'. |
Vikram S. Adve | c190c01 | 2002-07-31 21:13:31 +0000 | [diff] [blame] | 558 | // The type of the load opCode is the integer type that matches the |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 559 | // source type in size: |
Vikram S. Adve | c190c01 | 2002-07-31 21:13:31 +0000 | [diff] [blame] | 560 | // On SparcV9: int for float, long for double. |
| 561 | // Note that we *must* use signed loads even for unsigned dest types, to |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 562 | // ensure correct sign-extension for UByte, UShort or UInt: |
| 563 | // |
| 564 | const Type* loadTy = (opTy == Type::FloatTy)? Type::IntTy : Type::LongTy; |
Vikram S. Adve | c190c01 | 2002-07-31 21:13:31 +0000 | [diff] [blame] | 565 | MachineInstr* load = new MachineInstr(ChooseLoadInstruction(loadTy)); |
Vikram S. Adve | e76af29 | 2002-03-18 03:09:15 +0000 | [diff] [blame] | 566 | load->SetMachineOperandReg(0, target.getRegInfo().getFramePointer()); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 567 | load->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed,offset); |
Vikram S. Adve | e76af29 | 2002-03-18 03:09:15 +0000 | [diff] [blame] | 568 | load->SetMachineOperandVal(2, MachineOperand::MO_VirtualRegister, dest); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 569 | mvec.push_back(load); |
| 570 | } |
| 571 | |
| 572 | |
| 573 | // Create instruction(s) to copy src to dest, for arbitrary types |
| 574 | // The generated instructions are returned in `mvec'. |
| 575 | // Any temp. registers (TmpInstruction) created are recorded in mcfi. |
| 576 | // Any stack space required is allocated via MachineCodeForMethod. |
| 577 | // |
| 578 | void |
| 579 | UltraSparcInstrInfo::CreateCopyInstructionsByType(const TargetMachine& target, |
| 580 | Function *F, |
| 581 | Value* src, |
| 582 | Instruction* dest, |
| 583 | vector<MachineInstr*>& mvec, |
| 584 | MachineCodeForInstruction& mcfi) const |
| 585 | { |
| 586 | bool loadConstantToReg = false; |
| 587 | |
| 588 | const Type* resultType = dest->getType(); |
| 589 | |
| 590 | MachineOpCode opCode = ChooseAddInstructionByType(resultType); |
| 591 | if (opCode == INVALID_OPCODE) |
| 592 | { |
| 593 | assert(0 && "Unsupported result type in CreateCopyInstructionsByType()"); |
| 594 | return; |
| 595 | } |
| 596 | |
| 597 | // if `src' is a constant that doesn't fit in the immed field or if it is |
| 598 | // a global variable (i.e., a constant address), generate a load |
| 599 | // instruction instead of an add |
| 600 | // |
| 601 | if (isa<Constant>(src)) |
| 602 | { |
| 603 | unsigned int machineRegNum; |
| 604 | int64_t immedValue; |
| 605 | MachineOperand::MachineOperandType opType = |
| 606 | ChooseRegOrImmed(src, opCode, target, /*canUseImmed*/ true, |
| 607 | machineRegNum, immedValue); |
| 608 | |
| 609 | if (opType == MachineOperand::MO_VirtualRegister) |
| 610 | loadConstantToReg = true; |
| 611 | } |
| 612 | else if (isa<GlobalValue>(src)) |
| 613 | loadConstantToReg = true; |
| 614 | |
| 615 | if (loadConstantToReg) |
| 616 | { // `src' is constant and cannot fit in immed field for the ADD |
| 617 | // Insert instructions to "load" the constant into a register |
| 618 | target.getInstrInfo().CreateCodeToLoadConst(target, F, src, dest, |
| 619 | mvec, mcfi); |
| 620 | } |
| 621 | else |
| 622 | { // Create an add-with-0 instruction of the appropriate type. |
| 623 | // Make `src' the second operand, in case it is a constant |
| 624 | // Use (unsigned long) 0 for a NULL pointer value. |
| 625 | // |
| 626 | const Type* zeroValueType = |
| 627 | isa<PointerType>(resultType) ? Type::ULongTy : resultType; |
| 628 | MachineInstr* minstr = |
| 629 | Create3OperandInstr(opCode, Constant::getNullValue(zeroValueType), |
| 630 | src, dest); |
| 631 | mvec.push_back(minstr); |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 636 | // Helper function for sign-extension and zero-extension. |
| 637 | // For SPARC v9, we sign-extend the given operand using SLL; SRA/SRL. |
| 638 | inline void |
| 639 | CreateBitExtensionInstructions(bool signExtend, |
| 640 | const TargetMachine& target, |
| 641 | Function* F, |
| 642 | Value* srcVal, |
Vikram S. Adve | 5cedede | 2002-09-27 14:29:45 +0000 | [diff] [blame^] | 643 | Value* destVal, |
| 644 | unsigned int numLowBits, |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 645 | vector<MachineInstr*>& mvec, |
| 646 | MachineCodeForInstruction& mcfi) |
| 647 | { |
| 648 | MachineInstr* M; |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 649 | |
Vikram S. Adve | 5cedede | 2002-09-27 14:29:45 +0000 | [diff] [blame^] | 650 | assert(numLowBits <= 32 && "Otherwise, nothing should be done here!"); |
| 651 | |
| 652 | if (numLowBits < 32) |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 653 | { // SLL is needed since operand size is < 32 bits. |
Vikram S. Adve | 5cedede | 2002-09-27 14:29:45 +0000 | [diff] [blame^] | 654 | TmpInstruction *tmpI = new TmpInstruction(destVal->getType(), |
| 655 | srcVal, destVal, "make32"); |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 656 | mcfi.addTemp(tmpI); |
Vikram S. Adve | 5cedede | 2002-09-27 14:29:45 +0000 | [diff] [blame^] | 657 | M = Create3OperandInstr_UImmed(SLLX, srcVal, 32-numLowBits, tmpI); |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 658 | mvec.push_back(M); |
| 659 | srcVal = tmpI; |
| 660 | } |
| 661 | |
| 662 | M = Create3OperandInstr_UImmed(signExtend? SRA : SRL, |
Vikram S. Adve | 5cedede | 2002-09-27 14:29:45 +0000 | [diff] [blame^] | 663 | srcVal, 32-numLowBits, destVal); |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 664 | mvec.push_back(M); |
| 665 | } |
| 666 | |
| 667 | |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 668 | // Create instruction sequence to produce a sign-extended register value |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 669 | // from an arbitrary-sized integer value (sized in bits, not bytes). |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 670 | // The generated instructions are returned in `mvec'. |
| 671 | // Any temp. registers (TmpInstruction) created are recorded in mcfi. |
| 672 | // Any stack space required is allocated via MachineCodeForMethod. |
| 673 | // |
| 674 | void |
| 675 | UltraSparcInstrInfo::CreateSignExtensionInstructions( |
| 676 | const TargetMachine& target, |
| 677 | Function* F, |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 678 | Value* srcVal, |
Vikram S. Adve | 5cedede | 2002-09-27 14:29:45 +0000 | [diff] [blame^] | 679 | Value* destVal, |
| 680 | unsigned int numLowBits, |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 681 | vector<MachineInstr*>& mvec, |
| 682 | MachineCodeForInstruction& mcfi) const |
| 683 | { |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 684 | CreateBitExtensionInstructions(/*signExtend*/ true, target, F, srcVal, |
Vikram S. Adve | 5cedede | 2002-09-27 14:29:45 +0000 | [diff] [blame^] | 685 | destVal, numLowBits, mvec, mcfi); |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | |
| 689 | // Create instruction sequence to produce a zero-extended register value |
| 690 | // from an arbitrary-sized integer value (sized in bits, not bytes). |
| 691 | // For SPARC v9, we sign-extend the given operand using SLL; SRL. |
| 692 | // The generated instructions are returned in `mvec'. |
| 693 | // Any temp. registers (TmpInstruction) created are recorded in mcfi. |
| 694 | // Any stack space required is allocated via MachineCodeForMethod. |
| 695 | // |
| 696 | void |
| 697 | UltraSparcInstrInfo::CreateZeroExtensionInstructions( |
| 698 | const TargetMachine& target, |
| 699 | Function* F, |
| 700 | Value* srcVal, |
Vikram S. Adve | 5cedede | 2002-09-27 14:29:45 +0000 | [diff] [blame^] | 701 | Value* destVal, |
| 702 | unsigned int numLowBits, |
Vikram S. Adve | 84c0fcb | 2002-09-05 18:33:59 +0000 | [diff] [blame] | 703 | vector<MachineInstr*>& mvec, |
| 704 | MachineCodeForInstruction& mcfi) const |
| 705 | { |
| 706 | CreateBitExtensionInstructions(/*signExtend*/ false, target, F, srcVal, |
Vikram S. Adve | 5cedede | 2002-09-27 14:29:45 +0000 | [diff] [blame^] | 707 | destVal, numLowBits, mvec, mcfi); |
Vikram S. Adve | b9c3863 | 2001-11-08 04:57:53 +0000 | [diff] [blame] | 708 | } |