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