Chris Lattner | 035dfbe | 2002-08-09 20:08:06 +0000 | [diff] [blame] | 1 | //===-- SparcInstrSelection.cpp -------------------------------------------===// |
| 2 | // |
| 3 | // BURS instruction selection for SPARC V9 architecture. |
| 4 | // |
| 5 | //===----------------------------------------------------------------------===// |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 6 | |
| 7 | #include "SparcInternals.h" |
Vikram S. Adve | 7fe2787 | 2001-10-18 00:26:20 +0000 | [diff] [blame] | 8 | #include "SparcInstrSelectionSupport.h" |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 9 | #include "SparcRegClassInfo.h" |
Vikram S. Adve | 8557b22 | 2001-10-10 20:56:33 +0000 | [diff] [blame] | 10 | #include "llvm/CodeGen/InstrSelectionSupport.h" |
Chris Lattner | e5b1ed9 | 2003-01-15 00:03:28 +0000 | [diff] [blame] | 11 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 12 | #include "llvm/CodeGen/MachineInstrAnnot.h" |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 13 | #include "llvm/CodeGen/InstrForest.h" |
| 14 | #include "llvm/CodeGen/InstrSelection.h" |
Misha Brukman | fce1143 | 2002-10-28 00:28:31 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/MachineFunction.h" |
Chris Lattner | ea45d7b | 2002-12-28 20:19:44 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/MachineFunctionInfo.h" |
Chris Lattner | 9c46108 | 2002-02-03 07:50:56 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineCodeForInstruction.h" |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 18 | #include "llvm/DerivedTypes.h" |
| 19 | #include "llvm/iTerminators.h" |
| 20 | #include "llvm/iMemory.h" |
| 21 | #include "llvm/iOther.h" |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 22 | #include "llvm/Function.h" |
Chris Lattner | 31bcdb8 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 23 | #include "llvm/Constants.h" |
Vikram S. Adve | d3e2648 | 2002-10-13 00:18:57 +0000 | [diff] [blame] | 24 | #include "llvm/ConstantHandling.h" |
Vikram S. Adve | 5b1b47b | 2003-05-25 15:59:47 +0000 | [diff] [blame] | 25 | #include "llvm/Intrinsics.h" |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 26 | #include "Support/MathExtras.h" |
Chris Lattner | 749655f | 2001-10-13 06:54:30 +0000 | [diff] [blame] | 27 | #include <math.h> |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 28 | |
Chris Lattner | 54e898e | 2003-01-15 19:23:34 +0000 | [diff] [blame] | 29 | static inline void Add3OperandInstr(unsigned Opcode, InstructionNode* Node, |
Misha Brukman | ee563cb | 2003-05-21 17:59:06 +0000 | [diff] [blame] | 30 | std::vector<MachineInstr*>& mvec) { |
Chris Lattner | 54e898e | 2003-01-15 19:23:34 +0000 | [diff] [blame] | 31 | mvec.push_back(BuildMI(Opcode, 3).addReg(Node->leftChild()->getValue()) |
| 32 | .addReg(Node->rightChild()->getValue()) |
| 33 | .addRegDef(Node->getValue())); |
| 34 | } |
| 35 | |
| 36 | |
| 37 | |
Chris Lattner | 795ba6c | 2003-01-15 21:36:50 +0000 | [diff] [blame] | 38 | //--------------------------------------------------------------------------- |
| 39 | // Function: GetMemInstArgs |
| 40 | // |
| 41 | // Purpose: |
| 42 | // Get the pointer value and the index vector for a memory operation |
| 43 | // (GetElementPtr, Load, or Store). If all indices of the given memory |
| 44 | // operation are constant, fold in constant indices in a chain of |
| 45 | // preceding GetElementPtr instructions (if any), and return the |
| 46 | // pointer value of the first instruction in the chain. |
| 47 | // All folded instructions are marked so no code is generated for them. |
| 48 | // |
| 49 | // Return values: |
| 50 | // Returns the pointer Value to use. |
| 51 | // Returns the resulting IndexVector in idxVec. |
| 52 | // Returns true/false in allConstantIndices if all indices are/aren't const. |
| 53 | //--------------------------------------------------------------------------- |
| 54 | |
| 55 | |
| 56 | //--------------------------------------------------------------------------- |
| 57 | // Function: FoldGetElemChain |
| 58 | // |
| 59 | // Purpose: |
| 60 | // Fold a chain of GetElementPtr instructions containing only |
| 61 | // constant offsets into an equivalent (Pointer, IndexVector) pair. |
| 62 | // Returns the pointer Value, and stores the resulting IndexVector |
| 63 | // in argument chainIdxVec. This is a helper function for |
| 64 | // FoldConstantIndices that does the actual folding. |
| 65 | //--------------------------------------------------------------------------- |
| 66 | |
| 67 | |
| 68 | // Check for a constant 0. |
| 69 | inline bool |
| 70 | IsZero(Value* idx) |
| 71 | { |
| 72 | return (idx == ConstantSInt::getNullValue(idx->getType())); |
| 73 | } |
| 74 | |
| 75 | static Value* |
Misha Brukman | ee563cb | 2003-05-21 17:59:06 +0000 | [diff] [blame] | 76 | FoldGetElemChain(InstrTreeNode* ptrNode, std::vector<Value*>& chainIdxVec, |
Chris Lattner | 795ba6c | 2003-01-15 21:36:50 +0000 | [diff] [blame] | 77 | bool lastInstHasLeadingNonZero) |
| 78 | { |
| 79 | InstructionNode* gepNode = dyn_cast<InstructionNode>(ptrNode); |
| 80 | GetElementPtrInst* gepInst = |
| 81 | dyn_cast_or_null<GetElementPtrInst>(gepNode ? gepNode->getInstruction() :0); |
| 82 | |
| 83 | // ptr value is not computed in this tree or ptr value does not come from GEP |
| 84 | // instruction |
| 85 | if (gepInst == NULL) |
| 86 | return NULL; |
| 87 | |
| 88 | // Return NULL if we don't fold any instructions in. |
| 89 | Value* ptrVal = NULL; |
| 90 | |
| 91 | // Now chase the chain of getElementInstr instructions, if any. |
| 92 | // Check for any non-constant indices and stop there. |
| 93 | // Also, stop if the first index of child is a non-zero array index |
| 94 | // and the last index of the current node is a non-array index: |
| 95 | // in that case, a non-array declared type is being accessed as an array |
| 96 | // which is not type-safe, but could be legal. |
| 97 | // |
| 98 | InstructionNode* ptrChild = gepNode; |
| 99 | while (ptrChild && (ptrChild->getOpLabel() == Instruction::GetElementPtr || |
| 100 | ptrChild->getOpLabel() == GetElemPtrIdx)) |
Misha Brukman | 81b0686 | 2003-05-21 18:48:06 +0000 | [diff] [blame] | 101 | { |
| 102 | // Child is a GetElemPtr instruction |
| 103 | gepInst = cast<GetElementPtrInst>(ptrChild->getValue()); |
| 104 | User::op_iterator OI, firstIdx = gepInst->idx_begin(); |
| 105 | User::op_iterator lastIdx = gepInst->idx_end(); |
| 106 | bool allConstantOffsets = true; |
Chris Lattner | 795ba6c | 2003-01-15 21:36:50 +0000 | [diff] [blame] | 107 | |
Misha Brukman | 81b0686 | 2003-05-21 18:48:06 +0000 | [diff] [blame] | 108 | // The first index of every GEP must be an array index. |
| 109 | assert((*firstIdx)->getType() == Type::LongTy && |
| 110 | "INTERNAL ERROR: Structure index for a pointer type!"); |
Chris Lattner | 795ba6c | 2003-01-15 21:36:50 +0000 | [diff] [blame] | 111 | |
Misha Brukman | 81b0686 | 2003-05-21 18:48:06 +0000 | [diff] [blame] | 112 | // If the last instruction had a leading non-zero index, check if the |
| 113 | // current one references a sequential (i.e., indexable) type. |
| 114 | // If not, the code is not type-safe and we would create an illegal GEP |
| 115 | // by folding them, so don't fold any more instructions. |
| 116 | // |
| 117 | if (lastInstHasLeadingNonZero) |
| 118 | if (! isa<SequentialType>(gepInst->getType()->getElementType())) |
| 119 | break; // cannot fold in any preceding getElementPtr instrs. |
Chris Lattner | 795ba6c | 2003-01-15 21:36:50 +0000 | [diff] [blame] | 120 | |
Misha Brukman | 81b0686 | 2003-05-21 18:48:06 +0000 | [diff] [blame] | 121 | // Check that all offsets are constant for this instruction |
| 122 | for (OI = firstIdx; allConstantOffsets && OI != lastIdx; ++OI) |
| 123 | allConstantOffsets = isa<ConstantInt>(*OI); |
Chris Lattner | 795ba6c | 2003-01-15 21:36:50 +0000 | [diff] [blame] | 124 | |
Misha Brukman | 81b0686 | 2003-05-21 18:48:06 +0000 | [diff] [blame] | 125 | if (allConstantOffsets) { |
| 126 | // Get pointer value out of ptrChild. |
| 127 | ptrVal = gepInst->getPointerOperand(); |
Chris Lattner | 795ba6c | 2003-01-15 21:36:50 +0000 | [diff] [blame] | 128 | |
Misha Brukman | 81b0686 | 2003-05-21 18:48:06 +0000 | [diff] [blame] | 129 | // Remember if it has leading zero index: it will be discarded later. |
| 130 | lastInstHasLeadingNonZero = ! IsZero(*firstIdx); |
Chris Lattner | 795ba6c | 2003-01-15 21:36:50 +0000 | [diff] [blame] | 131 | |
Misha Brukman | 81b0686 | 2003-05-21 18:48:06 +0000 | [diff] [blame] | 132 | // Insert its index vector at the start, skipping any leading [0] |
| 133 | chainIdxVec.insert(chainIdxVec.begin(), |
| 134 | firstIdx + !lastInstHasLeadingNonZero, lastIdx); |
Chris Lattner | 795ba6c | 2003-01-15 21:36:50 +0000 | [diff] [blame] | 135 | |
Misha Brukman | 81b0686 | 2003-05-21 18:48:06 +0000 | [diff] [blame] | 136 | // Mark the folded node so no code is generated for it. |
| 137 | ((InstructionNode*) ptrChild)->markFoldedIntoParent(); |
Chris Lattner | 795ba6c | 2003-01-15 21:36:50 +0000 | [diff] [blame] | 138 | |
Misha Brukman | 81b0686 | 2003-05-21 18:48:06 +0000 | [diff] [blame] | 139 | // Get the previous GEP instruction and continue trying to fold |
| 140 | ptrChild = dyn_cast<InstructionNode>(ptrChild->leftChild()); |
| 141 | } else // cannot fold this getElementPtr instr. or any preceding ones |
| 142 | break; |
| 143 | } |
Chris Lattner | 795ba6c | 2003-01-15 21:36:50 +0000 | [diff] [blame] | 144 | |
| 145 | // If the first getElementPtr instruction had a leading [0], add it back. |
| 146 | // Note that this instruction is the *last* one successfully folded above. |
| 147 | if (ptrVal && ! lastInstHasLeadingNonZero) |
| 148 | chainIdxVec.insert(chainIdxVec.begin(), ConstantSInt::get(Type::LongTy,0)); |
| 149 | |
| 150 | return ptrVal; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | //--------------------------------------------------------------------------- |
| 155 | // Function: GetGEPInstArgs |
| 156 | // |
| 157 | // Purpose: |
| 158 | // Helper function for GetMemInstArgs that handles the final getElementPtr |
| 159 | // instruction used by (or same as) the memory operation. |
| 160 | // Extracts the indices of the current instruction and tries to fold in |
| 161 | // preceding ones if all indices of the current one are constant. |
| 162 | //--------------------------------------------------------------------------- |
| 163 | |
| 164 | static Value * |
| 165 | GetGEPInstArgs(InstructionNode* gepNode, |
Misha Brukman | ee563cb | 2003-05-21 17:59:06 +0000 | [diff] [blame] | 166 | std::vector<Value*>& idxVec, |
Chris Lattner | 795ba6c | 2003-01-15 21:36:50 +0000 | [diff] [blame] | 167 | bool& allConstantIndices) |
| 168 | { |
| 169 | allConstantIndices = true; |
| 170 | GetElementPtrInst* gepI = cast<GetElementPtrInst>(gepNode->getInstruction()); |
| 171 | |
| 172 | // Default pointer is the one from the current instruction. |
| 173 | Value* ptrVal = gepI->getPointerOperand(); |
| 174 | InstrTreeNode* ptrChild = gepNode->leftChild(); |
| 175 | |
| 176 | // Extract the index vector of the GEP instructin. |
| 177 | // If all indices are constant and first index is zero, try to fold |
| 178 | // in preceding GEPs with all constant indices. |
| 179 | for (User::op_iterator OI=gepI->idx_begin(), OE=gepI->idx_end(); |
| 180 | allConstantIndices && OI != OE; ++OI) |
| 181 | if (! isa<Constant>(*OI)) |
| 182 | allConstantIndices = false; // note: this also terminates loop! |
| 183 | |
| 184 | // If we have only constant indices, fold chains of constant indices |
| 185 | // in this and any preceding GetElemPtr instructions. |
| 186 | bool foldedGEPs = false; |
| 187 | bool leadingNonZeroIdx = gepI && ! IsZero(*gepI->idx_begin()); |
| 188 | if (allConstantIndices) |
Misha Brukman | 81b0686 | 2003-05-21 18:48:06 +0000 | [diff] [blame] | 189 | if (Value* newPtr = FoldGetElemChain(ptrChild, idxVec, leadingNonZeroIdx)) { |
| 190 | ptrVal = newPtr; |
| 191 | foldedGEPs = true; |
| 192 | } |
Chris Lattner | 795ba6c | 2003-01-15 21:36:50 +0000 | [diff] [blame] | 193 | |
| 194 | // Append the index vector of the current instruction. |
| 195 | // Skip the leading [0] index if preceding GEPs were folded into this. |
| 196 | idxVec.insert(idxVec.end(), |
| 197 | gepI->idx_begin() + (foldedGEPs && !leadingNonZeroIdx), |
| 198 | gepI->idx_end()); |
| 199 | |
| 200 | return ptrVal; |
| 201 | } |
| 202 | |
| 203 | //--------------------------------------------------------------------------- |
| 204 | // Function: GetMemInstArgs |
| 205 | // |
| 206 | // Purpose: |
| 207 | // Get the pointer value and the index vector for a memory operation |
| 208 | // (GetElementPtr, Load, or Store). If all indices of the given memory |
| 209 | // operation are constant, fold in constant indices in a chain of |
| 210 | // preceding GetElementPtr instructions (if any), and return the |
| 211 | // pointer value of the first instruction in the chain. |
| 212 | // All folded instructions are marked so no code is generated for them. |
| 213 | // |
| 214 | // Return values: |
| 215 | // Returns the pointer Value to use. |
| 216 | // Returns the resulting IndexVector in idxVec. |
| 217 | // Returns true/false in allConstantIndices if all indices are/aren't const. |
| 218 | //--------------------------------------------------------------------------- |
| 219 | |
| 220 | static Value* |
| 221 | GetMemInstArgs(InstructionNode* memInstrNode, |
Misha Brukman | ee563cb | 2003-05-21 17:59:06 +0000 | [diff] [blame] | 222 | std::vector<Value*>& idxVec, |
Chris Lattner | 795ba6c | 2003-01-15 21:36:50 +0000 | [diff] [blame] | 223 | bool& allConstantIndices) |
| 224 | { |
| 225 | allConstantIndices = false; |
| 226 | Instruction* memInst = memInstrNode->getInstruction(); |
| 227 | assert(idxVec.size() == 0 && "Need empty vector to return indices"); |
| 228 | |
| 229 | // If there is a GetElemPtr instruction to fold in to this instr, |
| 230 | // it must be in the left child for Load and GetElemPtr, and in the |
| 231 | // right child for Store instructions. |
| 232 | InstrTreeNode* ptrChild = (memInst->getOpcode() == Instruction::Store |
| 233 | ? memInstrNode->rightChild() |
| 234 | : memInstrNode->leftChild()); |
| 235 | |
| 236 | // Default pointer is the one from the current instruction. |
| 237 | Value* ptrVal = ptrChild->getValue(); |
| 238 | |
| 239 | // Find the "last" GetElemPtr instruction: this one or the immediate child. |
| 240 | // There will be none if this is a load or a store from a scalar pointer. |
| 241 | InstructionNode* gepNode = NULL; |
| 242 | if (isa<GetElementPtrInst>(memInst)) |
| 243 | gepNode = memInstrNode; |
Misha Brukman | 81b0686 | 2003-05-21 18:48:06 +0000 | [diff] [blame] | 244 | else if (isa<InstructionNode>(ptrChild) && isa<GetElementPtrInst>(ptrVal)) { |
| 245 | // Child of load/store is a GEP and memInst is its only use. |
| 246 | // Use its indices and mark it as folded. |
| 247 | gepNode = cast<InstructionNode>(ptrChild); |
| 248 | gepNode->markFoldedIntoParent(); |
| 249 | } |
Chris Lattner | 795ba6c | 2003-01-15 21:36:50 +0000 | [diff] [blame] | 250 | |
| 251 | // If there are no indices, return the current pointer. |
| 252 | // Else extract the pointer from the GEP and fold the indices. |
| 253 | return gepNode ? GetGEPInstArgs(gepNode, idxVec, allConstantIndices) |
| 254 | : ptrVal; |
| 255 | } |
| 256 | |
Chris Lattner | 54e898e | 2003-01-15 19:23:34 +0000 | [diff] [blame] | 257 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 258 | //************************ Internal Functions ******************************/ |
| 259 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 260 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 261 | static inline MachineOpCode |
| 262 | ChooseBprInstruction(const InstructionNode* instrNode) |
| 263 | { |
| 264 | MachineOpCode opCode; |
| 265 | |
| 266 | Instruction* setCCInstr = |
| 267 | ((InstructionNode*) instrNode->leftChild())->getInstruction(); |
| 268 | |
| 269 | switch(setCCInstr->getOpcode()) |
Misha Brukman | 81b0686 | 2003-05-21 18:48:06 +0000 | [diff] [blame] | 270 | { |
| 271 | case Instruction::SetEQ: opCode = V9::BRZ; break; |
| 272 | case Instruction::SetNE: opCode = V9::BRNZ; break; |
| 273 | case Instruction::SetLE: opCode = V9::BRLEZ; break; |
| 274 | case Instruction::SetGE: opCode = V9::BRGEZ; break; |
| 275 | case Instruction::SetLT: opCode = V9::BRLZ; break; |
| 276 | case Instruction::SetGT: opCode = V9::BRGZ; break; |
| 277 | default: |
| 278 | assert(0 && "Unrecognized VM instruction!"); |
| 279 | opCode = V9::INVALID_OPCODE; |
| 280 | break; |
| 281 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 282 | |
| 283 | return opCode; |
| 284 | } |
| 285 | |
| 286 | |
| 287 | static inline MachineOpCode |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 288 | ChooseBpccInstruction(const InstructionNode* instrNode, |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 289 | const BinaryOperator* setCCInstr) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 290 | { |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 291 | MachineOpCode opCode = V9::INVALID_OPCODE; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 292 | |
| 293 | bool isSigned = setCCInstr->getOperand(0)->getType()->isSigned(); |
| 294 | |
Misha Brukman | 81b0686 | 2003-05-21 18:48:06 +0000 | [diff] [blame] | 295 | if (isSigned) { |
| 296 | switch(setCCInstr->getOpcode()) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 297 | { |
Misha Brukman | 81b0686 | 2003-05-21 18:48:06 +0000 | [diff] [blame] | 298 | case Instruction::SetEQ: opCode = V9::BE; break; |
| 299 | case Instruction::SetNE: opCode = V9::BNE; break; |
| 300 | case Instruction::SetLE: opCode = V9::BLE; break; |
| 301 | case Instruction::SetGE: opCode = V9::BGE; break; |
| 302 | case Instruction::SetLT: opCode = V9::BL; break; |
| 303 | case Instruction::SetGT: opCode = V9::BG; break; |
| 304 | default: |
| 305 | assert(0 && "Unrecognized VM instruction!"); |
| 306 | break; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 307 | } |
Misha Brukman | 81b0686 | 2003-05-21 18:48:06 +0000 | [diff] [blame] | 308 | } else { |
| 309 | switch(setCCInstr->getOpcode()) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 310 | { |
Misha Brukman | 81b0686 | 2003-05-21 18:48:06 +0000 | [diff] [blame] | 311 | case Instruction::SetEQ: opCode = V9::BE; break; |
| 312 | case Instruction::SetNE: opCode = V9::BNE; break; |
| 313 | case Instruction::SetLE: opCode = V9::BLEU; break; |
| 314 | case Instruction::SetGE: opCode = V9::BCC; break; |
| 315 | case Instruction::SetLT: opCode = V9::BCS; break; |
| 316 | case Instruction::SetGT: opCode = V9::BGU; break; |
| 317 | default: |
| 318 | assert(0 && "Unrecognized VM instruction!"); |
| 319 | break; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 320 | } |
Misha Brukman | 81b0686 | 2003-05-21 18:48:06 +0000 | [diff] [blame] | 321 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 322 | |
| 323 | return opCode; |
| 324 | } |
| 325 | |
| 326 | static inline MachineOpCode |
| 327 | ChooseBFpccInstruction(const InstructionNode* instrNode, |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 328 | const BinaryOperator* setCCInstr) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 329 | { |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 330 | MachineOpCode opCode = V9::INVALID_OPCODE; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 331 | |
| 332 | switch(setCCInstr->getOpcode()) |
Misha Brukman | 81b0686 | 2003-05-21 18:48:06 +0000 | [diff] [blame] | 333 | { |
| 334 | case Instruction::SetEQ: opCode = V9::FBE; break; |
| 335 | case Instruction::SetNE: opCode = V9::FBNE; break; |
| 336 | case Instruction::SetLE: opCode = V9::FBLE; break; |
| 337 | case Instruction::SetGE: opCode = V9::FBGE; break; |
| 338 | case Instruction::SetLT: opCode = V9::FBL; break; |
| 339 | case Instruction::SetGT: opCode = V9::FBG; break; |
| 340 | default: |
| 341 | assert(0 && "Unrecognized VM instruction!"); |
| 342 | break; |
| 343 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 344 | |
| 345 | return opCode; |
| 346 | } |
| 347 | |
| 348 | |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 349 | // Create a unique TmpInstruction for a boolean value, |
| 350 | // representing the CC register used by a branch on that value. |
| 351 | // For now, hack this using a little static cache of TmpInstructions. |
| 352 | // Eventually the entire BURG instruction selection should be put |
| 353 | // into a separate class that can hold such information. |
Vikram S. Adve | ff5a09e | 2001-11-08 05:04:09 +0000 | [diff] [blame] | 354 | // The static cache is not too bad because the memory for these |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 355 | // TmpInstructions will be freed along with the rest of the Function anyway. |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 356 | // |
| 357 | static TmpInstruction* |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 358 | GetTmpForCC(Value* boolVal, const Function *F, const Type* ccType) |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 359 | { |
Chris Lattner | 09ff112 | 2002-07-24 21:21:32 +0000 | [diff] [blame] | 360 | typedef hash_map<const Value*, TmpInstruction*> BoolTmpCache; |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 361 | static BoolTmpCache boolToTmpCache; // Map boolVal -> TmpInstruction* |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 362 | static const Function *lastFunction = 0;// Use to flush cache between funcs |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 363 | |
| 364 | assert(boolVal->getType() == Type::BoolTy && "Weird but ok! Delete assert"); |
| 365 | |
Misha Brukman | 81b0686 | 2003-05-21 18:48:06 +0000 | [diff] [blame] | 366 | if (lastFunction != F) { |
| 367 | lastFunction = F; |
| 368 | boolToTmpCache.clear(); |
| 369 | } |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 370 | |
Vikram S. Adve | ff5a09e | 2001-11-08 05:04:09 +0000 | [diff] [blame] | 371 | // Look for tmpI and create a new one otherwise. The new value is |
| 372 | // directly written to map using the ref returned by operator[]. |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 373 | TmpInstruction*& tmpI = boolToTmpCache[boolVal]; |
| 374 | if (tmpI == NULL) |
Chris Lattner | 9c46108 | 2002-02-03 07:50:56 +0000 | [diff] [blame] | 375 | tmpI = new TmpInstruction(ccType, boolVal); |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 376 | |
| 377 | return tmpI; |
| 378 | } |
| 379 | |
| 380 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 381 | static inline MachineOpCode |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 382 | ChooseBccInstruction(const InstructionNode* instrNode, |
| 383 | bool& isFPBranch) |
| 384 | { |
| 385 | InstructionNode* setCCNode = (InstructionNode*) instrNode->leftChild(); |
Vikram S. Adve | 30a6f49 | 2002-08-22 02:56:10 +0000 | [diff] [blame] | 386 | assert(setCCNode->getOpLabel() == SetCCOp); |
| 387 | BinaryOperator* setCCInstr =cast<BinaryOperator>(setCCNode->getInstruction()); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 388 | const Type* setCCType = setCCInstr->getOperand(0)->getType(); |
| 389 | |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 390 | isFPBranch = setCCType->isFloatingPoint(); // Return value: don't delete! |
| 391 | |
| 392 | if (isFPBranch) |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 393 | return ChooseBFpccInstruction(instrNode, setCCInstr); |
| 394 | else |
| 395 | return ChooseBpccInstruction(instrNode, setCCInstr); |
| 396 | } |
| 397 | |
| 398 | |
| 399 | static inline MachineOpCode |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 400 | ChooseMovFpccInstruction(const InstructionNode* instrNode) |
| 401 | { |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 402 | MachineOpCode opCode = V9::INVALID_OPCODE; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 403 | |
| 404 | switch(instrNode->getInstruction()->getOpcode()) |
Misha Brukman | 81b0686 | 2003-05-21 18:48:06 +0000 | [diff] [blame] | 405 | { |
| 406 | case Instruction::SetEQ: opCode = V9::MOVFE; break; |
| 407 | case Instruction::SetNE: opCode = V9::MOVFNE; break; |
| 408 | case Instruction::SetLE: opCode = V9::MOVFLE; break; |
| 409 | case Instruction::SetGE: opCode = V9::MOVFGE; break; |
| 410 | case Instruction::SetLT: opCode = V9::MOVFL; break; |
| 411 | case Instruction::SetGT: opCode = V9::MOVFG; break; |
| 412 | default: |
| 413 | assert(0 && "Unrecognized VM instruction!"); |
| 414 | break; |
| 415 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 416 | |
| 417 | return opCode; |
| 418 | } |
| 419 | |
| 420 | |
| 421 | // Assumes that SUBcc v1, v2 -> v3 has been executed. |
| 422 | // In most cases, we want to clear v3 and then follow it by instruction |
| 423 | // MOVcc 1 -> v3. |
| 424 | // Set mustClearReg=false if v3 need not be cleared before conditional move. |
| 425 | // Set valueToMove=0 if we want to conditionally move 0 instead of 1 |
| 426 | // (i.e., we want to test inverse of a condition) |
Vikram S. Adve | 243dd45 | 2001-09-18 13:03:13 +0000 | [diff] [blame] | 427 | // (The latter two cases do not seem to arise because SetNE needs nothing.) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 428 | // |
| 429 | static MachineOpCode |
| 430 | ChooseMovpccAfterSub(const InstructionNode* instrNode, |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 431 | bool& mustClearReg, |
| 432 | int& valueToMove) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 433 | { |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 434 | MachineOpCode opCode = V9::INVALID_OPCODE; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 435 | mustClearReg = true; |
| 436 | valueToMove = 1; |
| 437 | |
| 438 | switch(instrNode->getInstruction()->getOpcode()) |
Misha Brukman | 81b0686 | 2003-05-21 18:48:06 +0000 | [diff] [blame] | 439 | { |
| 440 | case Instruction::SetEQ: opCode = V9::MOVE; break; |
| 441 | case Instruction::SetLE: opCode = V9::MOVLE; break; |
| 442 | case Instruction::SetGE: opCode = V9::MOVGE; break; |
| 443 | case Instruction::SetLT: opCode = V9::MOVL; break; |
| 444 | case Instruction::SetGT: opCode = V9::MOVG; break; |
| 445 | case Instruction::SetNE: assert(0 && "No move required!"); break; |
| 446 | default: assert(0 && "Unrecognized VM instr!"); break; |
| 447 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 448 | |
| 449 | return opCode; |
| 450 | } |
| 451 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 452 | static inline MachineOpCode |
Vikram S. Adve | dbc4fad | 2002-04-25 04:37:51 +0000 | [diff] [blame] | 453 | ChooseConvertToFloatInstr(OpLabel vopCode, const Type* opType) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 454 | { |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 455 | MachineOpCode opCode = V9::INVALID_OPCODE; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 456 | |
Vikram S. Adve | dbc4fad | 2002-04-25 04:37:51 +0000 | [diff] [blame] | 457 | switch(vopCode) |
Misha Brukman | 81b0686 | 2003-05-21 18:48:06 +0000 | [diff] [blame] | 458 | { |
| 459 | case ToFloatTy: |
| 460 | if (opType == Type::SByteTy || opType == Type::ShortTy || |
| 461 | opType == Type::IntTy) |
| 462 | opCode = V9::FITOS; |
| 463 | else if (opType == Type::LongTy) |
| 464 | opCode = V9::FXTOS; |
| 465 | else if (opType == Type::DoubleTy) |
| 466 | opCode = V9::FDTOS; |
| 467 | else if (opType == Type::FloatTy) |
| 468 | ; |
| 469 | else |
| 470 | assert(0 && "Cannot convert this type to FLOAT on SPARC"); |
| 471 | break; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 472 | |
Misha Brukman | 81b0686 | 2003-05-21 18:48:06 +0000 | [diff] [blame] | 473 | case ToDoubleTy: |
| 474 | // This is usually used in conjunction with CreateCodeToCopyIntToFloat(). |
| 475 | // Both functions should treat the integer as a 32-bit value for types |
| 476 | // of 4 bytes or less, and as a 64-bit value otherwise. |
| 477 | if (opType == Type::SByteTy || opType == Type::UByteTy || |
| 478 | opType == Type::ShortTy || opType == Type::UShortTy || |
| 479 | opType == Type::IntTy || opType == Type::UIntTy) |
| 480 | opCode = V9::FITOD; |
| 481 | else if (opType == Type::LongTy || opType == Type::ULongTy) |
| 482 | opCode = V9::FXTOD; |
| 483 | else if (opType == Type::FloatTy) |
| 484 | opCode = V9::FSTOD; |
| 485 | else if (opType == Type::DoubleTy) |
| 486 | ; |
| 487 | else |
| 488 | assert(0 && "Cannot convert this type to DOUBLE on SPARC"); |
| 489 | break; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 490 | |
Misha Brukman | 81b0686 | 2003-05-21 18:48:06 +0000 | [diff] [blame] | 491 | default: |
| 492 | break; |
| 493 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 494 | |
| 495 | return opCode; |
| 496 | } |
| 497 | |
| 498 | static inline MachineOpCode |
Vikram S. Adve | 94c4081 | 2002-09-27 14:33:08 +0000 | [diff] [blame] | 499 | ChooseConvertFPToIntInstr(Type::PrimitiveID tid, const Type* opType) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 500 | { |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 501 | MachineOpCode opCode = V9::INVALID_OPCODE;; |
Vikram S. Adve | 94c4081 | 2002-09-27 14:33:08 +0000 | [diff] [blame] | 502 | |
| 503 | assert((opType == Type::FloatTy || opType == Type::DoubleTy) |
| 504 | && "This function should only be called for FLOAT or DOUBLE"); |
| 505 | |
Misha Brukman | 81b0686 | 2003-05-21 18:48:06 +0000 | [diff] [blame] | 506 | if (tid == Type::UIntTyID) { |
| 507 | assert(tid != Type::UIntTyID && "FP-to-uint conversions must be expanded" |
| 508 | " into FP->long->uint for SPARC v9: SO RUN PRESELECTION PASS!"); |
| 509 | } else if (tid == Type::SByteTyID || tid == Type::ShortTyID || |
| 510 | tid == Type::IntTyID || tid == Type::UByteTyID || |
| 511 | tid == Type::UShortTyID) { |
| 512 | opCode = (opType == Type::FloatTy)? V9::FSTOI : V9::FDTOI; |
| 513 | } else if (tid == Type::LongTyID || tid == Type::ULongTyID) { |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 514 | opCode = (opType == Type::FloatTy)? V9::FSTOX : V9::FDTOX; |
Misha Brukman | 81b0686 | 2003-05-21 18:48:06 +0000 | [diff] [blame] | 515 | } else |
| 516 | assert(0 && "Should not get here, Mo!"); |
Vikram S. Adve | 94c4081 | 2002-09-27 14:33:08 +0000 | [diff] [blame] | 517 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 518 | return opCode; |
| 519 | } |
| 520 | |
Vikram S. Adve | dbc4fad | 2002-04-25 04:37:51 +0000 | [diff] [blame] | 521 | MachineInstr* |
Vikram S. Adve | 94c4081 | 2002-09-27 14:33:08 +0000 | [diff] [blame] | 522 | CreateConvertFPToIntInstr(Type::PrimitiveID destTID, |
| 523 | Value* srcVal, Value* destVal) |
Vikram S. Adve | dbc4fad | 2002-04-25 04:37:51 +0000 | [diff] [blame] | 524 | { |
Vikram S. Adve | 94c4081 | 2002-09-27 14:33:08 +0000 | [diff] [blame] | 525 | MachineOpCode opCode = ChooseConvertFPToIntInstr(destTID, srcVal->getType()); |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 526 | assert(opCode != V9::INVALID_OPCODE && "Expected to need conversion!"); |
Chris Lattner | 00dca91 | 2003-01-15 17:47:49 +0000 | [diff] [blame] | 527 | return BuildMI(opCode, 2).addReg(srcVal).addRegDef(destVal); |
Vikram S. Adve | dbc4fad | 2002-04-25 04:37:51 +0000 | [diff] [blame] | 528 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 529 | |
Vikram S. Adve | 8cfffd3 | 2002-08-24 20:56:53 +0000 | [diff] [blame] | 530 | // CreateCodeToConvertFloatToInt: Convert FP value to signed or unsigned integer |
Vikram S. Adve | 1e60669 | 2002-07-31 21:01:34 +0000 | [diff] [blame] | 531 | // The FP value must be converted to the dest type in an FP register, |
| 532 | // and the result is then copied from FP to int register via memory. |
Vikram S. Adve | babc0fa | 2002-09-05 18:32:13 +0000 | [diff] [blame] | 533 | // |
| 534 | // Since fdtoi converts to signed integers, any FP value V between MAXINT+1 |
| 535 | // and MAXUNSIGNED (i.e., 2^31 <= V <= 2^32-1) would be converted incorrectly |
Chris Lattner | ea45d7b | 2002-12-28 20:19:44 +0000 | [diff] [blame] | 536 | // *only* when converting to an unsigned. (Unsigned byte, short or long |
Vikram S. Adve | babc0fa | 2002-09-05 18:32:13 +0000 | [diff] [blame] | 537 | // don't have this problem.) |
| 538 | // For unsigned int, we therefore have to generate the code sequence: |
| 539 | // |
| 540 | // if (V > (float) MAXINT) { |
| 541 | // unsigned result = (unsigned) (V - (float) MAXINT); |
| 542 | // result = result + (unsigned) MAXINT; |
| 543 | // } |
| 544 | // else |
Chris Lattner | ea45d7b | 2002-12-28 20:19:44 +0000 | [diff] [blame] | 545 | // result = (unsigned) V; |
Vikram S. Adve | babc0fa | 2002-09-05 18:32:13 +0000 | [diff] [blame] | 546 | // |
Vikram S. Adve | 1e60669 | 2002-07-31 21:01:34 +0000 | [diff] [blame] | 547 | static void |
Vikram S. Adve | 8cfffd3 | 2002-08-24 20:56:53 +0000 | [diff] [blame] | 548 | CreateCodeToConvertFloatToInt(const TargetMachine& target, |
| 549 | Value* opVal, |
| 550 | Instruction* destI, |
| 551 | std::vector<MachineInstr*>& mvec, |
| 552 | MachineCodeForInstruction& mcfi) |
Vikram S. Adve | 1e60669 | 2002-07-31 21:01:34 +0000 | [diff] [blame] | 553 | { |
| 554 | // Create a temporary to represent the FP register into which the |
| 555 | // int value will placed after conversion. The type of this temporary |
| 556 | // depends on the type of FP register to use: single-prec for a 32-bit |
| 557 | // int or smaller; double-prec for a 64-bit int. |
| 558 | // |
Chris Lattner | ea45d7b | 2002-12-28 20:19:44 +0000 | [diff] [blame] | 559 | size_t destSize = target.getTargetData().getTypeSize(destI->getType()); |
Vikram S. Adve | babc0fa | 2002-09-05 18:32:13 +0000 | [diff] [blame] | 560 | const Type* destTypeToUse = (destSize > 4)? Type::DoubleTy : Type::FloatTy; |
| 561 | TmpInstruction* destForCast = new TmpInstruction(destTypeToUse, opVal); |
Vikram S. Adve | 1e60669 | 2002-07-31 21:01:34 +0000 | [diff] [blame] | 562 | mcfi.addTemp(destForCast); |
| 563 | |
| 564 | // Create the fp-to-int conversion code |
Vikram S. Adve | 94c4081 | 2002-09-27 14:33:08 +0000 | [diff] [blame] | 565 | MachineInstr* M =CreateConvertFPToIntInstr(destI->getType()->getPrimitiveID(), |
| 566 | opVal, destForCast); |
Vikram S. Adve | 1e60669 | 2002-07-31 21:01:34 +0000 | [diff] [blame] | 567 | mvec.push_back(M); |
| 568 | |
| 569 | // Create the fpreg-to-intreg copy code |
| 570 | target.getInstrInfo(). |
| 571 | CreateCodeToCopyFloatToInt(target, destI->getParent()->getParent(), |
Vikram S. Adve | babc0fa | 2002-09-05 18:32:13 +0000 | [diff] [blame] | 572 | destForCast, destI, mvec, mcfi); |
Vikram S. Adve | 1e60669 | 2002-07-31 21:01:34 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 576 | static inline MachineOpCode |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 577 | ChooseAddInstruction(const InstructionNode* instrNode) |
| 578 | { |
| 579 | return ChooseAddInstructionByType(instrNode->getInstruction()->getType()); |
| 580 | } |
| 581 | |
| 582 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 583 | static inline MachineInstr* |
| 584 | CreateMovFloatInstruction(const InstructionNode* instrNode, |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 585 | const Type* resultType) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 586 | { |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 587 | return BuildMI((resultType == Type::FloatTy) ? V9::FMOVS : V9::FMOVD, 2) |
Chris Lattner | 00dca91 | 2003-01-15 17:47:49 +0000 | [diff] [blame] | 588 | .addReg(instrNode->leftChild()->getValue()) |
| 589 | .addRegDef(instrNode->getValue()); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | static inline MachineInstr* |
| 593 | CreateAddConstInstruction(const InstructionNode* instrNode) |
| 594 | { |
| 595 | MachineInstr* minstr = NULL; |
| 596 | |
| 597 | Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue(); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 598 | assert(isa<Constant>(constOp)); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 599 | |
| 600 | // Cases worth optimizing are: |
| 601 | // (1) Add with 0 for float or double: use an FMOV of appropriate type, |
| 602 | // instead of an FADD (1 vs 3 cycles). There is no integer MOV. |
| 603 | // |
Chris Lattner | 9b62503 | 2002-05-06 16:15:30 +0000 | [diff] [blame] | 604 | if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) { |
Misha Brukman | 81b0686 | 2003-05-21 18:48:06 +0000 | [diff] [blame] | 605 | double dval = FPC->getValue(); |
| 606 | if (dval == 0.0) |
| 607 | minstr = CreateMovFloatInstruction(instrNode, |
| 608 | instrNode->getInstruction()->getType()); |
| 609 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 610 | |
| 611 | return minstr; |
| 612 | } |
| 613 | |
| 614 | |
| 615 | static inline MachineOpCode |
Vikram S. Adve | 510eec7 | 2001-11-04 21:59:14 +0000 | [diff] [blame] | 616 | ChooseSubInstructionByType(const Type* resultType) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 617 | { |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 618 | MachineOpCode opCode = V9::INVALID_OPCODE; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 619 | |
Misha Brukman | 81b0686 | 2003-05-21 18:48:06 +0000 | [diff] [blame] | 620 | if (resultType->isInteger() || isa<PointerType>(resultType)) { |
Misha Brukman | 91aee47 | 2003-05-27 22:37:00 +0000 | [diff] [blame] | 621 | opCode = V9::SUBr; |
Misha Brukman | 81b0686 | 2003-05-21 18:48:06 +0000 | [diff] [blame] | 622 | } else { |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 623 | switch(resultType->getPrimitiveID()) |
Misha Brukman | 81b0686 | 2003-05-21 18:48:06 +0000 | [diff] [blame] | 624 | { |
| 625 | case Type::FloatTyID: opCode = V9::FSUBS; break; |
| 626 | case Type::DoubleTyID: opCode = V9::FSUBD; break; |
| 627 | default: assert(0 && "Invalid type for SUB instruction"); break; |
| 628 | } |
| 629 | } |
| 630 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 631 | return opCode; |
| 632 | } |
| 633 | |
| 634 | |
| 635 | static inline MachineInstr* |
| 636 | CreateSubConstInstruction(const InstructionNode* instrNode) |
| 637 | { |
| 638 | MachineInstr* minstr = NULL; |
| 639 | |
| 640 | Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue(); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 641 | assert(isa<Constant>(constOp)); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 642 | |
| 643 | // Cases worth optimizing are: |
| 644 | // (1) Sub with 0 for float or double: use an FMOV of appropriate type, |
| 645 | // instead of an FSUB (1 vs 3 cycles). There is no integer MOV. |
| 646 | // |
Chris Lattner | 9b62503 | 2002-05-06 16:15:30 +0000 | [diff] [blame] | 647 | if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) { |
| 648 | double dval = FPC->getValue(); |
| 649 | if (dval == 0.0) |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 650 | minstr = CreateMovFloatInstruction(instrNode, |
| 651 | instrNode->getInstruction()->getType()); |
Chris Lattner | 9b62503 | 2002-05-06 16:15:30 +0000 | [diff] [blame] | 652 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 653 | |
| 654 | return minstr; |
| 655 | } |
| 656 | |
| 657 | |
| 658 | static inline MachineOpCode |
| 659 | ChooseFcmpInstruction(const InstructionNode* instrNode) |
| 660 | { |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 661 | MachineOpCode opCode = V9::INVALID_OPCODE; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 662 | |
| 663 | Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue(); |
| 664 | switch(operand->getType()->getPrimitiveID()) { |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 665 | case Type::FloatTyID: opCode = V9::FCMPS; break; |
| 666 | case Type::DoubleTyID: opCode = V9::FCMPD; break; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 667 | default: assert(0 && "Invalid type for FCMP instruction"); break; |
| 668 | } |
| 669 | |
| 670 | return opCode; |
| 671 | } |
| 672 | |
| 673 | |
| 674 | // Assumes that leftArg and rightArg are both cast instructions. |
| 675 | // |
| 676 | static inline bool |
| 677 | BothFloatToDouble(const InstructionNode* instrNode) |
| 678 | { |
| 679 | InstrTreeNode* leftArg = instrNode->leftChild(); |
| 680 | InstrTreeNode* rightArg = instrNode->rightChild(); |
| 681 | InstrTreeNode* leftArgArg = leftArg->leftChild(); |
| 682 | InstrTreeNode* rightArgArg = rightArg->leftChild(); |
| 683 | assert(leftArg->getValue()->getType() == rightArg->getValue()->getType()); |
| 684 | |
| 685 | // Check if both arguments are floats cast to double |
| 686 | return (leftArg->getValue()->getType() == Type::DoubleTy && |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 687 | leftArgArg->getValue()->getType() == Type::FloatTy && |
| 688 | rightArgArg->getValue()->getType() == Type::FloatTy); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | |
| 692 | static inline MachineOpCode |
Vikram S. Adve | 510eec7 | 2001-11-04 21:59:14 +0000 | [diff] [blame] | 693 | ChooseMulInstructionByType(const Type* resultType) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 694 | { |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 695 | MachineOpCode opCode = V9::INVALID_OPCODE; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 696 | |
Chris Lattner | 0c4e886 | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 697 | if (resultType->isInteger()) |
Misha Brukman | 91aee47 | 2003-05-27 22:37:00 +0000 | [diff] [blame] | 698 | opCode = V9::MULXr; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 699 | else |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 700 | switch(resultType->getPrimitiveID()) |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 701 | { |
| 702 | case Type::FloatTyID: opCode = V9::FMULS; break; |
| 703 | case Type::DoubleTyID: opCode = V9::FMULD; break; |
| 704 | default: assert(0 && "Invalid type for MUL instruction"); break; |
| 705 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 706 | |
| 707 | return opCode; |
| 708 | } |
| 709 | |
| 710 | |
Vikram S. Adve | 510eec7 | 2001-11-04 21:59:14 +0000 | [diff] [blame] | 711 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 712 | static inline MachineInstr* |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 713 | CreateIntNegInstruction(const TargetMachine& target, |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 714 | Value* vreg) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 715 | { |
Misha Brukman | 91aee47 | 2003-05-27 22:37:00 +0000 | [diff] [blame] | 716 | return BuildMI(V9::SUBr, 3).addMReg(target.getRegInfo().getZeroRegNum()) |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 717 | .addReg(vreg).addRegDef(vreg); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 721 | // Create instruction sequence for any shift operation. |
| 722 | // SLL or SLLX on an operand smaller than the integer reg. size (64bits) |
| 723 | // requires a second instruction for explicit sign-extension. |
| 724 | // Note that we only have to worry about a sign-bit appearing in the |
| 725 | // most significant bit of the operand after shifting (e.g., bit 32 of |
| 726 | // Int or bit 16 of Short), so we do not have to worry about results |
| 727 | // that are as large as a normal integer register. |
| 728 | // |
| 729 | static inline void |
| 730 | CreateShiftInstructions(const TargetMachine& target, |
| 731 | Function* F, |
| 732 | MachineOpCode shiftOpCode, |
| 733 | Value* argVal1, |
| 734 | Value* optArgVal2, /* Use optArgVal2 if not NULL */ |
Chris Lattner | ea45d7b | 2002-12-28 20:19:44 +0000 | [diff] [blame] | 735 | unsigned optShiftNum, /* else use optShiftNum */ |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 736 | Instruction* destVal, |
Misha Brukman | ee563cb | 2003-05-21 17:59:06 +0000 | [diff] [blame] | 737 | std::vector<MachineInstr*>& mvec, |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 738 | MachineCodeForInstruction& mcfi) |
| 739 | { |
| 740 | assert((optArgVal2 != NULL || optShiftNum <= 64) && |
| 741 | "Large shift sizes unexpected, but can be handled below: " |
| 742 | "You need to check whether or not it fits in immed field below"); |
| 743 | |
| 744 | // If this is a logical left shift of a type smaller than the standard |
| 745 | // integer reg. size, we have to extend the sign-bit into upper bits |
| 746 | // of dest, so we need to put the result of the SLL into a temporary. |
| 747 | // |
| 748 | Value* shiftDest = destVal; |
Chris Lattner | ea45d7b | 2002-12-28 20:19:44 +0000 | [diff] [blame] | 749 | unsigned opSize = target.getTargetData().getTypeSize(argVal1->getType()); |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 750 | if ((shiftOpCode == V9::SLLr6 || shiftOpCode == V9::SLLXr6) && opSize < 8) { |
| 751 | // put SLL result into a temporary |
| 752 | shiftDest = new TmpInstruction(argVal1, optArgVal2, "sllTmp"); |
| 753 | mcfi.addTemp(shiftDest); |
| 754 | } |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 755 | |
| 756 | MachineInstr* M = (optArgVal2 != NULL) |
Chris Lattner | e5b1ed9 | 2003-01-15 00:03:28 +0000 | [diff] [blame] | 757 | ? BuildMI(shiftOpCode, 3).addReg(argVal1).addReg(optArgVal2) |
| 758 | .addReg(shiftDest, MOTy::Def) |
| 759 | : BuildMI(shiftOpCode, 3).addReg(argVal1).addZImm(optShiftNum) |
| 760 | .addReg(shiftDest, MOTy::Def); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 761 | mvec.push_back(M); |
| 762 | |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 763 | if (shiftDest != destVal) { |
| 764 | // extend the sign-bit of the result into all upper bits of dest |
| 765 | assert(8*opSize <= 32 && "Unexpected type size > 4 and < IntRegSize?"); |
| 766 | target.getInstrInfo(). |
| 767 | CreateSignExtensionInstructions(target, F, shiftDest, destVal, |
| 768 | 8*opSize, mvec, mcfi); |
| 769 | } |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 773 | // Does not create any instructions if we cannot exploit constant to |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 774 | // create a cheaper instruction. |
| 775 | // This returns the approximate cost of the instructions generated, |
| 776 | // which is used to pick the cheapest when both operands are constant. |
Vikram S. Adve | 645fea3 | 2003-05-25 21:59:47 +0000 | [diff] [blame] | 777 | static unsigned |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 778 | CreateMulConstInstruction(const TargetMachine &target, Function* F, |
| 779 | Value* lval, Value* rval, Instruction* destVal, |
Misha Brukman | ee563cb | 2003-05-21 17:59:06 +0000 | [diff] [blame] | 780 | std::vector<MachineInstr*>& mvec, |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 781 | MachineCodeForInstruction& mcfi) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 782 | { |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 783 | /* Use max. multiply cost, viz., cost of MULX */ |
Misha Brukman | 91aee47 | 2003-05-27 22:37:00 +0000 | [diff] [blame] | 784 | unsigned cost = target.getInstrInfo().minLatency(V9::MULXr); |
Chris Lattner | ea45d7b | 2002-12-28 20:19:44 +0000 | [diff] [blame] | 785 | unsigned firstNewInstr = mvec.size(); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 786 | |
| 787 | Value* constOp = rval; |
| 788 | if (! isa<Constant>(constOp)) |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 789 | return cost; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 790 | |
| 791 | // Cases worth optimizing are: |
| 792 | // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV) |
| 793 | // (2) Multiply by 2^x for integer types: replace with Shift |
| 794 | // |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 795 | const Type* resultType = destVal->getType(); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 796 | |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 797 | if (resultType->isInteger() || isa<PointerType>(resultType)) { |
| 798 | bool isValidConst; |
| 799 | int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst); |
| 800 | if (isValidConst) { |
| 801 | unsigned pow; |
| 802 | bool needNeg = false; |
| 803 | if (C < 0) { |
| 804 | needNeg = true; |
| 805 | C = -C; |
| 806 | } |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 807 | |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 808 | if (C == 0 || C == 1) { |
Misha Brukman | 91aee47 | 2003-05-27 22:37:00 +0000 | [diff] [blame] | 809 | cost = target.getInstrInfo().minLatency(V9::ADDr); |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 810 | unsigned Zero = target.getRegInfo().getZeroRegNum(); |
| 811 | MachineInstr* M; |
| 812 | if (C == 0) |
Misha Brukman | 91aee47 | 2003-05-27 22:37:00 +0000 | [diff] [blame] | 813 | M =BuildMI(V9::ADDr,3).addMReg(Zero).addMReg(Zero).addRegDef(destVal); |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 814 | else |
Misha Brukman | 91aee47 | 2003-05-27 22:37:00 +0000 | [diff] [blame] | 815 | M = BuildMI(V9::ADDr,3).addReg(lval).addMReg(Zero).addRegDef(destVal); |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 816 | mvec.push_back(M); |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 817 | } else if (isPowerOf2(C, pow)) { |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 818 | unsigned opSize = target.getTargetData().getTypeSize(resultType); |
Misha Brukman | 91aee47 | 2003-05-27 22:37:00 +0000 | [diff] [blame] | 819 | MachineOpCode opCode = (opSize <= 32)? V9::SLLr6 : V9::SLLXr6; |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 820 | CreateShiftInstructions(target, F, opCode, lval, NULL, pow, |
| 821 | destVal, mvec, mcfi); |
| 822 | } |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 823 | |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 824 | if (mvec.size() > 0 && needNeg) { |
| 825 | // insert <reg = SUB 0, reg> after the instr to flip the sign |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 826 | MachineInstr* M = CreateIntNegInstruction(target, destVal); |
| 827 | mvec.push_back(M); |
| 828 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 829 | } |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 830 | } else { |
| 831 | if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) { |
| 832 | double dval = FPC->getValue(); |
| 833 | if (fabs(dval) == 1) { |
| 834 | MachineOpCode opCode = (dval < 0) |
| 835 | ? (resultType == Type::FloatTy? V9::FNEGS : V9::FNEGD) |
| 836 | : (resultType == Type::FloatTy? V9::FMOVS : V9::FMOVD); |
| 837 | mvec.push_back(BuildMI(opCode,2).addReg(lval).addRegDef(destVal)); |
| 838 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 839 | } |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 840 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 841 | |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 842 | if (firstNewInstr < mvec.size()) { |
| 843 | cost = 0; |
| 844 | for (unsigned i=firstNewInstr; i < mvec.size(); ++i) |
| 845 | cost += target.getInstrInfo().minLatency(mvec[i]->getOpCode()); |
| 846 | } |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 847 | |
| 848 | return cost; |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 852 | // Does not create any instructions if we cannot exploit constant to |
| 853 | // create a cheaper instruction. |
| 854 | // |
| 855 | static inline void |
| 856 | CreateCheapestMulConstInstruction(const TargetMachine &target, |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 857 | Function* F, |
| 858 | Value* lval, Value* rval, |
| 859 | Instruction* destVal, |
Misha Brukman | ee563cb | 2003-05-21 17:59:06 +0000 | [diff] [blame] | 860 | std::vector<MachineInstr*>& mvec, |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 861 | MachineCodeForInstruction& mcfi) |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 862 | { |
| 863 | Value* constOp; |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 864 | if (isa<Constant>(lval) && isa<Constant>(rval)) { |
| 865 | // both operands are constant: evaluate and "set" in dest |
| 866 | Constant* P = ConstantFoldBinaryInstruction(Instruction::Mul, |
| 867 | cast<Constant>(lval), |
| 868 | cast<Constant>(rval)); |
| 869 | target.getInstrInfo().CreateCodeToLoadConst(target,F,P,destVal,mvec,mcfi); |
| 870 | } |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 871 | else if (isa<Constant>(rval)) // rval is constant, but not lval |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 872 | CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi); |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 873 | else if (isa<Constant>(lval)) // lval is constant, but not rval |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 874 | CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi); |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 875 | |
| 876 | // else neither is constant |
| 877 | return; |
| 878 | } |
| 879 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 880 | // Return NULL if we cannot exploit constant to create a cheaper instruction |
| 881 | static inline void |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 882 | CreateMulInstruction(const TargetMachine &target, Function* F, |
| 883 | Value* lval, Value* rval, Instruction* destVal, |
Misha Brukman | ee563cb | 2003-05-21 17:59:06 +0000 | [diff] [blame] | 884 | std::vector<MachineInstr*>& mvec, |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 885 | MachineCodeForInstruction& mcfi, |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 886 | MachineOpCode forceMulOp = INVALID_MACHINE_OPCODE) |
| 887 | { |
Chris Lattner | ea45d7b | 2002-12-28 20:19:44 +0000 | [diff] [blame] | 888 | unsigned L = mvec.size(); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 889 | CreateCheapestMulConstInstruction(target,F, lval, rval, destVal, mvec, mcfi); |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 890 | if (mvec.size() == L) { |
| 891 | // no instructions were added so create MUL reg, reg, reg. |
| 892 | // Use FSMULD if both operands are actually floats cast to doubles. |
| 893 | // Otherwise, use the default opcode for the appropriate type. |
| 894 | MachineOpCode mulOp = ((forceMulOp != INVALID_MACHINE_OPCODE) |
| 895 | ? forceMulOp |
| 896 | : ChooseMulInstructionByType(destVal->getType())); |
| 897 | mvec.push_back(BuildMI(mulOp, 3).addReg(lval).addReg(rval) |
| 898 | .addRegDef(destVal)); |
| 899 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | |
Vikram S. Adve | 510eec7 | 2001-11-04 21:59:14 +0000 | [diff] [blame] | 903 | // Generate a divide instruction for Div or Rem. |
| 904 | // For Rem, this assumes that the operand type will be signed if the result |
| 905 | // type is signed. This is correct because they must have the same sign. |
| 906 | // |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 907 | static inline MachineOpCode |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 908 | ChooseDivInstruction(TargetMachine &target, |
| 909 | const InstructionNode* instrNode) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 910 | { |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 911 | MachineOpCode opCode = V9::INVALID_OPCODE; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 912 | |
| 913 | const Type* resultType = instrNode->getInstruction()->getType(); |
| 914 | |
Chris Lattner | 0c4e886 | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 915 | if (resultType->isInteger()) |
Misha Brukman | 91aee47 | 2003-05-27 22:37:00 +0000 | [diff] [blame] | 916 | opCode = resultType->isSigned()? V9::SDIVXr : V9::UDIVXr; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 917 | else |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 918 | switch(resultType->getPrimitiveID()) |
| 919 | { |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 920 | case Type::FloatTyID: opCode = V9::FDIVS; break; |
| 921 | case Type::DoubleTyID: opCode = V9::FDIVD; break; |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 922 | default: assert(0 && "Invalid type for DIV instruction"); break; |
| 923 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 924 | |
| 925 | return opCode; |
| 926 | } |
| 927 | |
| 928 | |
Chris Lattner | 54e898e | 2003-01-15 19:23:34 +0000 | [diff] [blame] | 929 | // Return if we cannot exploit constant to create a cheaper instruction |
Vikram S. Adve | 645fea3 | 2003-05-25 21:59:47 +0000 | [diff] [blame] | 930 | static void |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 931 | CreateDivConstInstruction(TargetMachine &target, |
| 932 | const InstructionNode* instrNode, |
Misha Brukman | ee563cb | 2003-05-21 17:59:06 +0000 | [diff] [blame] | 933 | std::vector<MachineInstr*>& mvec) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 934 | { |
Chris Lattner | 54e898e | 2003-01-15 19:23:34 +0000 | [diff] [blame] | 935 | Value* LHS = instrNode->leftChild()->getValue(); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 936 | Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue(); |
Chris Lattner | 54e898e | 2003-01-15 19:23:34 +0000 | [diff] [blame] | 937 | if (!isa<Constant>(constOp)) |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 938 | return; |
Chris Lattner | 54e898e | 2003-01-15 19:23:34 +0000 | [diff] [blame] | 939 | |
Vikram S. Adve | 645fea3 | 2003-05-25 21:59:47 +0000 | [diff] [blame] | 940 | Instruction* destVal = instrNode->getInstruction(); |
Chris Lattner | 54e898e | 2003-01-15 19:23:34 +0000 | [diff] [blame] | 941 | unsigned ZeroReg = target.getRegInfo().getZeroRegNum(); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 942 | |
| 943 | // Cases worth optimizing are: |
| 944 | // (1) Divide by 1 for any type: replace with copy (ADD or FMOV) |
| 945 | // (2) Divide by 2^x for integer types: replace with SR[L or A]{X} |
| 946 | // |
| 947 | const Type* resultType = instrNode->getInstruction()->getType(); |
Chris Lattner | 54e898e | 2003-01-15 19:23:34 +0000 | [diff] [blame] | 948 | |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 949 | if (resultType->isInteger()) { |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 950 | unsigned pow; |
| 951 | bool isValidConst; |
| 952 | int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst); |
| 953 | if (isValidConst) { |
| 954 | bool needNeg = false; |
| 955 | if (C < 0) { |
| 956 | needNeg = true; |
| 957 | C = -C; |
| 958 | } |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 959 | |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 960 | if (C == 1) { |
Misha Brukman | 91aee47 | 2003-05-27 22:37:00 +0000 | [diff] [blame] | 961 | mvec.push_back(BuildMI(V9::ADDr, 3).addReg(LHS).addMReg(ZeroReg) |
Vikram S. Adve | 645fea3 | 2003-05-25 21:59:47 +0000 | [diff] [blame] | 962 | .addRegDef(destVal)); |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 963 | } else if (isPowerOf2(C, pow)) { |
Vikram S. Adve | 645fea3 | 2003-05-25 21:59:47 +0000 | [diff] [blame] | 964 | unsigned opCode; |
| 965 | Value* shiftOperand; |
| 966 | |
| 967 | if (resultType->isSigned()) { |
| 968 | // The result may be negative and we need to add one before shifting |
| 969 | // a negative value. Use: |
| 970 | // srl i0, 31, x0; add x0, i0, i1 (if i0 is <= 32 bits) |
| 971 | // or |
| 972 | // srlx i0, 63, x0; add x0, i0, i1 (if i0 is 64 bits) |
| 973 | // to compute i1=i0+1 if i0 < 0 and i1=i0 otherwise. |
| 974 | // |
| 975 | TmpInstruction *srlTmp, *addTmp; |
| 976 | MachineCodeForInstruction& mcfi |
| 977 | = MachineCodeForInstruction::get(destVal); |
| 978 | srlTmp = new TmpInstruction(resultType, LHS, 0, "getSign"); |
| 979 | addTmp = new TmpInstruction(resultType, LHS, srlTmp, "incIfNeg"); |
| 980 | mcfi.addTemp(srlTmp); |
| 981 | mcfi.addTemp(addTmp); |
| 982 | |
| 983 | // Create the SRL or SRLX instruction to get the sign bit |
Misha Brukman | 91aee47 | 2003-05-27 22:37:00 +0000 | [diff] [blame] | 984 | mvec.push_back(BuildMI((resultType==Type::LongTy) ? |
| 985 | V9::SRLXi6 : V9::SRLi6, 3) |
Vikram S. Adve | 645fea3 | 2003-05-25 21:59:47 +0000 | [diff] [blame] | 986 | .addReg(LHS) |
| 987 | .addSImm((resultType==Type::LongTy)? 63 : 31) |
| 988 | .addRegDef(srlTmp)); |
| 989 | |
| 990 | // Create the ADD instruction to add 1 for negative values |
Misha Brukman | 91aee47 | 2003-05-27 22:37:00 +0000 | [diff] [blame] | 991 | mvec.push_back(BuildMI(V9::ADDr, 3).addReg(LHS).addReg(srlTmp) |
Vikram S. Adve | 645fea3 | 2003-05-25 21:59:47 +0000 | [diff] [blame] | 992 | .addRegDef(addTmp)); |
| 993 | |
| 994 | // Get the shift operand and "right-shift" opcode to do the divide |
| 995 | shiftOperand = addTmp; |
Misha Brukman | 91aee47 | 2003-05-27 22:37:00 +0000 | [diff] [blame] | 996 | opCode = (resultType==Type::LongTy) ? V9::SRAXi6 : V9::SRAi6; |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 997 | } else { |
Vikram S. Adve | 645fea3 | 2003-05-25 21:59:47 +0000 | [diff] [blame] | 998 | // Get the shift operand and "right-shift" opcode to do the divide |
| 999 | shiftOperand = LHS; |
Misha Brukman | 91aee47 | 2003-05-27 22:37:00 +0000 | [diff] [blame] | 1000 | opCode = (resultType==Type::LongTy) ? V9::SRLXi6 : V9::SRLi6; |
Vikram S. Adve | 645fea3 | 2003-05-25 21:59:47 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | // Now do the actual shift! |
| 1004 | mvec.push_back(BuildMI(opCode, 3).addReg(shiftOperand).addZImm(pow) |
| 1005 | .addRegDef(destVal)); |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 1006 | } |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1007 | |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 1008 | if (needNeg && (C == 1 || isPowerOf2(C, pow))) { |
| 1009 | // insert <reg = SUB 0, reg> after the instr to flip the sign |
Vikram S. Adve | 645fea3 | 2003-05-25 21:59:47 +0000 | [diff] [blame] | 1010 | mvec.push_back(CreateIntNegInstruction(target, destVal)); |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 1011 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1012 | } |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 1013 | } else { |
| 1014 | if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) { |
| 1015 | double dval = FPC->getValue(); |
| 1016 | if (fabs(dval) == 1) { |
| 1017 | unsigned opCode = |
| 1018 | (dval < 0) ? (resultType == Type::FloatTy? V9::FNEGS : V9::FNEGD) |
| 1019 | : (resultType == Type::FloatTy? V9::FMOVS : V9::FMOVD); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1020 | |
Vikram S. Adve | 645fea3 | 2003-05-25 21:59:47 +0000 | [diff] [blame] | 1021 | mvec.push_back(BuildMI(opCode, 2).addReg(LHS).addRegDef(destVal)); |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 1022 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1023 | } |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 1024 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1028 | static void |
| 1029 | CreateCodeForVariableSizeAlloca(const TargetMachine& target, |
| 1030 | Instruction* result, |
Chris Lattner | ea45d7b | 2002-12-28 20:19:44 +0000 | [diff] [blame] | 1031 | unsigned tsize, |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1032 | Value* numElementsVal, |
Misha Brukman | ee563cb | 2003-05-21 17:59:06 +0000 | [diff] [blame] | 1033 | std::vector<MachineInstr*>& getMvec) |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1034 | { |
Vikram S. Adve | aabb595 | 2002-10-29 19:37:31 +0000 | [diff] [blame] | 1035 | Value* totalSizeVal; |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1036 | MachineInstr* M; |
Vikram S. Adve | d3e2648 | 2002-10-13 00:18:57 +0000 | [diff] [blame] | 1037 | MachineCodeForInstruction& mcfi = MachineCodeForInstruction::get(result); |
Vikram S. Adve | aabb595 | 2002-10-29 19:37:31 +0000 | [diff] [blame] | 1038 | Function *F = result->getParent()->getParent(); |
Vikram S. Adve | d3e2648 | 2002-10-13 00:18:57 +0000 | [diff] [blame] | 1039 | |
Vikram S. Adve | aabb595 | 2002-10-29 19:37:31 +0000 | [diff] [blame] | 1040 | // Enforce the alignment constraints on the stack pointer at |
| 1041 | // compile time if the total size is a known constant. |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 1042 | if (isa<Constant>(numElementsVal)) { |
| 1043 | bool isValid; |
| 1044 | int64_t numElem = GetConstantValueAsSignedInt(numElementsVal, isValid); |
| 1045 | assert(isValid && "Unexpectedly large array dimension in alloca!"); |
| 1046 | int64_t total = numElem * tsize; |
| 1047 | if (int extra= total % target.getFrameInfo().getStackFrameSizeAlignment()) |
| 1048 | total += target.getFrameInfo().getStackFrameSizeAlignment() - extra; |
| 1049 | totalSizeVal = ConstantSInt::get(Type::IntTy, total); |
| 1050 | } else { |
| 1051 | // The size is not a constant. Generate code to compute it and |
| 1052 | // code to pad the size for stack alignment. |
| 1053 | // Create a Value to hold the (constant) element size |
| 1054 | Value* tsizeVal = ConstantSInt::get(Type::IntTy, tsize); |
Vikram S. Adve | aabb595 | 2002-10-29 19:37:31 +0000 | [diff] [blame] | 1055 | |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 1056 | // Create temporary values to hold the result of MUL, SLL, SRL |
| 1057 | // THIS CASE IS INCOMPLETE AND WILL BE FIXED SHORTLY. |
| 1058 | TmpInstruction* tmpProd = new TmpInstruction(numElementsVal, tsizeVal); |
| 1059 | TmpInstruction* tmpSLL = new TmpInstruction(numElementsVal, tmpProd); |
| 1060 | TmpInstruction* tmpSRL = new TmpInstruction(numElementsVal, tmpSLL); |
| 1061 | mcfi.addTemp(tmpProd); |
| 1062 | mcfi.addTemp(tmpSLL); |
| 1063 | mcfi.addTemp(tmpSRL); |
Vikram S. Adve | aabb595 | 2002-10-29 19:37:31 +0000 | [diff] [blame] | 1064 | |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 1065 | // Instruction 1: mul numElements, typeSize -> tmpProd |
| 1066 | // This will optimize the MUL as far as possible. |
| 1067 | CreateMulInstruction(target, F, numElementsVal, tsizeVal, tmpProd,getMvec, |
| 1068 | mcfi, INVALID_MACHINE_OPCODE); |
Vikram S. Adve | aabb595 | 2002-10-29 19:37:31 +0000 | [diff] [blame] | 1069 | |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 1070 | assert(0 && "Need to insert padding instructions here!"); |
Vikram S. Adve | aabb595 | 2002-10-29 19:37:31 +0000 | [diff] [blame] | 1071 | |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 1072 | totalSizeVal = tmpProd; |
| 1073 | } |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1074 | |
| 1075 | // Get the constant offset from SP for dynamically allocated storage |
| 1076 | // and create a temporary Value to hold it. |
Misha Brukman | fce1143 | 2002-10-28 00:28:31 +0000 | [diff] [blame] | 1077 | MachineFunction& mcInfo = MachineFunction::get(F); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1078 | bool growUp; |
| 1079 | ConstantSInt* dynamicAreaOffset = |
| 1080 | ConstantSInt::get(Type::IntTy, |
Vikram S. Adve | aabb595 | 2002-10-29 19:37:31 +0000 | [diff] [blame] | 1081 | target.getFrameInfo().getDynamicAreaOffset(mcInfo,growUp)); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1082 | assert(! growUp && "Has SPARC v9 stack frame convention changed?"); |
| 1083 | |
Chris Lattner | 54e898e | 2003-01-15 19:23:34 +0000 | [diff] [blame] | 1084 | unsigned SPReg = target.getRegInfo().getStackPointer(); |
| 1085 | |
Vikram S. Adve | aabb595 | 2002-10-29 19:37:31 +0000 | [diff] [blame] | 1086 | // Instruction 2: sub %sp, totalSizeVal -> %sp |
Misha Brukman | 91aee47 | 2003-05-27 22:37:00 +0000 | [diff] [blame] | 1087 | getMvec.push_back(BuildMI(V9::SUBr, 3).addMReg(SPReg).addReg(totalSizeVal) |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 1088 | .addMReg(SPReg,MOTy::Def)); |
Vikram S. Adve | aabb595 | 2002-10-29 19:37:31 +0000 | [diff] [blame] | 1089 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1090 | // Instruction 3: add %sp, frameSizeBelowDynamicArea -> result |
Misha Brukman | 91aee47 | 2003-05-27 22:37:00 +0000 | [diff] [blame] | 1091 | getMvec.push_back(BuildMI(V9::ADDr,3).addMReg(SPReg).addReg(dynamicAreaOffset) |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 1092 | .addRegDef(result)); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | |
| 1096 | static void |
| 1097 | CreateCodeForFixedSizeAlloca(const TargetMachine& target, |
| 1098 | Instruction* result, |
Chris Lattner | ea45d7b | 2002-12-28 20:19:44 +0000 | [diff] [blame] | 1099 | unsigned tsize, |
| 1100 | unsigned numElements, |
Misha Brukman | ee563cb | 2003-05-21 17:59:06 +0000 | [diff] [blame] | 1101 | std::vector<MachineInstr*>& getMvec) |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1102 | { |
Vikram S. Adve | d3e2648 | 2002-10-13 00:18:57 +0000 | [diff] [blame] | 1103 | assert(tsize > 0 && "Illegal (zero) type size for alloca"); |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 1104 | assert(result && result->getParent() && |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 1105 | "Result value is not part of a function?"); |
| 1106 | Function *F = result->getParent()->getParent(); |
Misha Brukman | fce1143 | 2002-10-28 00:28:31 +0000 | [diff] [blame] | 1107 | MachineFunction &mcInfo = MachineFunction::get(F); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1108 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 1109 | // Check if the offset would small enough to use as an immediate in |
| 1110 | // load/stores (check LDX because all load/stores have the same-size immediate |
| 1111 | // field). If not, put the variable in the dynamically sized area of the |
| 1112 | // frame. |
Chris Lattner | ea45d7b | 2002-12-28 20:19:44 +0000 | [diff] [blame] | 1113 | unsigned paddedSizeIgnored; |
| 1114 | int offsetFromFP = mcInfo.getInfo()->computeOffsetforLocalVar(result, |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 1115 | paddedSizeIgnored, |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1116 | tsize * numElements); |
Misha Brukman | 91aee47 | 2003-05-27 22:37:00 +0000 | [diff] [blame] | 1117 | if (! target.getInstrInfo().constantFitsInImmedField(V9::LDXi,offsetFromFP)) { |
Chris Lattner | ea45d7b | 2002-12-28 20:19:44 +0000 | [diff] [blame] | 1118 | CreateCodeForVariableSizeAlloca(target, result, tsize, |
| 1119 | ConstantSInt::get(Type::IntTy,numElements), |
| 1120 | getMvec); |
| 1121 | return; |
| 1122 | } |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1123 | |
| 1124 | // else offset fits in immediate field so go ahead and allocate it. |
Chris Lattner | ea45d7b | 2002-12-28 20:19:44 +0000 | [diff] [blame] | 1125 | offsetFromFP = mcInfo.getInfo()->allocateLocalVar(result, tsize *numElements); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1126 | |
| 1127 | // Create a temporary Value to hold the constant offset. |
| 1128 | // This is needed because it may not fit in the immediate field. |
| 1129 | ConstantSInt* offsetVal = ConstantSInt::get(Type::IntTy, offsetFromFP); |
| 1130 | |
| 1131 | // Instruction 1: add %fp, offsetFromFP -> result |
Chris Lattner | 54e898e | 2003-01-15 19:23:34 +0000 | [diff] [blame] | 1132 | unsigned FPReg = target.getRegInfo().getFramePointer(); |
Misha Brukman | 91aee47 | 2003-05-27 22:37:00 +0000 | [diff] [blame] | 1133 | getMvec.push_back(BuildMI(V9::ADDr, 3).addMReg(FPReg).addReg(offsetVal) |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 1134 | .addRegDef(result)); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
| 1137 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1138 | //------------------------------------------------------------------------ |
| 1139 | // Function SetOperandsForMemInstr |
| 1140 | // |
| 1141 | // Choose addressing mode for the given load or store instruction. |
| 1142 | // Use [reg+reg] if it is an indexed reference, and the index offset is |
| 1143 | // not a constant or if it cannot fit in the offset field. |
| 1144 | // Use [reg+offset] in all other cases. |
| 1145 | // |
| 1146 | // This assumes that all array refs are "lowered" to one of these forms: |
| 1147 | // %x = load (subarray*) ptr, constant ; single constant offset |
| 1148 | // %x = load (subarray*) ptr, offsetVal ; single non-constant offset |
| 1149 | // Generally, this should happen via strength reduction + LICM. |
| 1150 | // Also, strength reduction should take care of using the same register for |
| 1151 | // the loop index variable and an array index, when that is profitable. |
| 1152 | //------------------------------------------------------------------------ |
| 1153 | |
| 1154 | static void |
Chris Lattner | 54e898e | 2003-01-15 19:23:34 +0000 | [diff] [blame] | 1155 | SetOperandsForMemInstr(unsigned Opcode, |
Misha Brukman | ee563cb | 2003-05-21 17:59:06 +0000 | [diff] [blame] | 1156 | std::vector<MachineInstr*>& mvec, |
Vikram S. Adve | efc9433 | 2002-10-14 16:32:24 +0000 | [diff] [blame] | 1157 | InstructionNode* vmInstrNode, |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1158 | const TargetMachine& target) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1159 | { |
Vikram S. Adve | 8cfffd3 | 2002-08-24 20:56:53 +0000 | [diff] [blame] | 1160 | Instruction* memInst = vmInstrNode->getInstruction(); |
Vikram S. Adve | 8cfffd3 | 2002-08-24 20:56:53 +0000 | [diff] [blame] | 1161 | // Index vector, ptr value, and flag if all indices are const. |
Misha Brukman | ee563cb | 2003-05-21 17:59:06 +0000 | [diff] [blame] | 1162 | std::vector<Value*> idxVec; |
Vikram S. Adve | 8cfffd3 | 2002-08-24 20:56:53 +0000 | [diff] [blame] | 1163 | bool allConstantIndices; |
| 1164 | Value* ptrVal = GetMemInstArgs(vmInstrNode, idxVec, allConstantIndices); |
Vikram S. Adve | ed3fefb | 2002-08-03 13:48:21 +0000 | [diff] [blame] | 1165 | |
Vikram S. Adve | 8cfffd3 | 2002-08-24 20:56:53 +0000 | [diff] [blame] | 1166 | // Now create the appropriate operands for the machine instruction. |
| 1167 | // First, initialize so we default to storing the offset in a register. |
Chris Lattner | 8e5c0b4 | 2001-11-07 14:01:59 +0000 | [diff] [blame] | 1168 | int64_t smallConstOffset = 0; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1169 | Value* valueForRegOffset = NULL; |
Vikram S. Adve | ed3fefb | 2002-08-03 13:48:21 +0000 | [diff] [blame] | 1170 | MachineOperand::MachineOperandType offsetOpType = |
| 1171 | MachineOperand::MO_VirtualRegister; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1172 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1173 | // Check if there is an index vector and if so, compute the |
| 1174 | // right offset for structures and for arrays |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1175 | // |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 1176 | if (!idxVec.empty()) { |
| 1177 | const PointerType* ptrType = cast<PointerType>(ptrVal->getType()); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1178 | |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 1179 | // If all indices are constant, compute the combined offset directly. |
| 1180 | if (allConstantIndices) { |
| 1181 | // Compute the offset value using the index vector. Create a |
| 1182 | // virtual reg. for it since it may not fit in the immed field. |
| 1183 | uint64_t offset = target.getTargetData().getIndexedOffset(ptrType,idxVec); |
| 1184 | valueForRegOffset = ConstantSInt::get(Type::LongTy, offset); |
| 1185 | } else { |
| 1186 | // There is at least one non-constant offset. Therefore, this must |
| 1187 | // be an array ref, and must have been lowered to a single non-zero |
| 1188 | // offset. (An extra leading zero offset, if any, can be ignored.) |
| 1189 | // Generate code sequence to compute address from index. |
| 1190 | // |
| 1191 | bool firstIdxIsZero = IsZero(idxVec[0]); |
| 1192 | assert(idxVec.size() == 1U + firstIdxIsZero |
| 1193 | && "Array refs must be lowered before Instruction Selection"); |
Vikram S. Adve | ed3fefb | 2002-08-03 13:48:21 +0000 | [diff] [blame] | 1194 | |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 1195 | Value* idxVal = idxVec[firstIdxIsZero]; |
Vikram S. Adve | ed3fefb | 2002-08-03 13:48:21 +0000 | [diff] [blame] | 1196 | |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 1197 | std::vector<MachineInstr*> mulVec; |
| 1198 | Instruction* addr = new TmpInstruction(Type::ULongTy, memInst); |
| 1199 | MachineCodeForInstruction::get(memInst).addTemp(addr); |
Vikram S. Adve | ed3fefb | 2002-08-03 13:48:21 +0000 | [diff] [blame] | 1200 | |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 1201 | // Get the array type indexed by idxVal, and compute its element size. |
| 1202 | // The call to getTypeSize() will fail if size is not constant. |
| 1203 | const Type* vecType = (firstIdxIsZero |
| 1204 | ? GetElementPtrInst::getIndexedType(ptrType, |
Vikram S. Adve | babc0fa | 2002-09-05 18:32:13 +0000 | [diff] [blame] | 1205 | std::vector<Value*>(1U, idxVec[0]), |
| 1206 | /*AllowCompositeLeaf*/ true) |
| 1207 | : ptrType); |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 1208 | const Type* eltType = cast<SequentialType>(vecType)->getElementType(); |
| 1209 | ConstantUInt* eltSizeVal = ConstantUInt::get(Type::ULongTy, |
| 1210 | target.getTargetData().getTypeSize(eltType)); |
Vikram S. Adve | ed3fefb | 2002-08-03 13:48:21 +0000 | [diff] [blame] | 1211 | |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 1212 | // CreateMulInstruction() folds constants intelligently enough. |
| 1213 | CreateMulInstruction(target, memInst->getParent()->getParent(), |
| 1214 | idxVal, /* lval, not likely to be const*/ |
| 1215 | eltSizeVal, /* rval, likely to be constant */ |
| 1216 | addr, /* result */ |
| 1217 | mulVec, MachineCodeForInstruction::get(memInst), |
| 1218 | INVALID_MACHINE_OPCODE); |
Vikram S. Adve | ed3fefb | 2002-08-03 13:48:21 +0000 | [diff] [blame] | 1219 | |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 1220 | assert(mulVec.size() > 0 && "No multiply code created?"); |
| 1221 | mvec.insert(mvec.end(), mulVec.begin(), mulVec.end()); |
| 1222 | |
| 1223 | valueForRegOffset = addr; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1224 | } |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 1225 | } else { |
| 1226 | offsetOpType = MachineOperand::MO_SignExtendedImmed; |
| 1227 | smallConstOffset = 0; |
| 1228 | } |
Vikram S. Adve | ed3fefb | 2002-08-03 13:48:21 +0000 | [diff] [blame] | 1229 | |
Vikram S. Adve | a10d1a7 | 2002-03-31 19:07:35 +0000 | [diff] [blame] | 1230 | // For STORE: |
| 1231 | // Operand 0 is value, operand 1 is ptr, operand 2 is offset |
| 1232 | // For LOAD or GET_ELEMENT_PTR, |
| 1233 | // Operand 0 is ptr, operand 1 is offset, operand 2 is result. |
| 1234 | // |
| 1235 | unsigned offsetOpNum, ptrOpNum; |
Chris Lattner | 54e898e | 2003-01-15 19:23:34 +0000 | [diff] [blame] | 1236 | MachineInstr *MI; |
| 1237 | if (memInst->getOpcode() == Instruction::Store) { |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 1238 | if (offsetOpType == MachineOperand::MO_VirtualRegister) { |
Chris Lattner | 54e898e | 2003-01-15 19:23:34 +0000 | [diff] [blame] | 1239 | MI = BuildMI(Opcode, 3).addReg(vmInstrNode->leftChild()->getValue()) |
| 1240 | .addReg(ptrVal).addReg(valueForRegOffset); |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 1241 | } else { |
Misha Brukman | 91aee47 | 2003-05-27 22:37:00 +0000 | [diff] [blame] | 1242 | Opcode = convertOpcodeFromRegToImm(Opcode); |
Chris Lattner | 54e898e | 2003-01-15 19:23:34 +0000 | [diff] [blame] | 1243 | MI = BuildMI(Opcode, 3).addReg(vmInstrNode->leftChild()->getValue()) |
| 1244 | .addReg(ptrVal).addSImm(smallConstOffset); |
Misha Brukman | 91aee47 | 2003-05-27 22:37:00 +0000 | [diff] [blame] | 1245 | } |
Chris Lattner | 54e898e | 2003-01-15 19:23:34 +0000 | [diff] [blame] | 1246 | } else { |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 1247 | if (offsetOpType == MachineOperand::MO_VirtualRegister) { |
Chris Lattner | 54e898e | 2003-01-15 19:23:34 +0000 | [diff] [blame] | 1248 | MI = BuildMI(Opcode, 3).addReg(ptrVal).addReg(valueForRegOffset) |
| 1249 | .addRegDef(memInst); |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 1250 | } else { |
Misha Brukman | 91aee47 | 2003-05-27 22:37:00 +0000 | [diff] [blame] | 1251 | Opcode = convertOpcodeFromRegToImm(Opcode); |
Chris Lattner | 54e898e | 2003-01-15 19:23:34 +0000 | [diff] [blame] | 1252 | MI = BuildMI(Opcode, 3).addReg(ptrVal).addSImm(smallConstOffset) |
| 1253 | .addRegDef(memInst); |
Misha Brukman | 91aee47 | 2003-05-27 22:37:00 +0000 | [diff] [blame] | 1254 | } |
Chris Lattner | 54e898e | 2003-01-15 19:23:34 +0000 | [diff] [blame] | 1255 | } |
| 1256 | mvec.push_back(MI); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1257 | } |
| 1258 | |
| 1259 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1260 | // |
| 1261 | // Substitute operand `operandNum' of the instruction in node `treeNode' |
Vikram S. Adve | c025fc1 | 2001-10-14 23:28:43 +0000 | [diff] [blame] | 1262 | // in place of the use(s) of that instruction in node `parent'. |
| 1263 | // Check both explicit and implicit operands! |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1264 | // Also make sure to skip over a parent who: |
| 1265 | // (1) is a list node in the Burg tree, or |
| 1266 | // (2) itself had its results forwarded to its parent |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1267 | // |
| 1268 | static void |
| 1269 | ForwardOperand(InstructionNode* treeNode, |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1270 | InstrTreeNode* parent, |
| 1271 | int operandNum) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1272 | { |
Vikram S. Adve | 243dd45 | 2001-09-18 13:03:13 +0000 | [diff] [blame] | 1273 | assert(treeNode && parent && "Invalid invocation of ForwardOperand"); |
| 1274 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1275 | Instruction* unusedOp = treeNode->getInstruction(); |
| 1276 | Value* fwdOp = unusedOp->getOperand(operandNum); |
Vikram S. Adve | 243dd45 | 2001-09-18 13:03:13 +0000 | [diff] [blame] | 1277 | |
| 1278 | // The parent itself may be a list node, so find the real parent instruction |
| 1279 | while (parent->getNodeType() != InstrTreeNode::NTInstructionNode) |
| 1280 | { |
| 1281 | parent = parent->parent(); |
| 1282 | assert(parent && "ERROR: Non-instruction node has no parent in tree."); |
| 1283 | } |
| 1284 | InstructionNode* parentInstrNode = (InstructionNode*) parent; |
| 1285 | |
| 1286 | Instruction* userInstr = parentInstrNode->getInstruction(); |
Chris Lattner | 9c46108 | 2002-02-03 07:50:56 +0000 | [diff] [blame] | 1287 | MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(userInstr); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1288 | |
| 1289 | // The parent's mvec would be empty if it was itself forwarded. |
| 1290 | // Recursively call ForwardOperand in that case... |
| 1291 | // |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 1292 | if (mvec.size() == 0) { |
| 1293 | assert(parent->parent() != NULL && |
| 1294 | "Parent could not have been forwarded, yet has no instructions?"); |
| 1295 | ForwardOperand(treeNode, parent->parent(), operandNum); |
| 1296 | } else { |
| 1297 | for (unsigned i=0, N=mvec.size(); i < N; i++) { |
| 1298 | MachineInstr* minstr = mvec[i]; |
| 1299 | for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i) { |
| 1300 | const MachineOperand& mop = minstr->getOperand(i); |
| 1301 | if (mop.getType() == MachineOperand::MO_VirtualRegister && |
| 1302 | mop.getVRegValue() == unusedOp) |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1303 | { |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 1304 | minstr->SetMachineOperandVal(i, MachineOperand::MO_VirtualRegister, |
| 1305 | fwdOp); |
| 1306 | } |
| 1307 | } |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1308 | |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 1309 | for (unsigned i=0,numOps=minstr->getNumImplicitRefs(); i<numOps; ++i) |
| 1310 | if (minstr->getImplicitRef(i) == unusedOp) { |
| 1311 | minstr->setImplicitRef(i, fwdOp, |
| 1312 | minstr->getImplicitOp(i).opIsDefOnly(), |
| 1313 | minstr->getImplicitOp(i).opIsDefAndUse()); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1314 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1315 | } |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 1316 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 1320 | inline bool |
| 1321 | AllUsesAreBranches(const Instruction* setccI) |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1322 | { |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 1323 | for (Value::use_const_iterator UI=setccI->use_begin(), UE=setccI->use_end(); |
| 1324 | UI != UE; ++UI) |
| 1325 | if (! isa<TmpInstruction>(*UI) // ignore tmp instructions here |
| 1326 | && cast<Instruction>(*UI)->getOpcode() != Instruction::Br) |
| 1327 | return false; |
| 1328 | return true; |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
Vikram S. Adve | 5b1b47b | 2003-05-25 15:59:47 +0000 | [diff] [blame] | 1331 | // Generate code for any intrinsic that needs a special code sequence |
| 1332 | // instead of a regular call. If not that kind of intrinsic, do nothing. |
| 1333 | // Returns true if code was generated, otherwise false. |
| 1334 | // |
| 1335 | bool CodeGenIntrinsic(LLVMIntrinsic::ID iid, CallInst &callInstr, |
| 1336 | TargetMachine &target, |
| 1337 | std::vector<MachineInstr*>& mvec) |
| 1338 | { |
| 1339 | switch (iid) { |
| 1340 | case LLVMIntrinsic::va_start: { |
| 1341 | // Get the address of the first vararg value on stack and copy it to |
| 1342 | // the argument of va_start(va_list* ap). |
| 1343 | bool ignore; |
| 1344 | Function* func = cast<Function>(callInstr.getParent()->getParent()); |
| 1345 | int numFixedArgs = func->getFunctionType()->getNumParams(); |
| 1346 | int fpReg = target.getFrameInfo().getIncomingArgBaseRegNum(); |
| 1347 | int argSize = target.getFrameInfo().getSizeOfEachArgOnStack(); |
| 1348 | int firstVarArgOff = numFixedArgs * argSize + target.getFrameInfo(). |
| 1349 | getFirstIncomingArgOffset(MachineFunction::get(func), ignore); |
Misha Brukman | 91aee47 | 2003-05-27 22:37:00 +0000 | [diff] [blame] | 1350 | mvec.push_back(BuildMI(V9::ADDi, 3).addMReg(fpReg).addSImm(firstVarArgOff). |
Vikram S. Adve | 5b1b47b | 2003-05-25 15:59:47 +0000 | [diff] [blame] | 1351 | addReg(callInstr.getOperand(1))); |
| 1352 | return true; |
| 1353 | } |
| 1354 | |
| 1355 | case LLVMIntrinsic::va_end: |
| 1356 | return true; // no-op on Sparc |
| 1357 | |
| 1358 | case LLVMIntrinsic::va_copy: |
| 1359 | // Simple copy of current va_list (arg2) to new va_list (arg1) |
Misha Brukman | 91aee47 | 2003-05-27 22:37:00 +0000 | [diff] [blame] | 1360 | mvec.push_back(BuildMI(V9::ORr, 3). |
Vikram S. Adve | 5b1b47b | 2003-05-25 15:59:47 +0000 | [diff] [blame] | 1361 | addMReg(target.getRegInfo().getZeroRegNum()). |
| 1362 | addReg(callInstr.getOperand(2)). |
| 1363 | addReg(callInstr.getOperand(1))); |
| 1364 | return true; |
| 1365 | |
| 1366 | default: |
| 1367 | return false; |
| 1368 | } |
| 1369 | } |
| 1370 | |
Vikram S. Adve | fb36112 | 2001-10-22 13:36:31 +0000 | [diff] [blame] | 1371 | //******************* Externally Visible Functions *************************/ |
| 1372 | |
Vikram S. Adve | fb36112 | 2001-10-22 13:36:31 +0000 | [diff] [blame] | 1373 | //------------------------------------------------------------------------ |
| 1374 | // External Function: ThisIsAChainRule |
| 1375 | // |
| 1376 | // Purpose: |
| 1377 | // Check if a given BURG rule is a chain rule. |
| 1378 | //------------------------------------------------------------------------ |
| 1379 | |
| 1380 | extern bool |
| 1381 | ThisIsAChainRule(int eruleno) |
| 1382 | { |
| 1383 | switch(eruleno) |
| 1384 | { |
| 1385 | case 111: // stmt: reg |
Vikram S. Adve | fb36112 | 2001-10-22 13:36:31 +0000 | [diff] [blame] | 1386 | case 123: |
| 1387 | case 124: |
| 1388 | case 125: |
| 1389 | case 126: |
| 1390 | case 127: |
| 1391 | case 128: |
| 1392 | case 129: |
| 1393 | case 130: |
| 1394 | case 131: |
| 1395 | case 132: |
| 1396 | case 133: |
| 1397 | case 155: |
| 1398 | case 221: |
| 1399 | case 222: |
| 1400 | case 241: |
| 1401 | case 242: |
| 1402 | case 243: |
| 1403 | case 244: |
Vikram S. Adve | 30a6f49 | 2002-08-22 02:56:10 +0000 | [diff] [blame] | 1404 | case 245: |
Vikram S. Adve | 85e1e9c | 2002-04-01 20:28:48 +0000 | [diff] [blame] | 1405 | case 321: |
Vikram S. Adve | fb36112 | 2001-10-22 13:36:31 +0000 | [diff] [blame] | 1406 | return true; break; |
Vikram S. Adve | 30a6f49 | 2002-08-22 02:56:10 +0000 | [diff] [blame] | 1407 | |
Vikram S. Adve | fb36112 | 2001-10-22 13:36:31 +0000 | [diff] [blame] | 1408 | default: |
| 1409 | return false; break; |
| 1410 | } |
| 1411 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1412 | |
| 1413 | |
| 1414 | //------------------------------------------------------------------------ |
| 1415 | // External Function: GetInstructionsByRule |
| 1416 | // |
| 1417 | // Purpose: |
| 1418 | // Choose machine instructions for the SPARC according to the |
| 1419 | // patterns chosen by the BURG-generated parser. |
| 1420 | //------------------------------------------------------------------------ |
| 1421 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1422 | void |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1423 | GetInstructionsByRule(InstructionNode* subtreeRoot, |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1424 | int ruleForNode, |
| 1425 | short* nts, |
Vikram S. Adve | ff5a09e | 2001-11-08 05:04:09 +0000 | [diff] [blame] | 1426 | TargetMachine &target, |
Misha Brukman | ee563cb | 2003-05-21 17:59:06 +0000 | [diff] [blame] | 1427 | std::vector<MachineInstr*>& mvec) |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1428 | { |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1429 | bool checkCast = false; // initialize here to use fall-through |
Vikram S. Adve | 65a2dee | 2002-08-13 17:40:54 +0000 | [diff] [blame] | 1430 | bool maskUnsignedResult = false; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1431 | int nextRule; |
| 1432 | int forwardOperandNum = -1; |
Chris Lattner | ea45d7b | 2002-12-28 20:19:44 +0000 | [diff] [blame] | 1433 | unsigned allocaSize = 0; |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1434 | MachineInstr* M, *M2; |
Chris Lattner | ea45d7b | 2002-12-28 20:19:44 +0000 | [diff] [blame] | 1435 | unsigned L; |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1436 | |
| 1437 | mvec.clear(); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1438 | |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 1439 | // If the code for this instruction was folded into the parent (user), |
| 1440 | // then do nothing! |
| 1441 | if (subtreeRoot->isFoldedIntoParent()) |
| 1442 | return; |
| 1443 | |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1444 | // |
| 1445 | // Let's check for chain rules outside the switch so that we don't have |
| 1446 | // to duplicate the list of chain rule production numbers here again |
| 1447 | // |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1448 | if (ThisIsAChainRule(ruleForNode)) { |
| 1449 | // Chain rules have a single nonterminal on the RHS. |
| 1450 | // Get the rule that matches the RHS non-terminal and use that instead. |
| 1451 | // |
| 1452 | assert(nts[0] && ! nts[1] |
| 1453 | && "A chain rule should have only one RHS non-terminal!"); |
| 1454 | nextRule = burm_rule(subtreeRoot->state, nts[0]); |
| 1455 | nts = burm_nts[nextRule]; |
| 1456 | GetInstructionsByRule(subtreeRoot, nextRule, nts, target, mvec); |
| 1457 | } else { |
| 1458 | switch(ruleForNode) { |
| 1459 | case 1: // stmt: Ret |
| 1460 | case 2: // stmt: RetValue(reg) |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 1461 | { // NOTE: Prepass of register allocation is responsible |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1462 | // for moving return value to appropriate register. |
| 1463 | // Mark the return-address register as a hidden virtual reg. |
Vikram S. Adve | a995e60 | 2001-10-11 04:23:19 +0000 | [diff] [blame] | 1464 | // Mark the return value register as an implicit ref of |
| 1465 | // the machine instruction. |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 1466 | // Finally put a NOP in the delay slot. |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 1467 | ReturnInst *returnInstr = |
| 1468 | cast<ReturnInst>(subtreeRoot->getInstruction()); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1469 | assert(returnInstr->getOpcode() == Instruction::Ret); |
| 1470 | |
Chris Lattner | 9c46108 | 2002-02-03 07:50:56 +0000 | [diff] [blame] | 1471 | Instruction* returnReg = new TmpInstruction(returnInstr); |
| 1472 | MachineCodeForInstruction::get(returnInstr).addTemp(returnReg); |
Vikram S. Adve | fb36112 | 2001-10-22 13:36:31 +0000 | [diff] [blame] | 1473 | |
Misha Brukman | 91aee47 | 2003-05-27 22:37:00 +0000 | [diff] [blame] | 1474 | M = BuildMI(V9::JMPLRETi, 3).addReg(returnReg).addSImm(8) |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 1475 | .addMReg(target.getRegInfo().getZeroRegNum(), MOTy::Def); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1476 | |
Vikram S. Adve | a995e60 | 2001-10-11 04:23:19 +0000 | [diff] [blame] | 1477 | if (returnInstr->getReturnValue() != NULL) |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1478 | M->addImplicitRef(returnInstr->getReturnValue()); |
Vikram S. Adve | a995e60 | 2001-10-11 04:23:19 +0000 | [diff] [blame] | 1479 | |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1480 | mvec.push_back(M); |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 1481 | mvec.push_back(BuildMI(V9::NOP, 0)); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1482 | |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1483 | break; |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 1484 | } |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1485 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1486 | case 3: // stmt: Store(reg,reg) |
| 1487 | case 4: // stmt: Store(reg,ptrreg) |
| 1488 | SetOperandsForMemInstr(ChooseStoreInstruction( |
Chris Lattner | 54e898e | 2003-01-15 19:23:34 +0000 | [diff] [blame] | 1489 | subtreeRoot->leftChild()->getValue()->getType()), |
| 1490 | mvec, subtreeRoot, target); |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1491 | break; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1492 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1493 | case 5: // stmt: BrUncond |
| 1494 | { |
| 1495 | BranchInst *BI = cast<BranchInst>(subtreeRoot->getInstruction()); |
| 1496 | mvec.push_back(BuildMI(V9::BA, 1).addPCDisp(BI->getSuccessor(0))); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1497 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1498 | // delay slot |
| 1499 | mvec.push_back(BuildMI(V9::NOP, 0)); |
| 1500 | break; |
| 1501 | } |
| 1502 | |
| 1503 | case 206: // stmt: BrCond(setCCconst) |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 1504 | { // setCCconst => boolean was computed with `%b = setCC type reg1 const' |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1505 | // If the constant is ZERO, we can use the branch-on-integer-register |
| 1506 | // instructions and avoid the SUBcc instruction entirely. |
| 1507 | // Otherwise this is just the same as case 5, so just fall through. |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 1508 | // |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1509 | InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild(); |
| 1510 | assert(constNode && |
| 1511 | constNode->getNodeType() ==InstrTreeNode::NTConstNode); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 1512 | Constant *constVal = cast<Constant>(constNode->getValue()); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1513 | bool isValidConst; |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 1514 | |
Chris Lattner | 0c4e886 | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 1515 | if ((constVal->getType()->isInteger() |
Chris Lattner | 9b62503 | 2002-05-06 16:15:30 +0000 | [diff] [blame] | 1516 | || isa<PointerType>(constVal->getType())) |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1517 | && GetConstantValueAsSignedInt(constVal, isValidConst) == 0 |
| 1518 | && isValidConst) |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1519 | { |
| 1520 | // That constant is a zero after all... |
| 1521 | // Use the left child of setCC as the first argument! |
| 1522 | // Mark the setCC node so that no code is generated for it. |
| 1523 | InstructionNode* setCCNode = (InstructionNode*) |
| 1524 | subtreeRoot->leftChild(); |
| 1525 | assert(setCCNode->getOpLabel() == SetCCOp); |
| 1526 | setCCNode->markFoldedIntoParent(); |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 1527 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1528 | BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction()); |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 1529 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1530 | M = BuildMI(ChooseBprInstruction(subtreeRoot), 2) |
| 1531 | .addReg(setCCNode->leftChild()->getValue()) |
| 1532 | .addPCDisp(brInst->getSuccessor(0)); |
| 1533 | mvec.push_back(M); |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 1534 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1535 | // delay slot |
| 1536 | mvec.push_back(BuildMI(V9::NOP, 0)); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1537 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1538 | // false branch |
| 1539 | mvec.push_back(BuildMI(V9::BA, 1) |
| 1540 | .addPCDisp(brInst->getSuccessor(1))); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1541 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1542 | // delay slot |
| 1543 | mvec.push_back(BuildMI(V9::NOP, 0)); |
| 1544 | break; |
| 1545 | } |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1546 | // ELSE FALL THROUGH |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 1547 | } |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1548 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1549 | case 6: // stmt: BrCond(setCC) |
Vikram S. Adve | 30a6f49 | 2002-08-22 02:56:10 +0000 | [diff] [blame] | 1550 | { // bool => boolean was computed with SetCC. |
| 1551 | // The branch to use depends on whether it is FP, signed, or unsigned. |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 1552 | // If it is an integer CC, we also need to find the unique |
| 1553 | // TmpInstruction representing that CC. |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1554 | // |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 1555 | BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction()); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1556 | bool isFPBranch; |
Chris Lattner | 54e898e | 2003-01-15 19:23:34 +0000 | [diff] [blame] | 1557 | unsigned Opcode = ChooseBccInstruction(subtreeRoot, isFPBranch); |
Vikram S. Adve | ff5a09e | 2001-11-08 05:04:09 +0000 | [diff] [blame] | 1558 | Value* ccValue = GetTmpForCC(subtreeRoot->leftChild()->getValue(), |
| 1559 | brInst->getParent()->getParent(), |
| 1560 | isFPBranch? Type::FloatTy : Type::IntTy); |
Chris Lattner | 54e898e | 2003-01-15 19:23:34 +0000 | [diff] [blame] | 1561 | M = BuildMI(Opcode, 2).addCCReg(ccValue) |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1562 | .addPCDisp(brInst->getSuccessor(0)); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1563 | mvec.push_back(M); |
Vikram S. Adve | 30a6f49 | 2002-08-22 02:56:10 +0000 | [diff] [blame] | 1564 | |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1565 | // delay slot |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 1566 | mvec.push_back(BuildMI(V9::NOP, 0)); |
Vikram S. Adve | 30a6f49 | 2002-08-22 02:56:10 +0000 | [diff] [blame] | 1567 | |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1568 | // false branch |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 1569 | mvec.push_back(BuildMI(V9::BA, 1).addPCDisp(brInst->getSuccessor(1))); |
Vikram S. Adve | 30a6f49 | 2002-08-22 02:56:10 +0000 | [diff] [blame] | 1570 | |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1571 | // delay slot |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 1572 | mvec.push_back(BuildMI(V9::NOP, 0)); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1573 | break; |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 1574 | } |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1575 | |
| 1576 | case 208: // stmt: BrCond(boolconst) |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 1577 | { |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1578 | // boolconst => boolean is a constant; use BA to first or second label |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 1579 | Constant* constVal = |
| 1580 | cast<Constant>(subtreeRoot->leftChild()->getValue()); |
| 1581 | unsigned dest = cast<ConstantBool>(constVal)->getValue()? 0 : 1; |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1582 | |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 1583 | M = BuildMI(V9::BA, 1).addPCDisp( |
Chris Lattner | 3550420 | 2002-04-27 03:14:39 +0000 | [diff] [blame] | 1584 | cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(dest)); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1585 | mvec.push_back(M); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1586 | |
| 1587 | // delay slot |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 1588 | mvec.push_back(BuildMI(V9::NOP, 0)); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1589 | break; |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 1590 | } |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1591 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1592 | case 8: // stmt: BrCond(boolreg) |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 1593 | { // boolreg => boolean is stored in an existing register. |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1594 | // Just use the branch-on-integer-register instruction! |
| 1595 | // |
Chris Lattner | 54e898e | 2003-01-15 19:23:34 +0000 | [diff] [blame] | 1596 | BranchInst *BI = cast<BranchInst>(subtreeRoot->getInstruction()); |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 1597 | M = BuildMI(V9::BRNZ, 2).addReg(subtreeRoot->leftChild()->getValue()) |
Chris Lattner | 54e898e | 2003-01-15 19:23:34 +0000 | [diff] [blame] | 1598 | .addPCDisp(BI->getSuccessor(0)); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1599 | mvec.push_back(M); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1600 | |
| 1601 | // delay slot |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 1602 | mvec.push_back(BuildMI(V9::NOP, 0)); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1603 | |
| 1604 | // false branch |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 1605 | mvec.push_back(BuildMI(V9::BA, 1).addPCDisp(BI->getSuccessor(1))); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1606 | |
| 1607 | // delay slot |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 1608 | mvec.push_back(BuildMI(V9::NOP, 0)); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1609 | break; |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 1610 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1611 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1612 | case 9: // stmt: Switch(reg) |
| 1613 | assert(0 && "*** SWITCH instruction is not implemented yet."); |
| 1614 | break; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 1615 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1616 | case 10: // reg: VRegList(reg, reg) |
| 1617 | assert(0 && "VRegList should never be the topmost non-chain rule"); |
| 1618 | break; |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1619 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1620 | case 21: // bool: Not(bool,reg): Both these are implemented as: |
| 1621 | case 421: // reg: BNot(reg,reg): reg = reg XOR-NOT 0 |
Vikram S. Adve | ce08e1d | 2002-08-15 14:17:37 +0000 | [diff] [blame] | 1622 | { // First find the unary operand. It may be left or right, usually right. |
| 1623 | Value* notArg = BinaryOperator::getNotArgument( |
| 1624 | cast<BinaryOperator>(subtreeRoot->getInstruction())); |
Chris Lattner | 00dca91 | 2003-01-15 17:47:49 +0000 | [diff] [blame] | 1625 | unsigned ZeroReg = target.getRegInfo().getZeroRegNum(); |
Misha Brukman | 91aee47 | 2003-05-27 22:37:00 +0000 | [diff] [blame] | 1626 | mvec.push_back(BuildMI(V9::XNORr, 3).addReg(notArg).addMReg(ZeroReg) |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1627 | .addRegDef(subtreeRoot->getValue())); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1628 | break; |
Vikram S. Adve | ce08e1d | 2002-08-15 14:17:37 +0000 | [diff] [blame] | 1629 | } |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1630 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1631 | case 22: // reg: ToBoolTy(reg): |
Vikram S. Adve | 6ad7c55 | 2001-11-09 02:18:16 +0000 | [diff] [blame] | 1632 | { |
| 1633 | const Type* opType = subtreeRoot->leftChild()->getValue()->getType(); |
Chris Lattner | 0c4e886 | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 1634 | assert(opType->isIntegral() || isa<PointerType>(opType)); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1635 | forwardOperandNum = 0; // forward first operand to user |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1636 | break; |
Vikram S. Adve | 6ad7c55 | 2001-11-09 02:18:16 +0000 | [diff] [blame] | 1637 | } |
| 1638 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1639 | case 23: // reg: ToUByteTy(reg) |
| 1640 | case 24: // reg: ToSByteTy(reg) |
| 1641 | case 25: // reg: ToUShortTy(reg) |
| 1642 | case 26: // reg: ToShortTy(reg) |
| 1643 | case 27: // reg: ToUIntTy(reg) |
| 1644 | case 28: // reg: ToIntTy(reg) |
Vikram S. Adve | 6ad7c55 | 2001-11-09 02:18:16 +0000 | [diff] [blame] | 1645 | { |
Vikram S. Adve | 94c4081 | 2002-09-27 14:33:08 +0000 | [diff] [blame] | 1646 | //====================================================================== |
| 1647 | // Rules for integer conversions: |
| 1648 | // |
| 1649 | //-------- |
| 1650 | // From ISO 1998 C++ Standard, Sec. 4.7: |
| 1651 | // |
| 1652 | // 2. If the destination type is unsigned, the resulting value is |
| 1653 | // the least unsigned integer congruent to the source integer |
| 1654 | // (modulo 2n where n is the number of bits used to represent the |
| 1655 | // unsigned type). [Note: In a two s complement representation, |
| 1656 | // this conversion is conceptual and there is no change in the |
| 1657 | // bit pattern (if there is no truncation). ] |
| 1658 | // |
| 1659 | // 3. If the destination type is signed, the value is unchanged if |
| 1660 | // it can be represented in the destination type (and bitfield width); |
| 1661 | // otherwise, the value is implementation-defined. |
| 1662 | //-------- |
| 1663 | // |
| 1664 | // Since we assume 2s complement representations, this implies: |
| 1665 | // |
| 1666 | // -- if operand is smaller than destination, zero-extend or sign-extend |
| 1667 | // according to the signedness of the *operand*: source decides. |
| 1668 | // ==> we have to do nothing here! |
| 1669 | // |
| 1670 | // -- if operand is same size as or larger than destination, and the |
| 1671 | // destination is *unsigned*, zero-extend the operand: dest. decides |
| 1672 | // |
| 1673 | // -- if operand is same size as or larger than destination, and the |
| 1674 | // destination is *signed*, the choice is implementation defined: |
| 1675 | // we sign-extend the operand: i.e., again dest. decides. |
| 1676 | // Note: this matches both Sun's cc and gcc3.2. |
| 1677 | //====================================================================== |
| 1678 | |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 1679 | Instruction* destI = subtreeRoot->getInstruction(); |
| 1680 | Value* opVal = subtreeRoot->leftChild()->getValue(); |
Vikram S. Adve | 94c4081 | 2002-09-27 14:33:08 +0000 | [diff] [blame] | 1681 | const Type* opType = opVal->getType(); |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 1682 | if (opType->isIntegral() || isa<PointerType>(opType)) { |
| 1683 | unsigned opSize = target.getTargetData().getTypeSize(opType); |
| 1684 | unsigned destSize = |
| 1685 | target.getTargetData().getTypeSize(destI->getType()); |
| 1686 | if (opSize >= destSize) { |
| 1687 | // Operand is same size as or larger than dest: |
| 1688 | // zero- or sign-extend, according to the signeddness of |
| 1689 | // the destination (see above). |
| 1690 | if (destI->getType()->isSigned()) |
| 1691 | target.getInstrInfo().CreateSignExtensionInstructions(target, |
Vikram S. Adve | 94c4081 | 2002-09-27 14:33:08 +0000 | [diff] [blame] | 1692 | destI->getParent()->getParent(), opVal, destI, 8*destSize, |
| 1693 | mvec, MachineCodeForInstruction::get(destI)); |
Vikram S. Adve | 1e60669 | 2002-07-31 21:01:34 +0000 | [diff] [blame] | 1694 | else |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 1695 | target.getInstrInfo().CreateZeroExtensionInstructions(target, |
| 1696 | destI->getParent()->getParent(), opVal, destI, 8*destSize, |
| 1697 | mvec, MachineCodeForInstruction::get(destI)); |
| 1698 | } else |
| 1699 | forwardOperandNum = 0; // forward first operand to user |
| 1700 | } else if (opType->isFloatingPoint()) { |
| 1701 | CreateCodeToConvertFloatToInt(target, opVal, destI, mvec, |
| 1702 | MachineCodeForInstruction::get(destI)); |
| 1703 | if (destI->getType()->isUnsigned()) |
| 1704 | maskUnsignedResult = true; // not handled by fp->int code |
| 1705 | } else |
Vikram S. Adve | 1e60669 | 2002-07-31 21:01:34 +0000 | [diff] [blame] | 1706 | assert(0 && "Unrecognized operand type for convert-to-unsigned"); |
| 1707 | |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1708 | break; |
Vikram S. Adve | 6ad7c55 | 2001-11-09 02:18:16 +0000 | [diff] [blame] | 1709 | } |
Vikram S. Adve | 94c4081 | 2002-09-27 14:33:08 +0000 | [diff] [blame] | 1710 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1711 | case 29: // reg: ToULongTy(reg) |
| 1712 | case 30: // reg: ToLongTy(reg) |
Vikram S. Adve | 6ad7c55 | 2001-11-09 02:18:16 +0000 | [diff] [blame] | 1713 | { |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 1714 | Value* opVal = subtreeRoot->leftChild()->getValue(); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 1715 | const Type* opType = opVal->getType(); |
Chris Lattner | 0c4e886 | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 1716 | if (opType->isIntegral() || isa<PointerType>(opType)) |
Vikram S. Adve | 94c4081 | 2002-09-27 14:33:08 +0000 | [diff] [blame] | 1717 | forwardOperandNum = 0; // forward first operand to user |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 1718 | else if (opType->isFloatingPoint()) { |
| 1719 | Instruction* destI = subtreeRoot->getInstruction(); |
| 1720 | CreateCodeToConvertFloatToInt(target, opVal, destI, mvec, |
| 1721 | MachineCodeForInstruction::get(destI)); |
| 1722 | } else |
Vikram S. Adve | 1e60669 | 2002-07-31 21:01:34 +0000 | [diff] [blame] | 1723 | assert(0 && "Unrecognized operand type for convert-to-signed"); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1724 | break; |
Vikram S. Adve | 94c4081 | 2002-09-27 14:33:08 +0000 | [diff] [blame] | 1725 | } |
| 1726 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1727 | case 31: // reg: ToFloatTy(reg): |
| 1728 | case 32: // reg: ToDoubleTy(reg): |
| 1729 | case 232: // reg: ToDoubleTy(Constant): |
Vikram S. Adve | ec7f482 | 2002-09-09 14:54:21 +0000 | [diff] [blame] | 1730 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1731 | // If this instruction has a parent (a user) in the tree |
| 1732 | // and the user is translated as an FsMULd instruction, |
| 1733 | // then the cast is unnecessary. So check that first. |
| 1734 | // In the future, we'll want to do the same for the FdMULq instruction, |
| 1735 | // so do the check here instead of only for ToFloatTy(reg). |
| 1736 | // |
| 1737 | if (subtreeRoot->parent() != NULL) { |
| 1738 | const MachineCodeForInstruction& mcfi = |
| 1739 | MachineCodeForInstruction::get( |
Vikram S. Adve | ec7f482 | 2002-09-09 14:54:21 +0000 | [diff] [blame] | 1740 | cast<InstructionNode>(subtreeRoot->parent())->getInstruction()); |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1741 | if (mcfi.size() == 0 || mcfi.front()->getOpCode() == V9::FSMULD) |
| 1742 | forwardOperandNum = 0; // forward first operand to user |
| 1743 | } |
Vikram S. Adve | ec7f482 | 2002-09-09 14:54:21 +0000 | [diff] [blame] | 1744 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1745 | if (forwardOperandNum != 0) { // we do need the cast |
| 1746 | Value* leftVal = subtreeRoot->leftChild()->getValue(); |
| 1747 | const Type* opType = leftVal->getType(); |
| 1748 | MachineOpCode opCode=ChooseConvertToFloatInstr( |
| 1749 | subtreeRoot->getOpLabel(), opType); |
| 1750 | if (opCode == V9::INVALID_OPCODE) { // no conversion needed |
| 1751 | forwardOperandNum = 0; // forward first operand to user |
| 1752 | } else { |
| 1753 | // If the source operand is a non-FP type it must be |
| 1754 | // first copied from int to float register via memory! |
| 1755 | Instruction *dest = subtreeRoot->getInstruction(); |
| 1756 | Value* srcForCast; |
| 1757 | int n = 0; |
| 1758 | if (! opType->isFloatingPoint()) { |
| 1759 | // Create a temporary to represent the FP register |
| 1760 | // into which the integer will be copied via memory. |
| 1761 | // The type of this temporary will determine the FP |
| 1762 | // register used: single-prec for a 32-bit int or smaller, |
| 1763 | // double-prec for a 64-bit int. |
| 1764 | // |
| 1765 | uint64_t srcSize = |
| 1766 | target.getTargetData().getTypeSize(leftVal->getType()); |
| 1767 | Type* tmpTypeToUse = |
| 1768 | (srcSize <= 4)? Type::FloatTy : Type::DoubleTy; |
| 1769 | srcForCast = new TmpInstruction(tmpTypeToUse, dest); |
| 1770 | MachineCodeForInstruction &destMCFI = |
| 1771 | MachineCodeForInstruction::get(dest); |
| 1772 | destMCFI.addTemp(srcForCast); |
Vikram S. Adve | babc0fa | 2002-09-05 18:32:13 +0000 | [diff] [blame] | 1773 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1774 | target.getInstrInfo().CreateCodeToCopyIntToFloat(target, |
Vikram S. Adve | ff5a09e | 2001-11-08 05:04:09 +0000 | [diff] [blame] | 1775 | dest->getParent()->getParent(), |
Vikram S. Adve | babc0fa | 2002-09-05 18:32:13 +0000 | [diff] [blame] | 1776 | leftVal, cast<Instruction>(srcForCast), |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 1777 | mvec, destMCFI); |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1778 | } else |
| 1779 | srcForCast = leftVal; |
| 1780 | |
| 1781 | M = BuildMI(opCode, 2).addReg(srcForCast).addRegDef(dest); |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 1782 | mvec.push_back(M); |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 1783 | } |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1784 | } |
| 1785 | break; |
| 1786 | |
| 1787 | case 19: // reg: ToArrayTy(reg): |
| 1788 | case 20: // reg: ToPointerTy(reg): |
| 1789 | forwardOperandNum = 0; // forward first operand to user |
| 1790 | break; |
| 1791 | |
| 1792 | case 233: // reg: Add(reg, Constant) |
| 1793 | maskUnsignedResult = true; |
| 1794 | M = CreateAddConstInstruction(subtreeRoot); |
| 1795 | if (M != NULL) { |
| 1796 | mvec.push_back(M); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1797 | break; |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1798 | } |
| 1799 | // ELSE FALL THROUGH |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1800 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1801 | case 33: // reg: Add(reg, reg) |
| 1802 | maskUnsignedResult = true; |
| 1803 | Add3OperandInstr(ChooseAddInstruction(subtreeRoot), subtreeRoot, mvec); |
| 1804 | break; |
| 1805 | |
| 1806 | case 234: // reg: Sub(reg, Constant) |
| 1807 | maskUnsignedResult = true; |
| 1808 | M = CreateSubConstInstruction(subtreeRoot); |
| 1809 | if (M != NULL) { |
| 1810 | mvec.push_back(M); |
| 1811 | break; |
| 1812 | } |
| 1813 | // ELSE FALL THROUGH |
| 1814 | |
| 1815 | case 34: // reg: Sub(reg, reg) |
| 1816 | maskUnsignedResult = true; |
| 1817 | Add3OperandInstr(ChooseSubInstructionByType( |
Chris Lattner | 54e898e | 2003-01-15 19:23:34 +0000 | [diff] [blame] | 1818 | subtreeRoot->getInstruction()->getType()), |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1819 | subtreeRoot, mvec); |
| 1820 | break; |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1821 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1822 | case 135: // reg: Mul(todouble, todouble) |
| 1823 | checkCast = true; |
| 1824 | // FALL THROUGH |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1825 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1826 | case 35: // reg: Mul(reg, reg) |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1827 | { |
Vikram S. Adve | 65a2dee | 2002-08-13 17:40:54 +0000 | [diff] [blame] | 1828 | maskUnsignedResult = true; |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1829 | MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot)) |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 1830 | ? V9::FSMULD |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1831 | : INVALID_MACHINE_OPCODE); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 1832 | Instruction* mulInstr = subtreeRoot->getInstruction(); |
| 1833 | CreateMulInstruction(target, mulInstr->getParent()->getParent(), |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1834 | subtreeRoot->leftChild()->getValue(), |
| 1835 | subtreeRoot->rightChild()->getValue(), |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 1836 | mulInstr, mvec, |
| 1837 | MachineCodeForInstruction::get(mulInstr),forceOp); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1838 | break; |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1839 | } |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1840 | case 335: // reg: Mul(todouble, todoubleConst) |
| 1841 | checkCast = true; |
| 1842 | // FALL THROUGH |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1843 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1844 | case 235: // reg: Mul(reg, Constant) |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1845 | { |
Vikram S. Adve | 65a2dee | 2002-08-13 17:40:54 +0000 | [diff] [blame] | 1846 | maskUnsignedResult = true; |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1847 | MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot)) |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 1848 | ? V9::FSMULD |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1849 | : INVALID_MACHINE_OPCODE); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 1850 | Instruction* mulInstr = subtreeRoot->getInstruction(); |
| 1851 | CreateMulInstruction(target, mulInstr->getParent()->getParent(), |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1852 | subtreeRoot->leftChild()->getValue(), |
| 1853 | subtreeRoot->rightChild()->getValue(), |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 1854 | mulInstr, mvec, |
| 1855 | MachineCodeForInstruction::get(mulInstr), |
| 1856 | forceOp); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1857 | break; |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1858 | } |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1859 | case 236: // reg: Div(reg, Constant) |
| 1860 | maskUnsignedResult = true; |
| 1861 | L = mvec.size(); |
| 1862 | CreateDivConstInstruction(target, subtreeRoot, mvec); |
| 1863 | if (mvec.size() > L) |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1864 | break; |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1865 | // ELSE FALL THROUGH |
| 1866 | |
| 1867 | case 36: // reg: Div(reg, reg) |
| 1868 | maskUnsignedResult = true; |
| 1869 | Add3OperandInstr(ChooseDivInstruction(target, subtreeRoot), |
| 1870 | subtreeRoot, mvec); |
| 1871 | break; |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1872 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1873 | case 37: // reg: Rem(reg, reg) |
| 1874 | case 237: // reg: Rem(reg, Constant) |
Vikram S. Adve | 510eec7 | 2001-11-04 21:59:14 +0000 | [diff] [blame] | 1875 | { |
Vikram S. Adve | 65a2dee | 2002-08-13 17:40:54 +0000 | [diff] [blame] | 1876 | maskUnsignedResult = true; |
Vikram S. Adve | 510eec7 | 2001-11-04 21:59:14 +0000 | [diff] [blame] | 1877 | Instruction* remInstr = subtreeRoot->getInstruction(); |
| 1878 | |
Chris Lattner | 9c46108 | 2002-02-03 07:50:56 +0000 | [diff] [blame] | 1879 | TmpInstruction* quot = new TmpInstruction( |
Vikram S. Adve | 510eec7 | 2001-11-04 21:59:14 +0000 | [diff] [blame] | 1880 | subtreeRoot->leftChild()->getValue(), |
| 1881 | subtreeRoot->rightChild()->getValue()); |
Chris Lattner | 9c46108 | 2002-02-03 07:50:56 +0000 | [diff] [blame] | 1882 | TmpInstruction* prod = new TmpInstruction( |
Vikram S. Adve | 510eec7 | 2001-11-04 21:59:14 +0000 | [diff] [blame] | 1883 | quot, |
| 1884 | subtreeRoot->rightChild()->getValue()); |
Chris Lattner | 9c46108 | 2002-02-03 07:50:56 +0000 | [diff] [blame] | 1885 | MachineCodeForInstruction::get(remInstr).addTemp(quot).addTemp(prod); |
Vikram S. Adve | 510eec7 | 2001-11-04 21:59:14 +0000 | [diff] [blame] | 1886 | |
Chris Lattner | 54e898e | 2003-01-15 19:23:34 +0000 | [diff] [blame] | 1887 | M = BuildMI(ChooseDivInstruction(target, subtreeRoot), 3) |
| 1888 | .addReg(subtreeRoot->leftChild()->getValue()) |
| 1889 | .addReg(subtreeRoot->rightChild()->getValue()) |
| 1890 | .addRegDef(quot); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1891 | mvec.push_back(M); |
Vikram S. Adve | 510eec7 | 2001-11-04 21:59:14 +0000 | [diff] [blame] | 1892 | |
Chris Lattner | e5b1ed9 | 2003-01-15 00:03:28 +0000 | [diff] [blame] | 1893 | unsigned MulOpcode = |
| 1894 | ChooseMulInstructionByType(subtreeRoot->getInstruction()->getType()); |
| 1895 | Value *MulRHS = subtreeRoot->rightChild()->getValue(); |
| 1896 | M = BuildMI(MulOpcode, 3).addReg(quot).addReg(MulRHS).addReg(prod, |
| 1897 | MOTy::Def); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1898 | mvec.push_back(M); |
Vikram S. Adve | 510eec7 | 2001-11-04 21:59:14 +0000 | [diff] [blame] | 1899 | |
Chris Lattner | 54e898e | 2003-01-15 19:23:34 +0000 | [diff] [blame] | 1900 | unsigned Opcode = ChooseSubInstructionByType( |
| 1901 | subtreeRoot->getInstruction()->getType()); |
| 1902 | M = BuildMI(Opcode, 3).addReg(subtreeRoot->leftChild()->getValue()) |
| 1903 | .addReg(prod).addRegDef(subtreeRoot->getValue()); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 1904 | mvec.push_back(M); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1905 | break; |
Vikram S. Adve | 510eec7 | 2001-11-04 21:59:14 +0000 | [diff] [blame] | 1906 | } |
| 1907 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1908 | case 38: // bool: And(bool, bool) |
| 1909 | case 238: // bool: And(bool, boolconst) |
| 1910 | case 338: // reg : BAnd(reg, reg) |
| 1911 | case 538: // reg : BAnd(reg, Constant) |
| 1912 | Add3OperandInstr(V9::ANDr, subtreeRoot, mvec); |
| 1913 | break; |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1914 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1915 | case 138: // bool: And(bool, not) |
| 1916 | case 438: // bool: BAnd(bool, bnot) |
Vikram S. Adve | ce08e1d | 2002-08-15 14:17:37 +0000 | [diff] [blame] | 1917 | { // Use the argument of NOT as the second argument! |
| 1918 | // Mark the NOT node so that no code is generated for it. |
| 1919 | InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild(); |
| 1920 | Value* notArg = BinaryOperator::getNotArgument( |
| 1921 | cast<BinaryOperator>(notNode->getInstruction())); |
| 1922 | notNode->markFoldedIntoParent(); |
Chris Lattner | e5b1ed9 | 2003-01-15 00:03:28 +0000 | [diff] [blame] | 1923 | Value *LHS = subtreeRoot->leftChild()->getValue(); |
| 1924 | Value *Dest = subtreeRoot->getValue(); |
Misha Brukman | 91aee47 | 2003-05-27 22:37:00 +0000 | [diff] [blame] | 1925 | mvec.push_back(BuildMI(V9::ANDNr, 3).addReg(LHS).addReg(notArg) |
Chris Lattner | e5b1ed9 | 2003-01-15 00:03:28 +0000 | [diff] [blame] | 1926 | .addReg(Dest, MOTy::Def)); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1927 | break; |
Vikram S. Adve | ce08e1d | 2002-08-15 14:17:37 +0000 | [diff] [blame] | 1928 | } |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1929 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1930 | case 39: // bool: Or(bool, bool) |
| 1931 | case 239: // bool: Or(bool, boolconst) |
| 1932 | case 339: // reg : BOr(reg, reg) |
| 1933 | case 539: // reg : BOr(reg, Constant) |
| 1934 | Add3OperandInstr(V9::ORr, subtreeRoot, mvec); |
| 1935 | break; |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1936 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1937 | case 139: // bool: Or(bool, not) |
| 1938 | case 439: // bool: BOr(bool, bnot) |
Vikram S. Adve | ce08e1d | 2002-08-15 14:17:37 +0000 | [diff] [blame] | 1939 | { // Use the argument of NOT as the second argument! |
| 1940 | // Mark the NOT node so that no code is generated for it. |
| 1941 | InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild(); |
| 1942 | Value* notArg = BinaryOperator::getNotArgument( |
| 1943 | cast<BinaryOperator>(notNode->getInstruction())); |
| 1944 | notNode->markFoldedIntoParent(); |
Chris Lattner | e5b1ed9 | 2003-01-15 00:03:28 +0000 | [diff] [blame] | 1945 | Value *LHS = subtreeRoot->leftChild()->getValue(); |
| 1946 | Value *Dest = subtreeRoot->getValue(); |
Misha Brukman | 91aee47 | 2003-05-27 22:37:00 +0000 | [diff] [blame] | 1947 | mvec.push_back(BuildMI(V9::ORNr, 3).addReg(LHS).addReg(notArg) |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 1948 | .addReg(Dest, MOTy::Def)); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1949 | break; |
Vikram S. Adve | ce08e1d | 2002-08-15 14:17:37 +0000 | [diff] [blame] | 1950 | } |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1951 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1952 | case 40: // bool: Xor(bool, bool) |
| 1953 | case 240: // bool: Xor(bool, boolconst) |
| 1954 | case 340: // reg : BXor(reg, reg) |
| 1955 | case 540: // reg : BXor(reg, Constant) |
| 1956 | Add3OperandInstr(V9::XORr, subtreeRoot, mvec); |
| 1957 | break; |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1958 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1959 | case 140: // bool: Xor(bool, not) |
| 1960 | case 440: // bool: BXor(bool, bnot) |
Vikram S. Adve | ce08e1d | 2002-08-15 14:17:37 +0000 | [diff] [blame] | 1961 | { // Use the argument of NOT as the second argument! |
| 1962 | // Mark the NOT node so that no code is generated for it. |
| 1963 | InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild(); |
| 1964 | Value* notArg = BinaryOperator::getNotArgument( |
| 1965 | cast<BinaryOperator>(notNode->getInstruction())); |
| 1966 | notNode->markFoldedIntoParent(); |
Chris Lattner | e5b1ed9 | 2003-01-15 00:03:28 +0000 | [diff] [blame] | 1967 | Value *LHS = subtreeRoot->leftChild()->getValue(); |
| 1968 | Value *Dest = subtreeRoot->getValue(); |
Misha Brukman | 91aee47 | 2003-05-27 22:37:00 +0000 | [diff] [blame] | 1969 | mvec.push_back(BuildMI(V9::XNORr, 3).addReg(LHS).addReg(notArg) |
Misha Brukman | a98cd45 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 1970 | .addReg(Dest, MOTy::Def)); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1971 | break; |
Vikram S. Adve | ce08e1d | 2002-08-15 14:17:37 +0000 | [diff] [blame] | 1972 | } |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1973 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 1974 | case 41: // boolconst: SetCC(reg, Constant) |
| 1975 | // |
| 1976 | // If the SetCC was folded into the user (parent), it will be |
| 1977 | // caught above. All other cases are the same as case 42, |
| 1978 | // so just fall through. |
| 1979 | // |
| 1980 | case 42: // bool: SetCC(reg, reg): |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1981 | { |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 1982 | // This generates a SUBCC instruction, putting the difference in |
| 1983 | // a result register, and setting a condition code. |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 1984 | // |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 1985 | // If the boolean result of the SetCC is used by anything other |
Vikram S. Adve | 6418eac | 2002-07-08 23:30:14 +0000 | [diff] [blame] | 1986 | // than a branch instruction, or if it is used outside the current |
| 1987 | // basic block, the boolean must be |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 1988 | // computed and stored in the result register. Otherwise, discard |
| 1989 | // the difference (by using %g0) and keep only the condition code. |
| 1990 | // |
| 1991 | // To compute the boolean result in a register we use a conditional |
| 1992 | // move, unless the result of the SUBCC instruction can be used as |
| 1993 | // the bool! This assumes that zero is FALSE and any non-zero |
| 1994 | // integer is TRUE. |
| 1995 | // |
| 1996 | InstructionNode* parentNode = (InstructionNode*) subtreeRoot->parent(); |
| 1997 | Instruction* setCCInstr = subtreeRoot->getInstruction(); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 1998 | |
Vikram S. Adve | 6418eac | 2002-07-08 23:30:14 +0000 | [diff] [blame] | 1999 | bool keepBoolVal = parentNode == NULL || |
| 2000 | ! AllUsesAreBranches(setCCInstr); |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 2001 | bool subValIsBoolVal = setCCInstr->getOpcode() == Instruction::SetNE; |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2002 | bool keepSubVal = keepBoolVal && subValIsBoolVal; |
| 2003 | bool computeBoolVal = keepBoolVal && ! subValIsBoolVal; |
| 2004 | |
| 2005 | bool mustClearReg; |
| 2006 | int valueToMove; |
Chris Lattner | 8e5c0b4 | 2001-11-07 14:01:59 +0000 | [diff] [blame] | 2007 | MachineOpCode movOpCode = 0; |
Vikram S. Adve | 6418eac | 2002-07-08 23:30:14 +0000 | [diff] [blame] | 2008 | |
Vikram S. Adve | ff5a09e | 2001-11-08 05:04:09 +0000 | [diff] [blame] | 2009 | // Mark the 4th operand as being a CC register, and as a def |
| 2010 | // A TmpInstruction is created to represent the CC "result". |
| 2011 | // Unlike other instances of TmpInstruction, this one is used |
| 2012 | // by machine code of multiple LLVM instructions, viz., |
| 2013 | // the SetCC and the branch. Make sure to get the same one! |
| 2014 | // Note that we do this even for FP CC registers even though they |
| 2015 | // are explicit operands, because the type of the operand |
| 2016 | // needs to be a floating point condition code, not an integer |
| 2017 | // condition code. Think of this as casting the bool result to |
| 2018 | // a FP condition code register. |
| 2019 | // |
Vikram S. Adve | 6ad7c55 | 2001-11-09 02:18:16 +0000 | [diff] [blame] | 2020 | Value* leftVal = subtreeRoot->leftChild()->getValue(); |
Chris Lattner | 9b62503 | 2002-05-06 16:15:30 +0000 | [diff] [blame] | 2021 | bool isFPCompare = leftVal->getType()->isFloatingPoint(); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2022 | |
Vikram S. Adve | ff5a09e | 2001-11-08 05:04:09 +0000 | [diff] [blame] | 2023 | TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr, |
| 2024 | setCCInstr->getParent()->getParent(), |
Chris Lattner | 9b62503 | 2002-05-06 16:15:30 +0000 | [diff] [blame] | 2025 | isFPCompare ? Type::FloatTy : Type::IntTy); |
Chris Lattner | 9c46108 | 2002-02-03 07:50:56 +0000 | [diff] [blame] | 2026 | MachineCodeForInstruction::get(setCCInstr).addTemp(tmpForCC); |
Vikram S. Adve | ff5a09e | 2001-11-08 05:04:09 +0000 | [diff] [blame] | 2027 | |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 2028 | if (! isFPCompare) { |
| 2029 | // Integer condition: dest. should be %g0 or an integer register. |
| 2030 | // If result must be saved but condition is not SetEQ then we need |
| 2031 | // a separate instruction to compute the bool result, so discard |
| 2032 | // result of SUBcc instruction anyway. |
| 2033 | // |
| 2034 | if (keepSubVal) { |
| 2035 | M = BuildMI(V9::SUBccr, 4) |
| 2036 | .addReg(subtreeRoot->leftChild()->getValue()) |
| 2037 | .addReg(subtreeRoot->rightChild()->getValue()) |
| 2038 | .addRegDef(subtreeRoot->getValue()) |
| 2039 | .addCCReg(tmpForCC, MOTy::Def); |
| 2040 | } else { |
| 2041 | M = BuildMI(V9::SUBccr, 4) |
| 2042 | .addReg(subtreeRoot->leftChild()->getValue()) |
| 2043 | .addReg(subtreeRoot->rightChild()->getValue()) |
| 2044 | .addMReg(target.getRegInfo().getZeroRegNum(), MOTy::Def) |
| 2045 | .addCCReg(tmpForCC, MOTy::Def); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2046 | } |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 2047 | mvec.push_back(M); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2048 | |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 2049 | if (computeBoolVal) { |
| 2050 | // recompute bool using the integer condition codes |
| 2051 | movOpCode = |
| 2052 | ChooseMovpccAfterSub(subtreeRoot,mustClearReg,valueToMove); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2053 | } |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 2054 | } else { |
| 2055 | // FP condition: dest of FCMP should be some FCCn register |
| 2056 | M = BuildMI(ChooseFcmpInstruction(subtreeRoot), 3) |
| 2057 | .addCCReg(tmpForCC, MOTy::Def) |
| 2058 | .addReg(subtreeRoot->leftChild()->getValue()) |
| 2059 | .addRegDef(subtreeRoot->rightChild()->getValue()); |
| 2060 | mvec.push_back(M); |
| 2061 | |
| 2062 | if (computeBoolVal) { |
| 2063 | // recompute bool using the FP condition codes |
| 2064 | mustClearReg = true; |
| 2065 | valueToMove = 1; |
| 2066 | movOpCode = ChooseMovFpccInstruction(subtreeRoot); |
| 2067 | } |
| 2068 | } |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2069 | |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 2070 | if (computeBoolVal) { |
| 2071 | if (mustClearReg) { |
| 2072 | // Unconditionally set register to 0 |
| 2073 | M = BuildMI(V9::SETHI, 2).addZImm(0).addRegDef(setCCInstr); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 2074 | mvec.push_back(M); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2075 | } |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 2076 | |
| 2077 | // Now conditionally move `valueToMove' (0 or 1) into the register |
| 2078 | // Mark the register as a use (as well as a def) because the old |
| 2079 | // value should be retained if the condition is false. |
| 2080 | M = BuildMI(movOpCode, 3).addCCReg(tmpForCC).addZImm(valueToMove) |
| 2081 | .addReg(setCCInstr, MOTy::UseAndDef); |
| 2082 | mvec.push_back(M); |
| 2083 | } |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2084 | break; |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 2085 | } |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2086 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 2087 | case 51: // reg: Load(reg) |
| 2088 | case 52: // reg: Load(ptrreg) |
Chris Lattner | 54e898e | 2003-01-15 19:23:34 +0000 | [diff] [blame] | 2089 | SetOperandsForMemInstr(ChooseLoadInstruction( |
| 2090 | subtreeRoot->getValue()->getType()), |
| 2091 | mvec, subtreeRoot, target); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2092 | break; |
| 2093 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 2094 | case 55: // reg: GetElemPtr(reg) |
| 2095 | case 56: // reg: GetElemPtrIdx(reg,reg) |
| 2096 | // If the GetElemPtr was folded into the user (parent), it will be |
| 2097 | // caught above. For other cases, we have to compute the address. |
| 2098 | SetOperandsForMemInstr(V9::ADDr, mvec, subtreeRoot, target); |
| 2099 | break; |
Vikram S. Adve | d3e2648 | 2002-10-13 00:18:57 +0000 | [diff] [blame] | 2100 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 2101 | case 57: // reg: Alloca: Implement as 1 instruction: |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 2102 | { // add %fp, offsetFromFP -> result |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 2103 | AllocationInst* instr = |
| 2104 | cast<AllocationInst>(subtreeRoot->getInstruction()); |
Chris Lattner | ea45d7b | 2002-12-28 20:19:44 +0000 | [diff] [blame] | 2105 | unsigned tsize = |
| 2106 | target.getTargetData().getTypeSize(instr->getAllocatedType()); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 2107 | assert(tsize != 0); |
| 2108 | CreateCodeForFixedSizeAlloca(target, instr, tsize, 1, mvec); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2109 | break; |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 2110 | } |
Vikram S. Adve | d3e2648 | 2002-10-13 00:18:57 +0000 | [diff] [blame] | 2111 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 2112 | case 58: // reg: Alloca(reg): Implement as 3 instructions: |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2113 | // mul num, typeSz -> tmp |
| 2114 | // sub %sp, tmp -> %sp |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 2115 | { // add %sp, frameSizeBelowDynamicArea -> result |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 2116 | AllocationInst* instr = |
| 2117 | cast<AllocationInst>(subtreeRoot->getInstruction()); |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 2118 | const Type* eltType = instr->getAllocatedType(); |
| 2119 | |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 2120 | // If #elements is constant, use simpler code for fixed-size allocas |
Chris Lattner | ea45d7b | 2002-12-28 20:19:44 +0000 | [diff] [blame] | 2121 | int tsize = (int) target.getTargetData().getTypeSize(eltType); |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 2122 | Value* numElementsVal = NULL; |
| 2123 | bool isArray = instr->isArrayAllocation(); |
| 2124 | |
| 2125 | if (!isArray || |
| 2126 | isa<Constant>(numElementsVal = instr->getArraySize())) |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 2127 | { |
| 2128 | // total size is constant: generate code for fixed-size alloca |
| 2129 | unsigned numElements = isArray? |
| 2130 | cast<ConstantUInt>(numElementsVal)->getValue() : 1; |
| 2131 | CreateCodeForFixedSizeAlloca(target, instr, tsize, |
| 2132 | numElements, mvec); |
| 2133 | } |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 2134 | else // total size is not constant. |
| 2135 | CreateCodeForVariableSizeAlloca(target, instr, tsize, |
Vikram S. Adve | fd3900a | 2002-03-24 03:33:02 +0000 | [diff] [blame] | 2136 | numElementsVal, mvec); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2137 | break; |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 2138 | } |
Vikram S. Adve | d3e2648 | 2002-10-13 00:18:57 +0000 | [diff] [blame] | 2139 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 2140 | case 61: // reg: Call |
Vikram S. Adve | 4a8bb2b | 2002-09-28 16:55:41 +0000 | [diff] [blame] | 2141 | { // Generate a direct (CALL) or indirect (JMPL) call. |
| 2142 | // Mark the return-address register, the indirection |
| 2143 | // register (for indirect calls), the operands of the Call, |
| 2144 | // and the return value (if any) as implicit operands |
| 2145 | // of the machine instruction. |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 2146 | // |
Vikram S. Adve | dbc4fad | 2002-04-25 04:37:51 +0000 | [diff] [blame] | 2147 | // If this is a varargs function, floating point arguments |
| 2148 | // have to passed in integer registers so insert |
| 2149 | // copy-float-to-int instructions for each float operand. |
| 2150 | // |
Chris Lattner | b00c582 | 2001-10-02 03:41:24 +0000 | [diff] [blame] | 2151 | CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction()); |
Chris Lattner | 749655f | 2001-10-13 06:54:30 +0000 | [diff] [blame] | 2152 | Value *callee = callInstr->getCalledValue(); |
Vikram S. Adve | 5b1b47b | 2003-05-25 15:59:47 +0000 | [diff] [blame] | 2153 | Function* calledFunc = dyn_cast<Function>(callee); |
Vikram S. Adve | 4a8bb2b | 2002-09-28 16:55:41 +0000 | [diff] [blame] | 2154 | |
Vikram S. Adve | 5b1b47b | 2003-05-25 15:59:47 +0000 | [diff] [blame] | 2155 | // Check if this is an intrinsic function that needs a special code |
| 2156 | // sequence (e.g., va_start). Indirect calls cannot be special. |
Vikram S. Adve | ea21a6c | 2001-10-20 20:57:06 +0000 | [diff] [blame] | 2157 | // |
Vikram S. Adve | 5b1b47b | 2003-05-25 15:59:47 +0000 | [diff] [blame] | 2158 | bool specialIntrinsic = false; |
| 2159 | LLVMIntrinsic::ID iid; |
| 2160 | if (calledFunc && (iid=(LLVMIntrinsic::ID)calledFunc->getIntrinsicID())) |
| 2161 | specialIntrinsic = CodeGenIntrinsic(iid, *callInstr, target, mvec); |
Vikram S. Adve | a10d1a7 | 2002-03-31 19:07:35 +0000 | [diff] [blame] | 2162 | |
Vikram S. Adve | 5b1b47b | 2003-05-25 15:59:47 +0000 | [diff] [blame] | 2163 | // If not, generate the normal call sequence for the function. |
| 2164 | // This can also handle any intrinsics that are just function calls. |
| 2165 | // |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 2166 | if (! specialIntrinsic) { |
| 2167 | // Create hidden virtual register for return address with type void* |
| 2168 | TmpInstruction* retAddrReg = |
| 2169 | new TmpInstruction(PointerType::get(Type::VoidTy), callInstr); |
| 2170 | MachineCodeForInstruction::get(callInstr).addTemp(retAddrReg); |
Vikram S. Adve | 5b1b47b | 2003-05-25 15:59:47 +0000 | [diff] [blame] | 2171 | |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 2172 | // Generate the machine instruction and its operands. |
| 2173 | // Use CALL for direct function calls; this optimistically assumes |
| 2174 | // the PC-relative address fits in the CALL address field (22 bits). |
| 2175 | // Use JMPL for indirect calls. |
| 2176 | // |
| 2177 | if (calledFunc) // direct function call |
| 2178 | M = BuildMI(V9::CALL, 1).addPCDisp(callee); |
| 2179 | else // indirect function call |
| 2180 | M = BuildMI(V9::JMPLCALLi, 3).addReg(callee).addSImm((int64_t)0) |
| 2181 | .addRegDef(retAddrReg); |
| 2182 | mvec.push_back(M); |
Vikram S. Adve | 5b1b47b | 2003-05-25 15:59:47 +0000 | [diff] [blame] | 2183 | |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 2184 | const FunctionType* funcType = |
| 2185 | cast<FunctionType>(cast<PointerType>(callee->getType()) |
Vikram S. Adve | 5b1b47b | 2003-05-25 15:59:47 +0000 | [diff] [blame] | 2186 | ->getElementType()); |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 2187 | bool isVarArgs = funcType->isVarArg(); |
| 2188 | bool noPrototype = isVarArgs && funcType->getNumParams() == 0; |
Vikram S. Adve | 5b1b47b | 2003-05-25 15:59:47 +0000 | [diff] [blame] | 2189 | |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 2190 | // Use a descriptor to pass information about call arguments |
| 2191 | // to the register allocator. This descriptor will be "owned" |
| 2192 | // and freed automatically when the MachineCodeForInstruction |
| 2193 | // object for the callInstr goes away. |
| 2194 | CallArgsDescriptor* argDesc = new CallArgsDescriptor(callInstr, |
Vikram S. Adve | 5b1b47b | 2003-05-25 15:59:47 +0000 | [diff] [blame] | 2195 | retAddrReg, isVarArgs,noPrototype); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 2196 | |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 2197 | assert(callInstr->getOperand(0) == callee |
| 2198 | && "This is assumed in the loop below!"); |
Vikram S. Adve | 5b1b47b | 2003-05-25 15:59:47 +0000 | [diff] [blame] | 2199 | |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 2200 | for (unsigned i=1, N=callInstr->getNumOperands(); i < N; ++i) { |
| 2201 | Value* argVal = callInstr->getOperand(i); |
| 2202 | Instruction* intArgReg = NULL; |
Vikram S. Adve | 5b1b47b | 2003-05-25 15:59:47 +0000 | [diff] [blame] | 2203 | |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 2204 | // Check for FP arguments to varargs functions. |
| 2205 | // Any such argument in the first $K$ args must be passed in an |
| 2206 | // integer register, where K = #integer argument registers. |
| 2207 | if (isVarArgs && argVal->getType()->isFloatingPoint()) { |
| 2208 | // If it is a function with no prototype, pass value |
| 2209 | // as an FP value as well as a varargs value |
| 2210 | if (noPrototype) |
| 2211 | argDesc->getArgInfo(i-1).setUseFPArgReg(); |
Vikram S. Adve | 5b1b47b | 2003-05-25 15:59:47 +0000 | [diff] [blame] | 2212 | |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 2213 | // If this arg. is in the first $K$ regs, add a copy |
| 2214 | // float-to-int instruction to pass the value as an integer. |
| 2215 | if (i <= target.getRegInfo().getNumOfIntArgRegs()) { |
| 2216 | MachineCodeForInstruction &destMCFI = |
| 2217 | MachineCodeForInstruction::get(callInstr); |
| 2218 | intArgReg = new TmpInstruction(Type::IntTy, argVal); |
| 2219 | destMCFI.addTemp(intArgReg); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 2220 | |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 2221 | std::vector<MachineInstr*> copyMvec; |
| 2222 | target.getInstrInfo().CreateCodeToCopyFloatToInt(target, |
Vikram S. Adve | 5b1b47b | 2003-05-25 15:59:47 +0000 | [diff] [blame] | 2223 | callInstr->getParent()->getParent(), |
| 2224 | argVal, (TmpInstruction*) intArgReg, |
| 2225 | copyMvec, destMCFI); |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 2226 | mvec.insert(mvec.begin(),copyMvec.begin(),copyMvec.end()); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 2227 | |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 2228 | argDesc->getArgInfo(i-1).setUseIntArgReg(); |
| 2229 | argDesc->getArgInfo(i-1).setArgCopy(intArgReg); |
| 2230 | } else |
| 2231 | // Cannot fit in first $K$ regs so pass arg on stack |
| 2232 | argDesc->getArgInfo(i-1).setUseStackSlot(); |
| 2233 | } |
Vikram S. Adve | 5b1b47b | 2003-05-25 15:59:47 +0000 | [diff] [blame] | 2234 | |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 2235 | if (intArgReg) |
| 2236 | mvec.back()->addImplicitRef(intArgReg); |
Vikram S. Adve | 5b1b47b | 2003-05-25 15:59:47 +0000 | [diff] [blame] | 2237 | |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 2238 | mvec.back()->addImplicitRef(argVal); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 2239 | } |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 2240 | |
| 2241 | // Add the return value as an implicit ref. The call operands |
| 2242 | // were added above. |
| 2243 | if (callInstr->getType() != Type::VoidTy) |
| 2244 | mvec.back()->addImplicitRef(callInstr, /*isDef*/ true); |
| 2245 | |
| 2246 | // For the CALL instruction, the ret. addr. reg. is also implicit |
| 2247 | if (isa<Function>(callee)) |
| 2248 | mvec.back()->addImplicitRef(retAddrReg, /*isDef*/ true); |
| 2249 | |
| 2250 | // delay slot |
| 2251 | mvec.push_back(BuildMI(V9::NOP, 0)); |
| 2252 | } |
Vikram S. Adve | 5b1b47b | 2003-05-25 15:59:47 +0000 | [diff] [blame] | 2253 | |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2254 | break; |
Vikram S. Adve | b7f06f4 | 2001-11-04 19:34:49 +0000 | [diff] [blame] | 2255 | } |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 2256 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 2257 | case 62: // reg: Shl(reg, reg) |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 2258 | { |
| 2259 | Value* argVal1 = subtreeRoot->leftChild()->getValue(); |
| 2260 | Value* argVal2 = subtreeRoot->rightChild()->getValue(); |
| 2261 | Instruction* shlInstr = subtreeRoot->getInstruction(); |
| 2262 | |
| 2263 | const Type* opType = argVal1->getType(); |
Chris Lattner | 0c4e886 | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 2264 | assert((opType->isInteger() || isa<PointerType>(opType)) && |
| 2265 | "Shl unsupported for other types"); |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 2266 | |
| 2267 | CreateShiftInstructions(target, shlInstr->getParent()->getParent(), |
Misha Brukman | 91aee47 | 2003-05-27 22:37:00 +0000 | [diff] [blame] | 2268 | (opType == Type::LongTy)? V9::SLLXr6:V9::SLLr6, |
Vikram S. Adve | 242a808 | 2002-05-19 15:25:51 +0000 | [diff] [blame] | 2269 | argVal1, argVal2, 0, shlInstr, mvec, |
| 2270 | MachineCodeForInstruction::get(shlInstr)); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2271 | break; |
Vikram S. Adve | 6ad7c55 | 2001-11-09 02:18:16 +0000 | [diff] [blame] | 2272 | } |
| 2273 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 2274 | case 63: // reg: Shr(reg, reg) |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 2275 | { |
| 2276 | const Type* opType = subtreeRoot->leftChild()->getValue()->getType(); |
Chris Lattner | 0c4e886 | 2002-09-03 01:08:28 +0000 | [diff] [blame] | 2277 | assert((opType->isInteger() || isa<PointerType>(opType)) && |
| 2278 | "Shr unsupported for other types"); |
Chris Lattner | 54e898e | 2003-01-15 19:23:34 +0000 | [diff] [blame] | 2279 | Add3OperandInstr(opType->isSigned() |
Misha Brukman | 91aee47 | 2003-05-27 22:37:00 +0000 | [diff] [blame] | 2280 | ? (opType == Type::LongTy ? V9::SRAXr6 : V9::SRAr6) |
| 2281 | : (opType == Type::LongTy ? V9::SRLXr6 : V9::SRLr6), |
Chris Lattner | 54e898e | 2003-01-15 19:23:34 +0000 | [diff] [blame] | 2282 | subtreeRoot, mvec); |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2283 | break; |
Vikram S. Adve | 6ad7c55 | 2001-11-09 02:18:16 +0000 | [diff] [blame] | 2284 | } |
| 2285 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 2286 | case 64: // reg: Phi(reg,reg) |
| 2287 | break; // don't forward the value |
Vikram S. Adve | 7482532 | 2002-03-18 03:15:35 +0000 | [diff] [blame] | 2288 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 2289 | case 65: // reg: VaArg(reg) |
Vikram S. Adve | 5b1b47b | 2003-05-25 15:59:47 +0000 | [diff] [blame] | 2290 | { |
| 2291 | // Use value initialized by va_start as pointer to args on the stack. |
| 2292 | // Load argument via current pointer value, then increment pointer. |
| 2293 | int argSize = target.getFrameInfo().getSizeOfEachArgOnStack(); |
| 2294 | Instruction* vaArgI = subtreeRoot->getInstruction(); |
Misha Brukman | 91aee47 | 2003-05-27 22:37:00 +0000 | [diff] [blame] | 2295 | mvec.push_back(BuildMI(V9::LDXi, 3).addReg(vaArgI->getOperand(0)). |
Vikram S. Adve | 5b1b47b | 2003-05-25 15:59:47 +0000 | [diff] [blame] | 2296 | addSImm(0).addRegDef(vaArgI)); |
Misha Brukman | 91aee47 | 2003-05-27 22:37:00 +0000 | [diff] [blame] | 2297 | mvec.push_back(BuildMI(V9::ADDi, 3).addReg(vaArgI->getOperand(0)). |
Vikram S. Adve | 5b1b47b | 2003-05-25 15:59:47 +0000 | [diff] [blame] | 2298 | addSImm(argSize).addRegDef(vaArgI->getOperand(0))); |
| 2299 | break; |
| 2300 | } |
| 2301 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 2302 | case 71: // reg: VReg |
| 2303 | case 72: // reg: Constant |
| 2304 | break; // don't forward the value |
Vikram S. Adve | 4cecdd2 | 2001-10-01 00:12:53 +0000 | [diff] [blame] | 2305 | |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 2306 | default: |
| 2307 | assert(0 && "Unrecognized BURG rule"); |
| 2308 | break; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 2309 | } |
Misha Brukman | b3fabe0 | 2003-05-31 06:22:37 +0000 | [diff] [blame^] | 2310 | } |
Vikram S. Adve | 65a2dee | 2002-08-13 17:40:54 +0000 | [diff] [blame] | 2311 | |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 2312 | if (forwardOperandNum >= 0) { |
| 2313 | // We did not generate a machine instruction but need to use operand. |
| 2314 | // If user is in the same tree, replace Value in its machine operand. |
| 2315 | // If not, insert a copy instruction which should get coalesced away |
| 2316 | // by register allocation. |
| 2317 | if (subtreeRoot->parent() != NULL) |
| 2318 | ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum); |
| 2319 | else { |
| 2320 | std::vector<MachineInstr*> minstrVec; |
| 2321 | Instruction* instr = subtreeRoot->getInstruction(); |
| 2322 | target.getInstrInfo(). |
| 2323 | CreateCopyInstructionsByType(target, |
| 2324 | instr->getParent()->getParent(), |
| 2325 | instr->getOperand(forwardOperandNum), |
| 2326 | instr, minstrVec, |
| 2327 | MachineCodeForInstruction::get(instr)); |
| 2328 | assert(minstrVec.size() > 0); |
| 2329 | mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end()); |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 2330 | } |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 2331 | } |
Vikram S. Adve | 65a2dee | 2002-08-13 17:40:54 +0000 | [diff] [blame] | 2332 | |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 2333 | if (maskUnsignedResult) { |
| 2334 | // If result is unsigned and smaller than int reg size, |
| 2335 | // we need to clear high bits of result value. |
| 2336 | assert(forwardOperandNum < 0 && "Need mask but no instruction generated"); |
| 2337 | Instruction* dest = subtreeRoot->getInstruction(); |
| 2338 | if (dest->getType()->isUnsigned()) { |
| 2339 | unsigned destSize=target.getTargetData().getTypeSize(dest->getType()); |
| 2340 | if (destSize <= 4) { |
| 2341 | // Mask high bits. Use a TmpInstruction to represent the |
| 2342 | // intermediate result before masking. Since those instructions |
| 2343 | // have already been generated, go back and substitute tmpI |
| 2344 | // for dest in the result position of each one of them. |
| 2345 | TmpInstruction *tmpI = new TmpInstruction(dest->getType(), dest, |
| 2346 | NULL, "maskHi"); |
| 2347 | MachineCodeForInstruction::get(dest).addTemp(tmpI); |
Vikram S. Adve | 65a2dee | 2002-08-13 17:40:54 +0000 | [diff] [blame] | 2348 | |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 2349 | for (unsigned i=0, N=mvec.size(); i < N; ++i) |
| 2350 | mvec[i]->substituteValue(dest, tmpI); |
Vikram S. Adve | 65a2dee | 2002-08-13 17:40:54 +0000 | [diff] [blame] | 2351 | |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 2352 | M = BuildMI(V9::SRLi6, 3).addReg(tmpI).addZImm(8*(4-destSize)) |
| 2353 | .addReg(dest, MOTy::Def); |
| 2354 | mvec.push_back(M); |
| 2355 | } else if (destSize < 8) { |
| 2356 | assert(0 && "Unsupported type size: 32 < size < 64 bits"); |
| 2357 | } |
Vikram S. Adve | 65a2dee | 2002-08-13 17:40:54 +0000 | [diff] [blame] | 2358 | } |
Misha Brukman | 7b64794 | 2003-05-30 20:11:56 +0000 | [diff] [blame] | 2359 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 2360 | } |