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