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 | |
Brian Gaeke | e3d6807 | 2004-02-25 18:44:15 +0000 | [diff] [blame] | 18 | #include "SparcV9Internals.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 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 32 | namespace llvm { |
| 33 | |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 34 | namespace { |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 35 | |
| 36 | //===--------------------------------------------------------------------===// |
| 37 | // PreSelection Pass - Specialize LLVM code for the current target machine. |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 38 | // |
Misha Brukman | feed25f | 2003-11-07 17:31:22 +0000 | [diff] [blame] | 39 | class PreSelection : public FunctionPass, public InstVisitor<PreSelection> { |
Vikram S. Adve | cf81945 | 2003-06-05 21:12:56 +0000 | [diff] [blame] | 40 | const TargetInstrInfo &instrInfo; |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 41 | |
| 42 | public: |
Chris Lattner | dac9131 | 2003-10-21 14:49:19 +0000 | [diff] [blame] | 43 | PreSelection(const TargetMachine &T) |
Misha Brukman | feed25f | 2003-11-07 17:31:22 +0000 | [diff] [blame] | 44 | : instrInfo(T.getInstrInfo()) {} |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 45 | |
Misha Brukman | feed25f | 2003-11-07 17:31:22 +0000 | [diff] [blame] | 46 | // runOnFunction - apply this pass to each Function |
| 47 | bool runOnFunction(Function &F) { |
| 48 | visit(F); |
Chris Lattner | dac9131 | 2003-10-21 14:49:19 +0000 | [diff] [blame] | 49 | return true; |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 50 | } |
Brian Gaeke | c46f8a4 | 2004-03-11 19:23:15 +0000 | [diff] [blame] | 51 | const char *getPassName() const { return "SparcV9 Instr. Pre-selection"; } |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 52 | |
| 53 | // These methods do the actual work of specializing code |
| 54 | void visitInstruction(Instruction &I); // common work for every instr. |
| 55 | void visitGetElementPtrInst(GetElementPtrInst &I); |
Vikram S. Adve | 9635867 | 2003-05-31 07:34:57 +0000 | [diff] [blame] | 56 | void visitCallInst(CallInst &I); |
Chris Lattner | 4c62e79 | 2003-10-21 16:09:23 +0000 | [diff] [blame] | 57 | void visitPHINode(PHINode &PN); |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 58 | |
| 59 | // Helper functions for visiting operands of every instruction |
Vikram S. Adve | 9635867 | 2003-05-31 07:34:57 +0000 | [diff] [blame] | 60 | // |
| 61 | // visitOperands() works on every operand in [firstOp, lastOp-1]. |
| 62 | // If lastOp==0, lastOp defaults to #operands or #incoming Phi values. |
| 63 | // |
| 64 | // visitOneOperand() does all the work for one operand. |
| 65 | // |
Chris Lattner | d48a1d7 | 2003-10-21 16:06:07 +0000 | [diff] [blame] | 66 | void visitOperands(Instruction &I, int firstOp=0); |
Vikram S. Adve | 9635867 | 2003-05-31 07:34:57 +0000 | [diff] [blame] | 67 | void visitOneOperand(Instruction &I, Value* Op, unsigned opNum, |
| 68 | Instruction& insertBefore); |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 69 | }; |
Chris Lattner | ec8aae3 | 2003-04-24 18:35:51 +0000 | [diff] [blame] | 70 | |
Misha Brukman | feed25f | 2003-11-07 17:31:22 +0000 | [diff] [blame] | 71 | #if 0 |
Chris Lattner | ec8aae3 | 2003-04-24 18:35:51 +0000 | [diff] [blame] | 72 | // Register the pass... |
Misha Brukman | feed25f | 2003-11-07 17:31:22 +0000 | [diff] [blame] | 73 | RegisterPass<PreSelection> X("preselect", |
| 74 | "Specialize LLVM code for a target machine" |
| 75 | createPreselectionPass); |
| 76 | #endif |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 77 | |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 78 | } // end anonymous namespace |
| 79 | |
| 80 | |
Vikram S. Adve | d0451a9 | 2002-10-13 00:01:57 +0000 | [diff] [blame] | 81 | //------------------------------------------------------------------------------ |
| 82 | // Helper functions used by methods of class PreSelection |
| 83 | //------------------------------------------------------------------------------ |
| 84 | |
| 85 | |
| 86 | // getGlobalAddr(): Put address of a global into a v. register. |
Misha Brukman | 523b30c | 2003-10-22 03:27:45 +0000 | [diff] [blame] | 87 | static GetElementPtrInst* getGlobalAddr(Value* ptr, Instruction& insertBefore) { |
Vikram S. Adve | d0451a9 | 2002-10-13 00:01:57 +0000 | [diff] [blame] | 88 | if (isa<ConstantPointerRef>(ptr)) |
| 89 | ptr = cast<ConstantPointerRef>(ptr)->getValue(); |
| 90 | |
Chris Lattner | 2ab5e12 | 2003-06-04 01:24:40 +0000 | [diff] [blame] | 91 | return (isa<GlobalVariable>(ptr)) |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 92 | ? new GetElementPtrInst(ptr, |
| 93 | std::vector<Value*>(1, ConstantSInt::get(Type::LongTy, 0U)), |
Vikram S. Adve | d0451a9 | 2002-10-13 00:01:57 +0000 | [diff] [blame] | 94 | "addrOfGlobal", &insertBefore) |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 95 | : NULL; |
| 96 | } |
| 97 | |
Misha Brukman | c3402cd | 2003-12-17 22:06:08 +0000 | [diff] [blame] | 98 | // Wrapper on Constant::classof to use in find_if |
Misha Brukman | 523b30c | 2003-10-22 03:27:45 +0000 | [diff] [blame] | 99 | inline static bool nonConstant(const Use& U) { |
Vikram S. Adve | d0451a9 | 2002-10-13 00:01:57 +0000 | [diff] [blame] | 100 | return ! isa<Constant>(U); |
| 101 | } |
| 102 | |
Vikram S. Adve | d0451a9 | 2002-10-13 00:01:57 +0000 | [diff] [blame] | 103 | static Instruction* DecomposeConstantExpr(ConstantExpr* CE, |
| 104 | Instruction& insertBefore) |
| 105 | { |
| 106 | Value *getArg1, *getArg2; |
| 107 | |
| 108 | switch(CE->getOpcode()) |
| 109 | { |
| 110 | case Instruction::Cast: |
| 111 | getArg1 = CE->getOperand(0); |
| 112 | if (ConstantExpr* CEarg = dyn_cast<ConstantExpr>(getArg1)) |
| 113 | getArg1 = DecomposeConstantExpr(CEarg, insertBefore); |
| 114 | return new CastInst(getArg1, CE->getType(), "constantCast",&insertBefore); |
| 115 | |
| 116 | case Instruction::GetElementPtr: |
Chris Lattner | dc476b8 | 2002-10-27 19:09:51 +0000 | [diff] [blame] | 117 | 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] | 118 | && "All indices in ConstantExpr getelementptr must be constant!"); |
Vikram S. Adve | d0451a9 | 2002-10-13 00:01:57 +0000 | [diff] [blame] | 119 | getArg1 = CE->getOperand(0); |
| 120 | if (ConstantExpr* CEarg = dyn_cast<ConstantExpr>(getArg1)) |
| 121 | getArg1 = DecomposeConstantExpr(CEarg, insertBefore); |
| 122 | else if (GetElementPtrInst* gep = getGlobalAddr(getArg1, insertBefore)) |
| 123 | getArg1 = gep; |
| 124 | return new GetElementPtrInst(getArg1, |
Chris Lattner | dc476b8 | 2002-10-27 19:09:51 +0000 | [diff] [blame] | 125 | std::vector<Value*>(CE->op_begin()+1, CE->op_end()), |
Vikram S. Adve | d0451a9 | 2002-10-13 00:01:57 +0000 | [diff] [blame] | 126 | "constantGEP", &insertBefore); |
| 127 | |
| 128 | default: // must be a binary operator |
Chris Lattner | 0b16ae2 | 2002-10-13 19:39:16 +0000 | [diff] [blame] | 129 | assert(CE->getOpcode() >= Instruction::BinaryOpsBegin && |
| 130 | CE->getOpcode() < Instruction::BinaryOpsEnd && |
Vikram S. Adve | d0451a9 | 2002-10-13 00:01:57 +0000 | [diff] [blame] | 131 | "Unrecognized opcode in ConstantExpr"); |
| 132 | getArg1 = CE->getOperand(0); |
| 133 | if (ConstantExpr* CEarg = dyn_cast<ConstantExpr>(getArg1)) |
| 134 | getArg1 = DecomposeConstantExpr(CEarg, insertBefore); |
| 135 | getArg2 = CE->getOperand(1); |
| 136 | if (ConstantExpr* CEarg = dyn_cast<ConstantExpr>(getArg2)) |
| 137 | getArg2 = DecomposeConstantExpr(CEarg, insertBefore); |
| 138 | return BinaryOperator::create((Instruction::BinaryOps) CE->getOpcode(), |
| 139 | getArg1, getArg2, |
| 140 | "constantBinaryOp", &insertBefore); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 145 | //------------------------------------------------------------------------------ |
| 146 | // Instruction visitor methods to perform instruction-specific operations |
| 147 | //------------------------------------------------------------------------------ |
Vikram S. Adve | cf81945 | 2003-06-05 21:12:56 +0000 | [diff] [blame] | 148 | inline void |
| 149 | PreSelection::visitOneOperand(Instruction &I, Value* Op, unsigned opNum, |
| 150 | Instruction& insertBefore) |
| 151 | { |
Vikram S. Adve | 799ffee | 2003-07-02 01:23:15 +0000 | [diff] [blame] | 152 | assert(&insertBefore != NULL && "Must have instruction to insert before."); |
| 153 | |
Vikram S. Adve | cf81945 | 2003-06-05 21:12:56 +0000 | [diff] [blame] | 154 | if (GetElementPtrInst* gep = getGlobalAddr(Op, insertBefore)) { |
| 155 | I.setOperand(opNum, gep); // replace global operand |
Vikram S. Adve | 799ffee | 2003-07-02 01:23:15 +0000 | [diff] [blame] | 156 | return; // nothing more to do for this op. |
Vikram S. Adve | cf81945 | 2003-06-05 21:12:56 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | Constant* CV = dyn_cast<Constant>(Op); |
| 160 | if (CV == NULL) |
| 161 | return; |
| 162 | |
Misha Brukman | 523b30c | 2003-10-22 03:27:45 +0000 | [diff] [blame] | 163 | if (ConstantExpr* CE = dyn_cast<ConstantExpr>(CV)) { |
| 164 | // load-time constant: factor it out so we optimize as best we can |
| 165 | Instruction* computeConst = DecomposeConstantExpr(CE, insertBefore); |
| 166 | I.setOperand(opNum, computeConst); // replace expr operand with result |
| 167 | } else if (instrInfo.ConstantTypeMustBeLoaded(CV)) { |
| 168 | // load address of constant into a register, then load the constant |
Misha Brukman | feed25f | 2003-11-07 17:31:22 +0000 | [diff] [blame] | 169 | // this is now done during instruction selection |
| 170 | // the constant will live in the MachineConstantPool later on |
Misha Brukman | 523b30c | 2003-10-22 03:27:45 +0000 | [diff] [blame] | 171 | } else if (instrInfo.ConstantMayNotFitInImmedField(CV, &I)) { |
| 172 | // put the constant into a virtual register using a cast |
| 173 | CastInst* castI = new CastInst(CV, CV->getType(), "copyConst", |
| 174 | &insertBefore); |
| 175 | I.setOperand(opNum, castI); // replace operand with copy in v.reg. |
| 176 | } |
Vikram S. Adve | cf81945 | 2003-06-05 21:12:56 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Misha Brukman | c3402cd | 2003-12-17 22:06:08 +0000 | [diff] [blame] | 179 | /// visitOperands - transform individual operands of all instructions: |
| 180 | /// -- Load "large" int constants into a virtual register. What is large |
| 181 | /// depends on the type of instruction and on the target architecture. |
| 182 | /// -- For any constants that cannot be put in an immediate field, |
| 183 | /// load address into virtual register first, and then load the constant. |
| 184 | /// |
| 185 | /// firstOp and lastOp can be used to skip leading and trailing operands. |
| 186 | /// If lastOp is 0, it defaults to #operands or #incoming Phi values. |
| 187 | /// |
Chris Lattner | d48a1d7 | 2003-10-21 16:06:07 +0000 | [diff] [blame] | 188 | inline void PreSelection::visitOperands(Instruction &I, int firstOp) { |
Vikram S. Adve | cf81945 | 2003-06-05 21:12:56 +0000 | [diff] [blame] | 189 | // For any instruction other than PHI, copies go just before the instr. |
Chris Lattner | 4c62e79 | 2003-10-21 16:09:23 +0000 | [diff] [blame] | 190 | for (unsigned i = firstOp, e = I.getNumOperands(); i != e; ++i) |
| 191 | visitOneOperand(I, I.getOperand(i), i, I); |
| 192 | } |
| 193 | |
| 194 | |
| 195 | void PreSelection::visitPHINode(PHINode &PN) { |
Vikram S. Adve | cf81945 | 2003-06-05 21:12:56 +0000 | [diff] [blame] | 196 | // For a PHI, operand copies must be before the terminator of the |
| 197 | // appropriate predecessor basic block. Remaining logic is simple |
| 198 | // so just handle PHIs and other instructions separately. |
| 199 | // |
Chris Lattner | 4c62e79 | 2003-10-21 16:09:23 +0000 | [diff] [blame] | 200 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) |
Chris Lattner | 08d702b | 2003-10-21 17:22:23 +0000 | [diff] [blame] | 201 | visitOneOperand(PN, PN.getIncomingValue(i), |
Chris Lattner | 4c62e79 | 2003-10-21 16:09:23 +0000 | [diff] [blame] | 202 | PN.getOperandNumForIncomingValue(i), |
| 203 | *PN.getIncomingBlock(i)->getTerminator()); |
| 204 | // do not call visitOperands! |
Vikram S. Adve | cf81945 | 2003-06-05 21:12:56 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 207 | // Common work for *all* instructions. This needs to be called explicitly |
| 208 | // by other visit<InstructionType> functions. |
Misha Brukman | 523b30c | 2003-10-22 03:27:45 +0000 | [diff] [blame] | 209 | inline void PreSelection::visitInstruction(Instruction &I) { |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 210 | visitOperands(I); // Perform operand transformations |
| 211 | } |
| 212 | |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 213 | // GetElementPtr instructions: check if pointer is a global |
Misha Brukman | 523b30c | 2003-10-22 03:27:45 +0000 | [diff] [blame] | 214 | void PreSelection::visitGetElementPtrInst(GetElementPtrInst &I) { |
Vikram S. Adve | 799ffee | 2003-07-02 01:23:15 +0000 | [diff] [blame] | 215 | Instruction* curI = &I; |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 216 | |
| 217 | // Decompose multidimensional array references |
Vikram S. Adve | 799ffee | 2003-07-02 01:23:15 +0000 | [diff] [blame] | 218 | if (I.getNumIndices() >= 2) { |
| 219 | // DecomposeArrayRef() replaces I and deletes it, if successful, |
| 220 | // so remember predecessor in order to find the replacement instruction. |
| 221 | // Also remember the basic block in case there is no predecessor. |
| 222 | Instruction* prevI = I.getPrev(); |
| 223 | BasicBlock* bb = I.getParent(); |
| 224 | if (DecomposeArrayRef(&I)) |
| 225 | // first instr. replacing I |
| 226 | curI = cast<GetElementPtrInst>(prevI? prevI->getNext() : &bb->front()); |
| 227 | } |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 228 | |
| 229 | // Perform other transformations common to all instructions |
Vikram S. Adve | 799ffee | 2003-07-02 01:23:15 +0000 | [diff] [blame] | 230 | visitInstruction(*curI); |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Misha Brukman | 523b30c | 2003-10-22 03:27:45 +0000 | [diff] [blame] | 233 | void PreSelection::visitCallInst(CallInst &I) { |
Vikram S. Adve | 9635867 | 2003-05-31 07:34:57 +0000 | [diff] [blame] | 234 | // Tell visitOperands to ignore the function name if this is a direct call. |
| 235 | visitOperands(I, (/*firstOp=*/ I.getCalledFunction()? 1 : 0)); |
| 236 | } |
| 237 | |
Misha Brukman | c3402cd | 2003-12-17 22:06:08 +0000 | [diff] [blame] | 238 | /// createPreSelectionPass - Public entry point for the PreSelection pass |
| 239 | /// |
Misha Brukman | feed25f | 2003-11-07 17:31:22 +0000 | [diff] [blame] | 240 | FunctionPass* createPreSelectionPass(const TargetMachine &TM) { |
| 241 | return new PreSelection(TM); |
Vikram S. Adve | abf055c | 2002-09-20 00:29:28 +0000 | [diff] [blame] | 242 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 243 | |
| 244 | } // End llvm namespace |