blob: 00cfd1295e6a5f9ce04b41aa88183db65197ea00 [file] [log] [blame]
Brian Gaeke3ca4fcc2004-04-25 07:04:49 +00001//===- SparcV9PreSelection.cpp - Specialize LLVM code for SparcV9 ---------===//
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//
Brian Gaeke3ca4fcc2004-04-25 07:04:49 +000010// This file defines the PreSelection pass which specializes LLVM code for
11// the SparcV9 instruction selector, while remaining in legal portable LLVM
12// form and preserving type information and type safety. This is meant to enable
13// dataflow optimizations on SparcV9-specific operations such as accesses to
Vikram S. Adveabf055c2002-09-20 00:29:28 +000014// constants, globals, and array indexing.
15//
16//===----------------------------------------------------------------------===//
17
Brian Gaekee3d68072004-02-25 18:44:15 +000018#include "SparcV9Internals.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"
Chris Lattner97220b72004-04-04 20:44:05 +000027#include "llvm/Support/GetElementPtrTypeIterator.h"
Misha Brukman523b30c2003-10-22 03:27:45 +000028#include "llvm/Target/TargetInstrInfo.h"
29#include "llvm/Target/TargetMachine.h"
30#include "llvm/Transforms/Scalar.h"
Vikram S. Adved0451a92002-10-13 00:01:57 +000031#include <algorithm>
Chris Lattner97220b72004-04-04 20:44:05 +000032using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000033
Vikram S. Adveabf055c2002-09-20 00:29:28 +000034namespace {
Vikram S. Adveabf055c2002-09-20 00:29:28 +000035
36 //===--------------------------------------------------------------------===//
Brian Gaeke3ca4fcc2004-04-25 07:04:49 +000037 // PreSelection Pass - Specialize LLVM code for the SparcV9 instr. selector.
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)
Chris Lattnerd029cd22004-06-02 05:55:25 +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 }
Brian Gaekec46f8a42004-03-11 19:23:15 +000051 const char *getPassName() const { return "SparcV9 Instr. Pre-selection"; }
Vikram S. Adveabf055c2002-09-20 00:29:28 +000052
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. Adve96358672003-05-31 07:34:57 +000056 void visitCallInst(CallInst &I);
Chris Lattner4c62e792003-10-21 16:09:23 +000057 void visitPHINode(PHINode &PN);
Vikram S. Adveabf055c2002-09-20 00:29:28 +000058
59 // Helper functions for visiting operands of every instruction
Vikram S. Adve96358672003-05-31 07:34:57 +000060 //
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 Lattnerd48a1d72003-10-21 16:06:07 +000066 void visitOperands(Instruction &I, int firstOp=0);
Vikram S. Adve96358672003-05-31 07:34:57 +000067 void visitOneOperand(Instruction &I, Value* Op, unsigned opNum,
68 Instruction& insertBefore);
Vikram S. Adveabf055c2002-09-20 00:29:28 +000069 };
Chris Lattnerec8aae32003-04-24 18:35:51 +000070
Misha Brukmanfeed25f2003-11-07 17:31:22 +000071#if 0
Chris Lattnerec8aae32003-04-24 18:35:51 +000072 // Register the pass...
Misha Brukmanfeed25f2003-11-07 17:31:22 +000073 RegisterPass<PreSelection> X("preselect",
74 "Specialize LLVM code for a target machine"
75 createPreselectionPass);
76#endif
Brian Gaeked0fde302003-11-11 22:41:34 +000077
Vikram S. Adveabf055c2002-09-20 00:29:28 +000078} // end anonymous namespace
79
80
Vikram S. Adved0451a92002-10-13 00:01:57 +000081//------------------------------------------------------------------------------
82// Helper functions used by methods of class PreSelection
83//------------------------------------------------------------------------------
84
85
86// getGlobalAddr(): Put address of a global into a v. register.
Misha Brukman523b30c2003-10-22 03:27:45 +000087static GetElementPtrInst* getGlobalAddr(Value* ptr, Instruction& insertBefore) {
Vikram S. Adved0451a92002-10-13 00:01:57 +000088 if (isa<ConstantPointerRef>(ptr))
89 ptr = cast<ConstantPointerRef>(ptr)->getValue();
90
Chris Lattner2ab5e122003-06-04 01:24:40 +000091 return (isa<GlobalVariable>(ptr))
Vikram S. Adveabf055c2002-09-20 00:29:28 +000092 ? new GetElementPtrInst(ptr,
93 std::vector<Value*>(1, ConstantSInt::get(Type::LongTy, 0U)),
Brian Gaekea846b722004-06-23 21:41:32 +000094 "addrOfGlobal:" + ptr->getName(), &insertBefore)
Vikram S. Adveabf055c2002-09-20 00:29:28 +000095 : NULL;
96}
97
Misha Brukmanc3402cd2003-12-17 22:06:08 +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
Vikram S. Adved0451a92002-10-13 00:01:57 +0000103static Instruction* DecomposeConstantExpr(ConstantExpr* CE,
104 Instruction& insertBefore)
105{
106 Value *getArg1, *getArg2;
Brian Gaeke142e22a2004-04-02 17:52:29 +0000107
Vikram S. Adved0451a92002-10-13 00:01:57 +0000108 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 Lattnerdc476b82002-10-27 19:09:51 +0000117 assert(find_if(CE->op_begin()+1, CE->op_end(),nonConstant) == CE->op_end()
Vikram S. Adved0451a92002-10-13 00:01:57 +0000118 && "All indices in ConstantExpr getelementptr must be constant!");
Vikram S. Adved0451a92002-10-13 00:01:57 +0000119 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 Lattnerdc476b82002-10-27 19:09:51 +0000125 std::vector<Value*>(CE->op_begin()+1, CE->op_end()),
Brian Gaekea846b722004-06-23 21:41:32 +0000126 "constantGEP:" + getArg1->getName(), &insertBefore);
Brian Gaeke142e22a2004-04-02 17:52:29 +0000127
128 case Instruction::Select: {
129 Value *C, *S1, *S2;
130 C = CE->getOperand (0);
131 if (ConstantExpr* CEarg = dyn_cast<ConstantExpr> (C))
132 C = DecomposeConstantExpr (CEarg, insertBefore);
133 S1 = CE->getOperand (1);
134 if (ConstantExpr* CEarg = dyn_cast<ConstantExpr> (S1))
135 S1 = DecomposeConstantExpr (CEarg, insertBefore);
136 S2 = CE->getOperand (2);
137 if (ConstantExpr* CEarg = dyn_cast<ConstantExpr> (S2))
138 S2 = DecomposeConstantExpr (CEarg, insertBefore);
Brian Gaeke58fe7692004-04-07 18:31:47 +0000139 return new SelectInst (C, S1, S2, "constantSelect", &insertBefore);
Brian Gaeke142e22a2004-04-02 17:52:29 +0000140 }
141
Vikram S. Adved0451a92002-10-13 00:01:57 +0000142 default: // must be a binary operator
Chris Lattner0b16ae22002-10-13 19:39:16 +0000143 assert(CE->getOpcode() >= Instruction::BinaryOpsBegin &&
144 CE->getOpcode() < Instruction::BinaryOpsEnd &&
Brian Gaeke142e22a2004-04-02 17:52:29 +0000145 "Unhandled opcode in ConstantExpr");
Vikram S. Adved0451a92002-10-13 00:01:57 +0000146 getArg1 = CE->getOperand(0);
147 if (ConstantExpr* CEarg = dyn_cast<ConstantExpr>(getArg1))
148 getArg1 = DecomposeConstantExpr(CEarg, insertBefore);
149 getArg2 = CE->getOperand(1);
150 if (ConstantExpr* CEarg = dyn_cast<ConstantExpr>(getArg2))
151 getArg2 = DecomposeConstantExpr(CEarg, insertBefore);
152 return BinaryOperator::create((Instruction::BinaryOps) CE->getOpcode(),
153 getArg1, getArg2,
154 "constantBinaryOp", &insertBefore);
155 }
156}
157
158
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000159//------------------------------------------------------------------------------
160// Instruction visitor methods to perform instruction-specific operations
161//------------------------------------------------------------------------------
Vikram S. Advecf819452003-06-05 21:12:56 +0000162inline void
163PreSelection::visitOneOperand(Instruction &I, Value* Op, unsigned opNum,
164 Instruction& insertBefore)
165{
Vikram S. Adve799ffee2003-07-02 01:23:15 +0000166 assert(&insertBefore != NULL && "Must have instruction to insert before.");
167
Vikram S. Advecf819452003-06-05 21:12:56 +0000168 if (GetElementPtrInst* gep = getGlobalAddr(Op, insertBefore)) {
169 I.setOperand(opNum, gep); // replace global operand
Vikram S. Adve799ffee2003-07-02 01:23:15 +0000170 return; // nothing more to do for this op.
Vikram S. Advecf819452003-06-05 21:12:56 +0000171 }
172
173 Constant* CV = dyn_cast<Constant>(Op);
174 if (CV == NULL)
175 return;
176
Misha Brukman523b30c2003-10-22 03:27:45 +0000177 if (ConstantExpr* CE = dyn_cast<ConstantExpr>(CV)) {
178 // load-time constant: factor it out so we optimize as best we can
179 Instruction* computeConst = DecomposeConstantExpr(CE, insertBefore);
180 I.setOperand(opNum, computeConst); // replace expr operand with result
181 } else if (instrInfo.ConstantTypeMustBeLoaded(CV)) {
182 // load address of constant into a register, then load the constant
Misha Brukmanfeed25f2003-11-07 17:31:22 +0000183 // this is now done during instruction selection
184 // the constant will live in the MachineConstantPool later on
Misha Brukman523b30c2003-10-22 03:27:45 +0000185 } else if (instrInfo.ConstantMayNotFitInImmedField(CV, &I)) {
186 // put the constant into a virtual register using a cast
187 CastInst* castI = new CastInst(CV, CV->getType(), "copyConst",
188 &insertBefore);
189 I.setOperand(opNum, castI); // replace operand with copy in v.reg.
190 }
Vikram S. Advecf819452003-06-05 21:12:56 +0000191}
192
Misha Brukmanc3402cd2003-12-17 22:06:08 +0000193/// visitOperands - transform individual operands of all instructions:
194/// -- Load "large" int constants into a virtual register. What is large
195/// depends on the type of instruction and on the target architecture.
196/// -- For any constants that cannot be put in an immediate field,
197/// load address into virtual register first, and then load the constant.
198///
199/// firstOp and lastOp can be used to skip leading and trailing operands.
200/// If lastOp is 0, it defaults to #operands or #incoming Phi values.
201///
Chris Lattnerd48a1d72003-10-21 16:06:07 +0000202inline void PreSelection::visitOperands(Instruction &I, int firstOp) {
Vikram S. Advecf819452003-06-05 21:12:56 +0000203 // For any instruction other than PHI, copies go just before the instr.
Chris Lattner4c62e792003-10-21 16:09:23 +0000204 for (unsigned i = firstOp, e = I.getNumOperands(); i != e; ++i)
205 visitOneOperand(I, I.getOperand(i), i, I);
206}
207
208
209void PreSelection::visitPHINode(PHINode &PN) {
Vikram S. Advecf819452003-06-05 21:12:56 +0000210 // For a PHI, operand copies must be before the terminator of the
211 // appropriate predecessor basic block. Remaining logic is simple
212 // so just handle PHIs and other instructions separately.
213 //
Chris Lattner4c62e792003-10-21 16:09:23 +0000214 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
Chris Lattner08d702b2003-10-21 17:22:23 +0000215 visitOneOperand(PN, PN.getIncomingValue(i),
Chris Lattner4c62e792003-10-21 16:09:23 +0000216 PN.getOperandNumForIncomingValue(i),
217 *PN.getIncomingBlock(i)->getTerminator());
218 // do not call visitOperands!
Vikram S. Advecf819452003-06-05 21:12:56 +0000219}
220
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000221// Common work for *all* instructions. This needs to be called explicitly
222// by other visit<InstructionType> functions.
Misha Brukman523b30c2003-10-22 03:27:45 +0000223inline void PreSelection::visitInstruction(Instruction &I) {
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000224 visitOperands(I); // Perform operand transformations
225}
226
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000227// GetElementPtr instructions: check if pointer is a global
Misha Brukman523b30c2003-10-22 03:27:45 +0000228void PreSelection::visitGetElementPtrInst(GetElementPtrInst &I) {
Vikram S. Adve799ffee2003-07-02 01:23:15 +0000229 Instruction* curI = &I;
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000230
Chris Lattner97220b72004-04-04 20:44:05 +0000231 // The Sparc backend doesn't handle array indexes that are not long types, so
232 // insert a cast from whatever it is to long, if the sequential type index is
233 // not a long already.
234 unsigned Idx = 1;
235 for (gep_type_iterator TI = gep_type_begin(I), E = gep_type_end(I); TI != E;
236 ++TI, ++Idx)
237 if (isa<SequentialType>(*TI) &&
238 I.getOperand(Idx)->getType() != Type::LongTy) {
239 Value *Op = I.getOperand(Idx);
240 if (Op->getType()->isUnsigned()) // Must sign extend!
241 Op = new CastInst(Op, Op->getType()->getSignedVersion(), "v9", &I);
242 if (Op->getType() != Type::LongTy)
243 Op = new CastInst(Op, Type::LongTy, "v9", &I);
244 I.setOperand(Idx, Op);
245 }
246
247
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000248 // 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
Misha Brukman523b30c2003-10-22 03:27:45 +0000264void PreSelection::visitCallInst(CallInst &I) {
Vikram S. Adve96358672003-05-31 07:34:57 +0000265 // Tell visitOperands to ignore the function name if this is a direct call.
266 visitOperands(I, (/*firstOp=*/ I.getCalledFunction()? 1 : 0));
267}
268
Misha Brukmanc3402cd2003-12-17 22:06:08 +0000269/// createPreSelectionPass - Public entry point for the PreSelection pass
270///
Chris Lattner97220b72004-04-04 20:44:05 +0000271FunctionPass* llvm::createPreSelectionPass(const TargetMachine &TM) {
Misha Brukmanfeed25f2003-11-07 17:31:22 +0000272 return new PreSelection(TM);
Vikram S. Adveabf055c2002-09-20 00:29:28 +0000273}