blob: 1d8bb80bb87d9334283e19b29ab1951ed1560df1 [file] [log] [blame]
Vikram S. Adveabf055c2002-09-20 00:29:28 +00001//===- 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"
Chris Lattner3501fea2003-01-14 22:00:31 +000013#include "llvm/Target/TargetInstrInfo.h"
Vikram S. Adveabf055c2002-09-20 00:29:28 +000014#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. Adveabf055c2002-09-20 00:29:28 +000023#include "Support/CommandLine.h"
Vikram S. Adved0451a92002-10-13 00:01:57 +000024#include <algorithm>
Vikram S. Adveabf055c2002-09-20 00:29:28 +000025
26namespace {
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. Adved0451a92002-10-13 00:01:57 +000051 // this as an annotation on the Module object.
Vikram S. Adveabf055c2002-09-20 00:29:28 +000052 // A single GlobalVariable is created for each constant in the pool
53 // representing the memory for that constant.
54 //
Chris Lattner5ff7ef52003-05-01 20:28:45 +000055 AnnotationID CPFM_AID(
Vikram S. Adveabf055c2002-09-20 00:29:28 +000056 AnnotationManager::getID("CodeGen::ConstantPoolForModule"));
57
Chris Lattner5ff7ef52003-05-01 20:28:45 +000058 class ConstantPoolForModule : private Annotation {
Vikram S. Adveabf055c2002-09-20 00:29:28 +000059 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
Chris Lattner5ff7ef52003-05-01 20:28:45 +000063 ConstantPoolForModule(); // DO NOT IMPLEMENT
64 void operator=(const ConstantPoolForModule&); // DO NOT IMPLEMENT
Vikram S. Adveabf055c2002-09-20 00:29:28 +000065 public:
66 static ConstantPoolForModule& get(Module* M) {
67 ConstantPoolForModule* cpool =
68 (ConstantPoolForModule*) M->getAnnotation(CPFM_AID);
69 if (cpool == NULL) // create a new annotation and add it to the Module
70 M->addAnnotation(cpool = new ConstantPoolForModule(M));
71 return *cpool;
72 }
73
74 GlobalVariable* getGlobalForConstant(Constant* CV) {
75 std::map<const Constant*, GlobalVariable*>::iterator I = gvars.find(CV);
76 if (I != gvars.end())
77 return I->second; // global exists so return it
78 return addToConstantPool(CV); // create a new global and return it
79 }
80
81 GlobalVariable* addToConstantPool(Constant* CV) {
82 GlobalVariable*& GV = gvars[CV]; // handle to global var entry in map
83 if (GV == NULL)
84 { // check if a global constant already existed; otherwise create one
85 std::map<const Constant*, GlobalVariable*>::iterator PI =
86 origGVars.find(CV);
87 if (PI != origGVars.end())
88 GV = PI->second; // put in map
89 else
90 {
Chris Lattner4ad02e72003-04-16 20:28:45 +000091 GV = new GlobalVariable(CV->getType(), true, //put in map
92 GlobalValue::InternalLinkage, CV);
Vikram S. Adveabf055c2002-09-20 00:29:28 +000093 myModule->getGlobalList().push_back(GV); // GV owned by module now
94 }
95 }
96 return GV;
97 }
98 };
99
100 /* ctor */
101 ConstantPoolForModule::ConstantPoolForModule(Module* M)
102 : Annotation(CPFM_AID), myModule(M)
103 {
104 // Build reverse map for pre-existing global constants so we can find them
105 for (Module::giterator GI = M->gbegin(), GE = M->gend(); GI != GE; ++GI)
106 if (GI->hasInitializer() && GI->isConstant())
107 origGVars[GI->getInitializer()] = GI;
108 }
109
110 //===--------------------------------------------------------------------===//
111 // PreSelection Pass - Specialize LLVM code for the current target machine.
112 // This was and will be a basicblock pass, but make it a FunctionPass until
113 // BasicBlockPass ::doFinalization(Function&) is available.
114 //
115 class PreSelection : public BasicBlockPass, public InstVisitor<PreSelection>
116 {
117 const TargetMachine &target;
Vikram S. Advecf819452003-06-05 21:12:56 +0000118 const TargetInstrInfo &instrInfo;
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000119 Function* function;
120
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000121 GlobalVariable* getGlobalForConstant(Constant* CV) {
122 Module* M = function->getParent();
123 return ConstantPoolForModule::get(M).getGlobalForConstant(CV);
124 }
125
126 public:
Vikram S. Advecf819452003-06-05 21:12:56 +0000127 PreSelection (const TargetMachine &T):
128 target(T), instrInfo(T.getInstrInfo()), function(NULL) {}
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000129
130 // runOnBasicBlock - apply this pass to each BB
131 bool runOnBasicBlock(BasicBlock &BB) {
132 function = BB.getParent();
133 this->visit(BB);
134 return true;
135 }
136
137 bool doFinalization(Function &F) {
138 if (PreSelectDebugLevel >= PreSelect_PrintOutput)
Chris Lattner64317fc2003-01-14 20:32:10 +0000139 std::cerr << "\n\n*** LLVM code after pre-selection for function "
140 << F.getName() << ":\n\n" << F;
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000141 return false;
142 }
143
144 // These methods do the actual work of specializing code
145 void visitInstruction(Instruction &I); // common work for every instr.
146 void visitGetElementPtrInst(GetElementPtrInst &I);
147 void visitLoadInst(LoadInst &I);
Vikram S. Advee9cb7352002-09-27 14:24:45 +0000148 void visitCastInst(CastInst &I);
Vikram S. Adve96358672003-05-31 07:34:57 +0000149 void visitCallInst(CallInst &I);
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000150 void visitStoreInst(StoreInst &I);
151
152 // Helper functions for visiting operands of every instruction
Vikram S. Adve96358672003-05-31 07:34:57 +0000153 //
154 // visitOperands() works on every operand in [firstOp, lastOp-1].
155 // If lastOp==0, lastOp defaults to #operands or #incoming Phi values.
156 //
157 // visitOneOperand() does all the work for one operand.
158 //
159 void visitOperands(Instruction &I, int firstOp=0, int lastOp=0);
160 void visitOneOperand(Instruction &I, Value* Op, unsigned opNum,
161 Instruction& insertBefore);
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000162 };
Chris Lattnerec8aae32003-04-24 18:35:51 +0000163
164 // Register the pass...
165 RegisterOpt<PreSelection> X("preselect",
166 "Specialize LLVM code for a target machine",
167 createPreSelectionPass);
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000168} // end anonymous namespace
169
170
Vikram S. Adved0451a92002-10-13 00:01:57 +0000171//------------------------------------------------------------------------------
172// Helper functions used by methods of class PreSelection
173//------------------------------------------------------------------------------
174
175
176// getGlobalAddr(): Put address of a global into a v. register.
177static GetElementPtrInst* getGlobalAddr(Value* ptr, Instruction& insertBefore)
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000178{
Vikram S. Adved0451a92002-10-13 00:01:57 +0000179 if (isa<ConstantPointerRef>(ptr))
180 ptr = cast<ConstantPointerRef>(ptr)->getValue();
181
Chris Lattner2ab5e122003-06-04 01:24:40 +0000182 return (isa<GlobalVariable>(ptr))
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000183 ? new GetElementPtrInst(ptr,
184 std::vector<Value*>(1, ConstantSInt::get(Type::LongTy, 0U)),
Vikram S. Adved0451a92002-10-13 00:01:57 +0000185 "addrOfGlobal", &insertBefore)
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000186 : NULL;
187}
188
189
Vikram S. Adved0451a92002-10-13 00:01:57 +0000190// Wrapper on Constant::classof to use in find_if :-(
191inline static bool nonConstant(const Use& U)
192{
193 return ! isa<Constant>(U);
194}
195
196
197static Instruction* DecomposeConstantExpr(ConstantExpr* CE,
198 Instruction& insertBefore)
199{
200 Value *getArg1, *getArg2;
201
202 switch(CE->getOpcode())
203 {
204 case Instruction::Cast:
205 getArg1 = CE->getOperand(0);
206 if (ConstantExpr* CEarg = dyn_cast<ConstantExpr>(getArg1))
207 getArg1 = DecomposeConstantExpr(CEarg, insertBefore);
208 return new CastInst(getArg1, CE->getType(), "constantCast",&insertBefore);
209
210 case Instruction::GetElementPtr:
Chris Lattnerdc476b82002-10-27 19:09:51 +0000211 assert(find_if(CE->op_begin()+1, CE->op_end(),nonConstant) == CE->op_end()
Vikram S. Adved0451a92002-10-13 00:01:57 +0000212 && "All indices in ConstantExpr getelementptr must be constant!");
Vikram S. Adved0451a92002-10-13 00:01:57 +0000213 getArg1 = CE->getOperand(0);
214 if (ConstantExpr* CEarg = dyn_cast<ConstantExpr>(getArg1))
215 getArg1 = DecomposeConstantExpr(CEarg, insertBefore);
216 else if (GetElementPtrInst* gep = getGlobalAddr(getArg1, insertBefore))
217 getArg1 = gep;
218 return new GetElementPtrInst(getArg1,
Chris Lattnerdc476b82002-10-27 19:09:51 +0000219 std::vector<Value*>(CE->op_begin()+1, CE->op_end()),
Vikram S. Adved0451a92002-10-13 00:01:57 +0000220 "constantGEP", &insertBefore);
221
222 default: // must be a binary operator
Chris Lattner0b16ae22002-10-13 19:39:16 +0000223 assert(CE->getOpcode() >= Instruction::BinaryOpsBegin &&
224 CE->getOpcode() < Instruction::BinaryOpsEnd &&
Vikram S. Adved0451a92002-10-13 00:01:57 +0000225 "Unrecognized opcode in ConstantExpr");
226 getArg1 = CE->getOperand(0);
227 if (ConstantExpr* CEarg = dyn_cast<ConstantExpr>(getArg1))
228 getArg1 = DecomposeConstantExpr(CEarg, insertBefore);
229 getArg2 = CE->getOperand(1);
230 if (ConstantExpr* CEarg = dyn_cast<ConstantExpr>(getArg2))
231 getArg2 = DecomposeConstantExpr(CEarg, insertBefore);
232 return BinaryOperator::create((Instruction::BinaryOps) CE->getOpcode(),
233 getArg1, getArg2,
234 "constantBinaryOp", &insertBefore);
235 }
236}
237
238
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000239//------------------------------------------------------------------------------
240// Instruction visitor methods to perform instruction-specific operations
241//------------------------------------------------------------------------------
Vikram S. Advecf819452003-06-05 21:12:56 +0000242inline void
243PreSelection::visitOneOperand(Instruction &I, Value* Op, unsigned opNum,
244 Instruction& insertBefore)
245{
246 if (GetElementPtrInst* gep = getGlobalAddr(Op, insertBefore)) {
247 I.setOperand(opNum, gep); // replace global operand
248 return;
249 }
250
251 Constant* CV = dyn_cast<Constant>(Op);
252 if (CV == NULL)
253 return;
254
255 if (ConstantExpr* CE = dyn_cast<ConstantExpr>(CV))
256 { // load-time constant: factor it out so we optimize as best we can
257 Instruction* computeConst = DecomposeConstantExpr(CE, insertBefore);
258 I.setOperand(opNum, computeConst); // replace expr operand with result
259 }
260 else if (instrInfo.ConstantTypeMustBeLoaded(CV))
261 { // load address of constant into a register, then load the constant
262 GetElementPtrInst* gep = getGlobalAddr(getGlobalForConstant(CV),
263 insertBefore);
264 LoadInst* ldI = new LoadInst(gep, "loadConst", &insertBefore);
265 I.setOperand(opNum, ldI); // replace operand with copy in v.reg.
266 }
267 else if (instrInfo.ConstantMayNotFitInImmedField(CV, &I))
268 { // put the constant into a virtual register using a cast
269 CastInst* castI = new CastInst(CV, CV->getType(), "copyConst",
270 &insertBefore);
271 I.setOperand(opNum, castI); // replace operand with copy in v.reg.
272 }
273}
274
275// visitOperands() transforms individual operands of all instructions:
276// -- Load "large" int constants into a virtual register. What is large
277// depends on the type of instruction and on the target architecture.
278// -- For any constants that cannot be put in an immediate field,
279// load address into virtual register first, and then load the constant.
280//
281// firstOp and lastOp can be used to skip leading and trailing operands.
282// If lastOp is 0, it defaults to #operands or #incoming Phi values.
283//
284inline void
285PreSelection::visitOperands(Instruction &I, int firstOp, int lastOp)
286{
287 // For any instruction other than PHI, copies go just before the instr.
288 // For a PHI, operand copies must be before the terminator of the
289 // appropriate predecessor basic block. Remaining logic is simple
290 // so just handle PHIs and other instructions separately.
291 //
292 if (PHINode* phi = dyn_cast<PHINode>(&I))
293 {
294 if (lastOp == 0)
295 lastOp = phi->getNumIncomingValues();
296 for (unsigned i=firstOp, N=lastOp; i < N; ++i)
297 this->visitOneOperand(I, phi->getIncomingValue(i),
298 phi->getOperandNumForIncomingValue(i),
299 * phi->getIncomingBlock(i)->getTerminator());
300 }
301 else
302 {
303 if (lastOp == 0)
304 lastOp = I.getNumOperands();
305 for (unsigned i=firstOp, N=lastOp; i < N; ++i)
306 this->visitOneOperand(I, I.getOperand(i), i, I);
307 }
308}
309
310
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000311
312// Common work for *all* instructions. This needs to be called explicitly
313// by other visit<InstructionType> functions.
314inline void
315PreSelection::visitInstruction(Instruction &I)
316{
317 visitOperands(I); // Perform operand transformations
318}
319
320
321// GetElementPtr instructions: check if pointer is a global
322void
323PreSelection::visitGetElementPtrInst(GetElementPtrInst &I)
324{
325 // Check for a global and put its address into a register before this instr
Vikram S. Adved0451a92002-10-13 00:01:57 +0000326 if (GetElementPtrInst* gep = getGlobalAddr(I.getPointerOperand(), I))
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000327 I.setOperand(I.getPointerOperandIndex(), gep); // replace pointer operand
328
329 // Decompose multidimensional array references
330 DecomposeArrayRef(&I);
331
332 // Perform other transformations common to all instructions
333 visitInstruction(I);
334}
335
336
337// Load instructions: check if pointer is a global
338void
339PreSelection::visitLoadInst(LoadInst &I)
340{
341 // Check for a global and put its address into a register before this instr
Vikram S. Adved0451a92002-10-13 00:01:57 +0000342 if (GetElementPtrInst* gep = getGlobalAddr(I.getPointerOperand(), I))
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000343 I.setOperand(I.getPointerOperandIndex(), gep); // replace pointer operand
344
345 // Perform other transformations common to all instructions
346 visitInstruction(I);
347}
348
349
350// Store instructions: check if pointer is a global
351void
352PreSelection::visitStoreInst(StoreInst &I)
353{
354 // Check for a global and put its address into a register before this instr
Vikram S. Adved0451a92002-10-13 00:01:57 +0000355 if (GetElementPtrInst* gep = getGlobalAddr(I.getPointerOperand(), I))
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000356 I.setOperand(I.getPointerOperandIndex(), gep); // replace pointer operand
357
358 // Perform other transformations common to all instructions
359 visitInstruction(I);
360}
361
362
Vikram S. Adved0451a92002-10-13 00:01:57 +0000363// Cast instructions:
364// -- check if argument is a global
365// -- make multi-step casts explicit:
366// -- float/double to uint32_t:
367// If target does not have a float-to-unsigned instruction, we
368// need to convert to uint64_t and then to uint32_t, or we may
369// overflow the signed int representation for legal uint32_t
370// values. Expand this without checking target.
Vikram S. Advee9cb7352002-09-27 14:24:45 +0000371//
372void
373PreSelection::visitCastInst(CastInst &I)
374{
375 CastInst* castI = NULL;
376
377 // Check for a global and put its address into a register before this instr
Vikram S. Adved0451a92002-10-13 00:01:57 +0000378 if (GetElementPtrInst* gep = getGlobalAddr(I.getOperand(0), I))
379 {
380 I.setOperand(0, gep); // replace pointer operand
381 }
382 else if (I.getType() == Type::UIntTy &&
383 I.getOperand(0)->getType()->isFloatingPoint())
Vikram S. Advee9cb7352002-09-27 14:24:45 +0000384 { // insert a cast-fp-to-long before I, and then replace the operand of I
385 castI = new CastInst(I.getOperand(0), Type::LongTy, "fp2Long2Uint", &I);
386 I.setOperand(0, castI); // replace fp operand with long
387 }
388
389 // Perform other transformations common to all instructions
390 visitInstruction(I);
391 if (castI)
392 visitInstruction(*castI);
393}
394
Vikram S. Adve96358672003-05-31 07:34:57 +0000395void
396PreSelection::visitCallInst(CallInst &I)
397{
398 // Tell visitOperands to ignore the function name if this is a direct call.
399 visitOperands(I, (/*firstOp=*/ I.getCalledFunction()? 1 : 0));
400}
401
Vikram S. Advee9cb7352002-09-27 14:24:45 +0000402
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000403//===----------------------------------------------------------------------===//
404// createPreSelectionPass - Public entrypoint for pre-selection pass
405// and this file as a whole...
406//
407Pass*
408createPreSelectionPass(TargetMachine &T)
409{
410 return new PreSelection(T);
411}
412