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