blob: fb9fad472d9b2b02bbfcd233ba0e56af9374c106 [file] [log] [blame]
Sam Parker3828c6f2018-07-23 12:27:47 +00001//===----- ARMCodeGenPrepare.cpp ------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10/// \file
11/// This pass inserts intrinsics to handle small types that would otherwise be
12/// promoted during legalization. Here we can manually promote types or insert
13/// intrinsics which can handle narrow types that aren't supported by the
14/// register classes.
15//
16//===----------------------------------------------------------------------===//
17
18#include "ARM.h"
19#include "ARMSubtarget.h"
20#include "ARMTargetMachine.h"
21#include "llvm/ADT/StringRef.h"
22#include "llvm/CodeGen/Passes.h"
23#include "llvm/CodeGen/TargetPassConfig.h"
24#include "llvm/IR/Attributes.h"
25#include "llvm/IR/BasicBlock.h"
26#include "llvm/IR/IRBuilder.h"
27#include "llvm/IR/Constants.h"
28#include "llvm/IR/InstrTypes.h"
29#include "llvm/IR/Instruction.h"
30#include "llvm/IR/Instructions.h"
31#include "llvm/IR/IntrinsicInst.h"
32#include "llvm/IR/Intrinsics.h"
33#include "llvm/IR/Type.h"
34#include "llvm/IR/Value.h"
35#include "llvm/IR/Verifier.h"
36#include "llvm/Pass.h"
37#include "llvm/Support/Casting.h"
38#include "llvm/Support/CommandLine.h"
39
40#define DEBUG_TYPE "arm-codegenprepare"
41
42using namespace llvm;
43
44static cl::opt<bool>
Hans Wennborg5555c002018-09-24 11:40:07 +000045DisableCGP("arm-disable-cgp", cl::Hidden, cl::init(true),
Sam Parker3828c6f2018-07-23 12:27:47 +000046 cl::desc("Disable ARM specific CodeGenPrepare pass"));
47
48static cl::opt<bool>
49EnableDSP("arm-enable-scalar-dsp", cl::Hidden, cl::init(false),
50 cl::desc("Use DSP instructions for scalar operations"));
51
52static cl::opt<bool>
53EnableDSPWithImms("arm-enable-scalar-dsp-imms", cl::Hidden, cl::init(false),
54 cl::desc("Use DSP instructions for scalar operations\
55 with immediate operands"));
56
Sjoerd Meijer31239a42018-08-17 07:34:01 +000057// The goal of this pass is to enable more efficient code generation for
58// operations on narrow types (i.e. types with < 32-bits) and this is a
59// motivating IR code example:
60//
61// define hidden i32 @cmp(i8 zeroext) {
62// %2 = add i8 %0, -49
63// %3 = icmp ult i8 %2, 3
64// ..
65// }
66//
67// The issue here is that i8 is type-legalized to i32 because i8 is not a
68// legal type. Thus, arithmetic is done in integer-precision, but then the
69// byte value is masked out as follows:
70//
71// t19: i32 = add t4, Constant:i32<-49>
72// t24: i32 = and t19, Constant:i32<255>
73//
74// Consequently, we generate code like this:
75//
76// subs r0, #49
77// uxtb r1, r0
78// cmp r1, #3
79//
80// This shows that masking out the byte value results in generation of
81// the UXTB instruction. This is not optimal as r0 already contains the byte
82// value we need, and so instead we can just generate:
83//
84// sub.w r1, r0, #49
85// cmp r1, #3
86//
87// We achieve this by type promoting the IR to i32 like so for this example:
88//
89// define i32 @cmp(i8 zeroext %c) {
90// %0 = zext i8 %c to i32
91// %c.off = add i32 %0, -49
92// %1 = icmp ult i32 %c.off, 3
93// ..
94// }
95//
96// For this to be valid and legal, we need to prove that the i32 add is
97// producing the same value as the i8 addition, and that e.g. no overflow
98// happens.
99//
100// A brief sketch of the algorithm and some terminology.
101// We pattern match interesting IR patterns:
102// - which have "sources": instructions producing narrow values (i8, i16), and
103// - they have "sinks": instructions consuming these narrow values.
104//
105// We collect all instruction connecting sources and sinks in a worklist, so
106// that we can mutate these instruction and perform type promotion when it is
107// legal to do so.
Sam Parker3828c6f2018-07-23 12:27:47 +0000108
Sjoerd Meijer31239a42018-08-17 07:34:01 +0000109namespace {
Sam Parker3828c6f2018-07-23 12:27:47 +0000110class IRPromoter {
111 SmallPtrSet<Value*, 8> NewInsts;
112 SmallVector<Instruction*, 4> InstsToRemove;
113 Module *M = nullptr;
114 LLVMContext &Ctx;
115
116public:
117 IRPromoter(Module *M) : M(M), Ctx(M->getContext()) { }
118
119 void Cleanup() {
120 for (auto *I : InstsToRemove) {
121 LLVM_DEBUG(dbgs() << "ARM CGP: Removing " << *I << "\n");
122 I->dropAllReferences();
123 I->eraseFromParent();
124 }
125 InstsToRemove.clear();
126 NewInsts.clear();
127 }
128
129 void Mutate(Type *OrigTy,
130 SmallPtrSetImpl<Value*> &Visited,
Sjoerd Meijer31239a42018-08-17 07:34:01 +0000131 SmallPtrSetImpl<Value*> &Sources,
132 SmallPtrSetImpl<Instruction*> &Sinks);
Sam Parker3828c6f2018-07-23 12:27:47 +0000133};
134
135class ARMCodeGenPrepare : public FunctionPass {
136 const ARMSubtarget *ST = nullptr;
137 IRPromoter *Promoter = nullptr;
138 std::set<Value*> AllVisited;
Sam Parker3828c6f2018-07-23 12:27:47 +0000139
Sam Parker3828c6f2018-07-23 12:27:47 +0000140 bool isSupportedValue(Value *V);
141 bool isLegalToPromote(Value *V);
142 bool TryToPromote(Value *V);
143
144public:
145 static char ID;
Sam Parker8c4b9642018-08-10 13:57:13 +0000146 static unsigned TypeSize;
147 Type *OrigTy = nullptr;
Sam Parker3828c6f2018-07-23 12:27:47 +0000148
149 ARMCodeGenPrepare() : FunctionPass(ID) {}
150
Sam Parker3828c6f2018-07-23 12:27:47 +0000151 void getAnalysisUsage(AnalysisUsage &AU) const override {
152 AU.addRequired<TargetPassConfig>();
153 }
154
155 StringRef getPassName() const override { return "ARM IR optimizations"; }
156
157 bool doInitialization(Module &M) override;
158 bool runOnFunction(Function &F) override;
Matt Morehousea70685f2018-07-23 17:00:45 +0000159 bool doFinalization(Module &M) override;
Sam Parker3828c6f2018-07-23 12:27:47 +0000160};
161
162}
163
Sjoerd Meijer31239a42018-08-17 07:34:01 +0000164static bool generateSignBits(Value *V) {
Sam Parker3828c6f2018-07-23 12:27:47 +0000165 if (!isa<Instruction>(V))
166 return false;
167
168 unsigned Opc = cast<Instruction>(V)->getOpcode();
169 return Opc == Instruction::AShr || Opc == Instruction::SDiv ||
170 Opc == Instruction::SRem;
171}
172
173/// Some instructions can use 8- and 16-bit operands, and we don't need to
174/// promote anything larger. We disallow booleans to make life easier when
175/// dealing with icmps but allow any other integer that is <= 16 bits. Void
176/// types are accepted so we can handle switches.
177static bool isSupportedType(Value *V) {
Sam Parker8c4b9642018-08-10 13:57:13 +0000178 Type *Ty = V->getType();
Sam Parker7def86b2018-08-15 07:52:35 +0000179
180 // Allow voids and pointers, these won't be promoted.
181 if (Ty->isVoidTy() || Ty->isPointerTy())
Sam Parker3828c6f2018-07-23 12:27:47 +0000182 return true;
183
Sam Parker8c4b9642018-08-10 13:57:13 +0000184 if (auto *Ld = dyn_cast<LoadInst>(V))
185 Ty = cast<PointerType>(Ld->getPointerOperandType())->getElementType();
186
187 const IntegerType *IntTy = dyn_cast<IntegerType>(Ty);
Sam Parkeraaec3c62018-09-13 15:14:12 +0000188 if (!IntTy)
Sam Parker3828c6f2018-07-23 12:27:47 +0000189 return false;
190
Sam Parker8c4b9642018-08-10 13:57:13 +0000191 return IntTy->getBitWidth() == ARMCodeGenPrepare::TypeSize;
192}
Sam Parker3828c6f2018-07-23 12:27:47 +0000193
Sjoerd Meijer31239a42018-08-17 07:34:01 +0000194/// Return true if the given value is a source in the use-def chain, producing
Sam Parker8c4b9642018-08-10 13:57:13 +0000195/// a narrow (i8, i16) value. These values will be zext to start the promotion
196/// of the tree to i32. We guarantee that these won't populate the upper bits
197/// of the register. ZExt on the loads will be free, and the same for call
198/// return values because we only accept ones that guarantee a zeroext ret val.
199/// Many arguments will have the zeroext attribute too, so those would be free
200/// too.
201static bool isSource(Value *V) {
Sam Parker7def86b2018-08-15 07:52:35 +0000202 if (!isa<IntegerType>(V->getType()))
203 return false;
Volodymyr Sapsai703ab842018-09-18 00:11:55 +0000204 // TODO Allow zext to be sources.
205 if (isa<Argument>(V))
Sam Parker8c4b9642018-08-10 13:57:13 +0000206 return true;
Volodymyr Sapsai703ab842018-09-18 00:11:55 +0000207 else if (isa<LoadInst>(V))
208 return true;
209 else if (isa<BitCastInst>(V))
210 return true;
211 else if (auto *Call = dyn_cast<CallInst>(V))
212 return Call->hasRetAttr(Attribute::AttrKind::ZExt);
213 else if (auto *Trunc = dyn_cast<TruncInst>(V))
214 return isSupportedType(Trunc);
Sam Parker8c4b9642018-08-10 13:57:13 +0000215 return false;
Sam Parker3828c6f2018-07-23 12:27:47 +0000216}
217
218/// Return true if V will require any promoted values to be truncated for the
Sam Parker8c4b9642018-08-10 13:57:13 +0000219/// the IR to remain valid. We can't mutate the value type of these
220/// instructions.
Sam Parker3828c6f2018-07-23 12:27:47 +0000221static bool isSink(Value *V) {
Sam Parker8c4b9642018-08-10 13:57:13 +0000222 // TODO The truncate also isn't actually necessary because we would already
223 // proved that the data value is kept within the range of the original data
224 // type.
Sam Parker3828c6f2018-07-23 12:27:47 +0000225 auto UsesNarrowValue = [](Value *V) {
Sam Parker8c4b9642018-08-10 13:57:13 +0000226 return V->getType()->getScalarSizeInBits() == ARMCodeGenPrepare::TypeSize;
Sam Parker3828c6f2018-07-23 12:27:47 +0000227 };
228
229 if (auto *Store = dyn_cast<StoreInst>(V))
230 return UsesNarrowValue(Store->getValueOperand());
231 if (auto *Return = dyn_cast<ReturnInst>(V))
232 return UsesNarrowValue(Return->getReturnValue());
Sam Parker8c4b9642018-08-10 13:57:13 +0000233 if (auto *Trunc = dyn_cast<TruncInst>(V))
234 return UsesNarrowValue(Trunc->getOperand(0));
Sam Parker0e2f0bd2018-08-16 11:54:09 +0000235 if (auto *ZExt = dyn_cast<ZExtInst>(V))
236 return UsesNarrowValue(ZExt->getOperand(0));
Sam Parker13567db2018-08-16 10:05:39 +0000237 if (auto *ICmp = dyn_cast<ICmpInst>(V))
238 return ICmp->isSigned();
Sam Parker3828c6f2018-07-23 12:27:47 +0000239
240 return isa<CallInst>(V);
241}
242
Sam Parker3828c6f2018-07-23 12:27:47 +0000243/// Return whether the instruction can be promoted within any modifications to
244/// it's operands or result.
245static bool isSafeOverflow(Instruction *I) {
Sam Parker8c4b9642018-08-10 13:57:13 +0000246 // FIXME Do we need NSW too?
Sam Parker3828c6f2018-07-23 12:27:47 +0000247 if (isa<OverflowingBinaryOperator>(I) && I->hasNoUnsignedWrap())
248 return true;
249
Sam Parker75aca942018-09-26 10:56:00 +0000250 // We can support a, potentially, overflowing instruction (I) if:
251 // - It is only used by an unsigned icmp.
252 // - The icmp uses a constant.
253 // - The overflowing value (I) is decreasing, i.e would underflow - wrapping
254 // around zero to become a larger number than before.
255 // - The underflowing instruction (I) also uses a constant.
256 //
257 // We can then use the two constants to calculate whether the result would
258 // wrap in respect to itself in the original bitwidth. If it doesn't wrap,
259 // just underflows the range, the icmp would give the same result whether the
260 // result has been truncated or not. We calculate this by:
261 // - Zero extending both constants, if needed, to 32-bits.
262 // - Take the absolute value of I's constant, adding this to the icmp const.
263 // - Check that this value is not out of range for small type. If it is, it
264 // means that it has underflowed enough to wrap around the icmp constant.
265 //
266 // For example:
267 //
268 // %sub = sub i8 %a, 2
269 // %cmp = icmp ule i8 %sub, 254
270 //
271 // If %a = 0, %sub = -2 == FE == 254
272 // But if this is evalulated as a i32
273 // %sub = -2 == FF FF FF FE == 4294967294
274 // So the unsigned compares (i8 and i32) would not yield the same result.
275 //
276 // Another way to look at it is:
277 // %a - 2 <= 254
278 // %a + 2 <= 254 + 2
279 // %a <= 256
280 // And we can't represent 256 in the i8 format, so we don't support it.
281 //
282 // Whereas:
283 //
284 // %sub i8 %a, 1
285 // %cmp = icmp ule i8 %sub, 254
286 //
287 // If %a = 0, %sub = -1 == FF == 255
288 // As i32:
289 // %sub = -1 == FF FF FF FF == 4294967295
290 //
291 // In this case, the unsigned compare results would be the same and this
292 // would also be true for ult, uge and ugt:
293 // - (255 < 254) == (0xFFFFFFFF < 254) == false
294 // - (255 <= 254) == (0xFFFFFFFF <= 254) == false
295 // - (255 > 254) == (0xFFFFFFFF > 254) == true
296 // - (255 >= 254) == (0xFFFFFFFF >= 254) == true
297 //
298 // To demonstrate why we can't handle increasing values:
299 //
300 // %add = add i8 %a, 2
301 // %cmp = icmp ult i8 %add, 127
302 //
303 // If %a = 254, %add = 256 == (i8 1)
304 // As i32:
305 // %add = 256
306 //
307 // (1 < 127) != (256 < 127)
308
Sam Parker3828c6f2018-07-23 12:27:47 +0000309 unsigned Opc = I->getOpcode();
Sam Parker75aca942018-09-26 10:56:00 +0000310 if (Opc != Instruction::Add && Opc != Instruction::Sub)
311 return false;
312
313 if (!I->hasOneUse() ||
314 !isa<ICmpInst>(*I->user_begin()) ||
315 !isa<ConstantInt>(I->getOperand(1)))
316 return false;
317
318 ConstantInt *OverflowConst = cast<ConstantInt>(I->getOperand(1));
319 bool NegImm = OverflowConst->isNegative();
320 bool IsDecreasing = ((Opc == Instruction::Sub) && !NegImm) ||
321 ((Opc == Instruction::Add) && NegImm);
322 if (!IsDecreasing)
323 return false;
324
325 // Don't support an icmp that deals with sign bits.
326 auto *CI = cast<ICmpInst>(*I->user_begin());
327 if (CI->isSigned() || CI->isEquality())
328 return false;
329
330 ConstantInt *ICmpConst = nullptr;
331 if (auto *Const = dyn_cast<ConstantInt>(CI->getOperand(0)))
332 ICmpConst = Const;
333 else if (auto *Const = dyn_cast<ConstantInt>(CI->getOperand(1)))
334 ICmpConst = Const;
335 else
336 return false;
337
338 // Now check that the result can't wrap on itself.
339 APInt Total = ICmpConst->getValue().getBitWidth() < 32 ?
340 ICmpConst->getValue().zext(32) : ICmpConst->getValue();
341
342 Total += OverflowConst->getValue().getBitWidth() < 32 ?
343 OverflowConst->getValue().abs().zext(32) : OverflowConst->getValue().abs();
344
345 APInt Max = APInt::getAllOnesValue(ARMCodeGenPrepare::TypeSize);
346
347 if (Total.getBitWidth() > Max.getBitWidth()) {
348 if (Total.ugt(Max.zext(Total.getBitWidth())))
Sam Parker3828c6f2018-07-23 12:27:47 +0000349 return false;
Sam Parker75aca942018-09-26 10:56:00 +0000350 } else if (Max.getBitWidth() > Total.getBitWidth()) {
351 if (Total.zext(Max.getBitWidth()).ugt(Max))
Sam Parker3828c6f2018-07-23 12:27:47 +0000352 return false;
Sam Parker75aca942018-09-26 10:56:00 +0000353 } else if (Total.ugt(Max))
354 return false;
Sam Parker3828c6f2018-07-23 12:27:47 +0000355
Sam Parker75aca942018-09-26 10:56:00 +0000356 LLVM_DEBUG(dbgs() << "ARM CGP: Allowing safe overflow for " << *I << "\n");
357 return true;
Sam Parker3828c6f2018-07-23 12:27:47 +0000358}
359
360static bool shouldPromote(Value *V) {
Sam Parkeraaec3c62018-09-13 15:14:12 +0000361 if (!isa<IntegerType>(V->getType()) || isSink(V))
Sam Parker8c4b9642018-08-10 13:57:13 +0000362 return false;
363
364 if (isSource(V))
365 return true;
366
Sam Parker3828c6f2018-07-23 12:27:47 +0000367 auto *I = dyn_cast<Instruction>(V);
368 if (!I)
369 return false;
370
Sam Parker8c4b9642018-08-10 13:57:13 +0000371 if (isa<ICmpInst>(I))
Sam Parker3828c6f2018-07-23 12:27:47 +0000372 return false;
373
Sam Parker3828c6f2018-07-23 12:27:47 +0000374 return true;
375}
376
377/// Return whether we can safely mutate V's type to ExtTy without having to be
378/// concerned with zero extending or truncation.
379static bool isPromotedResultSafe(Value *V) {
380 if (!isa<Instruction>(V))
381 return true;
382
Sjoerd Meijer31239a42018-08-17 07:34:01 +0000383 if (generateSignBits(V))
Sam Parker3828c6f2018-07-23 12:27:47 +0000384 return false;
385
386 // If I is only being used by something that will require its value to be
387 // truncated, then we don't care about the promoted result.
388 auto *I = cast<Instruction>(V);
389 if (I->hasOneUse() && isSink(*I->use_begin()))
390 return true;
391
392 if (isa<OverflowingBinaryOperator>(I))
393 return isSafeOverflow(I);
394 return true;
395}
396
397/// Return the intrinsic for the instruction that can perform the same
398/// operation but on a narrow type. This is using the parallel dsp intrinsics
399/// on scalar values.
Sam Parker8c4b9642018-08-10 13:57:13 +0000400static Intrinsic::ID getNarrowIntrinsic(Instruction *I) {
Sam Parker3828c6f2018-07-23 12:27:47 +0000401 // Whether we use the signed or unsigned versions of these intrinsics
402 // doesn't matter because we're not using the GE bits that they set in
403 // the APSR.
404 switch(I->getOpcode()) {
405 default:
406 break;
407 case Instruction::Add:
Sam Parker8c4b9642018-08-10 13:57:13 +0000408 return ARMCodeGenPrepare::TypeSize == 16 ? Intrinsic::arm_uadd16 :
Sam Parker3828c6f2018-07-23 12:27:47 +0000409 Intrinsic::arm_uadd8;
410 case Instruction::Sub:
Sam Parker8c4b9642018-08-10 13:57:13 +0000411 return ARMCodeGenPrepare::TypeSize == 16 ? Intrinsic::arm_usub16 :
Sam Parker3828c6f2018-07-23 12:27:47 +0000412 Intrinsic::arm_usub8;
413 }
414 llvm_unreachable("unhandled opcode for narrow intrinsic");
415}
416
417void IRPromoter::Mutate(Type *OrigTy,
418 SmallPtrSetImpl<Value*> &Visited,
Sjoerd Meijer31239a42018-08-17 07:34:01 +0000419 SmallPtrSetImpl<Value*> &Sources,
420 SmallPtrSetImpl<Instruction*> &Sinks) {
Sam Parker3828c6f2018-07-23 12:27:47 +0000421 IRBuilder<> Builder{Ctx};
422 Type *ExtTy = Type::getInt32Ty(M->getContext());
Sam Parker3828c6f2018-07-23 12:27:47 +0000423 SmallPtrSet<Value*, 8> Promoted;
Sam Parker8c4b9642018-08-10 13:57:13 +0000424 LLVM_DEBUG(dbgs() << "ARM CGP: Promoting use-def chains to from "
425 << ARMCodeGenPrepare::TypeSize << " to 32-bits\n");
Sam Parker3828c6f2018-07-23 12:27:47 +0000426
Sam Parker13567db2018-08-16 10:05:39 +0000427 // Cache original types.
428 DenseMap<Value*, Type*> TruncTysMap;
429 for (auto *V : Visited)
430 TruncTysMap[V] = V->getType();
431
Sam Parker3828c6f2018-07-23 12:27:47 +0000432 auto ReplaceAllUsersOfWith = [&](Value *From, Value *To) {
433 SmallVector<Instruction*, 4> Users;
434 Instruction *InstTo = dyn_cast<Instruction>(To);
435 for (Use &U : From->uses()) {
436 auto *User = cast<Instruction>(U.getUser());
437 if (InstTo && User->isIdenticalTo(InstTo))
438 continue;
439 Users.push_back(User);
440 }
441
Sam Parkeraaec3c62018-09-13 15:14:12 +0000442 for (auto *U : Users)
Sam Parker3828c6f2018-07-23 12:27:47 +0000443 U->replaceUsesOfWith(From, To);
444 };
445
446 auto FixConst = [&](ConstantInt *Const, Instruction *I) {
Sam Parker96f77f12018-09-13 14:48:10 +0000447 Constant *NewConst = isSafeOverflow(I) && Const->isNegative() ?
448 ConstantExpr::getSExt(Const, ExtTy) :
449 ConstantExpr::getZExt(Const, ExtTy);
Sam Parker3828c6f2018-07-23 12:27:47 +0000450 I->replaceUsesOfWith(Const, NewConst);
451 };
452
453 auto InsertDSPIntrinsic = [&](Instruction *I) {
454 LLVM_DEBUG(dbgs() << "ARM CGP: Inserting DSP intrinsic for "
455 << *I << "\n");
456 Function *DSPInst =
Sam Parker8c4b9642018-08-10 13:57:13 +0000457 Intrinsic::getDeclaration(M, getNarrowIntrinsic(I));
Sam Parker3828c6f2018-07-23 12:27:47 +0000458 Builder.SetInsertPoint(I);
459 Builder.SetCurrentDebugLocation(I->getDebugLoc());
460 Value *Args[] = { I->getOperand(0), I->getOperand(1) };
461 CallInst *Call = Builder.CreateCall(DSPInst, Args);
462 ReplaceAllUsersOfWith(I, Call);
463 InstsToRemove.push_back(I);
464 NewInsts.insert(Call);
Sam Parker13567db2018-08-16 10:05:39 +0000465 TruncTysMap[Call] = OrigTy;
Sam Parker3828c6f2018-07-23 12:27:47 +0000466 };
467
468 auto InsertZExt = [&](Value *V, Instruction *InsertPt) {
469 LLVM_DEBUG(dbgs() << "ARM CGP: Inserting ZExt for " << *V << "\n");
470 Builder.SetInsertPoint(InsertPt);
471 if (auto *I = dyn_cast<Instruction>(V))
472 Builder.SetCurrentDebugLocation(I->getDebugLoc());
473 auto *ZExt = cast<Instruction>(Builder.CreateZExt(V, ExtTy));
474 if (isa<Argument>(V))
475 ZExt->moveBefore(InsertPt);
476 else
477 ZExt->moveAfter(InsertPt);
478 ReplaceAllUsersOfWith(V, ZExt);
479 NewInsts.insert(ZExt);
Sam Parker13567db2018-08-16 10:05:39 +0000480 TruncTysMap[ZExt] = TruncTysMap[V];
Sam Parker3828c6f2018-07-23 12:27:47 +0000481 };
482
Sjoerd Meijer31239a42018-08-17 07:34:01 +0000483 // First, insert extending instructions between the sources and their users.
484 LLVM_DEBUG(dbgs() << "ARM CGP: Promoting sources:\n");
485 for (auto V : Sources) {
Sam Parker3828c6f2018-07-23 12:27:47 +0000486 LLVM_DEBUG(dbgs() << " - " << *V << "\n");
Sam Parker8c4b9642018-08-10 13:57:13 +0000487 if (auto *I = dyn_cast<Instruction>(V))
Sam Parker3828c6f2018-07-23 12:27:47 +0000488 InsertZExt(I, I);
489 else if (auto *Arg = dyn_cast<Argument>(V)) {
490 BasicBlock &BB = Arg->getParent()->front();
491 InsertZExt(Arg, &*BB.getFirstInsertionPt());
492 } else {
Sjoerd Meijer31239a42018-08-17 07:34:01 +0000493 llvm_unreachable("unhandled source that needs extending");
Sam Parker3828c6f2018-07-23 12:27:47 +0000494 }
495 Promoted.insert(V);
496 }
497
498 LLVM_DEBUG(dbgs() << "ARM CGP: Mutating the tree..\n");
499 // Then mutate the types of the instructions within the tree. Here we handle
500 // constant operands.
501 for (auto *V : Visited) {
Sjoerd Meijer31239a42018-08-17 07:34:01 +0000502 if (Sources.count(V))
Sam Parker3828c6f2018-07-23 12:27:47 +0000503 continue;
504
Sam Parker3828c6f2018-07-23 12:27:47 +0000505 auto *I = cast<Instruction>(V);
Sjoerd Meijer31239a42018-08-17 07:34:01 +0000506 if (Sinks.count(I))
Sam Parker3828c6f2018-07-23 12:27:47 +0000507 continue;
508
Sam Parker7def86b2018-08-15 07:52:35 +0000509 for (unsigned i = 0, e = I->getNumOperands(); i < e; ++i) {
510 Value *Op = I->getOperand(i);
511 if ((Op->getType() == ExtTy) || !isa<IntegerType>(Op->getType()))
Sam Parker3828c6f2018-07-23 12:27:47 +0000512 continue;
513
Sam Parker7def86b2018-08-15 07:52:35 +0000514 if (auto *Const = dyn_cast<ConstantInt>(Op))
Sam Parker3828c6f2018-07-23 12:27:47 +0000515 FixConst(Const, I);
Sam Parker7def86b2018-08-15 07:52:35 +0000516 else if (isa<UndefValue>(Op))
517 I->setOperand(i, UndefValue::get(ExtTy));
Sam Parker3828c6f2018-07-23 12:27:47 +0000518 }
519
520 if (shouldPromote(I)) {
521 I->mutateType(ExtTy);
522 Promoted.insert(I);
523 }
524 }
525
526 // Now we need to remove any zexts that have become unnecessary, as well
527 // as insert any intrinsics.
528 for (auto *V : Visited) {
Sjoerd Meijer31239a42018-08-17 07:34:01 +0000529 if (Sources.count(V))
Sam Parker3828c6f2018-07-23 12:27:47 +0000530 continue;
Sam Parker8c4b9642018-08-10 13:57:13 +0000531
Sam Parker3828c6f2018-07-23 12:27:47 +0000532 if (!shouldPromote(V) || isPromotedResultSafe(V))
533 continue;
534
Sam Parker75aca942018-09-26 10:56:00 +0000535 assert(EnableDSP && "DSP intrinisc insertion not enabled!");
536
Sam Parker3828c6f2018-07-23 12:27:47 +0000537 // Replace unsafe instructions with appropriate intrinsic calls.
538 InsertDSPIntrinsic(cast<Instruction>(V));
539 }
540
Sam Parker13567db2018-08-16 10:05:39 +0000541 auto InsertTrunc = [&](Value *V) -> Instruction* {
542 if (!isa<Instruction>(V) || !isa<IntegerType>(V->getType()))
543 return nullptr;
544
Sam Parker0e2f0bd2018-08-16 11:54:09 +0000545 if ((!Promoted.count(V) && !NewInsts.count(V)) || !TruncTysMap.count(V) ||
Sjoerd Meijer31239a42018-08-17 07:34:01 +0000546 Sources.count(V))
Sam Parker13567db2018-08-16 10:05:39 +0000547 return nullptr;
548
549 Type *TruncTy = TruncTysMap[V];
550 if (TruncTy == ExtTy)
551 return nullptr;
552
553 LLVM_DEBUG(dbgs() << "ARM CGP: Creating " << *TruncTy << " Trunc for "
554 << *V << "\n");
555 Builder.SetInsertPoint(cast<Instruction>(V));
556 auto *Trunc = cast<Instruction>(Builder.CreateTrunc(V, TruncTy));
557 NewInsts.insert(Trunc);
558 return Trunc;
559 };
560
Sjoerd Meijer31239a42018-08-17 07:34:01 +0000561 LLVM_DEBUG(dbgs() << "ARM CGP: Fixing up the sinks:\n");
Sam Parker3828c6f2018-07-23 12:27:47 +0000562 // Fix up any stores or returns that use the results of the promoted
563 // chain.
Sjoerd Meijer31239a42018-08-17 07:34:01 +0000564 for (auto I : Sinks) {
Sam Parker3828c6f2018-07-23 12:27:47 +0000565 LLVM_DEBUG(dbgs() << " - " << *I << "\n");
Sam Parker13567db2018-08-16 10:05:39 +0000566
567 // Handle calls separately as we need to iterate over arg operands.
568 if (auto *Call = dyn_cast<CallInst>(I)) {
569 for (unsigned i = 0; i < Call->getNumArgOperands(); ++i) {
570 Value *Arg = Call->getArgOperand(i);
571 if (Instruction *Trunc = InsertTrunc(Arg)) {
572 Trunc->moveBefore(Call);
573 Call->setArgOperand(i, Trunc);
574 }
575 }
576 continue;
Sam Parker3828c6f2018-07-23 12:27:47 +0000577 }
578
Sam Parker13567db2018-08-16 10:05:39 +0000579 // Now handle the others.
Sam Parker3828c6f2018-07-23 12:27:47 +0000580 for (unsigned i = 0; i < I->getNumOperands(); ++i) {
Sam Parker13567db2018-08-16 10:05:39 +0000581 if (Instruction *Trunc = InsertTrunc(I->getOperand(i))) {
582 Trunc->moveBefore(I);
583 I->setOperand(i, Trunc);
Sam Parker3828c6f2018-07-23 12:27:47 +0000584 }
585 }
586 }
Sam Parker0e2f0bd2018-08-16 11:54:09 +0000587 LLVM_DEBUG(dbgs() << "ARM CGP: Mutation complete:\n");
Sam Parkeraaec3c62018-09-13 15:14:12 +0000588 LLVM_DEBUG(dbgs();
589 for (auto *V : Sources)
590 V->dump();
591 for (auto *I : NewInsts)
592 I->dump();
593 for (auto *V : Visited) {
594 if (!Sources.count(V))
595 V->dump();
596 });
Sam Parker3828c6f2018-07-23 12:27:47 +0000597}
598
Sam Parker8c4b9642018-08-10 13:57:13 +0000599/// We accept most instructions, as well as Arguments and ConstantInsts. We
600/// Disallow casts other than zext and truncs and only allow calls if their
601/// return value is zeroext. We don't allow opcodes that can introduce sign
602/// bits.
603bool ARMCodeGenPrepare::isSupportedValue(Value *V) {
Sam Parker13567db2018-08-16 10:05:39 +0000604 if (isa<ICmpInst>(V))
605 return true;
Sam Parker8c4b9642018-08-10 13:57:13 +0000606
Volodymyr Sapsai703ab842018-09-18 00:11:55 +0000607 // Memory instructions
608 if (isa<StoreInst>(V) || isa<GetElementPtrInst>(V))
609 return true;
610
611 // Branches and targets.
612 if( isa<BranchInst>(V) || isa<SwitchInst>(V) || isa<BasicBlock>(V))
613 return true;
614
615 // Non-instruction values that we can handle.
616 if ((isa<Constant>(V) && !isa<ConstantExpr>(V)) || isa<Argument>(V))
617 return isSupportedType(V);
618
619 if (isa<PHINode>(V) || isa<SelectInst>(V) || isa<ReturnInst>(V) ||
620 isa<LoadInst>(V))
621 return isSupportedType(V);
622
623 // Truncs can be either sources or sinks.
624 if (auto *Trunc = dyn_cast<TruncInst>(V))
625 return isSupportedType(Trunc) || isSupportedType(Trunc->getOperand(0));
626
627 if (isa<CastInst>(V) && !isa<SExtInst>(V))
628 return isSupportedType(cast<CastInst>(V)->getOperand(0));
Sam Parker0e2f0bd2018-08-16 11:54:09 +0000629
Sam Parker8c4b9642018-08-10 13:57:13 +0000630 // Special cases for calls as we need to check for zeroext
631 // TODO We should accept calls even if they don't have zeroext, as they can
Sjoerd Meijer31239a42018-08-17 07:34:01 +0000632 // still be sinks.
Sam Parker8c4b9642018-08-10 13:57:13 +0000633 if (auto *Call = dyn_cast<CallInst>(V))
634 return isSupportedType(Call) &&
635 Call->hasRetAttr(Attribute::AttrKind::ZExt);
636
Volodymyr Sapsai703ab842018-09-18 00:11:55 +0000637 if (!isa<BinaryOperator>(V))
638 return false;
639
640 if (!isSupportedType(V))
641 return false;
642
643 if (generateSignBits(V)) {
644 LLVM_DEBUG(dbgs() << "ARM CGP: No, instruction can generate sign bits.\n");
645 return false;
646 }
647 return true;
Sam Parker8c4b9642018-08-10 13:57:13 +0000648}
649
650/// Check that the type of V would be promoted and that the original type is
651/// smaller than the targeted promoted type. Check that we're not trying to
652/// promote something larger than our base 'TypeSize' type.
653bool ARMCodeGenPrepare::isLegalToPromote(Value *V) {
654 if (isPromotedResultSafe(V))
655 return true;
656
657 auto *I = dyn_cast<Instruction>(V);
658 if (!I)
659 return false;
660
661 // If promotion is not safe, can we use a DSP instruction to natively
662 // handle the narrow type?
Sam Parker3828c6f2018-07-23 12:27:47 +0000663 if (!ST->hasDSP() || !EnableDSP || !isSupportedType(I))
664 return false;
665
666 if (ST->isThumb() && !ST->hasThumb2())
667 return false;
668
669 if (I->getOpcode() != Instruction::Add && I->getOpcode() != Instruction::Sub)
670 return false;
671
672 // TODO
673 // Would it be profitable? For Thumb code, these parallel DSP instructions
674 // are only Thumb-2, so we wouldn't be able to dual issue on Cortex-M33. For
675 // Cortex-A, specifically Cortex-A72, the latency is double and throughput is
676 // halved. They also do not take immediates as operands.
677 for (auto &Op : I->operands()) {
678 if (isa<Constant>(Op)) {
679 if (!EnableDSPWithImms)
680 return false;
681 }
682 }
683 return true;
684}
685
Sam Parker3828c6f2018-07-23 12:27:47 +0000686bool ARMCodeGenPrepare::TryToPromote(Value *V) {
687 OrigTy = V->getType();
688 TypeSize = OrigTy->getPrimitiveSizeInBits();
Sam Parkerfabf7fe2018-08-15 13:29:50 +0000689 if (TypeSize > 16 || TypeSize < 8)
Sam Parker8c4b9642018-08-10 13:57:13 +0000690 return false;
Sam Parker3828c6f2018-07-23 12:27:47 +0000691
692 if (!isSupportedValue(V) || !shouldPromote(V) || !isLegalToPromote(V))
693 return false;
694
Sam Parker8c4b9642018-08-10 13:57:13 +0000695 LLVM_DEBUG(dbgs() << "ARM CGP: TryToPromote: " << *V << ", TypeSize = "
696 << TypeSize << "\n");
Sam Parker3828c6f2018-07-23 12:27:47 +0000697
698 SetVector<Value*> WorkList;
Sjoerd Meijer31239a42018-08-17 07:34:01 +0000699 SmallPtrSet<Value*, 8> Sources;
700 SmallPtrSet<Instruction*, 4> Sinks;
Sam Parker3828c6f2018-07-23 12:27:47 +0000701 WorkList.insert(V);
702 SmallPtrSet<Value*, 16> CurrentVisited;
703 CurrentVisited.clear();
704
Sjoerd Meijer31239a42018-08-17 07:34:01 +0000705 // Return true if V was added to the worklist as a supported instruction,
706 // if it was already visited, or if we don't need to explore it (e.g.
707 // pointer values and GEPs), and false otherwise.
Sam Parker3828c6f2018-07-23 12:27:47 +0000708 auto AddLegalInst = [&](Value *V) {
709 if (CurrentVisited.count(V))
710 return true;
711
Sam Parker0d511972018-08-16 12:24:40 +0000712 // Ignore GEPs because they don't need promoting and the constant indices
713 // will prevent the transformation.
714 if (isa<GetElementPtrInst>(V))
715 return true;
716
Sam Parker3828c6f2018-07-23 12:27:47 +0000717 if (!isSupportedValue(V) || (shouldPromote(V) && !isLegalToPromote(V))) {
718 LLVM_DEBUG(dbgs() << "ARM CGP: Can't handle: " << *V << "\n");
719 return false;
720 }
721
722 WorkList.insert(V);
723 return true;
724 };
725
726 // Iterate through, and add to, a tree of operands and users in the use-def.
727 while (!WorkList.empty()) {
728 Value *V = WorkList.back();
729 WorkList.pop_back();
730 if (CurrentVisited.count(V))
731 continue;
732
Sam Parker7def86b2018-08-15 07:52:35 +0000733 // Ignore non-instructions, other than arguments.
Sam Parker3828c6f2018-07-23 12:27:47 +0000734 if (!isa<Instruction>(V) && !isSource(V))
735 continue;
736
737 // If we've already visited this value from somewhere, bail now because
738 // the tree has already been explored.
739 // TODO: This could limit the transform, ie if we try to promote something
740 // from an i8 and fail first, before trying an i16.
Sam Parkeraaec3c62018-09-13 15:14:12 +0000741 if (AllVisited.count(V))
Sam Parker3828c6f2018-07-23 12:27:47 +0000742 return false;
Sam Parker3828c6f2018-07-23 12:27:47 +0000743
744 CurrentVisited.insert(V);
745 AllVisited.insert(V);
746
747 // Calls can be both sources and sinks.
748 if (isSink(V))
Sjoerd Meijer31239a42018-08-17 07:34:01 +0000749 Sinks.insert(cast<Instruction>(V));
Sam Parker3828c6f2018-07-23 12:27:47 +0000750 if (isSource(V))
Sjoerd Meijer31239a42018-08-17 07:34:01 +0000751 Sources.insert(V);
Sam Parker3828c6f2018-07-23 12:27:47 +0000752 else if (auto *I = dyn_cast<Instruction>(V)) {
753 // Visit operands of any instruction visited.
754 for (auto &U : I->operands()) {
755 if (!AddLegalInst(U))
756 return false;
757 }
758 }
759
760 // Don't visit users of a node which isn't going to be mutated unless its a
761 // source.
762 if (isSource(V) || shouldPromote(V)) {
763 for (Use &U : V->uses()) {
764 if (!AddLegalInst(U.getUser()))
765 return false;
766 }
767 }
768 }
769
Sam Parker3828c6f2018-07-23 12:27:47 +0000770 LLVM_DEBUG(dbgs() << "ARM CGP: Visited nodes:\n";
771 for (auto *I : CurrentVisited)
772 I->dump();
773 );
Sam Parker7def86b2018-08-15 07:52:35 +0000774 unsigned ToPromote = 0;
775 for (auto *V : CurrentVisited) {
Sjoerd Meijer31239a42018-08-17 07:34:01 +0000776 if (Sources.count(V))
Sam Parker7def86b2018-08-15 07:52:35 +0000777 continue;
Sjoerd Meijer31239a42018-08-17 07:34:01 +0000778 if (Sinks.count(cast<Instruction>(V)))
Sam Parker7def86b2018-08-15 07:52:35 +0000779 continue;
780 ++ToPromote;
781 }
782
783 if (ToPromote < 2)
784 return false;
Sam Parker3828c6f2018-07-23 12:27:47 +0000785
Sjoerd Meijer31239a42018-08-17 07:34:01 +0000786 Promoter->Mutate(OrigTy, CurrentVisited, Sources, Sinks);
Sam Parker3828c6f2018-07-23 12:27:47 +0000787 return true;
788}
789
790bool ARMCodeGenPrepare::doInitialization(Module &M) {
791 Promoter = new IRPromoter(&M);
792 return false;
793}
794
795bool ARMCodeGenPrepare::runOnFunction(Function &F) {
796 if (skipFunction(F) || DisableCGP)
797 return false;
798
799 auto *TPC = &getAnalysis<TargetPassConfig>();
800 if (!TPC)
801 return false;
802
803 const TargetMachine &TM = TPC->getTM<TargetMachine>();
804 ST = &TM.getSubtarget<ARMSubtarget>(F);
805 bool MadeChange = false;
806 LLVM_DEBUG(dbgs() << "ARM CGP: Running on " << F.getName() << "\n");
807
808 // Search up from icmps to try to promote their operands.
809 for (BasicBlock &BB : F) {
810 auto &Insts = BB.getInstList();
811 for (auto &I : Insts) {
812 if (AllVisited.count(&I))
813 continue;
814
815 if (isa<ICmpInst>(I)) {
816 auto &CI = cast<ICmpInst>(I);
817
818 // Skip signed or pointer compares
819 if (CI.isSigned() || !isa<IntegerType>(CI.getOperand(0)->getType()))
820 continue;
821
Sam Parker3828c6f2018-07-23 12:27:47 +0000822 for (auto &Op : CI.operands()) {
Sam Parker8c4b9642018-08-10 13:57:13 +0000823 if (auto *I = dyn_cast<Instruction>(Op))
824 MadeChange |= TryToPromote(I);
Sam Parker3828c6f2018-07-23 12:27:47 +0000825 }
826 }
827 }
828 Promoter->Cleanup();
829 LLVM_DEBUG(if (verifyFunction(F, &dbgs())) {
830 dbgs();
831 report_fatal_error("Broken function after type promotion");
832 });
833 }
834 if (MadeChange)
835 LLVM_DEBUG(dbgs() << "After ARMCodeGenPrepare: " << F << "\n");
836
837 return MadeChange;
838}
839
Matt Morehousea70685f2018-07-23 17:00:45 +0000840bool ARMCodeGenPrepare::doFinalization(Module &M) {
841 delete Promoter;
842 return false;
843}
844
Sam Parker3828c6f2018-07-23 12:27:47 +0000845INITIALIZE_PASS_BEGIN(ARMCodeGenPrepare, DEBUG_TYPE,
846 "ARM IR optimizations", false, false)
847INITIALIZE_PASS_END(ARMCodeGenPrepare, DEBUG_TYPE, "ARM IR optimizations",
848 false, false)
849
850char ARMCodeGenPrepare::ID = 0;
Sam Parker8c4b9642018-08-10 13:57:13 +0000851unsigned ARMCodeGenPrepare::TypeSize = 0;
Sam Parker3828c6f2018-07-23 12:27:47 +0000852
853FunctionPass *llvm::createARMCodeGenPreparePass() {
854 return new ARMCodeGenPrepare();
855}