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