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