blob: 2c7adc83327631a6a46874470f67f7280bbe8d91 [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//===----------------------------------------------------------------------===//
Andrew Scull9612d322015-07-06 14:53:25 -07009///
10/// \file
Jim Stichnoth92a6e5b2015-12-02 16:52:44 -080011/// \brief Declares the TargetLowering, LoweringContext, and TargetDataLowering
12/// classes.
13///
14/// TargetLowering is an abstract class used to drive the translation/lowering
15/// process. LoweringContext maintains a context for lowering each instruction,
16/// offering conveniences such as iterating over non-deleted instructions.
17/// TargetDataLowering is an abstract class used to drive the lowering/emission
18/// of global initializers, external global declarations, and internal constant
19/// pools.
Andrew Scull9612d322015-07-06 14:53:25 -070020///
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070021//===----------------------------------------------------------------------===//
22
23#ifndef SUBZERO_SRC_ICETARGETLOWERING_H
24#define SUBZERO_SRC_ICETARGETLOWERING_H
25
26#include "IceDefs.h"
John Portoe82b5602016-02-24 15:58:55 -080027#include "IceBitVector.h"
28#include "IceCfgNode.h"
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070029#include "IceInst.h" // for the names of the Inst subtypes
Jan Voung76bb0be2015-05-14 09:26:19 -070030#include "IceOperand.h"
Jim Stichnotha18cc9c2014-09-30 19:10:22 -070031#include "IceTypes.h"
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070032
John Porto1d937a82015-12-17 06:19:34 -080033#include <utility>
34
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070035namespace Ice {
36
Karl Schimpfc5abdc12015-10-09 13:29:13 -070037// UnimplementedError is defined as a macro so that we can get actual line
38// numbers.
39#define UnimplementedError(Flags) \
40 do { \
41 if (!static_cast<const ClFlags &>(Flags).getSkipUnimplemented()) { \
42 /* Use llvm_unreachable instead of report_fatal_error, which gives \
43 better stack traces. */ \
44 llvm_unreachable("Not yet implemented"); \
45 abort(); \
46 } \
47 } while (0)
48
Jim Stichnoth91c773e2016-01-19 09:52:22 -080049// UnimplementedLoweringError is similar in style to UnimplementedError. Given
50// a TargetLowering object pointer and an Inst pointer, it adds appropriate
51// FakeDef and FakeUse instructions to try maintain liveness consistency.
52#define UnimplementedLoweringError(Target, Instr) \
53 do { \
Karl Schimpfd4699942016-04-02 09:55:31 -070054 if (getFlags().getSkipUnimplemented()) { \
Jim Stichnoth91c773e2016-01-19 09:52:22 -080055 (Target)->addFakeDefUses(Instr); \
56 } else { \
57 /* Use llvm_unreachable instead of report_fatal_error, which gives \
58 better stack traces. */ \
Eric Holke37076a2016-01-27 14:06:35 -080059 llvm_unreachable( \
Jim Stichnoth467ffe52016-03-29 15:01:06 -070060 (std::string("Not yet implemented: ") + Instr->getInstName()) \
61 .c_str()); \
Jim Stichnoth91c773e2016-01-19 09:52:22 -080062 abort(); \
63 } \
64 } while (0)
65
Andrew Scull57e12682015-09-16 11:30:19 -070066/// LoweringContext makes it easy to iterate through non-deleted instructions in
67/// a node, and insert new (lowered) instructions at the current point. Along
68/// with the instruction list container and associated iterators, it holds the
69/// current node, which is needed when inserting new instructions in order to
70/// track whether variables are used as single-block or multi-block.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070071class LoweringContext {
Jim Stichnoth7b451a92014-10-15 14:39:23 -070072 LoweringContext(const LoweringContext &) = delete;
73 LoweringContext &operator=(const LoweringContext &) = delete;
74
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070075public:
Jim Stichnotheafb56c2015-06-22 10:35:22 -070076 LoweringContext() = default;
77 ~LoweringContext() = default;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070078 void init(CfgNode *Node);
79 Inst *getNextInst() const {
80 if (Next == End)
Jim Stichnothae953202014-12-20 06:17:49 -080081 return nullptr;
Jim Stichnoth607e9f02014-11-06 13:32:05 -080082 return Next;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070083 }
Jan Voungc820ddf2014-07-29 14:38:51 -070084 Inst *getNextInst(InstList::iterator &Iter) const {
Jan Vounge6e497d2014-07-30 10:06:03 -070085 advanceForward(Iter);
Jan Voungc820ddf2014-07-29 14:38:51 -070086 if (Iter == End)
Jim Stichnothae953202014-12-20 06:17:49 -080087 return nullptr;
Jim Stichnoth607e9f02014-11-06 13:32:05 -080088 return Iter;
Jan Voungc820ddf2014-07-29 14:38:51 -070089 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070090 CfgNode *getNode() const { return Node; }
91 bool atEnd() const { return Cur == End; }
92 InstList::iterator getCur() const { return Cur; }
Jim Stichnoth5d2fa0c2014-12-01 09:30:55 -080093 InstList::iterator getNext() const { return Next; }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070094 InstList::iterator getEnd() const { return End; }
Jim Stichnoth8cfeb692016-02-05 09:50:02 -080095 void insert(Inst *Instr);
John Porto1d937a82015-12-17 06:19:34 -080096 template <typename Inst, typename... Args> Inst *insert(Args &&... A) {
97 auto *New = Inst::create(Node->getCfg(), std::forward<Args>(A)...);
98 insert(New);
99 return New;
100 }
Jan Vounge6e497d2014-07-30 10:06:03 -0700101 Inst *getLastInserted() const;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700102 void advanceCur() { Cur = Next; }
Jan Vounge6e497d2014-07-30 10:06:03 -0700103 void advanceNext() { advanceForward(Next); }
Jim Stichnotha3f57b92015-07-30 12:46:04 -0700104 void setCur(InstList::iterator C) { Cur = C; }
105 void setNext(InstList::iterator N) { Next = N; }
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700106 void rewind();
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700107 void setInsertPoint(const InstList::iterator &Position) { Next = Position; }
Jim Stichnoth318f4cd2015-10-01 21:02:37 -0700108 void availabilityReset();
109 void availabilityUpdate();
110 Variable *availabilityGet(Operand *Src) const;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700111
112private:
Andrew Scull9612d322015-07-06 14:53:25 -0700113 /// Node is the argument to Inst::updateVars().
Jim Stichnotheafb56c2015-06-22 10:35:22 -0700114 CfgNode *Node = nullptr;
115 Inst *LastInserted = nullptr;
Andrew Scull57e12682015-09-16 11:30:19 -0700116 /// Cur points to the current instruction being considered. It is guaranteed
117 /// to point to a non-deleted instruction, or to be End.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700118 InstList::iterator Cur;
Andrew Scull57e12682015-09-16 11:30:19 -0700119 /// Next doubles as a pointer to the next valid instruction (if any), and the
120 /// new-instruction insertion point. It is also updated for the caller in case
121 /// the lowering consumes more than one high-level instruction. It is
122 /// guaranteed to point to a non-deleted instruction after Cur, or to be End.
123 // TODO: Consider separating the notion of "next valid instruction" and "new
124 // instruction insertion point", to avoid confusion when previously-deleted
125 // instructions come between the two points.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700126 InstList::iterator Next;
Andrew Scull9612d322015-07-06 14:53:25 -0700127 /// Begin is a copy of Insts.begin(), used if iterators are moved backward.
Jan Vounge6e497d2014-07-30 10:06:03 -0700128 InstList::iterator Begin;
Andrew Scull9612d322015-07-06 14:53:25 -0700129 /// End is a copy of Insts.end(), used if Next needs to be advanced.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700130 InstList::iterator End;
Jim Stichnoth318f4cd2015-10-01 21:02:37 -0700131 /// LastDest and LastSrc capture the parameters of the last "Dest=Src" simple
132 /// assignment inserted (provided Src is a variable). This is used for simple
133 /// availability analysis.
134 Variable *LastDest = nullptr;
135 Variable *LastSrc = nullptr;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700136
Jan Voungc820ddf2014-07-29 14:38:51 -0700137 void skipDeleted(InstList::iterator &I) const;
Jan Vounge6e497d2014-07-30 10:06:03 -0700138 void advanceForward(InstList::iterator &I) const;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700139};
140
Jan Voung28068ad2015-07-31 12:58:46 -0700141/// A helper class to advance the LoweringContext at each loop iteration.
142class PostIncrLoweringContext {
143 PostIncrLoweringContext() = delete;
144 PostIncrLoweringContext(const PostIncrLoweringContext &) = delete;
145 PostIncrLoweringContext &operator=(const PostIncrLoweringContext &) = delete;
146
147public:
148 explicit PostIncrLoweringContext(LoweringContext &Context)
149 : Context(Context) {}
150 ~PostIncrLoweringContext() {
151 Context.advanceCur();
152 Context.advanceNext();
153 }
154
155private:
156 LoweringContext &Context;
157};
158
John Porto53611e22015-12-30 07:30:10 -0800159/// TargetLowering is the base class for all backends in Subzero. In addition to
160/// implementing the abstract methods in this class, each concrete target must
161/// also implement a named constructor in its own namespace. For instance, for
162/// X8632 we have:
163///
164/// namespace X8632 {
165/// void createTargetLowering(Cfg *Func);
166/// }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700167class TargetLowering {
Jim Stichnothc6ead202015-02-24 09:30:30 -0800168 TargetLowering() = delete;
Jim Stichnoth7b451a92014-10-15 14:39:23 -0700169 TargetLowering(const TargetLowering &) = delete;
170 TargetLowering &operator=(const TargetLowering &) = delete;
171
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700172public:
Karl Schimpf5403f5d2016-01-15 11:07:46 -0800173 static void staticInit(GlobalContext *Ctx);
Jim Stichnoth8ff4b282016-01-04 15:39:06 -0800174 // Each target must define a public static method:
Karl Schimpf5403f5d2016-01-15 11:07:46 -0800175 // static void staticInit(GlobalContext *Ctx);
Jim Stichnoth467ffe52016-03-29 15:01:06 -0700176 static bool shouldBePooled(const class Constant *C);
John Porto53611e22015-12-30 07:30:10 -0800177
178 static std::unique_ptr<TargetLowering> createLowering(TargetArch Target,
179 Cfg *Func);
180
181 virtual std::unique_ptr<Assembler> createAssembler() const = 0;
182
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700183 void translate() {
Jim Stichnothdd6dcfa2016-04-18 12:52:09 -0700184 switch (Func->getOptLevel()) {
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700185 case Opt_m1:
186 translateOm1();
187 break;
188 case Opt_0:
189 translateO0();
190 break;
191 case Opt_1:
192 translateO1();
193 break;
194 case Opt_2:
195 translateO2();
196 break;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700197 }
198 }
199 virtual void translateOm1() {
200 Func->setError("Target doesn't specify Om1 lowering steps.");
201 }
202 virtual void translateO0() {
203 Func->setError("Target doesn't specify O0 lowering steps.");
204 }
205 virtual void translateO1() {
206 Func->setError("Target doesn't specify O1 lowering steps.");
207 }
208 virtual void translateO2() {
209 Func->setError("Target doesn't specify O2 lowering steps.");
210 }
211
John Porto5e0a8a72015-11-20 13:50:36 -0800212 /// Generates calls to intrinsics for operations the Target can't handle.
213 void genTargetHelperCalls();
Andrew Scull9612d322015-07-06 14:53:25 -0700214 /// Tries to do address mode optimization on a single instruction.
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700215 void doAddressOpt();
Andrew Scull9612d322015-07-06 14:53:25 -0700216 /// Randomly insert NOPs.
Qining Luaee5fa82015-08-20 14:59:03 -0700217 void doNopInsertion(RandomNumberGenerator &RNG);
Andrew Scull9612d322015-07-06 14:53:25 -0700218 /// Lowers a single non-Phi instruction.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700219 void lower();
Jim Stichnotha3f57b92015-07-30 12:46:04 -0700220 /// Inserts and lowers a single high-level instruction at a specific insertion
221 /// point.
222 void lowerInst(CfgNode *Node, InstList::iterator Next, InstHighLevel *Instr);
Andrew Scull57e12682015-09-16 11:30:19 -0700223 /// Does preliminary lowering of the set of Phi instructions in the current
224 /// node. The main intention is to do what's needed to keep the unlowered Phi
225 /// instructions consistent with the lowered non-Phi instructions, e.g. to
226 /// lower 64-bit operands on a 32-bit target.
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700227 virtual void prelowerPhis() {}
Andrew Scull57e12682015-09-16 11:30:19 -0700228 /// Tries to do branch optimization on a single instruction. Returns true if
229 /// some optimization was done.
Jim Stichnothff9c7062014-09-18 04:50:49 -0700230 virtual bool doBranchOpt(Inst * /*I*/, const CfgNode * /*NextNode*/) {
231 return false;
232 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700233
Jim Stichnoth3d44fe82014-11-01 10:10:18 -0700234 virtual SizeT getNumRegisters() const = 0;
Andrew Scull57e12682015-09-16 11:30:19 -0700235 /// Returns a variable pre-colored to the specified physical register. This is
236 /// generally used to get very direct access to the register such as in the
237 /// prolog or epilog or for marking scratch registers as killed by a call. If
238 /// a Type is not provided, a target-specific default type is used.
Jim Stichnoth8aa39662016-02-10 11:20:30 -0800239 virtual Variable *getPhysicalRegister(RegNumT RegNum,
Jim Stichnoth98712a32014-10-24 10:59:02 -0700240 Type Ty = IceType_void) = 0;
Andrew Scull9612d322015-07-06 14:53:25 -0700241 /// Returns a printable name for the register.
Jim Stichnoth467ffe52016-03-29 15:01:06 -0700242 virtual const char *getRegName(RegNumT RegNum, Type Ty) const = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700243
244 virtual bool hasFramePointer() const { return false; }
David Sehre39d0ca2015-11-06 11:25:41 -0800245 virtual void setHasFramePointer() = 0;
Jim Stichnoth8aa39662016-02-10 11:20:30 -0800246 virtual RegNumT getStackReg() const = 0;
247 virtual RegNumT getFrameReg() const = 0;
248 virtual RegNumT getFrameOrStackReg() const = 0;
Matt Walad4799f42014-08-14 14:24:12 -0700249 virtual size_t typeWidthInBytesOnStack(Type Ty) const = 0;
David Sehre39d0ca2015-11-06 11:25:41 -0800250 virtual uint32_t getStackAlignment() const = 0;
David Sehr2f3b8ec2015-11-16 16:51:39 -0800251 virtual void reserveFixedAllocaArea(size_t Size, size_t Align) = 0;
252 virtual int32_t getFrameFixedAllocaOffset() const = 0;
John Porto614140e2015-11-23 11:43:13 -0800253 virtual uint32_t maxOutArgsSizeBytes() const { return 0; }
Jan Voung0fa6c5a2015-06-01 11:04:04 -0700254
Andrew Scull6d47bcd2015-09-17 17:10:05 -0700255 /// Return whether a 64-bit Variable should be split into a Variable64On32.
256 virtual bool shouldSplitToVariable64On32(Type Ty) const = 0;
257
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700258 bool hasComputedFrame() const { return HasComputedFrame; }
Andrew Scull57e12682015-09-16 11:30:19 -0700259 /// Returns true if this function calls a function that has the "returns
260 /// twice" attribute.
Jan Voung44d53e12014-09-11 19:18:03 -0700261 bool callsReturnsTwice() const { return CallsReturnsTwice; }
Jim Stichnothdd842db2015-01-27 12:53:53 -0800262 void setCallsReturnsTwice(bool RetTwice) { CallsReturnsTwice = RetTwice; }
Jan Voungb36ad9b2015-04-21 17:01:49 -0700263 SizeT makeNextLabelNumber() { return NextLabelNumber++; }
Andrew Scull86df4e92015-07-30 13:54:44 -0700264 SizeT makeNextJumpTableNumber() { return NextJumpTableNumber++; }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700265 LoweringContext &getContext() { return Context; }
Jim Stichnoth8ff4b282016-01-04 15:39:06 -0800266 Cfg *getFunc() const { return Func; }
267 GlobalContext *getGlobalContext() const { return Ctx; }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700268
269 enum RegSet {
270 RegSet_None = 0,
271 RegSet_CallerSave = 1 << 0,
272 RegSet_CalleeSave = 1 << 1,
273 RegSet_StackPointer = 1 << 2,
274 RegSet_FramePointer = 1 << 3,
275 RegSet_All = ~RegSet_None
276 };
Andrew Scull8072bae2015-09-14 16:01:26 -0700277 using RegSetMask = uint32_t;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700278
John Portoe82b5602016-02-24 15:58:55 -0800279 virtual SmallBitVector getRegisterSet(RegSetMask Include,
280 RegSetMask Exclude) const = 0;
Jim Stichnothb40595a2016-01-29 06:14:31 -0800281 /// Get the set of physical registers available for the specified Variable's
282 /// register class, applying register restrictions from the command line.
John Portoe82b5602016-02-24 15:58:55 -0800283 virtual const SmallBitVector &
Jim Stichnothc59288b2015-11-09 11:38:40 -0800284 getRegistersForVariable(const Variable *Var) const = 0;
Jim Stichnothb40595a2016-01-29 06:14:31 -0800285 /// Get the set of *all* physical registers available for the specified
286 /// Variable's register class, *not* applying register restrictions from the
287 /// command line.
John Portoe82b5602016-02-24 15:58:55 -0800288 virtual const SmallBitVector &
Jim Stichnothb40595a2016-01-29 06:14:31 -0800289 getAllRegistersForVariable(const Variable *Var) const = 0;
John Portoe82b5602016-02-24 15:58:55 -0800290 virtual const SmallBitVector &getAliasesForRegister(RegNumT) const = 0;
John Portobb0a5fe2015-09-04 11:23:41 -0700291
Jim Stichnoth70d0a052014-11-14 15:53:46 -0800292 void regAlloc(RegAllocKind Kind);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700293
Qining Luaee5fa82015-08-20 14:59:03 -0700294 virtual void
Jim Stichnoth8aa39662016-02-10 11:20:30 -0800295 makeRandomRegisterPermutation(llvm::SmallVectorImpl<RegNumT> &Permutation,
John Portoe82b5602016-02-24 15:58:55 -0800296 const SmallBitVector &ExcludeRegisters,
Qining Luaee5fa82015-08-20 14:59:03 -0700297 uint64_t Salt) const = 0;
Jim Stichnothe6d24782014-12-19 05:42:24 -0800298
Andrew Scull87f80c12015-07-20 10:19:16 -0700299 /// Get the minimum number of clusters required for a jump table to be
300 /// considered.
301 virtual SizeT getMinJumpTableSize() const = 0;
Andrew Scull86df4e92015-07-30 13:54:44 -0700302 virtual void emitJumpTable(const Cfg *Func,
303 const InstJumpTable *JumpTable) const = 0;
Andrew Scull87f80c12015-07-20 10:19:16 -0700304
Jim Stichnoth144cdce2014-09-22 16:02:59 -0700305 virtual void emitVariable(const Variable *Var) const = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700306
Jim Stichnoth8ff4b282016-01-04 15:39:06 -0800307 void emitWithoutPrefix(const ConstantRelocatable *CR,
308 const char *Suffix = "") const;
Jan Voung76bb0be2015-05-14 09:26:19 -0700309
Jan Voung76bb0be2015-05-14 09:26:19 -0700310 virtual void emit(const ConstantInteger32 *C) const = 0;
311 virtual void emit(const ConstantInteger64 *C) const = 0;
312 virtual void emit(const ConstantFloat *C) const = 0;
313 virtual void emit(const ConstantDouble *C) const = 0;
Jim Stichnoth8ff4b282016-01-04 15:39:06 -0800314 virtual void emit(const ConstantUndef *C) const = 0;
315 virtual void emit(const ConstantRelocatable *CR) const = 0;
Jan Voung76bb0be2015-05-14 09:26:19 -0700316
Andrew Scull9612d322015-07-06 14:53:25 -0700317 /// Performs target-specific argument lowering.
Matt Wala45a06232014-07-09 16:33:22 -0700318 virtual void lowerArguments() = 0;
319
Jim Stichnotha59ae6f2015-05-17 10:11:41 -0700320 virtual void initNodeForLowering(CfgNode *) {}
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700321 virtual void addProlog(CfgNode *Node) = 0;
322 virtual void addEpilog(CfgNode *Node) = 0;
323
Jim Stichnotheafb56c2015-06-22 10:35:22 -0700324 virtual ~TargetLowering() = default;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700325
John Porto3bf335f2016-01-15 11:17:55 -0800326private:
327 // This control variable is used by AutoBundle (RAII-style bundle
328 // locking/unlocking) to prevent nested bundles.
329 bool AutoBundling = false;
330
Eric Holkd6cf6b32016-02-17 11:09:48 -0800331 /// This indicates whether we are in the genTargetHelperCalls phase, and
332 /// therefore can do things like scalarization.
333 bool GeneratingTargetHelpers = false;
334
John Porto3bf335f2016-01-15 11:17:55 -0800335 // _bundle_lock(), and _bundle_unlock(), were made private to force subtargets
336 // to use the AutoBundle helper.
337 void
338 _bundle_lock(InstBundleLock::Option BundleOption = InstBundleLock::Opt_None) {
339 Context.insert<InstBundleLock>(BundleOption);
340 }
341 void _bundle_unlock() { Context.insert<InstBundleUnlock>(); }
342
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700343protected:
John Porto3bf335f2016-01-15 11:17:55 -0800344 /// AutoBundle provides RIAA-style bundling. Sub-targets are expected to use
345 /// it when emitting NaCl Bundles to ensure proper bundle_unlocking, and
346 /// prevent nested bundles.
347 ///
348 /// AutoBundle objects will emit a _bundle_lock during construction (but only
349 /// if sandboxed code generation was requested), and a bundle_unlock() during
350 /// destruction. By carefully scoping objects of this type, Subtargets can
351 /// ensure proper bundle emission.
352 class AutoBundle {
353 AutoBundle() = delete;
354 AutoBundle(const AutoBundle &) = delete;
355 AutoBundle &operator=(const AutoBundle &) = delete;
356
357 public:
358 explicit AutoBundle(TargetLowering *Target, InstBundleLock::Option Option =
359 InstBundleLock::Opt_None);
360 ~AutoBundle();
361
362 private:
363 TargetLowering *const Target;
364 const bool NeedSandboxing;
365 };
366
Jim Stichnothc6ead202015-02-24 09:30:30 -0800367 explicit TargetLowering(Cfg *Func);
Karl Schimpf5403f5d2016-01-15 11:07:46 -0800368 // Applies command line filters to TypeToRegisterSet array.
Jim Stichnoth467ffe52016-03-29 15:01:06 -0700369 static void filterTypeToRegisterSet(
370 GlobalContext *Ctx, int32_t NumRegs, SmallBitVector TypeToRegisterSet[],
371 size_t TypeToRegisterSetSize,
372 std::function<std::string(RegNumT)> getRegName,
373 std::function<const char *(RegClass)> getRegClassName);
Jim Stichnoth8cfeb692016-02-05 09:50:02 -0800374 virtual void lowerAlloca(const InstAlloca *Instr) = 0;
375 virtual void lowerArithmetic(const InstArithmetic *Instr) = 0;
376 virtual void lowerAssign(const InstAssign *Instr) = 0;
377 virtual void lowerBr(const InstBr *Instr) = 0;
Eric Holk67c7c412016-04-15 13:05:37 -0700378 virtual void lowerBreakpoint(const InstBreakpoint *Instr) = 0;
Jim Stichnoth8cfeb692016-02-05 09:50:02 -0800379 virtual void lowerCall(const InstCall *Instr) = 0;
380 virtual void lowerCast(const InstCast *Instr) = 0;
381 virtual void lowerFcmp(const InstFcmp *Instr) = 0;
382 virtual void lowerExtractElement(const InstExtractElement *Instr) = 0;
383 virtual void lowerIcmp(const InstIcmp *Instr) = 0;
384 virtual void lowerInsertElement(const InstInsertElement *Instr) = 0;
385 virtual void lowerIntrinsicCall(const InstIntrinsicCall *Instr) = 0;
386 virtual void lowerLoad(const InstLoad *Instr) = 0;
387 virtual void lowerPhi(const InstPhi *Instr) = 0;
388 virtual void lowerRet(const InstRet *Instr) = 0;
389 virtual void lowerSelect(const InstSelect *Instr) = 0;
390 virtual void lowerStore(const InstStore *Instr) = 0;
391 virtual void lowerSwitch(const InstSwitch *Instr) = 0;
392 virtual void lowerUnreachable(const InstUnreachable *Instr) = 0;
Jim Stichnothe4f65d82015-06-17 22:16:02 -0700393 virtual void lowerOther(const Inst *Instr);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700394
John Porto5e0a8a72015-11-20 13:50:36 -0800395 virtual void genTargetHelperCallFor(Inst *Instr) = 0;
John Portof4198542015-11-20 14:17:23 -0800396 virtual uint32_t getCallStackArgumentsSizeBytes(const InstCall *Instr) = 0;
John Porto5e0a8a72015-11-20 13:50:36 -0800397
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700398 virtual void doAddressOptLoad() {}
399 virtual void doAddressOptStore() {}
Jim Stichnothad2989b2015-09-15 10:21:42 -0700400 virtual void doMockBoundsCheck(Operand *) {}
Qining Luaee5fa82015-08-20 14:59:03 -0700401 virtual void randomlyInsertNop(float Probability,
402 RandomNumberGenerator &RNG) = 0;
Andrew Scull57e12682015-09-16 11:30:19 -0700403 /// This gives the target an opportunity to post-process the lowered expansion
404 /// before returning.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700405 virtual void postLower() {}
406
Jim Stichnoth91c773e2016-01-19 09:52:22 -0800407 /// When the SkipUnimplemented flag is set, addFakeDefUses() gets invoked by
408 /// the UnimplementedLoweringError macro to insert fake uses of all the
409 /// instruction variables and a fake def of the instruction dest, in order to
410 /// preserve integrity of liveness analysis.
411 void addFakeDefUses(const Inst *Instr);
412
Jim Stichnoth230d4102015-09-25 17:40:32 -0700413 /// Find (non-SSA) instructions where the Dest variable appears in some source
414 /// operand, and set the IsDestRedefined flag. This keeps liveness analysis
415 /// consistent.
416 void markRedefinitions();
Jan Voungb3401d22015-05-18 09:38:21 -0700417
Andrew Scull57e12682015-09-16 11:30:19 -0700418 /// Make a pass over the Cfg to determine which variables need stack slots and
419 /// place them in a sorted list (SortedSpilledVariables). Among those, vars,
420 /// classify the spill variables as local to the basic block vs global
421 /// (multi-block) in order to compute the parameters GlobalsSize and
422 /// SpillAreaSizeBytes (represents locals or general vars if the coalescing of
423 /// locals is disallowed) along with alignments required for variables in each
424 /// area. We rely on accurate VMetadata in order to classify a variable as
425 /// global vs local (otherwise the variable is conservatively global). The
426 /// in-args should be initialized to 0.
Andrew Scull9612d322015-07-06 14:53:25 -0700427 ///
Andrew Scull57e12682015-09-16 11:30:19 -0700428 /// This is only a pre-pass and the actual stack slot assignment is handled
429 /// separately.
Andrew Scull9612d322015-07-06 14:53:25 -0700430 ///
Andrew Scull57e12682015-09-16 11:30:19 -0700431 /// There may be target-specific Variable types, which will be handled by
432 /// TargetVarHook. If the TargetVarHook returns true, then the variable is
433 /// skipped and not considered with the rest of the spilled variables.
Jan Voung0fa6c5a2015-06-01 11:04:04 -0700434 void getVarStackSlotParams(VarList &SortedSpilledVariables,
John Portoe82b5602016-02-24 15:58:55 -0800435 SmallBitVector &RegsUsed, size_t *GlobalsSize,
436 size_t *SpillAreaSizeBytes,
Jan Voung0fa6c5a2015-06-01 11:04:04 -0700437 uint32_t *SpillAreaAlignmentBytes,
438 uint32_t *LocalsSlotsAlignmentBytes,
439 std::function<bool(Variable *)> TargetVarHook);
440
Andrew Scull57e12682015-09-16 11:30:19 -0700441 /// Calculate the amount of padding needed to align the local and global areas
442 /// to the required alignment. This assumes the globals/locals layout used by
443 /// getVarStackSlotParams and assignVarStackSlots.
Jan Voung0fa6c5a2015-06-01 11:04:04 -0700444 void alignStackSpillAreas(uint32_t SpillAreaStartOffset,
445 uint32_t SpillAreaAlignmentBytes,
446 size_t GlobalsSize,
447 uint32_t LocalsSlotsAlignmentBytes,
448 uint32_t *SpillAreaPaddingBytes,
449 uint32_t *LocalsSlotsPaddingBytes);
450
Andrew Scull57e12682015-09-16 11:30:19 -0700451 /// Make a pass through the SortedSpilledVariables and actually assign stack
452 /// slots. SpillAreaPaddingBytes takes into account stack alignment padding.
453 /// The SpillArea starts after that amount of padding. This matches the scheme
454 /// in getVarStackSlotParams, where there may be a separate multi-block global
455 /// var spill area and a local var spill area.
Jan Voung0fa6c5a2015-06-01 11:04:04 -0700456 void assignVarStackSlots(VarList &SortedSpilledVariables,
457 size_t SpillAreaPaddingBytes,
458 size_t SpillAreaSizeBytes,
459 size_t GlobalsAndSubsequentPaddingSize,
460 bool UsesFramePointer);
461
Andrew Scull57e12682015-09-16 11:30:19 -0700462 /// Sort the variables in Source based on required alignment. The variables
463 /// with the largest alignment need are placed in the front of the Dest list.
Jan Voung0fa6c5a2015-06-01 11:04:04 -0700464 void sortVarsByAlignment(VarList &Dest, const VarList &Source) const;
465
Karl Schimpf20070e82016-03-17 13:30:13 -0700466 InstCall *makeHelperCall(RuntimeHelper FuncID, Variable *Dest, SizeT MaxSrcs);
Jan Voungb36ad9b2015-04-21 17:01:49 -0700467
Jim Stichnoth230d4102015-09-25 17:40:32 -0700468 void _set_dest_redefined() { Context.getLastInserted()->setDestRedefined(); }
Jan Voung0fa6c5a2015-06-01 11:04:04 -0700469
Andrew Scullcfa628b2015-08-20 14:23:05 -0700470 bool shouldOptimizeMemIntrins();
471
Eric Holkcfc25532016-02-09 17:47:58 -0800472 void scalarizeArithmetic(InstArithmetic::OpKind K, Variable *Dest,
473 Operand *Src0, Operand *Src1);
474
Eric Holkcc69fa22016-02-10 13:07:06 -0800475 /// Generalizes scalarizeArithmetic to support other instruction types.
476 ///
Eric Holkd6cf6b32016-02-17 11:09:48 -0800477 /// insertScalarInstruction is a function-like object with signature
Eric Holkcc69fa22016-02-10 13:07:06 -0800478 /// (Variable *Dest, Variable *Src0, Variable *Src1) -> Instr *.
Eric Holkd6cf6b32016-02-17 11:09:48 -0800479 template <typename... Operands,
480 typename F = std::function<Inst *(Variable *, Operands *...)>>
481 void scalarizeInstruction(Variable *Dest, F insertScalarInstruction,
482 Operands *... Srcs) {
483 assert(GeneratingTargetHelpers &&
484 "scalarizeInstruction called during incorrect phase");
Eric Holkcc69fa22016-02-10 13:07:06 -0800485 const Type DestTy = Dest->getType();
486 assert(isVectorType(DestTy));
487 const Type DestElementTy = typeElementType(DestTy);
488 const SizeT NumElements = typeNumElements(DestTy);
Eric Holkcc69fa22016-02-10 13:07:06 -0800489
490 Variable *T = Func->makeVariable(DestTy);
491 Context.insert<InstFakeDef>(T);
Eric Holkcc69fa22016-02-10 13:07:06 -0800492
Eric Holkd6cf6b32016-02-17 11:09:48 -0800493 for (SizeT I = 0; I < NumElements; ++I) {
494 auto *Index = Ctx->getConstantInt32(I);
495
496 auto makeExtractThunk = [this, Index, NumElements](Operand *Src) {
497 return [this, Index, NumElements, Src]() {
498 assert(typeNumElements(Src->getType()) == NumElements);
499
500 const auto ElementTy = typeElementType(Src->getType());
501 auto *Op = Func->makeVariable(ElementTy);
502 Context.insert<InstExtractElement>(Op, Src, Index);
503 return Op;
504 };
505 };
Eric Holkcc69fa22016-02-10 13:07:06 -0800506
507 // Perform the operation as a scalar operation.
Eric Holkd6cf6b32016-02-17 11:09:48 -0800508 auto *Res = Func->makeVariable(DestElementTy);
509 auto *Arith = applyToThunkedArgs(insertScalarInstruction, Res,
510 makeExtractThunk(Srcs)...);
Eric Holkcc69fa22016-02-10 13:07:06 -0800511 genTargetHelperCallFor(Arith);
512
Eric Holkcc69fa22016-02-10 13:07:06 -0800513 Variable *DestT = Func->makeVariable(DestTy);
514 Context.insert<InstInsertElement>(DestT, T, Res, Index);
515 T = DestT;
516 }
517 Context.insert<InstAssign>(Dest, T);
518 }
519
Eric Holkd6cf6b32016-02-17 11:09:48 -0800520 // applyToThunkedArgs is used by scalarizeInstruction. Ideally, we would just
521 // call insertScalarInstruction(Res, Srcs...), but C++ does not specify
522 // evaluation order which means this leads to an unpredictable final
523 // output. Instead, we wrap each of the Srcs in a thunk and these
524 // applyToThunkedArgs functions apply the thunks in a well defined order so we
525 // still get well-defined output.
526 Inst *applyToThunkedArgs(
527 std::function<Inst *(Variable *, Variable *)> insertScalarInstruction,
528 Variable *Res, std::function<Variable *()> thunk0) {
529 auto *Src0 = thunk0();
530 return insertScalarInstruction(Res, Src0);
531 }
Eric Holkcc69fa22016-02-10 13:07:06 -0800532
Eric Holkd6cf6b32016-02-17 11:09:48 -0800533 Inst *
534 applyToThunkedArgs(std::function<Inst *(Variable *, Variable *, Variable *)>
535 insertScalarInstruction,
536 Variable *Res, std::function<Variable *()> thunk0,
537 std::function<Variable *()> thunk1) {
538 auto *Src0 = thunk0();
539 auto *Src1 = thunk1();
540 return insertScalarInstruction(Res, Src0, Src1);
541 }
Eric Holkcc69fa22016-02-10 13:07:06 -0800542
Eric Holkd6cf6b32016-02-17 11:09:48 -0800543 Inst *applyToThunkedArgs(
544 std::function<Inst *(Variable *, Variable *, Variable *, Variable *)>
545 insertScalarInstruction,
546 Variable *Res, std::function<Variable *()> thunk0,
547 std::function<Variable *()> thunk1, std::function<Variable *()> thunk2) {
548 auto *Src0 = thunk0();
549 auto *Src1 = thunk1();
550 auto *Src2 = thunk2();
551 return insertScalarInstruction(Res, Src0, Src1, Src2);
Eric Holkcc69fa22016-02-10 13:07:06 -0800552 }
553
John Portoac2388c2016-01-22 07:10:56 -0800554 /// SandboxType enumerates all possible sandboxing strategies that
555 enum SandboxType {
556 ST_None,
557 ST_NaCl,
558 ST_Nonsfi,
559 };
560
561 static SandboxType determineSandboxTypeFromFlags(const ClFlags &Flags);
562
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700563 Cfg *Func;
564 GlobalContext *Ctx;
Jim Stichnotheafb56c2015-06-22 10:35:22 -0700565 bool HasComputedFrame = false;
566 bool CallsReturnsTwice = false;
Jim Stichnotheafb56c2015-06-22 10:35:22 -0700567 SizeT NextLabelNumber = 0;
Andrew Scull86df4e92015-07-30 13:54:44 -0700568 SizeT NextJumpTableNumber = 0;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700569 LoweringContext Context;
John Portoac2388c2016-01-22 07:10:56 -0800570 const SandboxType SandboxingType = ST_None;
Jim Stichnoth9738a9e2015-02-23 16:39:06 -0800571
Jim Stichnoth8ff4b282016-01-04 15:39:06 -0800572 const static constexpr char *H_getIP_prefix = "__Sz_getIP_";
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700573};
574
Andrew Scull57e12682015-09-16 11:30:19 -0700575/// TargetDataLowering is used for "lowering" data including initializers for
576/// global variables, and the internal constant pools. It is separated out from
577/// TargetLowering because it does not require a Cfg.
Jan Voung72984d82015-01-29 14:42:38 -0800578class TargetDataLowering {
579 TargetDataLowering() = delete;
580 TargetDataLowering(const TargetDataLowering &) = delete;
581 TargetDataLowering &operator=(const TargetDataLowering &) = delete;
Jim Stichnoth7b451a92014-10-15 14:39:23 -0700582
Jim Stichnothde4ca712014-06-29 08:13:48 -0700583public:
Jim Stichnothbbca7542015-02-11 16:08:31 -0800584 static std::unique_ptr<TargetDataLowering> createLowering(GlobalContext *Ctx);
Jan Voung72984d82015-01-29 14:42:38 -0800585 virtual ~TargetDataLowering();
Jan Voung839c4ce2014-07-28 15:19:43 -0700586
John Porto8b1a7052015-06-17 13:20:08 -0700587 virtual void lowerGlobals(const VariableDeclarationList &Vars,
Jim Stichnoth467ffe52016-03-29 15:01:06 -0700588 const std::string &SectionSuffix) = 0;
John Porto0f86d032015-06-15 07:44:27 -0700589 virtual void lowerConstants() = 0;
Andrew Scull86df4e92015-07-30 13:54:44 -0700590 virtual void lowerJumpTables() = 0;
Jim Stichnothde4ca712014-06-29 08:13:48 -0700591
592protected:
John Porto8b1a7052015-06-17 13:20:08 -0700593 void emitGlobal(const VariableDeclaration &Var,
Jim Stichnoth467ffe52016-03-29 15:01:06 -0700594 const std::string &SectionSuffix);
Jan Voung58eea4d2015-06-15 15:11:56 -0700595
Andrew Scull57e12682015-09-16 11:30:19 -0700596 /// For now, we assume .long is the right directive for emitting 4 byte emit
597 /// global relocations. However, LLVM MIPS usually uses .4byte instead.
Andrew Scull9612d322015-07-06 14:53:25 -0700598 /// Perhaps there is some difference when the location is unaligned.
John Porto8b1a7052015-06-17 13:20:08 -0700599 static const char *getEmit32Directive() { return ".long"; }
Jan Voung58eea4d2015-06-15 15:11:56 -0700600
Jim Stichnothc6ead202015-02-24 09:30:30 -0800601 explicit TargetDataLowering(GlobalContext *Ctx) : Ctx(Ctx) {}
Jim Stichnothde4ca712014-06-29 08:13:48 -0700602 GlobalContext *Ctx;
Jim Stichnothde4ca712014-06-29 08:13:48 -0700603};
604
Andrew Scull57e12682015-09-16 11:30:19 -0700605/// TargetHeaderLowering is used to "lower" the header of an output file. It
606/// writes out the target-specific header attributes. E.g., for ARM this writes
607/// out the build attributes (float ABI, etc.).
Jan Voungfb792842015-06-11 15:27:50 -0700608class TargetHeaderLowering {
609 TargetHeaderLowering() = delete;
610 TargetHeaderLowering(const TargetHeaderLowering &) = delete;
611 TargetHeaderLowering &operator=(const TargetHeaderLowering &) = delete;
612
613public:
614 static std::unique_ptr<TargetHeaderLowering>
615 createLowering(GlobalContext *Ctx);
616 virtual ~TargetHeaderLowering();
617
618 virtual void lower() {}
619
620protected:
621 explicit TargetHeaderLowering(GlobalContext *Ctx) : Ctx(Ctx) {}
622 GlobalContext *Ctx;
623};
624
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700625} // end of namespace Ice
626
627#endif // SUBZERO_SRC_ICETARGETLOWERING_H