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