blob: 2dda5c5126311ece3edf2416a43aded20ebdeee9 [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
27// LoweringContext makes it easy to iterate through non-deleted
28// instructions in a node, and insert new (lowered) instructions at
29// the current point. Along with the instruction list container and
30// associated iterators, it holds the current node, which is needed
31// when inserting new instructions in order to track whether variables
32// are used as single-block or multi-block.
33class LoweringContext {
Jim Stichnoth7b451a92014-10-15 14:39:23 -070034 LoweringContext(const LoweringContext &) = delete;
35 LoweringContext &operator=(const LoweringContext &) = delete;
36
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070037public:
Jim Stichnothae953202014-12-20 06:17:49 -080038 LoweringContext() : Node(nullptr), LastInserted(nullptr) {}
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070039 ~LoweringContext() {}
40 void init(CfgNode *Node);
41 Inst *getNextInst() const {
42 if (Next == End)
Jim Stichnothae953202014-12-20 06:17:49 -080043 return nullptr;
Jim Stichnoth607e9f02014-11-06 13:32:05 -080044 return Next;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070045 }
Jan Voungc820ddf2014-07-29 14:38:51 -070046 Inst *getNextInst(InstList::iterator &Iter) const {
Jan Vounge6e497d2014-07-30 10:06:03 -070047 advanceForward(Iter);
Jan Voungc820ddf2014-07-29 14:38:51 -070048 if (Iter == End)
Jim Stichnothae953202014-12-20 06:17:49 -080049 return nullptr;
Jim Stichnoth607e9f02014-11-06 13:32:05 -080050 return Iter;
Jan Voungc820ddf2014-07-29 14:38:51 -070051 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070052 CfgNode *getNode() const { return Node; }
53 bool atEnd() const { return Cur == End; }
54 InstList::iterator getCur() const { return Cur; }
Jim Stichnoth5d2fa0c2014-12-01 09:30:55 -080055 InstList::iterator getNext() const { return Next; }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070056 InstList::iterator getEnd() const { return End; }
57 void insert(Inst *Inst);
Jan Vounge6e497d2014-07-30 10:06:03 -070058 Inst *getLastInserted() const;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070059 void advanceCur() { Cur = Next; }
Jan Vounge6e497d2014-07-30 10:06:03 -070060 void advanceNext() { advanceForward(Next); }
Jim Stichnoth336f6c42014-10-30 15:01:31 -070061 void rewind();
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070062 void setInsertPoint(const InstList::iterator &Position) { Next = Position; }
63
64private:
65 // Node is the argument to Inst::updateVars().
66 CfgNode *Node;
Jim Stichnoth98712a32014-10-24 10:59:02 -070067 Inst *LastInserted;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070068 // Cur points to the current instruction being considered. It is
69 // guaranteed to point to a non-deleted instruction, or to be End.
70 InstList::iterator Cur;
71 // Next doubles as a pointer to the next valid instruction (if any),
72 // and the new-instruction insertion point. It is also updated for
73 // the caller in case the lowering consumes more than one high-level
74 // instruction. It is guaranteed to point to a non-deleted
75 // instruction after Cur, or to be End. TODO: Consider separating
76 // the notion of "next valid instruction" and "new instruction
77 // insertion point", to avoid confusion when previously-deleted
78 // instructions come between the two points.
79 InstList::iterator Next;
Jan Vounge6e497d2014-07-30 10:06:03 -070080 // Begin is a copy of Insts.begin(), used if iterators are moved backward.
81 InstList::iterator Begin;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070082 // End is a copy of Insts.end(), used if Next needs to be advanced.
83 InstList::iterator End;
84
Jan Voungc820ddf2014-07-29 14:38:51 -070085 void skipDeleted(InstList::iterator &I) const;
Jan Vounge6e497d2014-07-30 10:06:03 -070086 void advanceForward(InstList::iterator &I) const;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070087};
88
89class TargetLowering {
Jim Stichnoth7b451a92014-10-15 14:39:23 -070090 TargetLowering(const TargetLowering &) = delete;
91 TargetLowering &operator=(const TargetLowering &) = delete;
92
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070093public:
94 static TargetLowering *createLowering(TargetArch Target, Cfg *Func);
Jan Voungec270732015-01-12 17:00:22 -080095 static std::unique_ptr<Assembler> createAssembler(TargetArch Target,
96 Cfg *Func);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070097 void translate() {
98 switch (Ctx->getOptLevel()) {
99 case Opt_m1:
100 translateOm1();
101 break;
102 case Opt_0:
103 translateO0();
104 break;
105 case Opt_1:
106 translateO1();
107 break;
108 case Opt_2:
109 translateO2();
110 break;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700111 }
112 }
113 virtual void translateOm1() {
114 Func->setError("Target doesn't specify Om1 lowering steps.");
115 }
116 virtual void translateO0() {
117 Func->setError("Target doesn't specify O0 lowering steps.");
118 }
119 virtual void translateO1() {
120 Func->setError("Target doesn't specify O1 lowering steps.");
121 }
122 virtual void translateO2() {
123 Func->setError("Target doesn't specify O2 lowering steps.");
124 }
125
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700126 // Tries to do address mode optimization on a single instruction.
127 void doAddressOpt();
Matt Walac3302742014-08-15 16:21:56 -0700128 // Randomly insert NOPs.
129 void doNopInsertion();
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700130 // Lowers a single non-Phi instruction.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700131 void lower();
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700132 // Does preliminary lowering of the set of Phi instructions in the
133 // current node. The main intention is to do what's needed to keep
134 // the unlowered Phi instructions consistent with the lowered
135 // non-Phi instructions, e.g. to lower 64-bit operands on a 32-bit
136 // target.
137 virtual void prelowerPhis() {}
138 // Lowers a list of "parallel" assignment instructions representing
139 // a topological sort of the Phi instructions.
140 virtual void lowerPhiAssignments(CfgNode *Node,
141 const AssignList &Assignments) = 0;
Jim Stichnothff9c7062014-09-18 04:50:49 -0700142 // Tries to do branch optimization on a single instruction. Returns
143 // true if some optimization was done.
144 virtual bool doBranchOpt(Inst * /*I*/, const CfgNode * /*NextNode*/) {
145 return false;
146 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700147
Jim Stichnoth3d44fe82014-11-01 10:10:18 -0700148 virtual SizeT getNumRegisters() const = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700149 // Returns a variable pre-colored to the specified physical
150 // register. This is generally used to get very direct access to
151 // the register such as in the prolog or epilog or for marking
Jim Stichnoth98712a32014-10-24 10:59:02 -0700152 // scratch registers as killed by a call. If a Type is not
153 // provided, a target-specific default type is used.
154 virtual Variable *getPhysicalRegister(SizeT RegNum,
155 Type Ty = IceType_void) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700156 // Returns a printable name for the register.
157 virtual IceString getRegName(SizeT RegNum, Type Ty) const = 0;
158
159 virtual bool hasFramePointer() const { return false; }
160 virtual SizeT getFrameOrStackReg() const = 0;
Matt Walad4799f42014-08-14 14:24:12 -0700161 virtual size_t typeWidthInBytesOnStack(Type Ty) const = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700162 bool hasComputedFrame() const { return HasComputedFrame; }
Matt Walac3302742014-08-15 16:21:56 -0700163 bool shouldDoNopInsertion() const;
Jan Voung44d53e12014-09-11 19:18:03 -0700164 // Returns true if this function calls a function that has the
165 // "returns twice" attribute.
166 bool callsReturnsTwice() const { return CallsReturnsTwice; }
Jim Stichnothdd842db2015-01-27 12:53:53 -0800167 void setCallsReturnsTwice(bool RetTwice) { CallsReturnsTwice = RetTwice; }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700168 int32_t getStackAdjustment() const { return StackAdjustment; }
169 void updateStackAdjustment(int32_t Offset) { StackAdjustment += Offset; }
170 void resetStackAdjustment() { StackAdjustment = 0; }
171 LoweringContext &getContext() { return Context; }
172
173 enum RegSet {
174 RegSet_None = 0,
175 RegSet_CallerSave = 1 << 0,
176 RegSet_CalleeSave = 1 << 1,
177 RegSet_StackPointer = 1 << 2,
178 RegSet_FramePointer = 1 << 3,
179 RegSet_All = ~RegSet_None
180 };
181 typedef uint32_t RegSetMask;
182
183 virtual llvm::SmallBitVector getRegisterSet(RegSetMask Include,
184 RegSetMask Exclude) const = 0;
185 virtual const llvm::SmallBitVector &getRegisterSetForType(Type Ty) const = 0;
Jim Stichnoth70d0a052014-11-14 15:53:46 -0800186 void regAlloc(RegAllocKind Kind);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700187
Jim Stichnothe6d24782014-12-19 05:42:24 -0800188 virtual void makeRandomRegisterPermutation(
189 llvm::SmallVectorImpl<int32_t> &Permutation,
190 const llvm::SmallBitVector &ExcludeRegisters) const = 0;
191
Jim Stichnoth144cdce2014-09-22 16:02:59 -0700192 virtual void emitVariable(const Variable *Var) const = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700193
Matt Wala45a06232014-07-09 16:33:22 -0700194 // Performs target-specific argument lowering.
195 virtual void lowerArguments() = 0;
196
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700197 virtual void addProlog(CfgNode *Node) = 0;
198 virtual void addEpilog(CfgNode *Node) = 0;
199
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700200 virtual ~TargetLowering() {}
201
202protected:
Jim Stichnothe6d24782014-12-19 05:42:24 -0800203 TargetLowering(Cfg *Func);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700204 virtual void lowerAlloca(const InstAlloca *Inst) = 0;
205 virtual void lowerArithmetic(const InstArithmetic *Inst) = 0;
206 virtual void lowerAssign(const InstAssign *Inst) = 0;
207 virtual void lowerBr(const InstBr *Inst) = 0;
208 virtual void lowerCall(const InstCall *Inst) = 0;
209 virtual void lowerCast(const InstCast *Inst) = 0;
210 virtual void lowerFcmp(const InstFcmp *Inst) = 0;
Matt Wala49889232014-07-18 12:45:09 -0700211 virtual void lowerExtractElement(const InstExtractElement *Inst) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700212 virtual void lowerIcmp(const InstIcmp *Inst) = 0;
Matt Wala49889232014-07-18 12:45:09 -0700213 virtual void lowerInsertElement(const InstInsertElement *Inst) = 0;
Jan Voung3bd9f1a2014-06-18 10:50:57 -0700214 virtual void lowerIntrinsicCall(const InstIntrinsicCall *Inst) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700215 virtual void lowerLoad(const InstLoad *Inst) = 0;
216 virtual void lowerPhi(const InstPhi *Inst) = 0;
217 virtual void lowerRet(const InstRet *Inst) = 0;
218 virtual void lowerSelect(const InstSelect *Inst) = 0;
219 virtual void lowerStore(const InstStore *Inst) = 0;
220 virtual void lowerSwitch(const InstSwitch *Inst) = 0;
221 virtual void lowerUnreachable(const InstUnreachable *Inst) = 0;
222
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700223 virtual void doAddressOptLoad() {}
224 virtual void doAddressOptStore() {}
Matt Walac3302742014-08-15 16:21:56 -0700225 virtual void randomlyInsertNop(float Probability) = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700226 // This gives the target an opportunity to post-process the lowered
Jim Stichnoth70d0a052014-11-14 15:53:46 -0800227 // expansion before returning.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700228 virtual void postLower() {}
229
230 Cfg *Func;
231 GlobalContext *Ctx;
Jim Stichnothe6d24782014-12-19 05:42:24 -0800232 const bool RandomizeRegisterAllocation;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700233 bool HasComputedFrame;
Jan Voung44d53e12014-09-11 19:18:03 -0700234 bool CallsReturnsTwice;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700235 // StackAdjustment keeps track of the current stack offset from its
236 // natural location, as arguments are pushed for a function call.
237 int32_t StackAdjustment;
238 LoweringContext Context;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700239};
240
Jan Voung72984d82015-01-29 14:42:38 -0800241// TargetDataLowering is used for "lowering" data including initializers
242// for global variables, and the internal constant pools. It is separated
243// out from TargetLowering because it does not require a Cfg.
244class TargetDataLowering {
245 TargetDataLowering() = delete;
246 TargetDataLowering(const TargetDataLowering &) = delete;
247 TargetDataLowering &operator=(const TargetDataLowering &) = delete;
Jim Stichnoth7b451a92014-10-15 14:39:23 -0700248
Jim Stichnothde4ca712014-06-29 08:13:48 -0700249public:
Jan Voung72984d82015-01-29 14:42:38 -0800250 static TargetDataLowering *createLowering(GlobalContext *Ctx);
251 virtual ~TargetDataLowering();
Jan Voung839c4ce2014-07-28 15:19:43 -0700252
Jan Voung72984d82015-01-29 14:42:38 -0800253 virtual void lowerGlobal(const VariableDeclaration &Var) const = 0;
254 virtual void lowerGlobalsELF(const VariableDeclarationList &Vars) const = 0;
Jim Stichnothfa4efea2015-01-27 05:06:03 -0800255 virtual void lowerConstants(GlobalContext *Ctx) const = 0;
Jim Stichnothde4ca712014-06-29 08:13:48 -0700256
257protected:
Jan Voung72984d82015-01-29 14:42:38 -0800258 TargetDataLowering(GlobalContext *Ctx) : Ctx(Ctx) {}
Jim Stichnothde4ca712014-06-29 08:13:48 -0700259 GlobalContext *Ctx;
Jim Stichnothde4ca712014-06-29 08:13:48 -0700260};
261
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700262} // end of namespace Ice
263
264#endif // SUBZERO_SRC_ICETARGETLOWERING_H