Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 1 | //===- PreSelection.cpp - Specialize LLVM code for target machine ---------===// |
| 2 | // |
| 3 | // This file defines the PreSelection pass which specializes LLVM code for a |
| 4 | // target machine, while remaining in legal portable LLVM form and |
| 5 | // preserving type information and type safety. This is meant to enable |
| 6 | // dataflow optimizations on target-specific operations such as accesses to |
| 7 | // constants, globals, and array indexing. |
| 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #include "llvm/CodeGen/PreSelection.h" |
| 12 | #include "llvm/Target/TargetMachine.h" |
| 13 | #include "llvm/Target/MachineInstrInfo.h" |
| 14 | #include "llvm/Transforms/Scalar.h" |
| 15 | #include "llvm/Support/InstVisitor.h" |
| 16 | #include "llvm/Module.h" |
| 17 | #include "llvm/Constants.h" |
| 18 | #include "llvm/iMemory.h" |
| 19 | #include "llvm/iPHINode.h" |
| 20 | #include "llvm/iOther.h" |
| 21 | #include "llvm/DerivedTypes.h" |
| 22 | #include "llvm/Pass.h" |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 23 | #include "Support/CommandLine.h" |
Vikram S. Adve | d0451a9 | 2002-10-13 00:01:57 +0000 | [diff] [blame] | 24 | #include <algorithm> |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 25 | |
| 26 | namespace { |
| 27 | //===--------------------------------------------------------------------===// |
| 28 | // SelectDebugLevel - Allow command line control over debugging. |
| 29 | // |
| 30 | enum PreSelectDebugLevel_t { |
| 31 | PreSelect_NoDebugInfo, |
| 32 | PreSelect_PrintOutput, |
| 33 | }; |
| 34 | |
| 35 | // Enable Debug Options to be specified on the command line |
| 36 | cl::opt<PreSelectDebugLevel_t> |
| 37 | PreSelectDebugLevel("dpreselect", cl::Hidden, |
| 38 | cl::desc("debug information for target-dependent pre-selection"), |
| 39 | cl::values( |
| 40 | clEnumValN(PreSelect_NoDebugInfo, "n", "disable debug output (default)"), |
| 41 | clEnumValN(PreSelect_PrintOutput, "y", "print generated machine code"), |
| 42 | /* default level = */ PreSelect_NoDebugInfo)); |
| 43 | |
| 44 | |
| 45 | //===--------------------------------------------------------------------===// |
| 46 | // class ConstantPoolForModule: |
| 47 | // |
| 48 | // The pool of constants that must be emitted for a module. |
| 49 | // This is a single pool for the entire module and is shared by |
| 50 | // all invocations of the PreSelection pass for this module by putting |
Vikram S. Adve | d0451a9 | 2002-10-13 00:01:57 +0000 | [diff] [blame] | 51 | // this as an annotation on the Module object. |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 52 | // A single GlobalVariable is created for each constant in the pool |
| 53 | // representing the memory for that constant. |
| 54 | // |
| 55 | static AnnotationID CPFM_AID( |
| 56 | AnnotationManager::getID("CodeGen::ConstantPoolForModule")); |
| 57 | |
| 58 | class ConstantPoolForModule: private Annotation, public NonCopyable { |
| 59 | Module* myModule; |
| 60 | std::map<const Constant*, GlobalVariable*> gvars; |
| 61 | std::map<const Constant*, GlobalVariable*> origGVars; |
| 62 | ConstantPoolForModule(Module* M); // called only by annotation builder |
| 63 | ConstantPoolForModule(); // do not implement |
| 64 | public: |
| 65 | static ConstantPoolForModule& get(Module* M) { |
| 66 | ConstantPoolForModule* cpool = |
| 67 | (ConstantPoolForModule*) M->getAnnotation(CPFM_AID); |
| 68 | if (cpool == NULL) // create a new annotation and add it to the Module |
| 69 | M->addAnnotation(cpool = new ConstantPoolForModule(M)); |
| 70 | return *cpool; |
| 71 | } |
| 72 | |
| 73 | GlobalVariable* getGlobalForConstant(Constant* CV) { |
| 74 | std::map<const Constant*, GlobalVariable*>::iterator I = gvars.find(CV); |
| 75 | if (I != gvars.end()) |
| 76 | return I->second; // global exists so return it |
| 77 | return addToConstantPool(CV); // create a new global and return it |
| 78 | } |
| 79 | |
| 80 | GlobalVariable* addToConstantPool(Constant* CV) { |
| 81 | GlobalVariable*& GV = gvars[CV]; // handle to global var entry in map |
| 82 | if (GV == NULL) |
| 83 | { // check if a global constant already existed; otherwise create one |
| 84 | std::map<const Constant*, GlobalVariable*>::iterator PI = |
| 85 | origGVars.find(CV); |
| 86 | if (PI != origGVars.end()) |
| 87 | GV = PI->second; // put in map |
| 88 | else |
| 89 | { |
| 90 | GV = new GlobalVariable(CV->getType(), true,true,CV); //put in map |
| 91 | myModule->getGlobalList().push_back(GV); // GV owned by module now |
| 92 | } |
| 93 | } |
| 94 | return GV; |
| 95 | } |
| 96 | }; |
| 97 | |
| 98 | /* ctor */ |
| 99 | ConstantPoolForModule::ConstantPoolForModule(Module* M) |
| 100 | : Annotation(CPFM_AID), myModule(M) |
| 101 | { |
| 102 | // Build reverse map for pre-existing global constants so we can find them |
| 103 | for (Module::giterator GI = M->gbegin(), GE = M->gend(); GI != GE; ++GI) |
| 104 | if (GI->hasInitializer() && GI->isConstant()) |
| 105 | origGVars[GI->getInitializer()] = GI; |
| 106 | } |
| 107 | |
| 108 | //===--------------------------------------------------------------------===// |
| 109 | // PreSelection Pass - Specialize LLVM code for the current target machine. |
| 110 | // This was and will be a basicblock pass, but make it a FunctionPass until |
| 111 | // BasicBlockPass ::doFinalization(Function&) is available. |
| 112 | // |
| 113 | class PreSelection : public BasicBlockPass, public InstVisitor<PreSelection> |
| 114 | { |
| 115 | const TargetMachine ⌖ |
| 116 | Function* function; |
| 117 | |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 118 | GlobalVariable* getGlobalForConstant(Constant* CV) { |
| 119 | Module* M = function->getParent(); |
| 120 | return ConstantPoolForModule::get(M).getGlobalForConstant(CV); |
| 121 | } |
| 122 | |
| 123 | public: |
| 124 | PreSelection (const TargetMachine &T): target(T), function(NULL) {} |
| 125 | |
| 126 | // runOnBasicBlock - apply this pass to each BB |
| 127 | bool runOnBasicBlock(BasicBlock &BB) { |
| 128 | function = BB.getParent(); |
| 129 | this->visit(BB); |
| 130 | return true; |
| 131 | } |
| 132 | |
| 133 | bool doFinalization(Function &F) { |
| 134 | if (PreSelectDebugLevel >= PreSelect_PrintOutput) |
Chris Lattner | 64317fc | 2003-01-14 20:32:10 +0000 | [diff] [blame^] | 135 | std::cerr << "\n\n*** LLVM code after pre-selection for function " |
| 136 | << F.getName() << ":\n\n" << F; |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 137 | return false; |
| 138 | } |
| 139 | |
| 140 | // These methods do the actual work of specializing code |
| 141 | void visitInstruction(Instruction &I); // common work for every instr. |
| 142 | void visitGetElementPtrInst(GetElementPtrInst &I); |
| 143 | void visitLoadInst(LoadInst &I); |
Vikram S. Adve | e9cb735 | 2002-09-27 14:24:45 +0000 | [diff] [blame] | 144 | void visitCastInst(CastInst &I); |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 145 | void visitStoreInst(StoreInst &I); |
| 146 | |
| 147 | // Helper functions for visiting operands of every instruction |
| 148 | void visitOperands(Instruction &I); // work on all operands of instr. |
| 149 | void visitOneOperand(Instruction &I, Constant* CV, unsigned opNum, |
| 150 | Instruction& insertBefore); // iworks on one operand |
| 151 | }; |
| 152 | } // end anonymous namespace |
| 153 | |
| 154 | |
| 155 | // Register the pass... |
| 156 | static RegisterOpt<PreSelection> X("preselect", |
| 157 | "Specialize LLVM code for a target machine", |
| 158 | createPreSelectionPass); |
| 159 | |
Vikram S. Adve | d0451a9 | 2002-10-13 00:01:57 +0000 | [diff] [blame] | 160 | //------------------------------------------------------------------------------ |
| 161 | // Helper functions used by methods of class PreSelection |
| 162 | //------------------------------------------------------------------------------ |
| 163 | |
| 164 | |
| 165 | // getGlobalAddr(): Put address of a global into a v. register. |
| 166 | static GetElementPtrInst* getGlobalAddr(Value* ptr, Instruction& insertBefore) |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 167 | { |
Vikram S. Adve | d0451a9 | 2002-10-13 00:01:57 +0000 | [diff] [blame] | 168 | if (isa<ConstantPointerRef>(ptr)) |
| 169 | ptr = cast<ConstantPointerRef>(ptr)->getValue(); |
| 170 | |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 171 | return (isa<GlobalValue>(ptr)) |
| 172 | ? new GetElementPtrInst(ptr, |
| 173 | std::vector<Value*>(1, ConstantSInt::get(Type::LongTy, 0U)), |
Vikram S. Adve | d0451a9 | 2002-10-13 00:01:57 +0000 | [diff] [blame] | 174 | "addrOfGlobal", &insertBefore) |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 175 | : NULL; |
| 176 | } |
| 177 | |
| 178 | |
Vikram S. Adve | d0451a9 | 2002-10-13 00:01:57 +0000 | [diff] [blame] | 179 | // Wrapper on Constant::classof to use in find_if :-( |
| 180 | inline static bool nonConstant(const Use& U) |
| 181 | { |
| 182 | return ! isa<Constant>(U); |
| 183 | } |
| 184 | |
| 185 | |
| 186 | static Instruction* DecomposeConstantExpr(ConstantExpr* CE, |
| 187 | Instruction& insertBefore) |
| 188 | { |
| 189 | Value *getArg1, *getArg2; |
| 190 | |
| 191 | switch(CE->getOpcode()) |
| 192 | { |
| 193 | case Instruction::Cast: |
| 194 | getArg1 = CE->getOperand(0); |
| 195 | if (ConstantExpr* CEarg = dyn_cast<ConstantExpr>(getArg1)) |
| 196 | getArg1 = DecomposeConstantExpr(CEarg, insertBefore); |
| 197 | return new CastInst(getArg1, CE->getType(), "constantCast",&insertBefore); |
| 198 | |
| 199 | case Instruction::GetElementPtr: |
Chris Lattner | dc476b8 | 2002-10-27 19:09:51 +0000 | [diff] [blame] | 200 | assert(find_if(CE->op_begin()+1, CE->op_end(),nonConstant) == CE->op_end() |
Vikram S. Adve | d0451a9 | 2002-10-13 00:01:57 +0000 | [diff] [blame] | 201 | && "All indices in ConstantExpr getelementptr must be constant!"); |
Vikram S. Adve | d0451a9 | 2002-10-13 00:01:57 +0000 | [diff] [blame] | 202 | getArg1 = CE->getOperand(0); |
| 203 | if (ConstantExpr* CEarg = dyn_cast<ConstantExpr>(getArg1)) |
| 204 | getArg1 = DecomposeConstantExpr(CEarg, insertBefore); |
| 205 | else if (GetElementPtrInst* gep = getGlobalAddr(getArg1, insertBefore)) |
| 206 | getArg1 = gep; |
| 207 | return new GetElementPtrInst(getArg1, |
Chris Lattner | dc476b8 | 2002-10-27 19:09:51 +0000 | [diff] [blame] | 208 | std::vector<Value*>(CE->op_begin()+1, CE->op_end()), |
Vikram S. Adve | d0451a9 | 2002-10-13 00:01:57 +0000 | [diff] [blame] | 209 | "constantGEP", &insertBefore); |
| 210 | |
| 211 | default: // must be a binary operator |
Chris Lattner | 0b16ae2 | 2002-10-13 19:39:16 +0000 | [diff] [blame] | 212 | assert(CE->getOpcode() >= Instruction::BinaryOpsBegin && |
| 213 | CE->getOpcode() < Instruction::BinaryOpsEnd && |
Vikram S. Adve | d0451a9 | 2002-10-13 00:01:57 +0000 | [diff] [blame] | 214 | "Unrecognized opcode in ConstantExpr"); |
| 215 | getArg1 = CE->getOperand(0); |
| 216 | if (ConstantExpr* CEarg = dyn_cast<ConstantExpr>(getArg1)) |
| 217 | getArg1 = DecomposeConstantExpr(CEarg, insertBefore); |
| 218 | getArg2 = CE->getOperand(1); |
| 219 | if (ConstantExpr* CEarg = dyn_cast<ConstantExpr>(getArg2)) |
| 220 | getArg2 = DecomposeConstantExpr(CEarg, insertBefore); |
| 221 | return BinaryOperator::create((Instruction::BinaryOps) CE->getOpcode(), |
| 222 | getArg1, getArg2, |
| 223 | "constantBinaryOp", &insertBefore); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 228 | //------------------------------------------------------------------------------ |
| 229 | // Instruction visitor methods to perform instruction-specific operations |
| 230 | //------------------------------------------------------------------------------ |
| 231 | |
| 232 | // Common work for *all* instructions. This needs to be called explicitly |
| 233 | // by other visit<InstructionType> functions. |
| 234 | inline void |
| 235 | PreSelection::visitInstruction(Instruction &I) |
| 236 | { |
| 237 | visitOperands(I); // Perform operand transformations |
| 238 | } |
| 239 | |
| 240 | |
| 241 | // GetElementPtr instructions: check if pointer is a global |
| 242 | void |
| 243 | PreSelection::visitGetElementPtrInst(GetElementPtrInst &I) |
| 244 | { |
| 245 | // Check for a global and put its address into a register before this instr |
Vikram S. Adve | d0451a9 | 2002-10-13 00:01:57 +0000 | [diff] [blame] | 246 | if (GetElementPtrInst* gep = getGlobalAddr(I.getPointerOperand(), I)) |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 247 | I.setOperand(I.getPointerOperandIndex(), gep); // replace pointer operand |
| 248 | |
| 249 | // Decompose multidimensional array references |
| 250 | DecomposeArrayRef(&I); |
| 251 | |
| 252 | // Perform other transformations common to all instructions |
| 253 | visitInstruction(I); |
| 254 | } |
| 255 | |
| 256 | |
| 257 | // Load instructions: check if pointer is a global |
| 258 | void |
| 259 | PreSelection::visitLoadInst(LoadInst &I) |
| 260 | { |
| 261 | // Check for a global and put its address into a register before this instr |
Vikram S. Adve | d0451a9 | 2002-10-13 00:01:57 +0000 | [diff] [blame] | 262 | if (GetElementPtrInst* gep = getGlobalAddr(I.getPointerOperand(), I)) |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 263 | I.setOperand(I.getPointerOperandIndex(), gep); // replace pointer operand |
| 264 | |
| 265 | // Perform other transformations common to all instructions |
| 266 | visitInstruction(I); |
| 267 | } |
| 268 | |
| 269 | |
| 270 | // Store instructions: check if pointer is a global |
| 271 | void |
| 272 | PreSelection::visitStoreInst(StoreInst &I) |
| 273 | { |
| 274 | // Check for a global and put its address into a register before this instr |
Vikram S. Adve | d0451a9 | 2002-10-13 00:01:57 +0000 | [diff] [blame] | 275 | if (GetElementPtrInst* gep = getGlobalAddr(I.getPointerOperand(), I)) |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 276 | I.setOperand(I.getPointerOperandIndex(), gep); // replace pointer operand |
| 277 | |
| 278 | // Perform other transformations common to all instructions |
| 279 | visitInstruction(I); |
| 280 | } |
| 281 | |
| 282 | |
Vikram S. Adve | d0451a9 | 2002-10-13 00:01:57 +0000 | [diff] [blame] | 283 | // Cast instructions: |
| 284 | // -- check if argument is a global |
| 285 | // -- make multi-step casts explicit: |
| 286 | // -- float/double to uint32_t: |
| 287 | // If target does not have a float-to-unsigned instruction, we |
| 288 | // need to convert to uint64_t and then to uint32_t, or we may |
| 289 | // overflow the signed int representation for legal uint32_t |
| 290 | // values. Expand this without checking target. |
Vikram S. Adve | e9cb735 | 2002-09-27 14:24:45 +0000 | [diff] [blame] | 291 | // |
| 292 | void |
| 293 | PreSelection::visitCastInst(CastInst &I) |
| 294 | { |
| 295 | CastInst* castI = NULL; |
| 296 | |
| 297 | // Check for a global and put its address into a register before this instr |
Vikram S. Adve | d0451a9 | 2002-10-13 00:01:57 +0000 | [diff] [blame] | 298 | if (GetElementPtrInst* gep = getGlobalAddr(I.getOperand(0), I)) |
| 299 | { |
| 300 | I.setOperand(0, gep); // replace pointer operand |
| 301 | } |
| 302 | else if (I.getType() == Type::UIntTy && |
| 303 | I.getOperand(0)->getType()->isFloatingPoint()) |
Vikram S. Adve | e9cb735 | 2002-09-27 14:24:45 +0000 | [diff] [blame] | 304 | { // insert a cast-fp-to-long before I, and then replace the operand of I |
| 305 | castI = new CastInst(I.getOperand(0), Type::LongTy, "fp2Long2Uint", &I); |
| 306 | I.setOperand(0, castI); // replace fp operand with long |
| 307 | } |
| 308 | |
| 309 | // Perform other transformations common to all instructions |
| 310 | visitInstruction(I); |
| 311 | if (castI) |
| 312 | visitInstruction(*castI); |
| 313 | } |
| 314 | |
| 315 | |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 316 | // visitOperands() transforms individual operands of all instructions: |
| 317 | // -- Load "large" int constants into a virtual register. What is large |
| 318 | // depends on the type of instruction and on the target architecture. |
| 319 | // -- For any constants that cannot be put in an immediate field, |
| 320 | // load address into virtual register first, and then load the constant. |
| 321 | // |
| 322 | void |
| 323 | PreSelection::visitOperands(Instruction &I) |
| 324 | { |
| 325 | // For any instruction other than PHI, copies go just before the instr. |
| 326 | // For a PHI, operand copies must be before the terminator of the |
| 327 | // appropriate predecessor basic block. Remaining logic is simple |
| 328 | // so just handle PHIs and other instructions separately. |
| 329 | // |
| 330 | if (PHINode* phi = dyn_cast<PHINode>(&I)) |
| 331 | { |
| 332 | for (unsigned i=0, N=phi->getNumIncomingValues(); i < N; ++i) |
| 333 | if (Constant* CV = dyn_cast<Constant>(phi->getIncomingValue(i))) |
| 334 | this->visitOneOperand(I, CV, phi->getOperandNumForIncomingValue(i), |
| 335 | * phi->getIncomingBlock(i)->getTerminator()); |
| 336 | } |
| 337 | else |
| 338 | for (unsigned i=0, N=I.getNumOperands(); i < N; ++i) |
| 339 | if (Constant* CV = dyn_cast<Constant>(I.getOperand(i))) |
| 340 | this->visitOneOperand(I, CV, i, I); |
| 341 | } |
| 342 | |
| 343 | void |
| 344 | PreSelection::visitOneOperand(Instruction &I, Constant* CV, unsigned opNum, |
| 345 | Instruction& insertBefore) |
| 346 | { |
Vikram S. Adve | d0451a9 | 2002-10-13 00:01:57 +0000 | [diff] [blame] | 347 | if (ConstantExpr* CE = dyn_cast<ConstantExpr>(CV)) |
| 348 | { // load-time constant: factor it out so we optimize as best we can |
| 349 | Instruction* computeConst = DecomposeConstantExpr(CE, insertBefore); |
| 350 | I.setOperand(opNum, computeConst); // replace expr operand with result |
| 351 | } |
| 352 | else if (target.getInstrInfo().ConstantTypeMustBeLoaded(CV)) |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 353 | { // load address of constant into a register, then load the constant |
| 354 | GetElementPtrInst* gep = getGlobalAddr(getGlobalForConstant(CV), |
Vikram S. Adve | d0451a9 | 2002-10-13 00:01:57 +0000 | [diff] [blame] | 355 | insertBefore); |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 356 | LoadInst* ldI = new LoadInst(gep, "loadConst", &insertBefore); |
| 357 | I.setOperand(opNum, ldI); // replace operand with copy in v.reg. |
| 358 | } |
| 359 | else if (target.getInstrInfo().ConstantMayNotFitInImmedField(CV, &I)) |
| 360 | { // put the constant into a virtual register using a cast |
| 361 | CastInst* castI = new CastInst(CV, CV->getType(), "copyConst", |
| 362 | &insertBefore); |
| 363 | I.setOperand(opNum, castI); // replace operand with copy in v.reg. |
| 364 | } |
| 365 | } |
| 366 | |
Vikram S. Adve | d0451a9 | 2002-10-13 00:01:57 +0000 | [diff] [blame] | 367 | |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 368 | //===----------------------------------------------------------------------===// |
| 369 | // createPreSelectionPass - Public entrypoint for pre-selection pass |
| 370 | // and this file as a whole... |
| 371 | // |
| 372 | Pass* |
| 373 | createPreSelectionPass(TargetMachine &T) |
| 374 | { |
| 375 | return new PreSelection(T); |
| 376 | } |
| 377 | |