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