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" |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame^] | 16 | #include "llvm/CodeGen/MachineInstr.h" |
| 17 | #include "llvm/CodeGen/MachineInstrAnnot.h" |
Chris Lattner | fb3b1ec | 2002-02-03 07:39:06 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineCodeForInstruction.h" |
| 19 | #include "llvm/CodeGen/MachineCodeForMethod.h" |
| 20 | #include "llvm/CodeGen/InstrForest.h" |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetMachine.h" |
| 22 | #include "llvm/Target/MachineRegInfo.h" |
Chris Lattner | 31bcdb8 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 23 | #include "llvm/Constants.h" |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 24 | #include "llvm/Function.h" |
Vikram S. Adve | 94e40ef | 2001-10-28 21:46:23 +0000 | [diff] [blame] | 25 | #include "llvm/BasicBlock.h" |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 26 | #include "llvm/Type.h" |
| 27 | #include "llvm/iMemory.h" |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 28 | using std::vector; |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 29 | |
| 30 | //*************************** Local Functions ******************************/ |
| 31 | |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 32 | |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame^] | 33 | // Generate code to load the constant into a TmpInstruction (virtual reg) and |
| 34 | // returns the virtual register. |
| 35 | // |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 36 | static TmpInstruction* |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 37 | InsertCodeToLoadConstant(Function *F, |
Vikram S. Adve | 42f6320 | 2002-03-18 03:33:43 +0000 | [diff] [blame] | 38 | Value* opValue, |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 39 | Instruction* vmInstr, |
| 40 | vector<MachineInstr*>& loadConstVec, |
| 41 | TargetMachine& target) |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 42 | { |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 43 | vector<TmpInstruction*> tempVec; |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 44 | |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 45 | // Create a tmp virtual register to hold the constant. |
Chris Lattner | fb3b1ec | 2002-02-03 07:39:06 +0000 | [diff] [blame] | 46 | TmpInstruction* tmpReg = new TmpInstruction(opValue); |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame^] | 47 | MachineCodeForInstruction &mcfi = MachineCodeForInstruction::get(vmInstr); |
| 48 | mcfi.addTemp(tmpReg); |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 49 | |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame^] | 50 | target.getInstrInfo().CreateCodeToLoadConst(target, F, opValue, tmpReg, |
| 51 | loadConstVec, mcfi); |
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 | } |
Vikram S. Adve | 1792779 | 2002-03-31 18:56:51 +0000 | [diff] [blame] | 100 | |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 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: |
Vikram S. Adve | c941b87 | 2002-03-24 03:37:53 +0000 | [diff] [blame] | 110 | // Fold a chain of GetElementPtr instructions containing only |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame^] | 111 | // constant offsets into an equivalent (Pointer, IndexVector) pair. |
Vikram S. Adve | c941b87 | 2002-03-24 03:37:53 +0000 | [diff] [blame] | 112 | // Returns the pointer Value, and stores the resulting IndexVector |
| 113 | // in argument chainIdxVec. |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 114 | //--------------------------------------------------------------------------- |
| 115 | |
| 116 | Value* |
| 117 | FoldGetElemChain(const InstructionNode* getElemInstrNode, |
Vikram S. Adve | fa24897 | 2001-12-15 00:36:32 +0000 | [diff] [blame] | 118 | vector<Value*>& chainIdxVec) |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 119 | { |
| 120 | MemAccessInst* getElemInst = (MemAccessInst*) |
| 121 | getElemInstrNode->getInstruction(); |
| 122 | |
Vikram S. Adve | 1792779 | 2002-03-31 18:56:51 +0000 | [diff] [blame] | 123 | // Return NULL if we don't fold any instructions in. |
Vikram S. Adve | c941b87 | 2002-03-24 03:37:53 +0000 | [diff] [blame] | 124 | Value* ptrVal = NULL; |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame^] | 125 | |
| 126 | // Remember if the last instruction had a leading [0] index. |
| 127 | bool hasLeadingZero = false; |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 128 | |
Vikram S. Adve | c941b87 | 2002-03-24 03:37:53 +0000 | [diff] [blame] | 129 | // Now chase the chain of getElementInstr instructions, if any. |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame^] | 130 | // Check for any non-constant indices and stop there. |
Vikram S. Adve | c941b87 | 2002-03-24 03:37:53 +0000 | [diff] [blame] | 131 | // |
| 132 | const InstrTreeNode* ptrChild = getElemInstrNode; |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 133 | while (ptrChild->getOpLabel() == Instruction::GetElementPtr || |
| 134 | ptrChild->getOpLabel() == GetElemPtrIdx) |
| 135 | { |
| 136 | // Child is a GetElemPtr instruction |
| 137 | getElemInst = (MemAccessInst*) |
| 138 | ((InstructionNode*) ptrChild)->getInstruction(); |
Vikram S. Adve | fa24897 | 2001-12-15 00:36:32 +0000 | [diff] [blame] | 139 | const vector<Value*>& idxVec = getElemInst->copyIndices(); |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame^] | 140 | bool allConstantOffsets = true; |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 141 | |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame^] | 142 | // Check for a leading [0] index, if any. It will be discarded later. |
| 143 | ConstantUInt* CV = dyn_cast<ConstantUInt>(idxVec[0]); |
| 144 | hasLeadingZero = bool(CV && CV->getType() == Type::UIntTy && |
| 145 | (CV->getValue() == 0)); |
Vikram S. Adve | c941b87 | 2002-03-24 03:37:53 +0000 | [diff] [blame] | 146 | |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame^] | 147 | // Check that all offsets are constant for this instruction |
| 148 | for (unsigned int i=0; i < idxVec.size(); i++) |
| 149 | if (! isa<ConstantUInt>(idxVec[i])) |
| 150 | { |
| 151 | allConstantOffsets = false; |
| 152 | break; |
| 153 | } |
Vikram S. Adve | c941b87 | 2002-03-24 03:37:53 +0000 | [diff] [blame] | 154 | |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame^] | 155 | if (allConstantOffsets) |
Vikram S. Adve | 1792779 | 2002-03-31 18:56:51 +0000 | [diff] [blame] | 156 | { // Get pointer value out of ptrChild. |
Vikram S. Adve | c941b87 | 2002-03-24 03:37:53 +0000 | [diff] [blame] | 157 | ptrVal = getElemInst->getPointerOperand(); |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame^] | 158 | |
| 159 | // Insert its index vector at the start. |
| 160 | chainIdxVec.insert(chainIdxVec.begin(), |
| 161 | idxVec.begin() + (hasLeadingZero? 1:0), |
| 162 | idxVec.end()); |
Vikram S. Adve | 1792779 | 2002-03-31 18:56:51 +0000 | [diff] [blame] | 163 | |
| 164 | // Mark the folded node so no code is generated for it. |
Vikram S. Adve | c941b87 | 2002-03-24 03:37:53 +0000 | [diff] [blame] | 165 | ((InstructionNode*) ptrChild)->markFoldedIntoParent(); |
Vikram S. Adve | c941b87 | 2002-03-24 03:37:53 +0000 | [diff] [blame] | 166 | } |
| 167 | else // cannot fold this getElementPtr instr. or any further ones |
| 168 | break; |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 169 | |
| 170 | ptrChild = ptrChild->leftChild(); |
| 171 | } |
| 172 | |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame^] | 173 | // If the first getElementPtr instruction had a leading [0], add it back. |
| 174 | // Note that this instruction is the *last* one handled above. |
| 175 | if (hasLeadingZero) |
| 176 | chainIdxVec.insert(chainIdxVec.begin(), ConstantUInt::get(Type::UIntTy,0)); |
| 177 | |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 178 | return ptrVal; |
| 179 | } |
| 180 | |
| 181 | |
| 182 | //------------------------------------------------------------------------ |
| 183 | // Function Set2OperandsFromInstr |
| 184 | // Function Set3OperandsFromInstr |
| 185 | // |
| 186 | // For the common case of 2- and 3-operand arithmetic/logical instructions, |
| 187 | // set the m/c instr. operands directly from the VM instruction's operands. |
| 188 | // Check whether the first or second operand is 0 and can use a dedicated "0" |
| 189 | // register. |
| 190 | // Check whether the second operand should use an immediate field or register. |
| 191 | // (First and third operands are never immediates for such instructions.) |
| 192 | // |
| 193 | // Arguments: |
| 194 | // canDiscardResult: Specifies that the result operand can be discarded |
| 195 | // by using the dedicated "0" |
| 196 | // |
| 197 | // op1position, op2position and resultPosition: Specify in which position |
| 198 | // in the machine instruction the 3 operands (arg1, arg2 |
| 199 | // and result) should go. |
| 200 | // |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 201 | //------------------------------------------------------------------------ |
| 202 | |
| 203 | void |
| 204 | Set2OperandsFromInstr(MachineInstr* minstr, |
| 205 | InstructionNode* vmInstrNode, |
| 206 | const TargetMachine& target, |
| 207 | bool canDiscardResult, |
| 208 | int op1Position, |
| 209 | int resultPosition) |
| 210 | { |
| 211 | Set3OperandsFromInstr(minstr, vmInstrNode, target, |
| 212 | canDiscardResult, op1Position, |
| 213 | /*op2Position*/ -1, resultPosition); |
| 214 | } |
| 215 | |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 216 | |
| 217 | void |
| 218 | Set3OperandsFromInstr(MachineInstr* minstr, |
| 219 | InstructionNode* vmInstrNode, |
| 220 | const TargetMachine& target, |
| 221 | bool canDiscardResult, |
| 222 | int op1Position, |
| 223 | int op2Position, |
| 224 | int resultPosition) |
| 225 | { |
| 226 | assert(op1Position >= 0); |
| 227 | assert(resultPosition >= 0); |
| 228 | |
| 229 | // operand 1 |
Vikram S. Adve | 42f6320 | 2002-03-18 03:33:43 +0000 | [diff] [blame] | 230 | minstr->SetMachineOperandVal(op1Position, MachineOperand::MO_VirtualRegister, |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 231 | vmInstrNode->leftChild()->getValue()); |
| 232 | |
| 233 | // operand 2 (if any) |
| 234 | if (op2Position >= 0) |
Vikram S. Adve | 42f6320 | 2002-03-18 03:33:43 +0000 | [diff] [blame] | 235 | minstr->SetMachineOperandVal(op2Position, MachineOperand::MO_VirtualRegister, |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 236 | vmInstrNode->rightChild()->getValue()); |
| 237 | |
| 238 | // result operand: if it can be discarded, use a dead register if one exists |
| 239 | if (canDiscardResult && target.getRegInfo().getZeroRegNum() >= 0) |
Vikram S. Adve | 42f6320 | 2002-03-18 03:33:43 +0000 | [diff] [blame] | 240 | minstr->SetMachineOperandReg(resultPosition, |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 241 | target.getRegInfo().getZeroRegNum()); |
| 242 | else |
Vikram S. Adve | 42f6320 | 2002-03-18 03:33:43 +0000 | [diff] [blame] | 243 | minstr->SetMachineOperandVal(resultPosition, |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 244 | MachineOperand::MO_VirtualRegister, vmInstrNode->getValue()); |
| 245 | } |
| 246 | |
| 247 | |
| 248 | MachineOperand::MachineOperandType |
| 249 | ChooseRegOrImmed(Value* val, |
| 250 | MachineOpCode opCode, |
| 251 | const TargetMachine& target, |
| 252 | bool canUseImmed, |
| 253 | unsigned int& getMachineRegNum, |
| 254 | int64_t& getImmedValue) |
| 255 | { |
| 256 | MachineOperand::MachineOperandType opType = |
| 257 | MachineOperand::MO_VirtualRegister; |
| 258 | getMachineRegNum = 0; |
| 259 | getImmedValue = 0; |
| 260 | |
| 261 | // Check for the common case first: argument is not constant |
| 262 | // |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 263 | Constant *CPV = dyn_cast<Constant>(val); |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 264 | if (!CPV) return opType; |
| 265 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 266 | if (ConstantBool *CPB = dyn_cast<ConstantBool>(CPV)) |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 267 | { |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 268 | if (!CPB->getValue() && target.getRegInfo().getZeroRegNum() >= 0) |
| 269 | { |
| 270 | getMachineRegNum = target.getRegInfo().getZeroRegNum(); |
| 271 | return MachineOperand::MO_MachineRegister; |
| 272 | } |
| 273 | |
| 274 | getImmedValue = 1; |
| 275 | return MachineOperand::MO_SignExtendedImmed; |
| 276 | } |
| 277 | |
Vikram S. Adve | c811745 | 2001-11-14 17:24:49 +0000 | [diff] [blame] | 278 | // Otherwise it needs to be an integer or a NULL pointer |
| 279 | if (! CPV->getType()->isIntegral() && |
Chris Lattner | 9b62503 | 2002-05-06 16:15:30 +0000 | [diff] [blame] | 280 | ! (isa<PointerType>(CPV->getType()) && |
Vikram S. Adve | c811745 | 2001-11-14 17:24:49 +0000 | [diff] [blame] | 281 | CPV->isNullValue())) |
| 282 | return opType; |
| 283 | |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 284 | // Now get the constant value and check if it fits in the IMMED field. |
| 285 | // Take advantage of the fact that the max unsigned value will rarely |
| 286 | // fit into any IMMED field and ignore that case (i.e., cast smaller |
| 287 | // unsigned constants to signed). |
| 288 | // |
| 289 | int64_t intValue; |
Chris Lattner | 9b62503 | 2002-05-06 16:15:30 +0000 | [diff] [blame] | 290 | if (isa<PointerType>(CPV->getType())) |
Vikram S. Adve | c811745 | 2001-11-14 17:24:49 +0000 | [diff] [blame] | 291 | { |
| 292 | intValue = 0; |
| 293 | } |
Vikram S. Adve | 9e29f78 | 2001-11-14 17:55:02 +0000 | [diff] [blame] | 294 | else if (CPV->getType()->isSigned()) |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 295 | { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 296 | intValue = cast<ConstantSInt>(CPV)->getValue(); |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 297 | } |
| 298 | else |
| 299 | { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 300 | uint64_t V = cast<ConstantUInt>(CPV)->getValue(); |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 301 | if (V >= INT64_MAX) return opType; |
| 302 | intValue = (int64_t)V; |
| 303 | } |
| 304 | |
| 305 | if (intValue == 0 && target.getRegInfo().getZeroRegNum() >= 0) |
| 306 | { |
| 307 | opType = MachineOperand::MO_MachineRegister; |
| 308 | getMachineRegNum = target.getRegInfo().getZeroRegNum(); |
| 309 | } |
| 310 | else if (canUseImmed && |
| 311 | target.getInstrInfo().constantFitsInImmedField(opCode, intValue)) |
| 312 | { |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame^] | 313 | opType = CPV->getType()->isSigned() |
| 314 | ? MachineOperand::MO_SignExtendedImmed |
| 315 | : MachineOperand::MO_UnextendedImmed; |
Vikram S. Adve | a1d14f3 | 2001-10-10 20:50:43 +0000 | [diff] [blame] | 316 | getImmedValue = intValue; |
| 317 | } |
| 318 | |
| 319 | return opType; |
| 320 | } |
| 321 | |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 322 | |
| 323 | //--------------------------------------------------------------------------- |
| 324 | // Function: FixConstantOperandsForInstr |
| 325 | // |
| 326 | // Purpose: |
| 327 | // Special handling for constant operands of a machine instruction |
| 328 | // -- if the constant is 0, use the hardwired 0 register, if any; |
| 329 | // -- if the constant fits in the IMMEDIATE field, use that field; |
| 330 | // -- else create instructions to put the constant into a register, either |
| 331 | // directly or by loading explicitly from the constant pool. |
| 332 | // |
| 333 | // In the first 2 cases, the operand of `minstr' is modified in place. |
| 334 | // Returns a vector of machine instructions generated for operands that |
| 335 | // fall under case 3; these must be inserted before `minstr'. |
| 336 | //--------------------------------------------------------------------------- |
| 337 | |
| 338 | vector<MachineInstr*> |
| 339 | FixConstantOperandsForInstr(Instruction* vmInstr, |
| 340 | MachineInstr* minstr, |
| 341 | TargetMachine& target) |
| 342 | { |
| 343 | vector<MachineInstr*> loadConstVec; |
| 344 | |
| 345 | const MachineInstrDescriptor& instrDesc = |
| 346 | target.getInstrInfo().getDescriptor(minstr->getOpCode()); |
| 347 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 348 | Function *F = vmInstr->getParent()->getParent(); |
Vikram S. Adve | 94e40ef | 2001-10-28 21:46:23 +0000 | [diff] [blame] | 349 | |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 350 | for (unsigned op=0; op < minstr->getNumOperands(); op++) |
| 351 | { |
| 352 | const MachineOperand& mop = minstr->getOperand(op); |
| 353 | |
| 354 | // skip the result position (for efficiency below) and any other |
| 355 | // positions already marked as not a virtual register |
| 356 | if (instrDesc.resultPos == (int) op || |
| 357 | mop.getOperandType() != MachineOperand::MO_VirtualRegister || |
| 358 | mop.getVRegValue() == NULL) |
| 359 | { |
| 360 | continue; |
| 361 | } |
| 362 | |
| 363 | Value* opValue = mop.getVRegValue(); |
| 364 | bool constantThatMustBeLoaded = false; |
| 365 | |
Vikram S. Adve | 42f6320 | 2002-03-18 03:33:43 +0000 | [diff] [blame] | 366 | if (Constant *opConst = dyn_cast<Constant>(opValue)) |
| 367 | { |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 368 | unsigned int machineRegNum; |
| 369 | int64_t immedValue; |
| 370 | MachineOperand::MachineOperandType opType = |
| 371 | ChooseRegOrImmed(opValue, minstr->getOpCode(), target, |
Vikram S. Adve | 42f6320 | 2002-03-18 03:33:43 +0000 | [diff] [blame] | 372 | (target.getInstrInfo().getImmedConstantPos(minstr->getOpCode()) == (int) op), |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 373 | machineRegNum, immedValue); |
Vikram S. Adve | ecd5813 | 2001-11-14 18:49:45 +0000 | [diff] [blame] | 374 | |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 375 | if (opType == MachineOperand::MO_MachineRegister) |
Vikram S. Adve | 42f6320 | 2002-03-18 03:33:43 +0000 | [diff] [blame] | 376 | minstr->SetMachineOperandReg(op, machineRegNum); |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 377 | else if (opType == MachineOperand::MO_VirtualRegister) |
| 378 | constantThatMustBeLoaded = true; // load is generated below |
| 379 | else |
Vikram S. Adve | 42f6320 | 2002-03-18 03:33:43 +0000 | [diff] [blame] | 380 | minstr->SetMachineOperandConst(op, opType, immedValue); |
Vikram S. Adve | 94e40ef | 2001-10-28 21:46:23 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 383 | if (constantThatMustBeLoaded || isa<GlobalValue>(opValue)) |
| 384 | { // opValue is a constant that must be explicitly loaded into a reg. |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame^] | 385 | TmpInstruction* tmpReg = InsertCodeToLoadConstant(F, opValue,vmInstr, |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 386 | loadConstVec, |
| 387 | target); |
Vikram S. Adve | 42f6320 | 2002-03-18 03:33:43 +0000 | [diff] [blame] | 388 | minstr->SetMachineOperandVal(op, MachineOperand::MO_VirtualRegister, |
| 389 | tmpReg); |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
| 393 | // |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame^] | 394 | // Also, check for implicit operands used by the machine instruction |
| 395 | // (no need to check those defined since they cannot be constants). |
| 396 | // These include: |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 397 | // -- arguments to a Call |
| 398 | // -- return value of a Return |
| 399 | // Any such operand that is a constant value needs to be fixed also. |
| 400 | // The current instructions with implicit refs (viz., Call and Return) |
| 401 | // have no immediate fields, so the constant always needs to be loaded |
| 402 | // into a register. |
| 403 | // |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame^] | 404 | bool isCall = target.getInstrInfo().isCall(minstr->getOpCode()); |
| 405 | unsigned lastCallArgNum = 0; // unused if not a call |
| 406 | CallArgsDescriptor* argDesc = NULL; // unused if not a call |
| 407 | if (isCall) |
| 408 | argDesc = CallArgsDescriptor::get(minstr); |
| 409 | |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 410 | for (unsigned i=0, N=minstr->getNumImplicitRefs(); i < N; ++i) |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 411 | if (isa<Constant>(minstr->getImplicitRef(i)) || |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 412 | isa<GlobalValue>(minstr->getImplicitRef(i))) |
| 413 | { |
Vikram S. Adve | 94e40ef | 2001-10-28 21:46:23 +0000 | [diff] [blame] | 414 | Value* oldVal = minstr->getImplicitRef(i); |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 415 | TmpInstruction* tmpReg = |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 416 | InsertCodeToLoadConstant(F, oldVal, vmInstr, loadConstVec, target); |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 417 | minstr->setImplicitRef(i, tmpReg); |
Vikram S. Adve | 36f0a9e | 2002-05-19 15:34:29 +0000 | [diff] [blame^] | 418 | |
| 419 | if (isCall) |
| 420 | { // find and replace the argument in the CallArgsDescriptor |
| 421 | unsigned i=lastCallArgNum; |
| 422 | while (argDesc->getArgInfo(i).getArgVal() != oldVal) |
| 423 | ++i; |
| 424 | assert(i < argDesc->getNumArgs() && |
| 425 | "Constant operands to a call *must* be in the arg list"); |
| 426 | lastCallArgNum = i; |
| 427 | argDesc->getArgInfo(i).replaceArgVal(tmpReg); |
| 428 | } |
Vikram S. Adve | 6d35326 | 2001-10-17 23:57:50 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | return loadConstVec; |
| 432 | } |
| 433 | |
| 434 | |