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