Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 1 | // $Id$ -*-c++-*- |
| 2 | //*************************************************************************** |
| 3 | // File: |
| 4 | // InstrSelectionSupport.h |
| 5 | // |
| 6 | // Purpose: |
| 7 | // Target-independent instruction selection code. |
| 8 | // See SparcInstrSelection.cpp for usage. |
| 9 | // |
| 10 | // History: |
| 11 | // 10/10/01 - Vikram Adve - Created |
| 12 | //**************************************************************************/ |
| 13 | |
| 14 | #include "llvm/CodeGen/InstrSelectionSupport.h" |
| 15 | #include "llvm/CodeGen/InstrSelection.h" |
| 16 | #include "llvm/CodeGen/MachineInstr.h" |
Chris Lattner | fb3b1ec | 2002-02-03 07:39:06 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineCodeForInstruction.h" |
| 18 | #include "llvm/CodeGen/MachineCodeForMethod.h" |
| 19 | #include "llvm/CodeGen/InstrForest.h" |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 20 | #include "llvm/Target/TargetMachine.h" |
| 21 | #include "llvm/Target/MachineRegInfo.h" |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 22 | #include "llvm/ConstantVals.h" |
Vikram S. Adve | 94e40ef | 2001-10-28 21:46:23 +0000 | [diff] [blame] | 23 | #include "llvm/Method.h" |
| 24 | #include "llvm/BasicBlock.h" |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 25 | #include "llvm/Type.h" |
| 26 | #include "llvm/iMemory.h" |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 27 | using std::vector; |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 28 | |
| 29 | //*************************** Local Functions ******************************/ |
| 30 | |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 31 | |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 32 | static TmpInstruction* |
Vikram S. Adve | 42f6320 | 2002-03-18 03:33:43 +0000 | [diff] [blame^] | 33 | InsertCodeToLoadConstant(Method* method, |
| 34 | Value* opValue, |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 35 | Instruction* vmInstr, |
| 36 | vector<MachineInstr*>& loadConstVec, |
| 37 | TargetMachine& target) |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 38 | { |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 39 | vector<TmpInstruction*> tempVec; |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 40 | |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 41 | // Create a tmp virtual register to hold the constant. |
Chris Lattner | fb3b1ec | 2002-02-03 07:39:06 +0000 | [diff] [blame] | 42 | TmpInstruction* tmpReg = new TmpInstruction(opValue); |
| 43 | MachineCodeForInstruction &MCFI = MachineCodeForInstruction::get(vmInstr); |
| 44 | MCFI.addTemp(tmpReg); |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 45 | |
Vikram S. Adve | 42f6320 | 2002-03-18 03:33:43 +0000 | [diff] [blame^] | 46 | target.getInstrInfo().CreateCodeToLoadConst(method, opValue, tmpReg, |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 47 | loadConstVec, tempVec); |
| 48 | |
| 49 | // Register the new tmp values created for this m/c instruction sequence |
| 50 | for (unsigned i=0; i < tempVec.size(); i++) |
Chris Lattner | fb3b1ec | 2002-02-03 07:39:06 +0000 | [diff] [blame] | 51 | MCFI.addTemp(tempVec[i]); |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 52 | |
| 53 | // Record the mapping from the tmp VM instruction to machine instruction. |
| 54 | // Do this for all machine instructions that were not mapped to any |
| 55 | // other temp values created by |
| 56 | // tmpReg->addMachineInstruction(loadConstVec.back()); |
| 57 | |
| 58 | return tmpReg; |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 62 | //--------------------------------------------------------------------------- |
| 63 | // Function GetConstantValueAsSignedInt |
| 64 | // |
| 65 | // Convenience function to get the value of an integer constant, for an |
| 66 | // appropriate integer or non-integer type that can be held in an integer. |
| 67 | // The type of the argument must be the following: |
| 68 | // Signed or unsigned integer |
| 69 | // Boolean |
| 70 | // Pointer |
| 71 | // |
| 72 | // isValidConstant is set to true if a valid constant was found. |
| 73 | //--------------------------------------------------------------------------- |
| 74 | |
| 75 | int64_t |
| 76 | GetConstantValueAsSignedInt(const Value *V, |
| 77 | bool &isValidConstant) |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 78 | { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 79 | if (!isa<Constant>(V)) |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 80 | { |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 81 | isValidConstant = false; |
| 82 | return 0; |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 85 | isValidConstant = true; |
| 86 | |
| 87 | if (V->getType() == Type::BoolTy) |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 88 | return (int64_t) cast<ConstantBool>(V)->getValue(); |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 89 | |
| 90 | if (V->getType()->isIntegral()) |
| 91 | { |
| 92 | if (V->getType()->isSigned()) |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 93 | return cast<ConstantSInt>(V)->getValue(); |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 94 | |
| 95 | assert(V->getType()->isUnsigned()); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 96 | uint64_t Val = cast<ConstantUInt>(V)->getValue(); |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 97 | if (Val < INT64_MAX) // then safe to cast to signed |
| 98 | return (int64_t)Val; |
| 99 | } |
| 100 | |
| 101 | isValidConstant = false; |
| 102 | return 0; |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | |
| 106 | //--------------------------------------------------------------------------- |
| 107 | // Function: FoldGetElemChain |
| 108 | // |
| 109 | // Purpose: |
| 110 | // Fold a chain of GetElementPtr instructions into an equivalent |
| 111 | // (Pointer, IndexVector) pair. Returns the pointer Value, and |
| 112 | // stores the resulting IndexVector in argument chainIdxVec. |
| 113 | //--------------------------------------------------------------------------- |
| 114 | |
| 115 | Value* |
| 116 | FoldGetElemChain(const InstructionNode* getElemInstrNode, |
Vikram S. Adve | fa24897 | 2001-12-15 00:36:32 +0000 | [diff] [blame] | 117 | vector<Value*>& chainIdxVec) |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 118 | { |
| 119 | MemAccessInst* getElemInst = (MemAccessInst*) |
| 120 | getElemInstrNode->getInstruction(); |
| 121 | |
| 122 | // Initialize return values from the incoming instruction |
Chris Lattner | 65ea171 | 2001-11-14 11:27:58 +0000 | [diff] [blame] | 123 | Value* ptrVal = getElemInst->getPointerOperand(); |
Vikram S. Adve | fa24897 | 2001-12-15 00:36:32 +0000 | [diff] [blame] | 124 | chainIdxVec = getElemInst->copyIndices(); |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 125 | |
| 126 | // Now chase the chain of getElementInstr instructions, if any |
| 127 | InstrTreeNode* ptrChild = getElemInstrNode->leftChild(); |
| 128 | while (ptrChild->getOpLabel() == Instruction::GetElementPtr || |
| 129 | ptrChild->getOpLabel() == GetElemPtrIdx) |
| 130 | { |
| 131 | // Child is a GetElemPtr instruction |
| 132 | getElemInst = (MemAccessInst*) |
| 133 | ((InstructionNode*) ptrChild)->getInstruction(); |
Vikram S. Adve | fa24897 | 2001-12-15 00:36:32 +0000 | [diff] [blame] | 134 | const vector<Value*>& idxVec = getElemInst->copyIndices(); |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 135 | |
| 136 | // Get the pointer value out of ptrChild and *prepend* its index vector |
Chris Lattner | 65ea171 | 2001-11-14 11:27:58 +0000 | [diff] [blame] | 137 | ptrVal = getElemInst->getPointerOperand(); |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 138 | chainIdxVec.insert(chainIdxVec.begin(), idxVec.begin(), idxVec.end()); |
| 139 | |
| 140 | ptrChild = ptrChild->leftChild(); |
| 141 | } |
| 142 | |
| 143 | return ptrVal; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | //------------------------------------------------------------------------ |
| 148 | // Function Set2OperandsFromInstr |
| 149 | // Function Set3OperandsFromInstr |
| 150 | // |
| 151 | // For the common case of 2- and 3-operand arithmetic/logical instructions, |
| 152 | // set the m/c instr. operands directly from the VM instruction's operands. |
| 153 | // Check whether the first or second operand is 0 and can use a dedicated "0" |
| 154 | // register. |
| 155 | // Check whether the second operand should use an immediate field or register. |
| 156 | // (First and third operands are never immediates for such instructions.) |
| 157 | // |
| 158 | // Arguments: |
| 159 | // canDiscardResult: Specifies that the result operand can be discarded |
| 160 | // by using the dedicated "0" |
| 161 | // |
| 162 | // op1position, op2position and resultPosition: Specify in which position |
| 163 | // in the machine instruction the 3 operands (arg1, arg2 |
| 164 | // and result) should go. |
| 165 | // |
| 166 | // RETURN VALUE: unsigned int flags, where |
| 167 | // flags & 0x01 => operand 1 is constant and needs a register |
| 168 | // flags & 0x02 => operand 2 is constant and needs a register |
| 169 | //------------------------------------------------------------------------ |
| 170 | |
| 171 | void |
| 172 | Set2OperandsFromInstr(MachineInstr* minstr, |
| 173 | InstructionNode* vmInstrNode, |
| 174 | const TargetMachine& target, |
| 175 | bool canDiscardResult, |
| 176 | int op1Position, |
| 177 | int resultPosition) |
| 178 | { |
| 179 | Set3OperandsFromInstr(minstr, vmInstrNode, target, |
| 180 | canDiscardResult, op1Position, |
| 181 | /*op2Position*/ -1, resultPosition); |
| 182 | } |
| 183 | |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 184 | |
| 185 | void |
| 186 | Set3OperandsFromInstr(MachineInstr* minstr, |
| 187 | InstructionNode* vmInstrNode, |
| 188 | const TargetMachine& target, |
| 189 | bool canDiscardResult, |
| 190 | int op1Position, |
| 191 | int op2Position, |
| 192 | int resultPosition) |
| 193 | { |
| 194 | assert(op1Position >= 0); |
| 195 | assert(resultPosition >= 0); |
| 196 | |
| 197 | // operand 1 |
Vikram S. Adve | 42f6320 | 2002-03-18 03:33:43 +0000 | [diff] [blame^] | 198 | minstr->SetMachineOperandVal(op1Position, MachineOperand::MO_VirtualRegister, |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 199 | vmInstrNode->leftChild()->getValue()); |
| 200 | |
| 201 | // operand 2 (if any) |
| 202 | if (op2Position >= 0) |
Vikram S. Adve | 42f6320 | 2002-03-18 03:33:43 +0000 | [diff] [blame^] | 203 | minstr->SetMachineOperandVal(op2Position, MachineOperand::MO_VirtualRegister, |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 204 | vmInstrNode->rightChild()->getValue()); |
| 205 | |
| 206 | // result operand: if it can be discarded, use a dead register if one exists |
| 207 | if (canDiscardResult && target.getRegInfo().getZeroRegNum() >= 0) |
Vikram S. Adve | 42f6320 | 2002-03-18 03:33:43 +0000 | [diff] [blame^] | 208 | minstr->SetMachineOperandReg(resultPosition, |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 209 | target.getRegInfo().getZeroRegNum()); |
| 210 | else |
Vikram S. Adve | 42f6320 | 2002-03-18 03:33:43 +0000 | [diff] [blame^] | 211 | minstr->SetMachineOperandVal(resultPosition, |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 212 | MachineOperand::MO_VirtualRegister, vmInstrNode->getValue()); |
| 213 | } |
| 214 | |
| 215 | |
| 216 | MachineOperand::MachineOperandType |
| 217 | ChooseRegOrImmed(Value* val, |
| 218 | MachineOpCode opCode, |
| 219 | const TargetMachine& target, |
| 220 | bool canUseImmed, |
| 221 | unsigned int& getMachineRegNum, |
| 222 | int64_t& getImmedValue) |
| 223 | { |
| 224 | MachineOperand::MachineOperandType opType = |
| 225 | MachineOperand::MO_VirtualRegister; |
| 226 | getMachineRegNum = 0; |
| 227 | getImmedValue = 0; |
| 228 | |
| 229 | // Check for the common case first: argument is not constant |
| 230 | // |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 231 | Constant *CPV = dyn_cast<Constant>(val); |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 232 | if (!CPV) return opType; |
| 233 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 234 | if (ConstantBool *CPB = dyn_cast<ConstantBool>(CPV)) |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 235 | { |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 236 | if (!CPB->getValue() && target.getRegInfo().getZeroRegNum() >= 0) |
| 237 | { |
| 238 | getMachineRegNum = target.getRegInfo().getZeroRegNum(); |
| 239 | return MachineOperand::MO_MachineRegister; |
| 240 | } |
| 241 | |
| 242 | getImmedValue = 1; |
| 243 | return MachineOperand::MO_SignExtendedImmed; |
| 244 | } |
| 245 | |
Vikram S. Adve | c811745 | 2001-11-14 17:24:49 +0000 | [diff] [blame] | 246 | // Otherwise it needs to be an integer or a NULL pointer |
| 247 | if (! CPV->getType()->isIntegral() && |
| 248 | ! (CPV->getType()->isPointerType() && |
| 249 | CPV->isNullValue())) |
| 250 | return opType; |
| 251 | |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 252 | // Now get the constant value and check if it fits in the IMMED field. |
| 253 | // Take advantage of the fact that the max unsigned value will rarely |
| 254 | // fit into any IMMED field and ignore that case (i.e., cast smaller |
| 255 | // unsigned constants to signed). |
| 256 | // |
| 257 | int64_t intValue; |
Vikram S. Adve | c811745 | 2001-11-14 17:24:49 +0000 | [diff] [blame] | 258 | if (CPV->getType()->isPointerType()) |
| 259 | { |
| 260 | intValue = 0; |
| 261 | } |
Vikram S. Adve | 9e29f78 | 2001-11-14 17:55:02 +0000 | [diff] [blame] | 262 | else if (CPV->getType()->isSigned()) |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 263 | { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 264 | intValue = cast<ConstantSInt>(CPV)->getValue(); |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 265 | } |
| 266 | else |
| 267 | { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 268 | uint64_t V = cast<ConstantUInt>(CPV)->getValue(); |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 269 | if (V >= INT64_MAX) return opType; |
| 270 | intValue = (int64_t)V; |
| 271 | } |
| 272 | |
| 273 | if (intValue == 0 && target.getRegInfo().getZeroRegNum() >= 0) |
| 274 | { |
| 275 | opType = MachineOperand::MO_MachineRegister; |
| 276 | getMachineRegNum = target.getRegInfo().getZeroRegNum(); |
| 277 | } |
| 278 | else if (canUseImmed && |
| 279 | target.getInstrInfo().constantFitsInImmedField(opCode, intValue)) |
| 280 | { |
| 281 | opType = MachineOperand::MO_SignExtendedImmed; |
| 282 | getImmedValue = intValue; |
| 283 | } |
| 284 | |
| 285 | return opType; |
| 286 | } |
| 287 | |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 288 | |
| 289 | //--------------------------------------------------------------------------- |
| 290 | // Function: FixConstantOperandsForInstr |
| 291 | // |
| 292 | // Purpose: |
| 293 | // Special handling for constant operands of a machine instruction |
| 294 | // -- if the constant is 0, use the hardwired 0 register, if any; |
| 295 | // -- if the constant fits in the IMMEDIATE field, use that field; |
| 296 | // -- else create instructions to put the constant into a register, either |
| 297 | // directly or by loading explicitly from the constant pool. |
| 298 | // |
| 299 | // In the first 2 cases, the operand of `minstr' is modified in place. |
| 300 | // Returns a vector of machine instructions generated for operands that |
| 301 | // fall under case 3; these must be inserted before `minstr'. |
| 302 | //--------------------------------------------------------------------------- |
| 303 | |
| 304 | vector<MachineInstr*> |
| 305 | FixConstantOperandsForInstr(Instruction* vmInstr, |
| 306 | MachineInstr* minstr, |
| 307 | TargetMachine& target) |
| 308 | { |
| 309 | vector<MachineInstr*> loadConstVec; |
| 310 | |
| 311 | const MachineInstrDescriptor& instrDesc = |
| 312 | target.getInstrInfo().getDescriptor(minstr->getOpCode()); |
| 313 | |
Vikram S. Adve | 94e40ef | 2001-10-28 21:46:23 +0000 | [diff] [blame] | 314 | Method* method = vmInstr->getParent()->getParent(); |
| 315 | |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 316 | for (unsigned op=0; op < minstr->getNumOperands(); op++) |
| 317 | { |
| 318 | const MachineOperand& mop = minstr->getOperand(op); |
| 319 | |
| 320 | // skip the result position (for efficiency below) and any other |
| 321 | // positions already marked as not a virtual register |
| 322 | if (instrDesc.resultPos == (int) op || |
| 323 | mop.getOperandType() != MachineOperand::MO_VirtualRegister || |
| 324 | mop.getVRegValue() == NULL) |
| 325 | { |
| 326 | continue; |
| 327 | } |
| 328 | |
| 329 | Value* opValue = mop.getVRegValue(); |
| 330 | bool constantThatMustBeLoaded = false; |
| 331 | |
Vikram S. Adve | 42f6320 | 2002-03-18 03:33:43 +0000 | [diff] [blame^] | 332 | if (Constant *opConst = dyn_cast<Constant>(opValue)) |
| 333 | { |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 334 | unsigned int machineRegNum; |
| 335 | int64_t immedValue; |
| 336 | MachineOperand::MachineOperandType opType = |
| 337 | ChooseRegOrImmed(opValue, minstr->getOpCode(), target, |
Vikram S. Adve | 42f6320 | 2002-03-18 03:33:43 +0000 | [diff] [blame^] | 338 | (target.getInstrInfo().getImmedConstantPos(minstr->getOpCode()) == (int) op), |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 339 | machineRegNum, immedValue); |
Vikram S. Adve | ecd5813 | 2001-11-14 18:49:45 +0000 | [diff] [blame] | 340 | |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 341 | if (opType == MachineOperand::MO_MachineRegister) |
Vikram S. Adve | 42f6320 | 2002-03-18 03:33:43 +0000 | [diff] [blame^] | 342 | minstr->SetMachineOperandReg(op, machineRegNum); |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 343 | else if (opType == MachineOperand::MO_VirtualRegister) |
| 344 | constantThatMustBeLoaded = true; // load is generated below |
| 345 | else |
Vikram S. Adve | 42f6320 | 2002-03-18 03:33:43 +0000 | [diff] [blame^] | 346 | minstr->SetMachineOperandConst(op, opType, immedValue); |
Vikram S. Adve | 94e40ef | 2001-10-28 21:46:23 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 349 | if (constantThatMustBeLoaded || isa<GlobalValue>(opValue)) |
| 350 | { // opValue is a constant that must be explicitly loaded into a reg. |
Vikram S. Adve | 42f6320 | 2002-03-18 03:33:43 +0000 | [diff] [blame^] | 351 | TmpInstruction* tmpReg = InsertCodeToLoadConstant(method, opValue, vmInstr, |
| 352 | loadConstVec, target); |
| 353 | minstr->SetMachineOperandVal(op, MachineOperand::MO_VirtualRegister, |
| 354 | tmpReg); |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | |
| 358 | // |
| 359 | // Also, check for implicit operands used (not those defined) by the |
| 360 | // machine instruction. These include: |
| 361 | // -- arguments to a Call |
| 362 | // -- return value of a Return |
| 363 | // Any such operand that is a constant value needs to be fixed also. |
| 364 | // The current instructions with implicit refs (viz., Call and Return) |
| 365 | // have no immediate fields, so the constant always needs to be loaded |
| 366 | // into a register. |
| 367 | // |
| 368 | for (unsigned i=0, N=minstr->getNumImplicitRefs(); i < N; ++i) |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 369 | if (isa<Constant>(minstr->getImplicitRef(i)) || |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 370 | isa<GlobalValue>(minstr->getImplicitRef(i))) |
| 371 | { |
Vikram S. Adve | 94e40ef | 2001-10-28 21:46:23 +0000 | [diff] [blame] | 372 | Value* oldVal = minstr->getImplicitRef(i); |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 373 | TmpInstruction* tmpReg = |
Vikram S. Adve | 42f6320 | 2002-03-18 03:33:43 +0000 | [diff] [blame^] | 374 | InsertCodeToLoadConstant(method, oldVal, vmInstr, loadConstVec, target); |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 375 | minstr->setImplicitRef(i, tmpReg); |
| 376 | } |
| 377 | |
| 378 | return loadConstVec; |
| 379 | } |
| 380 | |
| 381 | |