blob: 205ecd3145ce59fa9dbb8a16bf659d5d62ab5621 [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/Constants.h"
Misha Brukmanbb8c8632003-10-22 04:51:36 +000020#include "llvm/DerivedTypes.h"
Vikram S. Adveabf055c2002-09-20 00:29:28 +000021#include "llvm/iMemory.h"
22#include "llvm/iPHINode.h"
23#include "llvm/iOther.h"
Misha Brukman523b30c2003-10-22 03:27:45 +000024#include "llvm/Module.h"
Vikram S. Adveabf055c2002-09-20 00:29:28 +000025#include "llvm/Pass.h"
Misha Brukman523b30c2003-10-22 03:27:45 +000026#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. Adved0451a92002-10-13 00:01:57 +000030#include <algorithm>
Vikram S. Adveabf055c2002-09-20 00:29:28 +000031
Brian Gaeked0fde302003-11-11 22:41:34 +000032namespace llvm {
33
Vikram S. Adveabf055c2002-09-20 00:29:28 +000034namespace {
Vikram S. Adveabf055c2002-09-20 00:29:28 +000035
36 //===--------------------------------------------------------------------===//
37 // PreSelection Pass - Specialize LLVM code for the current target machine.
Vikram S. Adveabf055c2002-09-20 00:29:28 +000038 //
Misha Brukmanfeed25f2003-11-07 17:31:22 +000039 class PreSelection : public FunctionPass, public InstVisitor<PreSelection> {
Vikram S. Advecf819452003-06-05 21:12:56 +000040 const TargetInstrInfo &instrInfo;
Vikram S. Adveabf055c2002-09-20 00:29:28 +000041
42 public:
Chris Lattnerdac91312003-10-21 14:49:19 +000043 PreSelection(const TargetMachine &T)
Misha Brukmanfeed25f2003-11-07 17:31:22 +000044 : instrInfo(T.getInstrInfo()) {}
Vikram S. Adveabf055c2002-09-20 00:29:28 +000045
Misha Brukmanfeed25f2003-11-07 17:31:22 +000046 // runOnFunction - apply this pass to each Function
47 bool runOnFunction(Function &F) {
48 visit(F);
Chris Lattnerdac91312003-10-21 14:49:19 +000049 return true;
Vikram S. Adveabf055c2002-09-20 00:29:28 +000050 }
51
52 // These methods do the actual work of specializing code
53 void visitInstruction(Instruction &I); // common work for every instr.
54 void visitGetElementPtrInst(GetElementPtrInst &I);
Vikram S. Adve96358672003-05-31 07:34:57 +000055 void visitCallInst(CallInst &I);
Chris Lattner4c62e792003-10-21 16:09:23 +000056 void visitPHINode(PHINode &PN);
Vikram S. Adveabf055c2002-09-20 00:29:28 +000057
58 // Helper functions for visiting operands of every instruction
Vikram S. Adve96358672003-05-31 07:34:57 +000059 //
60 // visitOperands() works on every operand in [firstOp, lastOp-1].
61 // If lastOp==0, lastOp defaults to #operands or #incoming Phi values.
62 //
63 // visitOneOperand() does all the work for one operand.
64 //
Chris Lattnerd48a1d72003-10-21 16:06:07 +000065 void visitOperands(Instruction &I, int firstOp=0);
Vikram S. Adve96358672003-05-31 07:34:57 +000066 void visitOneOperand(Instruction &I, Value* Op, unsigned opNum,
67 Instruction& insertBefore);
Vikram S. Adveabf055c2002-09-20 00:29:28 +000068 };
Chris Lattnerec8aae32003-04-24 18:35:51 +000069
Misha Brukmanfeed25f2003-11-07 17:31:22 +000070#if 0
Chris Lattnerec8aae32003-04-24 18:35:51 +000071 // Register the pass...
Misha Brukmanfeed25f2003-11-07 17:31:22 +000072 RegisterPass<PreSelection> X("preselect",
73 "Specialize LLVM code for a target machine"
74 createPreselectionPass);
75#endif
Brian Gaeked0fde302003-11-11 22:41:34 +000076
Vikram S. Adveabf055c2002-09-20 00:29:28 +000077} // end anonymous namespace
78
79
Vikram S. Adved0451a92002-10-13 00:01:57 +000080//------------------------------------------------------------------------------
81// Helper functions used by methods of class PreSelection
82//------------------------------------------------------------------------------
83
84
85// getGlobalAddr(): Put address of a global into a v. register.
Misha Brukman523b30c2003-10-22 03:27:45 +000086static GetElementPtrInst* getGlobalAddr(Value* ptr, Instruction& insertBefore) {
Vikram S. Adved0451a92002-10-13 00:01:57 +000087 if (isa<ConstantPointerRef>(ptr))
88 ptr = cast<ConstantPointerRef>(ptr)->getValue();
89
Chris Lattner2ab5e122003-06-04 01:24:40 +000090 return (isa<GlobalVariable>(ptr))
Vikram S. Adveabf055c2002-09-20 00:29:28 +000091 ? new GetElementPtrInst(ptr,
92 std::vector<Value*>(1, ConstantSInt::get(Type::LongTy, 0U)),
Vikram S. Adved0451a92002-10-13 00:01:57 +000093 "addrOfGlobal", &insertBefore)
Vikram S. Adveabf055c2002-09-20 00:29:28 +000094 : NULL;
95}
96
97
Vikram S. Adved0451a92002-10-13 00:01:57 +000098// Wrapper on Constant::classof to use in find_if :-(
Misha Brukman523b30c2003-10-22 03:27:45 +000099inline static bool nonConstant(const Use& U) {
Vikram S. Adved0451a92002-10-13 00:01:57 +0000100 return ! isa<Constant>(U);
101}
102
103
104static Instruction* DecomposeConstantExpr(ConstantExpr* CE,
105 Instruction& insertBefore)
106{
107 Value *getArg1, *getArg2;
108
109 switch(CE->getOpcode())
110 {
111 case Instruction::Cast:
112 getArg1 = CE->getOperand(0);
113 if (ConstantExpr* CEarg = dyn_cast<ConstantExpr>(getArg1))
114 getArg1 = DecomposeConstantExpr(CEarg, insertBefore);
115 return new CastInst(getArg1, CE->getType(), "constantCast",&insertBefore);
116
117 case Instruction::GetElementPtr:
Chris Lattnerdc476b82002-10-27 19:09:51 +0000118 assert(find_if(CE->op_begin()+1, CE->op_end(),nonConstant) == CE->op_end()
Vikram S. Adved0451a92002-10-13 00:01:57 +0000119 && "All indices in ConstantExpr getelementptr must be constant!");
Vikram S. Adved0451a92002-10-13 00:01:57 +0000120 getArg1 = CE->getOperand(0);
121 if (ConstantExpr* CEarg = dyn_cast<ConstantExpr>(getArg1))
122 getArg1 = DecomposeConstantExpr(CEarg, insertBefore);
123 else if (GetElementPtrInst* gep = getGlobalAddr(getArg1, insertBefore))
124 getArg1 = gep;
125 return new GetElementPtrInst(getArg1,
Chris Lattnerdc476b82002-10-27 19:09:51 +0000126 std::vector<Value*>(CE->op_begin()+1, CE->op_end()),
Vikram S. Adved0451a92002-10-13 00:01:57 +0000127 "constantGEP", &insertBefore);
128
129 default: // must be a binary operator
Chris Lattner0b16ae22002-10-13 19:39:16 +0000130 assert(CE->getOpcode() >= Instruction::BinaryOpsBegin &&
131 CE->getOpcode() < Instruction::BinaryOpsEnd &&
Vikram S. Adved0451a92002-10-13 00:01:57 +0000132 "Unrecognized opcode in ConstantExpr");
133 getArg1 = CE->getOperand(0);
134 if (ConstantExpr* CEarg = dyn_cast<ConstantExpr>(getArg1))
135 getArg1 = DecomposeConstantExpr(CEarg, insertBefore);
136 getArg2 = CE->getOperand(1);
137 if (ConstantExpr* CEarg = dyn_cast<ConstantExpr>(getArg2))
138 getArg2 = DecomposeConstantExpr(CEarg, insertBefore);
139 return BinaryOperator::create((Instruction::BinaryOps) CE->getOpcode(),
140 getArg1, getArg2,
141 "constantBinaryOp", &insertBefore);
142 }
143}
144
145
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000146//------------------------------------------------------------------------------
147// Instruction visitor methods to perform instruction-specific operations
148//------------------------------------------------------------------------------
Vikram S. Advecf819452003-06-05 21:12:56 +0000149inline void
150PreSelection::visitOneOperand(Instruction &I, Value* Op, unsigned opNum,
151 Instruction& insertBefore)
152{
Vikram S. Adve799ffee2003-07-02 01:23:15 +0000153 assert(&insertBefore != NULL && "Must have instruction to insert before.");
154
Vikram S. Advecf819452003-06-05 21:12:56 +0000155 if (GetElementPtrInst* gep = getGlobalAddr(Op, insertBefore)) {
156 I.setOperand(opNum, gep); // replace global operand
Vikram S. Adve799ffee2003-07-02 01:23:15 +0000157 return; // nothing more to do for this op.
Vikram S. Advecf819452003-06-05 21:12:56 +0000158 }
159
160 Constant* CV = dyn_cast<Constant>(Op);
161 if (CV == NULL)
162 return;
163
Misha Brukman523b30c2003-10-22 03:27:45 +0000164 if (ConstantExpr* CE = dyn_cast<ConstantExpr>(CV)) {
165 // load-time constant: factor it out so we optimize as best we can
166 Instruction* computeConst = DecomposeConstantExpr(CE, insertBefore);
167 I.setOperand(opNum, computeConst); // replace expr operand with result
168 } else if (instrInfo.ConstantTypeMustBeLoaded(CV)) {
169 // load address of constant into a register, then load the constant
Misha Brukmanfeed25f2003-11-07 17:31:22 +0000170 // this is now done during instruction selection
171 // the constant will live in the MachineConstantPool later on
Misha Brukman523b30c2003-10-22 03:27:45 +0000172 } else if (instrInfo.ConstantMayNotFitInImmedField(CV, &I)) {
173 // put the constant into a virtual register using a cast
174 CastInst* castI = new CastInst(CV, CV->getType(), "copyConst",
175 &insertBefore);
176 I.setOperand(opNum, castI); // replace operand with copy in v.reg.
177 }
Vikram S. Advecf819452003-06-05 21:12:56 +0000178}
179
180// visitOperands() transforms individual operands of all instructions:
181// -- Load "large" int constants into a virtual register. What is large
182// depends on the type of instruction and on the target architecture.
183// -- For any constants that cannot be put in an immediate field,
184// load address into virtual register first, and then load the constant.
185//
186// firstOp and lastOp can be used to skip leading and trailing operands.
187// If lastOp is 0, it defaults to #operands or #incoming Phi values.
188//
Chris Lattnerd48a1d72003-10-21 16:06:07 +0000189inline void PreSelection::visitOperands(Instruction &I, int firstOp) {
Vikram S. Advecf819452003-06-05 21:12:56 +0000190 // For any instruction other than PHI, copies go just before the instr.
Chris Lattner4c62e792003-10-21 16:09:23 +0000191 for (unsigned i = firstOp, e = I.getNumOperands(); i != e; ++i)
192 visitOneOperand(I, I.getOperand(i), i, I);
193}
194
195
196void PreSelection::visitPHINode(PHINode &PN) {
Vikram S. Advecf819452003-06-05 21:12:56 +0000197 // For a PHI, operand copies must be before the terminator of the
198 // appropriate predecessor basic block. Remaining logic is simple
199 // so just handle PHIs and other instructions separately.
200 //
Chris Lattner4c62e792003-10-21 16:09:23 +0000201 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
Chris Lattner08d702b2003-10-21 17:22:23 +0000202 visitOneOperand(PN, PN.getIncomingValue(i),
Chris Lattner4c62e792003-10-21 16:09:23 +0000203 PN.getOperandNumForIncomingValue(i),
204 *PN.getIncomingBlock(i)->getTerminator());
205 // do not call visitOperands!
Vikram S. Advecf819452003-06-05 21:12:56 +0000206}
207
208
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000209
210// Common work for *all* instructions. This needs to be called explicitly
211// by other visit<InstructionType> functions.
Misha Brukman523b30c2003-10-22 03:27:45 +0000212inline void PreSelection::visitInstruction(Instruction &I) {
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000213 visitOperands(I); // Perform operand transformations
214}
215
216
217// GetElementPtr instructions: check if pointer is a global
Misha Brukman523b30c2003-10-22 03:27:45 +0000218void PreSelection::visitGetElementPtrInst(GetElementPtrInst &I) {
Vikram S. Adve799ffee2003-07-02 01:23:15 +0000219 Instruction* curI = &I;
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000220
221 // Decompose multidimensional array references
Vikram S. Adve799ffee2003-07-02 01:23:15 +0000222 if (I.getNumIndices() >= 2) {
223 // DecomposeArrayRef() replaces I and deletes it, if successful,
224 // so remember predecessor in order to find the replacement instruction.
225 // Also remember the basic block in case there is no predecessor.
226 Instruction* prevI = I.getPrev();
227 BasicBlock* bb = I.getParent();
228 if (DecomposeArrayRef(&I))
229 // first instr. replacing I
230 curI = cast<GetElementPtrInst>(prevI? prevI->getNext() : &bb->front());
231 }
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000232
233 // Perform other transformations common to all instructions
Vikram S. Adve799ffee2003-07-02 01:23:15 +0000234 visitInstruction(*curI);
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000235}
236
Misha Brukman523b30c2003-10-22 03:27:45 +0000237void PreSelection::visitCallInst(CallInst &I) {
Vikram S. Adve96358672003-05-31 07:34:57 +0000238 // Tell visitOperands to ignore the function name if this is a direct call.
239 visitOperands(I, (/*firstOp=*/ I.getCalledFunction()? 1 : 0));
240}
241
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000242//===----------------------------------------------------------------------===//
243// createPreSelectionPass - Public entrypoint for pre-selection pass
244// and this file as a whole...
245//
Misha Brukmanfeed25f2003-11-07 17:31:22 +0000246FunctionPass* createPreSelectionPass(const TargetMachine &TM) {
247 return new PreSelection(TM);
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000248}
Brian Gaeked0fde302003-11-11 22:41:34 +0000249
250} // End llvm namespace