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