blob: 1995124826d066cffd229671e1bc4066c58e9162 [file] [log] [blame]
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -07001//===- subzero/src/IceTargetLowering.h - Lowering interface -----*- C++ -*-===//
2//
3// The Subzero Code Generator
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file declares the TargetLowering and LoweringContext
11// classes. TargetLowering is an abstract class used to drive the
12// translation/lowering process. LoweringContext maintains a
13// context for lowering each instruction, offering conveniences such
14// as iterating over non-deleted instructions.
15//
16//===----------------------------------------------------------------------===//
17
18#ifndef SUBZERO_SRC_ICETARGETLOWERING_H
19#define SUBZERO_SRC_ICETARGETLOWERING_H
20
21#include "IceDefs.h"
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070022#include "IceInst.h" // for the names of the Inst subtypes
Jim Stichnotha18cc9c2014-09-30 19:10:22 -070023#include "IceTypes.h"
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070024
25namespace Ice {
26
Jan Voung8acded02014-09-22 18:02:25 -070027class Assembler;
28
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070029// LoweringContext makes it easy to iterate through non-deleted
30// instructions in a node, and insert new (lowered) instructions at
31// the current point. Along with the instruction list container and
32// associated iterators, it holds the current node, which is needed
33// when inserting new instructions in order to track whether variables
34// are used as single-block or multi-block.
35class LoweringContext {
36public:
37 LoweringContext() : Node(NULL) {}
38 ~LoweringContext() {}
39 void init(CfgNode *Node);
40 Inst *getNextInst() const {
41 if (Next == End)
42 return NULL;
43 return *Next;
44 }
Jan Voungc820ddf2014-07-29 14:38:51 -070045 Inst *getNextInst(InstList::iterator &Iter) const {
Jan Vounge6e497d2014-07-30 10:06:03 -070046 advanceForward(Iter);
Jan Voungc820ddf2014-07-29 14:38:51 -070047 if (Iter == End)
48 return NULL;
49 return *Iter;
50 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070051 CfgNode *getNode() const { return Node; }
52 bool atEnd() const { return Cur == End; }
53 InstList::iterator getCur() const { return Cur; }
54 InstList::iterator getEnd() const { return End; }
55 void insert(Inst *Inst);
Jan Vounge6e497d2014-07-30 10:06:03 -070056 Inst *getLastInserted() const;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070057 void advanceCur() { Cur = Next; }
Jan Vounge6e497d2014-07-30 10:06:03 -070058 void advanceNext() { advanceForward(Next); }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070059 void setInsertPoint(const InstList::iterator &Position) { Next = Position; }
60
61private:
62 // Node is the argument to Inst::updateVars().
63 CfgNode *Node;
64 // Cur points to the current instruction being considered. It is
65 // guaranteed to point to a non-deleted instruction, or to be End.
66 InstList::iterator Cur;
67 // Next doubles as a pointer to the next valid instruction (if any),
68 // and the new-instruction insertion point. It is also updated for
69 // the caller in case the lowering consumes more than one high-level
70 // instruction. It is guaranteed to point to a non-deleted
71 // instruction after Cur, or to be End. TODO: Consider separating
72 // the notion of "next valid instruction" and "new instruction
73 // insertion point", to avoid confusion when previously-deleted
74 // instructions come between the two points.
75 InstList::iterator Next;
Jan Vounge6e497d2014-07-30 10:06:03 -070076 // Begin is a copy of Insts.begin(), used if iterators are moved backward.
77 InstList::iterator Begin;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070078 // End is a copy of Insts.end(), used if Next needs to be advanced.
79 InstList::iterator End;
80
Jan Voungc820ddf2014-07-29 14:38:51 -070081 void skipDeleted(InstList::iterator &I) const;
Jan Vounge6e497d2014-07-30 10:06:03 -070082 void advanceForward(InstList::iterator &I) const;
83 void advanceBackward(InstList::iterator &I) const;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070084 LoweringContext(const LoweringContext &) LLVM_DELETED_FUNCTION;
85 LoweringContext &operator=(const LoweringContext &) LLVM_DELETED_FUNCTION;
86};
87
88class TargetLowering {
89public:
90 static TargetLowering *createLowering(TargetArch Target, Cfg *Func);
Jan Voung8acded02014-09-22 18:02:25 -070091 static Assembler *createAssembler(TargetArch Target, Cfg *Func);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070092 void translate() {
93 switch (Ctx->getOptLevel()) {
94 case Opt_m1:
95 translateOm1();
96 break;
97 case Opt_0:
98 translateO0();
99 break;
100 case Opt_1:
101 translateO1();
102 break;
103 case Opt_2:
104 translateO2();
105 break;
106 default:
107 Func->setError("Target doesn't specify lowering steps.");
108 break;
109 }
110 }
111 virtual void translateOm1() {
112 Func->setError("Target doesn't specify Om1 lowering steps.");
113 }
114 virtual void translateO0() {
115 Func->setError("Target doesn't specify O0 lowering steps.");
116 }
117 virtual void translateO1() {
118 Func->setError("Target doesn't specify O1 lowering steps.");
119 }
120 virtual void translateO2() {
121 Func->setError("Target doesn't specify O2 lowering steps.");
122 }
123
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700124 // Tries to do address mode optimization on a single instruction.
125 void doAddressOpt();
Matt Walac3302742014-08-15 16:21:56 -0700126 // Randomly insert NOPs.
127 void doNopInsertion();
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700128 // Lowers a single instruction.
129 void lower();
Jim Stichnothff9c7062014-09-18 04:50:49 -0700130 // Tries to do branch optimization on a single instruction. Returns
131 // true if some optimization was done.
132 virtual bool doBranchOpt(Inst * /*I*/, const CfgNode * /*NextNode*/) {
133 return false;
134 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700135
136 // Returns a variable pre-colored to the specified physical
137 // register. This is generally used to get very direct access to
138 // the register such as in the prolog or epilog or for marking
139 // scratch registers as killed by a call.
140 virtual Variable *getPhysicalRegister(SizeT RegNum) = 0;
141 // Returns a printable name for the register.
142 virtual IceString getRegName(SizeT RegNum, Type Ty) const = 0;
143
144 virtual bool hasFramePointer() const { return false; }
145 virtual SizeT getFrameOrStackReg() const = 0;
Matt Walad4799f42014-08-14 14:24:12 -0700146 virtual size_t typeWidthInBytesOnStack(Type Ty) const = 0;
Jan Voungb17f61d2014-08-28 16:00:53 -0700147 virtual SizeT getBundleAlignLog2Bytes() const = 0;
148 virtual llvm::ArrayRef<uint8_t> getNonExecBundlePadding() const = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700149 bool hasComputedFrame() const { return HasComputedFrame; }
Matt Walac3302742014-08-15 16:21:56 -0700150 bool shouldDoNopInsertion() const;
Jan Voung44d53e12014-09-11 19:18:03 -0700151 // Returns true if this function calls a function that has the
152 // "returns twice" attribute.
153 bool callsReturnsTwice() const { return CallsReturnsTwice; }
154 void setCallsReturnsTwice(bool RetTwice) {
155 CallsReturnsTwice = RetTwice;
156 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700157 int32_t getStackAdjustment() const { return StackAdjustment; }
158 void updateStackAdjustment(int32_t Offset) { StackAdjustment += Offset; }
159 void resetStackAdjustment() { StackAdjustment = 0; }
160 LoweringContext &getContext() { return Context; }
161
162 enum RegSet {
163 RegSet_None = 0,
164 RegSet_CallerSave = 1 << 0,
165 RegSet_CalleeSave = 1 << 1,
166 RegSet_StackPointer = 1 << 2,
167 RegSet_FramePointer = 1 << 3,
168 RegSet_All = ~RegSet_None
169 };
170 typedef uint32_t RegSetMask;
171
172 virtual llvm::SmallBitVector getRegisterSet(RegSetMask Include,
173 RegSetMask Exclude) const = 0;
174 virtual const llvm::SmallBitVector &getRegisterSetForType(Type Ty) const = 0;
175 void regAlloc();
176
Jim Stichnoth144cdce2014-09-22 16:02:59 -0700177 virtual void emitVariable(const Variable *Var) const = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700178
Matt Wala45a06232014-07-09 16:33:22 -0700179 // Performs target-specific argument lowering.
180 virtual void lowerArguments() = 0;
181
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700182 virtual void addProlog(CfgNode *Node) = 0;
183 virtual void addEpilog(CfgNode *Node) = 0;
184
Jim Stichnothf61d5b22014-05-23 13:31:24 -0700185 virtual void emitConstants() const = 0;
186
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700187 virtual ~TargetLowering() {}
188
189protected:
190 TargetLowering(Cfg *Func)
191 : Func(Func), Ctx(Func->getContext()), HasComputedFrame(false),
Jan Voung44d53e12014-09-11 19:18:03 -0700192 CallsReturnsTwice(false), StackAdjustment(0) {}
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700193 virtual void lowerAlloca(const InstAlloca *Inst) = 0;
194 virtual void lowerArithmetic(const InstArithmetic *Inst) = 0;
195 virtual void lowerAssign(const InstAssign *Inst) = 0;
196 virtual void lowerBr(const InstBr *Inst) = 0;
197 virtual void lowerCall(const InstCall *Inst) = 0;
198 virtual void lowerCast(const InstCast *Inst) = 0;
199 virtual void lowerFcmp(const InstFcmp *Inst) = 0;
Matt Wala49889232014-07-18 12:45:09 -0700200 virtual void lowerExtractElement(const InstExtractElement *Inst) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700201 virtual void lowerIcmp(const InstIcmp *Inst) = 0;
Matt Wala49889232014-07-18 12:45:09 -0700202 virtual void lowerInsertElement(const InstInsertElement *Inst) = 0;
Jan Voung3bd9f1a2014-06-18 10:50:57 -0700203 virtual void lowerIntrinsicCall(const InstIntrinsicCall *Inst) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700204 virtual void lowerLoad(const InstLoad *Inst) = 0;
205 virtual void lowerPhi(const InstPhi *Inst) = 0;
206 virtual void lowerRet(const InstRet *Inst) = 0;
207 virtual void lowerSelect(const InstSelect *Inst) = 0;
208 virtual void lowerStore(const InstStore *Inst) = 0;
209 virtual void lowerSwitch(const InstSwitch *Inst) = 0;
210 virtual void lowerUnreachable(const InstUnreachable *Inst) = 0;
211
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700212 virtual void doAddressOptLoad() {}
213 virtual void doAddressOptStore() {}
Matt Walac3302742014-08-15 16:21:56 -0700214 virtual void randomlyInsertNop(float Probability) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700215 // This gives the target an opportunity to post-process the lowered
216 // expansion before returning. The primary intention is to do some
217 // Register Manager activity as necessary, specifically to eagerly
218 // allocate registers based on affinity and other factors. The
219 // simplest lowering does nothing here and leaves it all to a
220 // subsequent global register allocation pass.
221 virtual void postLower() {}
222
223 Cfg *Func;
224 GlobalContext *Ctx;
225 bool HasComputedFrame;
Jan Voung44d53e12014-09-11 19:18:03 -0700226 bool CallsReturnsTwice;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700227 // StackAdjustment keeps track of the current stack offset from its
228 // natural location, as arguments are pushed for a function call.
229 int32_t StackAdjustment;
230 LoweringContext Context;
231
232private:
233 TargetLowering(const TargetLowering &) LLVM_DELETED_FUNCTION;
234 TargetLowering &operator=(const TargetLowering &) LLVM_DELETED_FUNCTION;
235};
236
Jim Stichnothde4ca712014-06-29 08:13:48 -0700237// TargetGlobalInitLowering is used for "lowering" global
238// initializers. It is separated out from TargetLowering because it
239// does not require a Cfg.
240class TargetGlobalInitLowering {
241public:
242 static TargetGlobalInitLowering *createLowering(TargetArch Target,
243 GlobalContext *Ctx);
Jan Voung839c4ce2014-07-28 15:19:43 -0700244 virtual ~TargetGlobalInitLowering();
245
Jim Stichnothde4ca712014-06-29 08:13:48 -0700246 // TODO: Allow relocations to be represented as part of the Data.
247 virtual void lower(const IceString &Name, SizeT Align, bool IsInternal,
248 bool IsConst, bool IsZeroInitializer, SizeT Size,
249 const char *Data, bool DisableTranslation) = 0;
250
251protected:
252 TargetGlobalInitLowering(GlobalContext *Ctx) : Ctx(Ctx) {}
253 GlobalContext *Ctx;
254
255private:
256 TargetGlobalInitLowering(const TargetGlobalInitLowering &)
257 LLVM_DELETED_FUNCTION;
258 TargetGlobalInitLowering &
259 operator=(const TargetGlobalInitLowering &) LLVM_DELETED_FUNCTION;
260};
261
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700262} // end of namespace Ice
263
264#endif // SUBZERO_SRC_ICETARGETLOWERING_H