Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 1 | //===- PreSelection.cpp - Specialize LLVM code for target machine ---------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file defines the PreSelection pass which specializes LLVM code for a |
| 11 | // target machine, while remaining in legal portable LLVM form and |
| 12 | // preserving type information and type safety. This is meant to enable |
| 13 | // dataflow optimizations on target-specific operations such as accesses to |
| 14 | // constants, globals, and array indexing. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Chris Lattner | 67699ff | 2003-09-01 20:33:07 +0000 | [diff] [blame] | 18 | #include "SparcInternals.h" |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 19 | #include "llvm/Constants.h" |
Misha Brukman | bb8c863 | 2003-10-22 04:51:36 +0000 | [diff] [blame^] | 20 | #include "llvm/DerivedTypes.h" |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 21 | #include "llvm/iMemory.h" |
| 22 | #include "llvm/iPHINode.h" |
| 23 | #include "llvm/iOther.h" |
Misha Brukman | 523b30c | 2003-10-22 03:27:45 +0000 | [diff] [blame] | 24 | #include "llvm/Module.h" |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 25 | #include "llvm/Pass.h" |
Misha Brukman | 523b30c | 2003-10-22 03:27:45 +0000 | [diff] [blame] | 26 | #include "llvm/Support/InstVisitor.h" |
| 27 | #include "llvm/Target/TargetInstrInfo.h" |
| 28 | #include "llvm/Target/TargetMachine.h" |
| 29 | #include "llvm/Transforms/Scalar.h" |
Vikram S. Adve | d0451a9 | 2002-10-13 00:01:57 +0000 | [diff] [blame] | 30 | #include <algorithm> |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 31 | |
| 32 | namespace { |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 33 | |
| 34 | //===--------------------------------------------------------------------===// |
| 35 | // PreSelection Pass - Specialize LLVM code for the current target machine. |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 36 | // |
Chris Lattner | dac9131 | 2003-10-21 14:49:19 +0000 | [diff] [blame] | 37 | class PreSelection : public Pass, public InstVisitor<PreSelection> { |
Vikram S. Adve | cf81945 | 2003-06-05 21:12:56 +0000 | [diff] [blame] | 38 | const TargetInstrInfo &instrInfo; |
Chris Lattner | dac9131 | 2003-10-21 14:49:19 +0000 | [diff] [blame] | 39 | Module *TheModule; |
| 40 | |
| 41 | std::map<const Constant*, GlobalVariable*> gvars; |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 42 | |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 43 | GlobalVariable* getGlobalForConstant(Constant* CV) { |
Chris Lattner | dac9131 | 2003-10-21 14:49:19 +0000 | [diff] [blame] | 44 | std::map<const Constant*, GlobalVariable*>::iterator I = gvars.find(CV); |
| 45 | if (I != gvars.end()) return I->second; // global exists so return it |
| 46 | |
| 47 | return I->second = new GlobalVariable(CV->getType(), true, |
| 48 | GlobalValue::InternalLinkage, CV, |
| 49 | "immcst", TheModule); |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | public: |
Chris Lattner | dac9131 | 2003-10-21 14:49:19 +0000 | [diff] [blame] | 53 | PreSelection(const TargetMachine &T) |
| 54 | : instrInfo(T.getInstrInfo()), TheModule(0) {} |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 55 | |
Misha Brukman | 523b30c | 2003-10-22 03:27:45 +0000 | [diff] [blame] | 56 | // run - apply this pass to the entire Module |
Chris Lattner | dac9131 | 2003-10-21 14:49:19 +0000 | [diff] [blame] | 57 | bool run(Module &M) { |
| 58 | TheModule = &M; |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 59 | |
Chris Lattner | dac9131 | 2003-10-21 14:49:19 +0000 | [diff] [blame] | 60 | // Build reverse map for pre-existing global constants so we can find them |
| 61 | for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I) |
| 62 | if (I->hasInitializer() && I->isConstant()) |
| 63 | gvars[I->getInitializer()] = I; |
| 64 | |
| 65 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 66 | visit(*I); |
| 67 | |
| 68 | gvars.clear(); |
| 69 | return true; |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | // These methods do the actual work of specializing code |
| 73 | void visitInstruction(Instruction &I); // common work for every instr. |
| 74 | void visitGetElementPtrInst(GetElementPtrInst &I); |
Vikram S. Adve | 9635867 | 2003-05-31 07:34:57 +0000 | [diff] [blame] | 75 | void visitCallInst(CallInst &I); |
Chris Lattner | 4c62e79 | 2003-10-21 16:09:23 +0000 | [diff] [blame] | 76 | void visitPHINode(PHINode &PN); |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 77 | |
| 78 | // Helper functions for visiting operands of every instruction |
Vikram S. Adve | 9635867 | 2003-05-31 07:34:57 +0000 | [diff] [blame] | 79 | // |
| 80 | // visitOperands() works on every operand in [firstOp, lastOp-1]. |
| 81 | // If lastOp==0, lastOp defaults to #operands or #incoming Phi values. |
| 82 | // |
| 83 | // visitOneOperand() does all the work for one operand. |
| 84 | // |
Chris Lattner | d48a1d7 | 2003-10-21 16:06:07 +0000 | [diff] [blame] | 85 | void visitOperands(Instruction &I, int firstOp=0); |
Vikram S. Adve | 9635867 | 2003-05-31 07:34:57 +0000 | [diff] [blame] | 86 | void visitOneOperand(Instruction &I, Value* Op, unsigned opNum, |
| 87 | Instruction& insertBefore); |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 88 | }; |
Chris Lattner | ec8aae3 | 2003-04-24 18:35:51 +0000 | [diff] [blame] | 89 | |
| 90 | // Register the pass... |
| 91 | RegisterOpt<PreSelection> X("preselect", |
| 92 | "Specialize LLVM code for a target machine", |
| 93 | createPreSelectionPass); |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 94 | } // end anonymous namespace |
| 95 | |
| 96 | |
Vikram S. Adve | d0451a9 | 2002-10-13 00:01:57 +0000 | [diff] [blame] | 97 | //------------------------------------------------------------------------------ |
| 98 | // Helper functions used by methods of class PreSelection |
| 99 | //------------------------------------------------------------------------------ |
| 100 | |
| 101 | |
| 102 | // getGlobalAddr(): Put address of a global into a v. register. |
Misha Brukman | 523b30c | 2003-10-22 03:27:45 +0000 | [diff] [blame] | 103 | static GetElementPtrInst* getGlobalAddr(Value* ptr, Instruction& insertBefore) { |
Vikram S. Adve | d0451a9 | 2002-10-13 00:01:57 +0000 | [diff] [blame] | 104 | if (isa<ConstantPointerRef>(ptr)) |
| 105 | ptr = cast<ConstantPointerRef>(ptr)->getValue(); |
| 106 | |
Chris Lattner | 2ab5e12 | 2003-06-04 01:24:40 +0000 | [diff] [blame] | 107 | return (isa<GlobalVariable>(ptr)) |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 108 | ? new GetElementPtrInst(ptr, |
| 109 | std::vector<Value*>(1, ConstantSInt::get(Type::LongTy, 0U)), |
Vikram S. Adve | d0451a9 | 2002-10-13 00:01:57 +0000 | [diff] [blame] | 110 | "addrOfGlobal", &insertBefore) |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 111 | : NULL; |
| 112 | } |
| 113 | |
| 114 | |
Vikram S. Adve | d0451a9 | 2002-10-13 00:01:57 +0000 | [diff] [blame] | 115 | // Wrapper on Constant::classof to use in find_if :-( |
Misha Brukman | 523b30c | 2003-10-22 03:27:45 +0000 | [diff] [blame] | 116 | inline static bool nonConstant(const Use& U) { |
Vikram S. Adve | d0451a9 | 2002-10-13 00:01:57 +0000 | [diff] [blame] | 117 | return ! isa<Constant>(U); |
| 118 | } |
| 119 | |
| 120 | |
| 121 | static Instruction* DecomposeConstantExpr(ConstantExpr* CE, |
| 122 | Instruction& insertBefore) |
| 123 | { |
| 124 | Value *getArg1, *getArg2; |
| 125 | |
| 126 | switch(CE->getOpcode()) |
| 127 | { |
| 128 | case Instruction::Cast: |
| 129 | getArg1 = CE->getOperand(0); |
| 130 | if (ConstantExpr* CEarg = dyn_cast<ConstantExpr>(getArg1)) |
| 131 | getArg1 = DecomposeConstantExpr(CEarg, insertBefore); |
| 132 | return new CastInst(getArg1, CE->getType(), "constantCast",&insertBefore); |
| 133 | |
| 134 | case Instruction::GetElementPtr: |
Chris Lattner | dc476b8 | 2002-10-27 19:09:51 +0000 | [diff] [blame] | 135 | 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] | 136 | && "All indices in ConstantExpr getelementptr must be constant!"); |
Vikram S. Adve | d0451a9 | 2002-10-13 00:01:57 +0000 | [diff] [blame] | 137 | getArg1 = CE->getOperand(0); |
| 138 | if (ConstantExpr* CEarg = dyn_cast<ConstantExpr>(getArg1)) |
| 139 | getArg1 = DecomposeConstantExpr(CEarg, insertBefore); |
| 140 | else if (GetElementPtrInst* gep = getGlobalAddr(getArg1, insertBefore)) |
| 141 | getArg1 = gep; |
| 142 | return new GetElementPtrInst(getArg1, |
Chris Lattner | dc476b8 | 2002-10-27 19:09:51 +0000 | [diff] [blame] | 143 | std::vector<Value*>(CE->op_begin()+1, CE->op_end()), |
Vikram S. Adve | d0451a9 | 2002-10-13 00:01:57 +0000 | [diff] [blame] | 144 | "constantGEP", &insertBefore); |
| 145 | |
| 146 | default: // must be a binary operator |
Chris Lattner | 0b16ae2 | 2002-10-13 19:39:16 +0000 | [diff] [blame] | 147 | assert(CE->getOpcode() >= Instruction::BinaryOpsBegin && |
| 148 | CE->getOpcode() < Instruction::BinaryOpsEnd && |
Vikram S. Adve | d0451a9 | 2002-10-13 00:01:57 +0000 | [diff] [blame] | 149 | "Unrecognized opcode in ConstantExpr"); |
| 150 | getArg1 = CE->getOperand(0); |
| 151 | if (ConstantExpr* CEarg = dyn_cast<ConstantExpr>(getArg1)) |
| 152 | getArg1 = DecomposeConstantExpr(CEarg, insertBefore); |
| 153 | getArg2 = CE->getOperand(1); |
| 154 | if (ConstantExpr* CEarg = dyn_cast<ConstantExpr>(getArg2)) |
| 155 | getArg2 = DecomposeConstantExpr(CEarg, insertBefore); |
| 156 | return BinaryOperator::create((Instruction::BinaryOps) CE->getOpcode(), |
| 157 | getArg1, getArg2, |
| 158 | "constantBinaryOp", &insertBefore); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 163 | //------------------------------------------------------------------------------ |
| 164 | // Instruction visitor methods to perform instruction-specific operations |
| 165 | //------------------------------------------------------------------------------ |
Vikram S. Adve | cf81945 | 2003-06-05 21:12:56 +0000 | [diff] [blame] | 166 | inline void |
| 167 | PreSelection::visitOneOperand(Instruction &I, Value* Op, unsigned opNum, |
| 168 | Instruction& insertBefore) |
| 169 | { |
Vikram S. Adve | 799ffee | 2003-07-02 01:23:15 +0000 | [diff] [blame] | 170 | assert(&insertBefore != NULL && "Must have instruction to insert before."); |
| 171 | |
Vikram S. Adve | cf81945 | 2003-06-05 21:12:56 +0000 | [diff] [blame] | 172 | if (GetElementPtrInst* gep = getGlobalAddr(Op, insertBefore)) { |
| 173 | I.setOperand(opNum, gep); // replace global operand |
Vikram S. Adve | 799ffee | 2003-07-02 01:23:15 +0000 | [diff] [blame] | 174 | return; // nothing more to do for this op. |
Vikram S. Adve | cf81945 | 2003-06-05 21:12:56 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | Constant* CV = dyn_cast<Constant>(Op); |
| 178 | if (CV == NULL) |
| 179 | return; |
| 180 | |
Misha Brukman | 523b30c | 2003-10-22 03:27:45 +0000 | [diff] [blame] | 181 | if (ConstantExpr* CE = dyn_cast<ConstantExpr>(CV)) { |
| 182 | // load-time constant: factor it out so we optimize as best we can |
| 183 | Instruction* computeConst = DecomposeConstantExpr(CE, insertBefore); |
| 184 | I.setOperand(opNum, computeConst); // replace expr operand with result |
| 185 | } else if (instrInfo.ConstantTypeMustBeLoaded(CV)) { |
| 186 | // load address of constant into a register, then load the constant |
| 187 | GetElementPtrInst* gep = getGlobalAddr(getGlobalForConstant(CV), |
| 188 | insertBefore); |
| 189 | LoadInst* ldI = new LoadInst(gep, "loadConst", &insertBefore); |
| 190 | I.setOperand(opNum, ldI); // replace operand with copy in v.reg. |
| 191 | } else if (instrInfo.ConstantMayNotFitInImmedField(CV, &I)) { |
| 192 | // put the constant into a virtual register using a cast |
| 193 | CastInst* castI = new CastInst(CV, CV->getType(), "copyConst", |
| 194 | &insertBefore); |
| 195 | I.setOperand(opNum, castI); // replace operand with copy in v.reg. |
| 196 | } |
Vikram S. Adve | cf81945 | 2003-06-05 21:12:56 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | // visitOperands() transforms individual operands of all instructions: |
| 200 | // -- Load "large" int constants into a virtual register. What is large |
| 201 | // depends on the type of instruction and on the target architecture. |
| 202 | // -- For any constants that cannot be put in an immediate field, |
| 203 | // load address into virtual register first, and then load the constant. |
| 204 | // |
| 205 | // firstOp and lastOp can be used to skip leading and trailing operands. |
| 206 | // If lastOp is 0, it defaults to #operands or #incoming Phi values. |
| 207 | // |
Chris Lattner | d48a1d7 | 2003-10-21 16:06:07 +0000 | [diff] [blame] | 208 | inline void PreSelection::visitOperands(Instruction &I, int firstOp) { |
Vikram S. Adve | cf81945 | 2003-06-05 21:12:56 +0000 | [diff] [blame] | 209 | // For any instruction other than PHI, copies go just before the instr. |
Chris Lattner | 4c62e79 | 2003-10-21 16:09:23 +0000 | [diff] [blame] | 210 | for (unsigned i = firstOp, e = I.getNumOperands(); i != e; ++i) |
| 211 | visitOneOperand(I, I.getOperand(i), i, I); |
| 212 | } |
| 213 | |
| 214 | |
| 215 | void PreSelection::visitPHINode(PHINode &PN) { |
Vikram S. Adve | cf81945 | 2003-06-05 21:12:56 +0000 | [diff] [blame] | 216 | // For a PHI, operand copies must be before the terminator of the |
| 217 | // appropriate predecessor basic block. Remaining logic is simple |
| 218 | // so just handle PHIs and other instructions separately. |
| 219 | // |
Chris Lattner | 4c62e79 | 2003-10-21 16:09:23 +0000 | [diff] [blame] | 220 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) |
Chris Lattner | 08d702b | 2003-10-21 17:22:23 +0000 | [diff] [blame] | 221 | visitOneOperand(PN, PN.getIncomingValue(i), |
Chris Lattner | 4c62e79 | 2003-10-21 16:09:23 +0000 | [diff] [blame] | 222 | PN.getOperandNumForIncomingValue(i), |
| 223 | *PN.getIncomingBlock(i)->getTerminator()); |
| 224 | // do not call visitOperands! |
Vikram S. Adve | cf81945 | 2003-06-05 21:12:56 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 228 | |
| 229 | // Common work for *all* instructions. This needs to be called explicitly |
| 230 | // by other visit<InstructionType> functions. |
Misha Brukman | 523b30c | 2003-10-22 03:27:45 +0000 | [diff] [blame] | 231 | inline void PreSelection::visitInstruction(Instruction &I) { |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 232 | visitOperands(I); // Perform operand transformations |
| 233 | } |
| 234 | |
| 235 | |
| 236 | // GetElementPtr instructions: check if pointer is a global |
Misha Brukman | 523b30c | 2003-10-22 03:27:45 +0000 | [diff] [blame] | 237 | void PreSelection::visitGetElementPtrInst(GetElementPtrInst &I) { |
Vikram S. Adve | 799ffee | 2003-07-02 01:23:15 +0000 | [diff] [blame] | 238 | Instruction* curI = &I; |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 239 | |
| 240 | // Decompose multidimensional array references |
Vikram S. Adve | 799ffee | 2003-07-02 01:23:15 +0000 | [diff] [blame] | 241 | if (I.getNumIndices() >= 2) { |
| 242 | // DecomposeArrayRef() replaces I and deletes it, if successful, |
| 243 | // so remember predecessor in order to find the replacement instruction. |
| 244 | // Also remember the basic block in case there is no predecessor. |
| 245 | Instruction* prevI = I.getPrev(); |
| 246 | BasicBlock* bb = I.getParent(); |
| 247 | if (DecomposeArrayRef(&I)) |
| 248 | // first instr. replacing I |
| 249 | curI = cast<GetElementPtrInst>(prevI? prevI->getNext() : &bb->front()); |
| 250 | } |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 251 | |
| 252 | // Perform other transformations common to all instructions |
Vikram S. Adve | 799ffee | 2003-07-02 01:23:15 +0000 | [diff] [blame] | 253 | visitInstruction(*curI); |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Misha Brukman | 523b30c | 2003-10-22 03:27:45 +0000 | [diff] [blame] | 256 | void PreSelection::visitCallInst(CallInst &I) { |
Vikram S. Adve | 9635867 | 2003-05-31 07:34:57 +0000 | [diff] [blame] | 257 | // Tell visitOperands to ignore the function name if this is a direct call. |
| 258 | visitOperands(I, (/*firstOp=*/ I.getCalledFunction()? 1 : 0)); |
| 259 | } |
| 260 | |
Vikram S. Adve | e9cb735 | 2002-09-27 14:24:45 +0000 | [diff] [blame] | 261 | |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 262 | //===----------------------------------------------------------------------===// |
| 263 | // createPreSelectionPass - Public entrypoint for pre-selection pass |
| 264 | // and this file as a whole... |
| 265 | // |
Chris Lattner | dac9131 | 2003-10-21 14:49:19 +0000 | [diff] [blame] | 266 | Pass* createPreSelectionPass(TargetMachine &T) { |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 267 | return new PreSelection(T); |
| 268 | } |