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