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