Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 1 | //===-- MachineCodeForFunction.cpp ------------------------------------------=// |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 2 | // |
| 3 | // Purpose: |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 4 | // Collect native machine code information for a function. |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 5 | // This allows target-specific information about the generated code |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 6 | // to be stored with each function. |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/CodeGen/MachineCodeForMethod.h" |
| 10 | #include "llvm/CodeGen/MachineInstr.h" // For debug output |
| 11 | #include "llvm/Target/TargetMachine.h" |
| 12 | #include "llvm/Target/MachineFrameInfo.h" |
| 13 | #include "llvm/Target/MachineCacheInfo.h" |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 14 | #include "llvm/Function.h" |
Chris Lattner | 221d688 | 2002-02-12 21:07:25 +0000 | [diff] [blame] | 15 | #include "llvm/BasicBlock.h" |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 16 | #include "llvm/iOther.h" |
| 17 | #include <limits.h> |
Chris Lattner | b91ca1f | 2002-02-24 23:01:56 +0000 | [diff] [blame] | 18 | #include <iostream> |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 19 | |
| 20 | const int INVALID_FRAME_OFFSET = INT_MAX; // std::numeric_limits<int>::max(); |
| 21 | |
| 22 | static AnnotationID MCFM_AID( |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 23 | AnnotationManager::getID("CodeGen::MachineCodeForFunction")); |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 24 | |
| 25 | // The next two methods are used to construct and to retrieve |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 26 | // the MachineCodeForFunction object for the given function. |
| 27 | // construct() -- Allocates and initializes for a given function and target |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 28 | // get() -- Returns a handle to the object. |
| 29 | // This should not be called before "construct()" |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 30 | // for a given Function. |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 31 | // |
Vikram S. Adve | 89e2da0 | 2002-03-18 03:36:30 +0000 | [diff] [blame] | 32 | MachineCodeForMethod& |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 33 | MachineCodeForMethod::construct(const Function *M, const TargetMachine &Tar) |
Vikram S. Adve | 89e2da0 | 2002-03-18 03:36:30 +0000 | [diff] [blame] | 34 | { |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 35 | assert(M->getAnnotation(MCFM_AID) == 0 && |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 36 | "Object already exists for this function!"); |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 37 | MachineCodeForMethod* mcInfo = new MachineCodeForMethod(M, Tar); |
| 38 | M->addAnnotation(mcInfo); |
| 39 | return *mcInfo; |
| 40 | } |
| 41 | |
Vikram S. Adve | 89e2da0 | 2002-03-18 03:36:30 +0000 | [diff] [blame] | 42 | void |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 43 | MachineCodeForMethod::destruct(const Function *M) |
Vikram S. Adve | 89e2da0 | 2002-03-18 03:36:30 +0000 | [diff] [blame] | 44 | { |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 45 | bool Deleted = M->deleteAnnotation(MCFM_AID); |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 46 | assert(Deleted && "Machine code did not exist for function!"); |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Vikram S. Adve | 89e2da0 | 2002-03-18 03:36:30 +0000 | [diff] [blame] | 49 | MachineCodeForMethod& |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 50 | MachineCodeForMethod::get(const Function *F) |
Vikram S. Adve | 89e2da0 | 2002-03-18 03:36:30 +0000 | [diff] [blame] | 51 | { |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 52 | MachineCodeForMethod *mc = (MachineCodeForMethod*)F->getAnnotation(MCFM_AID); |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 53 | assert(mc && "Call construct() method first to allocate the object"); |
| 54 | return *mc; |
| 55 | } |
| 56 | |
| 57 | static unsigned |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 58 | ComputeMaxOptionalArgsSize(const TargetMachine& target, const Function *F) |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 59 | { |
| 60 | const MachineFrameInfo& frameInfo = target.getFrameInfo(); |
| 61 | |
| 62 | unsigned int maxSize = 0; |
| 63 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 64 | for (Function::const_iterator MI = F->begin(), ME = F->end(); MI != ME; ++MI) |
Vikram S. Adve | 89e2da0 | 2002-03-18 03:36:30 +0000 | [diff] [blame] | 65 | { |
| 66 | const BasicBlock *BB = *MI; |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 67 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) |
Vikram S. Adve | 89e2da0 | 2002-03-18 03:36:30 +0000 | [diff] [blame] | 68 | if (CallInst *callInst = dyn_cast<CallInst>(*I)) |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 69 | { |
Vikram S. Adve | 89e2da0 | 2002-03-18 03:36:30 +0000 | [diff] [blame] | 70 | unsigned int numOperands = callInst->getNumOperands() - 1; |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 71 | int numExtra = (int)numOperands-frameInfo.getNumFixedOutgoingArgs(); |
Vikram S. Adve | 89e2da0 | 2002-03-18 03:36:30 +0000 | [diff] [blame] | 72 | if (numExtra <= 0) |
| 73 | continue; |
| 74 | |
| 75 | unsigned int sizeForThisCall; |
| 76 | if (frameInfo.argsOnStackHaveFixedSize()) |
| 77 | { |
| 78 | int argSize = frameInfo.getSizeOfEachArgOnStack(); |
| 79 | sizeForThisCall = numExtra * (unsigned) argSize; |
| 80 | } |
| 81 | else |
| 82 | { |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 83 | assert(0 && "UNTESTED CODE: Size per stack argument is not " |
| 84 | "fixed on this architecture: use actual arg sizes to " |
| 85 | "compute MaxOptionalArgsSize"); |
Vikram S. Adve | 89e2da0 | 2002-03-18 03:36:30 +0000 | [diff] [blame] | 86 | sizeForThisCall = 0; |
| 87 | for (unsigned i=0; i < numOperands; ++i) |
| 88 | sizeForThisCall += target.findOptimalStorageSize(callInst-> |
| 89 | getOperand(i)->getType()); |
| 90 | } |
| 91 | |
| 92 | if (maxSize < sizeForThisCall) |
| 93 | maxSize = sizeForThisCall; |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 94 | } |
Vikram S. Adve | 89e2da0 | 2002-03-18 03:36:30 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 97 | return maxSize; |
| 98 | } |
| 99 | |
| 100 | // Align data larger than one L1 cache line on L1 cache line boundaries. |
| 101 | // Align all smaller data on the next higher 2^x boundary (4, 8, ...). |
| 102 | // |
| 103 | // THIS FUNCTION HAS BEEN COPIED FROM EMITASSEMBLY.CPP AND |
| 104 | // SHOULD BE USED DIRECTLY THERE |
| 105 | // |
| 106 | inline unsigned int |
| 107 | SizeToAlignment(unsigned int size, const TargetMachine& target) |
| 108 | { |
| 109 | unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1); |
| 110 | if (size > (unsigned) cacheLineSize / 2) |
| 111 | return cacheLineSize; |
| 112 | else |
| 113 | for (unsigned sz=1; /*no condition*/; sz *= 2) |
| 114 | if (sz >= size) |
| 115 | return sz; |
| 116 | } |
| 117 | |
| 118 | |
| 119 | |
| 120 | /*ctor*/ |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 121 | MachineCodeForMethod::MachineCodeForMethod(const Function *F, |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 122 | const TargetMachine& target) |
| 123 | : Annotation(MCFM_AID), |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 124 | method(F), compiledAsLeaf(false), staticStackSize(0), |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 125 | automaticVarsSize(0), regSpillsSize(0), |
| 126 | currentOptionalArgsSize(0), maxOptionalArgsSize(0), |
Vikram S. Adve | fa79e6e | 2002-03-31 18:57:49 +0000 | [diff] [blame] | 127 | currentTmpValuesSize(0), maxTmpValuesSize(0) |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 128 | { |
| 129 | maxOptionalArgsSize = ComputeMaxOptionalArgsSize(target, method); |
Vikram S. Adve | 89e2da0 | 2002-03-18 03:36:30 +0000 | [diff] [blame] | 130 | staticStackSize = maxOptionalArgsSize |
| 131 | + target.getFrameInfo().getMinStackFrameSize(); |
| 132 | } |
| 133 | |
| 134 | int |
| 135 | MachineCodeForMethod::computeOffsetforLocalVar(const TargetMachine& target, |
| 136 | const Value* val, |
Vikram S. Adve | e4e4d4e | 2002-03-24 03:39:26 +0000 | [diff] [blame] | 137 | unsigned int& getPaddedSize, |
| 138 | unsigned int sizeToUse = 0) |
Vikram S. Adve | 89e2da0 | 2002-03-18 03:36:30 +0000 | [diff] [blame] | 139 | { |
| 140 | bool growUp; |
| 141 | int firstOffset =target.getFrameInfo().getFirstAutomaticVarOffset(*this, |
| 142 | growUp); |
| 143 | unsigned char align; |
Vikram S. Adve | e4e4d4e | 2002-03-24 03:39:26 +0000 | [diff] [blame] | 144 | if (sizeToUse == 0) |
Vikram S. Adve | 89e2da0 | 2002-03-18 03:36:30 +0000 | [diff] [blame] | 145 | { |
Vikram S. Adve | e4e4d4e | 2002-03-24 03:39:26 +0000 | [diff] [blame] | 146 | sizeToUse = target.findOptimalStorageSize(val->getType()); |
Vikram S. Adve | 89e2da0 | 2002-03-18 03:36:30 +0000 | [diff] [blame] | 147 | // align = target.DataLayout.getTypeAlignment(val->getType()); |
| 148 | } |
Vikram S. Adve | e4e4d4e | 2002-03-24 03:39:26 +0000 | [diff] [blame] | 149 | |
| 150 | align = SizeToAlignment(sizeToUse, target); |
Vikram S. Adve | 89e2da0 | 2002-03-18 03:36:30 +0000 | [diff] [blame] | 151 | |
| 152 | int offset = getAutomaticVarsSize(); |
| 153 | if (! growUp) |
Vikram S. Adve | e4e4d4e | 2002-03-24 03:39:26 +0000 | [diff] [blame] | 154 | offset += sizeToUse; |
Vikram S. Adve | 89e2da0 | 2002-03-18 03:36:30 +0000 | [diff] [blame] | 155 | |
| 156 | if (unsigned int mod = offset % align) |
| 157 | { |
Vikram S. Adve | e4e4d4e | 2002-03-24 03:39:26 +0000 | [diff] [blame] | 158 | offset += align - mod; |
| 159 | getPaddedSize = sizeToUse + align - mod; |
Vikram S. Adve | 89e2da0 | 2002-03-18 03:36:30 +0000 | [diff] [blame] | 160 | } |
Vikram S. Adve | e4e4d4e | 2002-03-24 03:39:26 +0000 | [diff] [blame] | 161 | else |
| 162 | getPaddedSize = sizeToUse; |
| 163 | |
Vikram S. Adve | 89e2da0 | 2002-03-18 03:36:30 +0000 | [diff] [blame] | 164 | offset = growUp? firstOffset + offset |
| 165 | : firstOffset - offset; |
Vikram S. Adve | e4e4d4e | 2002-03-24 03:39:26 +0000 | [diff] [blame] | 166 | |
Vikram S. Adve | 89e2da0 | 2002-03-18 03:36:30 +0000 | [diff] [blame] | 167 | return offset; |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | int |
| 171 | MachineCodeForMethod::allocateLocalVar(const TargetMachine& target, |
| 172 | const Value* val, |
Vikram S. Adve | e4e4d4e | 2002-03-24 03:39:26 +0000 | [diff] [blame] | 173 | unsigned int sizeToUse = 0) |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 174 | { |
| 175 | // Check if we've allocated a stack slot for this value already |
| 176 | // |
| 177 | int offset = getOffset(val); |
| 178 | if (offset == INVALID_FRAME_OFFSET) |
| 179 | { |
Vikram S. Adve | e4e4d4e | 2002-03-24 03:39:26 +0000 | [diff] [blame] | 180 | unsigned int getPaddedSize; |
| 181 | offset = this->computeOffsetforLocalVar(target, val, getPaddedSize, |
| 182 | sizeToUse); |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 183 | offsets[val] = offset; |
Vikram S. Adve | e4e4d4e | 2002-03-24 03:39:26 +0000 | [diff] [blame] | 184 | incrementAutomaticVarsSize(getPaddedSize); |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 185 | } |
| 186 | return offset; |
| 187 | } |
Vikram S. Adve | 89e2da0 | 2002-03-18 03:36:30 +0000 | [diff] [blame] | 188 | |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 189 | int |
| 190 | MachineCodeForMethod::allocateSpilledValue(const TargetMachine& target, |
| 191 | const Type* type) |
| 192 | { |
| 193 | unsigned int size = target.findOptimalStorageSize(type); |
| 194 | unsigned char align = target.DataLayout.getTypeAlignment(type); |
| 195 | |
| 196 | bool growUp; |
| 197 | int firstOffset = target.getFrameInfo().getRegSpillAreaOffset(*this, growUp); |
| 198 | |
| 199 | int offset = getRegSpillsSize(); |
| 200 | if (! growUp) |
| 201 | offset += size; |
| 202 | |
| 203 | if (unsigned int mod = offset % align) |
| 204 | { |
| 205 | offset += align - mod; |
| 206 | size += align - mod; |
| 207 | } |
| 208 | |
| 209 | offset = growUp? firstOffset + offset |
| 210 | : firstOffset - offset; |
| 211 | |
| 212 | incrementRegSpillsSize(size); |
| 213 | |
| 214 | return offset; |
| 215 | } |
| 216 | |
| 217 | int |
| 218 | MachineCodeForMethod::allocateOptionalArg(const TargetMachine& target, |
| 219 | const Type* type) |
| 220 | { |
| 221 | const MachineFrameInfo& frameInfo = target.getFrameInfo(); |
| 222 | |
| 223 | int size = INT_MAX; |
| 224 | if (frameInfo.argsOnStackHaveFixedSize()) |
| 225 | size = frameInfo.getSizeOfEachArgOnStack(); |
| 226 | else |
| 227 | { |
| 228 | size = target.findOptimalStorageSize(type); |
| 229 | assert(0 && "UNTESTED CODE: Size per stack argument is not fixed on this architecture: use actual argument sizes for computing optional arg offsets"); |
| 230 | } |
| 231 | unsigned char align = target.DataLayout.getTypeAlignment(type); |
| 232 | |
| 233 | bool growUp; |
| 234 | int firstOffset = frameInfo.getFirstOptionalOutgoingArgOffset(*this, growUp); |
| 235 | |
| 236 | int offset = getCurrentOptionalArgsSize(); |
| 237 | if (! growUp) |
| 238 | offset += size; |
| 239 | |
| 240 | if (unsigned int mod = offset % align) |
| 241 | { |
| 242 | offset += align - mod; |
| 243 | size += align - mod; |
| 244 | } |
| 245 | |
| 246 | offset = growUp? firstOffset + offset |
| 247 | : firstOffset - offset; |
| 248 | |
| 249 | incrementCurrentOptionalArgsSize(size); |
| 250 | |
| 251 | return offset; |
| 252 | } |
| 253 | |
| 254 | void |
| 255 | MachineCodeForMethod::resetOptionalArgs(const TargetMachine& target) |
| 256 | { |
| 257 | currentOptionalArgsSize = 0; |
| 258 | } |
| 259 | |
| 260 | int |
| 261 | MachineCodeForMethod::pushTempValue(const TargetMachine& target, |
| 262 | unsigned int size) |
| 263 | { |
| 264 | // Compute a power-of-2 alignment according to the possible sizes, |
| 265 | // but not greater than the alignment of the largest type we support |
| 266 | // (currently a double word -- see class TargetData). |
| 267 | unsigned char align = 1; |
| 268 | for (; align < size && align < target.DataLayout.getDoubleAlignment(); |
| 269 | align = 2*align) |
| 270 | ; |
| 271 | |
| 272 | bool growUp; |
| 273 | int firstTmpOffset = target.getFrameInfo().getTmpAreaOffset(*this, growUp); |
| 274 | |
| 275 | int offset = currentTmpValuesSize; |
| 276 | if (! growUp) |
| 277 | offset += size; |
| 278 | |
| 279 | if (unsigned int mod = offset % align) |
| 280 | { |
| 281 | offset += align - mod; |
| 282 | size += align - mod; |
| 283 | } |
| 284 | |
| 285 | offset = growUp ? firstTmpOffset + offset : firstTmpOffset - offset; |
| 286 | |
Vikram S. Adve | fa79e6e | 2002-03-31 18:57:49 +0000 | [diff] [blame] | 287 | incrementTmpAreaSize(size); |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 288 | return offset; |
| 289 | } |
| 290 | |
| 291 | void |
| 292 | MachineCodeForMethod::popAllTempValues(const TargetMachine& target) |
| 293 | { |
Vikram S. Adve | fa79e6e | 2002-03-31 18:57:49 +0000 | [diff] [blame] | 294 | resetTmpAreaSize(); |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | int |
| 298 | MachineCodeForMethod::getOffset(const Value* val) const |
| 299 | { |
| 300 | std::hash_map<const Value*, int>::const_iterator pair = offsets.find(val); |
| 301 | return (pair == offsets.end())? INVALID_FRAME_OFFSET : pair->second; |
| 302 | } |
| 303 | |
| 304 | void |
| 305 | MachineCodeForMethod::dump() const |
| 306 | { |
Chris Lattner | b91ca1f | 2002-02-24 23:01:56 +0000 | [diff] [blame] | 307 | std::cerr << "\n" << method->getReturnType() |
| 308 | << " \"" << method->getName() << "\"\n"; |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 309 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 310 | for (Function::const_iterator BI = method->begin(); BI != method->end(); ++BI) |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 311 | { |
| 312 | BasicBlock* bb = *BI; |
Chris Lattner | b91ca1f | 2002-02-24 23:01:56 +0000 | [diff] [blame] | 313 | std::cerr << "\n" << bb->getName() << " (" << bb << ")" << ":\n"; |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 314 | |
| 315 | MachineCodeForBasicBlock& mvec = bb->getMachineInstrVec(); |
| 316 | for (unsigned i=0; i < mvec.size(); i++) |
Chris Lattner | b91ca1f | 2002-02-24 23:01:56 +0000 | [diff] [blame] | 317 | std::cerr << "\t" << *mvec[i]; |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 318 | } |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 319 | std::cerr << "\nEnd function \"" << method->getName() << "\"\n\n"; |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 320 | } |