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