blob: 044caa7d317859cf0344cb3dff5a76fe86f3b65a [file] [log] [blame]
Vikram S. Adveabf055c2002-09-20 00:29:28 +00001//===- PreSelection.cpp - Specialize LLVM code for target machine ---------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
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. Adveabf055c2002-09-20 00:29:28 +00009//
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 Lattner67699ff2003-09-01 20:33:07 +000018#include "SparcInternals.h"
Vikram S. Adveabf055c2002-09-20 00:29:28 +000019#include "llvm/Target/TargetMachine.h"
Chris Lattner3501fea2003-01-14 22:00:31 +000020#include "llvm/Target/TargetInstrInfo.h"
Vikram S. Adveabf055c2002-09-20 00:29:28 +000021#include "llvm/Transforms/Scalar.h"
22#include "llvm/Support/InstVisitor.h"
23#include "llvm/Module.h"
24#include "llvm/Constants.h"
25#include "llvm/iMemory.h"
26#include "llvm/iPHINode.h"
27#include "llvm/iOther.h"
28#include "llvm/DerivedTypes.h"
29#include "llvm/Pass.h"
Vikram S. Adved0451a92002-10-13 00:01:57 +000030#include <algorithm>
Vikram S. Adveabf055c2002-09-20 00:29:28 +000031
32namespace {
Vikram S. Adveabf055c2002-09-20 00:29:28 +000033
34 //===--------------------------------------------------------------------===//
35 // PreSelection Pass - Specialize LLVM code for the current target machine.
Vikram S. Adveabf055c2002-09-20 00:29:28 +000036 //
Chris Lattnerdac91312003-10-21 14:49:19 +000037 class PreSelection : public Pass, public InstVisitor<PreSelection> {
Vikram S. Advecf819452003-06-05 21:12:56 +000038 const TargetInstrInfo &instrInfo;
Chris Lattnerdac91312003-10-21 14:49:19 +000039 Module *TheModule;
40
41 std::map<const Constant*, GlobalVariable*> gvars;
Vikram S. Adveabf055c2002-09-20 00:29:28 +000042
Vikram S. Adveabf055c2002-09-20 00:29:28 +000043 GlobalVariable* getGlobalForConstant(Constant* CV) {
Chris Lattnerdac91312003-10-21 14:49:19 +000044 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. Adveabf055c2002-09-20 00:29:28 +000050 }
51
52 public:
Chris Lattnerdac91312003-10-21 14:49:19 +000053 PreSelection(const TargetMachine &T)
54 : instrInfo(T.getInstrInfo()), TheModule(0) {}
Vikram S. Adveabf055c2002-09-20 00:29:28 +000055
56 // runOnBasicBlock - apply this pass to each BB
Chris Lattnerdac91312003-10-21 14:49:19 +000057 bool run(Module &M) {
58 TheModule = &M;
Vikram S. Adveabf055c2002-09-20 00:29:28 +000059
Chris Lattnerdac91312003-10-21 14:49:19 +000060 // 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. Adveabf055c2002-09-20 00:29:28 +000070 }
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. Adve96358672003-05-31 07:34:57 +000075 void visitCallInst(CallInst &I);
Chris Lattner4c62e792003-10-21 16:09:23 +000076 void visitPHINode(PHINode &PN);
Vikram S. Adveabf055c2002-09-20 00:29:28 +000077
78 // Helper functions for visiting operands of every instruction
Vikram S. Adve96358672003-05-31 07:34:57 +000079 //
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 Lattnerd48a1d72003-10-21 16:06:07 +000085 void visitOperands(Instruction &I, int firstOp=0);
Vikram S. Adve96358672003-05-31 07:34:57 +000086 void visitOneOperand(Instruction &I, Value* Op, unsigned opNum,
87 Instruction& insertBefore);
Vikram S. Adveabf055c2002-09-20 00:29:28 +000088 };
Chris Lattnerec8aae32003-04-24 18:35:51 +000089
90 // Register the pass...
91 RegisterOpt<PreSelection> X("preselect",
92 "Specialize LLVM code for a target machine",
93 createPreSelectionPass);
Vikram S. Adveabf055c2002-09-20 00:29:28 +000094} // end anonymous namespace
95
96
Vikram S. Adved0451a92002-10-13 00:01:57 +000097//------------------------------------------------------------------------------
98// Helper functions used by methods of class PreSelection
99//------------------------------------------------------------------------------
100
101
102// getGlobalAddr(): Put address of a global into a v. register.
103static GetElementPtrInst* getGlobalAddr(Value* ptr, Instruction& insertBefore)
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000104{
Vikram S. Adved0451a92002-10-13 00:01:57 +0000105 if (isa<ConstantPointerRef>(ptr))
106 ptr = cast<ConstantPointerRef>(ptr)->getValue();
107
Chris Lattner2ab5e122003-06-04 01:24:40 +0000108 return (isa<GlobalVariable>(ptr))
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000109 ? new GetElementPtrInst(ptr,
110 std::vector<Value*>(1, ConstantSInt::get(Type::LongTy, 0U)),
Vikram S. Adved0451a92002-10-13 00:01:57 +0000111 "addrOfGlobal", &insertBefore)
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000112 : NULL;
113}
114
115
Vikram S. Adved0451a92002-10-13 00:01:57 +0000116// Wrapper on Constant::classof to use in find_if :-(
117inline static bool nonConstant(const Use& U)
118{
119 return ! isa<Constant>(U);
120}
121
122
123static Instruction* DecomposeConstantExpr(ConstantExpr* CE,
124 Instruction& insertBefore)
125{
126 Value *getArg1, *getArg2;
127
128 switch(CE->getOpcode())
129 {
130 case Instruction::Cast:
131 getArg1 = CE->getOperand(0);
132 if (ConstantExpr* CEarg = dyn_cast<ConstantExpr>(getArg1))
133 getArg1 = DecomposeConstantExpr(CEarg, insertBefore);
134 return new CastInst(getArg1, CE->getType(), "constantCast",&insertBefore);
135
136 case Instruction::GetElementPtr:
Chris Lattnerdc476b82002-10-27 19:09:51 +0000137 assert(find_if(CE->op_begin()+1, CE->op_end(),nonConstant) == CE->op_end()
Vikram S. Adved0451a92002-10-13 00:01:57 +0000138 && "All indices in ConstantExpr getelementptr must be constant!");
Vikram S. Adved0451a92002-10-13 00:01:57 +0000139 getArg1 = CE->getOperand(0);
140 if (ConstantExpr* CEarg = dyn_cast<ConstantExpr>(getArg1))
141 getArg1 = DecomposeConstantExpr(CEarg, insertBefore);
142 else if (GetElementPtrInst* gep = getGlobalAddr(getArg1, insertBefore))
143 getArg1 = gep;
144 return new GetElementPtrInst(getArg1,
Chris Lattnerdc476b82002-10-27 19:09:51 +0000145 std::vector<Value*>(CE->op_begin()+1, CE->op_end()),
Vikram S. Adved0451a92002-10-13 00:01:57 +0000146 "constantGEP", &insertBefore);
147
148 default: // must be a binary operator
Chris Lattner0b16ae22002-10-13 19:39:16 +0000149 assert(CE->getOpcode() >= Instruction::BinaryOpsBegin &&
150 CE->getOpcode() < Instruction::BinaryOpsEnd &&
Vikram S. Adved0451a92002-10-13 00:01:57 +0000151 "Unrecognized opcode in ConstantExpr");
152 getArg1 = CE->getOperand(0);
153 if (ConstantExpr* CEarg = dyn_cast<ConstantExpr>(getArg1))
154 getArg1 = DecomposeConstantExpr(CEarg, insertBefore);
155 getArg2 = CE->getOperand(1);
156 if (ConstantExpr* CEarg = dyn_cast<ConstantExpr>(getArg2))
157 getArg2 = DecomposeConstantExpr(CEarg, insertBefore);
158 return BinaryOperator::create((Instruction::BinaryOps) CE->getOpcode(),
159 getArg1, getArg2,
160 "constantBinaryOp", &insertBefore);
161 }
162}
163
164
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000165//------------------------------------------------------------------------------
166// Instruction visitor methods to perform instruction-specific operations
167//------------------------------------------------------------------------------
Vikram S. Advecf819452003-06-05 21:12:56 +0000168inline void
169PreSelection::visitOneOperand(Instruction &I, Value* Op, unsigned opNum,
170 Instruction& insertBefore)
171{
Vikram S. Adve799ffee2003-07-02 01:23:15 +0000172 assert(&insertBefore != NULL && "Must have instruction to insert before.");
173
Vikram S. Advecf819452003-06-05 21:12:56 +0000174 if (GetElementPtrInst* gep = getGlobalAddr(Op, insertBefore)) {
175 I.setOperand(opNum, gep); // replace global operand
Vikram S. Adve799ffee2003-07-02 01:23:15 +0000176 return; // nothing more to do for this op.
Vikram S. Advecf819452003-06-05 21:12:56 +0000177 }
178
179 Constant* CV = dyn_cast<Constant>(Op);
180 if (CV == NULL)
181 return;
182
183 if (ConstantExpr* CE = dyn_cast<ConstantExpr>(CV))
184 { // load-time constant: factor it out so we optimize as best we can
185 Instruction* computeConst = DecomposeConstantExpr(CE, insertBefore);
186 I.setOperand(opNum, computeConst); // replace expr operand with result
187 }
188 else if (instrInfo.ConstantTypeMustBeLoaded(CV))
189 { // load address of constant into a register, then load the constant
190 GetElementPtrInst* gep = getGlobalAddr(getGlobalForConstant(CV),
191 insertBefore);
192 LoadInst* ldI = new LoadInst(gep, "loadConst", &insertBefore);
193 I.setOperand(opNum, ldI); // replace operand with copy in v.reg.
194 }
195 else if (instrInfo.ConstantMayNotFitInImmedField(CV, &I))
196 { // put the constant into a virtual register using a cast
197 CastInst* castI = new CastInst(CV, CV->getType(), "copyConst",
198 &insertBefore);
199 I.setOperand(opNum, castI); // replace operand with copy in v.reg.
200 }
201}
202
203// visitOperands() transforms individual operands of all instructions:
204// -- Load "large" int constants into a virtual register. What is large
205// depends on the type of instruction and on the target architecture.
206// -- For any constants that cannot be put in an immediate field,
207// load address into virtual register first, and then load the constant.
208//
209// firstOp and lastOp can be used to skip leading and trailing operands.
210// If lastOp is 0, it defaults to #operands or #incoming Phi values.
211//
Chris Lattnerd48a1d72003-10-21 16:06:07 +0000212inline void PreSelection::visitOperands(Instruction &I, int firstOp) {
Vikram S. Advecf819452003-06-05 21:12:56 +0000213 // For any instruction other than PHI, copies go just before the instr.
Chris Lattner4c62e792003-10-21 16:09:23 +0000214 for (unsigned i = firstOp, e = I.getNumOperands(); i != e; ++i)
215 visitOneOperand(I, I.getOperand(i), i, I);
216}
217
218
219void PreSelection::visitPHINode(PHINode &PN) {
Vikram S. Advecf819452003-06-05 21:12:56 +0000220 // For a PHI, operand copies must be before the terminator of the
221 // appropriate predecessor basic block. Remaining logic is simple
222 // so just handle PHIs and other instructions separately.
223 //
Chris Lattner4c62e792003-10-21 16:09:23 +0000224 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
Chris Lattner08d702b2003-10-21 17:22:23 +0000225 visitOneOperand(PN, PN.getIncomingValue(i),
Chris Lattner4c62e792003-10-21 16:09:23 +0000226 PN.getOperandNumForIncomingValue(i),
227 *PN.getIncomingBlock(i)->getTerminator());
228 // do not call visitOperands!
Vikram S. Advecf819452003-06-05 21:12:56 +0000229}
230
231
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000232
233// Common work for *all* instructions. This needs to be called explicitly
234// by other visit<InstructionType> functions.
235inline void
236PreSelection::visitInstruction(Instruction &I)
237{
238 visitOperands(I); // Perform operand transformations
239}
240
241
242// GetElementPtr instructions: check if pointer is a global
243void
244PreSelection::visitGetElementPtrInst(GetElementPtrInst &I)
245{
Vikram S. Adve799ffee2003-07-02 01:23:15 +0000246 Instruction* curI = &I;
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000247
248 // Decompose multidimensional array references
Vikram S. Adve799ffee2003-07-02 01:23:15 +0000249 if (I.getNumIndices() >= 2) {
250 // DecomposeArrayRef() replaces I and deletes it, if successful,
251 // so remember predecessor in order to find the replacement instruction.
252 // Also remember the basic block in case there is no predecessor.
253 Instruction* prevI = I.getPrev();
254 BasicBlock* bb = I.getParent();
255 if (DecomposeArrayRef(&I))
256 // first instr. replacing I
257 curI = cast<GetElementPtrInst>(prevI? prevI->getNext() : &bb->front());
258 }
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000259
260 // Perform other transformations common to all instructions
Vikram S. Adve799ffee2003-07-02 01:23:15 +0000261 visitInstruction(*curI);
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000262}
263
264
Vikram S. Adve96358672003-05-31 07:34:57 +0000265void
266PreSelection::visitCallInst(CallInst &I)
267{
268 // Tell visitOperands to ignore the function name if this is a direct call.
269 visitOperands(I, (/*firstOp=*/ I.getCalledFunction()? 1 : 0));
270}
271
Vikram S. Advee9cb7352002-09-27 14:24:45 +0000272
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000273//===----------------------------------------------------------------------===//
274// createPreSelectionPass - Public entrypoint for pre-selection pass
275// and this file as a whole...
276//
Chris Lattnerdac91312003-10-21 14:49:19 +0000277Pass* createPreSelectionPass(TargetMachine &T) {
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000278 return new PreSelection(T);
279}
280